blob: 9f4daeeffe9f5a20e4917d150cdbc5ec0c3a9a27 [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
1435 // an
1436 // explicit task.
1437 DVar =
1438 Stack->hasInnermostDSA(FD, MatchesAnyClause(OMPC_reduction),
1439 [](OpenMPDirectiveKind K) -> bool {
1440 return isOpenMPParallelDirective(K) ||
1441 isOpenMPWorksharingDirective(K) ||
1442 isOpenMPTeamsDirective(K);
1443 },
1444 false);
1445 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1446 ErrorFound = true;
1447 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1448 ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1449 return;
1450 }
1451
1452 // Define implicit data-sharing attributes for task.
1453 DVar = Stack->getImplicitDSA(FD, false);
1454 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
1455 ImplicitFirstprivate.push_back(E);
1456 }
1457 }
1458 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001459 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001460 for (auto *C : S->clauses()) {
1461 // Skip analysis of arguments of implicitly defined firstprivate clause
1462 // for task directives.
1463 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1464 for (auto *CC : C->children()) {
1465 if (CC)
1466 Visit(CC);
1467 }
1468 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001469 }
1470 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001471 for (auto *C : S->children()) {
1472 if (C && !isa<OMPExecutableDirective>(C))
1473 Visit(C);
1474 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001475 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001476
1477 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001478 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001479 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001480 return VarsWithInheritedDSA;
1481 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001482
Alexey Bataev7ff55242014-06-19 09:13:45 +00001483 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1484 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001485};
Alexey Bataeved09d242014-05-28 05:53:51 +00001486} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001487
Alexey Bataevbae9a792014-06-27 10:37:06 +00001488void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001489 switch (DKind) {
1490 case OMPD_parallel: {
1491 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001492 QualType KmpInt32PtrTy =
1493 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001494 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001495 std::make_pair(".global_tid.", KmpInt32PtrTy),
1496 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1497 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001498 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001499 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1500 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001501 break;
1502 }
1503 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001504 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001505 std::make_pair(StringRef(), QualType()) // __context with shared vars
1506 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001507 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1508 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001509 break;
1510 }
1511 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001512 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001513 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001514 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001515 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1516 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001517 break;
1518 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001519 case OMPD_for_simd: {
1520 Sema::CapturedParamNameType Params[] = {
1521 std::make_pair(StringRef(), QualType()) // __context with shared vars
1522 };
1523 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1524 Params);
1525 break;
1526 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001527 case OMPD_sections: {
1528 Sema::CapturedParamNameType Params[] = {
1529 std::make_pair(StringRef(), QualType()) // __context with shared vars
1530 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001531 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1532 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001533 break;
1534 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001535 case OMPD_section: {
1536 Sema::CapturedParamNameType Params[] = {
1537 std::make_pair(StringRef(), QualType()) // __context with shared vars
1538 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001539 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1540 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001541 break;
1542 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001543 case OMPD_single: {
1544 Sema::CapturedParamNameType Params[] = {
1545 std::make_pair(StringRef(), QualType()) // __context with shared vars
1546 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001547 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1548 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001549 break;
1550 }
Alexander Musman80c22892014-07-17 08:54:58 +00001551 case OMPD_master: {
1552 Sema::CapturedParamNameType Params[] = {
1553 std::make_pair(StringRef(), QualType()) // __context with shared vars
1554 };
1555 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1556 Params);
1557 break;
1558 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001559 case OMPD_critical: {
1560 Sema::CapturedParamNameType Params[] = {
1561 std::make_pair(StringRef(), QualType()) // __context with shared vars
1562 };
1563 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1564 Params);
1565 break;
1566 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001567 case OMPD_parallel_for: {
1568 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001569 QualType KmpInt32PtrTy =
1570 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001571 Sema::CapturedParamNameType Params[] = {
1572 std::make_pair(".global_tid.", KmpInt32PtrTy),
1573 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1574 std::make_pair(StringRef(), QualType()) // __context with shared vars
1575 };
1576 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1577 Params);
1578 break;
1579 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001580 case OMPD_parallel_for_simd: {
1581 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001582 QualType KmpInt32PtrTy =
1583 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001584 Sema::CapturedParamNameType Params[] = {
1585 std::make_pair(".global_tid.", KmpInt32PtrTy),
1586 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1587 std::make_pair(StringRef(), QualType()) // __context with shared vars
1588 };
1589 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1590 Params);
1591 break;
1592 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001593 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001594 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001595 QualType KmpInt32PtrTy =
1596 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001597 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001598 std::make_pair(".global_tid.", KmpInt32PtrTy),
1599 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001600 std::make_pair(StringRef(), QualType()) // __context with shared vars
1601 };
1602 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1603 Params);
1604 break;
1605 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001606 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001607 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001608 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1609 FunctionProtoType::ExtProtoInfo EPI;
1610 EPI.Variadic = true;
1611 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001612 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001613 std::make_pair(".global_tid.", KmpInt32Ty),
1614 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001615 std::make_pair(".privates.",
1616 Context.VoidPtrTy.withConst().withRestrict()),
1617 std::make_pair(
1618 ".copy_fn.",
1619 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001620 std::make_pair(StringRef(), QualType()) // __context with shared vars
1621 };
1622 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1623 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001624 // Mark this captured region as inlined, because we don't use outlined
1625 // function directly.
1626 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1627 AlwaysInlineAttr::CreateImplicit(
1628 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001629 break;
1630 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001631 case OMPD_ordered: {
1632 Sema::CapturedParamNameType Params[] = {
1633 std::make_pair(StringRef(), QualType()) // __context with shared vars
1634 };
1635 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1636 Params);
1637 break;
1638 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001639 case OMPD_atomic: {
1640 Sema::CapturedParamNameType Params[] = {
1641 std::make_pair(StringRef(), QualType()) // __context with shared vars
1642 };
1643 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1644 Params);
1645 break;
1646 }
Michael Wong65f367f2015-07-21 13:44:28 +00001647 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001648 case OMPD_target:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001649 case OMPD_target_parallel:
1650 case OMPD_target_parallel_for: {
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001651 Sema::CapturedParamNameType Params[] = {
1652 std::make_pair(StringRef(), QualType()) // __context with shared vars
1653 };
1654 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1655 Params);
1656 break;
1657 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001658 case OMPD_teams: {
1659 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001660 QualType KmpInt32PtrTy =
1661 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001662 Sema::CapturedParamNameType Params[] = {
1663 std::make_pair(".global_tid.", KmpInt32PtrTy),
1664 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1665 std::make_pair(StringRef(), QualType()) // __context with shared vars
1666 };
1667 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1668 Params);
1669 break;
1670 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001671 case OMPD_taskgroup: {
1672 Sema::CapturedParamNameType Params[] = {
1673 std::make_pair(StringRef(), QualType()) // __context with shared vars
1674 };
1675 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1676 Params);
1677 break;
1678 }
Alexey Bataev49f6e782015-12-01 04:18:41 +00001679 case OMPD_taskloop: {
1680 Sema::CapturedParamNameType Params[] = {
1681 std::make_pair(StringRef(), QualType()) // __context with shared vars
1682 };
1683 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1684 Params);
1685 break;
1686 }
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001687 case OMPD_taskloop_simd: {
1688 Sema::CapturedParamNameType Params[] = {
1689 std::make_pair(StringRef(), QualType()) // __context with shared vars
1690 };
1691 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1692 Params);
1693 break;
1694 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001695 case OMPD_distribute: {
1696 Sema::CapturedParamNameType Params[] = {
1697 std::make_pair(StringRef(), QualType()) // __context with shared vars
1698 };
1699 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1700 Params);
1701 break;
1702 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001703 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001704 case OMPD_taskyield:
1705 case OMPD_barrier:
1706 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001707 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001708 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001709 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001710 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001711 case OMPD_target_exit_data:
Alexey Bataev9959db52014-05-06 10:08:46 +00001712 llvm_unreachable("OpenMP Directive is not allowed");
1713 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001714 llvm_unreachable("Unknown OpenMP directive");
1715 }
1716}
1717
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001718StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1719 ArrayRef<OMPClause *> Clauses) {
1720 if (!S.isUsable()) {
1721 ActOnCapturedRegionError();
1722 return StmtError();
1723 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001724
1725 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001726 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001727 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001728 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001729 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001730 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001731 Clause->getClauseKind() == OMPC_copyprivate ||
1732 (getLangOpts().OpenMPUseTLS &&
1733 getASTContext().getTargetInfo().isTLSSupported() &&
1734 Clause->getClauseKind() == OMPC_copyin)) {
1735 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001736 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001737 for (auto *VarRef : Clause->children()) {
1738 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001739 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001740 }
1741 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001742 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev040d5402015-05-12 08:35:28 +00001743 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
1744 Clause->getClauseKind() == OMPC_schedule) {
1745 // Mark all variables in private list clauses as used in inner region.
1746 // Required for proper codegen of combined directives.
1747 // TODO: add processing for other clauses.
1748 if (auto *E = cast_or_null<Expr>(
Alexey Bataev6402bca2015-12-28 07:25:51 +00001749 cast<OMPScheduleClause>(Clause)->getHelperChunkSize()))
1750 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001751 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001752 if (Clause->getClauseKind() == OMPC_schedule)
1753 SC = cast<OMPScheduleClause>(Clause);
1754 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001755 OC = cast<OMPOrderedClause>(Clause);
1756 else if (Clause->getClauseKind() == OMPC_linear)
1757 LCs.push_back(cast<OMPLinearClause>(Clause));
1758 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001759 bool ErrorFound = false;
1760 // OpenMP, 2.7.1 Loop Construct, Restrictions
1761 // The nonmonotonic modifier cannot be specified if an ordered clause is
1762 // specified.
1763 if (SC &&
1764 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1765 SC->getSecondScheduleModifier() ==
1766 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1767 OC) {
1768 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1769 ? SC->getFirstScheduleModifierLoc()
1770 : SC->getSecondScheduleModifierLoc(),
1771 diag::err_omp_schedule_nonmonotonic_ordered)
1772 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1773 ErrorFound = true;
1774 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001775 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1776 for (auto *C : LCs) {
1777 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1778 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1779 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001780 ErrorFound = true;
1781 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001782 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1783 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1784 OC->getNumForLoops()) {
1785 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1786 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1787 ErrorFound = true;
1788 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001789 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001790 ActOnCapturedRegionError();
1791 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001792 }
1793 return ActOnCapturedRegionEnd(S.get());
1794}
1795
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001796static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1797 OpenMPDirectiveKind CurrentRegion,
1798 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001799 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001800 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001801 // Allowed nesting of constructs
1802 // +------------------+-----------------+------------------------------------+
1803 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1804 // +------------------+-----------------+------------------------------------+
1805 // | parallel | parallel | * |
1806 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001807 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001808 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001809 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001810 // | parallel | simd | * |
1811 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001812 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001813 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001814 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001815 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001816 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001817 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001818 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001819 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001820 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001821 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001822 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001823 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001824 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001825 // | parallel | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001826 // | parallel | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001827 // | parallel | target parallel | * |
1828 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001829 // | parallel | target enter | * |
1830 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001831 // | parallel | target exit | * |
1832 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001833 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001834 // | parallel | cancellation | |
1835 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001836 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001837 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001838 // | parallel | taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001839 // | parallel | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001840 // +------------------+-----------------+------------------------------------+
1841 // | for | parallel | * |
1842 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001843 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001844 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001845 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001846 // | for | simd | * |
1847 // | for | sections | + |
1848 // | for | section | + |
1849 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001850 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001851 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001852 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001853 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001854 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001855 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001856 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001857 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001858 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001859 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001860 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001861 // | for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001862 // | for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001863 // | for | target parallel | * |
1864 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001865 // | for | target enter | * |
1866 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001867 // | for | target exit | * |
1868 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001869 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001870 // | for | cancellation | |
1871 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001872 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001873 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001874 // | for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001875 // | for | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001876 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001877 // | master | parallel | * |
1878 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001879 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001880 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001881 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001882 // | master | simd | * |
1883 // | master | sections | + |
1884 // | master | section | + |
1885 // | master | single | + |
1886 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001887 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001888 // | master |parallel sections| * |
1889 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001890 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001891 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001892 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001893 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001894 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001895 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001896 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001897 // | master | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001898 // | master | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001899 // | master | target parallel | * |
1900 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001901 // | master | target enter | * |
1902 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001903 // | master | target exit | * |
1904 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001905 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001906 // | master | cancellation | |
1907 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001908 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001909 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001910 // | master | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001911 // | master | distribute | |
Alexander Musman80c22892014-07-17 08:54:58 +00001912 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001913 // | critical | parallel | * |
1914 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001915 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001916 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001917 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001918 // | critical | simd | * |
1919 // | critical | sections | + |
1920 // | critical | section | + |
1921 // | critical | single | + |
1922 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001923 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001924 // | critical |parallel sections| * |
1925 // | critical | task | * |
1926 // | critical | taskyield | * |
1927 // | critical | barrier | + |
1928 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001929 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001930 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001931 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001932 // | critical | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001933 // | critical | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001934 // | critical | target parallel | * |
1935 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001936 // | critical | target enter | * |
1937 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001938 // | critical | target exit | * |
1939 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001940 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001941 // | critical | cancellation | |
1942 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001943 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001944 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001945 // | critical | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001946 // | critical | distribute | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001947 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001948 // | simd | parallel | |
1949 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001950 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001951 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001952 // | simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00001953 // | simd | simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001954 // | simd | sections | |
1955 // | simd | section | |
1956 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001957 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001958 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001959 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001960 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001961 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001962 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001963 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001964 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001965 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001966 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001967 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001968 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001969 // | simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001970 // | simd | target parallel | |
1971 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001972 // | simd | target enter | |
1973 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001974 // | simd | target exit | |
1975 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001976 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001977 // | simd | cancellation | |
1978 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001979 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001980 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001981 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001982 // | simd | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001983 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001984 // | for simd | parallel | |
1985 // | for simd | for | |
1986 // | for simd | for simd | |
1987 // | for simd | master | |
1988 // | for simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00001989 // | for simd | simd | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001990 // | for simd | sections | |
1991 // | for simd | section | |
1992 // | for simd | single | |
1993 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001994 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001995 // | for simd |parallel sections| |
1996 // | for simd | task | |
1997 // | for simd | taskyield | |
1998 // | for simd | barrier | |
1999 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002000 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002001 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002002 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002003 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002004 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002005 // | for simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002006 // | for simd | target parallel | |
2007 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002008 // | for simd | target enter | |
2009 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002010 // | for simd | target exit | |
2011 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002012 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002013 // | for simd | cancellation | |
2014 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002015 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002016 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002017 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002018 // | for simd | distribute | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002019 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00002020 // | parallel for simd| parallel | |
2021 // | parallel for simd| for | |
2022 // | parallel for simd| for simd | |
2023 // | parallel for simd| master | |
2024 // | parallel for simd| critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002025 // | parallel for simd| simd | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002026 // | parallel for simd| sections | |
2027 // | parallel for simd| section | |
2028 // | parallel for simd| single | |
2029 // | parallel for simd| parallel for | |
2030 // | parallel for simd|parallel for simd| |
2031 // | parallel for simd|parallel sections| |
2032 // | parallel for simd| task | |
2033 // | parallel for simd| taskyield | |
2034 // | parallel for simd| barrier | |
2035 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002036 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002037 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002038 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002039 // | parallel for simd| atomic | |
2040 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002041 // | parallel for simd| target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002042 // | parallel for simd| target parallel | |
2043 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002044 // | parallel for simd| target enter | |
2045 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002046 // | parallel for simd| target exit | |
2047 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002048 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002049 // | parallel for simd| cancellation | |
2050 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002051 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002052 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002053 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002054 // | parallel for simd| distribute | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002055 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002056 // | sections | parallel | * |
2057 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002058 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002059 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002060 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002061 // | sections | simd | * |
2062 // | sections | sections | + |
2063 // | sections | section | * |
2064 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002065 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002066 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002067 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002068 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002069 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002070 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002071 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002072 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002073 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002074 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002075 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002076 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002077 // | sections | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002078 // | sections | target parallel | * |
2079 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002080 // | sections | target enter | * |
2081 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002082 // | sections | target exit | * |
2083 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002084 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002085 // | sections | cancellation | |
2086 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002087 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002088 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002089 // | sections | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002090 // | sections | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002091 // +------------------+-----------------+------------------------------------+
2092 // | section | parallel | * |
2093 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002094 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002095 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002096 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002097 // | section | simd | * |
2098 // | section | sections | + |
2099 // | section | section | + |
2100 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002101 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002102 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002103 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002104 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002105 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002106 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002107 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002108 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002109 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002110 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002111 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002112 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002113 // | section | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002114 // | section | target parallel | * |
2115 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002116 // | section | target enter | * |
2117 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002118 // | section | target exit | * |
2119 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002120 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002121 // | section | cancellation | |
2122 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002123 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002124 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002125 // | section | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002126 // | section | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002127 // +------------------+-----------------+------------------------------------+
2128 // | single | parallel | * |
2129 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002130 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002131 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002132 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002133 // | single | simd | * |
2134 // | single | sections | + |
2135 // | single | section | + |
2136 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002137 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002138 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002139 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002140 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002141 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002142 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002143 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002144 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002145 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002146 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002147 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002148 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002149 // | single | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002150 // | single | target parallel | * |
2151 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002152 // | single | target enter | * |
2153 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002154 // | single | target exit | * |
2155 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002156 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002157 // | single | cancellation | |
2158 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002159 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002160 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002161 // | single | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002162 // | single | distribute | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002163 // +------------------+-----------------+------------------------------------+
2164 // | parallel for | parallel | * |
2165 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002166 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002167 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002168 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002169 // | parallel for | simd | * |
2170 // | parallel for | sections | + |
2171 // | parallel for | section | + |
2172 // | parallel for | single | + |
2173 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002174 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002175 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002176 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002177 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002178 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002179 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002180 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002181 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002182 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002183 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002184 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002185 // | parallel for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002186 // | parallel for | target parallel | * |
2187 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002188 // | parallel for | target enter | * |
2189 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002190 // | parallel for | target exit | * |
2191 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002192 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002193 // | parallel for | cancellation | |
2194 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002195 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002196 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002197 // | parallel for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002198 // | parallel for | distribute | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002199 // +------------------+-----------------+------------------------------------+
2200 // | parallel sections| parallel | * |
2201 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002202 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002203 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002204 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002205 // | parallel sections| simd | * |
2206 // | parallel sections| sections | + |
2207 // | parallel sections| section | * |
2208 // | parallel sections| single | + |
2209 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002210 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002211 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002212 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002213 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002214 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002215 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002216 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002217 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002218 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002219 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002220 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002221 // | parallel sections| target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002222 // | parallel sections| target parallel | * |
2223 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002224 // | parallel sections| target enter | * |
2225 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002226 // | parallel sections| target exit | * |
2227 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002228 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002229 // | parallel sections| cancellation | |
2230 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002231 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002232 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002233 // | parallel sections| taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002234 // | parallel sections| distribute | |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002235 // +------------------+-----------------+------------------------------------+
2236 // | task | parallel | * |
2237 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002238 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002239 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002240 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002241 // | task | simd | * |
2242 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002243 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002244 // | task | single | + |
2245 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002246 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002247 // | task |parallel sections| * |
2248 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002249 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002250 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002251 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002252 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002253 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002254 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002255 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002256 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002257 // | task | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002258 // | task | target parallel | * |
2259 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002260 // | task | target enter | * |
2261 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002262 // | task | target exit | * |
2263 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002264 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002265 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002266 // | | point | ! |
2267 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002268 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002269 // | task | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002270 // | task | distribute | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002271 // +------------------+-----------------+------------------------------------+
2272 // | ordered | parallel | * |
2273 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002274 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002275 // | ordered | master | * |
2276 // | ordered | critical | * |
2277 // | ordered | simd | * |
2278 // | ordered | sections | + |
2279 // | ordered | section | + |
2280 // | ordered | single | + |
2281 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002282 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002283 // | ordered |parallel sections| * |
2284 // | ordered | task | * |
2285 // | ordered | taskyield | * |
2286 // | ordered | barrier | + |
2287 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002288 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002289 // | ordered | flush | * |
2290 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002291 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002292 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002293 // | ordered | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002294 // | ordered | target parallel | * |
2295 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002296 // | ordered | target enter | * |
2297 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002298 // | ordered | target exit | * |
2299 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002300 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002301 // | ordered | cancellation | |
2302 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002303 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002304 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002305 // | ordered | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002306 // | ordered | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002307 // +------------------+-----------------+------------------------------------+
2308 // | atomic | parallel | |
2309 // | atomic | for | |
2310 // | atomic | for simd | |
2311 // | atomic | master | |
2312 // | atomic | critical | |
2313 // | atomic | simd | |
2314 // | atomic | sections | |
2315 // | atomic | section | |
2316 // | atomic | single | |
2317 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002318 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002319 // | atomic |parallel sections| |
2320 // | atomic | task | |
2321 // | atomic | taskyield | |
2322 // | atomic | barrier | |
2323 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002324 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002325 // | atomic | flush | |
2326 // | atomic | ordered | |
2327 // | atomic | atomic | |
2328 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002329 // | atomic | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002330 // | atomic | target parallel | |
2331 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002332 // | atomic | target enter | |
2333 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002334 // | atomic | target exit | |
2335 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002336 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002337 // | atomic | cancellation | |
2338 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002339 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002340 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002341 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002342 // | atomic | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002343 // +------------------+-----------------+------------------------------------+
2344 // | target | parallel | * |
2345 // | target | for | * |
2346 // | target | for simd | * |
2347 // | target | master | * |
2348 // | target | critical | * |
2349 // | target | simd | * |
2350 // | target | sections | * |
2351 // | target | section | * |
2352 // | target | single | * |
2353 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002354 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002355 // | target |parallel sections| * |
2356 // | target | task | * |
2357 // | target | taskyield | * |
2358 // | target | barrier | * |
2359 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002360 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002361 // | target | flush | * |
2362 // | target | ordered | * |
2363 // | target | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002364 // | target | target | |
2365 // | target | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002366 // | target | target parallel | |
2367 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002368 // | target | target enter | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002369 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002370 // | target | target exit | |
Samuel Antao72590762016-01-19 20:04:50 +00002371 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002372 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002373 // | target | cancellation | |
2374 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002375 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002376 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002377 // | target | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002378 // | target | distribute | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002379 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002380 // | target parallel | parallel | * |
2381 // | target parallel | for | * |
2382 // | target parallel | for simd | * |
2383 // | target parallel | master | * |
2384 // | target parallel | critical | * |
2385 // | target parallel | simd | * |
2386 // | target parallel | sections | * |
2387 // | target parallel | section | * |
2388 // | target parallel | single | * |
2389 // | target parallel | parallel for | * |
2390 // | target parallel |parallel for simd| * |
2391 // | target parallel |parallel sections| * |
2392 // | target parallel | task | * |
2393 // | target parallel | taskyield | * |
2394 // | target parallel | barrier | * |
2395 // | target parallel | taskwait | * |
2396 // | target parallel | taskgroup | * |
2397 // | target parallel | flush | * |
2398 // | target parallel | ordered | * |
2399 // | target parallel | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002400 // | target parallel | target | |
2401 // | target parallel | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002402 // | target parallel | target parallel | |
2403 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002404 // | target parallel | target enter | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002405 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002406 // | target parallel | target exit | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002407 // | | data | |
2408 // | target parallel | teams | |
2409 // | target parallel | cancellation | |
2410 // | | point | ! |
2411 // | target parallel | cancel | ! |
2412 // | target parallel | taskloop | * |
2413 // | target parallel | taskloop simd | * |
2414 // | target parallel | distribute | |
2415 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002416 // | target parallel | parallel | * |
2417 // | for | | |
2418 // | target parallel | for | * |
2419 // | for | | |
2420 // | target parallel | for simd | * |
2421 // | for | | |
2422 // | target parallel | master | * |
2423 // | for | | |
2424 // | target parallel | critical | * |
2425 // | for | | |
2426 // | target parallel | simd | * |
2427 // | for | | |
2428 // | target parallel | sections | * |
2429 // | for | | |
2430 // | target parallel | section | * |
2431 // | for | | |
2432 // | target parallel | single | * |
2433 // | for | | |
2434 // | target parallel | parallel for | * |
2435 // | for | | |
2436 // | target parallel |parallel for simd| * |
2437 // | for | | |
2438 // | target parallel |parallel sections| * |
2439 // | for | | |
2440 // | target parallel | task | * |
2441 // | for | | |
2442 // | target parallel | taskyield | * |
2443 // | for | | |
2444 // | target parallel | barrier | * |
2445 // | for | | |
2446 // | target parallel | taskwait | * |
2447 // | for | | |
2448 // | target parallel | taskgroup | * |
2449 // | for | | |
2450 // | target parallel | flush | * |
2451 // | for | | |
2452 // | target parallel | ordered | * |
2453 // | for | | |
2454 // | target parallel | atomic | * |
2455 // | for | | |
2456 // | target parallel | target | |
2457 // | for | | |
2458 // | target parallel | target parallel | |
2459 // | for | | |
2460 // | target parallel | target parallel | |
2461 // | for | for | |
2462 // | target parallel | target enter | |
2463 // | for | data | |
2464 // | target parallel | target exit | |
2465 // | for | data | |
2466 // | target parallel | teams | |
2467 // | for | | |
2468 // | target parallel | cancellation | |
2469 // | for | point | ! |
2470 // | target parallel | cancel | ! |
2471 // | for | | |
2472 // | target parallel | taskloop | * |
2473 // | for | | |
2474 // | target parallel | taskloop simd | * |
2475 // | for | | |
2476 // | target parallel | distribute | |
2477 // | for | | |
2478 // +------------------+-----------------+------------------------------------+
Alexey Bataev13314bf2014-10-09 04:18:56 +00002479 // | teams | parallel | * |
2480 // | teams | for | + |
2481 // | teams | for simd | + |
2482 // | teams | master | + |
2483 // | teams | critical | + |
2484 // | teams | simd | + |
2485 // | teams | sections | + |
2486 // | teams | section | + |
2487 // | teams | single | + |
2488 // | teams | parallel for | * |
2489 // | teams |parallel for simd| * |
2490 // | teams |parallel sections| * |
2491 // | teams | task | + |
2492 // | teams | taskyield | + |
2493 // | teams | barrier | + |
2494 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002495 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002496 // | teams | flush | + |
2497 // | teams | ordered | + |
2498 // | teams | atomic | + |
2499 // | teams | target | + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002500 // | teams | target parallel | + |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002501 // | teams | target parallel | + |
2502 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002503 // | teams | target enter | + |
2504 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002505 // | teams | target exit | + |
2506 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002507 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002508 // | teams | cancellation | |
2509 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002510 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002511 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002512 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002513 // | teams | distribute | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002514 // +------------------+-----------------+------------------------------------+
2515 // | taskloop | parallel | * |
2516 // | taskloop | for | + |
2517 // | taskloop | for simd | + |
2518 // | taskloop | master | + |
2519 // | taskloop | critical | * |
2520 // | taskloop | simd | * |
2521 // | taskloop | sections | + |
2522 // | taskloop | section | + |
2523 // | taskloop | single | + |
2524 // | taskloop | parallel for | * |
2525 // | taskloop |parallel for simd| * |
2526 // | taskloop |parallel sections| * |
2527 // | taskloop | task | * |
2528 // | taskloop | taskyield | * |
2529 // | taskloop | barrier | + |
2530 // | taskloop | taskwait | * |
2531 // | taskloop | taskgroup | * |
2532 // | taskloop | flush | * |
2533 // | taskloop | ordered | + |
2534 // | taskloop | atomic | * |
2535 // | taskloop | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002536 // | taskloop | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002537 // | taskloop | target parallel | * |
2538 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002539 // | taskloop | target enter | * |
2540 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002541 // | taskloop | target exit | * |
2542 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002543 // | taskloop | teams | + |
2544 // | taskloop | cancellation | |
2545 // | | point | |
2546 // | taskloop | cancel | |
2547 // | taskloop | taskloop | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002548 // | taskloop | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002549 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002550 // | taskloop simd | parallel | |
2551 // | taskloop simd | for | |
2552 // | taskloop simd | for simd | |
2553 // | taskloop simd | master | |
2554 // | taskloop simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002555 // | taskloop simd | simd | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002556 // | taskloop simd | sections | |
2557 // | taskloop simd | section | |
2558 // | taskloop simd | single | |
2559 // | taskloop simd | parallel for | |
2560 // | taskloop simd |parallel for simd| |
2561 // | taskloop simd |parallel sections| |
2562 // | taskloop simd | task | |
2563 // | taskloop simd | taskyield | |
2564 // | taskloop simd | barrier | |
2565 // | taskloop simd | taskwait | |
2566 // | taskloop simd | taskgroup | |
2567 // | taskloop simd | flush | |
2568 // | taskloop simd | ordered | + (with simd clause) |
2569 // | taskloop simd | atomic | |
2570 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002571 // | taskloop simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002572 // | taskloop simd | target parallel | |
2573 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002574 // | taskloop simd | target enter | |
2575 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002576 // | taskloop simd | target exit | |
2577 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002578 // | taskloop simd | teams | |
2579 // | taskloop simd | cancellation | |
2580 // | | point | |
2581 // | taskloop simd | cancel | |
2582 // | taskloop simd | taskloop | |
2583 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002584 // | taskloop simd | distribute | |
2585 // +------------------+-----------------+------------------------------------+
2586 // | distribute | parallel | * |
2587 // | distribute | for | * |
2588 // | distribute | for simd | * |
2589 // | distribute | master | * |
2590 // | distribute | critical | * |
2591 // | distribute | simd | * |
2592 // | distribute | sections | * |
2593 // | distribute | section | * |
2594 // | distribute | single | * |
2595 // | distribute | parallel for | * |
2596 // | distribute |parallel for simd| * |
2597 // | distribute |parallel sections| * |
2598 // | distribute | task | * |
2599 // | distribute | taskyield | * |
2600 // | distribute | barrier | * |
2601 // | distribute | taskwait | * |
2602 // | distribute | taskgroup | * |
2603 // | distribute | flush | * |
2604 // | distribute | ordered | + |
2605 // | distribute | atomic | * |
2606 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002607 // | distribute | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002608 // | distribute | target parallel | |
2609 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002610 // | distribute | target enter | |
2611 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002612 // | distribute | target exit | |
2613 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002614 // | distribute | teams | |
2615 // | distribute | cancellation | + |
2616 // | | point | |
2617 // | distribute | cancel | + |
2618 // | distribute | taskloop | * |
2619 // | distribute | taskloop simd | * |
2620 // | distribute | distribute | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002621 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002622 if (Stack->getCurScope()) {
2623 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002624 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002625 bool NestingProhibited = false;
2626 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002627 enum {
2628 NoRecommend,
2629 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002630 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002631 ShouldBeInTargetRegion,
2632 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002633 } Recommend = NoRecommend;
Alexey Bataev1f092212016-02-02 04:59:52 +00002634 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered &&
2635 CurrentRegion != OMPD_simd) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002636 // OpenMP [2.16, Nesting of Regions]
2637 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002638 // OpenMP [2.8.1,simd Construct, Restrictions]
2639 // An ordered construct with the simd clause is the only OpenMP construct
2640 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002641 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2642 return true;
2643 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002644 if (ParentRegion == OMPD_atomic) {
2645 // OpenMP [2.16, Nesting of Regions]
2646 // OpenMP constructs may not be nested inside an atomic region.
2647 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2648 return true;
2649 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002650 if (CurrentRegion == OMPD_section) {
2651 // OpenMP [2.7.2, sections Construct, Restrictions]
2652 // Orphaned section directives are prohibited. That is, the section
2653 // directives must appear within the sections construct and must not be
2654 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002655 if (ParentRegion != OMPD_sections &&
2656 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002657 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2658 << (ParentRegion != OMPD_unknown)
2659 << getOpenMPDirectiveName(ParentRegion);
2660 return true;
2661 }
2662 return false;
2663 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002664 // Allow some constructs to be orphaned (they could be used in functions,
2665 // called from OpenMP regions with the required preconditions).
2666 if (ParentRegion == OMPD_unknown)
2667 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002668 if (CurrentRegion == OMPD_cancellation_point ||
2669 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002670 // OpenMP [2.16, Nesting of Regions]
2671 // A cancellation point construct for which construct-type-clause is
2672 // taskgroup must be nested inside a task construct. A cancellation
2673 // point construct for which construct-type-clause is not taskgroup must
2674 // be closely nested inside an OpenMP construct that matches the type
2675 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002676 // A cancel construct for which construct-type-clause is taskgroup must be
2677 // nested inside a task construct. A cancel construct for which
2678 // construct-type-clause is not taskgroup must be closely nested inside an
2679 // OpenMP construct that matches the type specified in
2680 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002681 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002682 !((CancelRegion == OMPD_parallel &&
2683 (ParentRegion == OMPD_parallel ||
2684 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002685 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002686 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
2687 ParentRegion == OMPD_target_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002688 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2689 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002690 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2691 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002692 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002693 // OpenMP [2.16, Nesting of Regions]
2694 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002695 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002696 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002697 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002698 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002699 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2700 // OpenMP [2.16, Nesting of Regions]
2701 // A critical region may not be nested (closely or otherwise) inside a
2702 // critical region with the same name. Note that this restriction is not
2703 // sufficient to prevent deadlock.
2704 SourceLocation PreviousCriticalLoc;
2705 bool DeadLock =
2706 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2707 OpenMPDirectiveKind K,
2708 const DeclarationNameInfo &DNI,
2709 SourceLocation Loc)
2710 ->bool {
2711 if (K == OMPD_critical &&
2712 DNI.getName() == CurrentName.getName()) {
2713 PreviousCriticalLoc = Loc;
2714 return true;
2715 } else
2716 return false;
2717 },
2718 false /* skip top directive */);
2719 if (DeadLock) {
2720 SemaRef.Diag(StartLoc,
2721 diag::err_omp_prohibited_region_critical_same_name)
2722 << CurrentName.getName();
2723 if (PreviousCriticalLoc.isValid())
2724 SemaRef.Diag(PreviousCriticalLoc,
2725 diag::note_omp_previous_critical_region);
2726 return true;
2727 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002728 } else if (CurrentRegion == OMPD_barrier) {
2729 // OpenMP [2.16, Nesting of Regions]
2730 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002731 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002732 NestingProhibited =
2733 isOpenMPWorksharingDirective(ParentRegion) ||
2734 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002735 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002736 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musman80c22892014-07-17 08:54:58 +00002737 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002738 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002739 // OpenMP [2.16, Nesting of Regions]
2740 // A worksharing region may not be closely nested inside a worksharing,
2741 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002742 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00002743 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002744 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002745 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002746 isOpenMPTaskLoopDirective(ParentRegion);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002747 Recommend = ShouldBeInParallelRegion;
2748 } else if (CurrentRegion == OMPD_ordered) {
2749 // OpenMP [2.16, Nesting of Regions]
2750 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002751 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002752 // An ordered region must be closely nested inside a loop region (or
2753 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002754 // OpenMP [2.8.1,simd Construct, Restrictions]
2755 // An ordered construct with the simd clause is the only OpenMP construct
2756 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002757 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00002758 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002759 isOpenMPTaskLoopDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002760 !(isOpenMPSimdDirective(ParentRegion) ||
2761 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002762 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002763 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2764 // OpenMP [2.16, Nesting of Regions]
2765 // If specified, a teams construct must be contained within a target
2766 // construct.
2767 NestingProhibited = ParentRegion != OMPD_target;
2768 Recommend = ShouldBeInTargetRegion;
2769 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2770 }
2771 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2772 // OpenMP [2.16, Nesting of Regions]
2773 // distribute, parallel, parallel sections, parallel workshare, and the
2774 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2775 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002776 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2777 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002778 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002779 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002780 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2781 // OpenMP 4.5 [2.17 Nesting of Regions]
2782 // The region associated with the distribute construct must be strictly
2783 // nested inside a teams region
2784 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2785 Recommend = ShouldBeInTeamsRegion;
2786 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002787 if (!NestingProhibited &&
2788 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2789 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2790 // OpenMP 4.5 [2.17 Nesting of Regions]
2791 // If a target, target update, target data, target enter data, or
2792 // target exit data construct is encountered during execution of a
2793 // target region, the behavior is unspecified.
2794 NestingProhibited = Stack->hasDirective(
2795 [&OffendingRegion](OpenMPDirectiveKind K,
2796 const DeclarationNameInfo &DNI,
2797 SourceLocation Loc) -> bool {
2798 if (isOpenMPTargetExecutionDirective(K)) {
2799 OffendingRegion = K;
2800 return true;
2801 } else
2802 return false;
2803 },
2804 false /* don't skip top directive */);
2805 CloseNesting = false;
2806 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002807 if (NestingProhibited) {
2808 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002809 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2810 << Recommend << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002811 return true;
2812 }
2813 }
2814 return false;
2815}
2816
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002817static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2818 ArrayRef<OMPClause *> Clauses,
2819 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2820 bool ErrorFound = false;
2821 unsigned NamedModifiersNumber = 0;
2822 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2823 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002824 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002825 for (const auto *C : Clauses) {
2826 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2827 // At most one if clause without a directive-name-modifier can appear on
2828 // the directive.
2829 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2830 if (FoundNameModifiers[CurNM]) {
2831 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2832 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2833 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2834 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002835 } else if (CurNM != OMPD_unknown) {
2836 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002837 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002838 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002839 FoundNameModifiers[CurNM] = IC;
2840 if (CurNM == OMPD_unknown)
2841 continue;
2842 // Check if the specified name modifier is allowed for the current
2843 // directive.
2844 // At most one if clause with the particular directive-name-modifier can
2845 // appear on the directive.
2846 bool MatchFound = false;
2847 for (auto NM : AllowedNameModifiers) {
2848 if (CurNM == NM) {
2849 MatchFound = true;
2850 break;
2851 }
2852 }
2853 if (!MatchFound) {
2854 S.Diag(IC->getNameModifierLoc(),
2855 diag::err_omp_wrong_if_directive_name_modifier)
2856 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2857 ErrorFound = true;
2858 }
2859 }
2860 }
2861 // If any if clause on the directive includes a directive-name-modifier then
2862 // all if clauses on the directive must include a directive-name-modifier.
2863 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2864 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2865 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2866 diag::err_omp_no_more_if_clause);
2867 } else {
2868 std::string Values;
2869 std::string Sep(", ");
2870 unsigned AllowedCnt = 0;
2871 unsigned TotalAllowedNum =
2872 AllowedNameModifiers.size() - NamedModifiersNumber;
2873 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2874 ++Cnt) {
2875 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2876 if (!FoundNameModifiers[NM]) {
2877 Values += "'";
2878 Values += getOpenMPDirectiveName(NM);
2879 Values += "'";
2880 if (AllowedCnt + 2 == TotalAllowedNum)
2881 Values += " or ";
2882 else if (AllowedCnt + 1 != TotalAllowedNum)
2883 Values += Sep;
2884 ++AllowedCnt;
2885 }
2886 }
2887 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2888 diag::err_omp_unnamed_if_clause)
2889 << (TotalAllowedNum > 1) << Values;
2890 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002891 for (auto Loc : NameModifierLoc) {
2892 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2893 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002894 ErrorFound = true;
2895 }
2896 return ErrorFound;
2897}
2898
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002899StmtResult Sema::ActOnOpenMPExecutableDirective(
2900 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2901 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2902 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002903 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002904 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2905 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002906 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002907
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002908 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002909 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002910 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002911 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002912 if (AStmt) {
2913 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2914
2915 // Check default data sharing attributes for referenced variables.
2916 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2917 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2918 if (DSAChecker.isErrorFound())
2919 return StmtError();
2920 // Generate list of implicitly defined firstprivate variables.
2921 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002922
2923 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2924 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2925 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2926 SourceLocation(), SourceLocation())) {
2927 ClausesWithImplicit.push_back(Implicit);
2928 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2929 DSAChecker.getImplicitFirstprivate().size();
2930 } else
2931 ErrorFound = true;
2932 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002933 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002934
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002935 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002936 switch (Kind) {
2937 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002938 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2939 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002940 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002941 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002942 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002943 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2944 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002945 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002946 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002947 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2948 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002949 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002950 case OMPD_for_simd:
2951 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2952 EndLoc, VarsWithInheritedDSA);
2953 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002954 case OMPD_sections:
2955 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2956 EndLoc);
2957 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002958 case OMPD_section:
2959 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002960 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002961 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2962 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002963 case OMPD_single:
2964 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2965 EndLoc);
2966 break;
Alexander Musman80c22892014-07-17 08:54:58 +00002967 case OMPD_master:
2968 assert(ClausesWithImplicit.empty() &&
2969 "No clauses are allowed for 'omp master' directive");
2970 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2971 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002972 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00002973 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
2974 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002975 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002976 case OMPD_parallel_for:
2977 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2978 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002979 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002980 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002981 case OMPD_parallel_for_simd:
2982 Res = ActOnOpenMPParallelForSimdDirective(
2983 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002984 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00002985 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002986 case OMPD_parallel_sections:
2987 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2988 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002989 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002990 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002991 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002992 Res =
2993 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002994 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002995 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00002996 case OMPD_taskyield:
2997 assert(ClausesWithImplicit.empty() &&
2998 "No clauses are allowed for 'omp taskyield' directive");
2999 assert(AStmt == nullptr &&
3000 "No associated statement allowed for 'omp taskyield' directive");
3001 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3002 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003003 case OMPD_barrier:
3004 assert(ClausesWithImplicit.empty() &&
3005 "No clauses are allowed for 'omp barrier' directive");
3006 assert(AStmt == nullptr &&
3007 "No associated statement allowed for 'omp barrier' directive");
3008 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3009 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003010 case OMPD_taskwait:
3011 assert(ClausesWithImplicit.empty() &&
3012 "No clauses are allowed for 'omp taskwait' directive");
3013 assert(AStmt == nullptr &&
3014 "No associated statement allowed for 'omp taskwait' directive");
3015 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3016 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003017 case OMPD_taskgroup:
3018 assert(ClausesWithImplicit.empty() &&
3019 "No clauses are allowed for 'omp taskgroup' directive");
3020 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3021 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003022 case OMPD_flush:
3023 assert(AStmt == nullptr &&
3024 "No associated statement allowed for 'omp flush' directive");
3025 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3026 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003027 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003028 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3029 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003030 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003031 case OMPD_atomic:
3032 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3033 EndLoc);
3034 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003035 case OMPD_teams:
3036 Res =
3037 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3038 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003039 case OMPD_target:
3040 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3041 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003042 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003043 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003044 case OMPD_target_parallel:
3045 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3046 StartLoc, EndLoc);
3047 AllowedNameModifiers.push_back(OMPD_target);
3048 AllowedNameModifiers.push_back(OMPD_parallel);
3049 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003050 case OMPD_target_parallel_for:
3051 Res = ActOnOpenMPTargetParallelForDirective(
3052 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3053 AllowedNameModifiers.push_back(OMPD_target);
3054 AllowedNameModifiers.push_back(OMPD_parallel);
3055 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003056 case OMPD_cancellation_point:
3057 assert(ClausesWithImplicit.empty() &&
3058 "No clauses are allowed for 'omp cancellation point' directive");
3059 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3060 "cancellation point' directive");
3061 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3062 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003063 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003064 assert(AStmt == nullptr &&
3065 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003066 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3067 CancelRegion);
3068 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003069 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003070 case OMPD_target_data:
3071 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3072 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003073 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003074 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003075 case OMPD_target_enter_data:
3076 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3077 EndLoc);
3078 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3079 break;
Samuel Antao72590762016-01-19 20:04:50 +00003080 case OMPD_target_exit_data:
3081 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3082 EndLoc);
3083 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3084 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003085 case OMPD_taskloop:
3086 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3087 EndLoc, VarsWithInheritedDSA);
3088 AllowedNameModifiers.push_back(OMPD_taskloop);
3089 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003090 case OMPD_taskloop_simd:
3091 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3092 EndLoc, VarsWithInheritedDSA);
3093 AllowedNameModifiers.push_back(OMPD_taskloop);
3094 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003095 case OMPD_distribute:
3096 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3097 EndLoc, VarsWithInheritedDSA);
3098 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003099 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003100 llvm_unreachable("OpenMP Directive is not allowed");
3101 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003102 llvm_unreachable("Unknown OpenMP directive");
3103 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003104
Alexey Bataev4acb8592014-07-07 13:01:15 +00003105 for (auto P : VarsWithInheritedDSA) {
3106 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3107 << P.first << P.second->getSourceRange();
3108 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003109 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3110
3111 if (!AllowedNameModifiers.empty())
3112 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3113 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003114
Alexey Bataeved09d242014-05-28 05:53:51 +00003115 if (ErrorFound)
3116 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003117 return Res;
3118}
3119
3120StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3121 Stmt *AStmt,
3122 SourceLocation StartLoc,
3123 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003124 if (!AStmt)
3125 return StmtError();
3126
Alexey Bataev9959db52014-05-06 10:08:46 +00003127 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3128 // 1.2.2 OpenMP Language Terminology
3129 // Structured block - An executable statement with a single entry at the
3130 // top and a single exit at the bottom.
3131 // The point of exit cannot be a branch out of the structured block.
3132 // longjmp() and throw() must not violate the entry/exit criteria.
3133 CS->getCapturedDecl()->setNothrow();
3134
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003135 getCurFunction()->setHasBranchProtectedScope();
3136
Alexey Bataev25e5b442015-09-15 12:52:43 +00003137 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3138 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003139}
3140
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003141namespace {
3142/// \brief Helper class for checking canonical form of the OpenMP loops and
3143/// extracting iteration space of each loop in the loop nest, that will be used
3144/// for IR generation.
3145class OpenMPIterationSpaceChecker {
3146 /// \brief Reference to Sema.
3147 Sema &SemaRef;
3148 /// \brief A location for diagnostics (when there is no some better location).
3149 SourceLocation DefaultLoc;
3150 /// \brief A location for diagnostics (when increment is not compatible).
3151 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003152 /// \brief A source location for referring to loop init later.
3153 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003154 /// \brief A source location for referring to condition later.
3155 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003156 /// \brief A source location for referring to increment later.
3157 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003158 /// \brief Loop variable.
3159 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003160 /// \brief Reference to loop variable.
3161 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003162 /// \brief Lower bound (initializer for the var).
3163 Expr *LB;
3164 /// \brief Upper bound.
3165 Expr *UB;
3166 /// \brief Loop step (increment).
3167 Expr *Step;
3168 /// \brief This flag is true when condition is one of:
3169 /// Var < UB
3170 /// Var <= UB
3171 /// UB > Var
3172 /// UB >= Var
3173 bool TestIsLessOp;
3174 /// \brief This flag is true when condition is strict ( < or > ).
3175 bool TestIsStrictOp;
3176 /// \brief This flag is true when step is subtracted on each iteration.
3177 bool SubtractStep;
3178
3179public:
3180 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
3181 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003182 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
3183 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003184 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
3185 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003186 /// \brief Check init-expr for canonical loop form and save loop counter
3187 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00003188 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003189 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3190 /// for less/greater and for strict/non-strict comparison.
3191 bool CheckCond(Expr *S);
3192 /// \brief Check incr-expr for canonical loop form and return true if it
3193 /// does not conform, otherwise save loop step (#Step).
3194 bool CheckInc(Expr *S);
3195 /// \brief Return the loop counter variable.
3196 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003197 /// \brief Return the reference expression to loop counter variable.
3198 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003199 /// \brief Source range of the loop init.
3200 SourceRange GetInitSrcRange() const { return InitSrcRange; }
3201 /// \brief Source range of the loop condition.
3202 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3203 /// \brief Source range of the loop increment.
3204 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3205 /// \brief True if the step should be subtracted.
3206 bool ShouldSubtractStep() const { return SubtractStep; }
3207 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003208 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003209 /// \brief Build the precondition expression for the loops.
3210 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003211 /// \brief Build reference expression to the counter be used for codegen.
3212 Expr *BuildCounterVar() const;
Alexey Bataeva8899172015-08-06 12:30:57 +00003213 /// \brief Build reference expression to the private counter be used for
3214 /// codegen.
3215 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003216 /// \brief Build initization of the counter be used for codegen.
3217 Expr *BuildCounterInit() const;
3218 /// \brief Build step of the counter be used for codegen.
3219 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003220 /// \brief Return true if any expression is dependent.
3221 bool Dependent() const;
3222
3223private:
3224 /// \brief Check the right-hand side of an assignment in the increment
3225 /// expression.
3226 bool CheckIncRHS(Expr *RHS);
3227 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003228 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003229 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00003230 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003231 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003232 /// \brief Helper to set loop increment.
3233 bool SetStep(Expr *NewStep, bool Subtract);
3234};
3235
3236bool OpenMPIterationSpaceChecker::Dependent() const {
3237 if (!Var) {
3238 assert(!LB && !UB && !Step);
3239 return false;
3240 }
3241 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
3242 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
3243}
3244
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003245template <typename T>
3246static T *getExprAsWritten(T *E) {
3247 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
3248 E = ExprTemp->getSubExpr();
3249
3250 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
3251 E = MTE->GetTemporaryExpr();
3252
3253 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
3254 E = Binder->getSubExpr();
3255
3256 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
3257 E = ICE->getSubExprAsWritten();
3258 return E->IgnoreParens();
3259}
3260
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003261bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
3262 DeclRefExpr *NewVarRefExpr,
3263 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003264 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003265 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
3266 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003267 if (!NewVar || !NewLB)
3268 return true;
3269 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003270 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003271 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3272 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003273 if ((Ctor->isCopyOrMoveConstructor() ||
3274 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3275 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003276 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003277 LB = NewLB;
3278 return false;
3279}
3280
3281bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00003282 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003283 // State consistency checking to ensure correct usage.
3284 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
3285 !TestIsLessOp && !TestIsStrictOp);
3286 if (!NewUB)
3287 return true;
3288 UB = NewUB;
3289 TestIsLessOp = LessOp;
3290 TestIsStrictOp = StrictOp;
3291 ConditionSrcRange = SR;
3292 ConditionLoc = SL;
3293 return false;
3294}
3295
3296bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3297 // State consistency checking to ensure correct usage.
3298 assert(Var != nullptr && LB != nullptr && Step == nullptr);
3299 if (!NewStep)
3300 return true;
3301 if (!NewStep->isValueDependent()) {
3302 // Check that the step is integer expression.
3303 SourceLocation StepLoc = NewStep->getLocStart();
3304 ExprResult Val =
3305 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
3306 if (Val.isInvalid())
3307 return true;
3308 NewStep = Val.get();
3309
3310 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3311 // If test-expr is of form var relational-op b and relational-op is < or
3312 // <= then incr-expr must cause var to increase on each iteration of the
3313 // loop. If test-expr is of form var relational-op b and relational-op is
3314 // > or >= then incr-expr must cause var to decrease on each iteration of
3315 // the loop.
3316 // If test-expr is of form b relational-op var and relational-op is < or
3317 // <= then incr-expr must cause var to decrease on each iteration of the
3318 // loop. If test-expr is of form b relational-op var and relational-op is
3319 // > or >= then incr-expr must cause var to increase on each iteration of
3320 // the loop.
3321 llvm::APSInt Result;
3322 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3323 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3324 bool IsConstNeg =
3325 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003326 bool IsConstPos =
3327 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003328 bool IsConstZero = IsConstant && !Result.getBoolValue();
3329 if (UB && (IsConstZero ||
3330 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00003331 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003332 SemaRef.Diag(NewStep->getExprLoc(),
3333 diag::err_omp_loop_incr_not_compatible)
3334 << Var << TestIsLessOp << NewStep->getSourceRange();
3335 SemaRef.Diag(ConditionLoc,
3336 diag::note_omp_loop_cond_requres_compatible_incr)
3337 << TestIsLessOp << ConditionSrcRange;
3338 return true;
3339 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003340 if (TestIsLessOp == Subtract) {
3341 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
3342 NewStep).get();
3343 Subtract = !Subtract;
3344 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003345 }
3346
3347 Step = NewStep;
3348 SubtractStep = Subtract;
3349 return false;
3350}
3351
Alexey Bataev9c821032015-04-30 04:23:23 +00003352bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003353 // Check init-expr for canonical loop form and save loop counter
3354 // variable - #Var and its initialization value - #LB.
3355 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3356 // var = lb
3357 // integer-type var = lb
3358 // random-access-iterator-type var = lb
3359 // pointer-type var = lb
3360 //
3361 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003362 if (EmitDiags) {
3363 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3364 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003365 return true;
3366 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003367 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003368 if (Expr *E = dyn_cast<Expr>(S))
3369 S = E->IgnoreParens();
3370 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3371 if (BO->getOpcode() == BO_Assign)
3372 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003373 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003374 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003375 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
3376 if (DS->isSingleDecl()) {
3377 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00003378 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003379 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00003380 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003381 SemaRef.Diag(S->getLocStart(),
3382 diag::ext_omp_loop_not_canonical_init)
3383 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003384 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003385 }
3386 }
3387 }
3388 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
3389 if (CE->getOperator() == OO_Equal)
3390 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003391 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
3392 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003393
Alexey Bataev9c821032015-04-30 04:23:23 +00003394 if (EmitDiags) {
3395 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3396 << S->getSourceRange();
3397 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003398 return true;
3399}
3400
Alexey Bataev23b69422014-06-18 07:08:49 +00003401/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003402/// variable (which may be the loop variable) if possible.
3403static const VarDecl *GetInitVarDecl(const Expr *E) {
3404 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00003405 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003406 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003407 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3408 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003409 if ((Ctor->isCopyOrMoveConstructor() ||
3410 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3411 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003412 E = CE->getArg(0)->IgnoreParenImpCasts();
3413 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
3414 if (!DRE)
3415 return nullptr;
3416 return dyn_cast<VarDecl>(DRE->getDecl());
3417}
3418
3419bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3420 // Check test-expr for canonical form, save upper-bound UB, flags for
3421 // less/greater and for strict/non-strict comparison.
3422 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3423 // var relational-op b
3424 // b relational-op var
3425 //
3426 if (!S) {
3427 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
3428 return true;
3429 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003430 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003431 SourceLocation CondLoc = S->getLocStart();
3432 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3433 if (BO->isRelationalOp()) {
3434 if (GetInitVarDecl(BO->getLHS()) == Var)
3435 return SetUB(BO->getRHS(),
3436 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3437 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3438 BO->getSourceRange(), BO->getOperatorLoc());
3439 if (GetInitVarDecl(BO->getRHS()) == Var)
3440 return SetUB(BO->getLHS(),
3441 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3442 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3443 BO->getSourceRange(), BO->getOperatorLoc());
3444 }
3445 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3446 if (CE->getNumArgs() == 2) {
3447 auto Op = CE->getOperator();
3448 switch (Op) {
3449 case OO_Greater:
3450 case OO_GreaterEqual:
3451 case OO_Less:
3452 case OO_LessEqual:
3453 if (GetInitVarDecl(CE->getArg(0)) == Var)
3454 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3455 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3456 CE->getOperatorLoc());
3457 if (GetInitVarDecl(CE->getArg(1)) == Var)
3458 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3459 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3460 CE->getOperatorLoc());
3461 break;
3462 default:
3463 break;
3464 }
3465 }
3466 }
3467 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
3468 << S->getSourceRange() << Var;
3469 return true;
3470}
3471
3472bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3473 // RHS of canonical loop form increment can be:
3474 // var + incr
3475 // incr + var
3476 // var - incr
3477 //
3478 RHS = RHS->IgnoreParenImpCasts();
3479 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3480 if (BO->isAdditiveOp()) {
3481 bool IsAdd = BO->getOpcode() == BO_Add;
3482 if (GetInitVarDecl(BO->getLHS()) == Var)
3483 return SetStep(BO->getRHS(), !IsAdd);
3484 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
3485 return SetStep(BO->getLHS(), false);
3486 }
3487 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3488 bool IsAdd = CE->getOperator() == OO_Plus;
3489 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
3490 if (GetInitVarDecl(CE->getArg(0)) == Var)
3491 return SetStep(CE->getArg(1), !IsAdd);
3492 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
3493 return SetStep(CE->getArg(0), false);
3494 }
3495 }
3496 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3497 << RHS->getSourceRange() << Var;
3498 return true;
3499}
3500
3501bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3502 // Check incr-expr for canonical loop form and return true if it
3503 // does not conform.
3504 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3505 // ++var
3506 // var++
3507 // --var
3508 // var--
3509 // var += incr
3510 // var -= incr
3511 // var = var + incr
3512 // var = incr + var
3513 // var = var - incr
3514 //
3515 if (!S) {
3516 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
3517 return true;
3518 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003519 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003520 S = S->IgnoreParens();
3521 if (auto UO = dyn_cast<UnaryOperator>(S)) {
3522 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
3523 return SetStep(
3524 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3525 (UO->isDecrementOp() ? -1 : 1)).get(),
3526 false);
3527 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3528 switch (BO->getOpcode()) {
3529 case BO_AddAssign:
3530 case BO_SubAssign:
3531 if (GetInitVarDecl(BO->getLHS()) == Var)
3532 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3533 break;
3534 case BO_Assign:
3535 if (GetInitVarDecl(BO->getLHS()) == Var)
3536 return CheckIncRHS(BO->getRHS());
3537 break;
3538 default:
3539 break;
3540 }
3541 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3542 switch (CE->getOperator()) {
3543 case OO_PlusPlus:
3544 case OO_MinusMinus:
3545 if (GetInitVarDecl(CE->getArg(0)) == Var)
3546 return SetStep(
3547 SemaRef.ActOnIntegerConstant(
3548 CE->getLocStart(),
3549 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3550 false);
3551 break;
3552 case OO_PlusEqual:
3553 case OO_MinusEqual:
3554 if (GetInitVarDecl(CE->getArg(0)) == Var)
3555 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3556 break;
3557 case OO_Equal:
3558 if (GetInitVarDecl(CE->getArg(0)) == Var)
3559 return CheckIncRHS(CE->getArg(1));
3560 break;
3561 default:
3562 break;
3563 }
3564 }
3565 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3566 << S->getSourceRange() << Var;
3567 return true;
3568}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003569
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003570namespace {
3571// Transform variables declared in GNU statement expressions to new ones to
3572// avoid crash on codegen.
3573class TransformToNewDefs : public TreeTransform<TransformToNewDefs> {
3574 typedef TreeTransform<TransformToNewDefs> BaseTransform;
3575
3576public:
3577 TransformToNewDefs(Sema &SemaRef) : BaseTransform(SemaRef) {}
3578
3579 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
3580 if (auto *VD = cast<VarDecl>(D))
3581 if (!isa<ParmVarDecl>(D) && !isa<VarTemplateSpecializationDecl>(D) &&
3582 !isa<ImplicitParamDecl>(D)) {
3583 auto *NewVD = VarDecl::Create(
3584 SemaRef.Context, VD->getDeclContext(), VD->getLocStart(),
3585 VD->getLocation(), VD->getIdentifier(), VD->getType(),
3586 VD->getTypeSourceInfo(), VD->getStorageClass());
3587 NewVD->setTSCSpec(VD->getTSCSpec());
3588 NewVD->setInit(VD->getInit());
3589 NewVD->setInitStyle(VD->getInitStyle());
3590 NewVD->setExceptionVariable(VD->isExceptionVariable());
3591 NewVD->setNRVOVariable(VD->isNRVOVariable());
3592 NewVD->setCXXForRangeDecl(VD->isInExternCXXContext());
3593 NewVD->setConstexpr(VD->isConstexpr());
3594 NewVD->setInitCapture(VD->isInitCapture());
3595 NewVD->setPreviousDeclInSameBlockScope(
3596 VD->isPreviousDeclInSameBlockScope());
3597 VD->getDeclContext()->addHiddenDecl(NewVD);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003598 if (VD->hasAttrs())
3599 NewVD->setAttrs(VD->getAttrs());
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003600 transformedLocalDecl(VD, NewVD);
3601 return NewVD;
3602 }
3603 return BaseTransform::TransformDefinition(Loc, D);
3604 }
3605
3606 ExprResult TransformDeclRefExpr(DeclRefExpr *E) {
3607 if (auto *NewD = TransformDecl(E->getExprLoc(), E->getDecl()))
3608 if (E->getDecl() != NewD) {
3609 NewD->setReferenced();
3610 NewD->markUsed(SemaRef.Context);
3611 return DeclRefExpr::Create(
3612 SemaRef.Context, E->getQualifierLoc(), E->getTemplateKeywordLoc(),
3613 cast<ValueDecl>(NewD), E->refersToEnclosingVariableOrCapture(),
3614 E->getNameInfo(), E->getType(), E->getValueKind());
3615 }
3616 return BaseTransform::TransformDeclRefExpr(E);
3617 }
3618};
3619}
3620
Alexander Musmana5f070a2014-10-01 06:03:56 +00003621/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003622Expr *
3623OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
3624 const bool LimitedType) const {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003625 TransformToNewDefs Transform(SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003626 ExprResult Diff;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003627 auto VarType = Var->getType().getNonReferenceType();
3628 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003629 SemaRef.getLangOpts().CPlusPlus) {
3630 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003631 auto *UBExpr = TestIsLessOp ? UB : LB;
3632 auto *LBExpr = TestIsLessOp ? LB : UB;
3633 Expr *Upper = Transform.TransformExpr(UBExpr).get();
3634 Expr *Lower = Transform.TransformExpr(LBExpr).get();
3635 if (!Upper || !Lower)
3636 return nullptr;
3637 Upper = SemaRef.PerformImplicitConversion(Upper, UBExpr->getType(),
3638 Sema::AA_Converting,
3639 /*AllowExplicit=*/true)
3640 .get();
3641 Lower = SemaRef.PerformImplicitConversion(Lower, LBExpr->getType(),
3642 Sema::AA_Converting,
3643 /*AllowExplicit=*/true)
3644 .get();
3645 if (!Upper || !Lower)
3646 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003647
3648 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3649
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003650 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003651 // BuildBinOp already emitted error, this one is to point user to upper
3652 // and lower bound, and to tell what is passed to 'operator-'.
3653 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3654 << Upper->getSourceRange() << Lower->getSourceRange();
3655 return nullptr;
3656 }
3657 }
3658
3659 if (!Diff.isUsable())
3660 return nullptr;
3661
3662 // Upper - Lower [- 1]
3663 if (TestIsStrictOp)
3664 Diff = SemaRef.BuildBinOp(
3665 S, DefaultLoc, BO_Sub, Diff.get(),
3666 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3667 if (!Diff.isUsable())
3668 return nullptr;
3669
3670 // Upper - Lower [- 1] + Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003671 auto NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3672 if (NewStep.isInvalid())
3673 return nullptr;
3674 NewStep = SemaRef.PerformImplicitConversion(
3675 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3676 /*AllowExplicit=*/true);
3677 if (NewStep.isInvalid())
3678 return nullptr;
3679 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003680 if (!Diff.isUsable())
3681 return nullptr;
3682
3683 // Parentheses (for dumping/debugging purposes only).
3684 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3685 if (!Diff.isUsable())
3686 return nullptr;
3687
3688 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003689 NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3690 if (NewStep.isInvalid())
3691 return nullptr;
3692 NewStep = SemaRef.PerformImplicitConversion(
3693 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3694 /*AllowExplicit=*/true);
3695 if (NewStep.isInvalid())
3696 return nullptr;
3697 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003698 if (!Diff.isUsable())
3699 return nullptr;
3700
Alexander Musman174b3ca2014-10-06 11:16:29 +00003701 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003702 QualType Type = Diff.get()->getType();
3703 auto &C = SemaRef.Context;
3704 bool UseVarType = VarType->hasIntegerRepresentation() &&
3705 C.getTypeSize(Type) > C.getTypeSize(VarType);
3706 if (!Type->isIntegerType() || UseVarType) {
3707 unsigned NewSize =
3708 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
3709 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
3710 : Type->hasSignedIntegerRepresentation();
3711 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
3712 Diff = SemaRef.PerformImplicitConversion(
3713 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
3714 if (!Diff.isUsable())
3715 return nullptr;
3716 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003717 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00003718 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
3719 if (NewSize != C.getTypeSize(Type)) {
3720 if (NewSize < C.getTypeSize(Type)) {
3721 assert(NewSize == 64 && "incorrect loop var size");
3722 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
3723 << InitSrcRange << ConditionSrcRange;
3724 }
3725 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003726 NewSize, Type->hasSignedIntegerRepresentation() ||
3727 C.getTypeSize(Type) < NewSize);
Alexander Musman174b3ca2014-10-06 11:16:29 +00003728 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
3729 Sema::AA_Converting, true);
3730 if (!Diff.isUsable())
3731 return nullptr;
3732 }
3733 }
3734
Alexander Musmana5f070a2014-10-01 06:03:56 +00003735 return Diff.get();
3736}
3737
Alexey Bataev62dbb972015-04-22 11:59:37 +00003738Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
3739 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
3740 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3741 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003742 TransformToNewDefs Transform(SemaRef);
3743
3744 auto NewLB = Transform.TransformExpr(LB);
3745 auto NewUB = Transform.TransformExpr(UB);
3746 if (NewLB.isInvalid() || NewUB.isInvalid())
3747 return Cond;
3748 NewLB = SemaRef.PerformImplicitConversion(NewLB.get(), LB->getType(),
3749 Sema::AA_Converting,
3750 /*AllowExplicit=*/true);
3751 NewUB = SemaRef.PerformImplicitConversion(NewUB.get(), UB->getType(),
3752 Sema::AA_Converting,
3753 /*AllowExplicit=*/true);
3754 if (NewLB.isInvalid() || NewUB.isInvalid())
3755 return Cond;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003756 auto CondExpr = SemaRef.BuildBinOp(
3757 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
3758 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003759 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003760 if (CondExpr.isUsable()) {
3761 CondExpr = SemaRef.PerformImplicitConversion(
3762 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
3763 /*AllowExplicit=*/true);
3764 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00003765 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3766 // Otherwise use original loop conditon and evaluate it in runtime.
3767 return CondExpr.isUsable() ? CondExpr.get() : Cond;
3768}
3769
Alexander Musmana5f070a2014-10-01 06:03:56 +00003770/// \brief Build reference expression to the counter be used for codegen.
3771Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataeva8899172015-08-06 12:30:57 +00003772 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
3773 DefaultLoc);
3774}
3775
3776Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
3777 if (Var && !Var->isInvalidDecl()) {
3778 auto Type = Var->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003779 auto *PrivateVar =
3780 buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName(),
3781 Var->hasAttrs() ? &Var->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00003782 if (PrivateVar->isInvalidDecl())
3783 return nullptr;
3784 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
3785 }
3786 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003787}
3788
3789/// \brief Build initization of the counter be used for codegen.
3790Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
3791
3792/// \brief Build step of the counter be used for codegen.
3793Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
3794
3795/// \brief Iteration space of a single for loop.
3796struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003797 /// \brief Condition of the loop.
3798 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003799 /// \brief This expression calculates the number of iterations in the loop.
3800 /// It is always possible to calculate it before starting the loop.
3801 Expr *NumIterations;
3802 /// \brief The loop counter variable.
3803 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003804 /// \brief Private loop counter variable.
3805 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003806 /// \brief This is initializer for the initial value of #CounterVar.
3807 Expr *CounterInit;
3808 /// \brief This is step for the #CounterVar used to generate its update:
3809 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
3810 Expr *CounterStep;
3811 /// \brief Should step be subtracted?
3812 bool Subtract;
3813 /// \brief Source range of the loop init.
3814 SourceRange InitSrcRange;
3815 /// \brief Source range of the loop condition.
3816 SourceRange CondSrcRange;
3817 /// \brief Source range of the loop increment.
3818 SourceRange IncSrcRange;
3819};
3820
Alexey Bataev23b69422014-06-18 07:08:49 +00003821} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003822
Alexey Bataev9c821032015-04-30 04:23:23 +00003823void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
3824 assert(getLangOpts().OpenMP && "OpenMP is not active.");
3825 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003826 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
3827 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00003828 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
3829 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003830 if (!ISC.CheckInit(Init, /*EmitDiags=*/false))
Alexey Bataev9c821032015-04-30 04:23:23 +00003831 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003832 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00003833 }
3834}
3835
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003836/// \brief Called on a for stmt to check and extract its iteration space
3837/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00003838static bool CheckOpenMPIterationSpace(
3839 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
3840 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003841 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003842 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003843 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003844 // OpenMP [2.6, Canonical Loop Form]
3845 // for (init-expr; test-expr; incr-expr) structured-block
3846 auto For = dyn_cast_or_null<ForStmt>(S);
3847 if (!For) {
3848 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003849 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
3850 << getOpenMPDirectiveName(DKind) << NestedLoopCount
3851 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
3852 if (NestedLoopCount > 1) {
3853 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
3854 SemaRef.Diag(DSA.getConstructLoc(),
3855 diag::note_omp_collapse_ordered_expr)
3856 << 2 << CollapseLoopCountExpr->getSourceRange()
3857 << OrderedLoopCountExpr->getSourceRange();
3858 else if (CollapseLoopCountExpr)
3859 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3860 diag::note_omp_collapse_ordered_expr)
3861 << 0 << CollapseLoopCountExpr->getSourceRange();
3862 else
3863 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3864 diag::note_omp_collapse_ordered_expr)
3865 << 1 << OrderedLoopCountExpr->getSourceRange();
3866 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003867 return true;
3868 }
3869 assert(For->getBody());
3870
3871 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3872
3873 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003874 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003875 if (ISC.CheckInit(Init)) {
3876 return true;
3877 }
3878
3879 bool HasErrors = false;
3880
3881 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003882 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003883
3884 // OpenMP [2.6, Canonical Loop Form]
3885 // Var is one of the following:
3886 // A variable of signed or unsigned integer type.
3887 // For C++, a variable of a random access iterator type.
3888 // For C, a variable of a pointer type.
Alexey Bataeva8899172015-08-06 12:30:57 +00003889 auto VarType = Var->getType().getNonReferenceType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003890 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3891 !VarType->isPointerType() &&
3892 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3893 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3894 << SemaRef.getLangOpts().CPlusPlus;
3895 HasErrors = true;
3896 }
3897
Alexey Bataev4acb8592014-07-07 13:01:15 +00003898 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
3899 // Construct
3900 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3901 // parallel for construct is (are) private.
3902 // The loop iteration variable in the associated for-loop of a simd construct
3903 // with just one associated for-loop is linear with a constant-linear-step
3904 // that is the increment of the associated for-loop.
3905 // Exclude loop var from the list of variables with implicitly defined data
3906 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00003907 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003908
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003909 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
3910 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00003911 // The loop iteration variable in the associated for-loop of a simd construct
3912 // with just one associated for-loop may be listed in a linear clause with a
3913 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003914 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3915 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003916 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003917 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
3918 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3919 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003920 auto PredeterminedCKind =
3921 isOpenMPSimdDirective(DKind)
3922 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3923 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003924 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003925 DVar.CKind != PredeterminedCKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003926 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
Alexey Bataeve648e802015-12-25 13:38:08 +00003927 isOpenMPDistributeDirective(DKind)) &&
Alexey Bataev49f6e782015-12-01 04:18:41 +00003928 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003929 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
3930 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003931 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00003932 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
3933 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00003934 if (DVar.RefExpr == nullptr)
3935 DVar.CKind = PredeterminedCKind;
3936 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003937 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003938 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003939 // Make the loop iteration variable private (for worksharing constructs),
3940 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00003941 // lastprivate (for simd directives with several collapsed or ordered
3942 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00003943 if (DVar.CKind == OMPC_unknown)
3944 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
3945 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00003946 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003947 }
3948
Alexey Bataev7ff55242014-06-19 09:13:45 +00003949 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00003950
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003951 // Check test-expr.
3952 HasErrors |= ISC.CheckCond(For->getCond());
3953
3954 // Check incr-expr.
3955 HasErrors |= ISC.CheckInc(For->getInc());
3956
Alexander Musmana5f070a2014-10-01 06:03:56 +00003957 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003958 return HasErrors;
3959
Alexander Musmana5f070a2014-10-01 06:03:56 +00003960 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003961 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00003962 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003963 DSA.getCurScope(), (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003964 isOpenMPTaskLoopDirective(DKind) ||
3965 isOpenMPDistributeDirective(DKind)));
Alexander Musmana5f070a2014-10-01 06:03:56 +00003966 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
Alexey Bataeva8899172015-08-06 12:30:57 +00003967 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003968 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
3969 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
3970 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
3971 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
3972 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
3973 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
3974
Alexey Bataev62dbb972015-04-22 11:59:37 +00003975 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
3976 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003977 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00003978 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003979 ResultIterSpace.CounterInit == nullptr ||
3980 ResultIterSpace.CounterStep == nullptr);
3981
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003982 return HasErrors;
3983}
3984
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003985/// \brief Build 'VarRef = Start.
3986static ExprResult BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc,
3987 ExprResult VarRef, ExprResult Start) {
3988 TransformToNewDefs Transform(SemaRef);
3989 // Build 'VarRef = Start.
3990 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3991 if (NewStart.isInvalid())
3992 return ExprError();
3993 NewStart = SemaRef.PerformImplicitConversion(
3994 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3995 Sema::AA_Converting,
3996 /*AllowExplicit=*/true);
3997 if (NewStart.isInvalid())
3998 return ExprError();
3999 NewStart = SemaRef.PerformImplicitConversion(
4000 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4001 /*AllowExplicit=*/true);
4002 if (!NewStart.isUsable())
4003 return ExprError();
4004
4005 auto Init =
4006 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4007 return Init;
4008}
4009
Alexander Musmana5f070a2014-10-01 06:03:56 +00004010/// \brief Build 'VarRef = Start + Iter * Step'.
4011static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
4012 SourceLocation Loc, ExprResult VarRef,
4013 ExprResult Start, ExprResult Iter,
4014 ExprResult Step, bool Subtract) {
4015 // Add parentheses (for debugging purposes only).
4016 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4017 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4018 !Step.isUsable())
4019 return ExprError();
4020
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004021 TransformToNewDefs Transform(SemaRef);
4022 auto NewStep = Transform.TransformExpr(Step.get()->IgnoreImplicit());
4023 if (NewStep.isInvalid())
4024 return ExprError();
4025 NewStep = SemaRef.PerformImplicitConversion(
4026 NewStep.get(), Step.get()->IgnoreImplicit()->getType(),
4027 Sema::AA_Converting,
4028 /*AllowExplicit=*/true);
4029 if (NewStep.isInvalid())
4030 return ExprError();
4031 ExprResult Update =
4032 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004033 if (!Update.isUsable())
4034 return ExprError();
4035
4036 // Build 'VarRef = Start + Iter * Step'.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004037 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
4038 if (NewStart.isInvalid())
4039 return ExprError();
4040 NewStart = SemaRef.PerformImplicitConversion(
4041 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
4042 Sema::AA_Converting,
4043 /*AllowExplicit=*/true);
4044 if (NewStart.isInvalid())
4045 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004046 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004047 NewStart.get(), Update.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004048 if (!Update.isUsable())
4049 return ExprError();
4050
4051 Update = SemaRef.PerformImplicitConversion(
4052 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4053 if (!Update.isUsable())
4054 return ExprError();
4055
4056 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4057 return Update;
4058}
4059
4060/// \brief Convert integer expression \a E to make it have at least \a Bits
4061/// bits.
4062static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
4063 Sema &SemaRef) {
4064 if (E == nullptr)
4065 return ExprError();
4066 auto &C = SemaRef.Context;
4067 QualType OldType = E->getType();
4068 unsigned HasBits = C.getTypeSize(OldType);
4069 if (HasBits >= Bits)
4070 return ExprResult(E);
4071 // OK to convert to signed, because new type has more bits than old.
4072 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4073 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4074 true);
4075}
4076
4077/// \brief Check if the given expression \a E is a constant integer that fits
4078/// into \a Bits bits.
4079static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4080 if (E == nullptr)
4081 return false;
4082 llvm::APSInt Result;
4083 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4084 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4085 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004086}
4087
4088/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00004089/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4090/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004091static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00004092CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
4093 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4094 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004095 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00004096 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004097 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004098 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004099 // Found 'collapse' clause - calculate collapse number.
4100 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004101 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004102 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004103 }
4104 if (OrderedLoopCountExpr) {
4105 // Found 'ordered' clause - calculate collapse number.
4106 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004107 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4108 if (Result.getLimitedValue() < NestedLoopCount) {
4109 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4110 diag::err_omp_wrong_ordered_loop_count)
4111 << OrderedLoopCountExpr->getSourceRange();
4112 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4113 diag::note_collapse_loop_count)
4114 << CollapseLoopCountExpr->getSourceRange();
4115 }
4116 NestedLoopCount = Result.getLimitedValue();
4117 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004118 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004119 // This is helper routine for loop directives (e.g., 'for', 'simd',
4120 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00004121 SmallVector<LoopIterationSpace, 4> IterSpaces;
4122 IterSpaces.resize(NestedLoopCount);
4123 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004124 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004125 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004126 NestedLoopCount, CollapseLoopCountExpr,
4127 OrderedLoopCountExpr, VarsWithImplicitDSA,
4128 IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004129 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004130 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004131 // OpenMP [2.8.1, simd construct, Restrictions]
4132 // All loops associated with the construct must be perfectly nested; that
4133 // is, there must be no intervening code nor any OpenMP directive between
4134 // any two loops.
4135 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004136 }
4137
Alexander Musmana5f070a2014-10-01 06:03:56 +00004138 Built.clear(/* size */ NestedLoopCount);
4139
4140 if (SemaRef.CurContext->isDependentContext())
4141 return NestedLoopCount;
4142
4143 // An example of what is generated for the following code:
4144 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004145 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004146 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004147 // for (k = 0; k < NK; ++k)
4148 // for (j = J0; j < NJ; j+=2) {
4149 // <loop body>
4150 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004151 //
4152 // We generate the code below.
4153 // Note: the loop body may be outlined in CodeGen.
4154 // Note: some counters may be C++ classes, operator- is used to find number of
4155 // iterations and operator+= to calculate counter value.
4156 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4157 // or i64 is currently supported).
4158 //
4159 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4160 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4161 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4162 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4163 // // similar updates for vars in clauses (e.g. 'linear')
4164 // <loop body (using local i and j)>
4165 // }
4166 // i = NI; // assign final values of counters
4167 // j = NJ;
4168 //
4169
4170 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4171 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004172 // Precondition tests if there is at least one iteration (all conditions are
4173 // true).
4174 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004175 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004176 ExprResult LastIteration32 = WidenIterationCount(
4177 32 /* Bits */, SemaRef.PerformImplicitConversion(
4178 N0->IgnoreImpCasts(), N0->getType(),
4179 Sema::AA_Converting, /*AllowExplicit=*/true)
4180 .get(),
4181 SemaRef);
4182 ExprResult LastIteration64 = WidenIterationCount(
4183 64 /* Bits */, SemaRef.PerformImplicitConversion(
4184 N0->IgnoreImpCasts(), N0->getType(),
4185 Sema::AA_Converting, /*AllowExplicit=*/true)
4186 .get(),
4187 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004188
4189 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4190 return NestedLoopCount;
4191
4192 auto &C = SemaRef.Context;
4193 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4194
4195 Scope *CurScope = DSA.getCurScope();
4196 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004197 if (PreCond.isUsable()) {
4198 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
4199 PreCond.get(), IterSpaces[Cnt].PreCond);
4200 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004201 auto N = IterSpaces[Cnt].NumIterations;
4202 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4203 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004204 LastIteration32 = SemaRef.BuildBinOp(
4205 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
4206 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4207 Sema::AA_Converting,
4208 /*AllowExplicit=*/true)
4209 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004210 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004211 LastIteration64 = SemaRef.BuildBinOp(
4212 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
4213 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4214 Sema::AA_Converting,
4215 /*AllowExplicit=*/true)
4216 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004217 }
4218
4219 // Choose either the 32-bit or 64-bit version.
4220 ExprResult LastIteration = LastIteration64;
4221 if (LastIteration32.isUsable() &&
4222 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4223 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4224 FitsInto(
4225 32 /* Bits */,
4226 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4227 LastIteration64.get(), SemaRef)))
4228 LastIteration = LastIteration32;
4229
4230 if (!LastIteration.isUsable())
4231 return 0;
4232
4233 // Save the number of iterations.
4234 ExprResult NumIterations = LastIteration;
4235 {
4236 LastIteration = SemaRef.BuildBinOp(
4237 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
4238 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4239 if (!LastIteration.isUsable())
4240 return 0;
4241 }
4242
4243 // Calculate the last iteration number beforehand instead of doing this on
4244 // each iteration. Do not do this if the number of iterations may be kfold-ed.
4245 llvm::APSInt Result;
4246 bool IsConstant =
4247 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4248 ExprResult CalcLastIteration;
4249 if (!IsConstant) {
4250 SourceLocation SaveLoc;
4251 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004252 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00004253 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00004254 ExprResult SaveRef = buildDeclRefExpr(
4255 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004256 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
4257 SaveRef.get(), LastIteration.get());
4258 LastIteration = SaveRef;
4259
4260 // Prepare SaveRef + 1.
4261 NumIterations = SemaRef.BuildBinOp(
4262 CurScope, SaveLoc, BO_Add, SaveRef.get(),
4263 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4264 if (!NumIterations.isUsable())
4265 return 0;
4266 }
4267
4268 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4269
Alexander Musmanc6388682014-12-15 07:07:06 +00004270 QualType VType = LastIteration.get()->getType();
4271 // Build variables passed into runtime, nesessary for worksharing directives.
4272 ExprResult LB, UB, IL, ST, EUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004273 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4274 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004275 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004276 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4277 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004278 SemaRef.AddInitializerToDecl(
4279 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4280 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4281
4282 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004283 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4284 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004285 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
4286 /*DirectInit*/ false,
4287 /*TypeMayContainAuto*/ false);
4288
4289 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4290 // This will be used to implement clause 'lastprivate'.
4291 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00004292 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4293 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004294 SemaRef.AddInitializerToDecl(
4295 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4296 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4297
4298 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00004299 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
4300 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004301 SemaRef.AddInitializerToDecl(
4302 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4303 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4304
4305 // Build expression: UB = min(UB, LastIteration)
4306 // It is nesessary for CodeGen of directives with static scheduling.
4307 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4308 UB.get(), LastIteration.get());
4309 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4310 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4311 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4312 CondOp.get());
4313 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
4314 }
4315
4316 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004317 ExprResult IV;
4318 ExprResult Init;
4319 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00004320 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
4321 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004322 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004323 isOpenMPTaskLoopDirective(DKind) ||
4324 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004325 ? LB.get()
4326 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4327 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4328 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004329 }
4330
Alexander Musmanc6388682014-12-15 07:07:06 +00004331 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004332 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00004333 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004334 (isOpenMPWorksharingDirective(DKind) ||
4335 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004336 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4337 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4338 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004339
4340 // Loop increment (IV = IV + 1)
4341 SourceLocation IncLoc;
4342 ExprResult Inc =
4343 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4344 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4345 if (!Inc.isUsable())
4346 return 0;
4347 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00004348 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4349 if (!Inc.isUsable())
4350 return 0;
4351
4352 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4353 // Used for directives with static scheduling.
4354 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004355 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4356 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004357 // LB + ST
4358 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4359 if (!NextLB.isUsable())
4360 return 0;
4361 // LB = LB + ST
4362 NextLB =
4363 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4364 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4365 if (!NextLB.isUsable())
4366 return 0;
4367 // UB + ST
4368 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4369 if (!NextUB.isUsable())
4370 return 0;
4371 // UB = UB + ST
4372 NextUB =
4373 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4374 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4375 if (!NextUB.isUsable())
4376 return 0;
4377 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004378
4379 // Build updates and final values of the loop counters.
4380 bool HasErrors = false;
4381 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004382 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004383 Built.Updates.resize(NestedLoopCount);
4384 Built.Finals.resize(NestedLoopCount);
4385 {
4386 ExprResult Div;
4387 // Go from inner nested loop to outer.
4388 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4389 LoopIterationSpace &IS = IterSpaces[Cnt];
4390 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4391 // Build: Iter = (IV / Div) % IS.NumIters
4392 // where Div is product of previous iterations' IS.NumIters.
4393 ExprResult Iter;
4394 if (Div.isUsable()) {
4395 Iter =
4396 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4397 } else {
4398 Iter = IV;
4399 assert((Cnt == (int)NestedLoopCount - 1) &&
4400 "unusable div expected on first iteration only");
4401 }
4402
4403 if (Cnt != 0 && Iter.isUsable())
4404 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4405 IS.NumIterations);
4406 if (!Iter.isUsable()) {
4407 HasErrors = true;
4408 break;
4409 }
4410
Alexey Bataev39f915b82015-05-08 10:41:21 +00004411 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
4412 auto *CounterVar = buildDeclRefExpr(
4413 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
4414 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
4415 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004416 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
4417 IS.CounterInit);
4418 if (!Init.isUsable()) {
4419 HasErrors = true;
4420 break;
4421 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004422 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004423 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004424 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
4425 if (!Update.isUsable()) {
4426 HasErrors = true;
4427 break;
4428 }
4429
4430 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4431 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00004432 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004433 IS.NumIterations, IS.CounterStep, IS.Subtract);
4434 if (!Final.isUsable()) {
4435 HasErrors = true;
4436 break;
4437 }
4438
4439 // Build Div for the next iteration: Div <- Div * IS.NumIters
4440 if (Cnt != 0) {
4441 if (Div.isUnset())
4442 Div = IS.NumIterations;
4443 else
4444 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4445 IS.NumIterations);
4446
4447 // Add parentheses (for debugging purposes only).
4448 if (Div.isUsable())
4449 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
4450 if (!Div.isUsable()) {
4451 HasErrors = true;
4452 break;
4453 }
4454 }
4455 if (!Update.isUsable() || !Final.isUsable()) {
4456 HasErrors = true;
4457 break;
4458 }
4459 // Save results
4460 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004461 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004462 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004463 Built.Updates[Cnt] = Update.get();
4464 Built.Finals[Cnt] = Final.get();
4465 }
4466 }
4467
4468 if (HasErrors)
4469 return 0;
4470
4471 // Save results
4472 Built.IterationVarRef = IV.get();
4473 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004474 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004475 Built.CalcLastIteration =
4476 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004477 Built.PreCond = PreCond.get();
4478 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004479 Built.Init = Init.get();
4480 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004481 Built.LB = LB.get();
4482 Built.UB = UB.get();
4483 Built.IL = IL.get();
4484 Built.ST = ST.get();
4485 Built.EUB = EUB.get();
4486 Built.NLB = NextLB.get();
4487 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004488
Alexey Bataevabfc0692014-06-25 06:52:00 +00004489 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004490}
4491
Alexey Bataev10e775f2015-07-30 11:36:16 +00004492static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004493 auto CollapseClauses =
4494 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4495 if (CollapseClauses.begin() != CollapseClauses.end())
4496 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004497 return nullptr;
4498}
4499
Alexey Bataev10e775f2015-07-30 11:36:16 +00004500static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004501 auto OrderedClauses =
4502 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4503 if (OrderedClauses.begin() != OrderedClauses.end())
4504 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004505 return nullptr;
4506}
4507
Alexey Bataev66b15b52015-08-21 11:14:16 +00004508static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
4509 const Expr *Safelen) {
4510 llvm::APSInt SimdlenRes, SafelenRes;
4511 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
4512 Simdlen->isInstantiationDependent() ||
4513 Simdlen->containsUnexpandedParameterPack())
4514 return false;
4515 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
4516 Safelen->isInstantiationDependent() ||
4517 Safelen->containsUnexpandedParameterPack())
4518 return false;
4519 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
4520 Safelen->EvaluateAsInt(SafelenRes, S.Context);
4521 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4522 // If both simdlen and safelen clauses are specified, the value of the simdlen
4523 // parameter must be less than or equal to the value of the safelen parameter.
4524 if (SimdlenRes > SafelenRes) {
4525 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
4526 << Simdlen->getSourceRange() << Safelen->getSourceRange();
4527 return true;
4528 }
4529 return false;
4530}
4531
Alexey Bataev4acb8592014-07-07 13:01:15 +00004532StmtResult Sema::ActOnOpenMPSimdDirective(
4533 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4534 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004535 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004536 if (!AStmt)
4537 return StmtError();
4538
4539 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004540 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004541 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4542 // define the nested loops number.
4543 unsigned NestedLoopCount = CheckOpenMPLoop(
4544 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4545 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004546 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004547 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004548
Alexander Musmana5f070a2014-10-01 06:03:56 +00004549 assert((CurContext->isDependentContext() || B.builtAll()) &&
4550 "omp simd loop exprs were not built");
4551
Alexander Musman3276a272015-03-21 10:12:56 +00004552 if (!CurContext->isDependentContext()) {
4553 // Finalize the clauses that need pre-built expressions for CodeGen.
4554 for (auto C : Clauses) {
4555 if (auto LC = dyn_cast<OMPLinearClause>(C))
4556 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4557 B.NumIterations, *this, CurScope))
4558 return StmtError();
4559 }
4560 }
4561
Alexey Bataev66b15b52015-08-21 11:14:16 +00004562 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4563 // If both simdlen and safelen clauses are specified, the value of the simdlen
4564 // parameter must be less than or equal to the value of the safelen parameter.
4565 OMPSafelenClause *Safelen = nullptr;
4566 OMPSimdlenClause *Simdlen = nullptr;
4567 for (auto *Clause : Clauses) {
4568 if (Clause->getClauseKind() == OMPC_safelen)
4569 Safelen = cast<OMPSafelenClause>(Clause);
4570 else if (Clause->getClauseKind() == OMPC_simdlen)
4571 Simdlen = cast<OMPSimdlenClause>(Clause);
4572 if (Safelen && Simdlen)
4573 break;
4574 }
4575 if (Simdlen && Safelen &&
4576 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4577 Safelen->getSafelen()))
4578 return StmtError();
4579
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004580 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004581 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4582 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004583}
4584
Alexey Bataev4acb8592014-07-07 13:01:15 +00004585StmtResult Sema::ActOnOpenMPForDirective(
4586 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4587 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004588 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004589 if (!AStmt)
4590 return StmtError();
4591
4592 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004593 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004594 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4595 // define the nested loops number.
4596 unsigned NestedLoopCount = CheckOpenMPLoop(
4597 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4598 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004599 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00004600 return StmtError();
4601
Alexander Musmana5f070a2014-10-01 06:03:56 +00004602 assert((CurContext->isDependentContext() || B.builtAll()) &&
4603 "omp for loop exprs were not built");
4604
Alexey Bataev54acd402015-08-04 11:18:19 +00004605 if (!CurContext->isDependentContext()) {
4606 // Finalize the clauses that need pre-built expressions for CodeGen.
4607 for (auto C : Clauses) {
4608 if (auto LC = dyn_cast<OMPLinearClause>(C))
4609 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4610 B.NumIterations, *this, CurScope))
4611 return StmtError();
4612 }
4613 }
4614
Alexey Bataevf29276e2014-06-18 04:14:57 +00004615 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004616 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004617 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004618}
4619
Alexander Musmanf82886e2014-09-18 05:12:34 +00004620StmtResult Sema::ActOnOpenMPForSimdDirective(
4621 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4622 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004623 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004624 if (!AStmt)
4625 return StmtError();
4626
4627 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004628 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004629 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4630 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00004631 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004632 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
4633 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4634 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004635 if (NestedLoopCount == 0)
4636 return StmtError();
4637
Alexander Musmanc6388682014-12-15 07:07:06 +00004638 assert((CurContext->isDependentContext() || B.builtAll()) &&
4639 "omp for simd loop exprs were not built");
4640
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00004641 if (!CurContext->isDependentContext()) {
4642 // Finalize the clauses that need pre-built expressions for CodeGen.
4643 for (auto C : Clauses) {
4644 if (auto LC = dyn_cast<OMPLinearClause>(C))
4645 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4646 B.NumIterations, *this, CurScope))
4647 return StmtError();
4648 }
4649 }
4650
Alexey Bataev66b15b52015-08-21 11:14:16 +00004651 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4652 // If both simdlen and safelen clauses are specified, the value of the simdlen
4653 // parameter must be less than or equal to the value of the safelen parameter.
4654 OMPSafelenClause *Safelen = nullptr;
4655 OMPSimdlenClause *Simdlen = nullptr;
4656 for (auto *Clause : Clauses) {
4657 if (Clause->getClauseKind() == OMPC_safelen)
4658 Safelen = cast<OMPSafelenClause>(Clause);
4659 else if (Clause->getClauseKind() == OMPC_simdlen)
4660 Simdlen = cast<OMPSimdlenClause>(Clause);
4661 if (Safelen && Simdlen)
4662 break;
4663 }
4664 if (Simdlen && Safelen &&
4665 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4666 Safelen->getSafelen()))
4667 return StmtError();
4668
Alexander Musmanf82886e2014-09-18 05:12:34 +00004669 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004670 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4671 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004672}
4673
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004674StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
4675 Stmt *AStmt,
4676 SourceLocation StartLoc,
4677 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004678 if (!AStmt)
4679 return StmtError();
4680
4681 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004682 auto BaseStmt = AStmt;
4683 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4684 BaseStmt = CS->getCapturedStmt();
4685 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4686 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004687 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004688 return StmtError();
4689 // All associated statements must be '#pragma omp section' except for
4690 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004691 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004692 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4693 if (SectionStmt)
4694 Diag(SectionStmt->getLocStart(),
4695 diag::err_omp_sections_substmt_not_section);
4696 return StmtError();
4697 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004698 cast<OMPSectionDirective>(SectionStmt)
4699 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004700 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004701 } else {
4702 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
4703 return StmtError();
4704 }
4705
4706 getCurFunction()->setHasBranchProtectedScope();
4707
Alexey Bataev25e5b442015-09-15 12:52:43 +00004708 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4709 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004710}
4711
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004712StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
4713 SourceLocation StartLoc,
4714 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004715 if (!AStmt)
4716 return StmtError();
4717
4718 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004719
4720 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00004721 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004722
Alexey Bataev25e5b442015-09-15 12:52:43 +00004723 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
4724 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004725}
4726
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004727StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
4728 Stmt *AStmt,
4729 SourceLocation StartLoc,
4730 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004731 if (!AStmt)
4732 return StmtError();
4733
4734 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00004735
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004736 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00004737
Alexey Bataev3255bf32015-01-19 05:20:46 +00004738 // OpenMP [2.7.3, single Construct, Restrictions]
4739 // The copyprivate clause must not be used with the nowait clause.
4740 OMPClause *Nowait = nullptr;
4741 OMPClause *Copyprivate = nullptr;
4742 for (auto *Clause : Clauses) {
4743 if (Clause->getClauseKind() == OMPC_nowait)
4744 Nowait = Clause;
4745 else if (Clause->getClauseKind() == OMPC_copyprivate)
4746 Copyprivate = Clause;
4747 if (Copyprivate && Nowait) {
4748 Diag(Copyprivate->getLocStart(),
4749 diag::err_omp_single_copyprivate_with_nowait);
4750 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
4751 return StmtError();
4752 }
4753 }
4754
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004755 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4756}
4757
Alexander Musman80c22892014-07-17 08:54:58 +00004758StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
4759 SourceLocation StartLoc,
4760 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004761 if (!AStmt)
4762 return StmtError();
4763
4764 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00004765
4766 getCurFunction()->setHasBranchProtectedScope();
4767
4768 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
4769}
4770
Alexey Bataev28c75412015-12-15 08:19:24 +00004771StmtResult Sema::ActOnOpenMPCriticalDirective(
4772 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
4773 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004774 if (!AStmt)
4775 return StmtError();
4776
4777 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004778
Alexey Bataev28c75412015-12-15 08:19:24 +00004779 bool ErrorFound = false;
4780 llvm::APSInt Hint;
4781 SourceLocation HintLoc;
4782 bool DependentHint = false;
4783 for (auto *C : Clauses) {
4784 if (C->getClauseKind() == OMPC_hint) {
4785 if (!DirName.getName()) {
4786 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
4787 ErrorFound = true;
4788 }
4789 Expr *E = cast<OMPHintClause>(C)->getHint();
4790 if (E->isTypeDependent() || E->isValueDependent() ||
4791 E->isInstantiationDependent())
4792 DependentHint = true;
4793 else {
4794 Hint = E->EvaluateKnownConstInt(Context);
4795 HintLoc = C->getLocStart();
4796 }
4797 }
4798 }
4799 if (ErrorFound)
4800 return StmtError();
4801 auto Pair = DSAStack->getCriticalWithHint(DirName);
4802 if (Pair.first && DirName.getName() && !DependentHint) {
4803 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
4804 Diag(StartLoc, diag::err_omp_critical_with_hint);
4805 if (HintLoc.isValid()) {
4806 Diag(HintLoc, diag::note_omp_critical_hint_here)
4807 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
4808 } else
4809 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
4810 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
4811 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
4812 << 1
4813 << C->getHint()->EvaluateKnownConstInt(Context).toString(
4814 /*Radix=*/10, /*Signed=*/false);
4815 } else
4816 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
4817 }
4818 }
4819
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004820 getCurFunction()->setHasBranchProtectedScope();
4821
Alexey Bataev28c75412015-12-15 08:19:24 +00004822 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
4823 Clauses, AStmt);
4824 if (!Pair.first && DirName.getName() && !DependentHint)
4825 DSAStack->addCriticalWithHint(Dir, Hint);
4826 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004827}
4828
Alexey Bataev4acb8592014-07-07 13:01:15 +00004829StmtResult Sema::ActOnOpenMPParallelForDirective(
4830 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4831 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004832 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004833 if (!AStmt)
4834 return StmtError();
4835
Alexey Bataev4acb8592014-07-07 13:01:15 +00004836 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4837 // 1.2.2 OpenMP Language Terminology
4838 // Structured block - An executable statement with a single entry at the
4839 // top and a single exit at the bottom.
4840 // The point of exit cannot be a branch out of the structured block.
4841 // longjmp() and throw() must not violate the entry/exit criteria.
4842 CS->getCapturedDecl()->setNothrow();
4843
Alexander Musmanc6388682014-12-15 07:07:06 +00004844 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004845 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4846 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004847 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004848 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
4849 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4850 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00004851 if (NestedLoopCount == 0)
4852 return StmtError();
4853
Alexander Musmana5f070a2014-10-01 06:03:56 +00004854 assert((CurContext->isDependentContext() || B.builtAll()) &&
4855 "omp parallel for loop exprs were not built");
4856
Alexey Bataev54acd402015-08-04 11:18:19 +00004857 if (!CurContext->isDependentContext()) {
4858 // Finalize the clauses that need pre-built expressions for CodeGen.
4859 for (auto C : Clauses) {
4860 if (auto LC = dyn_cast<OMPLinearClause>(C))
4861 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4862 B.NumIterations, *this, CurScope))
4863 return StmtError();
4864 }
4865 }
4866
Alexey Bataev4acb8592014-07-07 13:01:15 +00004867 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004868 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004869 NestedLoopCount, Clauses, AStmt, B,
4870 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00004871}
4872
Alexander Musmane4e893b2014-09-23 09:33:00 +00004873StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
4874 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4875 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004876 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004877 if (!AStmt)
4878 return StmtError();
4879
Alexander Musmane4e893b2014-09-23 09:33:00 +00004880 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4881 // 1.2.2 OpenMP Language Terminology
4882 // Structured block - An executable statement with a single entry at the
4883 // top and a single exit at the bottom.
4884 // The point of exit cannot be a branch out of the structured block.
4885 // longjmp() and throw() must not violate the entry/exit criteria.
4886 CS->getCapturedDecl()->setNothrow();
4887
Alexander Musmanc6388682014-12-15 07:07:06 +00004888 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004889 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4890 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00004891 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004892 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
4893 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4894 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004895 if (NestedLoopCount == 0)
4896 return StmtError();
4897
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00004898 if (!CurContext->isDependentContext()) {
4899 // Finalize the clauses that need pre-built expressions for CodeGen.
4900 for (auto C : Clauses) {
4901 if (auto LC = dyn_cast<OMPLinearClause>(C))
4902 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4903 B.NumIterations, *this, CurScope))
4904 return StmtError();
4905 }
4906 }
4907
Alexey Bataev66b15b52015-08-21 11:14:16 +00004908 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4909 // If both simdlen and safelen clauses are specified, the value of the simdlen
4910 // parameter must be less than or equal to the value of the safelen parameter.
4911 OMPSafelenClause *Safelen = nullptr;
4912 OMPSimdlenClause *Simdlen = nullptr;
4913 for (auto *Clause : Clauses) {
4914 if (Clause->getClauseKind() == OMPC_safelen)
4915 Safelen = cast<OMPSafelenClause>(Clause);
4916 else if (Clause->getClauseKind() == OMPC_simdlen)
4917 Simdlen = cast<OMPSimdlenClause>(Clause);
4918 if (Safelen && Simdlen)
4919 break;
4920 }
4921 if (Simdlen && Safelen &&
4922 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4923 Safelen->getSafelen()))
4924 return StmtError();
4925
Alexander Musmane4e893b2014-09-23 09:33:00 +00004926 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004927 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00004928 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004929}
4930
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004931StmtResult
4932Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
4933 Stmt *AStmt, SourceLocation StartLoc,
4934 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004935 if (!AStmt)
4936 return StmtError();
4937
4938 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004939 auto BaseStmt = AStmt;
4940 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4941 BaseStmt = CS->getCapturedStmt();
4942 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4943 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004944 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004945 return StmtError();
4946 // All associated statements must be '#pragma omp section' except for
4947 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004948 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004949 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4950 if (SectionStmt)
4951 Diag(SectionStmt->getLocStart(),
4952 diag::err_omp_parallel_sections_substmt_not_section);
4953 return StmtError();
4954 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004955 cast<OMPSectionDirective>(SectionStmt)
4956 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004957 }
4958 } else {
4959 Diag(AStmt->getLocStart(),
4960 diag::err_omp_parallel_sections_not_compound_stmt);
4961 return StmtError();
4962 }
4963
4964 getCurFunction()->setHasBranchProtectedScope();
4965
Alexey Bataev25e5b442015-09-15 12:52:43 +00004966 return OMPParallelSectionsDirective::Create(
4967 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004968}
4969
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004970StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
4971 Stmt *AStmt, SourceLocation StartLoc,
4972 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004973 if (!AStmt)
4974 return StmtError();
4975
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004976 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4977 // 1.2.2 OpenMP Language Terminology
4978 // Structured block - An executable statement with a single entry at the
4979 // top and a single exit at the bottom.
4980 // The point of exit cannot be a branch out of the structured block.
4981 // longjmp() and throw() must not violate the entry/exit criteria.
4982 CS->getCapturedDecl()->setNothrow();
4983
4984 getCurFunction()->setHasBranchProtectedScope();
4985
Alexey Bataev25e5b442015-09-15 12:52:43 +00004986 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4987 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004988}
4989
Alexey Bataev68446b72014-07-18 07:47:19 +00004990StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
4991 SourceLocation EndLoc) {
4992 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
4993}
4994
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00004995StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
4996 SourceLocation EndLoc) {
4997 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
4998}
4999
Alexey Bataev2df347a2014-07-18 10:17:07 +00005000StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5001 SourceLocation EndLoc) {
5002 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5003}
5004
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005005StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
5006 SourceLocation StartLoc,
5007 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005008 if (!AStmt)
5009 return StmtError();
5010
5011 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005012
5013 getCurFunction()->setHasBranchProtectedScope();
5014
5015 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
5016}
5017
Alexey Bataev6125da92014-07-21 11:26:11 +00005018StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5019 SourceLocation StartLoc,
5020 SourceLocation EndLoc) {
5021 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5022 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5023}
5024
Alexey Bataev346265e2015-09-25 10:37:12 +00005025StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5026 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005027 SourceLocation StartLoc,
5028 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00005029 OMPClause *DependFound = nullptr;
5030 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005031 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005032 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00005033 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005034 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005035 for (auto *C : Clauses) {
5036 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5037 DependFound = C;
5038 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5039 if (DependSourceClause) {
5040 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5041 << getOpenMPDirectiveName(OMPD_ordered)
5042 << getOpenMPClauseName(OMPC_depend) << 2;
5043 ErrorFound = true;
5044 } else
5045 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005046 if (DependSinkClause) {
5047 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5048 << 0;
5049 ErrorFound = true;
5050 }
5051 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5052 if (DependSourceClause) {
5053 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5054 << 1;
5055 ErrorFound = true;
5056 }
5057 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00005058 }
5059 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00005060 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005061 else if (C->getClauseKind() == OMPC_simd)
5062 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00005063 }
Alexey Bataeveb482352015-12-18 05:05:56 +00005064 if (!ErrorFound && !SC &&
5065 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005066 // OpenMP [2.8.1,simd Construct, Restrictions]
5067 // An ordered construct with the simd clause is the only OpenMP construct
5068 // that can appear in the simd region.
5069 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00005070 ErrorFound = true;
5071 } else if (DependFound && (TC || SC)) {
5072 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
5073 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
5074 ErrorFound = true;
5075 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
5076 Diag(DependFound->getLocStart(),
5077 diag::err_omp_ordered_directive_without_param);
5078 ErrorFound = true;
5079 } else if (TC || Clauses.empty()) {
5080 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
5081 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
5082 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
5083 << (TC != nullptr);
5084 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
5085 ErrorFound = true;
5086 }
5087 }
5088 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005089 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00005090
5091 if (AStmt) {
5092 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5093
5094 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005095 }
Alexey Bataev346265e2015-09-25 10:37:12 +00005096
5097 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005098}
5099
Alexey Bataev1d160b12015-03-13 12:27:31 +00005100namespace {
5101/// \brief Helper class for checking expression in 'omp atomic [update]'
5102/// construct.
5103class OpenMPAtomicUpdateChecker {
5104 /// \brief Error results for atomic update expressions.
5105 enum ExprAnalysisErrorCode {
5106 /// \brief A statement is not an expression statement.
5107 NotAnExpression,
5108 /// \brief Expression is not builtin binary or unary operation.
5109 NotABinaryOrUnaryExpression,
5110 /// \brief Unary operation is not post-/pre- increment/decrement operation.
5111 NotAnUnaryIncDecExpression,
5112 /// \brief An expression is not of scalar type.
5113 NotAScalarType,
5114 /// \brief A binary operation is not an assignment operation.
5115 NotAnAssignmentOp,
5116 /// \brief RHS part of the binary operation is not a binary expression.
5117 NotABinaryExpression,
5118 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
5119 /// expression.
5120 NotABinaryOperator,
5121 /// \brief RHS binary operation does not have reference to the updated LHS
5122 /// part.
5123 NotAnUpdateExpression,
5124 /// \brief No errors is found.
5125 NoError
5126 };
5127 /// \brief Reference to Sema.
5128 Sema &SemaRef;
5129 /// \brief A location for note diagnostics (when error is found).
5130 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005131 /// \brief 'x' lvalue part of the source atomic expression.
5132 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005133 /// \brief 'expr' rvalue part of the source atomic expression.
5134 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005135 /// \brief Helper expression of the form
5136 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5137 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5138 Expr *UpdateExpr;
5139 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
5140 /// important for non-associative operations.
5141 bool IsXLHSInRHSPart;
5142 BinaryOperatorKind Op;
5143 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005144 /// \brief true if the source expression is a postfix unary operation, false
5145 /// if it is a prefix unary operation.
5146 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005147
5148public:
5149 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00005150 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00005151 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00005152 /// \brief Check specified statement that it is suitable for 'atomic update'
5153 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00005154 /// expression. If DiagId and NoteId == 0, then only check is performed
5155 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005156 /// \param DiagId Diagnostic which should be emitted if error is found.
5157 /// \param NoteId Diagnostic note for the main error message.
5158 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00005159 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005160 /// \brief Return the 'x' lvalue part of the source atomic expression.
5161 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00005162 /// \brief Return the 'expr' rvalue part of the source atomic expression.
5163 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00005164 /// \brief Return the update expression used in calculation of the updated
5165 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5166 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5167 Expr *getUpdateExpr() const { return UpdateExpr; }
5168 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
5169 /// false otherwise.
5170 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5171
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() const { return IsPostfixUpdate; }
5175
Alexey Bataev1d160b12015-03-13 12:27:31 +00005176private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00005177 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5178 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005179};
5180} // namespace
5181
5182bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5183 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5184 ExprAnalysisErrorCode ErrorFound = NoError;
5185 SourceLocation ErrorLoc, NoteLoc;
5186 SourceRange ErrorRange, NoteRange;
5187 // Allowed constructs are:
5188 // x = x binop expr;
5189 // x = expr binop x;
5190 if (AtomicBinOp->getOpcode() == BO_Assign) {
5191 X = AtomicBinOp->getLHS();
5192 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
5193 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5194 if (AtomicInnerBinOp->isMultiplicativeOp() ||
5195 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5196 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005197 Op = AtomicInnerBinOp->getOpcode();
5198 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005199 auto *LHS = AtomicInnerBinOp->getLHS();
5200 auto *RHS = AtomicInnerBinOp->getRHS();
5201 llvm::FoldingSetNodeID XId, LHSId, RHSId;
5202 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
5203 /*Canonical=*/true);
5204 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
5205 /*Canonical=*/true);
5206 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
5207 /*Canonical=*/true);
5208 if (XId == LHSId) {
5209 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005210 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005211 } else if (XId == RHSId) {
5212 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005213 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005214 } else {
5215 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5216 ErrorRange = AtomicInnerBinOp->getSourceRange();
5217 NoteLoc = X->getExprLoc();
5218 NoteRange = X->getSourceRange();
5219 ErrorFound = NotAnUpdateExpression;
5220 }
5221 } else {
5222 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5223 ErrorRange = AtomicInnerBinOp->getSourceRange();
5224 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5225 NoteRange = SourceRange(NoteLoc, NoteLoc);
5226 ErrorFound = NotABinaryOperator;
5227 }
5228 } else {
5229 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5230 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5231 ErrorFound = NotABinaryExpression;
5232 }
5233 } else {
5234 ErrorLoc = AtomicBinOp->getExprLoc();
5235 ErrorRange = AtomicBinOp->getSourceRange();
5236 NoteLoc = AtomicBinOp->getOperatorLoc();
5237 NoteRange = SourceRange(NoteLoc, NoteLoc);
5238 ErrorFound = NotAnAssignmentOp;
5239 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005240 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005241 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5242 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5243 return true;
5244 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005245 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005246 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005247}
5248
5249bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5250 unsigned NoteId) {
5251 ExprAnalysisErrorCode ErrorFound = NoError;
5252 SourceLocation ErrorLoc, NoteLoc;
5253 SourceRange ErrorRange, NoteRange;
5254 // Allowed constructs are:
5255 // x++;
5256 // x--;
5257 // ++x;
5258 // --x;
5259 // x binop= expr;
5260 // x = x binop expr;
5261 // x = expr binop x;
5262 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5263 AtomicBody = AtomicBody->IgnoreParenImpCasts();
5264 if (AtomicBody->getType()->isScalarType() ||
5265 AtomicBody->isInstantiationDependent()) {
5266 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5267 AtomicBody->IgnoreParenImpCasts())) {
5268 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005269 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00005270 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00005271 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005272 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005273 X = AtomicCompAssignOp->getLHS();
5274 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005275 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5276 AtomicBody->IgnoreParenImpCasts())) {
5277 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005278 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
5279 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005280 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00005281 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
5282 // Check for Unary Operation
5283 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005284 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005285 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5286 OpLoc = AtomicUnaryOp->getOperatorLoc();
5287 X = AtomicUnaryOp->getSubExpr();
5288 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5289 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005290 } else {
5291 ErrorFound = NotAnUnaryIncDecExpression;
5292 ErrorLoc = AtomicUnaryOp->getExprLoc();
5293 ErrorRange = AtomicUnaryOp->getSourceRange();
5294 NoteLoc = AtomicUnaryOp->getOperatorLoc();
5295 NoteRange = SourceRange(NoteLoc, NoteLoc);
5296 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005297 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005298 ErrorFound = NotABinaryOrUnaryExpression;
5299 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5300 NoteRange = ErrorRange = AtomicBody->getSourceRange();
5301 }
5302 } else {
5303 ErrorFound = NotAScalarType;
5304 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
5305 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5306 }
5307 } else {
5308 ErrorFound = NotAnExpression;
5309 NoteLoc = ErrorLoc = S->getLocStart();
5310 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5311 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005312 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005313 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5314 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5315 return true;
5316 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005317 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005318 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005319 // Build an update expression of form 'OpaqueValueExpr(x) binop
5320 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
5321 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
5322 auto *OVEX = new (SemaRef.getASTContext())
5323 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
5324 auto *OVEExpr = new (SemaRef.getASTContext())
5325 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
5326 auto Update =
5327 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
5328 IsXLHSInRHSPart ? OVEExpr : OVEX);
5329 if (Update.isInvalid())
5330 return true;
5331 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
5332 Sema::AA_Casting);
5333 if (Update.isInvalid())
5334 return true;
5335 UpdateExpr = Update.get();
5336 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00005337 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005338}
5339
Alexey Bataev0162e452014-07-22 10:10:35 +00005340StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5341 Stmt *AStmt,
5342 SourceLocation StartLoc,
5343 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005344 if (!AStmt)
5345 return StmtError();
5346
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005347 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00005348 // 1.2.2 OpenMP Language Terminology
5349 // Structured block - An executable statement with a single entry at the
5350 // top and a single exit at the bottom.
5351 // The point of exit cannot be a branch out of the structured block.
5352 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00005353 OpenMPClauseKind AtomicKind = OMPC_unknown;
5354 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005355 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00005356 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00005357 C->getClauseKind() == OMPC_update ||
5358 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00005359 if (AtomicKind != OMPC_unknown) {
5360 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5361 << SourceRange(C->getLocStart(), C->getLocEnd());
5362 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5363 << getOpenMPClauseName(AtomicKind);
5364 } else {
5365 AtomicKind = C->getClauseKind();
5366 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005367 }
5368 }
5369 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005370
Alexey Bataev459dec02014-07-24 06:46:57 +00005371 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00005372 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5373 Body = EWC->getSubExpr();
5374
Alexey Bataev62cec442014-11-18 10:14:22 +00005375 Expr *X = nullptr;
5376 Expr *V = nullptr;
5377 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005378 Expr *UE = nullptr;
5379 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005380 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00005381 // OpenMP [2.12.6, atomic Construct]
5382 // In the next expressions:
5383 // * x and v (as applicable) are both l-value expressions with scalar type.
5384 // * During the execution of an atomic region, multiple syntactic
5385 // occurrences of x must designate the same storage location.
5386 // * Neither of v and expr (as applicable) may access the storage location
5387 // designated by x.
5388 // * Neither of x and expr (as applicable) may access the storage location
5389 // designated by v.
5390 // * expr is an expression with scalar type.
5391 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5392 // * binop, binop=, ++, and -- are not overloaded operators.
5393 // * The expression x binop expr must be numerically equivalent to x binop
5394 // (expr). This requirement is satisfied if the operators in expr have
5395 // precedence greater than binop, or by using parentheses around expr or
5396 // subexpressions of expr.
5397 // * The expression expr binop x must be numerically equivalent to (expr)
5398 // binop x. This requirement is satisfied if the operators in expr have
5399 // precedence equal to or greater than binop, or by using parentheses around
5400 // expr or subexpressions of expr.
5401 // * For forms that allow multiple occurrences of x, the number of times
5402 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00005403 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005404 enum {
5405 NotAnExpression,
5406 NotAnAssignmentOp,
5407 NotAScalarType,
5408 NotAnLValue,
5409 NoError
5410 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00005411 SourceLocation ErrorLoc, NoteLoc;
5412 SourceRange ErrorRange, NoteRange;
5413 // If clause is read:
5414 // v = x;
5415 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5416 auto AtomicBinOp =
5417 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5418 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5419 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5420 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5421 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5422 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5423 if (!X->isLValue() || !V->isLValue()) {
5424 auto NotLValueExpr = X->isLValue() ? V : X;
5425 ErrorFound = NotAnLValue;
5426 ErrorLoc = AtomicBinOp->getExprLoc();
5427 ErrorRange = AtomicBinOp->getSourceRange();
5428 NoteLoc = NotLValueExpr->getExprLoc();
5429 NoteRange = NotLValueExpr->getSourceRange();
5430 }
5431 } else if (!X->isInstantiationDependent() ||
5432 !V->isInstantiationDependent()) {
5433 auto NotScalarExpr =
5434 (X->isInstantiationDependent() || X->getType()->isScalarType())
5435 ? V
5436 : X;
5437 ErrorFound = NotAScalarType;
5438 ErrorLoc = AtomicBinOp->getExprLoc();
5439 ErrorRange = AtomicBinOp->getSourceRange();
5440 NoteLoc = NotScalarExpr->getExprLoc();
5441 NoteRange = NotScalarExpr->getSourceRange();
5442 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005443 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00005444 ErrorFound = NotAnAssignmentOp;
5445 ErrorLoc = AtomicBody->getExprLoc();
5446 ErrorRange = AtomicBody->getSourceRange();
5447 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5448 : AtomicBody->getExprLoc();
5449 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5450 : AtomicBody->getSourceRange();
5451 }
5452 } else {
5453 ErrorFound = NotAnExpression;
5454 NoteLoc = ErrorLoc = Body->getLocStart();
5455 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005456 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005457 if (ErrorFound != NoError) {
5458 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
5459 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005460 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5461 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00005462 return StmtError();
5463 } else if (CurContext->isDependentContext())
5464 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00005465 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005466 enum {
5467 NotAnExpression,
5468 NotAnAssignmentOp,
5469 NotAScalarType,
5470 NotAnLValue,
5471 NoError
5472 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005473 SourceLocation ErrorLoc, NoteLoc;
5474 SourceRange ErrorRange, NoteRange;
5475 // If clause is write:
5476 // x = expr;
5477 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5478 auto AtomicBinOp =
5479 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5480 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00005481 X = AtomicBinOp->getLHS();
5482 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00005483 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5484 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
5485 if (!X->isLValue()) {
5486 ErrorFound = NotAnLValue;
5487 ErrorLoc = AtomicBinOp->getExprLoc();
5488 ErrorRange = AtomicBinOp->getSourceRange();
5489 NoteLoc = X->getExprLoc();
5490 NoteRange = X->getSourceRange();
5491 }
5492 } else if (!X->isInstantiationDependent() ||
5493 !E->isInstantiationDependent()) {
5494 auto NotScalarExpr =
5495 (X->isInstantiationDependent() || X->getType()->isScalarType())
5496 ? E
5497 : X;
5498 ErrorFound = NotAScalarType;
5499 ErrorLoc = AtomicBinOp->getExprLoc();
5500 ErrorRange = AtomicBinOp->getSourceRange();
5501 NoteLoc = NotScalarExpr->getExprLoc();
5502 NoteRange = NotScalarExpr->getSourceRange();
5503 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005504 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00005505 ErrorFound = NotAnAssignmentOp;
5506 ErrorLoc = AtomicBody->getExprLoc();
5507 ErrorRange = AtomicBody->getSourceRange();
5508 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5509 : AtomicBody->getExprLoc();
5510 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5511 : AtomicBody->getSourceRange();
5512 }
5513 } else {
5514 ErrorFound = NotAnExpression;
5515 NoteLoc = ErrorLoc = Body->getLocStart();
5516 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005517 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00005518 if (ErrorFound != NoError) {
5519 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
5520 << ErrorRange;
5521 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5522 << NoteRange;
5523 return StmtError();
5524 } else if (CurContext->isDependentContext())
5525 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005526 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005527 // If clause is update:
5528 // x++;
5529 // x--;
5530 // ++x;
5531 // --x;
5532 // x binop= expr;
5533 // x = x binop expr;
5534 // x = expr binop x;
5535 OpenMPAtomicUpdateChecker Checker(*this);
5536 if (Checker.checkStatement(
5537 Body, (AtomicKind == OMPC_update)
5538 ? diag::err_omp_atomic_update_not_expression_statement
5539 : diag::err_omp_atomic_not_expression_statement,
5540 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00005541 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005542 if (!CurContext->isDependentContext()) {
5543 E = Checker.getExpr();
5544 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005545 UE = Checker.getUpdateExpr();
5546 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00005547 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005548 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005549 enum {
5550 NotAnAssignmentOp,
5551 NotACompoundStatement,
5552 NotTwoSubstatements,
5553 NotASpecificExpression,
5554 NoError
5555 } ErrorFound = NoError;
5556 SourceLocation ErrorLoc, NoteLoc;
5557 SourceRange ErrorRange, NoteRange;
5558 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5559 // If clause is a capture:
5560 // v = x++;
5561 // v = x--;
5562 // v = ++x;
5563 // v = --x;
5564 // v = x binop= expr;
5565 // v = x = x binop expr;
5566 // v = x = expr binop x;
5567 auto *AtomicBinOp =
5568 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5569 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5570 V = AtomicBinOp->getLHS();
5571 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5572 OpenMPAtomicUpdateChecker Checker(*this);
5573 if (Checker.checkStatement(
5574 Body, diag::err_omp_atomic_capture_not_expression_statement,
5575 diag::note_omp_atomic_update))
5576 return StmtError();
5577 E = Checker.getExpr();
5578 X = Checker.getX();
5579 UE = Checker.getUpdateExpr();
5580 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5581 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00005582 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005583 ErrorLoc = AtomicBody->getExprLoc();
5584 ErrorRange = AtomicBody->getSourceRange();
5585 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5586 : AtomicBody->getExprLoc();
5587 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5588 : AtomicBody->getSourceRange();
5589 ErrorFound = NotAnAssignmentOp;
5590 }
5591 if (ErrorFound != NoError) {
5592 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
5593 << ErrorRange;
5594 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5595 return StmtError();
5596 } else if (CurContext->isDependentContext()) {
5597 UE = V = E = X = nullptr;
5598 }
5599 } else {
5600 // If clause is a capture:
5601 // { v = x; x = expr; }
5602 // { v = x; x++; }
5603 // { v = x; x--; }
5604 // { v = x; ++x; }
5605 // { v = x; --x; }
5606 // { v = x; x binop= expr; }
5607 // { v = x; x = x binop expr; }
5608 // { v = x; x = expr binop x; }
5609 // { x++; v = x; }
5610 // { x--; v = x; }
5611 // { ++x; v = x; }
5612 // { --x; v = x; }
5613 // { x binop= expr; v = x; }
5614 // { x = x binop expr; v = x; }
5615 // { x = expr binop x; v = x; }
5616 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
5617 // Check that this is { expr1; expr2; }
5618 if (CS->size() == 2) {
5619 auto *First = CS->body_front();
5620 auto *Second = CS->body_back();
5621 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
5622 First = EWC->getSubExpr()->IgnoreParenImpCasts();
5623 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
5624 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
5625 // Need to find what subexpression is 'v' and what is 'x'.
5626 OpenMPAtomicUpdateChecker Checker(*this);
5627 bool IsUpdateExprFound = !Checker.checkStatement(Second);
5628 BinaryOperator *BinOp = nullptr;
5629 if (IsUpdateExprFound) {
5630 BinOp = dyn_cast<BinaryOperator>(First);
5631 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5632 }
5633 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5634 // { v = x; x++; }
5635 // { v = x; x--; }
5636 // { v = x; ++x; }
5637 // { v = x; --x; }
5638 // { v = x; x binop= expr; }
5639 // { v = x; x = x binop expr; }
5640 // { v = x; x = expr binop x; }
5641 // Check that the first expression has form v = x.
5642 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5643 llvm::FoldingSetNodeID XId, PossibleXId;
5644 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5645 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5646 IsUpdateExprFound = XId == PossibleXId;
5647 if (IsUpdateExprFound) {
5648 V = BinOp->getLHS();
5649 X = Checker.getX();
5650 E = Checker.getExpr();
5651 UE = Checker.getUpdateExpr();
5652 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005653 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005654 }
5655 }
5656 if (!IsUpdateExprFound) {
5657 IsUpdateExprFound = !Checker.checkStatement(First);
5658 BinOp = nullptr;
5659 if (IsUpdateExprFound) {
5660 BinOp = dyn_cast<BinaryOperator>(Second);
5661 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5662 }
5663 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5664 // { x++; v = x; }
5665 // { x--; v = x; }
5666 // { ++x; v = x; }
5667 // { --x; v = x; }
5668 // { x binop= expr; v = x; }
5669 // { x = x binop expr; v = x; }
5670 // { x = expr binop x; v = x; }
5671 // Check that the second expression has form v = x.
5672 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5673 llvm::FoldingSetNodeID XId, PossibleXId;
5674 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5675 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5676 IsUpdateExprFound = XId == PossibleXId;
5677 if (IsUpdateExprFound) {
5678 V = BinOp->getLHS();
5679 X = Checker.getX();
5680 E = Checker.getExpr();
5681 UE = Checker.getUpdateExpr();
5682 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005683 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005684 }
5685 }
5686 }
5687 if (!IsUpdateExprFound) {
5688 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00005689 auto *FirstExpr = dyn_cast<Expr>(First);
5690 auto *SecondExpr = dyn_cast<Expr>(Second);
5691 if (!FirstExpr || !SecondExpr ||
5692 !(FirstExpr->isInstantiationDependent() ||
5693 SecondExpr->isInstantiationDependent())) {
5694 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
5695 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005696 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00005697 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
5698 : First->getLocStart();
5699 NoteRange = ErrorRange = FirstBinOp
5700 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00005701 : SourceRange(ErrorLoc, ErrorLoc);
5702 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005703 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
5704 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
5705 ErrorFound = NotAnAssignmentOp;
5706 NoteLoc = ErrorLoc = SecondBinOp
5707 ? SecondBinOp->getOperatorLoc()
5708 : Second->getLocStart();
5709 NoteRange = ErrorRange =
5710 SecondBinOp ? SecondBinOp->getSourceRange()
5711 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00005712 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005713 auto *PossibleXRHSInFirst =
5714 FirstBinOp->getRHS()->IgnoreParenImpCasts();
5715 auto *PossibleXLHSInSecond =
5716 SecondBinOp->getLHS()->IgnoreParenImpCasts();
5717 llvm::FoldingSetNodeID X1Id, X2Id;
5718 PossibleXRHSInFirst->Profile(X1Id, Context,
5719 /*Canonical=*/true);
5720 PossibleXLHSInSecond->Profile(X2Id, Context,
5721 /*Canonical=*/true);
5722 IsUpdateExprFound = X1Id == X2Id;
5723 if (IsUpdateExprFound) {
5724 V = FirstBinOp->getLHS();
5725 X = SecondBinOp->getLHS();
5726 E = SecondBinOp->getRHS();
5727 UE = nullptr;
5728 IsXLHSInRHSPart = false;
5729 IsPostfixUpdate = true;
5730 } else {
5731 ErrorFound = NotASpecificExpression;
5732 ErrorLoc = FirstBinOp->getExprLoc();
5733 ErrorRange = FirstBinOp->getSourceRange();
5734 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
5735 NoteRange = SecondBinOp->getRHS()->getSourceRange();
5736 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005737 }
5738 }
5739 }
5740 }
5741 } else {
5742 NoteLoc = ErrorLoc = Body->getLocStart();
5743 NoteRange = ErrorRange =
5744 SourceRange(Body->getLocStart(), Body->getLocStart());
5745 ErrorFound = NotTwoSubstatements;
5746 }
5747 } else {
5748 NoteLoc = ErrorLoc = Body->getLocStart();
5749 NoteRange = ErrorRange =
5750 SourceRange(Body->getLocStart(), Body->getLocStart());
5751 ErrorFound = NotACompoundStatement;
5752 }
5753 if (ErrorFound != NoError) {
5754 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
5755 << ErrorRange;
5756 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5757 return StmtError();
5758 } else if (CurContext->isDependentContext()) {
5759 UE = V = E = X = nullptr;
5760 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005761 }
Alexey Bataevdea47612014-07-23 07:46:59 +00005762 }
Alexey Bataev0162e452014-07-22 10:10:35 +00005763
5764 getCurFunction()->setHasBranchProtectedScope();
5765
Alexey Bataev62cec442014-11-18 10:14:22 +00005766 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00005767 X, V, E, UE, IsXLHSInRHSPart,
5768 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00005769}
5770
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005771StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
5772 Stmt *AStmt,
5773 SourceLocation StartLoc,
5774 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005775 if (!AStmt)
5776 return StmtError();
5777
Samuel Antao4af1b7b2015-12-02 17:44:43 +00005778 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5779 // 1.2.2 OpenMP Language Terminology
5780 // Structured block - An executable statement with a single entry at the
5781 // top and a single exit at the bottom.
5782 // The point of exit cannot be a branch out of the structured block.
5783 // longjmp() and throw() must not violate the entry/exit criteria.
5784 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005785
Alexey Bataev13314bf2014-10-09 04:18:56 +00005786 // OpenMP [2.16, Nesting of Regions]
5787 // If specified, a teams construct must be contained within a target
5788 // construct. That target construct must contain no statements or directives
5789 // outside of the teams construct.
5790 if (DSAStack->hasInnerTeamsRegion()) {
5791 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
5792 bool OMPTeamsFound = true;
5793 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
5794 auto I = CS->body_begin();
5795 while (I != CS->body_end()) {
5796 auto OED = dyn_cast<OMPExecutableDirective>(*I);
5797 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
5798 OMPTeamsFound = false;
5799 break;
5800 }
5801 ++I;
5802 }
5803 assert(I != CS->body_end() && "Not found statement");
5804 S = *I;
5805 }
5806 if (!OMPTeamsFound) {
5807 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
5808 Diag(DSAStack->getInnerTeamsRegionLoc(),
5809 diag::note_omp_nested_teams_construct_here);
5810 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
5811 << isa<OMPExecutableDirective>(S);
5812 return StmtError();
5813 }
5814 }
5815
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005816 getCurFunction()->setHasBranchProtectedScope();
5817
5818 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5819}
5820
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00005821StmtResult
5822Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
5823 Stmt *AStmt, SourceLocation StartLoc,
5824 SourceLocation EndLoc) {
5825 if (!AStmt)
5826 return StmtError();
5827
5828 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5829 // 1.2.2 OpenMP Language Terminology
5830 // Structured block - An executable statement with a single entry at the
5831 // top and a single exit at the bottom.
5832 // The point of exit cannot be a branch out of the structured block.
5833 // longjmp() and throw() must not violate the entry/exit criteria.
5834 CS->getCapturedDecl()->setNothrow();
5835
5836 getCurFunction()->setHasBranchProtectedScope();
5837
5838 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5839 AStmt);
5840}
5841
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00005842StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
5843 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5844 SourceLocation EndLoc,
5845 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5846 if (!AStmt)
5847 return StmtError();
5848
5849 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5850 // 1.2.2 OpenMP Language Terminology
5851 // Structured block - An executable statement with a single entry at the
5852 // top and a single exit at the bottom.
5853 // The point of exit cannot be a branch out of the structured block.
5854 // longjmp() and throw() must not violate the entry/exit criteria.
5855 CS->getCapturedDecl()->setNothrow();
5856
5857 OMPLoopDirective::HelperExprs B;
5858 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5859 // define the nested loops number.
5860 unsigned NestedLoopCount =
5861 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
5862 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5863 VarsWithImplicitDSA, B);
5864 if (NestedLoopCount == 0)
5865 return StmtError();
5866
5867 assert((CurContext->isDependentContext() || B.builtAll()) &&
5868 "omp target parallel for loop exprs were not built");
5869
5870 if (!CurContext->isDependentContext()) {
5871 // Finalize the clauses that need pre-built expressions for CodeGen.
5872 for (auto C : Clauses) {
5873 if (auto LC = dyn_cast<OMPLinearClause>(C))
5874 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5875 B.NumIterations, *this, CurScope))
5876 return StmtError();
5877 }
5878 }
5879
5880 getCurFunction()->setHasBranchProtectedScope();
5881 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
5882 NestedLoopCount, Clauses, AStmt,
5883 B, DSAStack->isCancelRegion());
5884}
5885
Samuel Antaodf67fc42016-01-19 19:15:56 +00005886/// \brief Check for existence of a map clause in the list of clauses.
5887static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
5888 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
5889 I != E; ++I) {
5890 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
5891 return true;
5892 }
5893 }
5894
5895 return false;
5896}
5897
Michael Wong65f367f2015-07-21 13:44:28 +00005898StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
5899 Stmt *AStmt,
5900 SourceLocation StartLoc,
5901 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005902 if (!AStmt)
5903 return StmtError();
5904
5905 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5906
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00005907 // OpenMP [2.10.1, Restrictions, p. 97]
5908 // At least one map clause must appear on the directive.
5909 if (!HasMapClause(Clauses)) {
5910 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
5911 getOpenMPDirectiveName(OMPD_target_data);
5912 return StmtError();
5913 }
5914
Michael Wong65f367f2015-07-21 13:44:28 +00005915 getCurFunction()->setHasBranchProtectedScope();
5916
5917 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
5918 AStmt);
5919}
5920
Samuel Antaodf67fc42016-01-19 19:15:56 +00005921StmtResult
5922Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
5923 SourceLocation StartLoc,
5924 SourceLocation EndLoc) {
5925 // OpenMP [2.10.2, Restrictions, p. 99]
5926 // At least one map clause must appear on the directive.
5927 if (!HasMapClause(Clauses)) {
5928 Diag(StartLoc, diag::err_omp_no_map_for_directive)
5929 << getOpenMPDirectiveName(OMPD_target_enter_data);
5930 return StmtError();
5931 }
5932
5933 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
5934 Clauses);
5935}
5936
Samuel Antao72590762016-01-19 20:04:50 +00005937StmtResult
5938Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
5939 SourceLocation StartLoc,
5940 SourceLocation EndLoc) {
5941 // OpenMP [2.10.3, Restrictions, p. 102]
5942 // At least one map clause must appear on the directive.
5943 if (!HasMapClause(Clauses)) {
5944 Diag(StartLoc, diag::err_omp_no_map_for_directive)
5945 << getOpenMPDirectiveName(OMPD_target_exit_data);
5946 return StmtError();
5947 }
5948
5949 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
5950}
5951
Alexey Bataev13314bf2014-10-09 04:18:56 +00005952StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
5953 Stmt *AStmt, SourceLocation StartLoc,
5954 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005955 if (!AStmt)
5956 return StmtError();
5957
Alexey Bataev13314bf2014-10-09 04:18:56 +00005958 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5959 // 1.2.2 OpenMP Language Terminology
5960 // Structured block - An executable statement with a single entry at the
5961 // top and a single exit at the bottom.
5962 // The point of exit cannot be a branch out of the structured block.
5963 // longjmp() and throw() must not violate the entry/exit criteria.
5964 CS->getCapturedDecl()->setNothrow();
5965
5966 getCurFunction()->setHasBranchProtectedScope();
5967
5968 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5969}
5970
Alexey Bataev6d4ed052015-07-01 06:57:41 +00005971StmtResult
5972Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
5973 SourceLocation EndLoc,
5974 OpenMPDirectiveKind CancelRegion) {
5975 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5976 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5977 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5978 << getOpenMPDirectiveName(CancelRegion);
5979 return StmtError();
5980 }
5981 if (DSAStack->isParentNowaitRegion()) {
5982 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
5983 return StmtError();
5984 }
5985 if (DSAStack->isParentOrderedRegion()) {
5986 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
5987 return StmtError();
5988 }
5989 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
5990 CancelRegion);
5991}
5992
Alexey Bataev87933c72015-09-18 08:07:34 +00005993StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
5994 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00005995 SourceLocation EndLoc,
5996 OpenMPDirectiveKind CancelRegion) {
5997 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5998 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5999 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6000 << getOpenMPDirectiveName(CancelRegion);
6001 return StmtError();
6002 }
6003 if (DSAStack->isParentNowaitRegion()) {
6004 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6005 return StmtError();
6006 }
6007 if (DSAStack->isParentOrderedRegion()) {
6008 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6009 return StmtError();
6010 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006011 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00006012 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6013 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00006014}
6015
Alexey Bataev382967a2015-12-08 12:06:20 +00006016static bool checkGrainsizeNumTasksClauses(Sema &S,
6017 ArrayRef<OMPClause *> Clauses) {
6018 OMPClause *PrevClause = nullptr;
6019 bool ErrorFound = false;
6020 for (auto *C : Clauses) {
6021 if (C->getClauseKind() == OMPC_grainsize ||
6022 C->getClauseKind() == OMPC_num_tasks) {
6023 if (!PrevClause)
6024 PrevClause = C;
6025 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6026 S.Diag(C->getLocStart(),
6027 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6028 << getOpenMPClauseName(C->getClauseKind())
6029 << getOpenMPClauseName(PrevClause->getClauseKind());
6030 S.Diag(PrevClause->getLocStart(),
6031 diag::note_omp_previous_grainsize_num_tasks)
6032 << getOpenMPClauseName(PrevClause->getClauseKind());
6033 ErrorFound = true;
6034 }
6035 }
6036 }
6037 return ErrorFound;
6038}
6039
Alexey Bataev49f6e782015-12-01 04:18:41 +00006040StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6041 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6042 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006043 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00006044 if (!AStmt)
6045 return StmtError();
6046
6047 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6048 OMPLoopDirective::HelperExprs B;
6049 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6050 // define the nested loops number.
6051 unsigned NestedLoopCount =
6052 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006053 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00006054 VarsWithImplicitDSA, B);
6055 if (NestedLoopCount == 0)
6056 return StmtError();
6057
6058 assert((CurContext->isDependentContext() || B.builtAll()) &&
6059 "omp for loop exprs were not built");
6060
Alexey Bataev382967a2015-12-08 12:06:20 +00006061 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6062 // The grainsize clause and num_tasks clause are mutually exclusive and may
6063 // not appear on the same taskloop directive.
6064 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6065 return StmtError();
6066
Alexey Bataev49f6e782015-12-01 04:18:41 +00006067 getCurFunction()->setHasBranchProtectedScope();
6068 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
6069 NestedLoopCount, Clauses, AStmt, B);
6070}
6071
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006072StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
6073 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6074 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006075 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006076 if (!AStmt)
6077 return StmtError();
6078
6079 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6080 OMPLoopDirective::HelperExprs B;
6081 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6082 // define the nested loops number.
6083 unsigned NestedLoopCount =
6084 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
6085 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6086 VarsWithImplicitDSA, B);
6087 if (NestedLoopCount == 0)
6088 return StmtError();
6089
6090 assert((CurContext->isDependentContext() || B.builtAll()) &&
6091 "omp for loop exprs were not built");
6092
Alexey Bataev382967a2015-12-08 12:06:20 +00006093 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6094 // The grainsize clause and num_tasks clause are mutually exclusive and may
6095 // not appear on the same taskloop directive.
6096 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6097 return StmtError();
6098
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006099 getCurFunction()->setHasBranchProtectedScope();
6100 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
6101 NestedLoopCount, Clauses, AStmt, B);
6102}
6103
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006104StmtResult Sema::ActOnOpenMPDistributeDirective(
6105 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6106 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006107 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006108 if (!AStmt)
6109 return StmtError();
6110
6111 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6112 OMPLoopDirective::HelperExprs B;
6113 // In presence of clause 'collapse' with number of loops, it will
6114 // define the nested loops number.
6115 unsigned NestedLoopCount =
6116 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
6117 nullptr /*ordered not a clause on distribute*/, AStmt,
6118 *this, *DSAStack, VarsWithImplicitDSA, B);
6119 if (NestedLoopCount == 0)
6120 return StmtError();
6121
6122 assert((CurContext->isDependentContext() || B.builtAll()) &&
6123 "omp for loop exprs were not built");
6124
6125 getCurFunction()->setHasBranchProtectedScope();
6126 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
6127 NestedLoopCount, Clauses, AStmt, B);
6128}
6129
Alexey Bataeved09d242014-05-28 05:53:51 +00006130OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006131 SourceLocation StartLoc,
6132 SourceLocation LParenLoc,
6133 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006134 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006135 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00006136 case OMPC_final:
6137 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
6138 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00006139 case OMPC_num_threads:
6140 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
6141 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006142 case OMPC_safelen:
6143 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
6144 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00006145 case OMPC_simdlen:
6146 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
6147 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00006148 case OMPC_collapse:
6149 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
6150 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006151 case OMPC_ordered:
6152 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
6153 break;
Michael Wonge710d542015-08-07 16:16:36 +00006154 case OMPC_device:
6155 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
6156 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00006157 case OMPC_num_teams:
6158 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
6159 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006160 case OMPC_thread_limit:
6161 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
6162 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00006163 case OMPC_priority:
6164 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
6165 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006166 case OMPC_grainsize:
6167 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
6168 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00006169 case OMPC_num_tasks:
6170 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
6171 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00006172 case OMPC_hint:
6173 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
6174 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006175 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006176 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006177 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006178 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006179 case OMPC_private:
6180 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006181 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006182 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006183 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006184 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006185 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006186 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006187 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00006188 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006189 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006190 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006191 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006192 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006193 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006194 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006195 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006196 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006197 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006198 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00006199 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006200 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006201 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00006202 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006203 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006204 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006205 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006206 llvm_unreachable("Clause is not allowed.");
6207 }
6208 return Res;
6209}
6210
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006211OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
6212 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006213 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006214 SourceLocation NameModifierLoc,
6215 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006216 SourceLocation EndLoc) {
6217 Expr *ValExpr = Condition;
6218 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6219 !Condition->isInstantiationDependent() &&
6220 !Condition->containsUnexpandedParameterPack()) {
6221 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00006222 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006223 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006224 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006225
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006226 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006227 }
6228
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006229 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
6230 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006231}
6232
Alexey Bataev3778b602014-07-17 07:32:53 +00006233OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
6234 SourceLocation StartLoc,
6235 SourceLocation LParenLoc,
6236 SourceLocation EndLoc) {
6237 Expr *ValExpr = Condition;
6238 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6239 !Condition->isInstantiationDependent() &&
6240 !Condition->containsUnexpandedParameterPack()) {
6241 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
6242 Condition->getExprLoc(), Condition);
6243 if (Val.isInvalid())
6244 return nullptr;
6245
6246 ValExpr = Val.get();
6247 }
6248
6249 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
6250}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006251ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
6252 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00006253 if (!Op)
6254 return ExprError();
6255
6256 class IntConvertDiagnoser : public ICEConvertDiagnoser {
6257 public:
6258 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00006259 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00006260 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
6261 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006262 return S.Diag(Loc, diag::err_omp_not_integral) << T;
6263 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006264 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
6265 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006266 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
6267 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006268 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
6269 QualType T,
6270 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006271 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
6272 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006273 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
6274 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006275 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006276 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006277 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006278 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
6279 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006280 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
6281 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006282 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
6283 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006284 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006285 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006286 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006287 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
6288 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006289 llvm_unreachable("conversion functions are permitted");
6290 }
6291 } ConvertDiagnoser;
6292 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
6293}
6294
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006295static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00006296 OpenMPClauseKind CKind,
6297 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006298 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
6299 !ValExpr->isInstantiationDependent()) {
6300 SourceLocation Loc = ValExpr->getExprLoc();
6301 ExprResult Value =
6302 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
6303 if (Value.isInvalid())
6304 return false;
6305
6306 ValExpr = Value.get();
6307 // The expression must evaluate to a non-negative integer value.
6308 llvm::APSInt Result;
6309 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00006310 Result.isSigned() &&
6311 !((!StrictlyPositive && Result.isNonNegative()) ||
6312 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006313 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006314 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6315 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006316 return false;
6317 }
6318 }
6319 return true;
6320}
6321
Alexey Bataev568a8332014-03-06 06:15:19 +00006322OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
6323 SourceLocation StartLoc,
6324 SourceLocation LParenLoc,
6325 SourceLocation EndLoc) {
6326 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00006327
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006328 // OpenMP [2.5, Restrictions]
6329 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00006330 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
6331 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006332 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00006333
Alexey Bataeved09d242014-05-28 05:53:51 +00006334 return new (Context)
6335 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00006336}
6337
Alexey Bataev62c87d22014-03-21 04:51:18 +00006338ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006339 OpenMPClauseKind CKind,
6340 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006341 if (!E)
6342 return ExprError();
6343 if (E->isValueDependent() || E->isTypeDependent() ||
6344 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00006345 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006346 llvm::APSInt Result;
6347 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
6348 if (ICE.isInvalid())
6349 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006350 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
6351 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006352 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006353 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6354 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00006355 return ExprError();
6356 }
Alexander Musman09184fe2014-09-30 05:29:28 +00006357 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
6358 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
6359 << E->getSourceRange();
6360 return ExprError();
6361 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006362 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
6363 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00006364 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006365 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00006366 return ICE;
6367}
6368
6369OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
6370 SourceLocation LParenLoc,
6371 SourceLocation EndLoc) {
6372 // OpenMP [2.8.1, simd construct, Description]
6373 // The parameter of the safelen clause must be a constant
6374 // positive integer expression.
6375 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
6376 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006377 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006378 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006379 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00006380}
6381
Alexey Bataev66b15b52015-08-21 11:14:16 +00006382OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
6383 SourceLocation LParenLoc,
6384 SourceLocation EndLoc) {
6385 // OpenMP [2.8.1, simd construct, Description]
6386 // The parameter of the simdlen clause must be a constant
6387 // positive integer expression.
6388 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
6389 if (Simdlen.isInvalid())
6390 return nullptr;
6391 return new (Context)
6392 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
6393}
6394
Alexander Musman64d33f12014-06-04 07:53:32 +00006395OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
6396 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00006397 SourceLocation LParenLoc,
6398 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00006399 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006400 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00006401 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006402 // The parameter of the collapse clause must be a constant
6403 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00006404 ExprResult NumForLoopsResult =
6405 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
6406 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00006407 return nullptr;
6408 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00006409 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00006410}
6411
Alexey Bataev10e775f2015-07-30 11:36:16 +00006412OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
6413 SourceLocation EndLoc,
6414 SourceLocation LParenLoc,
6415 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00006416 // OpenMP [2.7.1, loop construct, Description]
6417 // OpenMP [2.8.1, simd construct, Description]
6418 // OpenMP [2.9.6, distribute construct, Description]
6419 // The parameter of the ordered clause must be a constant
6420 // positive integer expression if any.
6421 if (NumForLoops && LParenLoc.isValid()) {
6422 ExprResult NumForLoopsResult =
6423 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
6424 if (NumForLoopsResult.isInvalid())
6425 return nullptr;
6426 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00006427 } else
6428 NumForLoops = nullptr;
6429 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00006430 return new (Context)
6431 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
6432}
6433
Alexey Bataeved09d242014-05-28 05:53:51 +00006434OMPClause *Sema::ActOnOpenMPSimpleClause(
6435 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
6436 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006437 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006438 switch (Kind) {
6439 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006440 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00006441 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
6442 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006443 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006444 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00006445 Res = ActOnOpenMPProcBindClause(
6446 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
6447 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006448 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006449 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006450 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006451 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006452 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006453 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006454 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006455 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006456 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006457 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006458 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006459 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006460 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006461 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006462 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006463 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006464 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006465 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006466 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006467 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006468 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006469 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006470 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006471 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006472 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006473 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006474 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006475 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006476 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006477 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006478 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006479 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006480 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006481 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006482 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006483 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006484 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006485 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006486 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006487 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006488 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006489 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006490 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006491 llvm_unreachable("Clause is not allowed.");
6492 }
6493 return Res;
6494}
6495
Alexey Bataev6402bca2015-12-28 07:25:51 +00006496static std::string
6497getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
6498 ArrayRef<unsigned> Exclude = llvm::None) {
6499 std::string Values;
6500 unsigned Bound = Last >= 2 ? Last - 2 : 0;
6501 unsigned Skipped = Exclude.size();
6502 auto S = Exclude.begin(), E = Exclude.end();
6503 for (unsigned i = First; i < Last; ++i) {
6504 if (std::find(S, E, i) != E) {
6505 --Skipped;
6506 continue;
6507 }
6508 Values += "'";
6509 Values += getOpenMPSimpleClauseTypeName(K, i);
6510 Values += "'";
6511 if (i == Bound - Skipped)
6512 Values += " or ";
6513 else if (i != Bound + 1 - Skipped)
6514 Values += ", ";
6515 }
6516 return Values;
6517}
6518
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006519OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
6520 SourceLocation KindKwLoc,
6521 SourceLocation StartLoc,
6522 SourceLocation LParenLoc,
6523 SourceLocation EndLoc) {
6524 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00006525 static_assert(OMPC_DEFAULT_unknown > 0,
6526 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006527 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006528 << getListOfPossibleValues(OMPC_default, /*First=*/0,
6529 /*Last=*/OMPC_DEFAULT_unknown)
6530 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006531 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006532 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00006533 switch (Kind) {
6534 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006535 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006536 break;
6537 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006538 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006539 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006540 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006541 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00006542 break;
6543 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006544 return new (Context)
6545 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006546}
6547
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006548OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
6549 SourceLocation KindKwLoc,
6550 SourceLocation StartLoc,
6551 SourceLocation LParenLoc,
6552 SourceLocation EndLoc) {
6553 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006554 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006555 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
6556 /*Last=*/OMPC_PROC_BIND_unknown)
6557 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006558 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006559 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006560 return new (Context)
6561 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006562}
6563
Alexey Bataev56dafe82014-06-20 07:16:17 +00006564OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006565 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006566 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006567 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006568 SourceLocation EndLoc) {
6569 OMPClause *Res = nullptr;
6570 switch (Kind) {
6571 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006572 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
6573 assert(Argument.size() == NumberOfElements &&
6574 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006575 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006576 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
6577 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
6578 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
6579 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
6580 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006581 break;
6582 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006583 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
6584 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
6585 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
6586 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006587 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00006588 case OMPC_dist_schedule:
6589 Res = ActOnOpenMPDistScheduleClause(
6590 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
6591 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
6592 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006593 case OMPC_defaultmap:
6594 enum { Modifier, DefaultmapKind };
6595 Res = ActOnOpenMPDefaultmapClause(
6596 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
6597 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
6598 StartLoc, LParenLoc, ArgumentLoc[Modifier],
6599 ArgumentLoc[DefaultmapKind], EndLoc);
6600 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00006601 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006602 case OMPC_num_threads:
6603 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006604 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006605 case OMPC_collapse:
6606 case OMPC_default:
6607 case OMPC_proc_bind:
6608 case OMPC_private:
6609 case OMPC_firstprivate:
6610 case OMPC_lastprivate:
6611 case OMPC_shared:
6612 case OMPC_reduction:
6613 case OMPC_linear:
6614 case OMPC_aligned:
6615 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006616 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006617 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006618 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006619 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006620 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006621 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006622 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006623 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006624 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006625 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006626 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006627 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006628 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006629 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006630 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006631 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006632 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006633 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006634 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006635 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006636 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006637 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006638 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006639 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006640 case OMPC_unknown:
6641 llvm_unreachable("Clause is not allowed.");
6642 }
6643 return Res;
6644}
6645
Alexey Bataev6402bca2015-12-28 07:25:51 +00006646static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
6647 OpenMPScheduleClauseModifier M2,
6648 SourceLocation M1Loc, SourceLocation M2Loc) {
6649 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
6650 SmallVector<unsigned, 2> Excluded;
6651 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
6652 Excluded.push_back(M2);
6653 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
6654 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
6655 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
6656 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
6657 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
6658 << getListOfPossibleValues(OMPC_schedule,
6659 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
6660 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6661 Excluded)
6662 << getOpenMPClauseName(OMPC_schedule);
6663 return true;
6664 }
6665 return false;
6666}
6667
Alexey Bataev56dafe82014-06-20 07:16:17 +00006668OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006669 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006670 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006671 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
6672 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
6673 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
6674 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
6675 return nullptr;
6676 // OpenMP, 2.7.1, Loop Construct, Restrictions
6677 // Either the monotonic modifier or the nonmonotonic modifier can be specified
6678 // but not both.
6679 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
6680 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
6681 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
6682 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
6683 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
6684 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
6685 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
6686 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
6687 return nullptr;
6688 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006689 if (Kind == OMPC_SCHEDULE_unknown) {
6690 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00006691 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
6692 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
6693 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6694 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6695 Exclude);
6696 } else {
6697 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6698 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006699 }
6700 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
6701 << Values << getOpenMPClauseName(OMPC_schedule);
6702 return nullptr;
6703 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00006704 // OpenMP, 2.7.1, Loop Construct, Restrictions
6705 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
6706 // schedule(guided).
6707 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
6708 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
6709 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
6710 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
6711 diag::err_omp_schedule_nonmonotonic_static);
6712 return nullptr;
6713 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006714 Expr *ValExpr = ChunkSize;
Alexey Bataev040d5402015-05-12 08:35:28 +00006715 Expr *HelperValExpr = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006716 if (ChunkSize) {
6717 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
6718 !ChunkSize->isInstantiationDependent() &&
6719 !ChunkSize->containsUnexpandedParameterPack()) {
6720 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
6721 ExprResult Val =
6722 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
6723 if (Val.isInvalid())
6724 return nullptr;
6725
6726 ValExpr = Val.get();
6727
6728 // OpenMP [2.7.1, Restrictions]
6729 // chunk_size must be a loop invariant integer expression with a positive
6730 // value.
6731 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00006732 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
6733 if (Result.isSigned() && !Result.isStrictlyPositive()) {
6734 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006735 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00006736 return nullptr;
6737 }
6738 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
6739 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
6740 ChunkSize->getType(), ".chunk.");
6741 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
6742 ChunkSize->getExprLoc(),
6743 /*RefersToCapture=*/true);
6744 HelperValExpr = ImpVarRef;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006745 }
6746 }
6747 }
6748
Alexey Bataev6402bca2015-12-28 07:25:51 +00006749 return new (Context)
6750 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
6751 ValExpr, HelperValExpr, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006752}
6753
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006754OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
6755 SourceLocation StartLoc,
6756 SourceLocation EndLoc) {
6757 OMPClause *Res = nullptr;
6758 switch (Kind) {
6759 case OMPC_ordered:
6760 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
6761 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00006762 case OMPC_nowait:
6763 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
6764 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006765 case OMPC_untied:
6766 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
6767 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006768 case OMPC_mergeable:
6769 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
6770 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006771 case OMPC_read:
6772 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
6773 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00006774 case OMPC_write:
6775 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
6776 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006777 case OMPC_update:
6778 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
6779 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00006780 case OMPC_capture:
6781 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
6782 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006783 case OMPC_seq_cst:
6784 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
6785 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00006786 case OMPC_threads:
6787 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
6788 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006789 case OMPC_simd:
6790 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
6791 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00006792 case OMPC_nogroup:
6793 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
6794 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006795 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006796 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006797 case OMPC_num_threads:
6798 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006799 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006800 case OMPC_collapse:
6801 case OMPC_schedule:
6802 case OMPC_private:
6803 case OMPC_firstprivate:
6804 case OMPC_lastprivate:
6805 case OMPC_shared:
6806 case OMPC_reduction:
6807 case OMPC_linear:
6808 case OMPC_aligned:
6809 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006810 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006811 case OMPC_default:
6812 case OMPC_proc_bind:
6813 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006814 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006815 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006816 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006817 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006818 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006819 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006820 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006821 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00006822 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006823 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006824 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006825 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006826 case OMPC_unknown:
6827 llvm_unreachable("Clause is not allowed.");
6828 }
6829 return Res;
6830}
6831
Alexey Bataev236070f2014-06-20 11:19:47 +00006832OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
6833 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006834 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00006835 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
6836}
6837
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006838OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
6839 SourceLocation EndLoc) {
6840 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
6841}
6842
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006843OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
6844 SourceLocation EndLoc) {
6845 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
6846}
6847
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006848OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
6849 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006850 return new (Context) OMPReadClause(StartLoc, EndLoc);
6851}
6852
Alexey Bataevdea47612014-07-23 07:46:59 +00006853OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
6854 SourceLocation EndLoc) {
6855 return new (Context) OMPWriteClause(StartLoc, EndLoc);
6856}
6857
Alexey Bataev67a4f222014-07-23 10:25:33 +00006858OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
6859 SourceLocation EndLoc) {
6860 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
6861}
6862
Alexey Bataev459dec02014-07-24 06:46:57 +00006863OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
6864 SourceLocation EndLoc) {
6865 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
6866}
6867
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006868OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
6869 SourceLocation EndLoc) {
6870 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
6871}
6872
Alexey Bataev346265e2015-09-25 10:37:12 +00006873OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
6874 SourceLocation EndLoc) {
6875 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
6876}
6877
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006878OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
6879 SourceLocation EndLoc) {
6880 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
6881}
6882
Alexey Bataevb825de12015-12-07 10:51:44 +00006883OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
6884 SourceLocation EndLoc) {
6885 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
6886}
6887
Alexey Bataevc5e02582014-06-16 07:08:35 +00006888OMPClause *Sema::ActOnOpenMPVarListClause(
6889 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
6890 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
6891 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006892 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00006893 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
6894 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
6895 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006896 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006897 switch (Kind) {
6898 case OMPC_private:
6899 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6900 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006901 case OMPC_firstprivate:
6902 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6903 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006904 case OMPC_lastprivate:
6905 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6906 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00006907 case OMPC_shared:
6908 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
6909 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006910 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00006911 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
6912 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006913 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00006914 case OMPC_linear:
6915 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00006916 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00006917 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006918 case OMPC_aligned:
6919 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
6920 ColonLoc, EndLoc);
6921 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006922 case OMPC_copyin:
6923 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
6924 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006925 case OMPC_copyprivate:
6926 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6927 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00006928 case OMPC_flush:
6929 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
6930 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006931 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006932 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
6933 StartLoc, LParenLoc, EndLoc);
6934 break;
6935 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00006936 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
6937 DepLinMapLoc, ColonLoc, VarList, StartLoc,
6938 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006939 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006940 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006941 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006942 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006943 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006944 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006945 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006946 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006947 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006948 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006949 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006950 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006951 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006952 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006953 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006954 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006955 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006956 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006957 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006958 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00006959 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006960 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006961 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006962 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006963 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006964 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006965 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006966 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006967 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006968 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006969 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006970 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006971 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006972 llvm_unreachable("Clause is not allowed.");
6973 }
6974 return Res;
6975}
6976
Alexey Bataev90c228f2016-02-08 09:29:13 +00006977static DeclRefExpr *buildCapture(Sema &S, IdentifierInfo *Id,
6978 Expr *CaptureExpr) {
6979 ASTContext &C = S.getASTContext();
6980 Expr *Init = CaptureExpr->IgnoreImpCasts();
6981 QualType Ty = Init->getType();
6982 if (CaptureExpr->getObjectKind() == OK_Ordinary) {
6983 if (S.getLangOpts().CPlusPlus)
6984 Ty = C.getLValueReferenceType(Ty);
6985 else {
6986 Ty = C.getPointerType(Ty);
6987 ExprResult Res =
6988 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
6989 if (!Res.isUsable())
6990 return nullptr;
6991 Init = Res.get();
6992 }
6993 }
6994 auto *CFD = OMPCapturedFieldDecl::Create(C, S.CurContext, Id, Ty);
6995 S.CurContext->addHiddenDecl(CFD);
6996 S.AddInitializerToDecl(CFD, Init, /*DirectInit=*/false,
6997 /*TypeMayContainAuto=*/true);
6998 return buildDeclRefExpr(S, CFD, Ty.getNonReferenceType(), SourceLocation());
6999}
7000
7001ExprResult 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 Bataev5ec3eb12013-07-19 03:13:43 +00007021OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
7022 SourceLocation StartLoc,
7023 SourceLocation LParenLoc,
7024 SourceLocation EndLoc) {
7025 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00007026 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00007027 for (auto &RefExpr : VarList) {
7028 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007029 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7030 RefExpr->containsUnexpandedParameterPack()) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007031 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007032 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007033 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007034 continue;
7035 }
7036
Alexey Bataeved09d242014-05-28 05:53:51 +00007037 SourceLocation ELoc = RefExpr->getExprLoc();
Carlo Bertolli9e8c6c12016-01-19 16:53:55 +00007038 // OpenMP [3.1, C/C++]
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007039 // A list item is a variable name.
7040 // OpenMP [2.9.3.3, Restrictions, p.1]
7041 // A variable that is part of another variable (as an array or
7042 // structure element) cannot appear in a private clause.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007043 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr->IgnoreParens());
7044 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr->IgnoreParens());
7045 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
7046 (getCurrentThisType().isNull() || !ME ||
7047 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
7048 !isa<FieldDecl>(ME->getMemberDecl()))) {
7049 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7050 << (getCurrentThisType().isNull() ? 0 : 1)
7051 << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007052 continue;
7053 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007054 ValueDecl *D = DE ? DE->getDecl() : ME->getMemberDecl();
7055 QualType Type = D->getType();
7056 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007057
7058 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7059 // A variable that appears in a private clause must not have an incomplete
7060 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007061 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007062 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007063 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007064
Alexey Bataev758e55e2013-09-06 18:03:48 +00007065 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7066 // in a Construct]
7067 // Variables with the predetermined data-sharing attributes may not be
7068 // listed in data-sharing attributes clauses, except for the cases
7069 // listed below. For these exceptions only, listing a predetermined
7070 // variable in a data-sharing attribute clause is allowed and overrides
7071 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007072 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007073 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007074 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7075 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007076 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007077 continue;
7078 }
7079
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007080 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007081 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007082 DSAStack->getCurrentDirective() == OMPD_task) {
7083 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7084 << getOpenMPClauseName(OMPC_private) << Type
7085 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7086 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007087 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007088 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007089 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007090 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007091 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007092 continue;
7093 }
7094
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007095 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
7096 // A variable of class type (or array thereof) that appears in a private
7097 // clause requires an accessible, unambiguous default constructor for the
7098 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00007099 // Generate helper private variable and initialize it with the default
7100 // value. The address of the original variable is replaced by the address of
7101 // the new private variable in CodeGen. This new variable is not added to
7102 // IdResolver, so the code in the OpenMP region uses original variable for
7103 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007104 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007105 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7106 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007107 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007108 if (VDPrivate->isInvalidDecl())
7109 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007110 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007111 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007112
Alexey Bataev90c228f2016-02-08 09:29:13 +00007113 DeclRefExpr *Ref = nullptr;
7114 if (!VD)
7115 Ref = buildCapture(*this, D->getIdentifier(), RefExpr);
7116 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
7117 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007118 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007119 }
7120
Alexey Bataeved09d242014-05-28 05:53:51 +00007121 if (Vars.empty())
7122 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007123
Alexey Bataev03b340a2014-10-21 03:16:40 +00007124 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7125 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007126}
7127
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007128namespace {
7129class DiagsUninitializedSeveretyRAII {
7130private:
7131 DiagnosticsEngine &Diags;
7132 SourceLocation SavedLoc;
7133 bool IsIgnored;
7134
7135public:
7136 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
7137 bool IsIgnored)
7138 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
7139 if (!IsIgnored) {
7140 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
7141 /*Map*/ diag::Severity::Ignored, Loc);
7142 }
7143 }
7144 ~DiagsUninitializedSeveretyRAII() {
7145 if (!IsIgnored)
7146 Diags.popMappings(SavedLoc);
7147 }
7148};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007149}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007150
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007151OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
7152 SourceLocation StartLoc,
7153 SourceLocation LParenLoc,
7154 SourceLocation EndLoc) {
7155 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007156 SmallVector<Expr *, 8> PrivateCopies;
7157 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007158 bool IsImplicitClause =
7159 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
7160 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
7161
Alexey Bataeved09d242014-05-28 05:53:51 +00007162 for (auto &RefExpr : VarList) {
7163 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
7164 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007165 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007166 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007167 PrivateCopies.push_back(nullptr);
7168 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007169 continue;
7170 }
7171
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007172 SourceLocation ELoc =
7173 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007174 // OpenMP [2.1, C/C++]
7175 // A list item is a variable name.
7176 // OpenMP [2.9.3.3, Restrictions, p.1]
7177 // A variable that is part of another variable (as an array or
7178 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00007179 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007180 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007181 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7182 << 0 << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007183 continue;
7184 }
7185 Decl *D = DE->getDecl();
7186 VarDecl *VD = cast<VarDecl>(D);
7187
7188 QualType Type = VD->getType();
7189 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7190 // It will be analyzed later.
7191 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007192 PrivateCopies.push_back(nullptr);
7193 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007194 continue;
7195 }
7196
7197 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7198 // A variable that appears in a private clause must not have an incomplete
7199 // type or a reference type.
7200 if (RequireCompleteType(ELoc, Type,
7201 diag::err_omp_firstprivate_incomplete_type)) {
7202 continue;
7203 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007204 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007205
7206 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
7207 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00007208 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007209 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007210 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007211
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007212 // If an implicit firstprivate variable found it was checked already.
7213 if (!IsImplicitClause) {
7214 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007215 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007216 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
7217 // A list item that specifies a given variable may not appear in more
7218 // than one clause on the same directive, except that a variable may be
7219 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007220 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00007221 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007222 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007223 << getOpenMPClauseName(DVar.CKind)
7224 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007225 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007226 continue;
7227 }
7228
7229 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7230 // in a Construct]
7231 // Variables with the predetermined data-sharing attributes may not be
7232 // listed in data-sharing attributes clauses, except for the cases
7233 // listed below. For these exceptions only, listing a predetermined
7234 // variable in a data-sharing attribute clause is allowed and overrides
7235 // the variable's predetermined data-sharing attributes.
7236 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7237 // in a Construct, C/C++, p.2]
7238 // Variables with const-qualified type having no mutable member may be
7239 // listed in a firstprivate clause, even if they are static data members.
7240 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
7241 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
7242 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007243 << getOpenMPClauseName(DVar.CKind)
7244 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007245 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007246 continue;
7247 }
7248
Alexey Bataevf29276e2014-06-18 04:14:57 +00007249 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007250 // OpenMP [2.9.3.4, Restrictions, p.2]
7251 // A list item that is private within a parallel region must not appear
7252 // in a firstprivate clause on a worksharing construct if any of the
7253 // worksharing regions arising from the worksharing construct ever bind
7254 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00007255 if (isOpenMPWorksharingDirective(CurrDir) &&
7256 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007257 DVar = DSAStack->getImplicitDSA(VD, true);
7258 if (DVar.CKind != OMPC_shared &&
7259 (isOpenMPParallelDirective(DVar.DKind) ||
7260 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00007261 Diag(ELoc, diag::err_omp_required_access)
7262 << getOpenMPClauseName(OMPC_firstprivate)
7263 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007264 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007265 continue;
7266 }
7267 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007268 // OpenMP [2.9.3.4, Restrictions, p.3]
7269 // A list item that appears in a reduction clause of a parallel construct
7270 // must not appear in a firstprivate clause on a worksharing or task
7271 // construct if any of the worksharing or task regions arising from the
7272 // worksharing or task construct ever bind to any of the parallel regions
7273 // arising from the parallel construct.
7274 // OpenMP [2.9.3.4, Restrictions, p.4]
7275 // A list item that appears in a reduction clause in worksharing
7276 // construct must not appear in a firstprivate clause in a task construct
7277 // encountered during execution of any of the worksharing regions arising
7278 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007279 if (CurrDir == OMPD_task) {
7280 DVar =
7281 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
7282 [](OpenMPDirectiveKind K) -> bool {
7283 return isOpenMPParallelDirective(K) ||
7284 isOpenMPWorksharingDirective(K);
7285 },
7286 false);
7287 if (DVar.CKind == OMPC_reduction &&
7288 (isOpenMPParallelDirective(DVar.DKind) ||
7289 isOpenMPWorksharingDirective(DVar.DKind))) {
7290 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
7291 << getOpenMPDirectiveName(DVar.DKind);
7292 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7293 continue;
7294 }
7295 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007296
7297 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7298 // A list item that is private within a teams region must not appear in a
7299 // firstprivate clause on a distribute construct if any of the distribute
7300 // regions arising from the distribute construct ever bind to any of the
7301 // teams regions arising from the teams construct.
7302 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7303 // A list item that appears in a reduction clause of a teams construct
7304 // must not appear in a firstprivate clause on a distribute construct if
7305 // any of the distribute regions arising from the distribute construct
7306 // ever bind to any of the teams regions arising from the teams construct.
7307 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7308 // A list item may appear in a firstprivate or lastprivate clause but not
7309 // both.
7310 if (CurrDir == OMPD_distribute) {
7311 DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_private),
7312 [](OpenMPDirectiveKind K) -> bool {
7313 return isOpenMPTeamsDirective(K);
7314 },
7315 false);
7316 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
7317 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
7318 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7319 continue;
7320 }
7321 DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
7322 [](OpenMPDirectiveKind K) -> bool {
7323 return isOpenMPTeamsDirective(K);
7324 },
7325 false);
7326 if (DVar.CKind == OMPC_reduction &&
7327 isOpenMPTeamsDirective(DVar.DKind)) {
7328 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
7329 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7330 continue;
7331 }
7332 DVar = DSAStack->getTopDSA(VD, false);
7333 if (DVar.CKind == OMPC_lastprivate) {
7334 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
7335 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7336 continue;
7337 }
7338 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007339 }
7340
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007341 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007342 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007343 DSAStack->getCurrentDirective() == OMPD_task) {
7344 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7345 << getOpenMPClauseName(OMPC_firstprivate) << Type
7346 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7347 bool IsDecl =
7348 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7349 Diag(VD->getLocation(),
7350 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7351 << VD;
7352 continue;
7353 }
7354
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007355 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007356 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName(),
7357 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007358 // Generate helper private variable and initialize it with the value of the
7359 // original variable. The address of the original variable is replaced by
7360 // the address of the new private variable in the CodeGen. This new variable
7361 // is not added to IdResolver, so the code in the OpenMP region uses
7362 // original variable for proper diagnostics and variable capturing.
7363 Expr *VDInitRefExpr = nullptr;
7364 // For arrays generate initializer for single element and replace it by the
7365 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007366 if (Type->isArrayType()) {
7367 auto VDInit =
7368 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
7369 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007370 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007371 ElemType = ElemType.getUnqualifiedType();
7372 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
7373 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00007374 InitializedEntity Entity =
7375 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007376 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
7377
7378 InitializationSequence InitSeq(*this, Entity, Kind, Init);
7379 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
7380 if (Result.isInvalid())
7381 VDPrivate->setInvalidDecl();
7382 else
7383 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007384 // Remove temp variable declaration.
7385 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007386 } else {
Alexey Bataev69c62a92015-04-15 04:52:20 +00007387 auto *VDInit =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007388 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007389 VDInitRefExpr =
7390 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00007391 AddInitializerToDecl(VDPrivate,
7392 DefaultLvalueConversion(VDInitRefExpr).get(),
7393 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007394 }
7395 if (VDPrivate->isInvalidDecl()) {
7396 if (IsImplicitClause) {
7397 Diag(DE->getExprLoc(),
7398 diag::note_omp_task_predetermined_firstprivate_here);
7399 }
7400 continue;
7401 }
7402 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007403 auto VDPrivateRefExpr = buildDeclRefExpr(
7404 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007405 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
7406 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007407 PrivateCopies.push_back(VDPrivateRefExpr);
7408 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007409 }
7410
Alexey Bataeved09d242014-05-28 05:53:51 +00007411 if (Vars.empty())
7412 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007413
7414 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007415 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007416}
7417
Alexander Musman1bb328c2014-06-04 13:06:39 +00007418OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
7419 SourceLocation StartLoc,
7420 SourceLocation LParenLoc,
7421 SourceLocation EndLoc) {
7422 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00007423 SmallVector<Expr *, 8> SrcExprs;
7424 SmallVector<Expr *, 8> DstExprs;
7425 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007426 for (auto &RefExpr : VarList) {
7427 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
7428 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7429 // It will be analyzed later.
7430 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007431 SrcExprs.push_back(nullptr);
7432 DstExprs.push_back(nullptr);
7433 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007434 continue;
7435 }
7436
7437 SourceLocation ELoc = RefExpr->getExprLoc();
7438 // OpenMP [2.1, C/C++]
7439 // A list item is a variable name.
7440 // OpenMP [2.14.3.5, Restrictions, p.1]
7441 // A variable that is part of another variable (as an array or structure
7442 // element) cannot appear in a lastprivate clause.
7443 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
7444 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007445 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7446 << 0 << RefExpr->getSourceRange();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007447 continue;
7448 }
7449 Decl *D = DE->getDecl();
7450 VarDecl *VD = cast<VarDecl>(D);
7451
7452 QualType Type = VD->getType();
7453 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7454 // It will be analyzed later.
7455 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00007456 SrcExprs.push_back(nullptr);
7457 DstExprs.push_back(nullptr);
7458 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007459 continue;
7460 }
7461
7462 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
7463 // A variable that appears in a lastprivate clause must not have an
7464 // incomplete type or a reference type.
7465 if (RequireCompleteType(ELoc, Type,
7466 diag::err_omp_lastprivate_incomplete_type)) {
7467 continue;
7468 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007469 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007470
7471 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7472 // in a Construct]
7473 // Variables with the predetermined data-sharing attributes may not be
7474 // listed in data-sharing attributes clauses, except for the cases
7475 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007476 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007477 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
7478 DVar.CKind != OMPC_firstprivate &&
7479 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
7480 Diag(ELoc, diag::err_omp_wrong_dsa)
7481 << getOpenMPClauseName(DVar.CKind)
7482 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007483 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007484 continue;
7485 }
7486
Alexey Bataevf29276e2014-06-18 04:14:57 +00007487 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7488 // OpenMP [2.14.3.5, Restrictions, p.2]
7489 // A list item that is private within a parallel region, or that appears in
7490 // the reduction clause of a parallel construct, must not appear in a
7491 // lastprivate clause on a worksharing construct if any of the corresponding
7492 // worksharing regions ever binds to any of the corresponding parallel
7493 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00007494 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00007495 if (isOpenMPWorksharingDirective(CurrDir) &&
7496 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007497 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007498 if (DVar.CKind != OMPC_shared) {
7499 Diag(ELoc, diag::err_omp_required_access)
7500 << getOpenMPClauseName(OMPC_lastprivate)
7501 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007502 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007503 continue;
7504 }
7505 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00007506 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00007507 // A variable of class type (or array thereof) that appears in a
7508 // lastprivate clause requires an accessible, unambiguous default
7509 // constructor for the class type, unless the list item is also specified
7510 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00007511 // A variable of class type (or array thereof) that appears in a
7512 // lastprivate clause requires an accessible, unambiguous copy assignment
7513 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00007514 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00007515 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007516 Type.getUnqualifiedType(), ".lastprivate.src",
7517 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007518 auto *PseudoSrcExpr = buildDeclRefExpr(
7519 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00007520 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007521 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst",
7522 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007523 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007524 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00007525 // For arrays generate assignment operation for single element and replace
7526 // it by the original array element in CodeGen.
7527 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7528 PseudoDstExpr, PseudoSrcExpr);
7529 if (AssignmentOp.isInvalid())
7530 continue;
7531 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7532 /*DiscardedValue=*/true);
7533 if (AssignmentOp.isInvalid())
7534 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007535
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007536 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7537 // A list item may appear in a firstprivate or lastprivate clause but not
7538 // both.
7539 if (CurrDir == OMPD_distribute) {
7540 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
7541 if (DVar.CKind == OMPC_firstprivate) {
7542 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
7543 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7544 continue;
7545 }
7546 }
7547
Alexey Bataev39f915b82015-05-08 10:41:21 +00007548 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataevf29276e2014-06-18 04:14:57 +00007549 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007550 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00007551 SrcExprs.push_back(PseudoSrcExpr);
7552 DstExprs.push_back(PseudoDstExpr);
7553 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00007554 }
7555
7556 if (Vars.empty())
7557 return nullptr;
7558
7559 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00007560 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007561}
7562
Alexey Bataev758e55e2013-09-06 18:03:48 +00007563OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
7564 SourceLocation StartLoc,
7565 SourceLocation LParenLoc,
7566 SourceLocation EndLoc) {
7567 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00007568 for (auto &RefExpr : VarList) {
7569 assert(RefExpr && "NULL expr in OpenMP shared clause.");
7570 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00007571 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007572 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007573 continue;
7574 }
7575
Alexey Bataeved09d242014-05-28 05:53:51 +00007576 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00007577 // OpenMP [2.1, C/C++]
7578 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00007579 // OpenMP [2.14.3.2, Restrictions, p.1]
7580 // A variable that is part of another variable (as an array or structure
7581 // element) cannot appear in a shared unless it is a static data member
7582 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00007583 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007584 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007585 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
7586 << 0 << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00007587 continue;
7588 }
7589 Decl *D = DE->getDecl();
7590 VarDecl *VD = cast<VarDecl>(D);
7591
7592 QualType Type = VD->getType();
7593 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7594 // It will be analyzed later.
7595 Vars.push_back(DE);
7596 continue;
7597 }
7598
7599 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7600 // in a Construct]
7601 // Variables with the predetermined data-sharing attributes may not be
7602 // listed in data-sharing attributes clauses, except for the cases
7603 // listed below. For these exceptions only, listing a predetermined
7604 // variable in a data-sharing attribute clause is allowed and overrides
7605 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007606 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00007607 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
7608 DVar.RefExpr) {
7609 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7610 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007611 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007612 continue;
7613 }
7614
7615 DSAStack->addDSA(VD, DE, OMPC_shared);
7616 Vars.push_back(DE);
7617 }
7618
Alexey Bataeved09d242014-05-28 05:53:51 +00007619 if (Vars.empty())
7620 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007621
7622 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
7623}
7624
Alexey Bataevc5e02582014-06-16 07:08:35 +00007625namespace {
7626class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
7627 DSAStackTy *Stack;
7628
7629public:
7630 bool VisitDeclRefExpr(DeclRefExpr *E) {
7631 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007632 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007633 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
7634 return false;
7635 if (DVar.CKind != OMPC_unknown)
7636 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007637 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007638 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007639 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007640 return true;
7641 return false;
7642 }
7643 return false;
7644 }
7645 bool VisitStmt(Stmt *S) {
7646 for (auto Child : S->children()) {
7647 if (Child && Visit(Child))
7648 return true;
7649 }
7650 return false;
7651 }
Alexey Bataev23b69422014-06-18 07:08:49 +00007652 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00007653};
Alexey Bataev23b69422014-06-18 07:08:49 +00007654} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00007655
7656OMPClause *Sema::ActOnOpenMPReductionClause(
7657 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
7658 SourceLocation ColonLoc, SourceLocation EndLoc,
7659 CXXScopeSpec &ReductionIdScopeSpec,
7660 const DeclarationNameInfo &ReductionId) {
7661 // TODO: Allow scope specification search when 'declare reduction' is
7662 // supported.
7663 assert(ReductionIdScopeSpec.isEmpty() &&
7664 "No support for scoped reduction identifiers yet.");
7665
7666 auto DN = ReductionId.getName();
7667 auto OOK = DN.getCXXOverloadedOperator();
7668 BinaryOperatorKind BOK = BO_Comma;
7669
7670 // OpenMP [2.14.3.6, reduction clause]
7671 // C
7672 // reduction-identifier is either an identifier or one of the following
7673 // operators: +, -, *, &, |, ^, && and ||
7674 // C++
7675 // reduction-identifier is either an id-expression or one of the following
7676 // operators: +, -, *, &, |, ^, && and ||
7677 // FIXME: Only 'min' and 'max' identifiers are supported for now.
7678 switch (OOK) {
7679 case OO_Plus:
7680 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007681 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007682 break;
7683 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007684 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007685 break;
7686 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007687 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007688 break;
7689 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007690 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007691 break;
7692 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007693 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007694 break;
7695 case OO_AmpAmp:
7696 BOK = BO_LAnd;
7697 break;
7698 case OO_PipePipe:
7699 BOK = BO_LOr;
7700 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007701 case OO_New:
7702 case OO_Delete:
7703 case OO_Array_New:
7704 case OO_Array_Delete:
7705 case OO_Slash:
7706 case OO_Percent:
7707 case OO_Tilde:
7708 case OO_Exclaim:
7709 case OO_Equal:
7710 case OO_Less:
7711 case OO_Greater:
7712 case OO_LessEqual:
7713 case OO_GreaterEqual:
7714 case OO_PlusEqual:
7715 case OO_MinusEqual:
7716 case OO_StarEqual:
7717 case OO_SlashEqual:
7718 case OO_PercentEqual:
7719 case OO_CaretEqual:
7720 case OO_AmpEqual:
7721 case OO_PipeEqual:
7722 case OO_LessLess:
7723 case OO_GreaterGreater:
7724 case OO_LessLessEqual:
7725 case OO_GreaterGreaterEqual:
7726 case OO_EqualEqual:
7727 case OO_ExclaimEqual:
7728 case OO_PlusPlus:
7729 case OO_MinusMinus:
7730 case OO_Comma:
7731 case OO_ArrowStar:
7732 case OO_Arrow:
7733 case OO_Call:
7734 case OO_Subscript:
7735 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00007736 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007737 case NUM_OVERLOADED_OPERATORS:
7738 llvm_unreachable("Unexpected reduction identifier");
7739 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007740 if (auto II = DN.getAsIdentifierInfo()) {
7741 if (II->isStr("max"))
7742 BOK = BO_GT;
7743 else if (II->isStr("min"))
7744 BOK = BO_LT;
7745 }
7746 break;
7747 }
7748 SourceRange ReductionIdRange;
7749 if (ReductionIdScopeSpec.isValid()) {
7750 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
7751 }
7752 ReductionIdRange.setEnd(ReductionId.getEndLoc());
7753 if (BOK == BO_Comma) {
7754 // Not allowed reduction identifier is found.
7755 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
7756 << ReductionIdRange;
7757 return nullptr;
7758 }
7759
7760 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007761 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007762 SmallVector<Expr *, 8> LHSs;
7763 SmallVector<Expr *, 8> RHSs;
7764 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007765 for (auto RefExpr : VarList) {
7766 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
7767 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7768 // It will be analyzed later.
7769 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007770 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007771 LHSs.push_back(nullptr);
7772 RHSs.push_back(nullptr);
7773 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007774 continue;
7775 }
7776
7777 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7778 RefExpr->isInstantiationDependent() ||
7779 RefExpr->containsUnexpandedParameterPack()) {
7780 // It will be analyzed later.
7781 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007782 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007783 LHSs.push_back(nullptr);
7784 RHSs.push_back(nullptr);
7785 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007786 continue;
7787 }
7788
7789 auto ELoc = RefExpr->getExprLoc();
7790 auto ERange = RefExpr->getSourceRange();
7791 // OpenMP [2.1, C/C++]
7792 // A list item is a variable or array section, subject to the restrictions
7793 // specified in Section 2.4 on page 42 and in each of the sections
7794 // describing clauses and directives for which a list appears.
7795 // OpenMP [2.14.3.3, Restrictions, p.1]
7796 // A variable that is part of another variable (as an array or
7797 // structure element) cannot appear in a private clause.
Alexey Bataeva1764212015-09-30 09:22:36 +00007798 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
7799 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr);
7800 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr);
7801 if (!ASE && !OASE && (!DE || !isa<VarDecl>(DE->getDecl()))) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007802 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
7803 << 0 << ERange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007804 continue;
7805 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007806 QualType Type;
7807 VarDecl *VD = nullptr;
7808 if (DE) {
7809 auto D = DE->getDecl();
7810 VD = cast<VarDecl>(D);
Alexey Bataev31300ed2016-02-04 11:27:03 +00007811 Type = Context.getBaseElementType(VD->getType().getNonReferenceType());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007812 } else if (ASE) {
Alexey Bataev31300ed2016-02-04 11:27:03 +00007813 Type = ASE->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007814 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7815 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7816 Base = TempASE->getBase()->IgnoreParenImpCasts();
7817 DE = dyn_cast<DeclRefExpr>(Base);
7818 if (DE)
7819 VD = dyn_cast<VarDecl>(DE->getDecl());
7820 if (!VD) {
7821 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7822 << 0 << Base->getSourceRange();
7823 continue;
7824 }
7825 } else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007826 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
7827 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
7828 Type = ATy->getElementType();
7829 else
7830 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +00007831 Type = Type.getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007832 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7833 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7834 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7835 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7836 Base = TempASE->getBase()->IgnoreParenImpCasts();
7837 DE = dyn_cast<DeclRefExpr>(Base);
7838 if (DE)
7839 VD = dyn_cast<VarDecl>(DE->getDecl());
7840 if (!VD) {
7841 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7842 << 1 << Base->getSourceRange();
7843 continue;
7844 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007845 }
7846
Alexey Bataevc5e02582014-06-16 07:08:35 +00007847 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7848 // A variable that appears in a private clause must not have an incomplete
7849 // type or a reference type.
7850 if (RequireCompleteType(ELoc, Type,
7851 diag::err_omp_reduction_incomplete_type))
7852 continue;
7853 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00007854 // A list item that appears in a reduction clause must not be
7855 // const-qualified.
7856 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007857 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007858 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007859 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007860 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7861 VarDecl::DeclarationOnly;
7862 Diag(VD->getLocation(),
7863 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7864 << VD;
7865 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007866 continue;
7867 }
7868 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
7869 // If a list-item is a reference type then it must bind to the same object
7870 // for all threads of the team.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007871 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007872 VarDecl *VDDef = VD->getDefinition();
Alexey Bataev31300ed2016-02-04 11:27:03 +00007873 if (VD->getType()->isReferenceType() && VDDef) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007874 DSARefChecker Check(DSAStack);
7875 if (Check.Visit(VDDef->getInit())) {
7876 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
7877 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
7878 continue;
7879 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007880 }
7881 }
7882 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7883 // The type of a list item that appears in a reduction clause must be valid
7884 // for the reduction-identifier. For a max or min reduction in C, the type
7885 // of the list item must be an allowed arithmetic data type: char, int,
7886 // float, double, or _Bool, possibly modified with long, short, signed, or
7887 // unsigned. For a max or min reduction in C++, the type of the list item
7888 // must be an allowed arithmetic data type: char, wchar_t, int, float,
7889 // double, or bool, possibly modified with long, short, signed, or unsigned.
7890 if ((BOK == BO_GT || BOK == BO_LT) &&
7891 !(Type->isScalarType() ||
7892 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
7893 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
7894 << getLangOpts().CPlusPlus;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007895 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007896 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7897 VarDecl::DeclarationOnly;
7898 Diag(VD->getLocation(),
7899 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7900 << VD;
7901 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007902 continue;
7903 }
7904 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
7905 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
7906 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007907 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007908 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7909 VarDecl::DeclarationOnly;
7910 Diag(VD->getLocation(),
7911 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7912 << VD;
7913 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007914 continue;
7915 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007916 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7917 // in a Construct]
7918 // Variables with the predetermined data-sharing attributes may not be
7919 // listed in data-sharing attributes clauses, except for the cases
7920 // listed below. For these exceptions only, listing a predetermined
7921 // variable in a data-sharing attribute clause is allowed and overrides
7922 // the variable's predetermined data-sharing attributes.
7923 // OpenMP [2.14.3.6, Restrictions, p.3]
7924 // Any number of reduction clauses can be specified on the directive,
7925 // but a list item can appear only once in the reduction clauses for that
7926 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00007927 DSAStackTy::DSAVarData DVar;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007928 DVar = DSAStack->getTopDSA(VD, false);
7929 if (DVar.CKind == OMPC_reduction) {
7930 Diag(ELoc, diag::err_omp_once_referenced)
7931 << getOpenMPClauseName(OMPC_reduction);
7932 if (DVar.RefExpr) {
7933 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007934 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007935 } else if (DVar.CKind != OMPC_unknown) {
7936 Diag(ELoc, diag::err_omp_wrong_dsa)
7937 << getOpenMPClauseName(DVar.CKind)
7938 << getOpenMPClauseName(OMPC_reduction);
7939 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7940 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007941 }
7942
7943 // OpenMP [2.14.3.6, Restrictions, p.1]
7944 // A list item that appears in a reduction clause of a worksharing
7945 // construct must be shared in the parallel regions to which any of the
7946 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007947 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7948 if (isOpenMPWorksharingDirective(CurrDir) &&
7949 !isOpenMPParallelDirective(CurrDir)) {
7950 DVar = DSAStack->getImplicitDSA(VD, true);
7951 if (DVar.CKind != OMPC_shared) {
7952 Diag(ELoc, diag::err_omp_required_access)
7953 << getOpenMPClauseName(OMPC_reduction)
7954 << getOpenMPClauseName(OMPC_shared);
7955 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7956 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007957 }
7958 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007959
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007960 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007961 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
7962 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7963 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName(),
7964 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7965 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00007966 if (OASE ||
7967 (DE && VD->getType().getNonReferenceType()->isVariablyModifiedType())) {
7968 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007969 // Create pseudo array type for private copy. The size for this array will
7970 // be generated during codegen.
7971 // For array subscripts or single variables Private Ty is the same as Type
7972 // (type of the variable or single array element).
7973 PrivateTy = Context.getVariableArrayType(
7974 Type, new (Context) OpaqueValueExpr(SourceLocation(),
7975 Context.getSizeType(), VK_RValue),
7976 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev1189bd02016-01-26 12:20:39 +00007977 } else if (DE &&
7978 Context.getAsArrayType(VD->getType().getNonReferenceType()))
7979 PrivateTy = VD->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007980 // Private copy.
7981 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, VD->getName(),
7982 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007983 // Add initializer for private variable.
7984 Expr *Init = nullptr;
7985 switch (BOK) {
7986 case BO_Add:
7987 case BO_Xor:
7988 case BO_Or:
7989 case BO_LOr:
7990 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
7991 if (Type->isScalarType() || Type->isAnyComplexType()) {
7992 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007993 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007994 break;
7995 case BO_Mul:
7996 case BO_LAnd:
7997 if (Type->isScalarType() || Type->isAnyComplexType()) {
7998 // '*' and '&&' reduction ops - initializer is '1'.
7999 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
8000 }
8001 break;
8002 case BO_And: {
8003 // '&' reduction op - initializer is '~0'.
8004 QualType OrigType = Type;
8005 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
8006 Type = ComplexTy->getElementType();
8007 }
8008 if (Type->isRealFloatingType()) {
8009 llvm::APFloat InitValue =
8010 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
8011 /*isIEEE=*/true);
8012 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8013 Type, ELoc);
8014 } else if (Type->isScalarType()) {
8015 auto Size = Context.getTypeSize(Type);
8016 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
8017 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
8018 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8019 }
8020 if (Init && OrigType->isAnyComplexType()) {
8021 // Init = 0xFFFF + 0xFFFFi;
8022 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
8023 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
8024 }
8025 Type = OrigType;
8026 break;
8027 }
8028 case BO_LT:
8029 case BO_GT: {
8030 // 'min' reduction op - initializer is 'Largest representable number in
8031 // the reduction list item type'.
8032 // 'max' reduction op - initializer is 'Least representable number in
8033 // the reduction list item type'.
8034 if (Type->isIntegerType() || Type->isPointerType()) {
8035 bool IsSigned = Type->hasSignedIntegerRepresentation();
8036 auto Size = Context.getTypeSize(Type);
8037 QualType IntTy =
8038 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
8039 llvm::APInt InitValue =
8040 (BOK != BO_LT)
8041 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
8042 : llvm::APInt::getMinValue(Size)
8043 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
8044 : llvm::APInt::getMaxValue(Size);
8045 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8046 if (Type->isPointerType()) {
8047 // Cast to pointer type.
8048 auto CastExpr = BuildCStyleCastExpr(
8049 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
8050 SourceLocation(), Init);
8051 if (CastExpr.isInvalid())
8052 continue;
8053 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00008054 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008055 } else if (Type->isRealFloatingType()) {
8056 llvm::APFloat InitValue = llvm::APFloat::getLargest(
8057 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
8058 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8059 Type, ELoc);
8060 }
8061 break;
8062 }
8063 case BO_PtrMemD:
8064 case BO_PtrMemI:
8065 case BO_MulAssign:
8066 case BO_Div:
8067 case BO_Rem:
8068 case BO_Sub:
8069 case BO_Shl:
8070 case BO_Shr:
8071 case BO_LE:
8072 case BO_GE:
8073 case BO_EQ:
8074 case BO_NE:
8075 case BO_AndAssign:
8076 case BO_XorAssign:
8077 case BO_OrAssign:
8078 case BO_Assign:
8079 case BO_AddAssign:
8080 case BO_SubAssign:
8081 case BO_DivAssign:
8082 case BO_RemAssign:
8083 case BO_ShlAssign:
8084 case BO_ShrAssign:
8085 case BO_Comma:
8086 llvm_unreachable("Unexpected reduction operation");
8087 }
8088 if (Init) {
8089 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
8090 /*TypeMayContainAuto=*/false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008091 } else
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008092 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008093 if (!RHSVD->hasInit()) {
8094 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
8095 << ReductionIdRange;
Alexey Bataeva1764212015-09-30 09:22:36 +00008096 if (VD) {
8097 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
8098 VarDecl::DeclarationOnly;
8099 Diag(VD->getLocation(),
8100 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8101 << VD;
8102 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008103 continue;
8104 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008105 // Store initializer for single element in private copy. Will be used during
8106 // codegen.
8107 PrivateVD->setInit(RHSVD->getInit());
8108 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataev39f915b82015-05-08 10:41:21 +00008109 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
8110 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008111 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008112 ExprResult ReductionOp =
8113 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
8114 LHSDRE, RHSDRE);
8115 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00008116 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008117 ReductionOp =
8118 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8119 BO_Assign, LHSDRE, ReductionOp.get());
8120 } else {
8121 auto *ConditionalOp = new (Context) ConditionalOperator(
8122 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
8123 RHSDRE, Type, VK_LValue, OK_Ordinary);
8124 ReductionOp =
8125 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8126 BO_Assign, LHSDRE, ConditionalOp);
8127 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008128 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008129 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008130 if (ReductionOp.isInvalid())
8131 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008132
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008133 DSAStack->addDSA(VD, DE, OMPC_reduction);
Alexey Bataeva1764212015-09-30 09:22:36 +00008134 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008135 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008136 LHSs.push_back(LHSDRE);
8137 RHSs.push_back(RHSDRE);
8138 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008139 }
8140
8141 if (Vars.empty())
8142 return nullptr;
8143
8144 return OMPReductionClause::Create(
8145 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008146 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
8147 LHSs, RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008148}
8149
Alexey Bataev182227b2015-08-20 10:54:39 +00008150OMPClause *Sema::ActOnOpenMPLinearClause(
8151 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
8152 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
8153 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008154 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008155 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00008156 SmallVector<Expr *, 8> Inits;
Alexey Bataev182227b2015-08-20 10:54:39 +00008157 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
8158 LinKind == OMPC_LINEAR_unknown) {
8159 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
8160 LinKind = OMPC_LINEAR_val;
8161 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008162 for (auto &RefExpr : VarList) {
8163 assert(RefExpr && "NULL expr in OpenMP linear clause.");
8164 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008165 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008166 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008167 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00008168 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008169 continue;
8170 }
8171
8172 // OpenMP [2.14.3.7, linear clause]
8173 // A list item that appears in a linear clause is subject to the private
8174 // clause semantics described in Section 2.14.3.3 on page 159 except as
8175 // noted. In addition, the value of the new list item on each iteration
8176 // of the associated loop(s) corresponds to the value of the original
8177 // list item before entering the construct plus the logical number of
8178 // the iteration times linear-step.
8179
Alexey Bataeved09d242014-05-28 05:53:51 +00008180 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00008181 // OpenMP [2.1, C/C++]
8182 // A list item is a variable name.
8183 // OpenMP [2.14.3.3, Restrictions, p.1]
8184 // A variable that is part of another variable (as an array or
8185 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00008186 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008187 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008188 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8189 << 0 << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00008190 continue;
8191 }
8192
8193 VarDecl *VD = cast<VarDecl>(DE->getDecl());
8194
8195 // OpenMP [2.14.3.7, linear clause]
8196 // A list-item cannot appear in more than one linear clause.
8197 // A list-item that appears in a linear clause cannot appear in any
8198 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008199 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00008200 if (DVar.RefExpr) {
8201 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8202 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00008203 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00008204 continue;
8205 }
8206
8207 QualType QType = VD->getType();
8208 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
8209 // It will be analyzed later.
8210 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008211 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00008212 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008213 continue;
8214 }
8215
8216 // A variable must not have an incomplete type or a reference type.
8217 if (RequireCompleteType(ELoc, QType,
8218 diag::err_omp_linear_incomplete_type)) {
8219 continue;
8220 }
Alexey Bataev1185e192015-08-20 12:15:57 +00008221 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
8222 !QType->isReferenceType()) {
8223 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
8224 << QType << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
8225 continue;
8226 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008227 QType = QType.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00008228
8229 // A list item must not be const-qualified.
8230 if (QType.isConstant(Context)) {
8231 Diag(ELoc, diag::err_omp_const_variable)
8232 << getOpenMPClauseName(OMPC_linear);
8233 bool IsDecl =
8234 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8235 Diag(VD->getLocation(),
8236 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8237 << VD;
8238 continue;
8239 }
8240
8241 // A list item must be of integral or pointer type.
8242 QType = QType.getUnqualifiedType().getCanonicalType();
8243 const Type *Ty = QType.getTypePtrOrNull();
8244 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
8245 !Ty->isPointerType())) {
8246 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
8247 bool IsDecl =
8248 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8249 Diag(VD->getLocation(),
8250 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8251 << VD;
8252 continue;
8253 }
8254
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008255 // Build private copy of original var.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008256 auto *Private = buildVarDecl(*this, ELoc, QType, VD->getName(),
8257 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008258 auto *PrivateRef = buildDeclRefExpr(
8259 *this, Private, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman3276a272015-03-21 10:12:56 +00008260 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008261 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008262 Expr *InitExpr;
8263 if (LinKind == OMPC_LINEAR_uval)
8264 InitExpr = VD->getInit();
8265 else
8266 InitExpr = DE;
8267 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexander Musman3276a272015-03-21 10:12:56 +00008268 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008269 auto InitRef = buildDeclRefExpr(
8270 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00008271 DSAStack->addDSA(VD, DE, OMPC_linear);
8272 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008273 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00008274 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00008275 }
8276
8277 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008278 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008279
8280 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00008281 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008282 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
8283 !Step->isInstantiationDependent() &&
8284 !Step->containsUnexpandedParameterPack()) {
8285 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00008286 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00008287 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008288 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00008289 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00008290
Alexander Musman3276a272015-03-21 10:12:56 +00008291 // Build var to save the step value.
8292 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008293 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00008294 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008295 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00008296 ExprResult CalcStep =
8297 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008298 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00008299
Alexander Musman8dba6642014-04-22 13:09:42 +00008300 // Warn about zero linear step (it would be probably better specified as
8301 // making corresponding variables 'const').
8302 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00008303 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
8304 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00008305 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
8306 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00008307 if (!IsConstant && CalcStep.isUsable()) {
8308 // Calculate the step beforehand instead of doing this on each iteration.
8309 // (This is not used if the number of iterations may be kfold-ed).
8310 CalcStepExpr = CalcStep.get();
8311 }
Alexander Musman8dba6642014-04-22 13:09:42 +00008312 }
8313
Alexey Bataev182227b2015-08-20 10:54:39 +00008314 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
8315 ColonLoc, EndLoc, Vars, Privates, Inits,
8316 StepExpr, CalcStepExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00008317}
8318
8319static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
8320 Expr *NumIterations, Sema &SemaRef,
8321 Scope *S) {
8322 // Walk the vars and build update/final expressions for the CodeGen.
8323 SmallVector<Expr *, 8> Updates;
8324 SmallVector<Expr *, 8> Finals;
8325 Expr *Step = Clause.getStep();
8326 Expr *CalcStep = Clause.getCalcStep();
8327 // OpenMP [2.14.3.7, linear clause]
8328 // If linear-step is not specified it is assumed to be 1.
8329 if (Step == nullptr)
8330 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
8331 else if (CalcStep)
8332 Step = cast<BinaryOperator>(CalcStep)->getLHS();
8333 bool HasErrors = false;
8334 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008335 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008336 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00008337 for (auto &RefExpr : Clause.varlists()) {
8338 Expr *InitExpr = *CurInit;
8339
8340 // Build privatized reference to the current linear var.
8341 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008342 Expr *CapturedRef;
8343 if (LinKind == OMPC_LINEAR_uval)
8344 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
8345 else
8346 CapturedRef =
8347 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
8348 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
8349 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008350
8351 // Build update: Var = InitExpr + IV * Step
8352 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008353 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00008354 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008355 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
8356 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008357
8358 // Build final: Var = InitExpr + NumIterations * Step
8359 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008360 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00008361 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008362 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
8363 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008364 if (!Update.isUsable() || !Final.isUsable()) {
8365 Updates.push_back(nullptr);
8366 Finals.push_back(nullptr);
8367 HasErrors = true;
8368 } else {
8369 Updates.push_back(Update.get());
8370 Finals.push_back(Final.get());
8371 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008372 ++CurInit, ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00008373 }
8374 Clause.setUpdates(Updates);
8375 Clause.setFinals(Finals);
8376 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00008377}
8378
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008379OMPClause *Sema::ActOnOpenMPAlignedClause(
8380 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
8381 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
8382
8383 SmallVector<Expr *, 8> Vars;
8384 for (auto &RefExpr : VarList) {
8385 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
8386 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
8387 // It will be analyzed later.
8388 Vars.push_back(RefExpr);
8389 continue;
8390 }
8391
8392 SourceLocation ELoc = RefExpr->getExprLoc();
8393 // OpenMP [2.1, C/C++]
8394 // A list item is a variable name.
8395 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
8396 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008397 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8398 << 0 << RefExpr->getSourceRange();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008399 continue;
8400 }
8401
8402 VarDecl *VD = cast<VarDecl>(DE->getDecl());
8403
8404 // OpenMP [2.8.1, simd construct, Restrictions]
8405 // The type of list items appearing in the aligned clause must be
8406 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008407 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008408 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008409 const Type *Ty = QType.getTypePtrOrNull();
8410 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
8411 !Ty->isPointerType())) {
8412 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
8413 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
8414 bool IsDecl =
8415 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8416 Diag(VD->getLocation(),
8417 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8418 << VD;
8419 continue;
8420 }
8421
8422 // OpenMP [2.8.1, simd construct, Restrictions]
8423 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008424 if (Expr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008425 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
8426 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
8427 << getOpenMPClauseName(OMPC_aligned);
8428 continue;
8429 }
8430
8431 Vars.push_back(DE);
8432 }
8433
8434 // OpenMP [2.8.1, simd construct, Description]
8435 // The parameter of the aligned clause, alignment, must be a constant
8436 // positive integer expression.
8437 // If no optional parameter is specified, implementation-defined default
8438 // alignments for SIMD instructions on the target platforms are assumed.
8439 if (Alignment != nullptr) {
8440 ExprResult AlignResult =
8441 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
8442 if (AlignResult.isInvalid())
8443 return nullptr;
8444 Alignment = AlignResult.get();
8445 }
8446 if (Vars.empty())
8447 return nullptr;
8448
8449 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
8450 EndLoc, Vars, Alignment);
8451}
8452
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008453OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
8454 SourceLocation StartLoc,
8455 SourceLocation LParenLoc,
8456 SourceLocation EndLoc) {
8457 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008458 SmallVector<Expr *, 8> SrcExprs;
8459 SmallVector<Expr *, 8> DstExprs;
8460 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00008461 for (auto &RefExpr : VarList) {
8462 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
8463 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008464 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008465 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008466 SrcExprs.push_back(nullptr);
8467 DstExprs.push_back(nullptr);
8468 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008469 continue;
8470 }
8471
Alexey Bataeved09d242014-05-28 05:53:51 +00008472 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008473 // OpenMP [2.1, C/C++]
8474 // A list item is a variable name.
8475 // OpenMP [2.14.4.1, Restrictions, p.1]
8476 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00008477 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008478 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008479 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8480 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008481 continue;
8482 }
8483
8484 Decl *D = DE->getDecl();
8485 VarDecl *VD = cast<VarDecl>(D);
8486
8487 QualType Type = VD->getType();
8488 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8489 // It will be analyzed later.
8490 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008491 SrcExprs.push_back(nullptr);
8492 DstExprs.push_back(nullptr);
8493 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008494 continue;
8495 }
8496
8497 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
8498 // A list item that appears in a copyin clause must be threadprivate.
8499 if (!DSAStack->isThreadPrivate(VD)) {
8500 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00008501 << getOpenMPClauseName(OMPC_copyin)
8502 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008503 continue;
8504 }
8505
8506 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8507 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00008508 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008509 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008510 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008511 auto *SrcVD =
8512 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
8513 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008514 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008515 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
8516 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008517 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
8518 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008519 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008520 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008521 // For arrays generate assignment operation for single element and replace
8522 // it by the original array element in CodeGen.
8523 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8524 PseudoDstExpr, PseudoSrcExpr);
8525 if (AssignmentOp.isInvalid())
8526 continue;
8527 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8528 /*DiscardedValue=*/true);
8529 if (AssignmentOp.isInvalid())
8530 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008531
8532 DSAStack->addDSA(VD, DE, OMPC_copyin);
8533 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008534 SrcExprs.push_back(PseudoSrcExpr);
8535 DstExprs.push_back(PseudoDstExpr);
8536 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008537 }
8538
Alexey Bataeved09d242014-05-28 05:53:51 +00008539 if (Vars.empty())
8540 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008541
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008542 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8543 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008544}
8545
Alexey Bataevbae9a792014-06-27 10:37:06 +00008546OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
8547 SourceLocation StartLoc,
8548 SourceLocation LParenLoc,
8549 SourceLocation EndLoc) {
8550 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00008551 SmallVector<Expr *, 8> SrcExprs;
8552 SmallVector<Expr *, 8> DstExprs;
8553 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008554 for (auto &RefExpr : VarList) {
8555 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
8556 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
8557 // It will be analyzed later.
8558 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008559 SrcExprs.push_back(nullptr);
8560 DstExprs.push_back(nullptr);
8561 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008562 continue;
8563 }
8564
8565 SourceLocation ELoc = RefExpr->getExprLoc();
8566 // OpenMP [2.1, C/C++]
8567 // A list item is a variable name.
8568 // OpenMP [2.14.4.1, Restrictions, p.1]
8569 // A list item that appears in a copyin clause must be threadprivate.
8570 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
8571 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008572 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8573 << 0 << RefExpr->getSourceRange();
Alexey Bataevbae9a792014-06-27 10:37:06 +00008574 continue;
8575 }
8576
8577 Decl *D = DE->getDecl();
8578 VarDecl *VD = cast<VarDecl>(D);
8579
8580 QualType Type = VD->getType();
8581 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8582 // It will be analyzed later.
8583 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008584 SrcExprs.push_back(nullptr);
8585 DstExprs.push_back(nullptr);
8586 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008587 continue;
8588 }
8589
8590 // OpenMP [2.14.4.2, Restrictions, p.2]
8591 // A list item that appears in a copyprivate clause may not appear in a
8592 // private or firstprivate clause on the single construct.
8593 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008594 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008595 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
8596 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00008597 Diag(ELoc, diag::err_omp_wrong_dsa)
8598 << getOpenMPClauseName(DVar.CKind)
8599 << getOpenMPClauseName(OMPC_copyprivate);
8600 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8601 continue;
8602 }
8603
8604 // OpenMP [2.11.4.2, Restrictions, p.1]
8605 // All list items that appear in a copyprivate clause must be either
8606 // threadprivate or private in the enclosing context.
8607 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008608 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008609 if (DVar.CKind == OMPC_shared) {
8610 Diag(ELoc, diag::err_omp_required_access)
8611 << getOpenMPClauseName(OMPC_copyprivate)
8612 << "threadprivate or private in the enclosing context";
8613 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8614 continue;
8615 }
8616 }
8617 }
8618
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008619 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008620 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008621 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008622 << getOpenMPClauseName(OMPC_copyprivate) << Type
8623 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00008624 bool IsDecl =
8625 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8626 Diag(VD->getLocation(),
8627 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8628 << VD;
8629 continue;
8630 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008631
Alexey Bataevbae9a792014-06-27 10:37:06 +00008632 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8633 // A variable of class type (or array thereof) that appears in a
8634 // copyin clause requires an accessible, unambiguous copy assignment
8635 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008636 Type = Context.getBaseElementType(Type.getNonReferenceType())
8637 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00008638 auto *SrcVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008639 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src",
8640 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008641 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008642 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00008643 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008644 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst",
8645 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00008646 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008647 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00008648 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8649 PseudoDstExpr, PseudoSrcExpr);
8650 if (AssignmentOp.isInvalid())
8651 continue;
8652 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8653 /*DiscardedValue=*/true);
8654 if (AssignmentOp.isInvalid())
8655 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008656
8657 // No need to mark vars as copyprivate, they are already threadprivate or
8658 // implicitly private.
8659 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008660 SrcExprs.push_back(PseudoSrcExpr);
8661 DstExprs.push_back(PseudoDstExpr);
8662 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00008663 }
8664
8665 if (Vars.empty())
8666 return nullptr;
8667
Alexey Bataeva63048e2015-03-23 06:18:07 +00008668 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
8669 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008670}
8671
Alexey Bataev6125da92014-07-21 11:26:11 +00008672OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
8673 SourceLocation StartLoc,
8674 SourceLocation LParenLoc,
8675 SourceLocation EndLoc) {
8676 if (VarList.empty())
8677 return nullptr;
8678
8679 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
8680}
Alexey Bataevdea47612014-07-23 07:46:59 +00008681
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008682OMPClause *
8683Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
8684 SourceLocation DepLoc, SourceLocation ColonLoc,
8685 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
8686 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008687 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008688 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +00008689 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008690 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +00008691 return nullptr;
8692 }
8693 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008694 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
8695 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00008696 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008697 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008698 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
8699 /*Last=*/OMPC_DEPEND_unknown, Except)
8700 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008701 return nullptr;
8702 }
8703 SmallVector<Expr *, 8> Vars;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008704 llvm::APSInt DepCounter(/*BitWidth=*/32);
8705 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
8706 if (DepKind == OMPC_DEPEND_sink) {
8707 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
8708 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
8709 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008710 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008711 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008712 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
8713 DSAStack->getParentOrderedRegionParam()) {
8714 for (auto &RefExpr : VarList) {
8715 assert(RefExpr && "NULL expr in OpenMP shared clause.");
8716 if (isa<DependentScopeDeclRefExpr>(RefExpr) ||
8717 (DepKind == OMPC_DEPEND_sink && CurContext->isDependentContext())) {
8718 // It will be analyzed later.
8719 Vars.push_back(RefExpr);
8720 continue;
8721 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008722
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008723 SourceLocation ELoc = RefExpr->getExprLoc();
8724 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
8725 if (DepKind == OMPC_DEPEND_sink) {
8726 if (DepCounter >= TotalDepCount) {
8727 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
8728 continue;
8729 }
8730 ++DepCounter;
8731 // OpenMP [2.13.9, Summary]
8732 // depend(dependence-type : vec), where dependence-type is:
8733 // 'sink' and where vec is the iteration vector, which has the form:
8734 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
8735 // where n is the value specified by the ordered clause in the loop
8736 // directive, xi denotes the loop iteration variable of the i-th nested
8737 // loop associated with the loop directive, and di is a constant
8738 // non-negative integer.
8739 SimpleExpr = SimpleExpr->IgnoreImplicit();
8740 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8741 if (!DE) {
8742 OverloadedOperatorKind OOK = OO_None;
8743 SourceLocation OOLoc;
8744 Expr *LHS, *RHS;
8745 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
8746 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
8747 OOLoc = BO->getOperatorLoc();
8748 LHS = BO->getLHS()->IgnoreParenImpCasts();
8749 RHS = BO->getRHS()->IgnoreParenImpCasts();
8750 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
8751 OOK = OCE->getOperator();
8752 OOLoc = OCE->getOperatorLoc();
8753 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8754 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
8755 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
8756 OOK = MCE->getMethodDecl()
8757 ->getNameInfo()
8758 .getName()
8759 .getCXXOverloadedOperator();
8760 OOLoc = MCE->getCallee()->getExprLoc();
8761 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
8762 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
8763 } else {
8764 Diag(ELoc, diag::err_omp_depend_sink_wrong_expr);
8765 continue;
8766 }
8767 DE = dyn_cast<DeclRefExpr>(LHS);
8768 if (!DE) {
8769 Diag(LHS->getExprLoc(),
8770 diag::err_omp_depend_sink_expected_loop_iteration)
8771 << DSAStack->getParentLoopControlVariable(
8772 DepCounter.getZExtValue());
8773 continue;
8774 }
8775 if (OOK != OO_Plus && OOK != OO_Minus) {
8776 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
8777 continue;
8778 }
8779 ExprResult Res = VerifyPositiveIntegerConstantInClause(
8780 RHS, OMPC_depend, /*StrictlyPositive=*/false);
8781 if (Res.isInvalid())
8782 continue;
8783 }
8784 auto *VD = dyn_cast<VarDecl>(DE->getDecl());
8785 if (!CurContext->isDependentContext() &&
8786 DSAStack->getParentOrderedRegionParam() &&
8787 (!VD || DepCounter != DSAStack->isParentLoopControlVariable(VD))) {
8788 Diag(DE->getExprLoc(),
8789 diag::err_omp_depend_sink_expected_loop_iteration)
8790 << DSAStack->getParentLoopControlVariable(
8791 DepCounter.getZExtValue());
8792 continue;
8793 }
8794 } else {
8795 // OpenMP [2.11.1.1, Restrictions, p.3]
8796 // A variable that is part of another variable (such as a field of a
8797 // structure) but is not an array element or an array section cannot
8798 // appear in a depend clause.
8799 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8800 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
8801 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
8802 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
8803 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev31300ed2016-02-04 11:27:03 +00008804 (ASE &&
8805 !ASE->getBase()
8806 ->getType()
8807 .getNonReferenceType()
8808 ->isPointerType() &&
8809 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008810 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
8811 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008812 continue;
8813 }
8814 }
8815
8816 Vars.push_back(RefExpr->IgnoreParenImpCasts());
8817 }
8818
8819 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
8820 TotalDepCount > VarList.size() &&
8821 DSAStack->getParentOrderedRegionParam()) {
8822 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
8823 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
8824 }
8825 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
8826 Vars.empty())
8827 return nullptr;
8828 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008829
8830 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
8831 DepLoc, ColonLoc, Vars);
8832}
Michael Wonge710d542015-08-07 16:16:36 +00008833
8834OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
8835 SourceLocation LParenLoc,
8836 SourceLocation EndLoc) {
8837 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00008838
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008839 // OpenMP [2.9.1, Restrictions]
8840 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008841 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
8842 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008843 return nullptr;
8844
Michael Wonge710d542015-08-07 16:16:36 +00008845 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8846}
Kelvin Li0bff7af2015-11-23 05:32:03 +00008847
8848static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
8849 DSAStackTy *Stack, CXXRecordDecl *RD) {
8850 if (!RD || RD->isInvalidDecl())
8851 return true;
8852
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00008853 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
8854 if (auto *CTD = CTSD->getSpecializedTemplate())
8855 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00008856 auto QTy = SemaRef.Context.getRecordType(RD);
8857 if (RD->isDynamicClass()) {
8858 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8859 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
8860 return false;
8861 }
8862 auto *DC = RD;
8863 bool IsCorrect = true;
8864 for (auto *I : DC->decls()) {
8865 if (I) {
8866 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
8867 if (MD->isStatic()) {
8868 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8869 SemaRef.Diag(MD->getLocation(),
8870 diag::note_omp_static_member_in_target);
8871 IsCorrect = false;
8872 }
8873 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
8874 if (VD->isStaticDataMember()) {
8875 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8876 SemaRef.Diag(VD->getLocation(),
8877 diag::note_omp_static_member_in_target);
8878 IsCorrect = false;
8879 }
8880 }
8881 }
8882 }
8883
8884 for (auto &I : RD->bases()) {
8885 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
8886 I.getType()->getAsCXXRecordDecl()))
8887 IsCorrect = false;
8888 }
8889 return IsCorrect;
8890}
8891
8892static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
8893 DSAStackTy *Stack, QualType QTy) {
8894 NamedDecl *ND;
8895 if (QTy->isIncompleteType(&ND)) {
8896 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
8897 return false;
8898 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
8899 if (!RD->isInvalidDecl() &&
8900 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
8901 return false;
8902 }
8903 return true;
8904}
8905
Samuel Antao5de996e2016-01-22 20:21:36 +00008906// Return the expression of the base of the map clause or null if it cannot
8907// be determined and do all the necessary checks to see if the expression is
8908// valid as a standalone map clause expression.
8909static Expr *CheckMapClauseExpressionBase(Sema &SemaRef, Expr *E) {
8910 SourceLocation ELoc = E->getExprLoc();
8911 SourceRange ERange = E->getSourceRange();
8912
8913 // The base of elements of list in a map clause have to be either:
8914 // - a reference to variable or field.
8915 // - a member expression.
8916 // - an array expression.
8917 //
8918 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
8919 // reference to 'r'.
8920 //
8921 // If we have:
8922 //
8923 // struct SS {
8924 // Bla S;
8925 // foo() {
8926 // #pragma omp target map (S.Arr[:12]);
8927 // }
8928 // }
8929 //
8930 // We want to retrieve the member expression 'this->S';
8931
8932 Expr *RelevantExpr = nullptr;
8933
8934 // Flags to help capture some memory
8935
8936 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
8937 // If a list item is an array section, it must specify contiguous storage.
8938 //
8939 // For this restriction it is sufficient that we make sure only references
8940 // to variables or fields and array expressions, and that no array sections
8941 // exist except in the rightmost expression. E.g. these would be invalid:
8942 //
8943 // r.ArrS[3:5].Arr[6:7]
8944 //
8945 // r.ArrS[3:5].x
8946 //
8947 // but these would be valid:
8948 // r.ArrS[3].Arr[6:7]
8949 //
8950 // r.ArrS[3].x
8951
8952 bool IsRightMostExpression = true;
8953
8954 while (!RelevantExpr) {
8955 auto AllowArraySection = IsRightMostExpression;
8956 IsRightMostExpression = false;
8957
8958 E = E->IgnoreParenImpCasts();
8959
8960 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
8961 if (!isa<VarDecl>(CurE->getDecl()))
8962 break;
8963
8964 RelevantExpr = CurE;
8965 continue;
8966 }
8967
8968 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
8969 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
8970
8971 if (isa<CXXThisExpr>(BaseE))
8972 // We found a base expression: this->Val.
8973 RelevantExpr = CurE;
8974 else
8975 E = BaseE;
8976
8977 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
8978 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
8979 << CurE->getSourceRange();
8980 break;
8981 }
8982
8983 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
8984
8985 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
8986 // A bit-field cannot appear in a map clause.
8987 //
8988 if (FD->isBitField()) {
8989 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_map_clause)
8990 << CurE->getSourceRange();
8991 break;
8992 }
8993
8994 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
8995 // If the type of a list item is a reference to a type T then the type
8996 // will be considered to be T for all purposes of this clause.
8997 QualType CurType = BaseE->getType();
8998 if (CurType->isReferenceType())
8999 CurType = CurType->getPointeeType();
9000
9001 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
9002 // A list item cannot be a variable that is a member of a structure with
9003 // a union type.
9004 //
9005 if (auto *RT = CurType->getAs<RecordType>())
9006 if (RT->isUnionType()) {
9007 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
9008 << CurE->getSourceRange();
9009 break;
9010 }
9011
9012 continue;
9013 }
9014
9015 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
9016 E = CurE->getBase()->IgnoreParenImpCasts();
9017
9018 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
9019 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9020 << 0 << CurE->getSourceRange();
9021 break;
9022 }
9023 continue;
9024 }
9025
9026 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
9027 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
9028 // If a list item is an element of a structure, only the rightmost symbol
9029 // of the variable reference can be an array section.
9030 //
9031 if (!AllowArraySection) {
9032 SemaRef.Diag(ELoc, diag::err_omp_array_section_in_rightmost_expression)
9033 << CurE->getSourceRange();
9034 break;
9035 }
9036
9037 E = CurE->getBase()->IgnoreParenImpCasts();
9038
9039 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9040 // If the type of a list item is a reference to a type T then the type
9041 // will be considered to be T for all purposes of this clause.
9042 QualType CurType = E->getType();
9043 if (CurType->isReferenceType())
9044 CurType = CurType->getPointeeType();
9045
9046 if (!CurType->isAnyPointerType() && !CurType->isArrayType()) {
9047 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9048 << 0 << CurE->getSourceRange();
9049 break;
9050 }
9051
9052 continue;
9053 }
9054
9055 // If nothing else worked, this is not a valid map clause expression.
9056 SemaRef.Diag(ELoc,
9057 diag::err_omp_expected_named_var_member_or_array_expression)
9058 << ERange;
9059 break;
9060 }
9061
9062 return RelevantExpr;
9063}
9064
9065// Return true if expression E associated with value VD has conflicts with other
9066// map information.
9067static bool CheckMapConflicts(Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD,
9068 Expr *E, bool CurrentRegionOnly) {
9069 assert(VD && E);
9070
9071 // Types used to organize the components of a valid map clause.
9072 typedef std::pair<Expr *, ValueDecl *> MapExpressionComponent;
9073 typedef SmallVector<MapExpressionComponent, 4> MapExpressionComponents;
9074
9075 // Helper to extract the components in the map clause expression E and store
9076 // them into MEC. This assumes that E is a valid map clause expression, i.e.
9077 // it has already passed the single clause checks.
9078 auto ExtractMapExpressionComponents = [](Expr *TE,
9079 MapExpressionComponents &MEC) {
9080 while (true) {
9081 TE = TE->IgnoreParenImpCasts();
9082
9083 if (auto *CurE = dyn_cast<DeclRefExpr>(TE)) {
9084 MEC.push_back(
9085 MapExpressionComponent(CurE, cast<VarDecl>(CurE->getDecl())));
9086 break;
9087 }
9088
9089 if (auto *CurE = dyn_cast<MemberExpr>(TE)) {
9090 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
9091
9092 MEC.push_back(MapExpressionComponent(
9093 CurE, cast<FieldDecl>(CurE->getMemberDecl())));
9094 if (isa<CXXThisExpr>(BaseE))
9095 break;
9096
9097 TE = BaseE;
9098 continue;
9099 }
9100
9101 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(TE)) {
9102 MEC.push_back(MapExpressionComponent(CurE, nullptr));
9103 TE = CurE->getBase()->IgnoreParenImpCasts();
9104 continue;
9105 }
9106
9107 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(TE)) {
9108 MEC.push_back(MapExpressionComponent(CurE, nullptr));
9109 TE = CurE->getBase()->IgnoreParenImpCasts();
9110 continue;
9111 }
9112
9113 llvm_unreachable(
9114 "Expecting only valid map clause expressions at this point!");
9115 }
9116 };
9117
9118 SourceLocation ELoc = E->getExprLoc();
9119 SourceRange ERange = E->getSourceRange();
9120
9121 // In order to easily check the conflicts we need to match each component of
9122 // the expression under test with the components of the expressions that are
9123 // already in the stack.
9124
9125 MapExpressionComponents CurComponents;
9126 ExtractMapExpressionComponents(E, CurComponents);
9127
9128 assert(!CurComponents.empty() && "Map clause expression with no components!");
9129 assert(CurComponents.back().second == VD &&
9130 "Map clause expression with unexpected base!");
9131
9132 // Variables to help detecting enclosing problems in data environment nests.
9133 bool IsEnclosedByDataEnvironmentExpr = false;
9134 Expr *EnclosingExpr = nullptr;
9135
9136 bool FoundError =
9137 DSAS->checkMapInfoForVar(VD, CurrentRegionOnly, [&](Expr *RE) -> bool {
9138 MapExpressionComponents StackComponents;
9139 ExtractMapExpressionComponents(RE, StackComponents);
9140 assert(!StackComponents.empty() &&
9141 "Map clause expression with no components!");
9142 assert(StackComponents.back().second == VD &&
9143 "Map clause expression with unexpected base!");
9144
9145 // Expressions must start from the same base. Here we detect at which
9146 // point both expressions diverge from each other and see if we can
9147 // detect if the memory referred to both expressions is contiguous and
9148 // do not overlap.
9149 auto CI = CurComponents.rbegin();
9150 auto CE = CurComponents.rend();
9151 auto SI = StackComponents.rbegin();
9152 auto SE = StackComponents.rend();
9153 for (; CI != CE && SI != SE; ++CI, ++SI) {
9154
9155 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
9156 // At most one list item can be an array item derived from a given
9157 // variable in map clauses of the same construct.
9158 if (CurrentRegionOnly && (isa<ArraySubscriptExpr>(CI->first) ||
9159 isa<OMPArraySectionExpr>(CI->first)) &&
9160 (isa<ArraySubscriptExpr>(SI->first) ||
9161 isa<OMPArraySectionExpr>(SI->first))) {
9162 SemaRef.Diag(CI->first->getExprLoc(),
9163 diag::err_omp_multiple_array_items_in_map_clause)
9164 << CI->first->getSourceRange();
9165 ;
9166 SemaRef.Diag(SI->first->getExprLoc(), diag::note_used_here)
9167 << SI->first->getSourceRange();
9168 return true;
9169 }
9170
9171 // Do both expressions have the same kind?
9172 if (CI->first->getStmtClass() != SI->first->getStmtClass())
9173 break;
9174
9175 // Are we dealing with different variables/fields?
9176 if (CI->second != SI->second)
9177 break;
9178 }
9179
9180 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
9181 // List items of map clauses in the same construct must not share
9182 // original storage.
9183 //
9184 // If the expressions are exactly the same or one is a subset of the
9185 // other, it means they are sharing storage.
9186 if (CI == CE && SI == SE) {
9187 if (CurrentRegionOnly) {
9188 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
9189 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9190 << RE->getSourceRange();
9191 return true;
9192 } else {
9193 // If we find the same expression in the enclosing data environment,
9194 // that is legal.
9195 IsEnclosedByDataEnvironmentExpr = true;
9196 return false;
9197 }
9198 }
9199
9200 QualType DerivedType = std::prev(CI)->first->getType();
9201 SourceLocation DerivedLoc = std::prev(CI)->first->getExprLoc();
9202
9203 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9204 // If the type of a list item is a reference to a type T then the type
9205 // will be considered to be T for all purposes of this clause.
9206 if (DerivedType->isReferenceType())
9207 DerivedType = DerivedType->getPointeeType();
9208
9209 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
9210 // A variable for which the type is pointer and an array section
9211 // derived from that variable must not appear as list items of map
9212 // clauses of the same construct.
9213 //
9214 // Also, cover one of the cases in:
9215 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
9216 // If any part of the original storage of a list item has corresponding
9217 // storage in the device data environment, all of the original storage
9218 // must have corresponding storage in the device data environment.
9219 //
9220 if (DerivedType->isAnyPointerType()) {
9221 if (CI == CE || SI == SE) {
9222 SemaRef.Diag(
9223 DerivedLoc,
9224 diag::err_omp_pointer_mapped_along_with_derived_section)
9225 << DerivedLoc;
9226 } else {
9227 assert(CI != CE && SI != SE);
9228 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
9229 << DerivedLoc;
9230 }
9231 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9232 << RE->getSourceRange();
9233 return true;
9234 }
9235
9236 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
9237 // List items of map clauses in the same construct must not share
9238 // original storage.
9239 //
9240 // An expression is a subset of the other.
9241 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
9242 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
9243 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9244 << RE->getSourceRange();
9245 return true;
9246 }
9247
9248 // The current expression uses the same base as other expression in the
9249 // data environment but does not contain it completelly.
9250 if (!CurrentRegionOnly && SI != SE)
9251 EnclosingExpr = RE;
9252
9253 // The current expression is a subset of the expression in the data
9254 // environment.
9255 IsEnclosedByDataEnvironmentExpr |=
9256 (!CurrentRegionOnly && CI != CE && SI == SE);
9257
9258 return false;
9259 });
9260
9261 if (CurrentRegionOnly)
9262 return FoundError;
9263
9264 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
9265 // If any part of the original storage of a list item has corresponding
9266 // storage in the device data environment, all of the original storage must
9267 // have corresponding storage in the device data environment.
9268 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
9269 // If a list item is an element of a structure, and a different element of
9270 // the structure has a corresponding list item in the device data environment
9271 // prior to a task encountering the construct associated with the map clause,
9272 // then the list item must also have a correspnding list item in the device
9273 // data environment prior to the task encountering the construct.
9274 //
9275 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
9276 SemaRef.Diag(ELoc,
9277 diag::err_omp_original_storage_is_shared_and_does_not_contain)
9278 << ERange;
9279 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
9280 << EnclosingExpr->getSourceRange();
9281 return true;
9282 }
9283
9284 return FoundError;
9285}
9286
Samuel Antao23abd722016-01-19 20:40:49 +00009287OMPClause *
9288Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
9289 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
9290 SourceLocation MapLoc, SourceLocation ColonLoc,
9291 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
9292 SourceLocation LParenLoc, SourceLocation EndLoc) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009293 SmallVector<Expr *, 4> Vars;
9294
9295 for (auto &RE : VarList) {
9296 assert(RE && "Null expr in omp map");
9297 if (isa<DependentScopeDeclRefExpr>(RE)) {
9298 // It will be analyzed later.
9299 Vars.push_back(RE);
9300 continue;
9301 }
9302 SourceLocation ELoc = RE->getExprLoc();
9303
Kelvin Li0bff7af2015-11-23 05:32:03 +00009304 auto *VE = RE->IgnoreParenLValueCasts();
9305
9306 if (VE->isValueDependent() || VE->isTypeDependent() ||
9307 VE->isInstantiationDependent() ||
9308 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009309 // We can only analyze this information once the missing information is
9310 // resolved.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009311 Vars.push_back(RE);
9312 continue;
9313 }
9314
9315 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009316
Samuel Antao5de996e2016-01-22 20:21:36 +00009317 if (!RE->IgnoreParenImpCasts()->isLValue()) {
9318 Diag(ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
9319 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009320 continue;
9321 }
9322
Samuel Antao5de996e2016-01-22 20:21:36 +00009323 // Obtain the array or member expression bases if required.
9324 auto *BE = CheckMapClauseExpressionBase(*this, SimpleExpr);
9325 if (!BE)
9326 continue;
9327
9328 // If the base is a reference to a variable, we rely on that variable for
9329 // the following checks. If it is a 'this' expression we rely on the field.
9330 ValueDecl *D = nullptr;
9331 if (auto *DRE = dyn_cast<DeclRefExpr>(BE)) {
9332 D = DRE->getDecl();
9333 } else {
9334 auto *ME = cast<MemberExpr>(BE);
9335 assert(isa<CXXThisExpr>(ME->getBase()) && "Unexpected expression!");
9336 D = ME->getMemberDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009337 }
9338 assert(D && "Null decl on map clause.");
Kelvin Li0bff7af2015-11-23 05:32:03 +00009339
Samuel Antao5de996e2016-01-22 20:21:36 +00009340 auto *VD = dyn_cast<VarDecl>(D);
9341 auto *FD = dyn_cast<FieldDecl>(D);
9342
9343 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +00009344 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +00009345
9346 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
9347 // threadprivate variables cannot appear in a map clause.
9348 if (VD && DSAStack->isThreadPrivate(VD)) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009349 auto DVar = DSAStack->getTopDSA(VD, false);
9350 Diag(ELoc, diag::err_omp_threadprivate_in_map);
9351 ReportOriginalDSA(*this, DSAStack, VD, DVar);
9352 continue;
9353 }
9354
Samuel Antao5de996e2016-01-22 20:21:36 +00009355 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
9356 // A list item cannot appear in both a map clause and a data-sharing
9357 // attribute clause on the same construct.
9358 //
9359 // TODO: Implement this check - it cannot currently be tested because of
9360 // missing implementation of the other data sharing clauses in target
9361 // directives.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009362
Samuel Antao5de996e2016-01-22 20:21:36 +00009363 // Check conflicts with other map clause expressions. We check the conflicts
9364 // with the current construct separately from the enclosing data
9365 // environment, because the restrictions are different.
9366 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9367 /*CurrentRegionOnly=*/true))
9368 break;
9369 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9370 /*CurrentRegionOnly=*/false))
9371 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +00009372
Samuel Antao5de996e2016-01-22 20:21:36 +00009373 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9374 // If the type of a list item is a reference to a type T then the type will
9375 // be considered to be T for all purposes of this clause.
9376 QualType Type = D->getType();
9377 if (Type->isReferenceType())
9378 Type = Type->getPointeeType();
9379
9380 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +00009381 // A list item must have a mappable type.
9382 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
9383 DSAStack, Type))
9384 continue;
9385
Samuel Antaodf67fc42016-01-19 19:15:56 +00009386 // target enter data
9387 // OpenMP [2.10.2, Restrictions, p. 99]
9388 // A map-type must be specified in all map clauses and must be either
9389 // to or alloc.
9390 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
9391 if (DKind == OMPD_target_enter_data &&
9392 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
9393 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00009394 << (IsMapTypeImplicit ? 1 : 0)
9395 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antaodf67fc42016-01-19 19:15:56 +00009396 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009397 continue;
Samuel Antaodf67fc42016-01-19 19:15:56 +00009398 }
9399
Samuel Antao72590762016-01-19 20:04:50 +00009400 // target exit_data
9401 // OpenMP [2.10.3, Restrictions, p. 102]
9402 // A map-type must be specified in all map clauses and must be either
9403 // from, release, or delete.
9404 DKind = DSAStack->getCurrentDirective();
9405 if (DKind == OMPD_target_exit_data &&
9406 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
9407 MapType == OMPC_MAP_delete)) {
9408 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00009409 << (IsMapTypeImplicit ? 1 : 0)
9410 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antao72590762016-01-19 20:04:50 +00009411 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009412 continue;
Samuel Antao72590762016-01-19 20:04:50 +00009413 }
9414
Kelvin Li0bff7af2015-11-23 05:32:03 +00009415 Vars.push_back(RE);
Samuel Antao5de996e2016-01-22 20:21:36 +00009416 DSAStack->addExprToVarMapInfo(D, RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +00009417 }
Kelvin Li0bff7af2015-11-23 05:32:03 +00009418
Samuel Antao5de996e2016-01-22 20:21:36 +00009419 // We need to produce a map clause even if we don't have variables so that
9420 // other diagnostics related with non-existing map clauses are accurate.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009421 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
Samuel Antao23abd722016-01-19 20:40:49 +00009422 MapTypeModifier, MapType, IsMapTypeImplicit,
9423 MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +00009424}
Kelvin Li099bb8c2015-11-24 20:50:12 +00009425
9426OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
9427 SourceLocation StartLoc,
9428 SourceLocation LParenLoc,
9429 SourceLocation EndLoc) {
9430 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +00009431
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009432 // OpenMP [teams Constrcut, Restrictions]
9433 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009434 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
9435 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009436 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +00009437
9438 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9439}
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009440
9441OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
9442 SourceLocation StartLoc,
9443 SourceLocation LParenLoc,
9444 SourceLocation EndLoc) {
9445 Expr *ValExpr = ThreadLimit;
9446
9447 // OpenMP [teams Constrcut, Restrictions]
9448 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009449 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
9450 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009451 return nullptr;
9452
9453 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
9454 EndLoc);
9455}
Alexey Bataeva0569352015-12-01 10:17:31 +00009456
9457OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
9458 SourceLocation StartLoc,
9459 SourceLocation LParenLoc,
9460 SourceLocation EndLoc) {
9461 Expr *ValExpr = Priority;
9462
9463 // OpenMP [2.9.1, task Constrcut]
9464 // The priority-value is a non-negative numerical scalar expression.
9465 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
9466 /*StrictlyPositive=*/false))
9467 return nullptr;
9468
9469 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9470}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009471
9472OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
9473 SourceLocation StartLoc,
9474 SourceLocation LParenLoc,
9475 SourceLocation EndLoc) {
9476 Expr *ValExpr = Grainsize;
9477
9478 // OpenMP [2.9.2, taskloop Constrcut]
9479 // The parameter of the grainsize clause must be a positive integer
9480 // expression.
9481 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
9482 /*StrictlyPositive=*/true))
9483 return nullptr;
9484
9485 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9486}
Alexey Bataev382967a2015-12-08 12:06:20 +00009487
9488OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
9489 SourceLocation StartLoc,
9490 SourceLocation LParenLoc,
9491 SourceLocation EndLoc) {
9492 Expr *ValExpr = NumTasks;
9493
9494 // OpenMP [2.9.2, taskloop Constrcut]
9495 // The parameter of the num_tasks clause must be a positive integer
9496 // expression.
9497 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
9498 /*StrictlyPositive=*/true))
9499 return nullptr;
9500
9501 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9502}
9503
Alexey Bataev28c75412015-12-15 08:19:24 +00009504OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
9505 SourceLocation LParenLoc,
9506 SourceLocation EndLoc) {
9507 // OpenMP [2.13.2, critical construct, Description]
9508 // ... where hint-expression is an integer constant expression that evaluates
9509 // to a valid lock hint.
9510 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
9511 if (HintExpr.isInvalid())
9512 return nullptr;
9513 return new (Context)
9514 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
9515}
9516
Carlo Bertollib4adf552016-01-15 18:50:31 +00009517OMPClause *Sema::ActOnOpenMPDistScheduleClause(
9518 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
9519 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
9520 SourceLocation EndLoc) {
9521 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
9522 std::string Values;
9523 Values += "'";
9524 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
9525 Values += "'";
9526 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
9527 << Values << getOpenMPClauseName(OMPC_dist_schedule);
9528 return nullptr;
9529 }
9530 Expr *ValExpr = ChunkSize;
9531 Expr *HelperValExpr = nullptr;
9532 if (ChunkSize) {
9533 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
9534 !ChunkSize->isInstantiationDependent() &&
9535 !ChunkSize->containsUnexpandedParameterPack()) {
9536 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
9537 ExprResult Val =
9538 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
9539 if (Val.isInvalid())
9540 return nullptr;
9541
9542 ValExpr = Val.get();
9543
9544 // OpenMP [2.7.1, Restrictions]
9545 // chunk_size must be a loop invariant integer expression with a positive
9546 // value.
9547 llvm::APSInt Result;
9548 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
9549 if (Result.isSigned() && !Result.isStrictlyPositive()) {
9550 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
9551 << "dist_schedule" << ChunkSize->getSourceRange();
9552 return nullptr;
9553 }
9554 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
9555 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
9556 ChunkSize->getType(), ".chunk.");
9557 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
9558 ChunkSize->getExprLoc(),
9559 /*RefersToCapture=*/true);
9560 HelperValExpr = ImpVarRef;
9561 }
9562 }
9563 }
9564
9565 return new (Context)
9566 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
9567 Kind, ValExpr, HelperValExpr);
9568}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009569
9570OMPClause *Sema::ActOnOpenMPDefaultmapClause(
9571 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
9572 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
9573 SourceLocation KindLoc, SourceLocation EndLoc) {
9574 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
9575 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
9576 Kind != OMPC_DEFAULTMAP_scalar) {
9577 std::string Value;
9578 SourceLocation Loc;
9579 Value += "'";
9580 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
9581 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
9582 OMPC_DEFAULTMAP_MODIFIER_tofrom);
9583 Loc = MLoc;
9584 } else {
9585 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
9586 OMPC_DEFAULTMAP_scalar);
9587 Loc = KindLoc;
9588 }
9589 Value += "'";
9590 Diag(Loc, diag::err_omp_unexpected_clause_value)
9591 << Value << getOpenMPClauseName(OMPC_defaultmap);
9592 return nullptr;
9593 }
9594
9595 return new (Context)
9596 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
9597}