blob: 41013c415b88a71a5aefd98ee59638a139969918 [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
Alexey Bataeva769e072013-03-22 06:34:35 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// \brief This file implements semantic analysis for OpenMP directives and
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000011/// clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000012///
13//===----------------------------------------------------------------------===//
14
Alexey Bataevb08f89f2015-08-14 12:25:37 +000015#include "TreeTransform.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000016#include "clang/AST/ASTContext.h"
Alexey Bataev97720002014-11-11 04:05:39 +000017#include "clang/AST/ASTMutationListener.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000018#include "clang/AST/Decl.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000019#include "clang/AST/DeclCXX.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000020#include "clang/AST/DeclOpenMP.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000021#include "clang/AST/StmtCXX.h"
22#include "clang/AST/StmtOpenMP.h"
23#include "clang/AST/StmtVisitor.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000024#include "clang/Basic/OpenMPKinds.h"
Samuel Antaof8b50122015-07-13 22:54:53 +000025#include "clang/Basic/TargetInfo.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000026#include "clang/Lex/Preprocessor.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000027#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000028#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000029#include "clang/Sema/Scope.h"
30#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000031#include "clang/Sema/SemaInternal.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000032using namespace clang;
33
Alexey Bataev758e55e2013-09-06 18:03:48 +000034//===----------------------------------------------------------------------===//
35// Stack of data-sharing attributes for variables
36//===----------------------------------------------------------------------===//
37
38namespace {
39/// \brief Default data sharing attributes, which can be applied to directive.
40enum DefaultDataSharingAttributes {
Alexey Bataeved09d242014-05-28 05:53:51 +000041 DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
42 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
43 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'.
Alexey Bataev758e55e2013-09-06 18:03:48 +000044};
Alexey Bataev7ff55242014-06-19 09:13:45 +000045
Alexey Bataevf29276e2014-06-18 04:14:57 +000046template <class T> struct MatchesAny {
Alexey Bataev23b69422014-06-18 07:08:49 +000047 explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {}
Alexey Bataevf29276e2014-06-18 04:14:57 +000048 bool operator()(T Kind) {
49 for (auto KindEl : Arr)
50 if (KindEl == Kind)
51 return true;
52 return false;
53 }
54
55private:
56 ArrayRef<T> Arr;
57};
Alexey Bataev23b69422014-06-18 07:08:49 +000058struct MatchesAlways {
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000059 MatchesAlways() {}
Alexey Bataev7ff55242014-06-19 09:13:45 +000060 template <class T> bool operator()(T) { return true; }
Alexey Bataevf29276e2014-06-18 04:14:57 +000061};
62
63typedef MatchesAny<OpenMPClauseKind> MatchesAnyClause;
64typedef MatchesAny<OpenMPDirectiveKind> MatchesAnyDirective;
Alexey Bataev758e55e2013-09-06 18:03:48 +000065
66/// \brief Stack for tracking declarations used in OpenMP directives and
67/// clauses and their data-sharing attributes.
68class DSAStackTy {
69public:
70 struct DSAVarData {
71 OpenMPDirectiveKind DKind;
72 OpenMPClauseKind CKind;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000073 Expr *RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +000074 DeclRefExpr *PrivateCopy;
Alexey Bataevbae9a792014-06-27 10:37:06 +000075 SourceLocation ImplicitDSALoc;
76 DSAVarData()
77 : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr),
Alexey Bataev90c228f2016-02-08 09:29:13 +000078 PrivateCopy(nullptr), ImplicitDSALoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000079 };
Alexey Bataeved09d242014-05-28 05:53:51 +000080
Alexey Bataev758e55e2013-09-06 18:03:48 +000081private:
Samuel Antao5de996e2016-01-22 20:21:36 +000082 typedef SmallVector<Expr *, 4> MapInfo;
83
Alexey Bataev758e55e2013-09-06 18:03:48 +000084 struct DSAInfo {
85 OpenMPClauseKind Attributes;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000086 Expr *RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +000087 DeclRefExpr *PrivateCopy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000088 };
Alexey Bataev90c228f2016-02-08 09:29:13 +000089 typedef llvm::DenseMap<ValueDecl *, DSAInfo> DeclSAMapTy;
90 typedef llvm::DenseMap<ValueDecl *, Expr *> AlignedMapTy;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000091 typedef llvm::DenseMap<ValueDecl *, unsigned> LoopControlVariablesMapTy;
Alexey Bataev90c228f2016-02-08 09:29:13 +000092 typedef llvm::DenseMap<ValueDecl *, MapInfo> MappedDeclsTy;
Alexey Bataev28c75412015-12-15 08:19:24 +000093 typedef llvm::StringMap<std::pair<OMPCriticalDirective *, llvm::APSInt>>
94 CriticalsWithHintsTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000095
96 struct SharingMapTy {
97 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000098 AlignedMapTy AlignedMap;
Kelvin Li0bff7af2015-11-23 05:32:03 +000099 MappedDeclsTy MappedDecls;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000100 LoopControlVariablesMapTy LCVMap;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000101 DefaultDataSharingAttributes DefaultAttr;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000102 SourceLocation DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000103 OpenMPDirectiveKind Directive;
104 DeclarationNameInfo DirectiveName;
105 Scope *CurScope;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000106 SourceLocation ConstructLoc;
Alexey Bataev346265e2015-09-25 10:37:12 +0000107 /// \brief first argument (Expr *) contains optional argument of the
108 /// 'ordered' clause, the second one is true if the regions has 'ordered'
109 /// clause, false otherwise.
110 llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000111 bool NowaitRegion;
Alexey Bataev25e5b442015-09-15 12:52:43 +0000112 bool CancelRegion;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000113 unsigned AssociatedLoops;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000114 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +0000115 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000116 Scope *CurScope, SourceLocation Loc)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000117 : SharingMap(), AlignedMap(), LCVMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000118 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
Alexey Bataev346265e2015-09-25 10:37:12 +0000119 ConstructLoc(Loc), OrderedRegion(), NowaitRegion(false),
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000120 CancelRegion(false), AssociatedLoops(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000121 SharingMapTy()
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000122 : SharingMap(), AlignedMap(), LCVMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000123 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
Alexey Bataev346265e2015-09-25 10:37:12 +0000124 ConstructLoc(), OrderedRegion(), NowaitRegion(false),
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000125 CancelRegion(false), AssociatedLoops(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000126 };
127
Axel Naumann323862e2016-02-03 10:45:22 +0000128 typedef SmallVector<SharingMapTy, 4> StackTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000129
130 /// \brief Stack of used declaration and their data-sharing attributes.
131 StackTy Stack;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000132 /// \brief true, if check for DSA must be from parent directive, false, if
133 /// from current directive.
Alexey Bataevaac108a2015-06-23 04:51:00 +0000134 OpenMPClauseKind ClauseKindMode;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000135 Sema &SemaRef;
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000136 bool ForceCapturing;
Alexey Bataev28c75412015-12-15 08:19:24 +0000137 CriticalsWithHintsTy Criticals;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000138
139 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
140
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000141 DSAVarData getDSA(StackTy::reverse_iterator Iter, ValueDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000142
143 /// \brief Checks if the variable is a local for OpenMP region.
144 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000145
Alexey Bataev758e55e2013-09-06 18:03:48 +0000146public:
Alexey Bataevaac108a2015-06-23 04:51:00 +0000147 explicit DSAStackTy(Sema &S)
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000148 : Stack(1), ClauseKindMode(OMPC_unknown), SemaRef(S),
149 ForceCapturing(false) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000150
Alexey Bataevaac108a2015-06-23 04:51:00 +0000151 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
152 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000153
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000154 bool isForceVarCapturing() const { return ForceCapturing; }
155 void setForceVarCapturing(bool V) { ForceCapturing = V; }
156
Alexey Bataev758e55e2013-09-06 18:03:48 +0000157 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000158 Scope *CurScope, SourceLocation Loc) {
159 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
160 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000161 }
162
163 void pop() {
164 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
165 Stack.pop_back();
166 }
167
Alexey Bataev28c75412015-12-15 08:19:24 +0000168 void addCriticalWithHint(OMPCriticalDirective *D, llvm::APSInt Hint) {
169 Criticals[D->getDirectiveName().getAsString()] = std::make_pair(D, Hint);
170 }
171 const std::pair<OMPCriticalDirective *, llvm::APSInt>
172 getCriticalWithHint(const DeclarationNameInfo &Name) const {
173 auto I = Criticals.find(Name.getAsString());
174 if (I != Criticals.end())
175 return I->second;
176 return std::make_pair(nullptr, llvm::APSInt());
177 }
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000178 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000179 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000180 /// for diagnostics.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000181 Expr *addUniqueAligned(ValueDecl *D, Expr *NewDE);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000182
Alexey Bataev9c821032015-04-30 04:23:23 +0000183 /// \brief Register specified variable as loop control variable.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000184 void addLoopControlVariable(ValueDecl *D);
Alexey Bataev9c821032015-04-30 04:23:23 +0000185 /// \brief Check if the specified variable is a loop control variable for
186 /// current region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000187 /// \return The index of the loop control variable in the list of associated
188 /// for-loops (from outer to inner).
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000189 unsigned isLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000190 /// \brief Check if the specified variable is a loop control variable for
191 /// parent region.
192 /// \return The index of the loop control variable in the list of associated
193 /// for-loops (from outer to inner).
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000194 unsigned isParentLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000195 /// \brief Get the loop control variable for the I-th loop (or nullptr) in
196 /// parent directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000197 ValueDecl *getParentLoopControlVariable(unsigned I);
Alexey Bataev9c821032015-04-30 04:23:23 +0000198
Alexey Bataev758e55e2013-09-06 18:03:48 +0000199 /// \brief Adds explicit data sharing attribute to the specified declaration.
Alexey Bataev90c228f2016-02-08 09:29:13 +0000200 void addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
201 DeclRefExpr *PrivateCopy = nullptr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000202
Alexey Bataev758e55e2013-09-06 18:03:48 +0000203 /// \brief Returns data sharing attributes from top of the stack for the
204 /// specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000205 DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000206 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000207 DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000208 /// \brief Checks if the specified variables has data-sharing attributes which
209 /// match specified \a CPred predicate in any directive which matches \a DPred
210 /// predicate.
211 template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000212 DSAVarData hasDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000213 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000214 /// \brief Checks if the specified variables has data-sharing attributes which
215 /// match specified \a CPred predicate in any innermost directive which
216 /// matches \a DPred predicate.
217 template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000218 DSAVarData hasInnermostDSA(ValueDecl *D, ClausesPredicate CPred,
219 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000220 /// \brief Checks if the specified variables has explicit data-sharing
221 /// attributes which match specified \a CPred predicate at the specified
222 /// OpenMP region.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000223 bool hasExplicitDSA(ValueDecl *D,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000224 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
225 unsigned Level);
Samuel Antao4be30e92015-10-02 17:14:03 +0000226
227 /// \brief Returns true if the directive at level \Level matches in the
228 /// specified \a DPred predicate.
229 bool hasExplicitDirective(
230 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
231 unsigned Level);
232
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000233 /// \brief Finds a directive which matches specified \a DPred predicate.
234 template <class NamedDirectivesPredicate>
235 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000236
Alexey Bataev758e55e2013-09-06 18:03:48 +0000237 /// \brief Returns currently analyzed directive.
238 OpenMPDirectiveKind getCurrentDirective() const {
239 return Stack.back().Directive;
240 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000241 /// \brief Returns parent directive.
242 OpenMPDirectiveKind getParentDirective() const {
243 if (Stack.size() > 2)
244 return Stack[Stack.size() - 2].Directive;
245 return OMPD_unknown;
246 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000247 /// \brief Return the directive associated with the provided scope.
248 OpenMPDirectiveKind getDirectiveForScope(const Scope *S) const;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000249
250 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000251 void setDefaultDSANone(SourceLocation Loc) {
252 Stack.back().DefaultAttr = DSA_none;
253 Stack.back().DefaultAttrLoc = Loc;
254 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000255 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000256 void setDefaultDSAShared(SourceLocation Loc) {
257 Stack.back().DefaultAttr = DSA_shared;
258 Stack.back().DefaultAttrLoc = Loc;
259 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000260
261 DefaultDataSharingAttributes getDefaultDSA() const {
262 return Stack.back().DefaultAttr;
263 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000264 SourceLocation getDefaultDSALocation() const {
265 return Stack.back().DefaultAttrLoc;
266 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000267
Alexey Bataevf29276e2014-06-18 04:14:57 +0000268 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000269 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000270 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000271 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000272 }
273
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000274 /// \brief Marks current region as ordered (it has an 'ordered' clause).
Alexey Bataev346265e2015-09-25 10:37:12 +0000275 void setOrderedRegion(bool IsOrdered, Expr *Param) {
276 Stack.back().OrderedRegion.setInt(IsOrdered);
277 Stack.back().OrderedRegion.setPointer(Param);
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000278 }
279 /// \brief Returns true, if parent region is ordered (has associated
280 /// 'ordered' clause), false - otherwise.
281 bool isParentOrderedRegion() const {
282 if (Stack.size() > 2)
Alexey Bataev346265e2015-09-25 10:37:12 +0000283 return Stack[Stack.size() - 2].OrderedRegion.getInt();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000284 return false;
285 }
Alexey Bataev346265e2015-09-25 10:37:12 +0000286 /// \brief Returns optional parameter for the ordered region.
287 Expr *getParentOrderedRegionParam() const {
288 if (Stack.size() > 2)
289 return Stack[Stack.size() - 2].OrderedRegion.getPointer();
290 return nullptr;
291 }
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000292 /// \brief Marks current region as nowait (it has a 'nowait' clause).
293 void setNowaitRegion(bool IsNowait = true) {
294 Stack.back().NowaitRegion = IsNowait;
295 }
296 /// \brief Returns true, if parent region is nowait (has associated
297 /// 'nowait' clause), false - otherwise.
298 bool isParentNowaitRegion() const {
299 if (Stack.size() > 2)
300 return Stack[Stack.size() - 2].NowaitRegion;
301 return false;
302 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000303 /// \brief Marks parent region as cancel region.
304 void setParentCancelRegion(bool Cancel = true) {
305 if (Stack.size() > 2)
306 Stack[Stack.size() - 2].CancelRegion =
307 Stack[Stack.size() - 2].CancelRegion || Cancel;
308 }
309 /// \brief Return true if current region has inner cancel construct.
310 bool isCancelRegion() const {
311 return Stack.back().CancelRegion;
312 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000313
Alexey Bataev9c821032015-04-30 04:23:23 +0000314 /// \brief Set collapse value for the region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000315 void setAssociatedLoops(unsigned Val) { Stack.back().AssociatedLoops = Val; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000316 /// \brief Return collapse value for region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000317 unsigned getAssociatedLoops() const { return Stack.back().AssociatedLoops; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000318
Alexey Bataev13314bf2014-10-09 04:18:56 +0000319 /// \brief Marks current target region as one with closely nested teams
320 /// region.
321 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
322 if (Stack.size() > 2)
323 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
324 }
325 /// \brief Returns true, if current region has closely nested teams region.
326 bool hasInnerTeamsRegion() const {
327 return getInnerTeamsRegionLoc().isValid();
328 }
329 /// \brief Returns location of the nested teams region (if any).
330 SourceLocation getInnerTeamsRegionLoc() const {
331 if (Stack.size() > 1)
332 return Stack.back().InnerTeamsRegionLoc;
333 return SourceLocation();
334 }
335
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000336 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000337 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000338 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000339
Samuel Antao5de996e2016-01-22 20:21:36 +0000340 // Do the check specified in MapInfoCheck and return true if any issue is
341 // found.
342 template <class MapInfoCheck>
343 bool checkMapInfoForVar(ValueDecl *VD, bool CurrentRegionOnly,
344 MapInfoCheck Check) {
345 auto SI = Stack.rbegin();
346 auto SE = Stack.rend();
347
348 if (SI == SE)
349 return false;
350
351 if (CurrentRegionOnly) {
352 SE = std::next(SI);
353 } else {
354 ++SI;
355 }
356
357 for (; SI != SE; ++SI) {
358 auto MI = SI->MappedDecls.find(VD);
359 if (MI != SI->MappedDecls.end()) {
360 for (Expr *E : MI->second) {
361 if (Check(E))
362 return true;
363 }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000364 }
365 }
Samuel Antao5de996e2016-01-22 20:21:36 +0000366 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000367 }
368
Samuel Antao5de996e2016-01-22 20:21:36 +0000369 void addExprToVarMapInfo(ValueDecl *VD, Expr *E) {
Kelvin Li0bff7af2015-11-23 05:32:03 +0000370 if (Stack.size() > 1) {
Samuel Antao5de996e2016-01-22 20:21:36 +0000371 Stack.back().MappedDecls[VD].push_back(E);
Kelvin Li0bff7af2015-11-23 05:32:03 +0000372 }
373 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000374};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000375bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
376 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
Alexey Bataev49f6e782015-12-01 04:18:41 +0000377 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000378 isOpenMPTaskLoopDirective(DKind);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000379}
Alexey Bataeved09d242014-05-28 05:53:51 +0000380} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000381
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000382static ValueDecl *getCanonicalDecl(ValueDecl *D) {
383 auto *VD = dyn_cast<VarDecl>(D);
384 auto *FD = dyn_cast<FieldDecl>(D);
385 if (VD != nullptr) {
386 VD = VD->getCanonicalDecl();
387 D = VD;
388 } else {
389 assert(FD);
390 FD = FD->getCanonicalDecl();
391 D = FD;
392 }
393 return D;
394}
395
Alexey Bataev758e55e2013-09-06 18:03:48 +0000396DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000397 ValueDecl *D) {
398 D = getCanonicalDecl(D);
399 auto *VD = dyn_cast<VarDecl>(D);
400 auto *FD = dyn_cast<FieldDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000401 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000402 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000403 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
404 // in a region but not in construct]
405 // File-scope or namespace-scope variables referenced in called routines
406 // in the region are shared unless they appear in a threadprivate
407 // directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000408 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000409 DVar.CKind = OMPC_shared;
410
411 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
412 // in a region but not in construct]
413 // Variables with static storage duration that are declared in called
414 // routines in the region are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000415 if (VD && VD->hasGlobalStorage())
416 DVar.CKind = OMPC_shared;
417
418 // Non-static data members are shared by default.
419 if (FD)
Alexey Bataev750a58b2014-03-18 12:19:12 +0000420 DVar.CKind = OMPC_shared;
421
Alexey Bataev758e55e2013-09-06 18:03:48 +0000422 return DVar;
423 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000424
Alexey Bataev758e55e2013-09-06 18:03:48 +0000425 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000426 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
427 // in a Construct, C/C++, predetermined, p.1]
428 // Variables with automatic storage duration that are declared in a scope
429 // inside the construct are private.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000430 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
431 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000432 DVar.CKind = OMPC_private;
433 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000434 }
435
Alexey Bataev758e55e2013-09-06 18:03:48 +0000436 // Explicitly specified attributes and local variables with predetermined
437 // attributes.
438 if (Iter->SharingMap.count(D)) {
439 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000440 DVar.PrivateCopy = Iter->SharingMap[D].PrivateCopy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000441 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000442 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000443 return DVar;
444 }
445
446 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
447 // in a Construct, C/C++, implicitly determined, p.1]
448 // In a parallel or task construct, the data-sharing attributes of these
449 // variables are determined by the default clause, if present.
450 switch (Iter->DefaultAttr) {
451 case DSA_shared:
452 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000453 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000454 return DVar;
455 case DSA_none:
456 return DVar;
457 case DSA_unspecified:
458 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
459 // in a Construct, implicitly determined, p.2]
460 // In a parallel construct, if no default clause is present, these
461 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000462 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000463 if (isOpenMPParallelDirective(DVar.DKind) ||
464 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000465 DVar.CKind = OMPC_shared;
466 return DVar;
467 }
468
469 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
470 // in a Construct, implicitly determined, p.4]
471 // In a task construct, if no default clause is present, a variable that in
472 // the enclosing context is determined to be shared by all implicit tasks
473 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000474 if (DVar.DKind == OMPD_task) {
475 DSAVarData DVarTemp;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000476 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000477 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000478 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
479 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000480 // in a Construct, implicitly determined, p.6]
481 // In a task construct, if no default clause is present, a variable
482 // whose data-sharing attribute is not determined by the rules above is
483 // firstprivate.
484 DVarTemp = getDSA(I, D);
485 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000486 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000487 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000488 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000489 return DVar;
490 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000491 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000492 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000493 }
494 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000495 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000496 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000497 return DVar;
498 }
499 }
500 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
501 // in a Construct, implicitly determined, p.3]
502 // For constructs other than task, if no default clause is present, these
503 // variables inherit their data-sharing attributes from the enclosing
504 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000505 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000506}
507
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000508Expr *DSAStackTy::addUniqueAligned(ValueDecl *D, Expr *NewDE) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000509 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000510 D = getCanonicalDecl(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000511 auto It = Stack.back().AlignedMap.find(D);
512 if (It == Stack.back().AlignedMap.end()) {
513 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
514 Stack.back().AlignedMap[D] = NewDE;
515 return nullptr;
516 } else {
517 assert(It->second && "Unexpected nullptr expr in the aligned map");
518 return It->second;
519 }
520 return nullptr;
521}
522
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000523void DSAStackTy::addLoopControlVariable(ValueDecl *D) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000524 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000525 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000526 Stack.back().LCVMap.insert(std::make_pair(D, Stack.back().LCVMap.size() + 1));
Alexey Bataev9c821032015-04-30 04:23:23 +0000527}
528
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000529unsigned DSAStackTy::isLoopControlVariable(ValueDecl *D) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000530 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000531 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000532 return Stack.back().LCVMap.count(D) > 0 ? Stack.back().LCVMap[D] : 0;
533}
534
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000535unsigned DSAStackTy::isParentLoopControlVariable(ValueDecl *D) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000536 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000537 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000538 return Stack[Stack.size() - 2].LCVMap.count(D) > 0
539 ? Stack[Stack.size() - 2].LCVMap[D]
540 : 0;
541}
542
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000543ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000544 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
545 if (Stack[Stack.size() - 2].LCVMap.size() < I)
546 return nullptr;
547 for (auto &Pair : Stack[Stack.size() - 2].LCVMap) {
548 if (Pair.second == I)
549 return Pair.first;
550 }
551 return nullptr;
Alexey Bataev9c821032015-04-30 04:23:23 +0000552}
553
Alexey Bataev90c228f2016-02-08 09:29:13 +0000554void DSAStackTy::addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
555 DeclRefExpr *PrivateCopy) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000556 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000557 if (A == OMPC_threadprivate) {
558 Stack[0].SharingMap[D].Attributes = A;
559 Stack[0].SharingMap[D].RefExpr = E;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000560 Stack[0].SharingMap[D].PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000561 } else {
562 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
563 Stack.back().SharingMap[D].Attributes = A;
564 Stack.back().SharingMap[D].RefExpr = E;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000565 Stack.back().SharingMap[D].PrivateCopy = PrivateCopy;
566 if (PrivateCopy)
567 addDSA(PrivateCopy->getDecl(), PrivateCopy, A);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000568 }
569}
570
Alexey Bataeved09d242014-05-28 05:53:51 +0000571bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000572 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000573 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000574 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000575 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000576 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000577 ++I;
578 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000579 if (I == E)
580 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000581 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000582 Scope *CurScope = getCurScope();
583 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000584 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000585 }
586 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000587 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000588 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000589}
590
Alexey Bataev39f915b82015-05-08 10:41:21 +0000591/// \brief Build a variable declaration for OpenMP loop iteration variable.
592static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000593 StringRef Name, const AttrVec *Attrs = nullptr) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000594 DeclContext *DC = SemaRef.CurContext;
595 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
596 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
597 VarDecl *Decl =
598 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000599 if (Attrs) {
600 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
601 I != E; ++I)
602 Decl->addAttr(*I);
603 }
Alexey Bataev39f915b82015-05-08 10:41:21 +0000604 Decl->setImplicit();
605 return Decl;
606}
607
608static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
609 SourceLocation Loc,
610 bool RefersToCapture = false) {
611 D->setReferenced();
612 D->markUsed(S.Context);
613 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
614 SourceLocation(), D, RefersToCapture, Loc, Ty,
615 VK_LValue);
616}
617
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000618DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, bool FromParent) {
619 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000620 DSAVarData DVar;
621
622 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
623 // in a Construct, C/C++, predetermined, p.1]
624 // Variables appearing in threadprivate directives are threadprivate.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000625 auto *VD = dyn_cast<VarDecl>(D);
626 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
627 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
Samuel Antaof8b50122015-07-13 22:54:53 +0000628 SemaRef.getLangOpts().OpenMPUseTLS &&
629 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000630 (VD && VD->getStorageClass() == SC_Register &&
631 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
632 addDSA(D, buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
Alexey Bataev39f915b82015-05-08 10:41:21 +0000633 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000634 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000635 }
636 if (Stack[0].SharingMap.count(D)) {
637 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
638 DVar.CKind = OMPC_threadprivate;
639 return DVar;
640 }
641
642 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000643 // in a Construct, C/C++, predetermined, p.4]
644 // Static data members are shared.
645 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
646 // in a Construct, C/C++, predetermined, p.7]
647 // Variables with static storage duration that are declared in a scope
648 // inside the construct are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000649 if (VD && VD->isStaticDataMember()) {
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000650 DSAVarData DVarTemp =
651 hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
652 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +0000653 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000654
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000655 DVar.CKind = OMPC_shared;
656 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000657 }
658
659 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000660 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
661 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000662 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
663 // in a Construct, C/C++, predetermined, p.6]
664 // Variables with const qualified type having no mutable member are
665 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000666 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000667 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataevc9bd03d2015-12-17 06:55:08 +0000668 if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
669 if (auto *CTD = CTSD->getSpecializedTemplate())
670 RD = CTD->getTemplatedDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000671 if (IsConstant &&
Alexey Bataev4bcad7f2016-02-10 10:50:12 +0000672 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
673 RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000674 // Variables with const-qualified type having no mutable member may be
675 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000676 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
677 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000678 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
679 return DVar;
680
Alexey Bataev758e55e2013-09-06 18:03:48 +0000681 DVar.CKind = OMPC_shared;
682 return DVar;
683 }
684
Alexey Bataev758e55e2013-09-06 18:03:48 +0000685 // Explicitly specified attributes and local variables with predetermined
686 // attributes.
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000687 auto StartI = std::next(Stack.rbegin());
688 auto EndI = std::prev(Stack.rend());
689 if (FromParent && StartI != EndI) {
690 StartI = std::next(StartI);
691 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000692 auto I = std::prev(StartI);
693 if (I->SharingMap.count(D)) {
694 DVar.RefExpr = I->SharingMap[D].RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000695 DVar.PrivateCopy = I->SharingMap[D].PrivateCopy;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000696 DVar.CKind = I->SharingMap[D].Attributes;
697 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000698 }
699
700 return DVar;
701}
702
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000703DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
704 bool FromParent) {
705 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000706 auto StartI = Stack.rbegin();
707 auto EndI = std::prev(Stack.rend());
708 if (FromParent && StartI != EndI) {
709 StartI = std::next(StartI);
710 }
711 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000712}
713
Alexey Bataevf29276e2014-06-18 04:14:57 +0000714template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000715DSAStackTy::DSAVarData DSAStackTy::hasDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000716 DirectivesPredicate DPred,
717 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000718 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000719 auto StartI = std::next(Stack.rbegin());
720 auto EndI = std::prev(Stack.rend());
721 if (FromParent && StartI != EndI) {
722 StartI = std::next(StartI);
723 }
724 for (auto I = StartI, EE = EndI; I != EE; ++I) {
725 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000726 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000727 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000728 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000729 return DVar;
730 }
731 return DSAVarData();
732}
733
Alexey Bataevf29276e2014-06-18 04:14:57 +0000734template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000735DSAStackTy::DSAVarData
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000736DSAStackTy::hasInnermostDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000737 DirectivesPredicate DPred, bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000738 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000739 auto StartI = std::next(Stack.rbegin());
740 auto EndI = std::prev(Stack.rend());
741 if (FromParent && StartI != EndI) {
742 StartI = std::next(StartI);
743 }
744 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000745 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000746 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000747 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000748 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000749 return DVar;
750 return DSAVarData();
751 }
752 return DSAVarData();
753}
754
Alexey Bataevaac108a2015-06-23 04:51:00 +0000755bool DSAStackTy::hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000756 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000757 unsigned Level) {
758 if (CPred(ClauseKindMode))
759 return true;
760 if (isClauseParsingMode())
761 ++Level;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000762 D = getCanonicalDecl(D);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000763 auto StartI = Stack.rbegin();
764 auto EndI = std::prev(Stack.rend());
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000765 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000766 return false;
767 std::advance(StartI, Level);
768 return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr &&
769 CPred(StartI->SharingMap[D].Attributes);
770}
771
Samuel Antao4be30e92015-10-02 17:14:03 +0000772bool DSAStackTy::hasExplicitDirective(
773 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
774 unsigned Level) {
775 if (isClauseParsingMode())
776 ++Level;
777 auto StartI = Stack.rbegin();
778 auto EndI = std::prev(Stack.rend());
779 if (std::distance(StartI, EndI) <= (int)Level)
780 return false;
781 std::advance(StartI, Level);
782 return DPred(StartI->Directive);
783}
784
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000785template <class NamedDirectivesPredicate>
786bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
787 auto StartI = std::next(Stack.rbegin());
788 auto EndI = std::prev(Stack.rend());
789 if (FromParent && StartI != EndI) {
790 StartI = std::next(StartI);
791 }
792 for (auto I = StartI, EE = EndI; I != EE; ++I) {
793 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
794 return true;
795 }
796 return false;
797}
798
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000799OpenMPDirectiveKind DSAStackTy::getDirectiveForScope(const Scope *S) const {
800 for (auto I = Stack.rbegin(), EE = Stack.rend(); I != EE; ++I)
801 if (I->CurScope == S)
802 return I->Directive;
803 return OMPD_unknown;
804}
805
Alexey Bataev758e55e2013-09-06 18:03:48 +0000806void Sema::InitDataSharingAttributesStack() {
807 VarDataSharingAttributesStack = new DSAStackTy(*this);
808}
809
810#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
811
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000812bool Sema::IsOpenMPCapturedByRef(ValueDecl *D,
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000813 const CapturedRegionScopeInfo *RSI) {
814 assert(LangOpts.OpenMP && "OpenMP is not allowed");
815
816 auto &Ctx = getASTContext();
817 bool IsByRef = true;
818
819 // Find the directive that is associated with the provided scope.
820 auto DKind = DSAStack->getDirectiveForScope(RSI->TheScope);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000821 auto Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000822
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000823 if (isOpenMPTargetExecutionDirective(DKind)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000824 // This table summarizes how a given variable should be passed to the device
825 // given its type and the clauses where it appears. This table is based on
826 // the description in OpenMP 4.5 [2.10.4, target Construct] and
827 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
828 //
829 // =========================================================================
830 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
831 // | |(tofrom:scalar)| | pvt | | | |
832 // =========================================================================
833 // | scl | | | | - | | bycopy|
834 // | scl | | - | x | - | - | bycopy|
835 // | scl | | x | - | - | - | null |
836 // | scl | x | | | - | | byref |
837 // | scl | x | - | x | - | - | bycopy|
838 // | scl | x | x | - | - | - | null |
839 // | scl | | - | - | - | x | byref |
840 // | scl | x | - | - | - | x | byref |
841 //
842 // | agg | n.a. | | | - | | byref |
843 // | agg | n.a. | - | x | - | - | byref |
844 // | agg | n.a. | x | - | - | - | null |
845 // | agg | n.a. | - | - | - | x | byref |
846 // | agg | n.a. | - | - | - | x[] | byref |
847 //
848 // | ptr | n.a. | | | - | | bycopy|
849 // | ptr | n.a. | - | x | - | - | bycopy|
850 // | ptr | n.a. | x | - | - | - | null |
851 // | ptr | n.a. | - | - | - | x | byref |
852 // | ptr | n.a. | - | - | - | x[] | bycopy|
853 // | ptr | n.a. | - | - | x | | bycopy|
854 // | ptr | n.a. | - | - | x | x | bycopy|
855 // | ptr | n.a. | - | - | x | x[] | bycopy|
856 // =========================================================================
857 // Legend:
858 // scl - scalar
859 // ptr - pointer
860 // agg - aggregate
861 // x - applies
862 // - - invalid in this combination
863 // [] - mapped with an array section
864 // byref - should be mapped by reference
865 // byval - should be mapped by value
866 // null - initialize a local variable to null on the device
867 //
868 // Observations:
869 // - All scalar declarations that show up in a map clause have to be passed
870 // by reference, because they may have been mapped in the enclosing data
871 // environment.
872 // - If the scalar value does not fit the size of uintptr, it has to be
873 // passed by reference, regardless the result in the table above.
874 // - For pointers mapped by value that have either an implicit map or an
875 // array section, the runtime library may pass the NULL value to the
876 // device instead of the value passed to it by the compiler.
877
878 // FIXME: Right now, only implicit maps are implemented. Properly mapping
879 // values requires having the map, private, and firstprivate clauses SEMA
880 // and parsing in place, which we don't yet.
881
882 if (Ty->isReferenceType())
883 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
884 IsByRef = !Ty->isScalarType();
885 }
886
887 // When passing data by value, we need to make sure it fits the uintptr size
888 // and alignment, because the runtime library only deals with uintptr types.
889 // If it does not fit the uintptr size, we need to pass the data by reference
890 // instead.
891 if (!IsByRef &&
892 (Ctx.getTypeSizeInChars(Ty) >
893 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000894 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType())))
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000895 IsByRef = true;
896
897 return IsByRef;
898}
899
Alexey Bataev90c228f2016-02-08 09:29:13 +0000900VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +0000901 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000902 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +0000903
904 // If we are attempting to capture a global variable in a directive with
905 // 'target' we return true so that this global is also mapped to the device.
906 //
907 // FIXME: If the declaration is enclosed in a 'declare target' directive,
908 // then it should not be captured. Therefore, an extra check has to be
909 // inserted here once support for 'declare target' is added.
910 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000911 auto *VD = dyn_cast<VarDecl>(D);
912 if (VD && !VD->hasLocalStorage()) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000913 if (DSAStack->getCurrentDirective() == OMPD_target &&
Alexey Bataev90c228f2016-02-08 09:29:13 +0000914 !DSAStack->isClauseParsingMode())
915 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +0000916 if (DSAStack->getCurScope() &&
917 DSAStack->hasDirective(
918 [](OpenMPDirectiveKind K, const DeclarationNameInfo &DNI,
919 SourceLocation Loc) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000920 return isOpenMPTargetExecutionDirective(K);
Samuel Antao4be30e92015-10-02 17:14:03 +0000921 },
Alexey Bataev90c228f2016-02-08 09:29:13 +0000922 false))
923 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +0000924 }
925
Alexey Bataev48977c32015-08-04 08:10:48 +0000926 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
927 (!DSAStack->isClauseParsingMode() ||
928 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000929 if (DSAStack->isLoopControlVariable(D) ||
930 (VD && VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000931 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000932 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataev90c228f2016-02-08 09:29:13 +0000933 return VD;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000934 auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000935 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +0000936 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000937 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate, MatchesAlways(),
Alexey Bataevaac108a2015-06-23 04:51:00 +0000938 DSAStack->isClauseParsingMode());
Alexey Bataev90c228f2016-02-08 09:29:13 +0000939 if (DVarPrivate.CKind != OMPC_unknown)
940 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000941 }
Alexey Bataev90c228f2016-02-08 09:29:13 +0000942 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +0000943}
944
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000945bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) {
Alexey Bataevaac108a2015-06-23 04:51:00 +0000946 assert(LangOpts.OpenMP && "OpenMP is not allowed");
947 return DSAStack->hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000948 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000949}
950
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000951bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000952 assert(LangOpts.OpenMP && "OpenMP is not allowed");
953 // Return true if the current level is no longer enclosed in a target region.
954
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000955 auto *VD = dyn_cast<VarDecl>(D);
956 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000957 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
958 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +0000959}
960
Alexey Bataeved09d242014-05-28 05:53:51 +0000961void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000962
963void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
964 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000965 Scope *CurScope, SourceLocation Loc) {
966 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000967 PushExpressionEvaluationContext(PotentiallyEvaluated);
968}
969
Alexey Bataevaac108a2015-06-23 04:51:00 +0000970void Sema::StartOpenMPClause(OpenMPClauseKind K) {
971 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000972}
973
Alexey Bataevaac108a2015-06-23 04:51:00 +0000974void Sema::EndOpenMPClause() {
975 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000976}
977
Alexey Bataev758e55e2013-09-06 18:03:48 +0000978void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000979 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
980 // A variable of class type (or array thereof) that appears in a lastprivate
981 // clause requires an accessible, unambiguous default constructor for the
982 // class type, unless the list item is also specified in a firstprivate
983 // clause.
984 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000985 for (auto *C : D->clauses()) {
986 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
987 SmallVector<Expr *, 8> PrivateCopies;
988 for (auto *DE : Clause->varlists()) {
989 if (DE->isValueDependent() || DE->isTypeDependent()) {
990 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000991 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +0000992 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000993 DE = DE->IgnoreParens();
994 VarDecl *VD = nullptr;
995 FieldDecl *FD = nullptr;
996 ValueDecl *D;
997 if (auto *DRE = dyn_cast<DeclRefExpr>(DE)) {
998 VD = cast<VarDecl>(DRE->getDecl());
999 D = VD;
1000 } else {
1001 assert(isa<MemberExpr>(DE));
1002 FD = cast<FieldDecl>(cast<MemberExpr>(DE)->getMemberDecl());
1003 D = FD;
1004 }
1005 QualType Type = D->getType().getNonReferenceType();
1006 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001007 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001008 // Generate helper private variable and initialize it with the
1009 // default value. The address of the original variable is replaced
1010 // by the address of the new private variable in CodeGen. This new
1011 // variable is not added to IdResolver, so the code in the OpenMP
1012 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001013 auto *VDPrivate = buildVarDecl(
1014 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001015 D->getName(), D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00001016 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
1017 if (VDPrivate->isInvalidDecl())
1018 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001019 PrivateCopies.push_back(buildDeclRefExpr(
1020 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001021 } else {
1022 // The variable is also a firstprivate, so initialization sequence
1023 // for private copy is generated already.
1024 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001025 }
1026 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001027 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001028 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001029 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001030 }
1031 }
1032 }
1033
Alexey Bataev758e55e2013-09-06 18:03:48 +00001034 DSAStack->pop();
1035 DiscardCleanupsInEvaluationContext();
1036 PopExpressionEvaluationContext();
1037}
1038
Alexander Musman3276a272015-03-21 10:12:56 +00001039static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1040 Expr *NumIterations, Sema &SemaRef,
1041 Scope *S);
1042
Alexey Bataeva769e072013-03-22 06:34:35 +00001043namespace {
1044
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001045class VarDeclFilterCCC : public CorrectionCandidateCallback {
1046private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001047 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001048
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001049public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001050 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001051 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001052 NamedDecl *ND = Candidate.getCorrectionDecl();
1053 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
1054 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001055 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1056 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001057 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001058 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001059 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001060};
Alexey Bataeved09d242014-05-28 05:53:51 +00001061} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001062
1063ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1064 CXXScopeSpec &ScopeSpec,
1065 const DeclarationNameInfo &Id) {
1066 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1067 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1068
1069 if (Lookup.isAmbiguous())
1070 return ExprError();
1071
1072 VarDecl *VD;
1073 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001074 if (TypoCorrection Corrected = CorrectTypo(
1075 Id, LookupOrdinaryName, CurScope, nullptr,
1076 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001077 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001078 PDiag(Lookup.empty()
1079 ? diag::err_undeclared_var_use_suggest
1080 : diag::err_omp_expected_var_arg_suggest)
1081 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001082 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001083 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001084 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1085 : diag::err_omp_expected_var_arg)
1086 << Id.getName();
1087 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001088 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001089 } else {
1090 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001091 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001092 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1093 return ExprError();
1094 }
1095 }
1096 Lookup.suppressDiagnostics();
1097
1098 // OpenMP [2.9.2, Syntax, C/C++]
1099 // Variables must be file-scope, namespace-scope, or static block-scope.
1100 if (!VD->hasGlobalStorage()) {
1101 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001102 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1103 bool IsDecl =
1104 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001105 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001106 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1107 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001108 return ExprError();
1109 }
1110
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001111 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1112 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001113 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1114 // A threadprivate directive for file-scope variables must appear outside
1115 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001116 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1117 !getCurLexicalContext()->isTranslationUnit()) {
1118 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001119 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1120 bool IsDecl =
1121 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1122 Diag(VD->getLocation(),
1123 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1124 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001125 return ExprError();
1126 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001127 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1128 // A threadprivate directive for static class member variables must appear
1129 // in the class definition, in the same scope in which the member
1130 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001131 if (CanonicalVD->isStaticDataMember() &&
1132 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1133 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001134 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1135 bool IsDecl =
1136 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1137 Diag(VD->getLocation(),
1138 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1139 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001140 return ExprError();
1141 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001142 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1143 // A threadprivate directive for namespace-scope variables must appear
1144 // outside any definition or declaration other than the namespace
1145 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001146 if (CanonicalVD->getDeclContext()->isNamespace() &&
1147 (!getCurLexicalContext()->isFileContext() ||
1148 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1149 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001150 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1151 bool IsDecl =
1152 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1153 Diag(VD->getLocation(),
1154 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1155 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001156 return ExprError();
1157 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001158 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1159 // A threadprivate directive for static block-scope variables must appear
1160 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001161 if (CanonicalVD->isStaticLocal() && CurScope &&
1162 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001163 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001164 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1165 bool IsDecl =
1166 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1167 Diag(VD->getLocation(),
1168 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1169 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001170 return ExprError();
1171 }
1172
1173 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1174 // A threadprivate directive must lexically precede all references to any
1175 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001176 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001177 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001178 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001179 return ExprError();
1180 }
1181
1182 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00001183 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1184 SourceLocation(), VD,
1185 /*RefersToEnclosingVariableOrCapture=*/false,
1186 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001187}
1188
Alexey Bataeved09d242014-05-28 05:53:51 +00001189Sema::DeclGroupPtrTy
1190Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1191 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001192 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001193 CurContext->addDecl(D);
1194 return DeclGroupPtrTy::make(DeclGroupRef(D));
1195 }
David Blaikie0403cb12016-01-15 23:43:25 +00001196 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001197}
1198
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001199namespace {
1200class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1201 Sema &SemaRef;
1202
1203public:
1204 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1205 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1206 if (VD->hasLocalStorage()) {
1207 SemaRef.Diag(E->getLocStart(),
1208 diag::err_omp_local_var_in_threadprivate_init)
1209 << E->getSourceRange();
1210 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1211 << VD << VD->getSourceRange();
1212 return true;
1213 }
1214 }
1215 return false;
1216 }
1217 bool VisitStmt(const Stmt *S) {
1218 for (auto Child : S->children()) {
1219 if (Child && Visit(Child))
1220 return true;
1221 }
1222 return false;
1223 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001224 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001225};
1226} // namespace
1227
Alexey Bataeved09d242014-05-28 05:53:51 +00001228OMPThreadPrivateDecl *
1229Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001230 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001231 for (auto &RefExpr : VarList) {
1232 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001233 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1234 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001235
Alexey Bataev376b4a42016-02-09 09:41:09 +00001236 // Mark variable as used.
1237 VD->setReferenced();
1238 VD->markUsed(Context);
1239
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001240 QualType QType = VD->getType();
1241 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1242 // It will be analyzed later.
1243 Vars.push_back(DE);
1244 continue;
1245 }
1246
Alexey Bataeva769e072013-03-22 06:34:35 +00001247 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1248 // A threadprivate variable must not have an incomplete type.
1249 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001250 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001251 continue;
1252 }
1253
1254 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1255 // A threadprivate variable must not have a reference type.
1256 if (VD->getType()->isReferenceType()) {
1257 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001258 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1259 bool IsDecl =
1260 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1261 Diag(VD->getLocation(),
1262 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1263 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001264 continue;
1265 }
1266
Samuel Antaof8b50122015-07-13 22:54:53 +00001267 // Check if this is a TLS variable. If TLS is not being supported, produce
1268 // the corresponding diagnostic.
1269 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1270 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1271 getLangOpts().OpenMPUseTLS &&
1272 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001273 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1274 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001275 Diag(ILoc, diag::err_omp_var_thread_local)
1276 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001277 bool IsDecl =
1278 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1279 Diag(VD->getLocation(),
1280 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1281 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001282 continue;
1283 }
1284
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001285 // Check if initial value of threadprivate variable reference variable with
1286 // local storage (it is not supported by runtime).
1287 if (auto Init = VD->getAnyInitializer()) {
1288 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001289 if (Checker.Visit(Init))
1290 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001291 }
1292
Alexey Bataeved09d242014-05-28 05:53:51 +00001293 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001294 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001295 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1296 Context, SourceRange(Loc, Loc)));
1297 if (auto *ML = Context.getASTMutationListener())
1298 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001299 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001300 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001301 if (!Vars.empty()) {
1302 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1303 Vars);
1304 D->setAccess(AS_public);
1305 }
1306 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001307}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001308
Alexey Bataev7ff55242014-06-19 09:13:45 +00001309static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001310 const ValueDecl *D, DSAStackTy::DSAVarData DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00001311 bool IsLoopIterVar = false) {
1312 if (DVar.RefExpr) {
1313 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1314 << getOpenMPClauseName(DVar.CKind);
1315 return;
1316 }
1317 enum {
1318 PDSA_StaticMemberShared,
1319 PDSA_StaticLocalVarShared,
1320 PDSA_LoopIterVarPrivate,
1321 PDSA_LoopIterVarLinear,
1322 PDSA_LoopIterVarLastprivate,
1323 PDSA_ConstVarShared,
1324 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001325 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001326 PDSA_LocalVarPrivate,
1327 PDSA_Implicit
1328 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001329 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001330 auto ReportLoc = D->getLocation();
1331 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001332 if (IsLoopIterVar) {
1333 if (DVar.CKind == OMPC_private)
1334 Reason = PDSA_LoopIterVarPrivate;
1335 else if (DVar.CKind == OMPC_lastprivate)
1336 Reason = PDSA_LoopIterVarLastprivate;
1337 else
1338 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001339 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
1340 Reason = PDSA_TaskVarFirstprivate;
1341 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001342 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001343 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001344 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001345 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001346 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001347 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001348 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00001349 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001350 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001351 ReportHint = true;
1352 Reason = PDSA_LocalVarPrivate;
1353 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001354 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001355 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001356 << Reason << ReportHint
1357 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1358 } else if (DVar.ImplicitDSALoc.isValid()) {
1359 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1360 << getOpenMPClauseName(DVar.CKind);
1361 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001362}
1363
Alexey Bataev758e55e2013-09-06 18:03:48 +00001364namespace {
1365class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1366 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001367 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001368 bool ErrorFound;
1369 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001370 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001371 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001372
Alexey Bataev758e55e2013-09-06 18:03:48 +00001373public:
1374 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001375 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001376 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001377 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1378 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001379
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001380 auto DVar = Stack->getTopDSA(VD, false);
1381 // Check if the variable has explicit DSA set and stop analysis if it so.
1382 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001383
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001384 auto ELoc = E->getExprLoc();
1385 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001386 // The default(none) clause requires that each variable that is referenced
1387 // in the construct, and does not have a predetermined data-sharing
1388 // attribute, must have its data-sharing attribute explicitly determined
1389 // by being listed in a data-sharing attribute clause.
1390 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001391 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001392 VarsWithInheritedDSA.count(VD) == 0) {
1393 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001394 return;
1395 }
1396
1397 // OpenMP [2.9.3.6, Restrictions, p.2]
1398 // A list item that appears in a reduction clause of the innermost
1399 // enclosing worksharing or parallel construct may not be accessed in an
1400 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001401 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001402 [](OpenMPDirectiveKind K) -> bool {
1403 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001404 isOpenMPWorksharingDirective(K) ||
1405 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001406 },
1407 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001408 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1409 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001410 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1411 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001412 return;
1413 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001414
1415 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001416 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001417 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001418 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001419 }
1420 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001421 void VisitMemberExpr(MemberExpr *E) {
1422 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1423 if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1424 auto DVar = Stack->getTopDSA(FD, false);
1425 // Check if the variable has explicit DSA set and stop analysis if it
1426 // so.
1427 if (DVar.RefExpr)
1428 return;
1429
1430 auto ELoc = E->getExprLoc();
1431 auto DKind = Stack->getCurrentDirective();
1432 // OpenMP [2.9.3.6, Restrictions, p.2]
1433 // A list item that appears in a reduction clause of the innermost
1434 // enclosing worksharing or parallel construct may not be accessed in
Alexey Bataevd985eda2016-02-10 11:29:16 +00001435 // an explicit task.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001436 DVar =
1437 Stack->hasInnermostDSA(FD, MatchesAnyClause(OMPC_reduction),
1438 [](OpenMPDirectiveKind K) -> bool {
1439 return isOpenMPParallelDirective(K) ||
1440 isOpenMPWorksharingDirective(K) ||
1441 isOpenMPTeamsDirective(K);
1442 },
1443 false);
1444 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1445 ErrorFound = true;
1446 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1447 ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1448 return;
1449 }
1450
1451 // Define implicit data-sharing attributes for task.
1452 DVar = Stack->getImplicitDSA(FD, false);
1453 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
1454 ImplicitFirstprivate.push_back(E);
1455 }
1456 }
1457 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001458 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001459 for (auto *C : S->clauses()) {
1460 // Skip analysis of arguments of implicitly defined firstprivate clause
1461 // for task directives.
1462 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1463 for (auto *CC : C->children()) {
1464 if (CC)
1465 Visit(CC);
1466 }
1467 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001468 }
1469 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001470 for (auto *C : S->children()) {
1471 if (C && !isa<OMPExecutableDirective>(C))
1472 Visit(C);
1473 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001474 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001475
1476 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001477 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001478 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001479 return VarsWithInheritedDSA;
1480 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001481
Alexey Bataev7ff55242014-06-19 09:13:45 +00001482 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1483 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001484};
Alexey Bataeved09d242014-05-28 05:53:51 +00001485} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001486
Alexey Bataevbae9a792014-06-27 10:37:06 +00001487void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001488 switch (DKind) {
1489 case OMPD_parallel: {
1490 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001491 QualType KmpInt32PtrTy =
1492 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001493 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001494 std::make_pair(".global_tid.", KmpInt32PtrTy),
1495 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1496 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001497 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001498 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1499 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001500 break;
1501 }
1502 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001503 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001504 std::make_pair(StringRef(), QualType()) // __context with shared vars
1505 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001506 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1507 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001508 break;
1509 }
1510 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001511 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001512 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001513 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001514 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1515 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001516 break;
1517 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001518 case OMPD_for_simd: {
1519 Sema::CapturedParamNameType Params[] = {
1520 std::make_pair(StringRef(), QualType()) // __context with shared vars
1521 };
1522 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1523 Params);
1524 break;
1525 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001526 case OMPD_sections: {
1527 Sema::CapturedParamNameType Params[] = {
1528 std::make_pair(StringRef(), QualType()) // __context with shared vars
1529 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001530 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1531 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001532 break;
1533 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001534 case OMPD_section: {
1535 Sema::CapturedParamNameType Params[] = {
1536 std::make_pair(StringRef(), QualType()) // __context with shared vars
1537 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001538 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1539 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001540 break;
1541 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001542 case OMPD_single: {
1543 Sema::CapturedParamNameType Params[] = {
1544 std::make_pair(StringRef(), QualType()) // __context with shared vars
1545 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001546 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1547 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001548 break;
1549 }
Alexander Musman80c22892014-07-17 08:54:58 +00001550 case OMPD_master: {
1551 Sema::CapturedParamNameType Params[] = {
1552 std::make_pair(StringRef(), QualType()) // __context with shared vars
1553 };
1554 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1555 Params);
1556 break;
1557 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001558 case OMPD_critical: {
1559 Sema::CapturedParamNameType Params[] = {
1560 std::make_pair(StringRef(), QualType()) // __context with shared vars
1561 };
1562 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1563 Params);
1564 break;
1565 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001566 case OMPD_parallel_for: {
1567 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001568 QualType KmpInt32PtrTy =
1569 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001570 Sema::CapturedParamNameType Params[] = {
1571 std::make_pair(".global_tid.", KmpInt32PtrTy),
1572 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1573 std::make_pair(StringRef(), QualType()) // __context with shared vars
1574 };
1575 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1576 Params);
1577 break;
1578 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001579 case OMPD_parallel_for_simd: {
1580 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001581 QualType KmpInt32PtrTy =
1582 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001583 Sema::CapturedParamNameType Params[] = {
1584 std::make_pair(".global_tid.", KmpInt32PtrTy),
1585 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1586 std::make_pair(StringRef(), QualType()) // __context with shared vars
1587 };
1588 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1589 Params);
1590 break;
1591 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001592 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001593 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001594 QualType KmpInt32PtrTy =
1595 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001596 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001597 std::make_pair(".global_tid.", KmpInt32PtrTy),
1598 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001599 std::make_pair(StringRef(), QualType()) // __context with shared vars
1600 };
1601 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1602 Params);
1603 break;
1604 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001605 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001606 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001607 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1608 FunctionProtoType::ExtProtoInfo EPI;
1609 EPI.Variadic = true;
1610 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001611 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001612 std::make_pair(".global_tid.", KmpInt32Ty),
1613 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001614 std::make_pair(".privates.",
1615 Context.VoidPtrTy.withConst().withRestrict()),
1616 std::make_pair(
1617 ".copy_fn.",
1618 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001619 std::make_pair(StringRef(), QualType()) // __context with shared vars
1620 };
1621 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1622 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001623 // Mark this captured region as inlined, because we don't use outlined
1624 // function directly.
1625 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1626 AlwaysInlineAttr::CreateImplicit(
1627 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001628 break;
1629 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001630 case OMPD_ordered: {
1631 Sema::CapturedParamNameType Params[] = {
1632 std::make_pair(StringRef(), QualType()) // __context with shared vars
1633 };
1634 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1635 Params);
1636 break;
1637 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001638 case OMPD_atomic: {
1639 Sema::CapturedParamNameType Params[] = {
1640 std::make_pair(StringRef(), QualType()) // __context with shared vars
1641 };
1642 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1643 Params);
1644 break;
1645 }
Michael Wong65f367f2015-07-21 13:44:28 +00001646 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001647 case OMPD_target:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001648 case OMPD_target_parallel:
1649 case OMPD_target_parallel_for: {
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001650 Sema::CapturedParamNameType Params[] = {
1651 std::make_pair(StringRef(), QualType()) // __context with shared vars
1652 };
1653 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1654 Params);
1655 break;
1656 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001657 case OMPD_teams: {
1658 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001659 QualType KmpInt32PtrTy =
1660 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001661 Sema::CapturedParamNameType Params[] = {
1662 std::make_pair(".global_tid.", KmpInt32PtrTy),
1663 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1664 std::make_pair(StringRef(), QualType()) // __context with shared vars
1665 };
1666 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1667 Params);
1668 break;
1669 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001670 case OMPD_taskgroup: {
1671 Sema::CapturedParamNameType Params[] = {
1672 std::make_pair(StringRef(), QualType()) // __context with shared vars
1673 };
1674 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1675 Params);
1676 break;
1677 }
Alexey Bataev49f6e782015-12-01 04:18:41 +00001678 case OMPD_taskloop: {
1679 Sema::CapturedParamNameType Params[] = {
1680 std::make_pair(StringRef(), QualType()) // __context with shared vars
1681 };
1682 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1683 Params);
1684 break;
1685 }
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001686 case OMPD_taskloop_simd: {
1687 Sema::CapturedParamNameType Params[] = {
1688 std::make_pair(StringRef(), QualType()) // __context with shared vars
1689 };
1690 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1691 Params);
1692 break;
1693 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001694 case OMPD_distribute: {
1695 Sema::CapturedParamNameType Params[] = {
1696 std::make_pair(StringRef(), QualType()) // __context with shared vars
1697 };
1698 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1699 Params);
1700 break;
1701 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001702 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001703 case OMPD_taskyield:
1704 case OMPD_barrier:
1705 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001706 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001707 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001708 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001709 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001710 case OMPD_target_exit_data:
Alexey Bataev9959db52014-05-06 10:08:46 +00001711 llvm_unreachable("OpenMP Directive is not allowed");
1712 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001713 llvm_unreachable("Unknown OpenMP directive");
1714 }
1715}
1716
Alexey Bataev4244be22016-02-11 05:35:55 +00001717static DeclRefExpr *buildCapture(Sema &S, IdentifierInfo *Id,
1718 Expr *CaptureExpr) {
1719 ASTContext &C = S.getASTContext();
1720 Expr *Init = CaptureExpr->IgnoreImpCasts();
1721 QualType Ty = Init->getType();
1722 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1723 if (S.getLangOpts().CPlusPlus)
1724 Ty = C.getLValueReferenceType(Ty);
1725 else {
1726 Ty = C.getPointerType(Ty);
1727 ExprResult Res =
1728 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1729 if (!Res.isUsable())
1730 return nullptr;
1731 Init = Res.get();
1732 }
1733 }
1734 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty);
1735 S.CurContext->addHiddenDecl(CED);
1736 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false,
1737 /*TypeMayContainAuto=*/true);
1738 return buildDeclRefExpr(S, CED, Ty.getNonReferenceType(), SourceLocation());
1739}
1740
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001741StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1742 ArrayRef<OMPClause *> Clauses) {
1743 if (!S.isUsable()) {
1744 ActOnCapturedRegionError();
1745 return StmtError();
1746 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001747
1748 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001749 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001750 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001751 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001752 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001753 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001754 Clause->getClauseKind() == OMPC_copyprivate ||
1755 (getLangOpts().OpenMPUseTLS &&
1756 getASTContext().getTargetInfo().isTLSSupported() &&
1757 Clause->getClauseKind() == OMPC_copyin)) {
1758 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001759 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001760 for (auto *VarRef : Clause->children()) {
1761 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001762 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001763 }
1764 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001765 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev040d5402015-05-12 08:35:28 +00001766 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
Alexey Bataev4244be22016-02-11 05:35:55 +00001767 (Clause->getClauseKind() == OMPC_schedule ||
1768 Clause->getClauseKind() == OMPC_dist_schedule)) {
Alexey Bataev040d5402015-05-12 08:35:28 +00001769 // Mark all variables in private list clauses as used in inner region.
1770 // Required for proper codegen of combined directives.
1771 // TODO: add processing for other clauses.
Alexey Bataev4244be22016-02-11 05:35:55 +00001772 if (auto *SC = dyn_cast<OMPScheduleClause>(Clause)) {
1773 if (SC->getHelperChunkSize())
1774 MarkDeclarationsReferencedInExpr(SC->getHelperChunkSize());
1775 } else if (auto *DSC = dyn_cast<OMPDistScheduleClause>(Clause)) {
1776 if (DSC->getHelperChunkSize())
1777 MarkDeclarationsReferencedInExpr(DSC->getHelperChunkSize());
1778 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001779 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001780 if (Clause->getClauseKind() == OMPC_schedule)
1781 SC = cast<OMPScheduleClause>(Clause);
1782 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001783 OC = cast<OMPOrderedClause>(Clause);
1784 else if (Clause->getClauseKind() == OMPC_linear)
1785 LCs.push_back(cast<OMPLinearClause>(Clause));
1786 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001787 bool ErrorFound = false;
1788 // OpenMP, 2.7.1 Loop Construct, Restrictions
1789 // The nonmonotonic modifier cannot be specified if an ordered clause is
1790 // specified.
1791 if (SC &&
1792 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1793 SC->getSecondScheduleModifier() ==
1794 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1795 OC) {
1796 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1797 ? SC->getFirstScheduleModifierLoc()
1798 : SC->getSecondScheduleModifierLoc(),
1799 diag::err_omp_schedule_nonmonotonic_ordered)
1800 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1801 ErrorFound = true;
1802 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001803 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1804 for (auto *C : LCs) {
1805 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1806 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1807 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001808 ErrorFound = true;
1809 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001810 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1811 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1812 OC->getNumForLoops()) {
1813 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1814 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1815 ErrorFound = true;
1816 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001817 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001818 ActOnCapturedRegionError();
1819 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001820 }
1821 return ActOnCapturedRegionEnd(S.get());
1822}
1823
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001824static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1825 OpenMPDirectiveKind CurrentRegion,
1826 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001827 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001828 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001829 // Allowed nesting of constructs
1830 // +------------------+-----------------+------------------------------------+
1831 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1832 // +------------------+-----------------+------------------------------------+
1833 // | parallel | parallel | * |
1834 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001835 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001836 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001837 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001838 // | parallel | simd | * |
1839 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001840 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001841 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001842 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001843 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001844 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001845 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001846 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001847 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001848 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001849 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001850 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001851 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001852 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001853 // | parallel | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001854 // | parallel | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001855 // | parallel | target parallel | * |
1856 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001857 // | parallel | target enter | * |
1858 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001859 // | parallel | target exit | * |
1860 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001861 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001862 // | parallel | cancellation | |
1863 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001864 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001865 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001866 // | parallel | taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001867 // | parallel | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001868 // +------------------+-----------------+------------------------------------+
1869 // | for | parallel | * |
1870 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001871 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001872 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001873 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001874 // | for | simd | * |
1875 // | for | sections | + |
1876 // | for | section | + |
1877 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001878 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001879 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001880 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001881 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001882 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001883 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001884 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001885 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001886 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001887 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001888 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001889 // | for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001890 // | for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001891 // | for | target parallel | * |
1892 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001893 // | for | target enter | * |
1894 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001895 // | for | target exit | * |
1896 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001897 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001898 // | for | cancellation | |
1899 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001900 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001901 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001902 // | for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001903 // | for | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001904 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001905 // | master | parallel | * |
1906 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001907 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001908 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001909 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001910 // | master | simd | * |
1911 // | master | sections | + |
1912 // | master | section | + |
1913 // | master | single | + |
1914 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001915 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001916 // | master |parallel sections| * |
1917 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001918 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001919 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001920 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001921 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001922 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001923 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001924 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001925 // | master | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001926 // | master | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001927 // | master | target parallel | * |
1928 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001929 // | master | target enter | * |
1930 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001931 // | master | target exit | * |
1932 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001933 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001934 // | master | cancellation | |
1935 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001936 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001937 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001938 // | master | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001939 // | master | distribute | |
Alexander Musman80c22892014-07-17 08:54:58 +00001940 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001941 // | critical | parallel | * |
1942 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001943 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001944 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001945 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001946 // | critical | simd | * |
1947 // | critical | sections | + |
1948 // | critical | section | + |
1949 // | critical | single | + |
1950 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001951 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001952 // | critical |parallel sections| * |
1953 // | critical | task | * |
1954 // | critical | taskyield | * |
1955 // | critical | barrier | + |
1956 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001957 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001958 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001959 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001960 // | critical | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001961 // | critical | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001962 // | critical | target parallel | * |
1963 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001964 // | critical | target enter | * |
1965 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001966 // | critical | target exit | * |
1967 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001968 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001969 // | critical | cancellation | |
1970 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001971 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001972 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001973 // | critical | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001974 // | critical | distribute | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001975 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001976 // | simd | parallel | |
1977 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001978 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001979 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001980 // | simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00001981 // | simd | simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001982 // | simd | sections | |
1983 // | simd | section | |
1984 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001985 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001986 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001987 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001988 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001989 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001990 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001991 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001992 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001993 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001994 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001995 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001996 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001997 // | simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001998 // | simd | target parallel | |
1999 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002000 // | simd | target enter | |
2001 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002002 // | simd | target exit | |
2003 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002004 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002005 // | simd | cancellation | |
2006 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002007 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002008 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002009 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002010 // | simd | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002011 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00002012 // | for simd | parallel | |
2013 // | for simd | for | |
2014 // | for simd | for simd | |
2015 // | for simd | master | |
2016 // | for simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002017 // | for simd | simd | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002018 // | for simd | sections | |
2019 // | for simd | section | |
2020 // | for simd | single | |
2021 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002022 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002023 // | for simd |parallel sections| |
2024 // | for simd | task | |
2025 // | for simd | taskyield | |
2026 // | for simd | barrier | |
2027 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002028 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002029 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002030 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002031 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002032 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002033 // | for simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002034 // | for simd | target parallel | |
2035 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002036 // | for simd | target enter | |
2037 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002038 // | for simd | target exit | |
2039 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002040 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002041 // | for simd | cancellation | |
2042 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002043 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002044 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002045 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002046 // | for simd | distribute | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002047 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00002048 // | parallel for simd| parallel | |
2049 // | parallel for simd| for | |
2050 // | parallel for simd| for simd | |
2051 // | parallel for simd| master | |
2052 // | parallel for simd| critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002053 // | parallel for simd| simd | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002054 // | parallel for simd| sections | |
2055 // | parallel for simd| section | |
2056 // | parallel for simd| single | |
2057 // | parallel for simd| parallel for | |
2058 // | parallel for simd|parallel for simd| |
2059 // | parallel for simd|parallel sections| |
2060 // | parallel for simd| task | |
2061 // | parallel for simd| taskyield | |
2062 // | parallel for simd| barrier | |
2063 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002064 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002065 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002066 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002067 // | parallel for simd| atomic | |
2068 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002069 // | parallel for simd| target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002070 // | parallel for simd| target parallel | |
2071 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002072 // | parallel for simd| target enter | |
2073 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002074 // | parallel for simd| target exit | |
2075 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002076 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002077 // | parallel for simd| cancellation | |
2078 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002079 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002080 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002081 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002082 // | parallel for simd| distribute | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002083 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002084 // | sections | parallel | * |
2085 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002086 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002087 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002088 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002089 // | sections | simd | * |
2090 // | sections | sections | + |
2091 // | sections | section | * |
2092 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002093 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002094 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002095 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002096 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002097 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002098 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002099 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002100 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002101 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002102 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002103 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002104 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002105 // | sections | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002106 // | sections | target parallel | * |
2107 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002108 // | sections | target enter | * |
2109 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002110 // | sections | target exit | * |
2111 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002112 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002113 // | sections | cancellation | |
2114 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002115 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002116 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002117 // | sections | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002118 // | sections | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002119 // +------------------+-----------------+------------------------------------+
2120 // | section | parallel | * |
2121 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002122 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002123 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002124 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002125 // | section | simd | * |
2126 // | section | sections | + |
2127 // | section | section | + |
2128 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002129 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002130 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002131 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002132 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002133 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002134 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002135 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002136 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002137 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002138 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002139 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002140 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002141 // | section | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002142 // | section | target parallel | * |
2143 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002144 // | section | target enter | * |
2145 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002146 // | section | target exit | * |
2147 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002148 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002149 // | section | cancellation | |
2150 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002151 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002152 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002153 // | section | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002154 // | section | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002155 // +------------------+-----------------+------------------------------------+
2156 // | single | parallel | * |
2157 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002158 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002159 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002160 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002161 // | single | simd | * |
2162 // | single | sections | + |
2163 // | single | section | + |
2164 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002165 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002166 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002167 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002168 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002169 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002170 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002171 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002172 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002173 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002174 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002175 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002176 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002177 // | single | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002178 // | single | target parallel | * |
2179 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002180 // | single | target enter | * |
2181 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002182 // | single | target exit | * |
2183 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002184 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002185 // | single | cancellation | |
2186 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002187 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002188 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002189 // | single | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002190 // | single | distribute | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002191 // +------------------+-----------------+------------------------------------+
2192 // | parallel for | parallel | * |
2193 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002194 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002195 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002196 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002197 // | parallel for | simd | * |
2198 // | parallel for | sections | + |
2199 // | parallel for | section | + |
2200 // | parallel for | single | + |
2201 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002202 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002203 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002204 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002205 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002206 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002207 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002208 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002209 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002210 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002211 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002212 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002213 // | parallel for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002214 // | parallel for | target parallel | * |
2215 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002216 // | parallel for | target enter | * |
2217 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002218 // | parallel for | target exit | * |
2219 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002220 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002221 // | parallel for | cancellation | |
2222 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002223 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002224 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002225 // | parallel for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002226 // | parallel for | distribute | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002227 // +------------------+-----------------+------------------------------------+
2228 // | parallel sections| parallel | * |
2229 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002230 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002231 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002232 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002233 // | parallel sections| simd | * |
2234 // | parallel sections| sections | + |
2235 // | parallel sections| section | * |
2236 // | parallel sections| single | + |
2237 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002238 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002239 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002240 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002241 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002242 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002243 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002244 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002245 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002246 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002247 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002248 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002249 // | parallel sections| target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002250 // | parallel sections| target parallel | * |
2251 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002252 // | parallel sections| target enter | * |
2253 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002254 // | parallel sections| target exit | * |
2255 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002256 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002257 // | parallel sections| cancellation | |
2258 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002259 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002260 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002261 // | parallel sections| taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002262 // | parallel sections| distribute | |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002263 // +------------------+-----------------+------------------------------------+
2264 // | task | parallel | * |
2265 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002266 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002267 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002268 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002269 // | task | simd | * |
2270 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002271 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002272 // | task | single | + |
2273 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002274 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002275 // | task |parallel sections| * |
2276 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002277 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002278 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002279 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002280 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002281 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002282 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002283 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002284 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002285 // | task | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002286 // | task | target parallel | * |
2287 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002288 // | task | target enter | * |
2289 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002290 // | task | target exit | * |
2291 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002292 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002293 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002294 // | | point | ! |
2295 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002296 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002297 // | task | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002298 // | task | distribute | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002299 // +------------------+-----------------+------------------------------------+
2300 // | ordered | parallel | * |
2301 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002302 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002303 // | ordered | master | * |
2304 // | ordered | critical | * |
2305 // | ordered | simd | * |
2306 // | ordered | sections | + |
2307 // | ordered | section | + |
2308 // | ordered | single | + |
2309 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002310 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002311 // | ordered |parallel sections| * |
2312 // | ordered | task | * |
2313 // | ordered | taskyield | * |
2314 // | ordered | barrier | + |
2315 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002316 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002317 // | ordered | flush | * |
2318 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002319 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002320 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002321 // | ordered | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002322 // | ordered | target parallel | * |
2323 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002324 // | ordered | target enter | * |
2325 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002326 // | ordered | target exit | * |
2327 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002328 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002329 // | ordered | cancellation | |
2330 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002331 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002332 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002333 // | ordered | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002334 // | ordered | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002335 // +------------------+-----------------+------------------------------------+
2336 // | atomic | parallel | |
2337 // | atomic | for | |
2338 // | atomic | for simd | |
2339 // | atomic | master | |
2340 // | atomic | critical | |
2341 // | atomic | simd | |
2342 // | atomic | sections | |
2343 // | atomic | section | |
2344 // | atomic | single | |
2345 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002346 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002347 // | atomic |parallel sections| |
2348 // | atomic | task | |
2349 // | atomic | taskyield | |
2350 // | atomic | barrier | |
2351 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002352 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002353 // | atomic | flush | |
2354 // | atomic | ordered | |
2355 // | atomic | atomic | |
2356 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002357 // | atomic | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002358 // | atomic | target parallel | |
2359 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002360 // | atomic | target enter | |
2361 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002362 // | atomic | target exit | |
2363 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002364 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002365 // | atomic | cancellation | |
2366 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002367 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002368 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002369 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002370 // | atomic | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002371 // +------------------+-----------------+------------------------------------+
2372 // | target | parallel | * |
2373 // | target | for | * |
2374 // | target | for simd | * |
2375 // | target | master | * |
2376 // | target | critical | * |
2377 // | target | simd | * |
2378 // | target | sections | * |
2379 // | target | section | * |
2380 // | target | single | * |
2381 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002382 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002383 // | target |parallel sections| * |
2384 // | target | task | * |
2385 // | target | taskyield | * |
2386 // | target | barrier | * |
2387 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002388 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002389 // | target | flush | * |
2390 // | target | ordered | * |
2391 // | target | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002392 // | target | target | |
2393 // | target | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002394 // | target | target parallel | |
2395 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002396 // | target | target enter | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002397 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002398 // | target | target exit | |
Samuel Antao72590762016-01-19 20:04:50 +00002399 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002400 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002401 // | target | cancellation | |
2402 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002403 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002404 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002405 // | target | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002406 // | target | distribute | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002407 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002408 // | target parallel | parallel | * |
2409 // | target parallel | for | * |
2410 // | target parallel | for simd | * |
2411 // | target parallel | master | * |
2412 // | target parallel | critical | * |
2413 // | target parallel | simd | * |
2414 // | target parallel | sections | * |
2415 // | target parallel | section | * |
2416 // | target parallel | single | * |
2417 // | target parallel | parallel for | * |
2418 // | target parallel |parallel for simd| * |
2419 // | target parallel |parallel sections| * |
2420 // | target parallel | task | * |
2421 // | target parallel | taskyield | * |
2422 // | target parallel | barrier | * |
2423 // | target parallel | taskwait | * |
2424 // | target parallel | taskgroup | * |
2425 // | target parallel | flush | * |
2426 // | target parallel | ordered | * |
2427 // | target parallel | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002428 // | target parallel | target | |
2429 // | target parallel | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002430 // | target parallel | target parallel | |
2431 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002432 // | target parallel | target enter | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002433 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002434 // | target parallel | target exit | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002435 // | | data | |
2436 // | target parallel | teams | |
2437 // | target parallel | cancellation | |
2438 // | | point | ! |
2439 // | target parallel | cancel | ! |
2440 // | target parallel | taskloop | * |
2441 // | target parallel | taskloop simd | * |
2442 // | target parallel | distribute | |
2443 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002444 // | target parallel | parallel | * |
2445 // | for | | |
2446 // | target parallel | for | * |
2447 // | for | | |
2448 // | target parallel | for simd | * |
2449 // | for | | |
2450 // | target parallel | master | * |
2451 // | for | | |
2452 // | target parallel | critical | * |
2453 // | for | | |
2454 // | target parallel | simd | * |
2455 // | for | | |
2456 // | target parallel | sections | * |
2457 // | for | | |
2458 // | target parallel | section | * |
2459 // | for | | |
2460 // | target parallel | single | * |
2461 // | for | | |
2462 // | target parallel | parallel for | * |
2463 // | for | | |
2464 // | target parallel |parallel for simd| * |
2465 // | for | | |
2466 // | target parallel |parallel sections| * |
2467 // | for | | |
2468 // | target parallel | task | * |
2469 // | for | | |
2470 // | target parallel | taskyield | * |
2471 // | for | | |
2472 // | target parallel | barrier | * |
2473 // | for | | |
2474 // | target parallel | taskwait | * |
2475 // | for | | |
2476 // | target parallel | taskgroup | * |
2477 // | for | | |
2478 // | target parallel | flush | * |
2479 // | for | | |
2480 // | target parallel | ordered | * |
2481 // | for | | |
2482 // | target parallel | atomic | * |
2483 // | for | | |
2484 // | target parallel | target | |
2485 // | for | | |
2486 // | target parallel | target parallel | |
2487 // | for | | |
2488 // | target parallel | target parallel | |
2489 // | for | for | |
2490 // | target parallel | target enter | |
2491 // | for | data | |
2492 // | target parallel | target exit | |
2493 // | for | data | |
2494 // | target parallel | teams | |
2495 // | for | | |
2496 // | target parallel | cancellation | |
2497 // | for | point | ! |
2498 // | target parallel | cancel | ! |
2499 // | for | | |
2500 // | target parallel | taskloop | * |
2501 // | for | | |
2502 // | target parallel | taskloop simd | * |
2503 // | for | | |
2504 // | target parallel | distribute | |
2505 // | for | | |
2506 // +------------------+-----------------+------------------------------------+
Alexey Bataev13314bf2014-10-09 04:18:56 +00002507 // | teams | parallel | * |
2508 // | teams | for | + |
2509 // | teams | for simd | + |
2510 // | teams | master | + |
2511 // | teams | critical | + |
2512 // | teams | simd | + |
2513 // | teams | sections | + |
2514 // | teams | section | + |
2515 // | teams | single | + |
2516 // | teams | parallel for | * |
2517 // | teams |parallel for simd| * |
2518 // | teams |parallel sections| * |
2519 // | teams | task | + |
2520 // | teams | taskyield | + |
2521 // | teams | barrier | + |
2522 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002523 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002524 // | teams | flush | + |
2525 // | teams | ordered | + |
2526 // | teams | atomic | + |
2527 // | teams | target | + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002528 // | teams | target parallel | + |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002529 // | teams | target parallel | + |
2530 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002531 // | teams | target enter | + |
2532 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002533 // | teams | target exit | + |
2534 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002535 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002536 // | teams | cancellation | |
2537 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002538 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002539 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002540 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002541 // | teams | distribute | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002542 // +------------------+-----------------+------------------------------------+
2543 // | taskloop | parallel | * |
2544 // | taskloop | for | + |
2545 // | taskloop | for simd | + |
2546 // | taskloop | master | + |
2547 // | taskloop | critical | * |
2548 // | taskloop | simd | * |
2549 // | taskloop | sections | + |
2550 // | taskloop | section | + |
2551 // | taskloop | single | + |
2552 // | taskloop | parallel for | * |
2553 // | taskloop |parallel for simd| * |
2554 // | taskloop |parallel sections| * |
2555 // | taskloop | task | * |
2556 // | taskloop | taskyield | * |
2557 // | taskloop | barrier | + |
2558 // | taskloop | taskwait | * |
2559 // | taskloop | taskgroup | * |
2560 // | taskloop | flush | * |
2561 // | taskloop | ordered | + |
2562 // | taskloop | atomic | * |
2563 // | taskloop | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002564 // | taskloop | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002565 // | taskloop | target parallel | * |
2566 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002567 // | taskloop | target enter | * |
2568 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002569 // | taskloop | target exit | * |
2570 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002571 // | taskloop | teams | + |
2572 // | taskloop | cancellation | |
2573 // | | point | |
2574 // | taskloop | cancel | |
2575 // | taskloop | taskloop | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002576 // | taskloop | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002577 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002578 // | taskloop simd | parallel | |
2579 // | taskloop simd | for | |
2580 // | taskloop simd | for simd | |
2581 // | taskloop simd | master | |
2582 // | taskloop simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002583 // | taskloop simd | simd | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002584 // | taskloop simd | sections | |
2585 // | taskloop simd | section | |
2586 // | taskloop simd | single | |
2587 // | taskloop simd | parallel for | |
2588 // | taskloop simd |parallel for simd| |
2589 // | taskloop simd |parallel sections| |
2590 // | taskloop simd | task | |
2591 // | taskloop simd | taskyield | |
2592 // | taskloop simd | barrier | |
2593 // | taskloop simd | taskwait | |
2594 // | taskloop simd | taskgroup | |
2595 // | taskloop simd | flush | |
2596 // | taskloop simd | ordered | + (with simd clause) |
2597 // | taskloop simd | atomic | |
2598 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002599 // | taskloop simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002600 // | taskloop simd | target parallel | |
2601 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002602 // | taskloop simd | target enter | |
2603 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002604 // | taskloop simd | target exit | |
2605 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002606 // | taskloop simd | teams | |
2607 // | taskloop simd | cancellation | |
2608 // | | point | |
2609 // | taskloop simd | cancel | |
2610 // | taskloop simd | taskloop | |
2611 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002612 // | taskloop simd | distribute | |
2613 // +------------------+-----------------+------------------------------------+
2614 // | distribute | parallel | * |
2615 // | distribute | for | * |
2616 // | distribute | for simd | * |
2617 // | distribute | master | * |
2618 // | distribute | critical | * |
2619 // | distribute | simd | * |
2620 // | distribute | sections | * |
2621 // | distribute | section | * |
2622 // | distribute | single | * |
2623 // | distribute | parallel for | * |
2624 // | distribute |parallel for simd| * |
2625 // | distribute |parallel sections| * |
2626 // | distribute | task | * |
2627 // | distribute | taskyield | * |
2628 // | distribute | barrier | * |
2629 // | distribute | taskwait | * |
2630 // | distribute | taskgroup | * |
2631 // | distribute | flush | * |
2632 // | distribute | ordered | + |
2633 // | distribute | atomic | * |
2634 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002635 // | distribute | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002636 // | distribute | target parallel | |
2637 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002638 // | distribute | target enter | |
2639 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002640 // | distribute | target exit | |
2641 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002642 // | distribute | teams | |
2643 // | distribute | cancellation | + |
2644 // | | point | |
2645 // | distribute | cancel | + |
2646 // | distribute | taskloop | * |
2647 // | distribute | taskloop simd | * |
2648 // | distribute | distribute | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002649 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002650 if (Stack->getCurScope()) {
2651 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002652 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002653 bool NestingProhibited = false;
2654 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002655 enum {
2656 NoRecommend,
2657 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002658 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002659 ShouldBeInTargetRegion,
2660 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002661 } Recommend = NoRecommend;
Alexey Bataev1f092212016-02-02 04:59:52 +00002662 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered &&
2663 CurrentRegion != OMPD_simd) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002664 // OpenMP [2.16, Nesting of Regions]
2665 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002666 // OpenMP [2.8.1,simd Construct, Restrictions]
2667 // An ordered construct with the simd clause is the only OpenMP construct
2668 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002669 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2670 return true;
2671 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002672 if (ParentRegion == OMPD_atomic) {
2673 // OpenMP [2.16, Nesting of Regions]
2674 // OpenMP constructs may not be nested inside an atomic region.
2675 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2676 return true;
2677 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002678 if (CurrentRegion == OMPD_section) {
2679 // OpenMP [2.7.2, sections Construct, Restrictions]
2680 // Orphaned section directives are prohibited. That is, the section
2681 // directives must appear within the sections construct and must not be
2682 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002683 if (ParentRegion != OMPD_sections &&
2684 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002685 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2686 << (ParentRegion != OMPD_unknown)
2687 << getOpenMPDirectiveName(ParentRegion);
2688 return true;
2689 }
2690 return false;
2691 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002692 // Allow some constructs to be orphaned (they could be used in functions,
2693 // called from OpenMP regions with the required preconditions).
2694 if (ParentRegion == OMPD_unknown)
2695 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002696 if (CurrentRegion == OMPD_cancellation_point ||
2697 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002698 // OpenMP [2.16, Nesting of Regions]
2699 // A cancellation point construct for which construct-type-clause is
2700 // taskgroup must be nested inside a task construct. A cancellation
2701 // point construct for which construct-type-clause is not taskgroup must
2702 // be closely nested inside an OpenMP construct that matches the type
2703 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002704 // A cancel construct for which construct-type-clause is taskgroup must be
2705 // nested inside a task construct. A cancel construct for which
2706 // construct-type-clause is not taskgroup must be closely nested inside an
2707 // OpenMP construct that matches the type specified in
2708 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002709 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002710 !((CancelRegion == OMPD_parallel &&
2711 (ParentRegion == OMPD_parallel ||
2712 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002713 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002714 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
2715 ParentRegion == OMPD_target_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002716 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2717 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002718 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2719 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002720 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002721 // OpenMP [2.16, Nesting of Regions]
2722 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002723 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002724 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002725 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002726 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002727 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2728 // OpenMP [2.16, Nesting of Regions]
2729 // A critical region may not be nested (closely or otherwise) inside a
2730 // critical region with the same name. Note that this restriction is not
2731 // sufficient to prevent deadlock.
2732 SourceLocation PreviousCriticalLoc;
2733 bool DeadLock =
2734 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2735 OpenMPDirectiveKind K,
2736 const DeclarationNameInfo &DNI,
2737 SourceLocation Loc)
2738 ->bool {
2739 if (K == OMPD_critical &&
2740 DNI.getName() == CurrentName.getName()) {
2741 PreviousCriticalLoc = Loc;
2742 return true;
2743 } else
2744 return false;
2745 },
2746 false /* skip top directive */);
2747 if (DeadLock) {
2748 SemaRef.Diag(StartLoc,
2749 diag::err_omp_prohibited_region_critical_same_name)
2750 << CurrentName.getName();
2751 if (PreviousCriticalLoc.isValid())
2752 SemaRef.Diag(PreviousCriticalLoc,
2753 diag::note_omp_previous_critical_region);
2754 return true;
2755 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002756 } else if (CurrentRegion == OMPD_barrier) {
2757 // OpenMP [2.16, Nesting of Regions]
2758 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002759 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002760 NestingProhibited =
2761 isOpenMPWorksharingDirective(ParentRegion) ||
2762 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002763 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002764 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musman80c22892014-07-17 08:54:58 +00002765 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002766 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002767 // OpenMP [2.16, Nesting of Regions]
2768 // A worksharing region may not be closely nested inside a worksharing,
2769 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002770 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00002771 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002772 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002773 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002774 isOpenMPTaskLoopDirective(ParentRegion);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002775 Recommend = ShouldBeInParallelRegion;
2776 } else if (CurrentRegion == OMPD_ordered) {
2777 // OpenMP [2.16, Nesting of Regions]
2778 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002779 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002780 // An ordered region must be closely nested inside a loop region (or
2781 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002782 // OpenMP [2.8.1,simd Construct, Restrictions]
2783 // An ordered construct with the simd clause is the only OpenMP construct
2784 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002785 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00002786 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002787 isOpenMPTaskLoopDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002788 !(isOpenMPSimdDirective(ParentRegion) ||
2789 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002790 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002791 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2792 // OpenMP [2.16, Nesting of Regions]
2793 // If specified, a teams construct must be contained within a target
2794 // construct.
2795 NestingProhibited = ParentRegion != OMPD_target;
2796 Recommend = ShouldBeInTargetRegion;
2797 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2798 }
2799 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2800 // OpenMP [2.16, Nesting of Regions]
2801 // distribute, parallel, parallel sections, parallel workshare, and the
2802 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2803 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002804 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2805 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002806 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002807 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002808 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2809 // OpenMP 4.5 [2.17 Nesting of Regions]
2810 // The region associated with the distribute construct must be strictly
2811 // nested inside a teams region
2812 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2813 Recommend = ShouldBeInTeamsRegion;
2814 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002815 if (!NestingProhibited &&
2816 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2817 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2818 // OpenMP 4.5 [2.17 Nesting of Regions]
2819 // If a target, target update, target data, target enter data, or
2820 // target exit data construct is encountered during execution of a
2821 // target region, the behavior is unspecified.
2822 NestingProhibited = Stack->hasDirective(
2823 [&OffendingRegion](OpenMPDirectiveKind K,
2824 const DeclarationNameInfo &DNI,
2825 SourceLocation Loc) -> bool {
2826 if (isOpenMPTargetExecutionDirective(K)) {
2827 OffendingRegion = K;
2828 return true;
2829 } else
2830 return false;
2831 },
2832 false /* don't skip top directive */);
2833 CloseNesting = false;
2834 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002835 if (NestingProhibited) {
2836 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002837 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2838 << Recommend << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002839 return true;
2840 }
2841 }
2842 return false;
2843}
2844
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002845static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2846 ArrayRef<OMPClause *> Clauses,
2847 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2848 bool ErrorFound = false;
2849 unsigned NamedModifiersNumber = 0;
2850 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2851 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002852 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002853 for (const auto *C : Clauses) {
2854 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2855 // At most one if clause without a directive-name-modifier can appear on
2856 // the directive.
2857 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2858 if (FoundNameModifiers[CurNM]) {
2859 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2860 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2861 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2862 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002863 } else if (CurNM != OMPD_unknown) {
2864 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002865 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002866 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002867 FoundNameModifiers[CurNM] = IC;
2868 if (CurNM == OMPD_unknown)
2869 continue;
2870 // Check if the specified name modifier is allowed for the current
2871 // directive.
2872 // At most one if clause with the particular directive-name-modifier can
2873 // appear on the directive.
2874 bool MatchFound = false;
2875 for (auto NM : AllowedNameModifiers) {
2876 if (CurNM == NM) {
2877 MatchFound = true;
2878 break;
2879 }
2880 }
2881 if (!MatchFound) {
2882 S.Diag(IC->getNameModifierLoc(),
2883 diag::err_omp_wrong_if_directive_name_modifier)
2884 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2885 ErrorFound = true;
2886 }
2887 }
2888 }
2889 // If any if clause on the directive includes a directive-name-modifier then
2890 // all if clauses on the directive must include a directive-name-modifier.
2891 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2892 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2893 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2894 diag::err_omp_no_more_if_clause);
2895 } else {
2896 std::string Values;
2897 std::string Sep(", ");
2898 unsigned AllowedCnt = 0;
2899 unsigned TotalAllowedNum =
2900 AllowedNameModifiers.size() - NamedModifiersNumber;
2901 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2902 ++Cnt) {
2903 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2904 if (!FoundNameModifiers[NM]) {
2905 Values += "'";
2906 Values += getOpenMPDirectiveName(NM);
2907 Values += "'";
2908 if (AllowedCnt + 2 == TotalAllowedNum)
2909 Values += " or ";
2910 else if (AllowedCnt + 1 != TotalAllowedNum)
2911 Values += Sep;
2912 ++AllowedCnt;
2913 }
2914 }
2915 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2916 diag::err_omp_unnamed_if_clause)
2917 << (TotalAllowedNum > 1) << Values;
2918 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002919 for (auto Loc : NameModifierLoc) {
2920 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2921 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002922 ErrorFound = true;
2923 }
2924 return ErrorFound;
2925}
2926
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002927StmtResult Sema::ActOnOpenMPExecutableDirective(
2928 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2929 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2930 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002931 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002932 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2933 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002934 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002935
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002936 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002937 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002938 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002939 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002940 if (AStmt) {
2941 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2942
2943 // Check default data sharing attributes for referenced variables.
2944 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2945 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2946 if (DSAChecker.isErrorFound())
2947 return StmtError();
2948 // Generate list of implicitly defined firstprivate variables.
2949 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002950
2951 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2952 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2953 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2954 SourceLocation(), SourceLocation())) {
2955 ClausesWithImplicit.push_back(Implicit);
2956 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2957 DSAChecker.getImplicitFirstprivate().size();
2958 } else
2959 ErrorFound = true;
2960 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002961 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002962
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002963 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002964 switch (Kind) {
2965 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002966 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2967 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002968 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002969 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002970 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002971 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2972 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002973 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002974 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002975 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2976 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002977 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002978 case OMPD_for_simd:
2979 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2980 EndLoc, VarsWithInheritedDSA);
2981 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002982 case OMPD_sections:
2983 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2984 EndLoc);
2985 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002986 case OMPD_section:
2987 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002988 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002989 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2990 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002991 case OMPD_single:
2992 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2993 EndLoc);
2994 break;
Alexander Musman80c22892014-07-17 08:54:58 +00002995 case OMPD_master:
2996 assert(ClausesWithImplicit.empty() &&
2997 "No clauses are allowed for 'omp master' directive");
2998 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2999 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003000 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003001 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3002 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003003 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003004 case OMPD_parallel_for:
3005 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3006 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003007 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003008 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003009 case OMPD_parallel_for_simd:
3010 Res = ActOnOpenMPParallelForSimdDirective(
3011 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003012 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003013 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003014 case OMPD_parallel_sections:
3015 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3016 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003017 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003018 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003019 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003020 Res =
3021 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003022 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003023 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003024 case OMPD_taskyield:
3025 assert(ClausesWithImplicit.empty() &&
3026 "No clauses are allowed for 'omp taskyield' directive");
3027 assert(AStmt == nullptr &&
3028 "No associated statement allowed for 'omp taskyield' directive");
3029 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3030 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003031 case OMPD_barrier:
3032 assert(ClausesWithImplicit.empty() &&
3033 "No clauses are allowed for 'omp barrier' directive");
3034 assert(AStmt == nullptr &&
3035 "No associated statement allowed for 'omp barrier' directive");
3036 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3037 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003038 case OMPD_taskwait:
3039 assert(ClausesWithImplicit.empty() &&
3040 "No clauses are allowed for 'omp taskwait' directive");
3041 assert(AStmt == nullptr &&
3042 "No associated statement allowed for 'omp taskwait' directive");
3043 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3044 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003045 case OMPD_taskgroup:
3046 assert(ClausesWithImplicit.empty() &&
3047 "No clauses are allowed for 'omp taskgroup' directive");
3048 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3049 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003050 case OMPD_flush:
3051 assert(AStmt == nullptr &&
3052 "No associated statement allowed for 'omp flush' directive");
3053 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3054 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003055 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003056 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3057 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003058 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003059 case OMPD_atomic:
3060 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3061 EndLoc);
3062 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003063 case OMPD_teams:
3064 Res =
3065 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3066 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003067 case OMPD_target:
3068 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3069 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003070 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003071 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003072 case OMPD_target_parallel:
3073 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3074 StartLoc, EndLoc);
3075 AllowedNameModifiers.push_back(OMPD_target);
3076 AllowedNameModifiers.push_back(OMPD_parallel);
3077 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003078 case OMPD_target_parallel_for:
3079 Res = ActOnOpenMPTargetParallelForDirective(
3080 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3081 AllowedNameModifiers.push_back(OMPD_target);
3082 AllowedNameModifiers.push_back(OMPD_parallel);
3083 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003084 case OMPD_cancellation_point:
3085 assert(ClausesWithImplicit.empty() &&
3086 "No clauses are allowed for 'omp cancellation point' directive");
3087 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3088 "cancellation point' directive");
3089 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3090 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003091 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003092 assert(AStmt == nullptr &&
3093 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003094 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3095 CancelRegion);
3096 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003097 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003098 case OMPD_target_data:
3099 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3100 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003101 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003102 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003103 case OMPD_target_enter_data:
3104 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3105 EndLoc);
3106 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3107 break;
Samuel Antao72590762016-01-19 20:04:50 +00003108 case OMPD_target_exit_data:
3109 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3110 EndLoc);
3111 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3112 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003113 case OMPD_taskloop:
3114 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3115 EndLoc, VarsWithInheritedDSA);
3116 AllowedNameModifiers.push_back(OMPD_taskloop);
3117 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003118 case OMPD_taskloop_simd:
3119 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3120 EndLoc, VarsWithInheritedDSA);
3121 AllowedNameModifiers.push_back(OMPD_taskloop);
3122 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003123 case OMPD_distribute:
3124 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3125 EndLoc, VarsWithInheritedDSA);
3126 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003127 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003128 llvm_unreachable("OpenMP Directive is not allowed");
3129 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003130 llvm_unreachable("Unknown OpenMP directive");
3131 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003132
Alexey Bataev4acb8592014-07-07 13:01:15 +00003133 for (auto P : VarsWithInheritedDSA) {
3134 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3135 << P.first << P.second->getSourceRange();
3136 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003137 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3138
3139 if (!AllowedNameModifiers.empty())
3140 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3141 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003142
Alexey Bataeved09d242014-05-28 05:53:51 +00003143 if (ErrorFound)
3144 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003145 return Res;
3146}
3147
3148StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3149 Stmt *AStmt,
3150 SourceLocation StartLoc,
3151 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003152 if (!AStmt)
3153 return StmtError();
3154
Alexey Bataev9959db52014-05-06 10:08:46 +00003155 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3156 // 1.2.2 OpenMP Language Terminology
3157 // Structured block - An executable statement with a single entry at the
3158 // top and a single exit at the bottom.
3159 // The point of exit cannot be a branch out of the structured block.
3160 // longjmp() and throw() must not violate the entry/exit criteria.
3161 CS->getCapturedDecl()->setNothrow();
3162
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003163 getCurFunction()->setHasBranchProtectedScope();
3164
Alexey Bataev25e5b442015-09-15 12:52:43 +00003165 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3166 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003167}
3168
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003169namespace {
3170/// \brief Helper class for checking canonical form of the OpenMP loops and
3171/// extracting iteration space of each loop in the loop nest, that will be used
3172/// for IR generation.
3173class OpenMPIterationSpaceChecker {
3174 /// \brief Reference to Sema.
3175 Sema &SemaRef;
3176 /// \brief A location for diagnostics (when there is no some better location).
3177 SourceLocation DefaultLoc;
3178 /// \brief A location for diagnostics (when increment is not compatible).
3179 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003180 /// \brief A source location for referring to loop init later.
3181 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003182 /// \brief A source location for referring to condition later.
3183 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003184 /// \brief A source location for referring to increment later.
3185 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003186 /// \brief Loop variable.
3187 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003188 /// \brief Reference to loop variable.
3189 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003190 /// \brief Lower bound (initializer for the var).
3191 Expr *LB;
3192 /// \brief Upper bound.
3193 Expr *UB;
3194 /// \brief Loop step (increment).
3195 Expr *Step;
3196 /// \brief This flag is true when condition is one of:
3197 /// Var < UB
3198 /// Var <= UB
3199 /// UB > Var
3200 /// UB >= Var
3201 bool TestIsLessOp;
3202 /// \brief This flag is true when condition is strict ( < or > ).
3203 bool TestIsStrictOp;
3204 /// \brief This flag is true when step is subtracted on each iteration.
3205 bool SubtractStep;
3206
3207public:
3208 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
3209 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003210 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
3211 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003212 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
3213 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003214 /// \brief Check init-expr for canonical loop form and save loop counter
3215 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00003216 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003217 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3218 /// for less/greater and for strict/non-strict comparison.
3219 bool CheckCond(Expr *S);
3220 /// \brief Check incr-expr for canonical loop form and return true if it
3221 /// does not conform, otherwise save loop step (#Step).
3222 bool CheckInc(Expr *S);
3223 /// \brief Return the loop counter variable.
3224 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003225 /// \brief Return the reference expression to loop counter variable.
3226 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003227 /// \brief Source range of the loop init.
3228 SourceRange GetInitSrcRange() const { return InitSrcRange; }
3229 /// \brief Source range of the loop condition.
3230 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3231 /// \brief Source range of the loop increment.
3232 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3233 /// \brief True if the step should be subtracted.
3234 bool ShouldSubtractStep() const { return SubtractStep; }
3235 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003236 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003237 /// \brief Build the precondition expression for the loops.
3238 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003239 /// \brief Build reference expression to the counter be used for codegen.
3240 Expr *BuildCounterVar() const;
Alexey Bataeva8899172015-08-06 12:30:57 +00003241 /// \brief Build reference expression to the private counter be used for
3242 /// codegen.
3243 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003244 /// \brief Build initization of the counter be used for codegen.
3245 Expr *BuildCounterInit() const;
3246 /// \brief Build step of the counter be used for codegen.
3247 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003248 /// \brief Return true if any expression is dependent.
3249 bool Dependent() const;
3250
3251private:
3252 /// \brief Check the right-hand side of an assignment in the increment
3253 /// expression.
3254 bool CheckIncRHS(Expr *RHS);
3255 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003256 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003257 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00003258 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003259 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003260 /// \brief Helper to set loop increment.
3261 bool SetStep(Expr *NewStep, bool Subtract);
3262};
3263
3264bool OpenMPIterationSpaceChecker::Dependent() const {
3265 if (!Var) {
3266 assert(!LB && !UB && !Step);
3267 return false;
3268 }
3269 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
3270 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
3271}
3272
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003273template <typename T>
3274static T *getExprAsWritten(T *E) {
3275 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
3276 E = ExprTemp->getSubExpr();
3277
3278 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
3279 E = MTE->GetTemporaryExpr();
3280
3281 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
3282 E = Binder->getSubExpr();
3283
3284 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
3285 E = ICE->getSubExprAsWritten();
3286 return E->IgnoreParens();
3287}
3288
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003289bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
3290 DeclRefExpr *NewVarRefExpr,
3291 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003292 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003293 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
3294 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003295 if (!NewVar || !NewLB)
3296 return true;
3297 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003298 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003299 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3300 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003301 if ((Ctor->isCopyOrMoveConstructor() ||
3302 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3303 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003304 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003305 LB = NewLB;
3306 return false;
3307}
3308
3309bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00003310 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003311 // State consistency checking to ensure correct usage.
3312 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
3313 !TestIsLessOp && !TestIsStrictOp);
3314 if (!NewUB)
3315 return true;
3316 UB = NewUB;
3317 TestIsLessOp = LessOp;
3318 TestIsStrictOp = StrictOp;
3319 ConditionSrcRange = SR;
3320 ConditionLoc = SL;
3321 return false;
3322}
3323
3324bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3325 // State consistency checking to ensure correct usage.
3326 assert(Var != nullptr && LB != nullptr && Step == nullptr);
3327 if (!NewStep)
3328 return true;
3329 if (!NewStep->isValueDependent()) {
3330 // Check that the step is integer expression.
3331 SourceLocation StepLoc = NewStep->getLocStart();
3332 ExprResult Val =
3333 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
3334 if (Val.isInvalid())
3335 return true;
3336 NewStep = Val.get();
3337
3338 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3339 // If test-expr is of form var relational-op b and relational-op is < or
3340 // <= then incr-expr must cause var to increase on each iteration of the
3341 // loop. If test-expr is of form var relational-op b and relational-op is
3342 // > or >= then incr-expr must cause var to decrease on each iteration of
3343 // the loop.
3344 // If test-expr is of form b relational-op var and relational-op is < or
3345 // <= then incr-expr must cause var to decrease on each iteration of the
3346 // loop. If test-expr is of form b relational-op var and relational-op is
3347 // > or >= then incr-expr must cause var to increase on each iteration of
3348 // the loop.
3349 llvm::APSInt Result;
3350 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3351 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3352 bool IsConstNeg =
3353 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003354 bool IsConstPos =
3355 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003356 bool IsConstZero = IsConstant && !Result.getBoolValue();
3357 if (UB && (IsConstZero ||
3358 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00003359 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003360 SemaRef.Diag(NewStep->getExprLoc(),
3361 diag::err_omp_loop_incr_not_compatible)
3362 << Var << TestIsLessOp << NewStep->getSourceRange();
3363 SemaRef.Diag(ConditionLoc,
3364 diag::note_omp_loop_cond_requres_compatible_incr)
3365 << TestIsLessOp << ConditionSrcRange;
3366 return true;
3367 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003368 if (TestIsLessOp == Subtract) {
3369 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
3370 NewStep).get();
3371 Subtract = !Subtract;
3372 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003373 }
3374
3375 Step = NewStep;
3376 SubtractStep = Subtract;
3377 return false;
3378}
3379
Alexey Bataev9c821032015-04-30 04:23:23 +00003380bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003381 // Check init-expr for canonical loop form and save loop counter
3382 // variable - #Var and its initialization value - #LB.
3383 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3384 // var = lb
3385 // integer-type var = lb
3386 // random-access-iterator-type var = lb
3387 // pointer-type var = lb
3388 //
3389 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003390 if (EmitDiags) {
3391 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3392 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003393 return true;
3394 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003395 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003396 if (Expr *E = dyn_cast<Expr>(S))
3397 S = E->IgnoreParens();
3398 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3399 if (BO->getOpcode() == BO_Assign)
3400 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003401 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003402 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003403 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
3404 if (DS->isSingleDecl()) {
3405 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00003406 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003407 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00003408 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003409 SemaRef.Diag(S->getLocStart(),
3410 diag::ext_omp_loop_not_canonical_init)
3411 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003412 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003413 }
3414 }
3415 }
3416 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
3417 if (CE->getOperator() == OO_Equal)
3418 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003419 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
3420 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003421
Alexey Bataev9c821032015-04-30 04:23:23 +00003422 if (EmitDiags) {
3423 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3424 << S->getSourceRange();
3425 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003426 return true;
3427}
3428
Alexey Bataev23b69422014-06-18 07:08:49 +00003429/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003430/// variable (which may be the loop variable) if possible.
3431static const VarDecl *GetInitVarDecl(const Expr *E) {
3432 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00003433 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003434 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003435 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3436 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003437 if ((Ctor->isCopyOrMoveConstructor() ||
3438 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3439 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003440 E = CE->getArg(0)->IgnoreParenImpCasts();
3441 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
3442 if (!DRE)
3443 return nullptr;
3444 return dyn_cast<VarDecl>(DRE->getDecl());
3445}
3446
3447bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3448 // Check test-expr for canonical form, save upper-bound UB, flags for
3449 // less/greater and for strict/non-strict comparison.
3450 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3451 // var relational-op b
3452 // b relational-op var
3453 //
3454 if (!S) {
3455 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
3456 return true;
3457 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003458 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003459 SourceLocation CondLoc = S->getLocStart();
3460 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3461 if (BO->isRelationalOp()) {
3462 if (GetInitVarDecl(BO->getLHS()) == Var)
3463 return SetUB(BO->getRHS(),
3464 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3465 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3466 BO->getSourceRange(), BO->getOperatorLoc());
3467 if (GetInitVarDecl(BO->getRHS()) == Var)
3468 return SetUB(BO->getLHS(),
3469 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3470 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3471 BO->getSourceRange(), BO->getOperatorLoc());
3472 }
3473 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3474 if (CE->getNumArgs() == 2) {
3475 auto Op = CE->getOperator();
3476 switch (Op) {
3477 case OO_Greater:
3478 case OO_GreaterEqual:
3479 case OO_Less:
3480 case OO_LessEqual:
3481 if (GetInitVarDecl(CE->getArg(0)) == Var)
3482 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3483 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3484 CE->getOperatorLoc());
3485 if (GetInitVarDecl(CE->getArg(1)) == Var)
3486 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3487 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3488 CE->getOperatorLoc());
3489 break;
3490 default:
3491 break;
3492 }
3493 }
3494 }
3495 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
3496 << S->getSourceRange() << Var;
3497 return true;
3498}
3499
3500bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3501 // RHS of canonical loop form increment can be:
3502 // var + incr
3503 // incr + var
3504 // var - incr
3505 //
3506 RHS = RHS->IgnoreParenImpCasts();
3507 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3508 if (BO->isAdditiveOp()) {
3509 bool IsAdd = BO->getOpcode() == BO_Add;
3510 if (GetInitVarDecl(BO->getLHS()) == Var)
3511 return SetStep(BO->getRHS(), !IsAdd);
3512 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
3513 return SetStep(BO->getLHS(), false);
3514 }
3515 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3516 bool IsAdd = CE->getOperator() == OO_Plus;
3517 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
3518 if (GetInitVarDecl(CE->getArg(0)) == Var)
3519 return SetStep(CE->getArg(1), !IsAdd);
3520 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
3521 return SetStep(CE->getArg(0), false);
3522 }
3523 }
3524 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3525 << RHS->getSourceRange() << Var;
3526 return true;
3527}
3528
3529bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3530 // Check incr-expr for canonical loop form and return true if it
3531 // does not conform.
3532 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3533 // ++var
3534 // var++
3535 // --var
3536 // var--
3537 // var += incr
3538 // var -= incr
3539 // var = var + incr
3540 // var = incr + var
3541 // var = var - incr
3542 //
3543 if (!S) {
3544 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
3545 return true;
3546 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003547 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003548 S = S->IgnoreParens();
3549 if (auto UO = dyn_cast<UnaryOperator>(S)) {
3550 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
3551 return SetStep(
3552 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3553 (UO->isDecrementOp() ? -1 : 1)).get(),
3554 false);
3555 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3556 switch (BO->getOpcode()) {
3557 case BO_AddAssign:
3558 case BO_SubAssign:
3559 if (GetInitVarDecl(BO->getLHS()) == Var)
3560 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3561 break;
3562 case BO_Assign:
3563 if (GetInitVarDecl(BO->getLHS()) == Var)
3564 return CheckIncRHS(BO->getRHS());
3565 break;
3566 default:
3567 break;
3568 }
3569 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3570 switch (CE->getOperator()) {
3571 case OO_PlusPlus:
3572 case OO_MinusMinus:
3573 if (GetInitVarDecl(CE->getArg(0)) == Var)
3574 return SetStep(
3575 SemaRef.ActOnIntegerConstant(
3576 CE->getLocStart(),
3577 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3578 false);
3579 break;
3580 case OO_PlusEqual:
3581 case OO_MinusEqual:
3582 if (GetInitVarDecl(CE->getArg(0)) == Var)
3583 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3584 break;
3585 case OO_Equal:
3586 if (GetInitVarDecl(CE->getArg(0)) == Var)
3587 return CheckIncRHS(CE->getArg(1));
3588 break;
3589 default:
3590 break;
3591 }
3592 }
3593 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3594 << S->getSourceRange() << Var;
3595 return true;
3596}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003597
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003598namespace {
3599// Transform variables declared in GNU statement expressions to new ones to
3600// avoid crash on codegen.
3601class TransformToNewDefs : public TreeTransform<TransformToNewDefs> {
3602 typedef TreeTransform<TransformToNewDefs> BaseTransform;
3603
3604public:
3605 TransformToNewDefs(Sema &SemaRef) : BaseTransform(SemaRef) {}
3606
3607 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
3608 if (auto *VD = cast<VarDecl>(D))
3609 if (!isa<ParmVarDecl>(D) && !isa<VarTemplateSpecializationDecl>(D) &&
3610 !isa<ImplicitParamDecl>(D)) {
3611 auto *NewVD = VarDecl::Create(
3612 SemaRef.Context, VD->getDeclContext(), VD->getLocStart(),
3613 VD->getLocation(), VD->getIdentifier(), VD->getType(),
3614 VD->getTypeSourceInfo(), VD->getStorageClass());
3615 NewVD->setTSCSpec(VD->getTSCSpec());
3616 NewVD->setInit(VD->getInit());
3617 NewVD->setInitStyle(VD->getInitStyle());
3618 NewVD->setExceptionVariable(VD->isExceptionVariable());
3619 NewVD->setNRVOVariable(VD->isNRVOVariable());
3620 NewVD->setCXXForRangeDecl(VD->isInExternCXXContext());
3621 NewVD->setConstexpr(VD->isConstexpr());
3622 NewVD->setInitCapture(VD->isInitCapture());
3623 NewVD->setPreviousDeclInSameBlockScope(
3624 VD->isPreviousDeclInSameBlockScope());
3625 VD->getDeclContext()->addHiddenDecl(NewVD);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003626 if (VD->hasAttrs())
3627 NewVD->setAttrs(VD->getAttrs());
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003628 transformedLocalDecl(VD, NewVD);
3629 return NewVD;
3630 }
3631 return BaseTransform::TransformDefinition(Loc, D);
3632 }
3633
3634 ExprResult TransformDeclRefExpr(DeclRefExpr *E) {
3635 if (auto *NewD = TransformDecl(E->getExprLoc(), E->getDecl()))
3636 if (E->getDecl() != NewD) {
3637 NewD->setReferenced();
3638 NewD->markUsed(SemaRef.Context);
3639 return DeclRefExpr::Create(
3640 SemaRef.Context, E->getQualifierLoc(), E->getTemplateKeywordLoc(),
3641 cast<ValueDecl>(NewD), E->refersToEnclosingVariableOrCapture(),
3642 E->getNameInfo(), E->getType(), E->getValueKind());
3643 }
3644 return BaseTransform::TransformDeclRefExpr(E);
3645 }
3646};
3647}
3648
Alexander Musmana5f070a2014-10-01 06:03:56 +00003649/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003650Expr *
3651OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
3652 const bool LimitedType) const {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003653 TransformToNewDefs Transform(SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003654 ExprResult Diff;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003655 auto VarType = Var->getType().getNonReferenceType();
3656 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003657 SemaRef.getLangOpts().CPlusPlus) {
3658 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003659 auto *UBExpr = TestIsLessOp ? UB : LB;
3660 auto *LBExpr = TestIsLessOp ? LB : UB;
3661 Expr *Upper = Transform.TransformExpr(UBExpr).get();
3662 Expr *Lower = Transform.TransformExpr(LBExpr).get();
3663 if (!Upper || !Lower)
3664 return nullptr;
3665 Upper = SemaRef.PerformImplicitConversion(Upper, UBExpr->getType(),
3666 Sema::AA_Converting,
3667 /*AllowExplicit=*/true)
3668 .get();
3669 Lower = SemaRef.PerformImplicitConversion(Lower, LBExpr->getType(),
3670 Sema::AA_Converting,
3671 /*AllowExplicit=*/true)
3672 .get();
3673 if (!Upper || !Lower)
3674 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003675
3676 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3677
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003678 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003679 // BuildBinOp already emitted error, this one is to point user to upper
3680 // and lower bound, and to tell what is passed to 'operator-'.
3681 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3682 << Upper->getSourceRange() << Lower->getSourceRange();
3683 return nullptr;
3684 }
3685 }
3686
3687 if (!Diff.isUsable())
3688 return nullptr;
3689
3690 // Upper - Lower [- 1]
3691 if (TestIsStrictOp)
3692 Diff = SemaRef.BuildBinOp(
3693 S, DefaultLoc, BO_Sub, Diff.get(),
3694 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3695 if (!Diff.isUsable())
3696 return nullptr;
3697
3698 // Upper - Lower [- 1] + Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003699 auto NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3700 if (NewStep.isInvalid())
3701 return nullptr;
3702 NewStep = SemaRef.PerformImplicitConversion(
3703 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3704 /*AllowExplicit=*/true);
3705 if (NewStep.isInvalid())
3706 return nullptr;
3707 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003708 if (!Diff.isUsable())
3709 return nullptr;
3710
3711 // Parentheses (for dumping/debugging purposes only).
3712 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3713 if (!Diff.isUsable())
3714 return nullptr;
3715
3716 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003717 NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3718 if (NewStep.isInvalid())
3719 return nullptr;
3720 NewStep = SemaRef.PerformImplicitConversion(
3721 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3722 /*AllowExplicit=*/true);
3723 if (NewStep.isInvalid())
3724 return nullptr;
3725 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003726 if (!Diff.isUsable())
3727 return nullptr;
3728
Alexander Musman174b3ca2014-10-06 11:16:29 +00003729 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003730 QualType Type = Diff.get()->getType();
3731 auto &C = SemaRef.Context;
3732 bool UseVarType = VarType->hasIntegerRepresentation() &&
3733 C.getTypeSize(Type) > C.getTypeSize(VarType);
3734 if (!Type->isIntegerType() || UseVarType) {
3735 unsigned NewSize =
3736 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
3737 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
3738 : Type->hasSignedIntegerRepresentation();
3739 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
3740 Diff = SemaRef.PerformImplicitConversion(
3741 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
3742 if (!Diff.isUsable())
3743 return nullptr;
3744 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003745 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00003746 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
3747 if (NewSize != C.getTypeSize(Type)) {
3748 if (NewSize < C.getTypeSize(Type)) {
3749 assert(NewSize == 64 && "incorrect loop var size");
3750 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
3751 << InitSrcRange << ConditionSrcRange;
3752 }
3753 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003754 NewSize, Type->hasSignedIntegerRepresentation() ||
3755 C.getTypeSize(Type) < NewSize);
Alexander Musman174b3ca2014-10-06 11:16:29 +00003756 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
3757 Sema::AA_Converting, true);
3758 if (!Diff.isUsable())
3759 return nullptr;
3760 }
3761 }
3762
Alexander Musmana5f070a2014-10-01 06:03:56 +00003763 return Diff.get();
3764}
3765
Alexey Bataev62dbb972015-04-22 11:59:37 +00003766Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
3767 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
3768 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3769 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003770 TransformToNewDefs Transform(SemaRef);
3771
3772 auto NewLB = Transform.TransformExpr(LB);
3773 auto NewUB = Transform.TransformExpr(UB);
3774 if (NewLB.isInvalid() || NewUB.isInvalid())
3775 return Cond;
3776 NewLB = SemaRef.PerformImplicitConversion(NewLB.get(), LB->getType(),
3777 Sema::AA_Converting,
3778 /*AllowExplicit=*/true);
3779 NewUB = SemaRef.PerformImplicitConversion(NewUB.get(), UB->getType(),
3780 Sema::AA_Converting,
3781 /*AllowExplicit=*/true);
3782 if (NewLB.isInvalid() || NewUB.isInvalid())
3783 return Cond;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003784 auto CondExpr = SemaRef.BuildBinOp(
3785 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
3786 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003787 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003788 if (CondExpr.isUsable()) {
3789 CondExpr = SemaRef.PerformImplicitConversion(
3790 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
3791 /*AllowExplicit=*/true);
3792 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00003793 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3794 // Otherwise use original loop conditon and evaluate it in runtime.
3795 return CondExpr.isUsable() ? CondExpr.get() : Cond;
3796}
3797
Alexander Musmana5f070a2014-10-01 06:03:56 +00003798/// \brief Build reference expression to the counter be used for codegen.
3799Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataeva8899172015-08-06 12:30:57 +00003800 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
3801 DefaultLoc);
3802}
3803
3804Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
3805 if (Var && !Var->isInvalidDecl()) {
3806 auto Type = Var->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003807 auto *PrivateVar =
3808 buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName(),
3809 Var->hasAttrs() ? &Var->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00003810 if (PrivateVar->isInvalidDecl())
3811 return nullptr;
3812 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
3813 }
3814 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003815}
3816
3817/// \brief Build initization of the counter be used for codegen.
3818Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
3819
3820/// \brief Build step of the counter be used for codegen.
3821Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
3822
3823/// \brief Iteration space of a single for loop.
3824struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003825 /// \brief Condition of the loop.
3826 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003827 /// \brief This expression calculates the number of iterations in the loop.
3828 /// It is always possible to calculate it before starting the loop.
3829 Expr *NumIterations;
3830 /// \brief The loop counter variable.
3831 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003832 /// \brief Private loop counter variable.
3833 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003834 /// \brief This is initializer for the initial value of #CounterVar.
3835 Expr *CounterInit;
3836 /// \brief This is step for the #CounterVar used to generate its update:
3837 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
3838 Expr *CounterStep;
3839 /// \brief Should step be subtracted?
3840 bool Subtract;
3841 /// \brief Source range of the loop init.
3842 SourceRange InitSrcRange;
3843 /// \brief Source range of the loop condition.
3844 SourceRange CondSrcRange;
3845 /// \brief Source range of the loop increment.
3846 SourceRange IncSrcRange;
3847};
3848
Alexey Bataev23b69422014-06-18 07:08:49 +00003849} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003850
Alexey Bataev9c821032015-04-30 04:23:23 +00003851void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
3852 assert(getLangOpts().OpenMP && "OpenMP is not active.");
3853 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003854 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
3855 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00003856 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
3857 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003858 if (!ISC.CheckInit(Init, /*EmitDiags=*/false))
Alexey Bataev9c821032015-04-30 04:23:23 +00003859 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003860 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00003861 }
3862}
3863
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003864/// \brief Called on a for stmt to check and extract its iteration space
3865/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00003866static bool CheckOpenMPIterationSpace(
3867 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
3868 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003869 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003870 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003871 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003872 // OpenMP [2.6, Canonical Loop Form]
3873 // for (init-expr; test-expr; incr-expr) structured-block
3874 auto For = dyn_cast_or_null<ForStmt>(S);
3875 if (!For) {
3876 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003877 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
3878 << getOpenMPDirectiveName(DKind) << NestedLoopCount
3879 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
3880 if (NestedLoopCount > 1) {
3881 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
3882 SemaRef.Diag(DSA.getConstructLoc(),
3883 diag::note_omp_collapse_ordered_expr)
3884 << 2 << CollapseLoopCountExpr->getSourceRange()
3885 << OrderedLoopCountExpr->getSourceRange();
3886 else if (CollapseLoopCountExpr)
3887 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3888 diag::note_omp_collapse_ordered_expr)
3889 << 0 << CollapseLoopCountExpr->getSourceRange();
3890 else
3891 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3892 diag::note_omp_collapse_ordered_expr)
3893 << 1 << OrderedLoopCountExpr->getSourceRange();
3894 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003895 return true;
3896 }
3897 assert(For->getBody());
3898
3899 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3900
3901 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003902 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003903 if (ISC.CheckInit(Init)) {
3904 return true;
3905 }
3906
3907 bool HasErrors = false;
3908
3909 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003910 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003911
3912 // OpenMP [2.6, Canonical Loop Form]
3913 // Var is one of the following:
3914 // A variable of signed or unsigned integer type.
3915 // For C++, a variable of a random access iterator type.
3916 // For C, a variable of a pointer type.
Alexey Bataeva8899172015-08-06 12:30:57 +00003917 auto VarType = Var->getType().getNonReferenceType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003918 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3919 !VarType->isPointerType() &&
3920 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3921 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3922 << SemaRef.getLangOpts().CPlusPlus;
3923 HasErrors = true;
3924 }
3925
Alexey Bataev4acb8592014-07-07 13:01:15 +00003926 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
3927 // Construct
3928 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3929 // parallel for construct is (are) private.
3930 // The loop iteration variable in the associated for-loop of a simd construct
3931 // with just one associated for-loop is linear with a constant-linear-step
3932 // that is the increment of the associated for-loop.
3933 // Exclude loop var from the list of variables with implicitly defined data
3934 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00003935 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003936
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003937 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
3938 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00003939 // The loop iteration variable in the associated for-loop of a simd construct
3940 // with just one associated for-loop may be listed in a linear clause with a
3941 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003942 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3943 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003944 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003945 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
3946 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3947 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003948 auto PredeterminedCKind =
3949 isOpenMPSimdDirective(DKind)
3950 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3951 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003952 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003953 DVar.CKind != PredeterminedCKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003954 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
Alexey Bataeve648e802015-12-25 13:38:08 +00003955 isOpenMPDistributeDirective(DKind)) &&
Alexey Bataev49f6e782015-12-01 04:18:41 +00003956 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003957 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
3958 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003959 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00003960 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
3961 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00003962 if (DVar.RefExpr == nullptr)
3963 DVar.CKind = PredeterminedCKind;
3964 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003965 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003966 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003967 // Make the loop iteration variable private (for worksharing constructs),
3968 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00003969 // lastprivate (for simd directives with several collapsed or ordered
3970 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00003971 if (DVar.CKind == OMPC_unknown)
3972 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
3973 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00003974 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003975 }
3976
Alexey Bataev7ff55242014-06-19 09:13:45 +00003977 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00003978
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003979 // Check test-expr.
3980 HasErrors |= ISC.CheckCond(For->getCond());
3981
3982 // Check incr-expr.
3983 HasErrors |= ISC.CheckInc(For->getInc());
3984
Alexander Musmana5f070a2014-10-01 06:03:56 +00003985 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003986 return HasErrors;
3987
Alexander Musmana5f070a2014-10-01 06:03:56 +00003988 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003989 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00003990 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003991 DSA.getCurScope(), (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003992 isOpenMPTaskLoopDirective(DKind) ||
3993 isOpenMPDistributeDirective(DKind)));
Alexander Musmana5f070a2014-10-01 06:03:56 +00003994 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
Alexey Bataeva8899172015-08-06 12:30:57 +00003995 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003996 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
3997 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
3998 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
3999 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4000 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4001 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4002
Alexey Bataev62dbb972015-04-22 11:59:37 +00004003 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4004 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004005 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004006 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004007 ResultIterSpace.CounterInit == nullptr ||
4008 ResultIterSpace.CounterStep == nullptr);
4009
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004010 return HasErrors;
4011}
4012
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004013/// \brief Build 'VarRef = Start.
4014static ExprResult BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc,
4015 ExprResult VarRef, ExprResult Start) {
4016 TransformToNewDefs Transform(SemaRef);
4017 // Build 'VarRef = Start.
4018 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
4019 if (NewStart.isInvalid())
4020 return ExprError();
4021 NewStart = SemaRef.PerformImplicitConversion(
4022 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
4023 Sema::AA_Converting,
4024 /*AllowExplicit=*/true);
4025 if (NewStart.isInvalid())
4026 return ExprError();
4027 NewStart = SemaRef.PerformImplicitConversion(
4028 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4029 /*AllowExplicit=*/true);
4030 if (!NewStart.isUsable())
4031 return ExprError();
4032
4033 auto Init =
4034 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4035 return Init;
4036}
4037
Alexander Musmana5f070a2014-10-01 06:03:56 +00004038/// \brief Build 'VarRef = Start + Iter * Step'.
4039static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
4040 SourceLocation Loc, ExprResult VarRef,
4041 ExprResult Start, ExprResult Iter,
4042 ExprResult Step, bool Subtract) {
4043 // Add parentheses (for debugging purposes only).
4044 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4045 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4046 !Step.isUsable())
4047 return ExprError();
4048
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004049 TransformToNewDefs Transform(SemaRef);
4050 auto NewStep = Transform.TransformExpr(Step.get()->IgnoreImplicit());
4051 if (NewStep.isInvalid())
4052 return ExprError();
4053 NewStep = SemaRef.PerformImplicitConversion(
4054 NewStep.get(), Step.get()->IgnoreImplicit()->getType(),
4055 Sema::AA_Converting,
4056 /*AllowExplicit=*/true);
4057 if (NewStep.isInvalid())
4058 return ExprError();
4059 ExprResult Update =
4060 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004061 if (!Update.isUsable())
4062 return ExprError();
4063
4064 // Build 'VarRef = Start + Iter * Step'.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004065 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
4066 if (NewStart.isInvalid())
4067 return ExprError();
4068 NewStart = SemaRef.PerformImplicitConversion(
4069 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
4070 Sema::AA_Converting,
4071 /*AllowExplicit=*/true);
4072 if (NewStart.isInvalid())
4073 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004074 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004075 NewStart.get(), Update.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004076 if (!Update.isUsable())
4077 return ExprError();
4078
4079 Update = SemaRef.PerformImplicitConversion(
4080 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4081 if (!Update.isUsable())
4082 return ExprError();
4083
4084 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4085 return Update;
4086}
4087
4088/// \brief Convert integer expression \a E to make it have at least \a Bits
4089/// bits.
4090static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
4091 Sema &SemaRef) {
4092 if (E == nullptr)
4093 return ExprError();
4094 auto &C = SemaRef.Context;
4095 QualType OldType = E->getType();
4096 unsigned HasBits = C.getTypeSize(OldType);
4097 if (HasBits >= Bits)
4098 return ExprResult(E);
4099 // OK to convert to signed, because new type has more bits than old.
4100 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4101 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4102 true);
4103}
4104
4105/// \brief Check if the given expression \a E is a constant integer that fits
4106/// into \a Bits bits.
4107static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4108 if (E == nullptr)
4109 return false;
4110 llvm::APSInt Result;
4111 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4112 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4113 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004114}
4115
4116/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00004117/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4118/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004119static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00004120CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
4121 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4122 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004123 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00004124 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004125 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004126 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004127 // Found 'collapse' clause - calculate collapse number.
4128 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004129 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004130 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004131 }
4132 if (OrderedLoopCountExpr) {
4133 // Found 'ordered' clause - calculate collapse number.
4134 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004135 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4136 if (Result.getLimitedValue() < NestedLoopCount) {
4137 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4138 diag::err_omp_wrong_ordered_loop_count)
4139 << OrderedLoopCountExpr->getSourceRange();
4140 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4141 diag::note_collapse_loop_count)
4142 << CollapseLoopCountExpr->getSourceRange();
4143 }
4144 NestedLoopCount = Result.getLimitedValue();
4145 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004146 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004147 // This is helper routine for loop directives (e.g., 'for', 'simd',
4148 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00004149 SmallVector<LoopIterationSpace, 4> IterSpaces;
4150 IterSpaces.resize(NestedLoopCount);
4151 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004152 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004153 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004154 NestedLoopCount, CollapseLoopCountExpr,
4155 OrderedLoopCountExpr, VarsWithImplicitDSA,
4156 IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004157 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004158 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004159 // OpenMP [2.8.1, simd construct, Restrictions]
4160 // All loops associated with the construct must be perfectly nested; that
4161 // is, there must be no intervening code nor any OpenMP directive between
4162 // any two loops.
4163 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004164 }
4165
Alexander Musmana5f070a2014-10-01 06:03:56 +00004166 Built.clear(/* size */ NestedLoopCount);
4167
4168 if (SemaRef.CurContext->isDependentContext())
4169 return NestedLoopCount;
4170
4171 // An example of what is generated for the following code:
4172 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004173 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004174 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004175 // for (k = 0; k < NK; ++k)
4176 // for (j = J0; j < NJ; j+=2) {
4177 // <loop body>
4178 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004179 //
4180 // We generate the code below.
4181 // Note: the loop body may be outlined in CodeGen.
4182 // Note: some counters may be C++ classes, operator- is used to find number of
4183 // iterations and operator+= to calculate counter value.
4184 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4185 // or i64 is currently supported).
4186 //
4187 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4188 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4189 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4190 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4191 // // similar updates for vars in clauses (e.g. 'linear')
4192 // <loop body (using local i and j)>
4193 // }
4194 // i = NI; // assign final values of counters
4195 // j = NJ;
4196 //
4197
4198 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4199 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004200 // Precondition tests if there is at least one iteration (all conditions are
4201 // true).
4202 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004203 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004204 ExprResult LastIteration32 = WidenIterationCount(
4205 32 /* Bits */, SemaRef.PerformImplicitConversion(
4206 N0->IgnoreImpCasts(), N0->getType(),
4207 Sema::AA_Converting, /*AllowExplicit=*/true)
4208 .get(),
4209 SemaRef);
4210 ExprResult LastIteration64 = WidenIterationCount(
4211 64 /* Bits */, SemaRef.PerformImplicitConversion(
4212 N0->IgnoreImpCasts(), N0->getType(),
4213 Sema::AA_Converting, /*AllowExplicit=*/true)
4214 .get(),
4215 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004216
4217 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4218 return NestedLoopCount;
4219
4220 auto &C = SemaRef.Context;
4221 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4222
4223 Scope *CurScope = DSA.getCurScope();
4224 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004225 if (PreCond.isUsable()) {
4226 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
4227 PreCond.get(), IterSpaces[Cnt].PreCond);
4228 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004229 auto N = IterSpaces[Cnt].NumIterations;
4230 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4231 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004232 LastIteration32 = SemaRef.BuildBinOp(
4233 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
4234 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4235 Sema::AA_Converting,
4236 /*AllowExplicit=*/true)
4237 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004238 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004239 LastIteration64 = SemaRef.BuildBinOp(
4240 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
4241 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4242 Sema::AA_Converting,
4243 /*AllowExplicit=*/true)
4244 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004245 }
4246
4247 // Choose either the 32-bit or 64-bit version.
4248 ExprResult LastIteration = LastIteration64;
4249 if (LastIteration32.isUsable() &&
4250 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4251 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4252 FitsInto(
4253 32 /* Bits */,
4254 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4255 LastIteration64.get(), SemaRef)))
4256 LastIteration = LastIteration32;
4257
4258 if (!LastIteration.isUsable())
4259 return 0;
4260
4261 // Save the number of iterations.
4262 ExprResult NumIterations = LastIteration;
4263 {
4264 LastIteration = SemaRef.BuildBinOp(
4265 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
4266 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4267 if (!LastIteration.isUsable())
4268 return 0;
4269 }
4270
4271 // Calculate the last iteration number beforehand instead of doing this on
4272 // each iteration. Do not do this if the number of iterations may be kfold-ed.
4273 llvm::APSInt Result;
4274 bool IsConstant =
4275 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4276 ExprResult CalcLastIteration;
4277 if (!IsConstant) {
4278 SourceLocation SaveLoc;
4279 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004280 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00004281 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00004282 ExprResult SaveRef = buildDeclRefExpr(
4283 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004284 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
4285 SaveRef.get(), LastIteration.get());
4286 LastIteration = SaveRef;
4287
4288 // Prepare SaveRef + 1.
4289 NumIterations = SemaRef.BuildBinOp(
4290 CurScope, SaveLoc, BO_Add, SaveRef.get(),
4291 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4292 if (!NumIterations.isUsable())
4293 return 0;
4294 }
4295
4296 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4297
Alexander Musmanc6388682014-12-15 07:07:06 +00004298 QualType VType = LastIteration.get()->getType();
4299 // Build variables passed into runtime, nesessary for worksharing directives.
4300 ExprResult LB, UB, IL, ST, EUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004301 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4302 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004303 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004304 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4305 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004306 SemaRef.AddInitializerToDecl(
4307 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4308 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4309
4310 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004311 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4312 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004313 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
4314 /*DirectInit*/ false,
4315 /*TypeMayContainAuto*/ false);
4316
4317 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4318 // This will be used to implement clause 'lastprivate'.
4319 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00004320 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4321 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004322 SemaRef.AddInitializerToDecl(
4323 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4324 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4325
4326 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00004327 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
4328 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004329 SemaRef.AddInitializerToDecl(
4330 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4331 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4332
4333 // Build expression: UB = min(UB, LastIteration)
4334 // It is nesessary for CodeGen of directives with static scheduling.
4335 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4336 UB.get(), LastIteration.get());
4337 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4338 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4339 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4340 CondOp.get());
4341 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
4342 }
4343
4344 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004345 ExprResult IV;
4346 ExprResult Init;
4347 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00004348 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
4349 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004350 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004351 isOpenMPTaskLoopDirective(DKind) ||
4352 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004353 ? LB.get()
4354 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4355 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4356 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004357 }
4358
Alexander Musmanc6388682014-12-15 07:07:06 +00004359 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004360 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00004361 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004362 (isOpenMPWorksharingDirective(DKind) ||
4363 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004364 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4365 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4366 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004367
4368 // Loop increment (IV = IV + 1)
4369 SourceLocation IncLoc;
4370 ExprResult Inc =
4371 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4372 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4373 if (!Inc.isUsable())
4374 return 0;
4375 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00004376 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4377 if (!Inc.isUsable())
4378 return 0;
4379
4380 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4381 // Used for directives with static scheduling.
4382 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004383 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4384 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004385 // LB + ST
4386 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4387 if (!NextLB.isUsable())
4388 return 0;
4389 // LB = LB + ST
4390 NextLB =
4391 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4392 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4393 if (!NextLB.isUsable())
4394 return 0;
4395 // UB + ST
4396 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4397 if (!NextUB.isUsable())
4398 return 0;
4399 // UB = UB + ST
4400 NextUB =
4401 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4402 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4403 if (!NextUB.isUsable())
4404 return 0;
4405 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004406
4407 // Build updates and final values of the loop counters.
4408 bool HasErrors = false;
4409 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004410 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004411 Built.Updates.resize(NestedLoopCount);
4412 Built.Finals.resize(NestedLoopCount);
4413 {
4414 ExprResult Div;
4415 // Go from inner nested loop to outer.
4416 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4417 LoopIterationSpace &IS = IterSpaces[Cnt];
4418 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4419 // Build: Iter = (IV / Div) % IS.NumIters
4420 // where Div is product of previous iterations' IS.NumIters.
4421 ExprResult Iter;
4422 if (Div.isUsable()) {
4423 Iter =
4424 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4425 } else {
4426 Iter = IV;
4427 assert((Cnt == (int)NestedLoopCount - 1) &&
4428 "unusable div expected on first iteration only");
4429 }
4430
4431 if (Cnt != 0 && Iter.isUsable())
4432 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4433 IS.NumIterations);
4434 if (!Iter.isUsable()) {
4435 HasErrors = true;
4436 break;
4437 }
4438
Alexey Bataev39f915b82015-05-08 10:41:21 +00004439 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
4440 auto *CounterVar = buildDeclRefExpr(
4441 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
4442 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
4443 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004444 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
4445 IS.CounterInit);
4446 if (!Init.isUsable()) {
4447 HasErrors = true;
4448 break;
4449 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004450 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004451 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004452 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
4453 if (!Update.isUsable()) {
4454 HasErrors = true;
4455 break;
4456 }
4457
4458 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4459 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00004460 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004461 IS.NumIterations, IS.CounterStep, IS.Subtract);
4462 if (!Final.isUsable()) {
4463 HasErrors = true;
4464 break;
4465 }
4466
4467 // Build Div for the next iteration: Div <- Div * IS.NumIters
4468 if (Cnt != 0) {
4469 if (Div.isUnset())
4470 Div = IS.NumIterations;
4471 else
4472 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4473 IS.NumIterations);
4474
4475 // Add parentheses (for debugging purposes only).
4476 if (Div.isUsable())
4477 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
4478 if (!Div.isUsable()) {
4479 HasErrors = true;
4480 break;
4481 }
4482 }
4483 if (!Update.isUsable() || !Final.isUsable()) {
4484 HasErrors = true;
4485 break;
4486 }
4487 // Save results
4488 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004489 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004490 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004491 Built.Updates[Cnt] = Update.get();
4492 Built.Finals[Cnt] = Final.get();
4493 }
4494 }
4495
4496 if (HasErrors)
4497 return 0;
4498
4499 // Save results
4500 Built.IterationVarRef = IV.get();
4501 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004502 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004503 Built.CalcLastIteration =
4504 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004505 Built.PreCond = PreCond.get();
4506 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004507 Built.Init = Init.get();
4508 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004509 Built.LB = LB.get();
4510 Built.UB = UB.get();
4511 Built.IL = IL.get();
4512 Built.ST = ST.get();
4513 Built.EUB = EUB.get();
4514 Built.NLB = NextLB.get();
4515 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004516
Alexey Bataevabfc0692014-06-25 06:52:00 +00004517 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004518}
4519
Alexey Bataev10e775f2015-07-30 11:36:16 +00004520static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004521 auto CollapseClauses =
4522 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4523 if (CollapseClauses.begin() != CollapseClauses.end())
4524 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004525 return nullptr;
4526}
4527
Alexey Bataev10e775f2015-07-30 11:36:16 +00004528static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004529 auto OrderedClauses =
4530 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4531 if (OrderedClauses.begin() != OrderedClauses.end())
4532 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004533 return nullptr;
4534}
4535
Alexey Bataev66b15b52015-08-21 11:14:16 +00004536static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
4537 const Expr *Safelen) {
4538 llvm::APSInt SimdlenRes, SafelenRes;
4539 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
4540 Simdlen->isInstantiationDependent() ||
4541 Simdlen->containsUnexpandedParameterPack())
4542 return false;
4543 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
4544 Safelen->isInstantiationDependent() ||
4545 Safelen->containsUnexpandedParameterPack())
4546 return false;
4547 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
4548 Safelen->EvaluateAsInt(SafelenRes, S.Context);
4549 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4550 // If both simdlen and safelen clauses are specified, the value of the simdlen
4551 // parameter must be less than or equal to the value of the safelen parameter.
4552 if (SimdlenRes > SafelenRes) {
4553 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
4554 << Simdlen->getSourceRange() << Safelen->getSourceRange();
4555 return true;
4556 }
4557 return false;
4558}
4559
Alexey Bataev4acb8592014-07-07 13:01:15 +00004560StmtResult Sema::ActOnOpenMPSimdDirective(
4561 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4562 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004563 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004564 if (!AStmt)
4565 return StmtError();
4566
4567 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004568 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004569 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4570 // define the nested loops number.
4571 unsigned NestedLoopCount = CheckOpenMPLoop(
4572 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4573 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004574 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004575 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004576
Alexander Musmana5f070a2014-10-01 06:03:56 +00004577 assert((CurContext->isDependentContext() || B.builtAll()) &&
4578 "omp simd loop exprs were not built");
4579
Alexander Musman3276a272015-03-21 10:12:56 +00004580 if (!CurContext->isDependentContext()) {
4581 // Finalize the clauses that need pre-built expressions for CodeGen.
4582 for (auto C : Clauses) {
4583 if (auto LC = dyn_cast<OMPLinearClause>(C))
4584 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4585 B.NumIterations, *this, CurScope))
4586 return StmtError();
4587 }
4588 }
4589
Alexey Bataev66b15b52015-08-21 11:14:16 +00004590 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4591 // If both simdlen and safelen clauses are specified, the value of the simdlen
4592 // parameter must be less than or equal to the value of the safelen parameter.
4593 OMPSafelenClause *Safelen = nullptr;
4594 OMPSimdlenClause *Simdlen = nullptr;
4595 for (auto *Clause : Clauses) {
4596 if (Clause->getClauseKind() == OMPC_safelen)
4597 Safelen = cast<OMPSafelenClause>(Clause);
4598 else if (Clause->getClauseKind() == OMPC_simdlen)
4599 Simdlen = cast<OMPSimdlenClause>(Clause);
4600 if (Safelen && Simdlen)
4601 break;
4602 }
4603 if (Simdlen && Safelen &&
4604 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4605 Safelen->getSafelen()))
4606 return StmtError();
4607
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004608 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004609 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4610 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004611}
4612
Alexey Bataev4acb8592014-07-07 13:01:15 +00004613StmtResult Sema::ActOnOpenMPForDirective(
4614 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4615 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004616 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004617 if (!AStmt)
4618 return StmtError();
4619
4620 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004621 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004622 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4623 // define the nested loops number.
4624 unsigned NestedLoopCount = CheckOpenMPLoop(
4625 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4626 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004627 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00004628 return StmtError();
4629
Alexander Musmana5f070a2014-10-01 06:03:56 +00004630 assert((CurContext->isDependentContext() || B.builtAll()) &&
4631 "omp for loop exprs were not built");
4632
Alexey Bataev54acd402015-08-04 11:18:19 +00004633 if (!CurContext->isDependentContext()) {
4634 // Finalize the clauses that need pre-built expressions for CodeGen.
4635 for (auto C : Clauses) {
4636 if (auto LC = dyn_cast<OMPLinearClause>(C))
4637 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4638 B.NumIterations, *this, CurScope))
4639 return StmtError();
4640 }
4641 }
4642
Alexey Bataevf29276e2014-06-18 04:14:57 +00004643 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004644 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004645 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004646}
4647
Alexander Musmanf82886e2014-09-18 05:12:34 +00004648StmtResult Sema::ActOnOpenMPForSimdDirective(
4649 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4650 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004651 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004652 if (!AStmt)
4653 return StmtError();
4654
4655 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004656 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004657 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4658 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00004659 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004660 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
4661 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4662 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004663 if (NestedLoopCount == 0)
4664 return StmtError();
4665
Alexander Musmanc6388682014-12-15 07:07:06 +00004666 assert((CurContext->isDependentContext() || B.builtAll()) &&
4667 "omp for simd loop exprs were not built");
4668
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00004669 if (!CurContext->isDependentContext()) {
4670 // Finalize the clauses that need pre-built expressions for CodeGen.
4671 for (auto C : Clauses) {
4672 if (auto LC = dyn_cast<OMPLinearClause>(C))
4673 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4674 B.NumIterations, *this, CurScope))
4675 return StmtError();
4676 }
4677 }
4678
Alexey Bataev66b15b52015-08-21 11:14:16 +00004679 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4680 // If both simdlen and safelen clauses are specified, the value of the simdlen
4681 // parameter must be less than or equal to the value of the safelen parameter.
4682 OMPSafelenClause *Safelen = nullptr;
4683 OMPSimdlenClause *Simdlen = nullptr;
4684 for (auto *Clause : Clauses) {
4685 if (Clause->getClauseKind() == OMPC_safelen)
4686 Safelen = cast<OMPSafelenClause>(Clause);
4687 else if (Clause->getClauseKind() == OMPC_simdlen)
4688 Simdlen = cast<OMPSimdlenClause>(Clause);
4689 if (Safelen && Simdlen)
4690 break;
4691 }
4692 if (Simdlen && Safelen &&
4693 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4694 Safelen->getSafelen()))
4695 return StmtError();
4696
Alexander Musmanf82886e2014-09-18 05:12:34 +00004697 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004698 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4699 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004700}
4701
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004702StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
4703 Stmt *AStmt,
4704 SourceLocation StartLoc,
4705 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004706 if (!AStmt)
4707 return StmtError();
4708
4709 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004710 auto BaseStmt = AStmt;
4711 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4712 BaseStmt = CS->getCapturedStmt();
4713 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4714 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004715 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004716 return StmtError();
4717 // All associated statements must be '#pragma omp section' except for
4718 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004719 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004720 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4721 if (SectionStmt)
4722 Diag(SectionStmt->getLocStart(),
4723 diag::err_omp_sections_substmt_not_section);
4724 return StmtError();
4725 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004726 cast<OMPSectionDirective>(SectionStmt)
4727 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004728 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004729 } else {
4730 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
4731 return StmtError();
4732 }
4733
4734 getCurFunction()->setHasBranchProtectedScope();
4735
Alexey Bataev25e5b442015-09-15 12:52:43 +00004736 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4737 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004738}
4739
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004740StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
4741 SourceLocation StartLoc,
4742 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004743 if (!AStmt)
4744 return StmtError();
4745
4746 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004747
4748 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00004749 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004750
Alexey Bataev25e5b442015-09-15 12:52:43 +00004751 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
4752 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004753}
4754
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004755StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
4756 Stmt *AStmt,
4757 SourceLocation StartLoc,
4758 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004759 if (!AStmt)
4760 return StmtError();
4761
4762 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00004763
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004764 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00004765
Alexey Bataev3255bf32015-01-19 05:20:46 +00004766 // OpenMP [2.7.3, single Construct, Restrictions]
4767 // The copyprivate clause must not be used with the nowait clause.
4768 OMPClause *Nowait = nullptr;
4769 OMPClause *Copyprivate = nullptr;
4770 for (auto *Clause : Clauses) {
4771 if (Clause->getClauseKind() == OMPC_nowait)
4772 Nowait = Clause;
4773 else if (Clause->getClauseKind() == OMPC_copyprivate)
4774 Copyprivate = Clause;
4775 if (Copyprivate && Nowait) {
4776 Diag(Copyprivate->getLocStart(),
4777 diag::err_omp_single_copyprivate_with_nowait);
4778 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
4779 return StmtError();
4780 }
4781 }
4782
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004783 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4784}
4785
Alexander Musman80c22892014-07-17 08:54:58 +00004786StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
4787 SourceLocation StartLoc,
4788 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004789 if (!AStmt)
4790 return StmtError();
4791
4792 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00004793
4794 getCurFunction()->setHasBranchProtectedScope();
4795
4796 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
4797}
4798
Alexey Bataev28c75412015-12-15 08:19:24 +00004799StmtResult Sema::ActOnOpenMPCriticalDirective(
4800 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
4801 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004802 if (!AStmt)
4803 return StmtError();
4804
4805 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004806
Alexey Bataev28c75412015-12-15 08:19:24 +00004807 bool ErrorFound = false;
4808 llvm::APSInt Hint;
4809 SourceLocation HintLoc;
4810 bool DependentHint = false;
4811 for (auto *C : Clauses) {
4812 if (C->getClauseKind() == OMPC_hint) {
4813 if (!DirName.getName()) {
4814 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
4815 ErrorFound = true;
4816 }
4817 Expr *E = cast<OMPHintClause>(C)->getHint();
4818 if (E->isTypeDependent() || E->isValueDependent() ||
4819 E->isInstantiationDependent())
4820 DependentHint = true;
4821 else {
4822 Hint = E->EvaluateKnownConstInt(Context);
4823 HintLoc = C->getLocStart();
4824 }
4825 }
4826 }
4827 if (ErrorFound)
4828 return StmtError();
4829 auto Pair = DSAStack->getCriticalWithHint(DirName);
4830 if (Pair.first && DirName.getName() && !DependentHint) {
4831 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
4832 Diag(StartLoc, diag::err_omp_critical_with_hint);
4833 if (HintLoc.isValid()) {
4834 Diag(HintLoc, diag::note_omp_critical_hint_here)
4835 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
4836 } else
4837 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
4838 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
4839 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
4840 << 1
4841 << C->getHint()->EvaluateKnownConstInt(Context).toString(
4842 /*Radix=*/10, /*Signed=*/false);
4843 } else
4844 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
4845 }
4846 }
4847
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004848 getCurFunction()->setHasBranchProtectedScope();
4849
Alexey Bataev28c75412015-12-15 08:19:24 +00004850 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
4851 Clauses, AStmt);
4852 if (!Pair.first && DirName.getName() && !DependentHint)
4853 DSAStack->addCriticalWithHint(Dir, Hint);
4854 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004855}
4856
Alexey Bataev4acb8592014-07-07 13:01:15 +00004857StmtResult Sema::ActOnOpenMPParallelForDirective(
4858 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4859 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004860 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004861 if (!AStmt)
4862 return StmtError();
4863
Alexey Bataev4acb8592014-07-07 13:01:15 +00004864 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4865 // 1.2.2 OpenMP Language Terminology
4866 // Structured block - An executable statement with a single entry at the
4867 // top and a single exit at the bottom.
4868 // The point of exit cannot be a branch out of the structured block.
4869 // longjmp() and throw() must not violate the entry/exit criteria.
4870 CS->getCapturedDecl()->setNothrow();
4871
Alexander Musmanc6388682014-12-15 07:07:06 +00004872 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004873 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4874 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004875 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004876 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
4877 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4878 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00004879 if (NestedLoopCount == 0)
4880 return StmtError();
4881
Alexander Musmana5f070a2014-10-01 06:03:56 +00004882 assert((CurContext->isDependentContext() || B.builtAll()) &&
4883 "omp parallel for loop exprs were not built");
4884
Alexey Bataev54acd402015-08-04 11:18:19 +00004885 if (!CurContext->isDependentContext()) {
4886 // Finalize the clauses that need pre-built expressions for CodeGen.
4887 for (auto C : Clauses) {
4888 if (auto LC = dyn_cast<OMPLinearClause>(C))
4889 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4890 B.NumIterations, *this, CurScope))
4891 return StmtError();
4892 }
4893 }
4894
Alexey Bataev4acb8592014-07-07 13:01:15 +00004895 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004896 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004897 NestedLoopCount, Clauses, AStmt, B,
4898 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00004899}
4900
Alexander Musmane4e893b2014-09-23 09:33:00 +00004901StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
4902 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4903 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004904 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004905 if (!AStmt)
4906 return StmtError();
4907
Alexander Musmane4e893b2014-09-23 09:33:00 +00004908 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4909 // 1.2.2 OpenMP Language Terminology
4910 // Structured block - An executable statement with a single entry at the
4911 // top and a single exit at the bottom.
4912 // The point of exit cannot be a branch out of the structured block.
4913 // longjmp() and throw() must not violate the entry/exit criteria.
4914 CS->getCapturedDecl()->setNothrow();
4915
Alexander Musmanc6388682014-12-15 07:07:06 +00004916 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004917 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4918 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00004919 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004920 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
4921 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4922 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004923 if (NestedLoopCount == 0)
4924 return StmtError();
4925
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00004926 if (!CurContext->isDependentContext()) {
4927 // Finalize the clauses that need pre-built expressions for CodeGen.
4928 for (auto C : Clauses) {
4929 if (auto LC = dyn_cast<OMPLinearClause>(C))
4930 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4931 B.NumIterations, *this, CurScope))
4932 return StmtError();
4933 }
4934 }
4935
Alexey Bataev66b15b52015-08-21 11:14:16 +00004936 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4937 // If both simdlen and safelen clauses are specified, the value of the simdlen
4938 // parameter must be less than or equal to the value of the safelen parameter.
4939 OMPSafelenClause *Safelen = nullptr;
4940 OMPSimdlenClause *Simdlen = nullptr;
4941 for (auto *Clause : Clauses) {
4942 if (Clause->getClauseKind() == OMPC_safelen)
4943 Safelen = cast<OMPSafelenClause>(Clause);
4944 else if (Clause->getClauseKind() == OMPC_simdlen)
4945 Simdlen = cast<OMPSimdlenClause>(Clause);
4946 if (Safelen && Simdlen)
4947 break;
4948 }
4949 if (Simdlen && Safelen &&
4950 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4951 Safelen->getSafelen()))
4952 return StmtError();
4953
Alexander Musmane4e893b2014-09-23 09:33:00 +00004954 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004955 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00004956 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004957}
4958
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004959StmtResult
4960Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
4961 Stmt *AStmt, SourceLocation StartLoc,
4962 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004963 if (!AStmt)
4964 return StmtError();
4965
4966 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004967 auto BaseStmt = AStmt;
4968 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4969 BaseStmt = CS->getCapturedStmt();
4970 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4971 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004972 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004973 return StmtError();
4974 // All associated statements must be '#pragma omp section' except for
4975 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004976 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004977 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4978 if (SectionStmt)
4979 Diag(SectionStmt->getLocStart(),
4980 diag::err_omp_parallel_sections_substmt_not_section);
4981 return StmtError();
4982 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004983 cast<OMPSectionDirective>(SectionStmt)
4984 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004985 }
4986 } else {
4987 Diag(AStmt->getLocStart(),
4988 diag::err_omp_parallel_sections_not_compound_stmt);
4989 return StmtError();
4990 }
4991
4992 getCurFunction()->setHasBranchProtectedScope();
4993
Alexey Bataev25e5b442015-09-15 12:52:43 +00004994 return OMPParallelSectionsDirective::Create(
4995 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004996}
4997
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004998StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
4999 Stmt *AStmt, SourceLocation StartLoc,
5000 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005001 if (!AStmt)
5002 return StmtError();
5003
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005004 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5005 // 1.2.2 OpenMP Language Terminology
5006 // Structured block - An executable statement with a single entry at the
5007 // top and a single exit at the bottom.
5008 // The point of exit cannot be a branch out of the structured block.
5009 // longjmp() and throw() must not violate the entry/exit criteria.
5010 CS->getCapturedDecl()->setNothrow();
5011
5012 getCurFunction()->setHasBranchProtectedScope();
5013
Alexey Bataev25e5b442015-09-15 12:52:43 +00005014 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5015 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005016}
5017
Alexey Bataev68446b72014-07-18 07:47:19 +00005018StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5019 SourceLocation EndLoc) {
5020 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5021}
5022
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00005023StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5024 SourceLocation EndLoc) {
5025 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5026}
5027
Alexey Bataev2df347a2014-07-18 10:17:07 +00005028StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5029 SourceLocation EndLoc) {
5030 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5031}
5032
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005033StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
5034 SourceLocation StartLoc,
5035 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005036 if (!AStmt)
5037 return StmtError();
5038
5039 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005040
5041 getCurFunction()->setHasBranchProtectedScope();
5042
5043 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
5044}
5045
Alexey Bataev6125da92014-07-21 11:26:11 +00005046StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5047 SourceLocation StartLoc,
5048 SourceLocation EndLoc) {
5049 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5050 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5051}
5052
Alexey Bataev346265e2015-09-25 10:37:12 +00005053StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5054 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005055 SourceLocation StartLoc,
5056 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00005057 OMPClause *DependFound = nullptr;
5058 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005059 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005060 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00005061 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005062 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005063 for (auto *C : Clauses) {
5064 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5065 DependFound = C;
5066 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5067 if (DependSourceClause) {
5068 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5069 << getOpenMPDirectiveName(OMPD_ordered)
5070 << getOpenMPClauseName(OMPC_depend) << 2;
5071 ErrorFound = true;
5072 } else
5073 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005074 if (DependSinkClause) {
5075 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5076 << 0;
5077 ErrorFound = true;
5078 }
5079 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5080 if (DependSourceClause) {
5081 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5082 << 1;
5083 ErrorFound = true;
5084 }
5085 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00005086 }
5087 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00005088 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005089 else if (C->getClauseKind() == OMPC_simd)
5090 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00005091 }
Alexey Bataeveb482352015-12-18 05:05:56 +00005092 if (!ErrorFound && !SC &&
5093 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005094 // OpenMP [2.8.1,simd Construct, Restrictions]
5095 // An ordered construct with the simd clause is the only OpenMP construct
5096 // that can appear in the simd region.
5097 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00005098 ErrorFound = true;
5099 } else if (DependFound && (TC || SC)) {
5100 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
5101 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
5102 ErrorFound = true;
5103 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
5104 Diag(DependFound->getLocStart(),
5105 diag::err_omp_ordered_directive_without_param);
5106 ErrorFound = true;
5107 } else if (TC || Clauses.empty()) {
5108 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
5109 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
5110 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
5111 << (TC != nullptr);
5112 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
5113 ErrorFound = true;
5114 }
5115 }
5116 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005117 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00005118
5119 if (AStmt) {
5120 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5121
5122 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005123 }
Alexey Bataev346265e2015-09-25 10:37:12 +00005124
5125 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005126}
5127
Alexey Bataev1d160b12015-03-13 12:27:31 +00005128namespace {
5129/// \brief Helper class for checking expression in 'omp atomic [update]'
5130/// construct.
5131class OpenMPAtomicUpdateChecker {
5132 /// \brief Error results for atomic update expressions.
5133 enum ExprAnalysisErrorCode {
5134 /// \brief A statement is not an expression statement.
5135 NotAnExpression,
5136 /// \brief Expression is not builtin binary or unary operation.
5137 NotABinaryOrUnaryExpression,
5138 /// \brief Unary operation is not post-/pre- increment/decrement operation.
5139 NotAnUnaryIncDecExpression,
5140 /// \brief An expression is not of scalar type.
5141 NotAScalarType,
5142 /// \brief A binary operation is not an assignment operation.
5143 NotAnAssignmentOp,
5144 /// \brief RHS part of the binary operation is not a binary expression.
5145 NotABinaryExpression,
5146 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
5147 /// expression.
5148 NotABinaryOperator,
5149 /// \brief RHS binary operation does not have reference to the updated LHS
5150 /// part.
5151 NotAnUpdateExpression,
5152 /// \brief No errors is found.
5153 NoError
5154 };
5155 /// \brief Reference to Sema.
5156 Sema &SemaRef;
5157 /// \brief A location for note diagnostics (when error is found).
5158 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005159 /// \brief 'x' lvalue part of the source atomic expression.
5160 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005161 /// \brief 'expr' rvalue part of the source atomic expression.
5162 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005163 /// \brief Helper expression of the form
5164 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5165 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5166 Expr *UpdateExpr;
5167 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
5168 /// important for non-associative operations.
5169 bool IsXLHSInRHSPart;
5170 BinaryOperatorKind Op;
5171 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005172 /// \brief true if the source expression is a postfix unary operation, false
5173 /// if it is a prefix unary operation.
5174 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005175
5176public:
5177 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00005178 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00005179 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00005180 /// \brief Check specified statement that it is suitable for 'atomic update'
5181 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00005182 /// expression. If DiagId and NoteId == 0, then only check is performed
5183 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005184 /// \param DiagId Diagnostic which should be emitted if error is found.
5185 /// \param NoteId Diagnostic note for the main error message.
5186 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00005187 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005188 /// \brief Return the 'x' lvalue part of the source atomic expression.
5189 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00005190 /// \brief Return the 'expr' rvalue part of the source atomic expression.
5191 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00005192 /// \brief Return the update expression used in calculation of the updated
5193 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5194 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5195 Expr *getUpdateExpr() const { return UpdateExpr; }
5196 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
5197 /// false otherwise.
5198 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5199
Alexey Bataevb78ca832015-04-01 03:33:17 +00005200 /// \brief true if the source expression is a postfix unary operation, false
5201 /// if it is a prefix unary operation.
5202 bool isPostfixUpdate() const { return IsPostfixUpdate; }
5203
Alexey Bataev1d160b12015-03-13 12:27:31 +00005204private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00005205 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5206 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005207};
5208} // namespace
5209
5210bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5211 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5212 ExprAnalysisErrorCode ErrorFound = NoError;
5213 SourceLocation ErrorLoc, NoteLoc;
5214 SourceRange ErrorRange, NoteRange;
5215 // Allowed constructs are:
5216 // x = x binop expr;
5217 // x = expr binop x;
5218 if (AtomicBinOp->getOpcode() == BO_Assign) {
5219 X = AtomicBinOp->getLHS();
5220 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
5221 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5222 if (AtomicInnerBinOp->isMultiplicativeOp() ||
5223 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5224 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005225 Op = AtomicInnerBinOp->getOpcode();
5226 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005227 auto *LHS = AtomicInnerBinOp->getLHS();
5228 auto *RHS = AtomicInnerBinOp->getRHS();
5229 llvm::FoldingSetNodeID XId, LHSId, RHSId;
5230 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
5231 /*Canonical=*/true);
5232 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
5233 /*Canonical=*/true);
5234 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
5235 /*Canonical=*/true);
5236 if (XId == LHSId) {
5237 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005238 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005239 } else if (XId == RHSId) {
5240 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005241 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005242 } else {
5243 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5244 ErrorRange = AtomicInnerBinOp->getSourceRange();
5245 NoteLoc = X->getExprLoc();
5246 NoteRange = X->getSourceRange();
5247 ErrorFound = NotAnUpdateExpression;
5248 }
5249 } else {
5250 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5251 ErrorRange = AtomicInnerBinOp->getSourceRange();
5252 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5253 NoteRange = SourceRange(NoteLoc, NoteLoc);
5254 ErrorFound = NotABinaryOperator;
5255 }
5256 } else {
5257 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5258 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5259 ErrorFound = NotABinaryExpression;
5260 }
5261 } else {
5262 ErrorLoc = AtomicBinOp->getExprLoc();
5263 ErrorRange = AtomicBinOp->getSourceRange();
5264 NoteLoc = AtomicBinOp->getOperatorLoc();
5265 NoteRange = SourceRange(NoteLoc, NoteLoc);
5266 ErrorFound = NotAnAssignmentOp;
5267 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005268 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005269 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5270 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5271 return true;
5272 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005273 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005274 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005275}
5276
5277bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5278 unsigned NoteId) {
5279 ExprAnalysisErrorCode ErrorFound = NoError;
5280 SourceLocation ErrorLoc, NoteLoc;
5281 SourceRange ErrorRange, NoteRange;
5282 // Allowed constructs are:
5283 // x++;
5284 // x--;
5285 // ++x;
5286 // --x;
5287 // x binop= expr;
5288 // x = x binop expr;
5289 // x = expr binop x;
5290 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5291 AtomicBody = AtomicBody->IgnoreParenImpCasts();
5292 if (AtomicBody->getType()->isScalarType() ||
5293 AtomicBody->isInstantiationDependent()) {
5294 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5295 AtomicBody->IgnoreParenImpCasts())) {
5296 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005297 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00005298 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00005299 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005300 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005301 X = AtomicCompAssignOp->getLHS();
5302 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005303 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5304 AtomicBody->IgnoreParenImpCasts())) {
5305 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005306 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
5307 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005308 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00005309 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
5310 // Check for Unary Operation
5311 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005312 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005313 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5314 OpLoc = AtomicUnaryOp->getOperatorLoc();
5315 X = AtomicUnaryOp->getSubExpr();
5316 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5317 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005318 } else {
5319 ErrorFound = NotAnUnaryIncDecExpression;
5320 ErrorLoc = AtomicUnaryOp->getExprLoc();
5321 ErrorRange = AtomicUnaryOp->getSourceRange();
5322 NoteLoc = AtomicUnaryOp->getOperatorLoc();
5323 NoteRange = SourceRange(NoteLoc, NoteLoc);
5324 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005325 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005326 ErrorFound = NotABinaryOrUnaryExpression;
5327 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5328 NoteRange = ErrorRange = AtomicBody->getSourceRange();
5329 }
5330 } else {
5331 ErrorFound = NotAScalarType;
5332 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
5333 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5334 }
5335 } else {
5336 ErrorFound = NotAnExpression;
5337 NoteLoc = ErrorLoc = S->getLocStart();
5338 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5339 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005340 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005341 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5342 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5343 return true;
5344 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005345 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005346 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005347 // Build an update expression of form 'OpaqueValueExpr(x) binop
5348 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
5349 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
5350 auto *OVEX = new (SemaRef.getASTContext())
5351 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
5352 auto *OVEExpr = new (SemaRef.getASTContext())
5353 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
5354 auto Update =
5355 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
5356 IsXLHSInRHSPart ? OVEExpr : OVEX);
5357 if (Update.isInvalid())
5358 return true;
5359 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
5360 Sema::AA_Casting);
5361 if (Update.isInvalid())
5362 return true;
5363 UpdateExpr = Update.get();
5364 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00005365 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005366}
5367
Alexey Bataev0162e452014-07-22 10:10:35 +00005368StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5369 Stmt *AStmt,
5370 SourceLocation StartLoc,
5371 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005372 if (!AStmt)
5373 return StmtError();
5374
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005375 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00005376 // 1.2.2 OpenMP Language Terminology
5377 // Structured block - An executable statement with a single entry at the
5378 // top and a single exit at the bottom.
5379 // The point of exit cannot be a branch out of the structured block.
5380 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00005381 OpenMPClauseKind AtomicKind = OMPC_unknown;
5382 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005383 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00005384 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00005385 C->getClauseKind() == OMPC_update ||
5386 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00005387 if (AtomicKind != OMPC_unknown) {
5388 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5389 << SourceRange(C->getLocStart(), C->getLocEnd());
5390 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5391 << getOpenMPClauseName(AtomicKind);
5392 } else {
5393 AtomicKind = C->getClauseKind();
5394 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005395 }
5396 }
5397 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005398
Alexey Bataev459dec02014-07-24 06:46:57 +00005399 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00005400 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5401 Body = EWC->getSubExpr();
5402
Alexey Bataev62cec442014-11-18 10:14:22 +00005403 Expr *X = nullptr;
5404 Expr *V = nullptr;
5405 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005406 Expr *UE = nullptr;
5407 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005408 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00005409 // OpenMP [2.12.6, atomic Construct]
5410 // In the next expressions:
5411 // * x and v (as applicable) are both l-value expressions with scalar type.
5412 // * During the execution of an atomic region, multiple syntactic
5413 // occurrences of x must designate the same storage location.
5414 // * Neither of v and expr (as applicable) may access the storage location
5415 // designated by x.
5416 // * Neither of x and expr (as applicable) may access the storage location
5417 // designated by v.
5418 // * expr is an expression with scalar type.
5419 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5420 // * binop, binop=, ++, and -- are not overloaded operators.
5421 // * The expression x binop expr must be numerically equivalent to x binop
5422 // (expr). This requirement is satisfied if the operators in expr have
5423 // precedence greater than binop, or by using parentheses around expr or
5424 // subexpressions of expr.
5425 // * The expression expr binop x must be numerically equivalent to (expr)
5426 // binop x. This requirement is satisfied if the operators in expr have
5427 // precedence equal to or greater than binop, or by using parentheses around
5428 // expr or subexpressions of expr.
5429 // * For forms that allow multiple occurrences of x, the number of times
5430 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00005431 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005432 enum {
5433 NotAnExpression,
5434 NotAnAssignmentOp,
5435 NotAScalarType,
5436 NotAnLValue,
5437 NoError
5438 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00005439 SourceLocation ErrorLoc, NoteLoc;
5440 SourceRange ErrorRange, NoteRange;
5441 // If clause is read:
5442 // v = x;
5443 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5444 auto AtomicBinOp =
5445 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5446 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5447 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5448 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5449 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5450 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5451 if (!X->isLValue() || !V->isLValue()) {
5452 auto NotLValueExpr = X->isLValue() ? V : X;
5453 ErrorFound = NotAnLValue;
5454 ErrorLoc = AtomicBinOp->getExprLoc();
5455 ErrorRange = AtomicBinOp->getSourceRange();
5456 NoteLoc = NotLValueExpr->getExprLoc();
5457 NoteRange = NotLValueExpr->getSourceRange();
5458 }
5459 } else if (!X->isInstantiationDependent() ||
5460 !V->isInstantiationDependent()) {
5461 auto NotScalarExpr =
5462 (X->isInstantiationDependent() || X->getType()->isScalarType())
5463 ? V
5464 : X;
5465 ErrorFound = NotAScalarType;
5466 ErrorLoc = AtomicBinOp->getExprLoc();
5467 ErrorRange = AtomicBinOp->getSourceRange();
5468 NoteLoc = NotScalarExpr->getExprLoc();
5469 NoteRange = NotScalarExpr->getSourceRange();
5470 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005471 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00005472 ErrorFound = NotAnAssignmentOp;
5473 ErrorLoc = AtomicBody->getExprLoc();
5474 ErrorRange = AtomicBody->getSourceRange();
5475 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5476 : AtomicBody->getExprLoc();
5477 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5478 : AtomicBody->getSourceRange();
5479 }
5480 } else {
5481 ErrorFound = NotAnExpression;
5482 NoteLoc = ErrorLoc = Body->getLocStart();
5483 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005484 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005485 if (ErrorFound != NoError) {
5486 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
5487 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005488 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5489 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00005490 return StmtError();
5491 } else if (CurContext->isDependentContext())
5492 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00005493 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005494 enum {
5495 NotAnExpression,
5496 NotAnAssignmentOp,
5497 NotAScalarType,
5498 NotAnLValue,
5499 NoError
5500 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005501 SourceLocation ErrorLoc, NoteLoc;
5502 SourceRange ErrorRange, NoteRange;
5503 // If clause is write:
5504 // x = expr;
5505 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5506 auto AtomicBinOp =
5507 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5508 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00005509 X = AtomicBinOp->getLHS();
5510 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00005511 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5512 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
5513 if (!X->isLValue()) {
5514 ErrorFound = NotAnLValue;
5515 ErrorLoc = AtomicBinOp->getExprLoc();
5516 ErrorRange = AtomicBinOp->getSourceRange();
5517 NoteLoc = X->getExprLoc();
5518 NoteRange = X->getSourceRange();
5519 }
5520 } else if (!X->isInstantiationDependent() ||
5521 !E->isInstantiationDependent()) {
5522 auto NotScalarExpr =
5523 (X->isInstantiationDependent() || X->getType()->isScalarType())
5524 ? E
5525 : X;
5526 ErrorFound = NotAScalarType;
5527 ErrorLoc = AtomicBinOp->getExprLoc();
5528 ErrorRange = AtomicBinOp->getSourceRange();
5529 NoteLoc = NotScalarExpr->getExprLoc();
5530 NoteRange = NotScalarExpr->getSourceRange();
5531 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005532 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00005533 ErrorFound = NotAnAssignmentOp;
5534 ErrorLoc = AtomicBody->getExprLoc();
5535 ErrorRange = AtomicBody->getSourceRange();
5536 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5537 : AtomicBody->getExprLoc();
5538 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5539 : AtomicBody->getSourceRange();
5540 }
5541 } else {
5542 ErrorFound = NotAnExpression;
5543 NoteLoc = ErrorLoc = Body->getLocStart();
5544 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005545 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00005546 if (ErrorFound != NoError) {
5547 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
5548 << ErrorRange;
5549 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5550 << NoteRange;
5551 return StmtError();
5552 } else if (CurContext->isDependentContext())
5553 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005554 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005555 // If clause is update:
5556 // x++;
5557 // x--;
5558 // ++x;
5559 // --x;
5560 // x binop= expr;
5561 // x = x binop expr;
5562 // x = expr binop x;
5563 OpenMPAtomicUpdateChecker Checker(*this);
5564 if (Checker.checkStatement(
5565 Body, (AtomicKind == OMPC_update)
5566 ? diag::err_omp_atomic_update_not_expression_statement
5567 : diag::err_omp_atomic_not_expression_statement,
5568 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00005569 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005570 if (!CurContext->isDependentContext()) {
5571 E = Checker.getExpr();
5572 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005573 UE = Checker.getUpdateExpr();
5574 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00005575 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005576 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005577 enum {
5578 NotAnAssignmentOp,
5579 NotACompoundStatement,
5580 NotTwoSubstatements,
5581 NotASpecificExpression,
5582 NoError
5583 } ErrorFound = NoError;
5584 SourceLocation ErrorLoc, NoteLoc;
5585 SourceRange ErrorRange, NoteRange;
5586 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5587 // If clause is a capture:
5588 // v = x++;
5589 // v = x--;
5590 // v = ++x;
5591 // v = --x;
5592 // v = x binop= expr;
5593 // v = x = x binop expr;
5594 // v = x = expr binop x;
5595 auto *AtomicBinOp =
5596 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5597 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5598 V = AtomicBinOp->getLHS();
5599 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5600 OpenMPAtomicUpdateChecker Checker(*this);
5601 if (Checker.checkStatement(
5602 Body, diag::err_omp_atomic_capture_not_expression_statement,
5603 diag::note_omp_atomic_update))
5604 return StmtError();
5605 E = Checker.getExpr();
5606 X = Checker.getX();
5607 UE = Checker.getUpdateExpr();
5608 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5609 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00005610 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005611 ErrorLoc = AtomicBody->getExprLoc();
5612 ErrorRange = AtomicBody->getSourceRange();
5613 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5614 : AtomicBody->getExprLoc();
5615 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5616 : AtomicBody->getSourceRange();
5617 ErrorFound = NotAnAssignmentOp;
5618 }
5619 if (ErrorFound != NoError) {
5620 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
5621 << ErrorRange;
5622 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5623 return StmtError();
5624 } else if (CurContext->isDependentContext()) {
5625 UE = V = E = X = nullptr;
5626 }
5627 } else {
5628 // If clause is a capture:
5629 // { v = x; x = expr; }
5630 // { v = x; x++; }
5631 // { v = x; x--; }
5632 // { v = x; ++x; }
5633 // { v = x; --x; }
5634 // { v = x; x binop= expr; }
5635 // { v = x; x = x binop expr; }
5636 // { v = x; x = expr binop x; }
5637 // { x++; v = x; }
5638 // { x--; v = x; }
5639 // { ++x; v = x; }
5640 // { --x; v = x; }
5641 // { x binop= expr; v = x; }
5642 // { x = x binop expr; v = x; }
5643 // { x = expr binop x; v = x; }
5644 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
5645 // Check that this is { expr1; expr2; }
5646 if (CS->size() == 2) {
5647 auto *First = CS->body_front();
5648 auto *Second = CS->body_back();
5649 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
5650 First = EWC->getSubExpr()->IgnoreParenImpCasts();
5651 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
5652 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
5653 // Need to find what subexpression is 'v' and what is 'x'.
5654 OpenMPAtomicUpdateChecker Checker(*this);
5655 bool IsUpdateExprFound = !Checker.checkStatement(Second);
5656 BinaryOperator *BinOp = nullptr;
5657 if (IsUpdateExprFound) {
5658 BinOp = dyn_cast<BinaryOperator>(First);
5659 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5660 }
5661 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5662 // { v = x; x++; }
5663 // { v = x; x--; }
5664 // { v = x; ++x; }
5665 // { v = x; --x; }
5666 // { v = x; x binop= expr; }
5667 // { v = x; x = x binop expr; }
5668 // { v = x; x = expr binop x; }
5669 // Check that the first expression has form v = x.
5670 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5671 llvm::FoldingSetNodeID XId, PossibleXId;
5672 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5673 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5674 IsUpdateExprFound = XId == PossibleXId;
5675 if (IsUpdateExprFound) {
5676 V = BinOp->getLHS();
5677 X = Checker.getX();
5678 E = Checker.getExpr();
5679 UE = Checker.getUpdateExpr();
5680 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005681 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005682 }
5683 }
5684 if (!IsUpdateExprFound) {
5685 IsUpdateExprFound = !Checker.checkStatement(First);
5686 BinOp = nullptr;
5687 if (IsUpdateExprFound) {
5688 BinOp = dyn_cast<BinaryOperator>(Second);
5689 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5690 }
5691 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5692 // { x++; v = x; }
5693 // { x--; v = x; }
5694 // { ++x; v = x; }
5695 // { --x; v = x; }
5696 // { x binop= expr; v = x; }
5697 // { x = x binop expr; v = x; }
5698 // { x = expr binop x; v = x; }
5699 // Check that the second expression has form v = x.
5700 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5701 llvm::FoldingSetNodeID XId, PossibleXId;
5702 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5703 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5704 IsUpdateExprFound = XId == PossibleXId;
5705 if (IsUpdateExprFound) {
5706 V = BinOp->getLHS();
5707 X = Checker.getX();
5708 E = Checker.getExpr();
5709 UE = Checker.getUpdateExpr();
5710 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005711 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005712 }
5713 }
5714 }
5715 if (!IsUpdateExprFound) {
5716 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00005717 auto *FirstExpr = dyn_cast<Expr>(First);
5718 auto *SecondExpr = dyn_cast<Expr>(Second);
5719 if (!FirstExpr || !SecondExpr ||
5720 !(FirstExpr->isInstantiationDependent() ||
5721 SecondExpr->isInstantiationDependent())) {
5722 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
5723 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005724 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00005725 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
5726 : First->getLocStart();
5727 NoteRange = ErrorRange = FirstBinOp
5728 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00005729 : SourceRange(ErrorLoc, ErrorLoc);
5730 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005731 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
5732 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
5733 ErrorFound = NotAnAssignmentOp;
5734 NoteLoc = ErrorLoc = SecondBinOp
5735 ? SecondBinOp->getOperatorLoc()
5736 : Second->getLocStart();
5737 NoteRange = ErrorRange =
5738 SecondBinOp ? SecondBinOp->getSourceRange()
5739 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00005740 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005741 auto *PossibleXRHSInFirst =
5742 FirstBinOp->getRHS()->IgnoreParenImpCasts();
5743 auto *PossibleXLHSInSecond =
5744 SecondBinOp->getLHS()->IgnoreParenImpCasts();
5745 llvm::FoldingSetNodeID X1Id, X2Id;
5746 PossibleXRHSInFirst->Profile(X1Id, Context,
5747 /*Canonical=*/true);
5748 PossibleXLHSInSecond->Profile(X2Id, Context,
5749 /*Canonical=*/true);
5750 IsUpdateExprFound = X1Id == X2Id;
5751 if (IsUpdateExprFound) {
5752 V = FirstBinOp->getLHS();
5753 X = SecondBinOp->getLHS();
5754 E = SecondBinOp->getRHS();
5755 UE = nullptr;
5756 IsXLHSInRHSPart = false;
5757 IsPostfixUpdate = true;
5758 } else {
5759 ErrorFound = NotASpecificExpression;
5760 ErrorLoc = FirstBinOp->getExprLoc();
5761 ErrorRange = FirstBinOp->getSourceRange();
5762 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
5763 NoteRange = SecondBinOp->getRHS()->getSourceRange();
5764 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005765 }
5766 }
5767 }
5768 }
5769 } else {
5770 NoteLoc = ErrorLoc = Body->getLocStart();
5771 NoteRange = ErrorRange =
5772 SourceRange(Body->getLocStart(), Body->getLocStart());
5773 ErrorFound = NotTwoSubstatements;
5774 }
5775 } else {
5776 NoteLoc = ErrorLoc = Body->getLocStart();
5777 NoteRange = ErrorRange =
5778 SourceRange(Body->getLocStart(), Body->getLocStart());
5779 ErrorFound = NotACompoundStatement;
5780 }
5781 if (ErrorFound != NoError) {
5782 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
5783 << ErrorRange;
5784 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5785 return StmtError();
5786 } else if (CurContext->isDependentContext()) {
5787 UE = V = E = X = nullptr;
5788 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005789 }
Alexey Bataevdea47612014-07-23 07:46:59 +00005790 }
Alexey Bataev0162e452014-07-22 10:10:35 +00005791
5792 getCurFunction()->setHasBranchProtectedScope();
5793
Alexey Bataev62cec442014-11-18 10:14:22 +00005794 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00005795 X, V, E, UE, IsXLHSInRHSPart,
5796 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00005797}
5798
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005799StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
5800 Stmt *AStmt,
5801 SourceLocation StartLoc,
5802 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005803 if (!AStmt)
5804 return StmtError();
5805
Samuel Antao4af1b7b2015-12-02 17:44:43 +00005806 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5807 // 1.2.2 OpenMP Language Terminology
5808 // Structured block - An executable statement with a single entry at the
5809 // top and a single exit at the bottom.
5810 // The point of exit cannot be a branch out of the structured block.
5811 // longjmp() and throw() must not violate the entry/exit criteria.
5812 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005813
Alexey Bataev13314bf2014-10-09 04:18:56 +00005814 // OpenMP [2.16, Nesting of Regions]
5815 // If specified, a teams construct must be contained within a target
5816 // construct. That target construct must contain no statements or directives
5817 // outside of the teams construct.
5818 if (DSAStack->hasInnerTeamsRegion()) {
5819 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
5820 bool OMPTeamsFound = true;
5821 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
5822 auto I = CS->body_begin();
5823 while (I != CS->body_end()) {
5824 auto OED = dyn_cast<OMPExecutableDirective>(*I);
5825 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
5826 OMPTeamsFound = false;
5827 break;
5828 }
5829 ++I;
5830 }
5831 assert(I != CS->body_end() && "Not found statement");
5832 S = *I;
5833 }
5834 if (!OMPTeamsFound) {
5835 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
5836 Diag(DSAStack->getInnerTeamsRegionLoc(),
5837 diag::note_omp_nested_teams_construct_here);
5838 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
5839 << isa<OMPExecutableDirective>(S);
5840 return StmtError();
5841 }
5842 }
5843
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005844 getCurFunction()->setHasBranchProtectedScope();
5845
5846 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5847}
5848
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00005849StmtResult
5850Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
5851 Stmt *AStmt, SourceLocation StartLoc,
5852 SourceLocation EndLoc) {
5853 if (!AStmt)
5854 return StmtError();
5855
5856 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5857 // 1.2.2 OpenMP Language Terminology
5858 // Structured block - An executable statement with a single entry at the
5859 // top and a single exit at the bottom.
5860 // The point of exit cannot be a branch out of the structured block.
5861 // longjmp() and throw() must not violate the entry/exit criteria.
5862 CS->getCapturedDecl()->setNothrow();
5863
5864 getCurFunction()->setHasBranchProtectedScope();
5865
5866 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5867 AStmt);
5868}
5869
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00005870StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
5871 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5872 SourceLocation EndLoc,
5873 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5874 if (!AStmt)
5875 return StmtError();
5876
5877 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5878 // 1.2.2 OpenMP Language Terminology
5879 // Structured block - An executable statement with a single entry at the
5880 // top and a single exit at the bottom.
5881 // The point of exit cannot be a branch out of the structured block.
5882 // longjmp() and throw() must not violate the entry/exit criteria.
5883 CS->getCapturedDecl()->setNothrow();
5884
5885 OMPLoopDirective::HelperExprs B;
5886 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5887 // define the nested loops number.
5888 unsigned NestedLoopCount =
5889 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
5890 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5891 VarsWithImplicitDSA, B);
5892 if (NestedLoopCount == 0)
5893 return StmtError();
5894
5895 assert((CurContext->isDependentContext() || B.builtAll()) &&
5896 "omp target parallel for loop exprs were not built");
5897
5898 if (!CurContext->isDependentContext()) {
5899 // Finalize the clauses that need pre-built expressions for CodeGen.
5900 for (auto C : Clauses) {
5901 if (auto LC = dyn_cast<OMPLinearClause>(C))
5902 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5903 B.NumIterations, *this, CurScope))
5904 return StmtError();
5905 }
5906 }
5907
5908 getCurFunction()->setHasBranchProtectedScope();
5909 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
5910 NestedLoopCount, Clauses, AStmt,
5911 B, DSAStack->isCancelRegion());
5912}
5913
Samuel Antaodf67fc42016-01-19 19:15:56 +00005914/// \brief Check for existence of a map clause in the list of clauses.
5915static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
5916 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
5917 I != E; ++I) {
5918 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
5919 return true;
5920 }
5921 }
5922
5923 return false;
5924}
5925
Michael Wong65f367f2015-07-21 13:44:28 +00005926StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
5927 Stmt *AStmt,
5928 SourceLocation StartLoc,
5929 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005930 if (!AStmt)
5931 return StmtError();
5932
5933 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5934
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00005935 // OpenMP [2.10.1, Restrictions, p. 97]
5936 // At least one map clause must appear on the directive.
5937 if (!HasMapClause(Clauses)) {
5938 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
5939 getOpenMPDirectiveName(OMPD_target_data);
5940 return StmtError();
5941 }
5942
Michael Wong65f367f2015-07-21 13:44:28 +00005943 getCurFunction()->setHasBranchProtectedScope();
5944
5945 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
5946 AStmt);
5947}
5948
Samuel Antaodf67fc42016-01-19 19:15:56 +00005949StmtResult
5950Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
5951 SourceLocation StartLoc,
5952 SourceLocation EndLoc) {
5953 // OpenMP [2.10.2, Restrictions, p. 99]
5954 // At least one map clause must appear on the directive.
5955 if (!HasMapClause(Clauses)) {
5956 Diag(StartLoc, diag::err_omp_no_map_for_directive)
5957 << getOpenMPDirectiveName(OMPD_target_enter_data);
5958 return StmtError();
5959 }
5960
5961 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
5962 Clauses);
5963}
5964
Samuel Antao72590762016-01-19 20:04:50 +00005965StmtResult
5966Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
5967 SourceLocation StartLoc,
5968 SourceLocation EndLoc) {
5969 // OpenMP [2.10.3, Restrictions, p. 102]
5970 // At least one map clause must appear on the directive.
5971 if (!HasMapClause(Clauses)) {
5972 Diag(StartLoc, diag::err_omp_no_map_for_directive)
5973 << getOpenMPDirectiveName(OMPD_target_exit_data);
5974 return StmtError();
5975 }
5976
5977 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
5978}
5979
Alexey Bataev13314bf2014-10-09 04:18:56 +00005980StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
5981 Stmt *AStmt, SourceLocation StartLoc,
5982 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005983 if (!AStmt)
5984 return StmtError();
5985
Alexey Bataev13314bf2014-10-09 04:18:56 +00005986 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5987 // 1.2.2 OpenMP Language Terminology
5988 // Structured block - An executable statement with a single entry at the
5989 // top and a single exit at the bottom.
5990 // The point of exit cannot be a branch out of the structured block.
5991 // longjmp() and throw() must not violate the entry/exit criteria.
5992 CS->getCapturedDecl()->setNothrow();
5993
5994 getCurFunction()->setHasBranchProtectedScope();
5995
5996 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5997}
5998
Alexey Bataev6d4ed052015-07-01 06:57:41 +00005999StmtResult
6000Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6001 SourceLocation EndLoc,
6002 OpenMPDirectiveKind CancelRegion) {
6003 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6004 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6005 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6006 << getOpenMPDirectiveName(CancelRegion);
6007 return StmtError();
6008 }
6009 if (DSAStack->isParentNowaitRegion()) {
6010 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6011 return StmtError();
6012 }
6013 if (DSAStack->isParentOrderedRegion()) {
6014 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
6015 return StmtError();
6016 }
6017 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
6018 CancelRegion);
6019}
6020
Alexey Bataev87933c72015-09-18 08:07:34 +00006021StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
6022 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00006023 SourceLocation EndLoc,
6024 OpenMPDirectiveKind CancelRegion) {
6025 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6026 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6027 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6028 << getOpenMPDirectiveName(CancelRegion);
6029 return StmtError();
6030 }
6031 if (DSAStack->isParentNowaitRegion()) {
6032 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6033 return StmtError();
6034 }
6035 if (DSAStack->isParentOrderedRegion()) {
6036 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6037 return StmtError();
6038 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006039 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00006040 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6041 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00006042}
6043
Alexey Bataev382967a2015-12-08 12:06:20 +00006044static bool checkGrainsizeNumTasksClauses(Sema &S,
6045 ArrayRef<OMPClause *> Clauses) {
6046 OMPClause *PrevClause = nullptr;
6047 bool ErrorFound = false;
6048 for (auto *C : Clauses) {
6049 if (C->getClauseKind() == OMPC_grainsize ||
6050 C->getClauseKind() == OMPC_num_tasks) {
6051 if (!PrevClause)
6052 PrevClause = C;
6053 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6054 S.Diag(C->getLocStart(),
6055 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6056 << getOpenMPClauseName(C->getClauseKind())
6057 << getOpenMPClauseName(PrevClause->getClauseKind());
6058 S.Diag(PrevClause->getLocStart(),
6059 diag::note_omp_previous_grainsize_num_tasks)
6060 << getOpenMPClauseName(PrevClause->getClauseKind());
6061 ErrorFound = true;
6062 }
6063 }
6064 }
6065 return ErrorFound;
6066}
6067
Alexey Bataev49f6e782015-12-01 04:18:41 +00006068StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6069 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6070 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006071 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00006072 if (!AStmt)
6073 return StmtError();
6074
6075 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6076 OMPLoopDirective::HelperExprs B;
6077 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6078 // define the nested loops number.
6079 unsigned NestedLoopCount =
6080 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006081 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00006082 VarsWithImplicitDSA, B);
6083 if (NestedLoopCount == 0)
6084 return StmtError();
6085
6086 assert((CurContext->isDependentContext() || B.builtAll()) &&
6087 "omp for loop exprs were not built");
6088
Alexey Bataev382967a2015-12-08 12:06:20 +00006089 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6090 // The grainsize clause and num_tasks clause are mutually exclusive and may
6091 // not appear on the same taskloop directive.
6092 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6093 return StmtError();
6094
Alexey Bataev49f6e782015-12-01 04:18:41 +00006095 getCurFunction()->setHasBranchProtectedScope();
6096 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
6097 NestedLoopCount, Clauses, AStmt, B);
6098}
6099
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006100StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
6101 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6102 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006103 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006104 if (!AStmt)
6105 return StmtError();
6106
6107 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6108 OMPLoopDirective::HelperExprs B;
6109 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6110 // define the nested loops number.
6111 unsigned NestedLoopCount =
6112 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
6113 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6114 VarsWithImplicitDSA, B);
6115 if (NestedLoopCount == 0)
6116 return StmtError();
6117
6118 assert((CurContext->isDependentContext() || B.builtAll()) &&
6119 "omp for loop exprs were not built");
6120
Alexey Bataev382967a2015-12-08 12:06:20 +00006121 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6122 // The grainsize clause and num_tasks clause are mutually exclusive and may
6123 // not appear on the same taskloop directive.
6124 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6125 return StmtError();
6126
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006127 getCurFunction()->setHasBranchProtectedScope();
6128 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
6129 NestedLoopCount, Clauses, AStmt, B);
6130}
6131
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006132StmtResult Sema::ActOnOpenMPDistributeDirective(
6133 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6134 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006135 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006136 if (!AStmt)
6137 return StmtError();
6138
6139 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6140 OMPLoopDirective::HelperExprs B;
6141 // In presence of clause 'collapse' with number of loops, it will
6142 // define the nested loops number.
6143 unsigned NestedLoopCount =
6144 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
6145 nullptr /*ordered not a clause on distribute*/, AStmt,
6146 *this, *DSAStack, VarsWithImplicitDSA, B);
6147 if (NestedLoopCount == 0)
6148 return StmtError();
6149
6150 assert((CurContext->isDependentContext() || B.builtAll()) &&
6151 "omp for loop exprs were not built");
6152
6153 getCurFunction()->setHasBranchProtectedScope();
6154 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
6155 NestedLoopCount, Clauses, AStmt, B);
6156}
6157
Alexey Bataeved09d242014-05-28 05:53:51 +00006158OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006159 SourceLocation StartLoc,
6160 SourceLocation LParenLoc,
6161 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006162 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006163 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00006164 case OMPC_final:
6165 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
6166 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00006167 case OMPC_num_threads:
6168 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
6169 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006170 case OMPC_safelen:
6171 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
6172 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00006173 case OMPC_simdlen:
6174 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
6175 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00006176 case OMPC_collapse:
6177 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
6178 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006179 case OMPC_ordered:
6180 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
6181 break;
Michael Wonge710d542015-08-07 16:16:36 +00006182 case OMPC_device:
6183 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
6184 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00006185 case OMPC_num_teams:
6186 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
6187 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006188 case OMPC_thread_limit:
6189 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
6190 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00006191 case OMPC_priority:
6192 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
6193 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006194 case OMPC_grainsize:
6195 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
6196 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00006197 case OMPC_num_tasks:
6198 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
6199 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00006200 case OMPC_hint:
6201 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
6202 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006203 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006204 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006205 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006206 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006207 case OMPC_private:
6208 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006209 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006210 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006211 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006212 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006213 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006214 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006215 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00006216 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006217 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006218 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006219 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006220 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006221 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006222 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006223 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006224 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006225 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006226 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00006227 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006228 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006229 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00006230 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006231 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006232 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006233 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006234 llvm_unreachable("Clause is not allowed.");
6235 }
6236 return Res;
6237}
6238
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006239OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
6240 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006241 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006242 SourceLocation NameModifierLoc,
6243 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006244 SourceLocation EndLoc) {
6245 Expr *ValExpr = Condition;
6246 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6247 !Condition->isInstantiationDependent() &&
6248 !Condition->containsUnexpandedParameterPack()) {
6249 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00006250 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006251 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006252 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006253
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006254 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006255 }
6256
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006257 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
6258 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006259}
6260
Alexey Bataev3778b602014-07-17 07:32:53 +00006261OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
6262 SourceLocation StartLoc,
6263 SourceLocation LParenLoc,
6264 SourceLocation EndLoc) {
6265 Expr *ValExpr = Condition;
6266 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6267 !Condition->isInstantiationDependent() &&
6268 !Condition->containsUnexpandedParameterPack()) {
6269 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
6270 Condition->getExprLoc(), Condition);
6271 if (Val.isInvalid())
6272 return nullptr;
6273
6274 ValExpr = Val.get();
6275 }
6276
6277 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
6278}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006279ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
6280 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00006281 if (!Op)
6282 return ExprError();
6283
6284 class IntConvertDiagnoser : public ICEConvertDiagnoser {
6285 public:
6286 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00006287 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00006288 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
6289 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006290 return S.Diag(Loc, diag::err_omp_not_integral) << T;
6291 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006292 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
6293 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006294 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
6295 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006296 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
6297 QualType T,
6298 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006299 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
6300 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006301 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
6302 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006303 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006304 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006305 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006306 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
6307 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006308 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
6309 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006310 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
6311 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006312 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006313 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006314 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006315 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
6316 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006317 llvm_unreachable("conversion functions are permitted");
6318 }
6319 } ConvertDiagnoser;
6320 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
6321}
6322
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006323static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00006324 OpenMPClauseKind CKind,
6325 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006326 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
6327 !ValExpr->isInstantiationDependent()) {
6328 SourceLocation Loc = ValExpr->getExprLoc();
6329 ExprResult Value =
6330 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
6331 if (Value.isInvalid())
6332 return false;
6333
6334 ValExpr = Value.get();
6335 // The expression must evaluate to a non-negative integer value.
6336 llvm::APSInt Result;
6337 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00006338 Result.isSigned() &&
6339 !((!StrictlyPositive && Result.isNonNegative()) ||
6340 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006341 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006342 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6343 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006344 return false;
6345 }
6346 }
6347 return true;
6348}
6349
Alexey Bataev568a8332014-03-06 06:15:19 +00006350OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
6351 SourceLocation StartLoc,
6352 SourceLocation LParenLoc,
6353 SourceLocation EndLoc) {
6354 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00006355
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006356 // OpenMP [2.5, Restrictions]
6357 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00006358 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
6359 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006360 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00006361
Alexey Bataeved09d242014-05-28 05:53:51 +00006362 return new (Context)
6363 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00006364}
6365
Alexey Bataev62c87d22014-03-21 04:51:18 +00006366ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006367 OpenMPClauseKind CKind,
6368 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006369 if (!E)
6370 return ExprError();
6371 if (E->isValueDependent() || E->isTypeDependent() ||
6372 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00006373 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006374 llvm::APSInt Result;
6375 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
6376 if (ICE.isInvalid())
6377 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006378 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
6379 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006380 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006381 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6382 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00006383 return ExprError();
6384 }
Alexander Musman09184fe2014-09-30 05:29:28 +00006385 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
6386 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
6387 << E->getSourceRange();
6388 return ExprError();
6389 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006390 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
6391 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00006392 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006393 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00006394 return ICE;
6395}
6396
6397OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
6398 SourceLocation LParenLoc,
6399 SourceLocation EndLoc) {
6400 // OpenMP [2.8.1, simd construct, Description]
6401 // The parameter of the safelen clause must be a constant
6402 // positive integer expression.
6403 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
6404 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006405 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006406 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006407 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00006408}
6409
Alexey Bataev66b15b52015-08-21 11:14:16 +00006410OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
6411 SourceLocation LParenLoc,
6412 SourceLocation EndLoc) {
6413 // OpenMP [2.8.1, simd construct, Description]
6414 // The parameter of the simdlen clause must be a constant
6415 // positive integer expression.
6416 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
6417 if (Simdlen.isInvalid())
6418 return nullptr;
6419 return new (Context)
6420 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
6421}
6422
Alexander Musman64d33f12014-06-04 07:53:32 +00006423OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
6424 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00006425 SourceLocation LParenLoc,
6426 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00006427 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006428 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00006429 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006430 // The parameter of the collapse clause must be a constant
6431 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00006432 ExprResult NumForLoopsResult =
6433 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
6434 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00006435 return nullptr;
6436 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00006437 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00006438}
6439
Alexey Bataev10e775f2015-07-30 11:36:16 +00006440OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
6441 SourceLocation EndLoc,
6442 SourceLocation LParenLoc,
6443 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00006444 // OpenMP [2.7.1, loop construct, Description]
6445 // OpenMP [2.8.1, simd construct, Description]
6446 // OpenMP [2.9.6, distribute construct, Description]
6447 // The parameter of the ordered clause must be a constant
6448 // positive integer expression if any.
6449 if (NumForLoops && LParenLoc.isValid()) {
6450 ExprResult NumForLoopsResult =
6451 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
6452 if (NumForLoopsResult.isInvalid())
6453 return nullptr;
6454 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00006455 } else
6456 NumForLoops = nullptr;
6457 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00006458 return new (Context)
6459 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
6460}
6461
Alexey Bataeved09d242014-05-28 05:53:51 +00006462OMPClause *Sema::ActOnOpenMPSimpleClause(
6463 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
6464 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006465 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006466 switch (Kind) {
6467 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006468 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00006469 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
6470 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006471 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006472 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00006473 Res = ActOnOpenMPProcBindClause(
6474 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
6475 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006476 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006477 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006478 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006479 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006480 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006481 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006482 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006483 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006484 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006485 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006486 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006487 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006488 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006489 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006490 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006491 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006492 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006493 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006494 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006495 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006496 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006497 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006498 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006499 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006500 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006501 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006502 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006503 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006504 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006505 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006506 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006507 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006508 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006509 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006510 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006511 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006512 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006513 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006514 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006515 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006516 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006517 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006518 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006519 llvm_unreachable("Clause is not allowed.");
6520 }
6521 return Res;
6522}
6523
Alexey Bataev6402bca2015-12-28 07:25:51 +00006524static std::string
6525getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
6526 ArrayRef<unsigned> Exclude = llvm::None) {
6527 std::string Values;
6528 unsigned Bound = Last >= 2 ? Last - 2 : 0;
6529 unsigned Skipped = Exclude.size();
6530 auto S = Exclude.begin(), E = Exclude.end();
6531 for (unsigned i = First; i < Last; ++i) {
6532 if (std::find(S, E, i) != E) {
6533 --Skipped;
6534 continue;
6535 }
6536 Values += "'";
6537 Values += getOpenMPSimpleClauseTypeName(K, i);
6538 Values += "'";
6539 if (i == Bound - Skipped)
6540 Values += " or ";
6541 else if (i != Bound + 1 - Skipped)
6542 Values += ", ";
6543 }
6544 return Values;
6545}
6546
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006547OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
6548 SourceLocation KindKwLoc,
6549 SourceLocation StartLoc,
6550 SourceLocation LParenLoc,
6551 SourceLocation EndLoc) {
6552 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00006553 static_assert(OMPC_DEFAULT_unknown > 0,
6554 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006555 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006556 << getListOfPossibleValues(OMPC_default, /*First=*/0,
6557 /*Last=*/OMPC_DEFAULT_unknown)
6558 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006559 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006560 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00006561 switch (Kind) {
6562 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006563 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006564 break;
6565 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006566 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006567 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006568 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006569 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00006570 break;
6571 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006572 return new (Context)
6573 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006574}
6575
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006576OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
6577 SourceLocation KindKwLoc,
6578 SourceLocation StartLoc,
6579 SourceLocation LParenLoc,
6580 SourceLocation EndLoc) {
6581 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006582 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006583 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
6584 /*Last=*/OMPC_PROC_BIND_unknown)
6585 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006586 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006587 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006588 return new (Context)
6589 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006590}
6591
Alexey Bataev56dafe82014-06-20 07:16:17 +00006592OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006593 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006594 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006595 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006596 SourceLocation EndLoc) {
6597 OMPClause *Res = nullptr;
6598 switch (Kind) {
6599 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006600 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
6601 assert(Argument.size() == NumberOfElements &&
6602 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006603 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006604 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
6605 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
6606 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
6607 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
6608 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006609 break;
6610 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006611 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
6612 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
6613 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
6614 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006615 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00006616 case OMPC_dist_schedule:
6617 Res = ActOnOpenMPDistScheduleClause(
6618 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
6619 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
6620 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006621 case OMPC_defaultmap:
6622 enum { Modifier, DefaultmapKind };
6623 Res = ActOnOpenMPDefaultmapClause(
6624 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
6625 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
6626 StartLoc, LParenLoc, ArgumentLoc[Modifier],
6627 ArgumentLoc[DefaultmapKind], EndLoc);
6628 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00006629 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006630 case OMPC_num_threads:
6631 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006632 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006633 case OMPC_collapse:
6634 case OMPC_default:
6635 case OMPC_proc_bind:
6636 case OMPC_private:
6637 case OMPC_firstprivate:
6638 case OMPC_lastprivate:
6639 case OMPC_shared:
6640 case OMPC_reduction:
6641 case OMPC_linear:
6642 case OMPC_aligned:
6643 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006644 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006645 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006646 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006647 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006648 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006649 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006650 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006651 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006652 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006653 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006654 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006655 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006656 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006657 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006658 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006659 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006660 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006661 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006662 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006663 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006664 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006665 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006666 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006667 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006668 case OMPC_unknown:
6669 llvm_unreachable("Clause is not allowed.");
6670 }
6671 return Res;
6672}
6673
Alexey Bataev6402bca2015-12-28 07:25:51 +00006674static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
6675 OpenMPScheduleClauseModifier M2,
6676 SourceLocation M1Loc, SourceLocation M2Loc) {
6677 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
6678 SmallVector<unsigned, 2> Excluded;
6679 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
6680 Excluded.push_back(M2);
6681 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
6682 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
6683 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
6684 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
6685 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
6686 << getListOfPossibleValues(OMPC_schedule,
6687 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
6688 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6689 Excluded)
6690 << getOpenMPClauseName(OMPC_schedule);
6691 return true;
6692 }
6693 return false;
6694}
6695
Alexey Bataev56dafe82014-06-20 07:16:17 +00006696OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006697 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006698 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006699 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
6700 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
6701 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
6702 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
6703 return nullptr;
6704 // OpenMP, 2.7.1, Loop Construct, Restrictions
6705 // Either the monotonic modifier or the nonmonotonic modifier can be specified
6706 // but not both.
6707 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
6708 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
6709 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
6710 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
6711 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
6712 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
6713 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
6714 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
6715 return nullptr;
6716 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006717 if (Kind == OMPC_SCHEDULE_unknown) {
6718 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00006719 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
6720 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
6721 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6722 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6723 Exclude);
6724 } else {
6725 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6726 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006727 }
6728 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
6729 << Values << getOpenMPClauseName(OMPC_schedule);
6730 return nullptr;
6731 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00006732 // OpenMP, 2.7.1, Loop Construct, Restrictions
6733 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
6734 // schedule(guided).
6735 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
6736 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
6737 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
6738 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
6739 diag::err_omp_schedule_nonmonotonic_static);
6740 return nullptr;
6741 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006742 Expr *ValExpr = ChunkSize;
Alexey Bataev040d5402015-05-12 08:35:28 +00006743 Expr *HelperValExpr = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006744 if (ChunkSize) {
6745 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
6746 !ChunkSize->isInstantiationDependent() &&
6747 !ChunkSize->containsUnexpandedParameterPack()) {
6748 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
6749 ExprResult Val =
6750 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
6751 if (Val.isInvalid())
6752 return nullptr;
6753
6754 ValExpr = Val.get();
6755
6756 // OpenMP [2.7.1, Restrictions]
6757 // chunk_size must be a loop invariant integer expression with a positive
6758 // value.
6759 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00006760 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
6761 if (Result.isSigned() && !Result.isStrictlyPositive()) {
6762 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006763 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00006764 return nullptr;
6765 }
6766 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev4244be22016-02-11 05:35:55 +00006767 HelperValExpr =
6768 buildCapture(*this, &Context.Idents.get(".chunk."), ValExpr);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006769 }
6770 }
6771 }
6772
Alexey Bataev6402bca2015-12-28 07:25:51 +00006773 return new (Context)
6774 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
6775 ValExpr, HelperValExpr, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006776}
6777
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006778OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
6779 SourceLocation StartLoc,
6780 SourceLocation EndLoc) {
6781 OMPClause *Res = nullptr;
6782 switch (Kind) {
6783 case OMPC_ordered:
6784 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
6785 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00006786 case OMPC_nowait:
6787 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
6788 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006789 case OMPC_untied:
6790 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
6791 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006792 case OMPC_mergeable:
6793 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
6794 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006795 case OMPC_read:
6796 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
6797 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00006798 case OMPC_write:
6799 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
6800 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006801 case OMPC_update:
6802 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
6803 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00006804 case OMPC_capture:
6805 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
6806 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006807 case OMPC_seq_cst:
6808 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
6809 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00006810 case OMPC_threads:
6811 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
6812 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006813 case OMPC_simd:
6814 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
6815 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00006816 case OMPC_nogroup:
6817 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
6818 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006819 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006820 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006821 case OMPC_num_threads:
6822 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006823 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006824 case OMPC_collapse:
6825 case OMPC_schedule:
6826 case OMPC_private:
6827 case OMPC_firstprivate:
6828 case OMPC_lastprivate:
6829 case OMPC_shared:
6830 case OMPC_reduction:
6831 case OMPC_linear:
6832 case OMPC_aligned:
6833 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006834 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006835 case OMPC_default:
6836 case OMPC_proc_bind:
6837 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006838 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006839 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006840 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006841 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006842 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006843 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006844 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006845 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00006846 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006847 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006848 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006849 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006850 case OMPC_unknown:
6851 llvm_unreachable("Clause is not allowed.");
6852 }
6853 return Res;
6854}
6855
Alexey Bataev236070f2014-06-20 11:19:47 +00006856OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
6857 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006858 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00006859 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
6860}
6861
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006862OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
6863 SourceLocation EndLoc) {
6864 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
6865}
6866
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006867OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
6868 SourceLocation EndLoc) {
6869 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
6870}
6871
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006872OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
6873 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006874 return new (Context) OMPReadClause(StartLoc, EndLoc);
6875}
6876
Alexey Bataevdea47612014-07-23 07:46:59 +00006877OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
6878 SourceLocation EndLoc) {
6879 return new (Context) OMPWriteClause(StartLoc, EndLoc);
6880}
6881
Alexey Bataev67a4f222014-07-23 10:25:33 +00006882OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
6883 SourceLocation EndLoc) {
6884 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
6885}
6886
Alexey Bataev459dec02014-07-24 06:46:57 +00006887OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
6888 SourceLocation EndLoc) {
6889 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
6890}
6891
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006892OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
6893 SourceLocation EndLoc) {
6894 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
6895}
6896
Alexey Bataev346265e2015-09-25 10:37:12 +00006897OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
6898 SourceLocation EndLoc) {
6899 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
6900}
6901
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006902OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
6903 SourceLocation EndLoc) {
6904 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
6905}
6906
Alexey Bataevb825de12015-12-07 10:51:44 +00006907OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
6908 SourceLocation EndLoc) {
6909 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
6910}
6911
Alexey Bataevc5e02582014-06-16 07:08:35 +00006912OMPClause *Sema::ActOnOpenMPVarListClause(
6913 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
6914 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
6915 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006916 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00006917 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
6918 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
6919 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006920 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006921 switch (Kind) {
6922 case OMPC_private:
6923 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6924 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006925 case OMPC_firstprivate:
6926 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6927 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006928 case OMPC_lastprivate:
6929 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6930 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00006931 case OMPC_shared:
6932 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
6933 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006934 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00006935 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
6936 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006937 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00006938 case OMPC_linear:
6939 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00006940 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00006941 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006942 case OMPC_aligned:
6943 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
6944 ColonLoc, EndLoc);
6945 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006946 case OMPC_copyin:
6947 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
6948 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006949 case OMPC_copyprivate:
6950 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6951 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00006952 case OMPC_flush:
6953 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
6954 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006955 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006956 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
6957 StartLoc, LParenLoc, EndLoc);
6958 break;
6959 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00006960 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
6961 DepLinMapLoc, ColonLoc, VarList, StartLoc,
6962 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006963 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006964 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006965 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006966 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006967 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006968 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006969 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006970 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006971 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006972 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006973 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006974 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006975 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006976 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006977 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006978 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006979 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006980 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006981 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006982 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00006983 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006984 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006985 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006986 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006987 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006988 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006989 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006990 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006991 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006992 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006993 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006994 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006995 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006996 llvm_unreachable("Clause is not allowed.");
6997 }
6998 return Res;
6999}
7000
Alexey Bataev90c228f2016-02-08 09:29:13 +00007001ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
7002 ExprObjectKind OK) {
7003 SourceLocation Loc = Capture->getInit()->getExprLoc();
7004 ExprResult Res = BuildDeclRefExpr(
7005 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
7006 if (!Res.isUsable())
7007 return ExprError();
7008 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
7009 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
7010 if (!Res.isUsable())
7011 return ExprError();
7012 }
7013 if (VK != VK_LValue && Res.get()->isGLValue()) {
7014 Res = DefaultLvalueConversion(Res.get());
7015 if (!Res.isUsable())
7016 return ExprError();
7017 }
7018 return Res;
7019}
7020
Alexey Bataevd985eda2016-02-10 11:29:16 +00007021static std::pair<ValueDecl *, bool> getPrivateItem(Sema &S, Expr *RefExpr) {
7022 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7023 RefExpr->containsUnexpandedParameterPack())
7024 return std::make_pair(nullptr, true);
7025
7026 SourceLocation ELoc = RefExpr->getExprLoc();
7027 SourceRange SR = RefExpr->getSourceRange();
7028 // OpenMP [3.1, C/C++]
7029 // A list item is a variable name.
7030 // OpenMP [2.9.3.3, Restrictions, p.1]
7031 // A variable that is part of another variable (as an array or
7032 // structure element) cannot appear in a private clause.
7033 RefExpr = RefExpr->IgnoreParens();
7034 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
7035 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
7036 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
7037 (S.getCurrentThisType().isNull() || !ME ||
7038 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
7039 !isa<FieldDecl>(ME->getMemberDecl()))) {
7040 S.Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7041 << (S.getCurrentThisType().isNull() ? 0 : 1) << SR;
7042 return std::make_pair(nullptr, false);
7043 }
7044 return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
7045}
7046
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007047OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
7048 SourceLocation StartLoc,
7049 SourceLocation LParenLoc,
7050 SourceLocation EndLoc) {
7051 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00007052 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00007053 for (auto &RefExpr : VarList) {
7054 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataevd985eda2016-02-10 11:29:16 +00007055 auto Res = getPrivateItem(*this, RefExpr);
7056 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007057 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007058 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007059 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007060 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007061 ValueDecl *D = Res.first;
7062 if (!D)
7063 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007064
Alexey Bataeved09d242014-05-28 05:53:51 +00007065 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007066 QualType Type = D->getType();
7067 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007068
7069 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7070 // A variable that appears in a private clause must not have an incomplete
7071 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007072 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007073 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007074 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007075
Alexey Bataev758e55e2013-09-06 18:03:48 +00007076 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7077 // in a Construct]
7078 // Variables with the predetermined data-sharing attributes may not be
7079 // listed in data-sharing attributes clauses, except for the cases
7080 // listed below. For these exceptions only, listing a predetermined
7081 // variable in a data-sharing attribute clause is allowed and overrides
7082 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007083 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007084 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007085 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7086 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007087 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007088 continue;
7089 }
7090
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007091 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007092 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007093 DSAStack->getCurrentDirective() == OMPD_task) {
7094 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7095 << getOpenMPClauseName(OMPC_private) << Type
7096 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7097 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007098 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007099 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007100 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007101 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007102 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007103 continue;
7104 }
7105
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007106 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
7107 // A variable of class type (or array thereof) that appears in a private
7108 // clause requires an accessible, unambiguous default constructor for the
7109 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00007110 // Generate helper private variable and initialize it with the default
7111 // value. The address of the original variable is replaced by the address of
7112 // the new private variable in CodeGen. This new variable is not added to
7113 // IdResolver, so the code in the OpenMP region uses original variable for
7114 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007115 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007116 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7117 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007118 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007119 if (VDPrivate->isInvalidDecl())
7120 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007121 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007122 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007123
Alexey Bataev90c228f2016-02-08 09:29:13 +00007124 DeclRefExpr *Ref = nullptr;
7125 if (!VD)
7126 Ref = buildCapture(*this, D->getIdentifier(), RefExpr);
7127 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
7128 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007129 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007130 }
7131
Alexey Bataeved09d242014-05-28 05:53:51 +00007132 if (Vars.empty())
7133 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007134
Alexey Bataev03b340a2014-10-21 03:16:40 +00007135 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7136 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007137}
7138
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007139namespace {
7140class DiagsUninitializedSeveretyRAII {
7141private:
7142 DiagnosticsEngine &Diags;
7143 SourceLocation SavedLoc;
7144 bool IsIgnored;
7145
7146public:
7147 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
7148 bool IsIgnored)
7149 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
7150 if (!IsIgnored) {
7151 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
7152 /*Map*/ diag::Severity::Ignored, Loc);
7153 }
7154 }
7155 ~DiagsUninitializedSeveretyRAII() {
7156 if (!IsIgnored)
7157 Diags.popMappings(SavedLoc);
7158 }
7159};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007160}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007161
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007162OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
7163 SourceLocation StartLoc,
7164 SourceLocation LParenLoc,
7165 SourceLocation EndLoc) {
7166 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007167 SmallVector<Expr *, 8> PrivateCopies;
7168 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007169 bool IsImplicitClause =
7170 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
7171 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
7172
Alexey Bataeved09d242014-05-28 05:53:51 +00007173 for (auto &RefExpr : VarList) {
7174 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataevd985eda2016-02-10 11:29:16 +00007175 auto Res = getPrivateItem(*this, RefExpr);
7176 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007177 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007178 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007179 PrivateCopies.push_back(nullptr);
7180 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007181 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007182 ValueDecl *D = Res.first;
7183 if (!D)
7184 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007185
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007186 SourceLocation ELoc =
7187 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007188 QualType Type = D->getType();
7189 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007190
7191 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7192 // A variable that appears in a private clause must not have an incomplete
7193 // type or a reference type.
7194 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00007195 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007196 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007197 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007198
7199 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
7200 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00007201 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007202 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007203 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007204
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007205 // If an implicit firstprivate variable found it was checked already.
7206 if (!IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007207 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007208 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007209 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
7210 // A list item that specifies a given variable may not appear in more
7211 // than one clause on the same directive, except that a variable may be
7212 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007213 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00007214 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007215 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007216 << getOpenMPClauseName(DVar.CKind)
7217 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007218 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007219 continue;
7220 }
7221
7222 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7223 // in a Construct]
7224 // Variables with the predetermined data-sharing attributes may not be
7225 // listed in data-sharing attributes clauses, except for the cases
7226 // listed below. For these exceptions only, listing a predetermined
7227 // variable in a data-sharing attribute clause is allowed and overrides
7228 // the variable's predetermined data-sharing attributes.
7229 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7230 // in a Construct, C/C++, p.2]
7231 // Variables with const-qualified type having no mutable member may be
7232 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00007233 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007234 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
7235 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007236 << getOpenMPClauseName(DVar.CKind)
7237 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007238 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007239 continue;
7240 }
7241
Alexey Bataevf29276e2014-06-18 04:14:57 +00007242 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007243 // OpenMP [2.9.3.4, Restrictions, p.2]
7244 // A list item that is private within a parallel region must not appear
7245 // in a firstprivate clause on a worksharing construct if any of the
7246 // worksharing regions arising from the worksharing construct ever bind
7247 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00007248 if (isOpenMPWorksharingDirective(CurrDir) &&
7249 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007250 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007251 if (DVar.CKind != OMPC_shared &&
7252 (isOpenMPParallelDirective(DVar.DKind) ||
7253 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00007254 Diag(ELoc, diag::err_omp_required_access)
7255 << getOpenMPClauseName(OMPC_firstprivate)
7256 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007257 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007258 continue;
7259 }
7260 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007261 // OpenMP [2.9.3.4, Restrictions, p.3]
7262 // A list item that appears in a reduction clause of a parallel construct
7263 // must not appear in a firstprivate clause on a worksharing or task
7264 // construct if any of the worksharing or task regions arising from the
7265 // worksharing or task construct ever bind to any of the parallel regions
7266 // arising from the parallel construct.
7267 // OpenMP [2.9.3.4, Restrictions, p.4]
7268 // A list item that appears in a reduction clause in worksharing
7269 // construct must not appear in a firstprivate clause in a task construct
7270 // encountered during execution of any of the worksharing regions arising
7271 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007272 if (CurrDir == OMPD_task) {
7273 DVar =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007274 DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007275 [](OpenMPDirectiveKind K) -> bool {
7276 return isOpenMPParallelDirective(K) ||
7277 isOpenMPWorksharingDirective(K);
7278 },
7279 false);
7280 if (DVar.CKind == OMPC_reduction &&
7281 (isOpenMPParallelDirective(DVar.DKind) ||
7282 isOpenMPWorksharingDirective(DVar.DKind))) {
7283 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
7284 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007285 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007286 continue;
7287 }
7288 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007289
7290 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7291 // A list item that is private within a teams region must not appear in a
7292 // firstprivate clause on a distribute construct if any of the distribute
7293 // regions arising from the distribute construct ever bind to any of the
7294 // teams regions arising from the teams construct.
7295 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7296 // A list item that appears in a reduction clause of a teams construct
7297 // must not appear in a firstprivate clause on a distribute construct if
7298 // any of the distribute regions arising from the distribute construct
7299 // ever bind to any of the teams regions arising from the teams construct.
7300 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7301 // A list item may appear in a firstprivate or lastprivate clause but not
7302 // both.
7303 if (CurrDir == OMPD_distribute) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007304 DVar = DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_private),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007305 [](OpenMPDirectiveKind K) -> bool {
7306 return isOpenMPTeamsDirective(K);
7307 },
7308 false);
7309 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
7310 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007311 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007312 continue;
7313 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007314 DVar = DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_reduction),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007315 [](OpenMPDirectiveKind K) -> bool {
7316 return isOpenMPTeamsDirective(K);
7317 },
7318 false);
7319 if (DVar.CKind == OMPC_reduction &&
7320 isOpenMPTeamsDirective(DVar.DKind)) {
7321 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007322 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007323 continue;
7324 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007325 DVar = DSAStack->getTopDSA(D, false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007326 if (DVar.CKind == OMPC_lastprivate) {
7327 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007328 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007329 continue;
7330 }
7331 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007332 }
7333
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007334 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007335 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007336 DSAStack->getCurrentDirective() == OMPD_task) {
7337 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7338 << getOpenMPClauseName(OMPC_firstprivate) << Type
7339 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7340 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007341 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007342 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00007343 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007344 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00007345 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007346 continue;
7347 }
7348
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007349 Type = Type.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007350 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7351 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007352 // Generate helper private variable and initialize it with the value of the
7353 // original variable. The address of the original variable is replaced by
7354 // the address of the new private variable in the CodeGen. This new variable
7355 // is not added to IdResolver, so the code in the OpenMP region uses
7356 // original variable for proper diagnostics and variable capturing.
7357 Expr *VDInitRefExpr = nullptr;
7358 // For arrays generate initializer for single element and replace it by the
7359 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007360 if (Type->isArrayType()) {
7361 auto VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007362 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007363 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007364 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007365 ElemType = ElemType.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007366 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007367 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00007368 InitializedEntity Entity =
7369 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007370 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
7371
7372 InitializationSequence InitSeq(*this, Entity, Kind, Init);
7373 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
7374 if (Result.isInvalid())
7375 VDPrivate->setInvalidDecl();
7376 else
7377 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007378 // Remove temp variable declaration.
7379 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007380 } else {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007381 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
7382 ".firstprivate.temp");
7383 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
7384 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00007385 AddInitializerToDecl(VDPrivate,
7386 DefaultLvalueConversion(VDInitRefExpr).get(),
7387 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007388 }
7389 if (VDPrivate->isInvalidDecl()) {
7390 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007391 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007392 diag::note_omp_task_predetermined_firstprivate_here);
7393 }
7394 continue;
7395 }
7396 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007397 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00007398 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
7399 RefExpr->getExprLoc());
7400 DeclRefExpr *Ref = nullptr;
7401 if (!VD)
7402 Ref = buildCapture(*this, D->getIdentifier(), RefExpr);
7403 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
7404 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007405 PrivateCopies.push_back(VDPrivateRefExpr);
7406 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007407 }
7408
Alexey Bataeved09d242014-05-28 05:53:51 +00007409 if (Vars.empty())
7410 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007411
7412 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007413 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007414}
7415
Alexander Musman1bb328c2014-06-04 13:06:39 +00007416OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
7417 SourceLocation StartLoc,
7418 SourceLocation LParenLoc,
7419 SourceLocation EndLoc) {
7420 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00007421 SmallVector<Expr *, 8> SrcExprs;
7422 SmallVector<Expr *, 8> DstExprs;
7423 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007424 for (auto &RefExpr : VarList) {
7425 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
7426 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7427 // It will be analyzed later.
7428 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007429 SrcExprs.push_back(nullptr);
7430 DstExprs.push_back(nullptr);
7431 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007432 continue;
7433 }
7434
7435 SourceLocation ELoc = RefExpr->getExprLoc();
7436 // OpenMP [2.1, C/C++]
7437 // A list item is a variable name.
7438 // OpenMP [2.14.3.5, Restrictions, p.1]
7439 // A variable that is part of another variable (as an array or structure
7440 // element) cannot appear in a lastprivate clause.
7441 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
7442 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007443 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7444 << 0 << RefExpr->getSourceRange();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007445 continue;
7446 }
7447 Decl *D = DE->getDecl();
7448 VarDecl *VD = cast<VarDecl>(D);
7449
7450 QualType Type = VD->getType();
7451 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7452 // It will be analyzed later.
7453 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00007454 SrcExprs.push_back(nullptr);
7455 DstExprs.push_back(nullptr);
7456 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007457 continue;
7458 }
7459
7460 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
7461 // A variable that appears in a lastprivate clause must not have an
7462 // incomplete type or a reference type.
7463 if (RequireCompleteType(ELoc, Type,
7464 diag::err_omp_lastprivate_incomplete_type)) {
7465 continue;
7466 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007467 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007468
7469 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7470 // in a Construct]
7471 // Variables with the predetermined data-sharing attributes may not be
7472 // listed in data-sharing attributes clauses, except for the cases
7473 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007474 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007475 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
7476 DVar.CKind != OMPC_firstprivate &&
7477 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
7478 Diag(ELoc, diag::err_omp_wrong_dsa)
7479 << getOpenMPClauseName(DVar.CKind)
7480 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007481 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007482 continue;
7483 }
7484
Alexey Bataevf29276e2014-06-18 04:14:57 +00007485 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7486 // OpenMP [2.14.3.5, Restrictions, p.2]
7487 // A list item that is private within a parallel region, or that appears in
7488 // the reduction clause of a parallel construct, must not appear in a
7489 // lastprivate clause on a worksharing construct if any of the corresponding
7490 // worksharing regions ever binds to any of the corresponding parallel
7491 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00007492 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00007493 if (isOpenMPWorksharingDirective(CurrDir) &&
7494 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007495 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007496 if (DVar.CKind != OMPC_shared) {
7497 Diag(ELoc, diag::err_omp_required_access)
7498 << getOpenMPClauseName(OMPC_lastprivate)
7499 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007500 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007501 continue;
7502 }
7503 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00007504 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00007505 // A variable of class type (or array thereof) that appears in a
7506 // lastprivate clause requires an accessible, unambiguous default
7507 // constructor for the class type, unless the list item is also specified
7508 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00007509 // A variable of class type (or array thereof) that appears in a
7510 // lastprivate clause requires an accessible, unambiguous copy assignment
7511 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00007512 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00007513 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007514 Type.getUnqualifiedType(), ".lastprivate.src",
7515 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007516 auto *PseudoSrcExpr = buildDeclRefExpr(
7517 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00007518 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007519 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst",
7520 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007521 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007522 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00007523 // For arrays generate assignment operation for single element and replace
7524 // it by the original array element in CodeGen.
7525 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7526 PseudoDstExpr, PseudoSrcExpr);
7527 if (AssignmentOp.isInvalid())
7528 continue;
7529 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7530 /*DiscardedValue=*/true);
7531 if (AssignmentOp.isInvalid())
7532 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007533
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007534 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7535 // A list item may appear in a firstprivate or lastprivate clause but not
7536 // both.
7537 if (CurrDir == OMPD_distribute) {
7538 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
7539 if (DVar.CKind == OMPC_firstprivate) {
7540 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
7541 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7542 continue;
7543 }
7544 }
7545
Alexey Bataev39f915b82015-05-08 10:41:21 +00007546 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataevf29276e2014-06-18 04:14:57 +00007547 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007548 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00007549 SrcExprs.push_back(PseudoSrcExpr);
7550 DstExprs.push_back(PseudoDstExpr);
7551 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00007552 }
7553
7554 if (Vars.empty())
7555 return nullptr;
7556
7557 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00007558 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007559}
7560
Alexey Bataev758e55e2013-09-06 18:03:48 +00007561OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
7562 SourceLocation StartLoc,
7563 SourceLocation LParenLoc,
7564 SourceLocation EndLoc) {
7565 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00007566 for (auto &RefExpr : VarList) {
7567 assert(RefExpr && "NULL expr in OpenMP shared clause.");
7568 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00007569 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007570 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007571 continue;
7572 }
7573
Alexey Bataeved09d242014-05-28 05:53:51 +00007574 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00007575 // OpenMP [2.1, C/C++]
7576 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00007577 // OpenMP [2.14.3.2, Restrictions, p.1]
7578 // A variable that is part of another variable (as an array or structure
7579 // element) cannot appear in a shared unless it is a static data member
7580 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00007581 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007582 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007583 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7584 << 0 << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00007585 continue;
7586 }
7587 Decl *D = DE->getDecl();
7588 VarDecl *VD = cast<VarDecl>(D);
7589
7590 QualType Type = VD->getType();
7591 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7592 // It will be analyzed later.
7593 Vars.push_back(DE);
7594 continue;
7595 }
7596
7597 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7598 // in a Construct]
7599 // Variables with the predetermined data-sharing attributes may not be
7600 // listed in data-sharing attributes clauses, except for the cases
7601 // listed below. For these exceptions only, listing a predetermined
7602 // variable in a data-sharing attribute clause is allowed and overrides
7603 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007604 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00007605 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
7606 DVar.RefExpr) {
7607 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7608 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007609 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007610 continue;
7611 }
7612
7613 DSAStack->addDSA(VD, DE, OMPC_shared);
7614 Vars.push_back(DE);
7615 }
7616
Alexey Bataeved09d242014-05-28 05:53:51 +00007617 if (Vars.empty())
7618 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007619
7620 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
7621}
7622
Alexey Bataevc5e02582014-06-16 07:08:35 +00007623namespace {
7624class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
7625 DSAStackTy *Stack;
7626
7627public:
7628 bool VisitDeclRefExpr(DeclRefExpr *E) {
7629 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007630 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007631 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
7632 return false;
7633 if (DVar.CKind != OMPC_unknown)
7634 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007635 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007636 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007637 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007638 return true;
7639 return false;
7640 }
7641 return false;
7642 }
7643 bool VisitStmt(Stmt *S) {
7644 for (auto Child : S->children()) {
7645 if (Child && Visit(Child))
7646 return true;
7647 }
7648 return false;
7649 }
Alexey Bataev23b69422014-06-18 07:08:49 +00007650 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00007651};
Alexey Bataev23b69422014-06-18 07:08:49 +00007652} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00007653
7654OMPClause *Sema::ActOnOpenMPReductionClause(
7655 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
7656 SourceLocation ColonLoc, SourceLocation EndLoc,
7657 CXXScopeSpec &ReductionIdScopeSpec,
7658 const DeclarationNameInfo &ReductionId) {
7659 // TODO: Allow scope specification search when 'declare reduction' is
7660 // supported.
7661 assert(ReductionIdScopeSpec.isEmpty() &&
7662 "No support for scoped reduction identifiers yet.");
7663
7664 auto DN = ReductionId.getName();
7665 auto OOK = DN.getCXXOverloadedOperator();
7666 BinaryOperatorKind BOK = BO_Comma;
7667
7668 // OpenMP [2.14.3.6, reduction clause]
7669 // C
7670 // reduction-identifier is either an identifier or one of the following
7671 // operators: +, -, *, &, |, ^, && and ||
7672 // C++
7673 // reduction-identifier is either an id-expression or one of the following
7674 // operators: +, -, *, &, |, ^, && and ||
7675 // FIXME: Only 'min' and 'max' identifiers are supported for now.
7676 switch (OOK) {
7677 case OO_Plus:
7678 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007679 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007680 break;
7681 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007682 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007683 break;
7684 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007685 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007686 break;
7687 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007688 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007689 break;
7690 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007691 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007692 break;
7693 case OO_AmpAmp:
7694 BOK = BO_LAnd;
7695 break;
7696 case OO_PipePipe:
7697 BOK = BO_LOr;
7698 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007699 case OO_New:
7700 case OO_Delete:
7701 case OO_Array_New:
7702 case OO_Array_Delete:
7703 case OO_Slash:
7704 case OO_Percent:
7705 case OO_Tilde:
7706 case OO_Exclaim:
7707 case OO_Equal:
7708 case OO_Less:
7709 case OO_Greater:
7710 case OO_LessEqual:
7711 case OO_GreaterEqual:
7712 case OO_PlusEqual:
7713 case OO_MinusEqual:
7714 case OO_StarEqual:
7715 case OO_SlashEqual:
7716 case OO_PercentEqual:
7717 case OO_CaretEqual:
7718 case OO_AmpEqual:
7719 case OO_PipeEqual:
7720 case OO_LessLess:
7721 case OO_GreaterGreater:
7722 case OO_LessLessEqual:
7723 case OO_GreaterGreaterEqual:
7724 case OO_EqualEqual:
7725 case OO_ExclaimEqual:
7726 case OO_PlusPlus:
7727 case OO_MinusMinus:
7728 case OO_Comma:
7729 case OO_ArrowStar:
7730 case OO_Arrow:
7731 case OO_Call:
7732 case OO_Subscript:
7733 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00007734 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007735 case NUM_OVERLOADED_OPERATORS:
7736 llvm_unreachable("Unexpected reduction identifier");
7737 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007738 if (auto II = DN.getAsIdentifierInfo()) {
7739 if (II->isStr("max"))
7740 BOK = BO_GT;
7741 else if (II->isStr("min"))
7742 BOK = BO_LT;
7743 }
7744 break;
7745 }
7746 SourceRange ReductionIdRange;
7747 if (ReductionIdScopeSpec.isValid()) {
7748 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
7749 }
7750 ReductionIdRange.setEnd(ReductionId.getEndLoc());
7751 if (BOK == BO_Comma) {
7752 // Not allowed reduction identifier is found.
7753 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
7754 << ReductionIdRange;
7755 return nullptr;
7756 }
7757
7758 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007759 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007760 SmallVector<Expr *, 8> LHSs;
7761 SmallVector<Expr *, 8> RHSs;
7762 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007763 for (auto RefExpr : VarList) {
7764 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
7765 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7766 // It will be analyzed later.
7767 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007768 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007769 LHSs.push_back(nullptr);
7770 RHSs.push_back(nullptr);
7771 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007772 continue;
7773 }
7774
7775 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7776 RefExpr->isInstantiationDependent() ||
7777 RefExpr->containsUnexpandedParameterPack()) {
7778 // It will be analyzed later.
7779 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007780 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007781 LHSs.push_back(nullptr);
7782 RHSs.push_back(nullptr);
7783 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007784 continue;
7785 }
7786
7787 auto ELoc = RefExpr->getExprLoc();
7788 auto ERange = RefExpr->getSourceRange();
7789 // OpenMP [2.1, C/C++]
7790 // A list item is a variable or array section, subject to the restrictions
7791 // specified in Section 2.4 on page 42 and in each of the sections
7792 // describing clauses and directives for which a list appears.
7793 // OpenMP [2.14.3.3, Restrictions, p.1]
7794 // A variable that is part of another variable (as an array or
7795 // structure element) cannot appear in a private clause.
Alexey Bataeva1764212015-09-30 09:22:36 +00007796 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
7797 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr);
7798 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr);
7799 if (!ASE && !OASE && (!DE || !isa<VarDecl>(DE->getDecl()))) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007800 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
7801 << 0 << ERange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007802 continue;
7803 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007804 QualType Type;
7805 VarDecl *VD = nullptr;
7806 if (DE) {
7807 auto D = DE->getDecl();
7808 VD = cast<VarDecl>(D);
Alexey Bataev31300ed2016-02-04 11:27:03 +00007809 Type = Context.getBaseElementType(VD->getType().getNonReferenceType());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007810 } else if (ASE) {
Alexey Bataev31300ed2016-02-04 11:27:03 +00007811 Type = ASE->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007812 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7813 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7814 Base = TempASE->getBase()->IgnoreParenImpCasts();
7815 DE = dyn_cast<DeclRefExpr>(Base);
7816 if (DE)
7817 VD = dyn_cast<VarDecl>(DE->getDecl());
7818 if (!VD) {
7819 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7820 << 0 << Base->getSourceRange();
7821 continue;
7822 }
7823 } else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007824 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
7825 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
7826 Type = ATy->getElementType();
7827 else
7828 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +00007829 Type = Type.getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007830 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7831 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7832 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7833 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7834 Base = TempASE->getBase()->IgnoreParenImpCasts();
7835 DE = dyn_cast<DeclRefExpr>(Base);
7836 if (DE)
7837 VD = dyn_cast<VarDecl>(DE->getDecl());
7838 if (!VD) {
7839 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7840 << 1 << Base->getSourceRange();
7841 continue;
7842 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007843 }
7844
Alexey Bataevc5e02582014-06-16 07:08:35 +00007845 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7846 // A variable that appears in a private clause must not have an incomplete
7847 // type or a reference type.
7848 if (RequireCompleteType(ELoc, Type,
7849 diag::err_omp_reduction_incomplete_type))
7850 continue;
7851 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00007852 // A list item that appears in a reduction clause must not be
7853 // const-qualified.
7854 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007855 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007856 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007857 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007858 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7859 VarDecl::DeclarationOnly;
7860 Diag(VD->getLocation(),
7861 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7862 << VD;
7863 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007864 continue;
7865 }
7866 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
7867 // If a list-item is a reference type then it must bind to the same object
7868 // for all threads of the team.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007869 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007870 VarDecl *VDDef = VD->getDefinition();
Alexey Bataev31300ed2016-02-04 11:27:03 +00007871 if (VD->getType()->isReferenceType() && VDDef) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007872 DSARefChecker Check(DSAStack);
7873 if (Check.Visit(VDDef->getInit())) {
7874 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
7875 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
7876 continue;
7877 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007878 }
7879 }
7880 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7881 // The type of a list item that appears in a reduction clause must be valid
7882 // for the reduction-identifier. For a max or min reduction in C, the type
7883 // of the list item must be an allowed arithmetic data type: char, int,
7884 // float, double, or _Bool, possibly modified with long, short, signed, or
7885 // unsigned. For a max or min reduction in C++, the type of the list item
7886 // must be an allowed arithmetic data type: char, wchar_t, int, float,
7887 // double, or bool, possibly modified with long, short, signed, or unsigned.
7888 if ((BOK == BO_GT || BOK == BO_LT) &&
7889 !(Type->isScalarType() ||
7890 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
7891 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
7892 << getLangOpts().CPlusPlus;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007893 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007894 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7895 VarDecl::DeclarationOnly;
7896 Diag(VD->getLocation(),
7897 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7898 << VD;
7899 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007900 continue;
7901 }
7902 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
7903 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
7904 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007905 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007906 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7907 VarDecl::DeclarationOnly;
7908 Diag(VD->getLocation(),
7909 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7910 << VD;
7911 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007912 continue;
7913 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007914 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7915 // in a Construct]
7916 // Variables with the predetermined data-sharing attributes may not be
7917 // listed in data-sharing attributes clauses, except for the cases
7918 // listed below. For these exceptions only, listing a predetermined
7919 // variable in a data-sharing attribute clause is allowed and overrides
7920 // the variable's predetermined data-sharing attributes.
7921 // OpenMP [2.14.3.6, Restrictions, p.3]
7922 // Any number of reduction clauses can be specified on the directive,
7923 // but a list item can appear only once in the reduction clauses for that
7924 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00007925 DSAStackTy::DSAVarData DVar;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007926 DVar = DSAStack->getTopDSA(VD, false);
7927 if (DVar.CKind == OMPC_reduction) {
7928 Diag(ELoc, diag::err_omp_once_referenced)
7929 << getOpenMPClauseName(OMPC_reduction);
7930 if (DVar.RefExpr) {
7931 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007932 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007933 } else if (DVar.CKind != OMPC_unknown) {
7934 Diag(ELoc, diag::err_omp_wrong_dsa)
7935 << getOpenMPClauseName(DVar.CKind)
7936 << getOpenMPClauseName(OMPC_reduction);
7937 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7938 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007939 }
7940
7941 // OpenMP [2.14.3.6, Restrictions, p.1]
7942 // A list item that appears in a reduction clause of a worksharing
7943 // construct must be shared in the parallel regions to which any of the
7944 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007945 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7946 if (isOpenMPWorksharingDirective(CurrDir) &&
7947 !isOpenMPParallelDirective(CurrDir)) {
7948 DVar = DSAStack->getImplicitDSA(VD, true);
7949 if (DVar.CKind != OMPC_shared) {
7950 Diag(ELoc, diag::err_omp_required_access)
7951 << getOpenMPClauseName(OMPC_reduction)
7952 << getOpenMPClauseName(OMPC_shared);
7953 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7954 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007955 }
7956 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007957
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007958 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007959 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
7960 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7961 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName(),
7962 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7963 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00007964 if (OASE ||
7965 (DE && VD->getType().getNonReferenceType()->isVariablyModifiedType())) {
7966 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007967 // Create pseudo array type for private copy. The size for this array will
7968 // be generated during codegen.
7969 // For array subscripts or single variables Private Ty is the same as Type
7970 // (type of the variable or single array element).
7971 PrivateTy = Context.getVariableArrayType(
7972 Type, new (Context) OpaqueValueExpr(SourceLocation(),
7973 Context.getSizeType(), VK_RValue),
7974 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev1189bd02016-01-26 12:20:39 +00007975 } else if (DE &&
7976 Context.getAsArrayType(VD->getType().getNonReferenceType()))
7977 PrivateTy = VD->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007978 // Private copy.
7979 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, VD->getName(),
7980 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007981 // Add initializer for private variable.
7982 Expr *Init = nullptr;
7983 switch (BOK) {
7984 case BO_Add:
7985 case BO_Xor:
7986 case BO_Or:
7987 case BO_LOr:
7988 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
7989 if (Type->isScalarType() || Type->isAnyComplexType()) {
7990 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007991 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007992 break;
7993 case BO_Mul:
7994 case BO_LAnd:
7995 if (Type->isScalarType() || Type->isAnyComplexType()) {
7996 // '*' and '&&' reduction ops - initializer is '1'.
7997 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
7998 }
7999 break;
8000 case BO_And: {
8001 // '&' reduction op - initializer is '~0'.
8002 QualType OrigType = Type;
8003 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
8004 Type = ComplexTy->getElementType();
8005 }
8006 if (Type->isRealFloatingType()) {
8007 llvm::APFloat InitValue =
8008 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
8009 /*isIEEE=*/true);
8010 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8011 Type, ELoc);
8012 } else if (Type->isScalarType()) {
8013 auto Size = Context.getTypeSize(Type);
8014 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
8015 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
8016 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8017 }
8018 if (Init && OrigType->isAnyComplexType()) {
8019 // Init = 0xFFFF + 0xFFFFi;
8020 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
8021 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
8022 }
8023 Type = OrigType;
8024 break;
8025 }
8026 case BO_LT:
8027 case BO_GT: {
8028 // 'min' reduction op - initializer is 'Largest representable number in
8029 // the reduction list item type'.
8030 // 'max' reduction op - initializer is 'Least representable number in
8031 // the reduction list item type'.
8032 if (Type->isIntegerType() || Type->isPointerType()) {
8033 bool IsSigned = Type->hasSignedIntegerRepresentation();
8034 auto Size = Context.getTypeSize(Type);
8035 QualType IntTy =
8036 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
8037 llvm::APInt InitValue =
8038 (BOK != BO_LT)
8039 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
8040 : llvm::APInt::getMinValue(Size)
8041 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
8042 : llvm::APInt::getMaxValue(Size);
8043 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8044 if (Type->isPointerType()) {
8045 // Cast to pointer type.
8046 auto CastExpr = BuildCStyleCastExpr(
8047 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
8048 SourceLocation(), Init);
8049 if (CastExpr.isInvalid())
8050 continue;
8051 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00008052 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008053 } else if (Type->isRealFloatingType()) {
8054 llvm::APFloat InitValue = llvm::APFloat::getLargest(
8055 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
8056 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8057 Type, ELoc);
8058 }
8059 break;
8060 }
8061 case BO_PtrMemD:
8062 case BO_PtrMemI:
8063 case BO_MulAssign:
8064 case BO_Div:
8065 case BO_Rem:
8066 case BO_Sub:
8067 case BO_Shl:
8068 case BO_Shr:
8069 case BO_LE:
8070 case BO_GE:
8071 case BO_EQ:
8072 case BO_NE:
8073 case BO_AndAssign:
8074 case BO_XorAssign:
8075 case BO_OrAssign:
8076 case BO_Assign:
8077 case BO_AddAssign:
8078 case BO_SubAssign:
8079 case BO_DivAssign:
8080 case BO_RemAssign:
8081 case BO_ShlAssign:
8082 case BO_ShrAssign:
8083 case BO_Comma:
8084 llvm_unreachable("Unexpected reduction operation");
8085 }
8086 if (Init) {
8087 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
8088 /*TypeMayContainAuto=*/false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008089 } else
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008090 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008091 if (!RHSVD->hasInit()) {
8092 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
8093 << ReductionIdRange;
Alexey Bataeva1764212015-09-30 09:22:36 +00008094 if (VD) {
8095 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
8096 VarDecl::DeclarationOnly;
8097 Diag(VD->getLocation(),
8098 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8099 << VD;
8100 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008101 continue;
8102 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008103 // Store initializer for single element in private copy. Will be used during
8104 // codegen.
8105 PrivateVD->setInit(RHSVD->getInit());
8106 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataev39f915b82015-05-08 10:41:21 +00008107 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
8108 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008109 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008110 ExprResult ReductionOp =
8111 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
8112 LHSDRE, RHSDRE);
8113 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00008114 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008115 ReductionOp =
8116 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8117 BO_Assign, LHSDRE, ReductionOp.get());
8118 } else {
8119 auto *ConditionalOp = new (Context) ConditionalOperator(
8120 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
8121 RHSDRE, Type, VK_LValue, OK_Ordinary);
8122 ReductionOp =
8123 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8124 BO_Assign, LHSDRE, ConditionalOp);
8125 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008126 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008127 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008128 if (ReductionOp.isInvalid())
8129 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008130
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008131 DSAStack->addDSA(VD, DE, OMPC_reduction);
Alexey Bataeva1764212015-09-30 09:22:36 +00008132 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008133 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008134 LHSs.push_back(LHSDRE);
8135 RHSs.push_back(RHSDRE);
8136 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008137 }
8138
8139 if (Vars.empty())
8140 return nullptr;
8141
8142 return OMPReductionClause::Create(
8143 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008144 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
8145 LHSs, RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008146}
8147
Alexey Bataev182227b2015-08-20 10:54:39 +00008148OMPClause *Sema::ActOnOpenMPLinearClause(
8149 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
8150 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
8151 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008152 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008153 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00008154 SmallVector<Expr *, 8> Inits;
Alexey Bataev182227b2015-08-20 10:54:39 +00008155 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
8156 LinKind == OMPC_LINEAR_unknown) {
8157 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
8158 LinKind = OMPC_LINEAR_val;
8159 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008160 for (auto &RefExpr : VarList) {
8161 assert(RefExpr && "NULL expr in OpenMP linear clause.");
8162 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008163 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008164 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008165 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00008166 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008167 continue;
8168 }
8169
8170 // OpenMP [2.14.3.7, linear clause]
8171 // A list item that appears in a linear clause is subject to the private
8172 // clause semantics described in Section 2.14.3.3 on page 159 except as
8173 // noted. In addition, the value of the new list item on each iteration
8174 // of the associated loop(s) corresponds to the value of the original
8175 // list item before entering the construct plus the logical number of
8176 // the iteration times linear-step.
8177
Alexey Bataeved09d242014-05-28 05:53:51 +00008178 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00008179 // OpenMP [2.1, C/C++]
8180 // A list item is a variable name.
8181 // OpenMP [2.14.3.3, Restrictions, p.1]
8182 // A variable that is part of another variable (as an array or
8183 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00008184 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008185 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008186 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8187 << 0 << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00008188 continue;
8189 }
8190
8191 VarDecl *VD = cast<VarDecl>(DE->getDecl());
8192
8193 // OpenMP [2.14.3.7, linear clause]
8194 // A list-item cannot appear in more than one linear clause.
8195 // A list-item that appears in a linear clause cannot appear in any
8196 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008197 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00008198 if (DVar.RefExpr) {
8199 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8200 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00008201 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00008202 continue;
8203 }
8204
8205 QualType QType = VD->getType();
8206 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
8207 // It will be analyzed later.
8208 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008209 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00008210 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008211 continue;
8212 }
8213
8214 // A variable must not have an incomplete type or a reference type.
8215 if (RequireCompleteType(ELoc, QType,
8216 diag::err_omp_linear_incomplete_type)) {
8217 continue;
8218 }
Alexey Bataev1185e192015-08-20 12:15:57 +00008219 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
8220 !QType->isReferenceType()) {
8221 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
8222 << QType << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
8223 continue;
8224 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008225 QType = QType.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00008226
8227 // A list item must not be const-qualified.
8228 if (QType.isConstant(Context)) {
8229 Diag(ELoc, diag::err_omp_const_variable)
8230 << getOpenMPClauseName(OMPC_linear);
8231 bool IsDecl =
8232 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8233 Diag(VD->getLocation(),
8234 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8235 << VD;
8236 continue;
8237 }
8238
8239 // A list item must be of integral or pointer type.
8240 QType = QType.getUnqualifiedType().getCanonicalType();
8241 const Type *Ty = QType.getTypePtrOrNull();
8242 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
8243 !Ty->isPointerType())) {
8244 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
8245 bool IsDecl =
8246 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8247 Diag(VD->getLocation(),
8248 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8249 << VD;
8250 continue;
8251 }
8252
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008253 // Build private copy of original var.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008254 auto *Private = buildVarDecl(*this, ELoc, QType, VD->getName(),
8255 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008256 auto *PrivateRef = buildDeclRefExpr(
8257 *this, Private, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman3276a272015-03-21 10:12:56 +00008258 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008259 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008260 Expr *InitExpr;
8261 if (LinKind == OMPC_LINEAR_uval)
8262 InitExpr = VD->getInit();
8263 else
8264 InitExpr = DE;
8265 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexander Musman3276a272015-03-21 10:12:56 +00008266 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008267 auto InitRef = buildDeclRefExpr(
8268 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00008269 DSAStack->addDSA(VD, DE, OMPC_linear);
8270 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008271 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00008272 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00008273 }
8274
8275 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008276 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008277
8278 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00008279 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008280 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
8281 !Step->isInstantiationDependent() &&
8282 !Step->containsUnexpandedParameterPack()) {
8283 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00008284 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00008285 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008286 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00008287 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00008288
Alexander Musman3276a272015-03-21 10:12:56 +00008289 // Build var to save the step value.
8290 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008291 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00008292 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008293 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00008294 ExprResult CalcStep =
8295 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008296 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00008297
Alexander Musman8dba6642014-04-22 13:09:42 +00008298 // Warn about zero linear step (it would be probably better specified as
8299 // making corresponding variables 'const').
8300 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00008301 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
8302 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00008303 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
8304 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00008305 if (!IsConstant && CalcStep.isUsable()) {
8306 // Calculate the step beforehand instead of doing this on each iteration.
8307 // (This is not used if the number of iterations may be kfold-ed).
8308 CalcStepExpr = CalcStep.get();
8309 }
Alexander Musman8dba6642014-04-22 13:09:42 +00008310 }
8311
Alexey Bataev182227b2015-08-20 10:54:39 +00008312 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
8313 ColonLoc, EndLoc, Vars, Privates, Inits,
8314 StepExpr, CalcStepExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00008315}
8316
8317static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
8318 Expr *NumIterations, Sema &SemaRef,
8319 Scope *S) {
8320 // Walk the vars and build update/final expressions for the CodeGen.
8321 SmallVector<Expr *, 8> Updates;
8322 SmallVector<Expr *, 8> Finals;
8323 Expr *Step = Clause.getStep();
8324 Expr *CalcStep = Clause.getCalcStep();
8325 // OpenMP [2.14.3.7, linear clause]
8326 // If linear-step is not specified it is assumed to be 1.
8327 if (Step == nullptr)
8328 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
8329 else if (CalcStep)
8330 Step = cast<BinaryOperator>(CalcStep)->getLHS();
8331 bool HasErrors = false;
8332 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008333 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008334 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00008335 for (auto &RefExpr : Clause.varlists()) {
8336 Expr *InitExpr = *CurInit;
8337
8338 // Build privatized reference to the current linear var.
8339 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008340 Expr *CapturedRef;
8341 if (LinKind == OMPC_LINEAR_uval)
8342 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
8343 else
8344 CapturedRef =
8345 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
8346 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
8347 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008348
8349 // Build update: Var = InitExpr + IV * Step
8350 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008351 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00008352 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008353 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
8354 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008355
8356 // Build final: Var = InitExpr + NumIterations * Step
8357 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008358 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00008359 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008360 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
8361 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008362 if (!Update.isUsable() || !Final.isUsable()) {
8363 Updates.push_back(nullptr);
8364 Finals.push_back(nullptr);
8365 HasErrors = true;
8366 } else {
8367 Updates.push_back(Update.get());
8368 Finals.push_back(Final.get());
8369 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008370 ++CurInit, ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00008371 }
8372 Clause.setUpdates(Updates);
8373 Clause.setFinals(Finals);
8374 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00008375}
8376
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008377OMPClause *Sema::ActOnOpenMPAlignedClause(
8378 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
8379 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
8380
8381 SmallVector<Expr *, 8> Vars;
8382 for (auto &RefExpr : VarList) {
8383 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
8384 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
8385 // It will be analyzed later.
8386 Vars.push_back(RefExpr);
8387 continue;
8388 }
8389
8390 SourceLocation ELoc = RefExpr->getExprLoc();
8391 // OpenMP [2.1, C/C++]
8392 // A list item is a variable name.
8393 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
8394 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008395 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8396 << 0 << RefExpr->getSourceRange();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008397 continue;
8398 }
8399
8400 VarDecl *VD = cast<VarDecl>(DE->getDecl());
8401
8402 // OpenMP [2.8.1, simd construct, Restrictions]
8403 // The type of list items appearing in the aligned clause must be
8404 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008405 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008406 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008407 const Type *Ty = QType.getTypePtrOrNull();
8408 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
8409 !Ty->isPointerType())) {
8410 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
8411 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
8412 bool IsDecl =
8413 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8414 Diag(VD->getLocation(),
8415 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8416 << VD;
8417 continue;
8418 }
8419
8420 // OpenMP [2.8.1, simd construct, Restrictions]
8421 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008422 if (Expr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008423 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
8424 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
8425 << getOpenMPClauseName(OMPC_aligned);
8426 continue;
8427 }
8428
8429 Vars.push_back(DE);
8430 }
8431
8432 // OpenMP [2.8.1, simd construct, Description]
8433 // The parameter of the aligned clause, alignment, must be a constant
8434 // positive integer expression.
8435 // If no optional parameter is specified, implementation-defined default
8436 // alignments for SIMD instructions on the target platforms are assumed.
8437 if (Alignment != nullptr) {
8438 ExprResult AlignResult =
8439 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
8440 if (AlignResult.isInvalid())
8441 return nullptr;
8442 Alignment = AlignResult.get();
8443 }
8444 if (Vars.empty())
8445 return nullptr;
8446
8447 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
8448 EndLoc, Vars, Alignment);
8449}
8450
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008451OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
8452 SourceLocation StartLoc,
8453 SourceLocation LParenLoc,
8454 SourceLocation EndLoc) {
8455 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008456 SmallVector<Expr *, 8> SrcExprs;
8457 SmallVector<Expr *, 8> DstExprs;
8458 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00008459 for (auto &RefExpr : VarList) {
8460 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
8461 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008462 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008463 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008464 SrcExprs.push_back(nullptr);
8465 DstExprs.push_back(nullptr);
8466 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008467 continue;
8468 }
8469
Alexey Bataeved09d242014-05-28 05:53:51 +00008470 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008471 // OpenMP [2.1, C/C++]
8472 // A list item is a variable name.
8473 // OpenMP [2.14.4.1, Restrictions, p.1]
8474 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00008475 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008476 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008477 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8478 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008479 continue;
8480 }
8481
8482 Decl *D = DE->getDecl();
8483 VarDecl *VD = cast<VarDecl>(D);
8484
8485 QualType Type = VD->getType();
8486 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8487 // It will be analyzed later.
8488 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008489 SrcExprs.push_back(nullptr);
8490 DstExprs.push_back(nullptr);
8491 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008492 continue;
8493 }
8494
8495 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
8496 // A list item that appears in a copyin clause must be threadprivate.
8497 if (!DSAStack->isThreadPrivate(VD)) {
8498 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00008499 << getOpenMPClauseName(OMPC_copyin)
8500 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008501 continue;
8502 }
8503
8504 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8505 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00008506 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008507 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008508 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008509 auto *SrcVD =
8510 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
8511 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008512 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008513 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
8514 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008515 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
8516 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008517 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008518 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008519 // For arrays generate assignment operation for single element and replace
8520 // it by the original array element in CodeGen.
8521 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8522 PseudoDstExpr, PseudoSrcExpr);
8523 if (AssignmentOp.isInvalid())
8524 continue;
8525 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8526 /*DiscardedValue=*/true);
8527 if (AssignmentOp.isInvalid())
8528 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008529
8530 DSAStack->addDSA(VD, DE, OMPC_copyin);
8531 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008532 SrcExprs.push_back(PseudoSrcExpr);
8533 DstExprs.push_back(PseudoDstExpr);
8534 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008535 }
8536
Alexey Bataeved09d242014-05-28 05:53:51 +00008537 if (Vars.empty())
8538 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008539
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008540 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8541 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008542}
8543
Alexey Bataevbae9a792014-06-27 10:37:06 +00008544OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
8545 SourceLocation StartLoc,
8546 SourceLocation LParenLoc,
8547 SourceLocation EndLoc) {
8548 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00008549 SmallVector<Expr *, 8> SrcExprs;
8550 SmallVector<Expr *, 8> DstExprs;
8551 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008552 for (auto &RefExpr : VarList) {
8553 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
8554 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
8555 // It will be analyzed later.
8556 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008557 SrcExprs.push_back(nullptr);
8558 DstExprs.push_back(nullptr);
8559 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008560 continue;
8561 }
8562
8563 SourceLocation ELoc = RefExpr->getExprLoc();
8564 // OpenMP [2.1, C/C++]
8565 // A list item is a variable name.
8566 // OpenMP [2.14.4.1, Restrictions, p.1]
8567 // A list item that appears in a copyin clause must be threadprivate.
8568 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
8569 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008570 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8571 << 0 << RefExpr->getSourceRange();
Alexey Bataevbae9a792014-06-27 10:37:06 +00008572 continue;
8573 }
8574
8575 Decl *D = DE->getDecl();
8576 VarDecl *VD = cast<VarDecl>(D);
8577
8578 QualType Type = VD->getType();
8579 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8580 // It will be analyzed later.
8581 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008582 SrcExprs.push_back(nullptr);
8583 DstExprs.push_back(nullptr);
8584 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008585 continue;
8586 }
8587
8588 // OpenMP [2.14.4.2, Restrictions, p.2]
8589 // A list item that appears in a copyprivate clause may not appear in a
8590 // private or firstprivate clause on the single construct.
8591 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008592 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008593 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
8594 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00008595 Diag(ELoc, diag::err_omp_wrong_dsa)
8596 << getOpenMPClauseName(DVar.CKind)
8597 << getOpenMPClauseName(OMPC_copyprivate);
8598 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8599 continue;
8600 }
8601
8602 // OpenMP [2.11.4.2, Restrictions, p.1]
8603 // All list items that appear in a copyprivate clause must be either
8604 // threadprivate or private in the enclosing context.
8605 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008606 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008607 if (DVar.CKind == OMPC_shared) {
8608 Diag(ELoc, diag::err_omp_required_access)
8609 << getOpenMPClauseName(OMPC_copyprivate)
8610 << "threadprivate or private in the enclosing context";
8611 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8612 continue;
8613 }
8614 }
8615 }
8616
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008617 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008618 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008619 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008620 << getOpenMPClauseName(OMPC_copyprivate) << Type
8621 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008622 bool IsDecl =
8623 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8624 Diag(VD->getLocation(),
8625 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8626 << VD;
8627 continue;
8628 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008629
Alexey Bataevbae9a792014-06-27 10:37:06 +00008630 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8631 // A variable of class type (or array thereof) that appears in a
8632 // copyin clause requires an accessible, unambiguous copy assignment
8633 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008634 Type = Context.getBaseElementType(Type.getNonReferenceType())
8635 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00008636 auto *SrcVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008637 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src",
8638 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008639 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008640 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00008641 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008642 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst",
8643 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008644 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008645 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00008646 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8647 PseudoDstExpr, PseudoSrcExpr);
8648 if (AssignmentOp.isInvalid())
8649 continue;
8650 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8651 /*DiscardedValue=*/true);
8652 if (AssignmentOp.isInvalid())
8653 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008654
8655 // No need to mark vars as copyprivate, they are already threadprivate or
8656 // implicitly private.
8657 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008658 SrcExprs.push_back(PseudoSrcExpr);
8659 DstExprs.push_back(PseudoDstExpr);
8660 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00008661 }
8662
8663 if (Vars.empty())
8664 return nullptr;
8665
Alexey Bataeva63048e2015-03-23 06:18:07 +00008666 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
8667 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008668}
8669
Alexey Bataev6125da92014-07-21 11:26:11 +00008670OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
8671 SourceLocation StartLoc,
8672 SourceLocation LParenLoc,
8673 SourceLocation EndLoc) {
8674 if (VarList.empty())
8675 return nullptr;
8676
8677 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
8678}
Alexey Bataevdea47612014-07-23 07:46:59 +00008679
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008680OMPClause *
8681Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
8682 SourceLocation DepLoc, SourceLocation ColonLoc,
8683 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
8684 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008685 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008686 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008687 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008688 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +00008689 return nullptr;
8690 }
8691 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008692 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
8693 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00008694 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008695 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008696 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
8697 /*Last=*/OMPC_DEPEND_unknown, Except)
8698 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008699 return nullptr;
8700 }
8701 SmallVector<Expr *, 8> Vars;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008702 llvm::APSInt DepCounter(/*BitWidth=*/32);
8703 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
8704 if (DepKind == OMPC_DEPEND_sink) {
8705 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
8706 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
8707 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008708 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008709 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008710 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
8711 DSAStack->getParentOrderedRegionParam()) {
8712 for (auto &RefExpr : VarList) {
8713 assert(RefExpr && "NULL expr in OpenMP shared clause.");
8714 if (isa<DependentScopeDeclRefExpr>(RefExpr) ||
8715 (DepKind == OMPC_DEPEND_sink && CurContext->isDependentContext())) {
8716 // It will be analyzed later.
8717 Vars.push_back(RefExpr);
8718 continue;
8719 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008720
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008721 SourceLocation ELoc = RefExpr->getExprLoc();
8722 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
8723 if (DepKind == OMPC_DEPEND_sink) {
8724 if (DepCounter >= TotalDepCount) {
8725 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
8726 continue;
8727 }
8728 ++DepCounter;
8729 // OpenMP [2.13.9, Summary]
8730 // depend(dependence-type : vec), where dependence-type is:
8731 // 'sink' and where vec is the iteration vector, which has the form:
8732 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
8733 // where n is the value specified by the ordered clause in the loop
8734 // directive, xi denotes the loop iteration variable of the i-th nested
8735 // loop associated with the loop directive, and di is a constant
8736 // non-negative integer.
8737 SimpleExpr = SimpleExpr->IgnoreImplicit();
8738 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8739 if (!DE) {
8740 OverloadedOperatorKind OOK = OO_None;
8741 SourceLocation OOLoc;
8742 Expr *LHS, *RHS;
8743 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
8744 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
8745 OOLoc = BO->getOperatorLoc();
8746 LHS = BO->getLHS()->IgnoreParenImpCasts();
8747 RHS = BO->getRHS()->IgnoreParenImpCasts();
8748 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
8749 OOK = OCE->getOperator();
8750 OOLoc = OCE->getOperatorLoc();
8751 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8752 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
8753 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
8754 OOK = MCE->getMethodDecl()
8755 ->getNameInfo()
8756 .getName()
8757 .getCXXOverloadedOperator();
8758 OOLoc = MCE->getCallee()->getExprLoc();
8759 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
8760 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8761 } else {
8762 Diag(ELoc, diag::err_omp_depend_sink_wrong_expr);
8763 continue;
8764 }
8765 DE = dyn_cast<DeclRefExpr>(LHS);
8766 if (!DE) {
8767 Diag(LHS->getExprLoc(),
8768 diag::err_omp_depend_sink_expected_loop_iteration)
8769 << DSAStack->getParentLoopControlVariable(
8770 DepCounter.getZExtValue());
8771 continue;
8772 }
8773 if (OOK != OO_Plus && OOK != OO_Minus) {
8774 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
8775 continue;
8776 }
8777 ExprResult Res = VerifyPositiveIntegerConstantInClause(
8778 RHS, OMPC_depend, /*StrictlyPositive=*/false);
8779 if (Res.isInvalid())
8780 continue;
8781 }
8782 auto *VD = dyn_cast<VarDecl>(DE->getDecl());
8783 if (!CurContext->isDependentContext() &&
8784 DSAStack->getParentOrderedRegionParam() &&
8785 (!VD || DepCounter != DSAStack->isParentLoopControlVariable(VD))) {
8786 Diag(DE->getExprLoc(),
8787 diag::err_omp_depend_sink_expected_loop_iteration)
8788 << DSAStack->getParentLoopControlVariable(
8789 DepCounter.getZExtValue());
8790 continue;
8791 }
8792 } else {
8793 // OpenMP [2.11.1.1, Restrictions, p.3]
8794 // A variable that is part of another variable (such as a field of a
8795 // structure) but is not an array element or an array section cannot
8796 // appear in a depend clause.
8797 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8798 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
8799 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
8800 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
8801 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev31300ed2016-02-04 11:27:03 +00008802 (ASE &&
8803 !ASE->getBase()
8804 ->getType()
8805 .getNonReferenceType()
8806 ->isPointerType() &&
8807 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008808 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
8809 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008810 continue;
8811 }
8812 }
8813
8814 Vars.push_back(RefExpr->IgnoreParenImpCasts());
8815 }
8816
8817 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
8818 TotalDepCount > VarList.size() &&
8819 DSAStack->getParentOrderedRegionParam()) {
8820 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
8821 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
8822 }
8823 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
8824 Vars.empty())
8825 return nullptr;
8826 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008827
8828 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
8829 DepLoc, ColonLoc, Vars);
8830}
Michael Wonge710d542015-08-07 16:16:36 +00008831
8832OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
8833 SourceLocation LParenLoc,
8834 SourceLocation EndLoc) {
8835 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00008836
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008837 // OpenMP [2.9.1, Restrictions]
8838 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008839 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
8840 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008841 return nullptr;
8842
Michael Wonge710d542015-08-07 16:16:36 +00008843 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8844}
Kelvin Li0bff7af2015-11-23 05:32:03 +00008845
8846static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
8847 DSAStackTy *Stack, CXXRecordDecl *RD) {
8848 if (!RD || RD->isInvalidDecl())
8849 return true;
8850
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00008851 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
8852 if (auto *CTD = CTSD->getSpecializedTemplate())
8853 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00008854 auto QTy = SemaRef.Context.getRecordType(RD);
8855 if (RD->isDynamicClass()) {
8856 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8857 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
8858 return false;
8859 }
8860 auto *DC = RD;
8861 bool IsCorrect = true;
8862 for (auto *I : DC->decls()) {
8863 if (I) {
8864 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
8865 if (MD->isStatic()) {
8866 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8867 SemaRef.Diag(MD->getLocation(),
8868 diag::note_omp_static_member_in_target);
8869 IsCorrect = false;
8870 }
8871 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
8872 if (VD->isStaticDataMember()) {
8873 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8874 SemaRef.Diag(VD->getLocation(),
8875 diag::note_omp_static_member_in_target);
8876 IsCorrect = false;
8877 }
8878 }
8879 }
8880 }
8881
8882 for (auto &I : RD->bases()) {
8883 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
8884 I.getType()->getAsCXXRecordDecl()))
8885 IsCorrect = false;
8886 }
8887 return IsCorrect;
8888}
8889
8890static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
8891 DSAStackTy *Stack, QualType QTy) {
8892 NamedDecl *ND;
8893 if (QTy->isIncompleteType(&ND)) {
8894 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
8895 return false;
8896 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
8897 if (!RD->isInvalidDecl() &&
8898 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
8899 return false;
8900 }
8901 return true;
8902}
8903
Samuel Antao5de996e2016-01-22 20:21:36 +00008904// Return the expression of the base of the map clause or null if it cannot
8905// be determined and do all the necessary checks to see if the expression is
8906// valid as a standalone map clause expression.
8907static Expr *CheckMapClauseExpressionBase(Sema &SemaRef, Expr *E) {
8908 SourceLocation ELoc = E->getExprLoc();
8909 SourceRange ERange = E->getSourceRange();
8910
8911 // The base of elements of list in a map clause have to be either:
8912 // - a reference to variable or field.
8913 // - a member expression.
8914 // - an array expression.
8915 //
8916 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
8917 // reference to 'r'.
8918 //
8919 // If we have:
8920 //
8921 // struct SS {
8922 // Bla S;
8923 // foo() {
8924 // #pragma omp target map (S.Arr[:12]);
8925 // }
8926 // }
8927 //
8928 // We want to retrieve the member expression 'this->S';
8929
8930 Expr *RelevantExpr = nullptr;
8931
8932 // Flags to help capture some memory
8933
8934 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
8935 // If a list item is an array section, it must specify contiguous storage.
8936 //
8937 // For this restriction it is sufficient that we make sure only references
8938 // to variables or fields and array expressions, and that no array sections
8939 // exist except in the rightmost expression. E.g. these would be invalid:
8940 //
8941 // r.ArrS[3:5].Arr[6:7]
8942 //
8943 // r.ArrS[3:5].x
8944 //
8945 // but these would be valid:
8946 // r.ArrS[3].Arr[6:7]
8947 //
8948 // r.ArrS[3].x
8949
8950 bool IsRightMostExpression = true;
8951
8952 while (!RelevantExpr) {
8953 auto AllowArraySection = IsRightMostExpression;
8954 IsRightMostExpression = false;
8955
8956 E = E->IgnoreParenImpCasts();
8957
8958 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
8959 if (!isa<VarDecl>(CurE->getDecl()))
8960 break;
8961
8962 RelevantExpr = CurE;
8963 continue;
8964 }
8965
8966 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
8967 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
8968
8969 if (isa<CXXThisExpr>(BaseE))
8970 // We found a base expression: this->Val.
8971 RelevantExpr = CurE;
8972 else
8973 E = BaseE;
8974
8975 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
8976 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
8977 << CurE->getSourceRange();
8978 break;
8979 }
8980
8981 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
8982
8983 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
8984 // A bit-field cannot appear in a map clause.
8985 //
8986 if (FD->isBitField()) {
8987 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_map_clause)
8988 << CurE->getSourceRange();
8989 break;
8990 }
8991
8992 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
8993 // If the type of a list item is a reference to a type T then the type
8994 // will be considered to be T for all purposes of this clause.
8995 QualType CurType = BaseE->getType();
8996 if (CurType->isReferenceType())
8997 CurType = CurType->getPointeeType();
8998
8999 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
9000 // A list item cannot be a variable that is a member of a structure with
9001 // a union type.
9002 //
9003 if (auto *RT = CurType->getAs<RecordType>())
9004 if (RT->isUnionType()) {
9005 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
9006 << CurE->getSourceRange();
9007 break;
9008 }
9009
9010 continue;
9011 }
9012
9013 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
9014 E = CurE->getBase()->IgnoreParenImpCasts();
9015
9016 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
9017 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9018 << 0 << CurE->getSourceRange();
9019 break;
9020 }
9021 continue;
9022 }
9023
9024 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
9025 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
9026 // If a list item is an element of a structure, only the rightmost symbol
9027 // of the variable reference can be an array section.
9028 //
9029 if (!AllowArraySection) {
9030 SemaRef.Diag(ELoc, diag::err_omp_array_section_in_rightmost_expression)
9031 << CurE->getSourceRange();
9032 break;
9033 }
9034
9035 E = CurE->getBase()->IgnoreParenImpCasts();
9036
9037 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9038 // If the type of a list item is a reference to a type T then the type
9039 // will be considered to be T for all purposes of this clause.
9040 QualType CurType = E->getType();
9041 if (CurType->isReferenceType())
9042 CurType = CurType->getPointeeType();
9043
9044 if (!CurType->isAnyPointerType() && !CurType->isArrayType()) {
9045 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9046 << 0 << CurE->getSourceRange();
9047 break;
9048 }
9049
9050 continue;
9051 }
9052
9053 // If nothing else worked, this is not a valid map clause expression.
9054 SemaRef.Diag(ELoc,
9055 diag::err_omp_expected_named_var_member_or_array_expression)
9056 << ERange;
9057 break;
9058 }
9059
9060 return RelevantExpr;
9061}
9062
9063// Return true if expression E associated with value VD has conflicts with other
9064// map information.
9065static bool CheckMapConflicts(Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD,
9066 Expr *E, bool CurrentRegionOnly) {
9067 assert(VD && E);
9068
9069 // Types used to organize the components of a valid map clause.
9070 typedef std::pair<Expr *, ValueDecl *> MapExpressionComponent;
9071 typedef SmallVector<MapExpressionComponent, 4> MapExpressionComponents;
9072
9073 // Helper to extract the components in the map clause expression E and store
9074 // them into MEC. This assumes that E is a valid map clause expression, i.e.
9075 // it has already passed the single clause checks.
9076 auto ExtractMapExpressionComponents = [](Expr *TE,
9077 MapExpressionComponents &MEC) {
9078 while (true) {
9079 TE = TE->IgnoreParenImpCasts();
9080
9081 if (auto *CurE = dyn_cast<DeclRefExpr>(TE)) {
9082 MEC.push_back(
9083 MapExpressionComponent(CurE, cast<VarDecl>(CurE->getDecl())));
9084 break;
9085 }
9086
9087 if (auto *CurE = dyn_cast<MemberExpr>(TE)) {
9088 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
9089
9090 MEC.push_back(MapExpressionComponent(
9091 CurE, cast<FieldDecl>(CurE->getMemberDecl())));
9092 if (isa<CXXThisExpr>(BaseE))
9093 break;
9094
9095 TE = BaseE;
9096 continue;
9097 }
9098
9099 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(TE)) {
9100 MEC.push_back(MapExpressionComponent(CurE, nullptr));
9101 TE = CurE->getBase()->IgnoreParenImpCasts();
9102 continue;
9103 }
9104
9105 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(TE)) {
9106 MEC.push_back(MapExpressionComponent(CurE, nullptr));
9107 TE = CurE->getBase()->IgnoreParenImpCasts();
9108 continue;
9109 }
9110
9111 llvm_unreachable(
9112 "Expecting only valid map clause expressions at this point!");
9113 }
9114 };
9115
9116 SourceLocation ELoc = E->getExprLoc();
9117 SourceRange ERange = E->getSourceRange();
9118
9119 // In order to easily check the conflicts we need to match each component of
9120 // the expression under test with the components of the expressions that are
9121 // already in the stack.
9122
9123 MapExpressionComponents CurComponents;
9124 ExtractMapExpressionComponents(E, CurComponents);
9125
9126 assert(!CurComponents.empty() && "Map clause expression with no components!");
9127 assert(CurComponents.back().second == VD &&
9128 "Map clause expression with unexpected base!");
9129
9130 // Variables to help detecting enclosing problems in data environment nests.
9131 bool IsEnclosedByDataEnvironmentExpr = false;
9132 Expr *EnclosingExpr = nullptr;
9133
9134 bool FoundError =
9135 DSAS->checkMapInfoForVar(VD, CurrentRegionOnly, [&](Expr *RE) -> bool {
9136 MapExpressionComponents StackComponents;
9137 ExtractMapExpressionComponents(RE, StackComponents);
9138 assert(!StackComponents.empty() &&
9139 "Map clause expression with no components!");
9140 assert(StackComponents.back().second == VD &&
9141 "Map clause expression with unexpected base!");
9142
9143 // Expressions must start from the same base. Here we detect at which
9144 // point both expressions diverge from each other and see if we can
9145 // detect if the memory referred to both expressions is contiguous and
9146 // do not overlap.
9147 auto CI = CurComponents.rbegin();
9148 auto CE = CurComponents.rend();
9149 auto SI = StackComponents.rbegin();
9150 auto SE = StackComponents.rend();
9151 for (; CI != CE && SI != SE; ++CI, ++SI) {
9152
9153 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
9154 // At most one list item can be an array item derived from a given
9155 // variable in map clauses of the same construct.
9156 if (CurrentRegionOnly && (isa<ArraySubscriptExpr>(CI->first) ||
9157 isa<OMPArraySectionExpr>(CI->first)) &&
9158 (isa<ArraySubscriptExpr>(SI->first) ||
9159 isa<OMPArraySectionExpr>(SI->first))) {
9160 SemaRef.Diag(CI->first->getExprLoc(),
9161 diag::err_omp_multiple_array_items_in_map_clause)
9162 << CI->first->getSourceRange();
9163 ;
9164 SemaRef.Diag(SI->first->getExprLoc(), diag::note_used_here)
9165 << SI->first->getSourceRange();
9166 return true;
9167 }
9168
9169 // Do both expressions have the same kind?
9170 if (CI->first->getStmtClass() != SI->first->getStmtClass())
9171 break;
9172
9173 // Are we dealing with different variables/fields?
9174 if (CI->second != SI->second)
9175 break;
9176 }
9177
9178 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
9179 // List items of map clauses in the same construct must not share
9180 // original storage.
9181 //
9182 // If the expressions are exactly the same or one is a subset of the
9183 // other, it means they are sharing storage.
9184 if (CI == CE && SI == SE) {
9185 if (CurrentRegionOnly) {
9186 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
9187 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9188 << RE->getSourceRange();
9189 return true;
9190 } else {
9191 // If we find the same expression in the enclosing data environment,
9192 // that is legal.
9193 IsEnclosedByDataEnvironmentExpr = true;
9194 return false;
9195 }
9196 }
9197
9198 QualType DerivedType = std::prev(CI)->first->getType();
9199 SourceLocation DerivedLoc = std::prev(CI)->first->getExprLoc();
9200
9201 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9202 // If the type of a list item is a reference to a type T then the type
9203 // will be considered to be T for all purposes of this clause.
9204 if (DerivedType->isReferenceType())
9205 DerivedType = DerivedType->getPointeeType();
9206
9207 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
9208 // A variable for which the type is pointer and an array section
9209 // derived from that variable must not appear as list items of map
9210 // clauses of the same construct.
9211 //
9212 // Also, cover one of the cases in:
9213 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
9214 // If any part of the original storage of a list item has corresponding
9215 // storage in the device data environment, all of the original storage
9216 // must have corresponding storage in the device data environment.
9217 //
9218 if (DerivedType->isAnyPointerType()) {
9219 if (CI == CE || SI == SE) {
9220 SemaRef.Diag(
9221 DerivedLoc,
9222 diag::err_omp_pointer_mapped_along_with_derived_section)
9223 << DerivedLoc;
9224 } else {
9225 assert(CI != CE && SI != SE);
9226 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
9227 << DerivedLoc;
9228 }
9229 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9230 << RE->getSourceRange();
9231 return true;
9232 }
9233
9234 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
9235 // List items of map clauses in the same construct must not share
9236 // original storage.
9237 //
9238 // An expression is a subset of the other.
9239 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
9240 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
9241 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9242 << RE->getSourceRange();
9243 return true;
9244 }
9245
9246 // The current expression uses the same base as other expression in the
9247 // data environment but does not contain it completelly.
9248 if (!CurrentRegionOnly && SI != SE)
9249 EnclosingExpr = RE;
9250
9251 // The current expression is a subset of the expression in the data
9252 // environment.
9253 IsEnclosedByDataEnvironmentExpr |=
9254 (!CurrentRegionOnly && CI != CE && SI == SE);
9255
9256 return false;
9257 });
9258
9259 if (CurrentRegionOnly)
9260 return FoundError;
9261
9262 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
9263 // If any part of the original storage of a list item has corresponding
9264 // storage in the device data environment, all of the original storage must
9265 // have corresponding storage in the device data environment.
9266 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
9267 // If a list item is an element of a structure, and a different element of
9268 // the structure has a corresponding list item in the device data environment
9269 // prior to a task encountering the construct associated with the map clause,
9270 // then the list item must also have a correspnding list item in the device
9271 // data environment prior to the task encountering the construct.
9272 //
9273 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
9274 SemaRef.Diag(ELoc,
9275 diag::err_omp_original_storage_is_shared_and_does_not_contain)
9276 << ERange;
9277 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
9278 << EnclosingExpr->getSourceRange();
9279 return true;
9280 }
9281
9282 return FoundError;
9283}
9284
Samuel Antao23abd722016-01-19 20:40:49 +00009285OMPClause *
9286Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
9287 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
9288 SourceLocation MapLoc, SourceLocation ColonLoc,
9289 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
9290 SourceLocation LParenLoc, SourceLocation EndLoc) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009291 SmallVector<Expr *, 4> Vars;
9292
9293 for (auto &RE : VarList) {
9294 assert(RE && "Null expr in omp map");
9295 if (isa<DependentScopeDeclRefExpr>(RE)) {
9296 // It will be analyzed later.
9297 Vars.push_back(RE);
9298 continue;
9299 }
9300 SourceLocation ELoc = RE->getExprLoc();
9301
Kelvin Li0bff7af2015-11-23 05:32:03 +00009302 auto *VE = RE->IgnoreParenLValueCasts();
9303
9304 if (VE->isValueDependent() || VE->isTypeDependent() ||
9305 VE->isInstantiationDependent() ||
9306 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009307 // We can only analyze this information once the missing information is
9308 // resolved.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009309 Vars.push_back(RE);
9310 continue;
9311 }
9312
9313 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009314
Samuel Antao5de996e2016-01-22 20:21:36 +00009315 if (!RE->IgnoreParenImpCasts()->isLValue()) {
9316 Diag(ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
9317 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009318 continue;
9319 }
9320
Samuel Antao5de996e2016-01-22 20:21:36 +00009321 // Obtain the array or member expression bases if required.
9322 auto *BE = CheckMapClauseExpressionBase(*this, SimpleExpr);
9323 if (!BE)
9324 continue;
9325
9326 // If the base is a reference to a variable, we rely on that variable for
9327 // the following checks. If it is a 'this' expression we rely on the field.
9328 ValueDecl *D = nullptr;
9329 if (auto *DRE = dyn_cast<DeclRefExpr>(BE)) {
9330 D = DRE->getDecl();
9331 } else {
9332 auto *ME = cast<MemberExpr>(BE);
9333 assert(isa<CXXThisExpr>(ME->getBase()) && "Unexpected expression!");
9334 D = ME->getMemberDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009335 }
9336 assert(D && "Null decl on map clause.");
Kelvin Li0bff7af2015-11-23 05:32:03 +00009337
Samuel Antao5de996e2016-01-22 20:21:36 +00009338 auto *VD = dyn_cast<VarDecl>(D);
9339 auto *FD = dyn_cast<FieldDecl>(D);
9340
9341 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +00009342 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +00009343
9344 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
9345 // threadprivate variables cannot appear in a map clause.
9346 if (VD && DSAStack->isThreadPrivate(VD)) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009347 auto DVar = DSAStack->getTopDSA(VD, false);
9348 Diag(ELoc, diag::err_omp_threadprivate_in_map);
9349 ReportOriginalDSA(*this, DSAStack, VD, DVar);
9350 continue;
9351 }
9352
Samuel Antao5de996e2016-01-22 20:21:36 +00009353 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
9354 // A list item cannot appear in both a map clause and a data-sharing
9355 // attribute clause on the same construct.
9356 //
9357 // TODO: Implement this check - it cannot currently be tested because of
9358 // missing implementation of the other data sharing clauses in target
9359 // directives.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009360
Samuel Antao5de996e2016-01-22 20:21:36 +00009361 // Check conflicts with other map clause expressions. We check the conflicts
9362 // with the current construct separately from the enclosing data
9363 // environment, because the restrictions are different.
9364 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9365 /*CurrentRegionOnly=*/true))
9366 break;
9367 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9368 /*CurrentRegionOnly=*/false))
9369 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +00009370
Samuel Antao5de996e2016-01-22 20:21:36 +00009371 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9372 // If the type of a list item is a reference to a type T then the type will
9373 // be considered to be T for all purposes of this clause.
9374 QualType Type = D->getType();
9375 if (Type->isReferenceType())
9376 Type = Type->getPointeeType();
9377
9378 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +00009379 // A list item must have a mappable type.
9380 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
9381 DSAStack, Type))
9382 continue;
9383
Samuel Antaodf67fc42016-01-19 19:15:56 +00009384 // target enter data
9385 // OpenMP [2.10.2, Restrictions, p. 99]
9386 // A map-type must be specified in all map clauses and must be either
9387 // to or alloc.
9388 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
9389 if (DKind == OMPD_target_enter_data &&
9390 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
9391 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00009392 << (IsMapTypeImplicit ? 1 : 0)
9393 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antaodf67fc42016-01-19 19:15:56 +00009394 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009395 continue;
Samuel Antaodf67fc42016-01-19 19:15:56 +00009396 }
9397
Samuel Antao72590762016-01-19 20:04:50 +00009398 // target exit_data
9399 // OpenMP [2.10.3, Restrictions, p. 102]
9400 // A map-type must be specified in all map clauses and must be either
9401 // from, release, or delete.
9402 DKind = DSAStack->getCurrentDirective();
9403 if (DKind == OMPD_target_exit_data &&
9404 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
9405 MapType == OMPC_MAP_delete)) {
9406 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00009407 << (IsMapTypeImplicit ? 1 : 0)
9408 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antao72590762016-01-19 20:04:50 +00009409 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009410 continue;
Samuel Antao72590762016-01-19 20:04:50 +00009411 }
9412
Kelvin Li0bff7af2015-11-23 05:32:03 +00009413 Vars.push_back(RE);
Samuel Antao5de996e2016-01-22 20:21:36 +00009414 DSAStack->addExprToVarMapInfo(D, RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +00009415 }
Kelvin Li0bff7af2015-11-23 05:32:03 +00009416
Samuel Antao5de996e2016-01-22 20:21:36 +00009417 // We need to produce a map clause even if we don't have variables so that
9418 // other diagnostics related with non-existing map clauses are accurate.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009419 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
Samuel Antao23abd722016-01-19 20:40:49 +00009420 MapTypeModifier, MapType, IsMapTypeImplicit,
9421 MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +00009422}
Kelvin Li099bb8c2015-11-24 20:50:12 +00009423
9424OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
9425 SourceLocation StartLoc,
9426 SourceLocation LParenLoc,
9427 SourceLocation EndLoc) {
9428 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +00009429
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009430 // OpenMP [teams Constrcut, Restrictions]
9431 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009432 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
9433 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009434 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +00009435
9436 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9437}
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009438
9439OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
9440 SourceLocation StartLoc,
9441 SourceLocation LParenLoc,
9442 SourceLocation EndLoc) {
9443 Expr *ValExpr = ThreadLimit;
9444
9445 // OpenMP [teams Constrcut, Restrictions]
9446 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009447 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
9448 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009449 return nullptr;
9450
9451 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
9452 EndLoc);
9453}
Alexey Bataeva0569352015-12-01 10:17:31 +00009454
9455OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
9456 SourceLocation StartLoc,
9457 SourceLocation LParenLoc,
9458 SourceLocation EndLoc) {
9459 Expr *ValExpr = Priority;
9460
9461 // OpenMP [2.9.1, task Constrcut]
9462 // The priority-value is a non-negative numerical scalar expression.
9463 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
9464 /*StrictlyPositive=*/false))
9465 return nullptr;
9466
9467 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9468}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009469
9470OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
9471 SourceLocation StartLoc,
9472 SourceLocation LParenLoc,
9473 SourceLocation EndLoc) {
9474 Expr *ValExpr = Grainsize;
9475
9476 // OpenMP [2.9.2, taskloop Constrcut]
9477 // The parameter of the grainsize clause must be a positive integer
9478 // expression.
9479 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
9480 /*StrictlyPositive=*/true))
9481 return nullptr;
9482
9483 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9484}
Alexey Bataev382967a2015-12-08 12:06:20 +00009485
9486OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
9487 SourceLocation StartLoc,
9488 SourceLocation LParenLoc,
9489 SourceLocation EndLoc) {
9490 Expr *ValExpr = NumTasks;
9491
9492 // OpenMP [2.9.2, taskloop Constrcut]
9493 // The parameter of the num_tasks clause must be a positive integer
9494 // expression.
9495 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
9496 /*StrictlyPositive=*/true))
9497 return nullptr;
9498
9499 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9500}
9501
Alexey Bataev28c75412015-12-15 08:19:24 +00009502OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
9503 SourceLocation LParenLoc,
9504 SourceLocation EndLoc) {
9505 // OpenMP [2.13.2, critical construct, Description]
9506 // ... where hint-expression is an integer constant expression that evaluates
9507 // to a valid lock hint.
9508 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
9509 if (HintExpr.isInvalid())
9510 return nullptr;
9511 return new (Context)
9512 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
9513}
9514
Carlo Bertollib4adf552016-01-15 18:50:31 +00009515OMPClause *Sema::ActOnOpenMPDistScheduleClause(
9516 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
9517 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
9518 SourceLocation EndLoc) {
9519 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
9520 std::string Values;
9521 Values += "'";
9522 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
9523 Values += "'";
9524 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
9525 << Values << getOpenMPClauseName(OMPC_dist_schedule);
9526 return nullptr;
9527 }
9528 Expr *ValExpr = ChunkSize;
9529 Expr *HelperValExpr = nullptr;
9530 if (ChunkSize) {
9531 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
9532 !ChunkSize->isInstantiationDependent() &&
9533 !ChunkSize->containsUnexpandedParameterPack()) {
9534 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
9535 ExprResult Val =
9536 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
9537 if (Val.isInvalid())
9538 return nullptr;
9539
9540 ValExpr = Val.get();
9541
9542 // OpenMP [2.7.1, Restrictions]
9543 // chunk_size must be a loop invariant integer expression with a positive
9544 // value.
9545 llvm::APSInt Result;
9546 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
9547 if (Result.isSigned() && !Result.isStrictlyPositive()) {
9548 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
9549 << "dist_schedule" << ChunkSize->getSourceRange();
9550 return nullptr;
9551 }
9552 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev4244be22016-02-11 05:35:55 +00009553 HelperValExpr =
9554 buildCapture(*this, &Context.Idents.get(".chunk."), ValExpr);
Carlo Bertollib4adf552016-01-15 18:50:31 +00009555 }
9556 }
9557 }
9558
9559 return new (Context)
9560 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
9561 Kind, ValExpr, HelperValExpr);
9562}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009563
9564OMPClause *Sema::ActOnOpenMPDefaultmapClause(
9565 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
9566 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
9567 SourceLocation KindLoc, SourceLocation EndLoc) {
9568 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
9569 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
9570 Kind != OMPC_DEFAULTMAP_scalar) {
9571 std::string Value;
9572 SourceLocation Loc;
9573 Value += "'";
9574 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
9575 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
9576 OMPC_DEFAULTMAP_MODIFIER_tofrom);
9577 Loc = MLoc;
9578 } else {
9579 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
9580 OMPC_DEFAULTMAP_scalar);
9581 Loc = KindLoc;
9582 }
9583 Value += "'";
9584 Diag(Loc, diag::err_omp_unexpected_clause_value)
9585 << Value << getOpenMPClauseName(OMPC_defaultmap);
9586 return nullptr;
9587 }
9588
9589 return new (Context)
9590 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
9591}