blob: f249e3f1e073e351adb9c994510987262243ef57 [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 Bataeva839ddd2016-03-17 10:19:46 +000018#include "clang/AST/CXXInheritance.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000019#include "clang/AST/Decl.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000020#include "clang/AST/DeclCXX.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000021#include "clang/AST/DeclOpenMP.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000022#include "clang/AST/StmtCXX.h"
23#include "clang/AST/StmtOpenMP.h"
24#include "clang/AST/StmtVisitor.h"
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000025#include "clang/AST/TypeOrdering.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000026#include "clang/Basic/OpenMPKinds.h"
Samuel Antaof8b50122015-07-13 22:54:53 +000027#include "clang/Basic/TargetInfo.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000028#include "clang/Lex/Preprocessor.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000029#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000030#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000031#include "clang/Sema/Scope.h"
32#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000033#include "clang/Sema/SemaInternal.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000034using namespace clang;
35
Alexey Bataev758e55e2013-09-06 18:03:48 +000036//===----------------------------------------------------------------------===//
37// Stack of data-sharing attributes for variables
38//===----------------------------------------------------------------------===//
39
40namespace {
41/// \brief Default data sharing attributes, which can be applied to directive.
42enum DefaultDataSharingAttributes {
Alexey Bataeved09d242014-05-28 05:53:51 +000043 DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
44 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
45 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'.
Alexey Bataev758e55e2013-09-06 18:03:48 +000046};
Alexey Bataev7ff55242014-06-19 09:13:45 +000047
Alexey Bataev758e55e2013-09-06 18:03:48 +000048/// \brief Stack for tracking declarations used in OpenMP directives and
49/// clauses and their data-sharing attributes.
Alexey Bataev7ace49d2016-05-17 08:55:33 +000050class DSAStackTy final {
Alexey Bataev758e55e2013-09-06 18:03:48 +000051public:
Alexey Bataev7ace49d2016-05-17 08:55:33 +000052 struct DSAVarData final {
53 OpenMPDirectiveKind DKind = OMPD_unknown;
54 OpenMPClauseKind CKind = OMPC_unknown;
55 Expr *RefExpr = nullptr;
56 DeclRefExpr *PrivateCopy = nullptr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000057 SourceLocation ImplicitDSALoc;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000058 DSAVarData() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000059 };
Alexey Bataeved09d242014-05-28 05:53:51 +000060
Alexey Bataev758e55e2013-09-06 18:03:48 +000061private:
Alexey Bataev7ace49d2016-05-17 08:55:33 +000062 struct DSAInfo final {
63 OpenMPClauseKind Attributes = OMPC_unknown;
64 /// Pointer to a reference expression and a flag which shows that the
65 /// variable is marked as lastprivate(true) or not (false).
66 llvm::PointerIntPair<Expr *, 1, bool> RefExpr;
67 DeclRefExpr *PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +000068 };
Alexey Bataev90c228f2016-02-08 09:29:13 +000069 typedef llvm::DenseMap<ValueDecl *, DSAInfo> DeclSAMapTy;
70 typedef llvm::DenseMap<ValueDecl *, Expr *> AlignedMapTy;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +000071 typedef std::pair<unsigned, VarDecl *> LCDeclInfo;
72 typedef llvm::DenseMap<ValueDecl *, LCDeclInfo> LoopControlVariablesMapTy;
Samuel Antao90927002016-04-26 14:54:23 +000073 typedef llvm::DenseMap<
74 ValueDecl *, OMPClauseMappableExprCommon::MappableExprComponentLists>
75 MappedExprComponentsTy;
Alexey Bataev28c75412015-12-15 08:19:24 +000076 typedef llvm::StringMap<std::pair<OMPCriticalDirective *, llvm::APSInt>>
77 CriticalsWithHintsTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000078
Alexey Bataev7ace49d2016-05-17 08:55:33 +000079 struct SharingMapTy final {
Alexey Bataev758e55e2013-09-06 18:03:48 +000080 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000081 AlignedMapTy AlignedMap;
Samuel Antao90927002016-04-26 14:54:23 +000082 MappedExprComponentsTy MappedExprComponents;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000083 LoopControlVariablesMapTy LCVMap;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000084 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified;
Alexey Bataevbae9a792014-06-27 10:37:06 +000085 SourceLocation DefaultAttrLoc;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000086 OpenMPDirectiveKind Directive = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +000087 DeclarationNameInfo DirectiveName;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000088 Scope *CurScope = nullptr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000089 SourceLocation ConstructLoc;
Alexey Bataev346265e2015-09-25 10:37:12 +000090 /// \brief first argument (Expr *) contains optional argument of the
91 /// 'ordered' clause, the second one is true if the regions has 'ordered'
92 /// clause, false otherwise.
93 llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000094 bool NowaitRegion = false;
95 bool CancelRegion = false;
96 unsigned AssociatedLoops = 1;
Alexey Bataev13314bf2014-10-09 04:18:56 +000097 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +000098 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +000099 Scope *CurScope, SourceLocation Loc)
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000100 : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
101 ConstructLoc(Loc) {}
102 SharingMapTy() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000103 };
104
Axel Naumann323862e2016-02-03 10:45:22 +0000105 typedef SmallVector<SharingMapTy, 4> StackTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000106
107 /// \brief Stack of used declaration and their data-sharing attributes.
108 StackTy Stack;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000109 /// \brief true, if check for DSA must be from parent directive, false, if
110 /// from current directive.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000111 OpenMPClauseKind ClauseKindMode = OMPC_unknown;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000112 Sema &SemaRef;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000113 bool ForceCapturing = false;
Alexey Bataev28c75412015-12-15 08:19:24 +0000114 CriticalsWithHintsTy Criticals;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000115
116 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
117
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000118 DSAVarData getDSA(StackTy::reverse_iterator& Iter, ValueDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000119
120 /// \brief Checks if the variable is a local for OpenMP region.
121 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000122
Alexey Bataev758e55e2013-09-06 18:03:48 +0000123public:
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000124 explicit DSAStackTy(Sema &S) : Stack(1), SemaRef(S) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000125
Alexey Bataevaac108a2015-06-23 04:51:00 +0000126 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
127 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000128
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000129 bool isForceVarCapturing() const { return ForceCapturing; }
130 void setForceVarCapturing(bool V) { ForceCapturing = V; }
131
Alexey Bataev758e55e2013-09-06 18:03:48 +0000132 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000133 Scope *CurScope, SourceLocation Loc) {
134 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
135 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000136 }
137
138 void pop() {
139 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
140 Stack.pop_back();
141 }
142
Alexey Bataev28c75412015-12-15 08:19:24 +0000143 void addCriticalWithHint(OMPCriticalDirective *D, llvm::APSInt Hint) {
144 Criticals[D->getDirectiveName().getAsString()] = std::make_pair(D, Hint);
145 }
146 const std::pair<OMPCriticalDirective *, llvm::APSInt>
147 getCriticalWithHint(const DeclarationNameInfo &Name) const {
148 auto I = Criticals.find(Name.getAsString());
149 if (I != Criticals.end())
150 return I->second;
151 return std::make_pair(nullptr, llvm::APSInt());
152 }
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000153 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000154 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000155 /// for diagnostics.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000156 Expr *addUniqueAligned(ValueDecl *D, Expr *NewDE);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000157
Alexey Bataev9c821032015-04-30 04:23:23 +0000158 /// \brief Register specified variable as loop control variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000159 void addLoopControlVariable(ValueDecl *D, VarDecl *Capture);
Alexey Bataev9c821032015-04-30 04:23:23 +0000160 /// \brief Check if the specified variable is a loop control variable for
161 /// current region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000162 /// \return The index of the loop control variable in the list of associated
163 /// for-loops (from outer to inner).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000164 LCDeclInfo isLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000165 /// \brief Check if the specified variable is a loop control variable for
166 /// parent region.
167 /// \return The index of the loop control variable in the list of associated
168 /// for-loops (from outer to inner).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000169 LCDeclInfo isParentLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000170 /// \brief Get the loop control variable for the I-th loop (or nullptr) in
171 /// parent directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000172 ValueDecl *getParentLoopControlVariable(unsigned I);
Alexey Bataev9c821032015-04-30 04:23:23 +0000173
Alexey Bataev758e55e2013-09-06 18:03:48 +0000174 /// \brief Adds explicit data sharing attribute to the specified declaration.
Alexey Bataev90c228f2016-02-08 09:29:13 +0000175 void addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
176 DeclRefExpr *PrivateCopy = nullptr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000177
Alexey Bataev758e55e2013-09-06 18:03:48 +0000178 /// \brief Returns data sharing attributes from top of the stack for the
179 /// specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000180 DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000181 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000182 DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000183 /// \brief Checks if the specified variables has data-sharing attributes which
184 /// match specified \a CPred predicate in any directive which matches \a DPred
185 /// predicate.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000186 DSAVarData hasDSA(ValueDecl *D,
187 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
188 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
189 bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000190 /// \brief Checks if the specified variables has data-sharing attributes which
191 /// match specified \a CPred predicate in any innermost directive which
192 /// matches \a DPred predicate.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000193 DSAVarData
194 hasInnermostDSA(ValueDecl *D,
195 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
196 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
197 bool FromParent);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000198 /// \brief Checks if the specified variables has explicit data-sharing
199 /// attributes which match specified \a CPred predicate at the specified
200 /// OpenMP region.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000201 bool hasExplicitDSA(ValueDecl *D,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000202 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000203 unsigned Level, bool NotLastprivate = false);
Samuel Antao4be30e92015-10-02 17:14:03 +0000204
205 /// \brief Returns true if the directive at level \Level matches in the
206 /// specified \a DPred predicate.
207 bool hasExplicitDirective(
208 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
209 unsigned Level);
210
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000211 /// \brief Finds a directive which matches specified \a DPred predicate.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000212 bool hasDirective(const llvm::function_ref<bool(OpenMPDirectiveKind,
213 const DeclarationNameInfo &,
214 SourceLocation)> &DPred,
215 bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000216
Alexey Bataev758e55e2013-09-06 18:03:48 +0000217 /// \brief Returns currently analyzed directive.
218 OpenMPDirectiveKind getCurrentDirective() const {
219 return Stack.back().Directive;
220 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000221 /// \brief Returns parent directive.
222 OpenMPDirectiveKind getParentDirective() const {
223 if (Stack.size() > 2)
224 return Stack[Stack.size() - 2].Directive;
225 return OMPD_unknown;
226 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000227
228 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000229 void setDefaultDSANone(SourceLocation Loc) {
230 Stack.back().DefaultAttr = DSA_none;
231 Stack.back().DefaultAttrLoc = Loc;
232 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000233 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000234 void setDefaultDSAShared(SourceLocation Loc) {
235 Stack.back().DefaultAttr = DSA_shared;
236 Stack.back().DefaultAttrLoc = Loc;
237 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000238
239 DefaultDataSharingAttributes getDefaultDSA() const {
240 return Stack.back().DefaultAttr;
241 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000242 SourceLocation getDefaultDSALocation() const {
243 return Stack.back().DefaultAttrLoc;
244 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000245
Alexey Bataevf29276e2014-06-18 04:14:57 +0000246 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000247 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000248 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000249 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000250 }
251
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000252 /// \brief Marks current region as ordered (it has an 'ordered' clause).
Alexey Bataev346265e2015-09-25 10:37:12 +0000253 void setOrderedRegion(bool IsOrdered, Expr *Param) {
254 Stack.back().OrderedRegion.setInt(IsOrdered);
255 Stack.back().OrderedRegion.setPointer(Param);
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000256 }
257 /// \brief Returns true, if parent region is ordered (has associated
258 /// 'ordered' clause), false - otherwise.
259 bool isParentOrderedRegion() const {
260 if (Stack.size() > 2)
Alexey Bataev346265e2015-09-25 10:37:12 +0000261 return Stack[Stack.size() - 2].OrderedRegion.getInt();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000262 return false;
263 }
Alexey Bataev346265e2015-09-25 10:37:12 +0000264 /// \brief Returns optional parameter for the ordered region.
265 Expr *getParentOrderedRegionParam() const {
266 if (Stack.size() > 2)
267 return Stack[Stack.size() - 2].OrderedRegion.getPointer();
268 return nullptr;
269 }
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000270 /// \brief Marks current region as nowait (it has a 'nowait' clause).
271 void setNowaitRegion(bool IsNowait = true) {
272 Stack.back().NowaitRegion = IsNowait;
273 }
274 /// \brief Returns true, if parent region is nowait (has associated
275 /// 'nowait' clause), false - otherwise.
276 bool isParentNowaitRegion() const {
277 if (Stack.size() > 2)
278 return Stack[Stack.size() - 2].NowaitRegion;
279 return false;
280 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000281 /// \brief Marks parent region as cancel region.
282 void setParentCancelRegion(bool Cancel = true) {
283 if (Stack.size() > 2)
284 Stack[Stack.size() - 2].CancelRegion =
285 Stack[Stack.size() - 2].CancelRegion || Cancel;
286 }
287 /// \brief Return true if current region has inner cancel construct.
288 bool isCancelRegion() const {
289 return Stack.back().CancelRegion;
290 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000291
Alexey Bataev9c821032015-04-30 04:23:23 +0000292 /// \brief Set collapse value for the region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000293 void setAssociatedLoops(unsigned Val) { Stack.back().AssociatedLoops = Val; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000294 /// \brief Return collapse value for region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000295 unsigned getAssociatedLoops() const { return Stack.back().AssociatedLoops; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000296
Alexey Bataev13314bf2014-10-09 04:18:56 +0000297 /// \brief Marks current target region as one with closely nested teams
298 /// region.
299 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
300 if (Stack.size() > 2)
301 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
302 }
303 /// \brief Returns true, if current region has closely nested teams region.
304 bool hasInnerTeamsRegion() const {
305 return getInnerTeamsRegionLoc().isValid();
306 }
307 /// \brief Returns location of the nested teams region (if any).
308 SourceLocation getInnerTeamsRegionLoc() const {
309 if (Stack.size() > 1)
310 return Stack.back().InnerTeamsRegionLoc;
311 return SourceLocation();
312 }
313
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000314 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000315 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000316 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000317
Samuel Antao90927002016-04-26 14:54:23 +0000318 // Do the check specified in \a Check to all component lists and return true
319 // if any issue is found.
320 bool checkMappableExprComponentListsForDecl(
321 ValueDecl *VD, bool CurrentRegionOnly,
322 const llvm::function_ref<bool(
323 OMPClauseMappableExprCommon::MappableExprComponentListRef)> &Check) {
Samuel Antao5de996e2016-01-22 20:21:36 +0000324 auto SI = Stack.rbegin();
325 auto SE = Stack.rend();
326
327 if (SI == SE)
328 return false;
329
330 if (CurrentRegionOnly) {
331 SE = std::next(SI);
332 } else {
333 ++SI;
334 }
335
336 for (; SI != SE; ++SI) {
Samuel Antao90927002016-04-26 14:54:23 +0000337 auto MI = SI->MappedExprComponents.find(VD);
338 if (MI != SI->MappedExprComponents.end())
339 for (auto &L : MI->second)
340 if (Check(L))
Samuel Antao5de996e2016-01-22 20:21:36 +0000341 return true;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000342 }
Samuel Antao5de996e2016-01-22 20:21:36 +0000343 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000344 }
345
Samuel Antao90927002016-04-26 14:54:23 +0000346 // Create a new mappable expression component list associated with a given
347 // declaration and initialize it with the provided list of components.
348 void addMappableExpressionComponents(
349 ValueDecl *VD,
350 OMPClauseMappableExprCommon::MappableExprComponentListRef Components) {
351 assert(Stack.size() > 1 &&
352 "Not expecting to retrieve components from a empty stack!");
353 auto &MEC = Stack.back().MappedExprComponents[VD];
354 // Create new entry and append the new components there.
355 MEC.resize(MEC.size() + 1);
356 MEC.back().append(Components.begin(), Components.end());
Kelvin Li0bff7af2015-11-23 05:32:03 +0000357 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000358
359 unsigned getNestingLevel() const {
360 assert(Stack.size() > 1);
361 return Stack.size() - 2;
362 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000363};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000364bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
Alexey Bataev35aaee62016-04-13 13:36:48 +0000365 return isOpenMPParallelDirective(DKind) || isOpenMPTaskingDirective(DKind) ||
366 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000367}
Alexey Bataeved09d242014-05-28 05:53:51 +0000368} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000369
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000370static ValueDecl *getCanonicalDecl(ValueDecl *D) {
371 auto *VD = dyn_cast<VarDecl>(D);
372 auto *FD = dyn_cast<FieldDecl>(D);
373 if (VD != nullptr) {
374 VD = VD->getCanonicalDecl();
375 D = VD;
376 } else {
377 assert(FD);
378 FD = FD->getCanonicalDecl();
379 D = FD;
380 }
381 return D;
382}
383
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000384DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator& Iter,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000385 ValueDecl *D) {
386 D = getCanonicalDecl(D);
387 auto *VD = dyn_cast<VarDecl>(D);
388 auto *FD = dyn_cast<FieldDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000389 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000390 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000391 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
392 // in a region but not in construct]
393 // File-scope or namespace-scope variables referenced in called routines
394 // in the region are shared unless they appear in a threadprivate
395 // directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000396 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000397 DVar.CKind = OMPC_shared;
398
399 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
400 // in a region but not in construct]
401 // Variables with static storage duration that are declared in called
402 // routines in the region are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000403 if (VD && VD->hasGlobalStorage())
404 DVar.CKind = OMPC_shared;
405
406 // Non-static data members are shared by default.
407 if (FD)
Alexey Bataev750a58b2014-03-18 12:19:12 +0000408 DVar.CKind = OMPC_shared;
409
Alexey Bataev758e55e2013-09-06 18:03:48 +0000410 return DVar;
411 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000412
Alexey Bataev758e55e2013-09-06 18:03:48 +0000413 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000414 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
415 // in a Construct, C/C++, predetermined, p.1]
416 // Variables with automatic storage duration that are declared in a scope
417 // inside the construct are private.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000418 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
419 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000420 DVar.CKind = OMPC_private;
421 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000422 }
423
Alexey Bataev758e55e2013-09-06 18:03:48 +0000424 // Explicitly specified attributes and local variables with predetermined
425 // attributes.
426 if (Iter->SharingMap.count(D)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000427 DVar.RefExpr = Iter->SharingMap[D].RefExpr.getPointer();
Alexey Bataev90c228f2016-02-08 09:29:13 +0000428 DVar.PrivateCopy = Iter->SharingMap[D].PrivateCopy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000429 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000430 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000431 return DVar;
432 }
433
434 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
435 // in a Construct, C/C++, implicitly determined, p.1]
436 // In a parallel or task construct, the data-sharing attributes of these
437 // variables are determined by the default clause, if present.
438 switch (Iter->DefaultAttr) {
439 case DSA_shared:
440 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000441 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000442 return DVar;
443 case DSA_none:
444 return DVar;
445 case DSA_unspecified:
446 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
447 // in a Construct, implicitly determined, p.2]
448 // In a parallel construct, if no default clause is present, these
449 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000450 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000451 if (isOpenMPParallelDirective(DVar.DKind) ||
452 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000453 DVar.CKind = OMPC_shared;
454 return DVar;
455 }
456
457 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
458 // in a Construct, implicitly determined, p.4]
459 // In a task construct, if no default clause is present, a variable that in
460 // the enclosing context is determined to be shared by all implicit tasks
461 // bound to the current team is shared.
Alexey Bataev35aaee62016-04-13 13:36:48 +0000462 if (isOpenMPTaskingDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000463 DSAVarData DVarTemp;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000464 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000465 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000466 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
Alexey Bataev35aaee62016-04-13 13:36:48 +0000467 // Referenced in a Construct, implicitly determined, p.6]
Alexey Bataev758e55e2013-09-06 18:03:48 +0000468 // In a task construct, if no default clause is present, a variable
469 // whose data-sharing attribute is not determined by the rules above is
470 // firstprivate.
471 DVarTemp = getDSA(I, D);
472 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000473 DVar.RefExpr = nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000474 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000475 return DVar;
476 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000477 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000478 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000479 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000480 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000481 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000482 return DVar;
483 }
484 }
485 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
486 // in a Construct, implicitly determined, p.3]
487 // For constructs other than task, if no default clause is present, these
488 // variables inherit their data-sharing attributes from the enclosing
489 // context.
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000490 return getDSA(++Iter, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000491}
492
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000493Expr *DSAStackTy::addUniqueAligned(ValueDecl *D, Expr *NewDE) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000494 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000495 D = getCanonicalDecl(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000496 auto It = Stack.back().AlignedMap.find(D);
497 if (It == Stack.back().AlignedMap.end()) {
498 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
499 Stack.back().AlignedMap[D] = NewDE;
500 return nullptr;
501 } else {
502 assert(It->second && "Unexpected nullptr expr in the aligned map");
503 return It->second;
504 }
505 return nullptr;
506}
507
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000508void DSAStackTy::addLoopControlVariable(ValueDecl *D, VarDecl *Capture) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000509 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000510 D = getCanonicalDecl(D);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000511 Stack.back().LCVMap.insert(
512 std::make_pair(D, LCDeclInfo(Stack.back().LCVMap.size() + 1, Capture)));
Alexey Bataev9c821032015-04-30 04:23:23 +0000513}
514
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000515DSAStackTy::LCDeclInfo DSAStackTy::isLoopControlVariable(ValueDecl *D) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000516 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000517 D = getCanonicalDecl(D);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000518 return Stack.back().LCVMap.count(D) > 0 ? Stack.back().LCVMap[D]
519 : LCDeclInfo(0, nullptr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000520}
521
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000522DSAStackTy::LCDeclInfo DSAStackTy::isParentLoopControlVariable(ValueDecl *D) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000523 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000524 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000525 return Stack[Stack.size() - 2].LCVMap.count(D) > 0
526 ? Stack[Stack.size() - 2].LCVMap[D]
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000527 : LCDeclInfo(0, nullptr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000528}
529
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000530ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000531 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
532 if (Stack[Stack.size() - 2].LCVMap.size() < I)
533 return nullptr;
534 for (auto &Pair : Stack[Stack.size() - 2].LCVMap) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000535 if (Pair.second.first == I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000536 return Pair.first;
537 }
538 return nullptr;
Alexey Bataev9c821032015-04-30 04:23:23 +0000539}
540
Alexey Bataev90c228f2016-02-08 09:29:13 +0000541void DSAStackTy::addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
542 DeclRefExpr *PrivateCopy) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000543 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000544 if (A == OMPC_threadprivate) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000545 auto &Data = Stack[0].SharingMap[D];
546 Data.Attributes = A;
547 Data.RefExpr.setPointer(E);
548 Data.PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000549 } else {
550 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000551 auto &Data = Stack.back().SharingMap[D];
552 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
553 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
554 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
555 (isLoopControlVariable(D).first && A == OMPC_private));
556 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
557 Data.RefExpr.setInt(/*IntVal=*/true);
558 return;
559 }
560 const bool IsLastprivate =
561 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
562 Data.Attributes = A;
563 Data.RefExpr.setPointerAndInt(E, IsLastprivate);
564 Data.PrivateCopy = PrivateCopy;
565 if (PrivateCopy) {
566 auto &Data = Stack.back().SharingMap[PrivateCopy->getDecl()];
567 Data.Attributes = A;
568 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
569 Data.PrivateCopy = nullptr;
570 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000571 }
572}
573
Alexey Bataeved09d242014-05-28 05:53:51 +0000574bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000575 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000576 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000577 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000578 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000579 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000580 ++I;
581 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000582 if (I == E)
583 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000584 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000585 Scope *CurScope = getCurScope();
586 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000587 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000588 }
589 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000590 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000591 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000592}
593
Alexey Bataev39f915b82015-05-08 10:41:21 +0000594/// \brief Build a variable declaration for OpenMP loop iteration variable.
595static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000596 StringRef Name, const AttrVec *Attrs = nullptr) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000597 DeclContext *DC = SemaRef.CurContext;
598 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
599 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
600 VarDecl *Decl =
601 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000602 if (Attrs) {
603 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
604 I != E; ++I)
605 Decl->addAttr(*I);
606 }
Alexey Bataev39f915b82015-05-08 10:41:21 +0000607 Decl->setImplicit();
608 return Decl;
609}
610
611static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
612 SourceLocation Loc,
613 bool RefersToCapture = false) {
614 D->setReferenced();
615 D->markUsed(S.Context);
616 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
617 SourceLocation(), D, RefersToCapture, Loc, Ty,
618 VK_LValue);
619}
620
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000621DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, bool FromParent) {
622 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000623 DSAVarData DVar;
624
625 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
626 // in a Construct, C/C++, predetermined, p.1]
627 // Variables appearing in threadprivate directives are threadprivate.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000628 auto *VD = dyn_cast<VarDecl>(D);
629 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
630 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
Samuel Antaof8b50122015-07-13 22:54:53 +0000631 SemaRef.getLangOpts().OpenMPUseTLS &&
632 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000633 (VD && VD->getStorageClass() == SC_Register &&
634 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
635 addDSA(D, buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
Alexey Bataev39f915b82015-05-08 10:41:21 +0000636 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000637 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000638 }
639 if (Stack[0].SharingMap.count(D)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000640 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr.getPointer();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000641 DVar.CKind = OMPC_threadprivate;
642 return DVar;
643 }
644
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000645 if (Stack.size() == 1) {
646 // Not in OpenMP execution region and top scope was already checked.
647 return DVar;
648 }
649
Alexey Bataev758e55e2013-09-06 18:03:48 +0000650 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000651 // in a Construct, C/C++, predetermined, p.4]
652 // Static data members are shared.
653 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
654 // in a Construct, C/C++, predetermined, p.7]
655 // Variables with static storage duration that are declared in a scope
656 // inside the construct are shared.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000657 auto &&MatchesAlways = [](OpenMPDirectiveKind) -> bool { return true; };
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000658 if (VD && VD->isStaticDataMember()) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000659 DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent);
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000660 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +0000661 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000662
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000663 DVar.CKind = OMPC_shared;
664 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000665 }
666
667 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000668 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
669 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000670 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
671 // in a Construct, C/C++, predetermined, p.6]
672 // Variables with const qualified type having no mutable member are
673 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000674 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000675 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataevc9bd03d2015-12-17 06:55:08 +0000676 if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
677 if (auto *CTD = CTSD->getSpecializedTemplate())
678 RD = CTD->getTemplatedDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000679 if (IsConstant &&
Alexey Bataev4bcad7f2016-02-10 10:50:12 +0000680 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
681 RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000682 // Variables with const-qualified type having no mutable member may be
683 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000684 DSAVarData DVarTemp = hasDSA(
685 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_firstprivate; },
686 MatchesAlways, FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000687 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
688 return DVar;
689
Alexey Bataev758e55e2013-09-06 18:03:48 +0000690 DVar.CKind = OMPC_shared;
691 return DVar;
692 }
693
Alexey Bataev758e55e2013-09-06 18:03:48 +0000694 // Explicitly specified attributes and local variables with predetermined
695 // attributes.
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000696 auto StartI = std::next(Stack.rbegin());
697 auto EndI = std::prev(Stack.rend());
698 if (FromParent && StartI != EndI) {
699 StartI = std::next(StartI);
700 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000701 auto I = std::prev(StartI);
702 if (I->SharingMap.count(D)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000703 DVar.RefExpr = I->SharingMap[D].RefExpr.getPointer();
Alexey Bataev90c228f2016-02-08 09:29:13 +0000704 DVar.PrivateCopy = I->SharingMap[D].PrivateCopy;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000705 DVar.CKind = I->SharingMap[D].Attributes;
706 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000707 }
708
709 return DVar;
710}
711
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000712DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
713 bool FromParent) {
714 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000715 auto StartI = Stack.rbegin();
716 auto EndI = std::prev(Stack.rend());
717 if (FromParent && StartI != EndI) {
718 StartI = std::next(StartI);
719 }
720 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000721}
722
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000723DSAStackTy::DSAVarData
724DSAStackTy::hasDSA(ValueDecl *D,
725 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
726 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
727 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000728 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000729 auto StartI = std::next(Stack.rbegin());
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000730 auto EndI = Stack.rend();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000731 if (FromParent && StartI != EndI) {
732 StartI = std::next(StartI);
733 }
734 for (auto I = StartI, EE = EndI; I != EE; ++I) {
735 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000736 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000737 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000738 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000739 return DVar;
740 }
741 return DSAVarData();
742}
743
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000744DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
745 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
746 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
747 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000748 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000749 auto StartI = std::next(Stack.rbegin());
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000750 auto EndI = Stack.rend();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000751 if (FromParent && StartI != EndI) {
752 StartI = std::next(StartI);
753 }
754 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000755 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000756 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000757 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000758 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000759 return DVar;
760 return DSAVarData();
761 }
762 return DSAVarData();
763}
764
Alexey Bataevaac108a2015-06-23 04:51:00 +0000765bool DSAStackTy::hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000766 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000767 unsigned Level, bool NotLastprivate) {
Alexey Bataevaac108a2015-06-23 04:51:00 +0000768 if (CPred(ClauseKindMode))
769 return true;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000770 D = getCanonicalDecl(D);
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000771 auto StartI = std::next(Stack.begin());
772 auto EndI = Stack.end();
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000773 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000774 return false;
775 std::advance(StartI, Level);
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000776 return (StartI->SharingMap.count(D) > 0) &&
777 StartI->SharingMap[D].RefExpr.getPointer() &&
778 CPred(StartI->SharingMap[D].Attributes) &&
779 (!NotLastprivate || !StartI->SharingMap[D].RefExpr.getInt());
Alexey Bataevaac108a2015-06-23 04:51:00 +0000780}
781
Samuel Antao4be30e92015-10-02 17:14:03 +0000782bool DSAStackTy::hasExplicitDirective(
783 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
784 unsigned Level) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000785 auto StartI = std::next(Stack.begin());
786 auto EndI = Stack.end();
Samuel Antao4be30e92015-10-02 17:14:03 +0000787 if (std::distance(StartI, EndI) <= (int)Level)
788 return false;
789 std::advance(StartI, Level);
790 return DPred(StartI->Directive);
791}
792
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000793bool DSAStackTy::hasDirective(
794 const llvm::function_ref<bool(OpenMPDirectiveKind,
795 const DeclarationNameInfo &, SourceLocation)>
796 &DPred,
797 bool FromParent) {
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000798 auto StartI = std::next(Stack.rbegin());
799 auto EndI = std::prev(Stack.rend());
800 if (FromParent && StartI != EndI) {
801 StartI = std::next(StartI);
802 }
803 for (auto I = StartI, EE = EndI; I != EE; ++I) {
804 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
805 return true;
806 }
807 return false;
808}
809
Alexey Bataev758e55e2013-09-06 18:03:48 +0000810void Sema::InitDataSharingAttributesStack() {
811 VarDataSharingAttributesStack = new DSAStackTy(*this);
812}
813
814#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
815
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000816bool Sema::IsOpenMPCapturedByRef(ValueDecl *D, unsigned Level) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000817 assert(LangOpts.OpenMP && "OpenMP is not allowed");
818
819 auto &Ctx = getASTContext();
820 bool IsByRef = true;
821
822 // Find the directive that is associated with the provided scope.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000823 auto Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000824
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000825 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000826 // This table summarizes how a given variable should be passed to the device
827 // given its type and the clauses where it appears. This table is based on
828 // the description in OpenMP 4.5 [2.10.4, target Construct] and
829 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
830 //
831 // =========================================================================
832 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
833 // | |(tofrom:scalar)| | pvt | | | |
834 // =========================================================================
835 // | scl | | | | - | | bycopy|
836 // | scl | | - | x | - | - | bycopy|
837 // | scl | | x | - | - | - | null |
838 // | scl | x | | | - | | byref |
839 // | scl | x | - | x | - | - | bycopy|
840 // | scl | x | x | - | - | - | null |
841 // | scl | | - | - | - | x | byref |
842 // | scl | x | - | - | - | x | byref |
843 //
844 // | agg | n.a. | | | - | | byref |
845 // | agg | n.a. | - | x | - | - | byref |
846 // | agg | n.a. | x | - | - | - | null |
847 // | agg | n.a. | - | - | - | x | byref |
848 // | agg | n.a. | - | - | - | x[] | byref |
849 //
850 // | ptr | n.a. | | | - | | bycopy|
851 // | ptr | n.a. | - | x | - | - | bycopy|
852 // | ptr | n.a. | x | - | - | - | null |
853 // | ptr | n.a. | - | - | - | x | byref |
854 // | ptr | n.a. | - | - | - | x[] | bycopy|
855 // | ptr | n.a. | - | - | x | | bycopy|
856 // | ptr | n.a. | - | - | x | x | bycopy|
857 // | ptr | n.a. | - | - | x | x[] | bycopy|
858 // =========================================================================
859 // Legend:
860 // scl - scalar
861 // ptr - pointer
862 // agg - aggregate
863 // x - applies
864 // - - invalid in this combination
865 // [] - mapped with an array section
866 // byref - should be mapped by reference
867 // byval - should be mapped by value
868 // null - initialize a local variable to null on the device
869 //
870 // Observations:
871 // - All scalar declarations that show up in a map clause have to be passed
872 // by reference, because they may have been mapped in the enclosing data
873 // environment.
874 // - If the scalar value does not fit the size of uintptr, it has to be
875 // passed by reference, regardless the result in the table above.
876 // - For pointers mapped by value that have either an implicit map or an
877 // array section, the runtime library may pass the NULL value to the
878 // device instead of the value passed to it by the compiler.
879
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000880
881 if (Ty->isReferenceType())
882 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
Samuel Antao86ace552016-04-27 22:40:57 +0000883
884 // Locate map clauses and see if the variable being captured is referred to
885 // in any of those clauses. Here we only care about variables, not fields,
886 // because fields are part of aggregates.
887 bool IsVariableUsedInMapClause = false;
888 bool IsVariableAssociatedWithSection = false;
889
890 DSAStack->checkMappableExprComponentListsForDecl(
891 D, /*CurrentRegionOnly=*/true,
892 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
893 MapExprComponents) {
894
895 auto EI = MapExprComponents.rbegin();
896 auto EE = MapExprComponents.rend();
897
898 assert(EI != EE && "Invalid map expression!");
899
900 if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
901 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
902
903 ++EI;
904 if (EI == EE)
905 return false;
906
907 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
908 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
909 isa<MemberExpr>(EI->getAssociatedExpression())) {
910 IsVariableAssociatedWithSection = true;
911 // There is nothing more we need to know about this variable.
912 return true;
913 }
914
915 // Keep looking for more map info.
916 return false;
917 });
918
919 if (IsVariableUsedInMapClause) {
920 // If variable is identified in a map clause it is always captured by
921 // reference except if it is a pointer that is dereferenced somehow.
922 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
923 } else {
924 // By default, all the data that has a scalar type is mapped by copy.
925 IsByRef = !Ty->isScalarType();
926 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000927 }
928
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000929 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
930 IsByRef = !DSAStack->hasExplicitDSA(
931 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
932 Level, /*NotLastprivate=*/true);
933 }
934
Samuel Antao86ace552016-04-27 22:40:57 +0000935 // When passing data by copy, we need to make sure it fits the uintptr size
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000936 // and alignment, because the runtime library only deals with uintptr types.
937 // If it does not fit the uintptr size, we need to pass the data by reference
938 // instead.
939 if (!IsByRef &&
940 (Ctx.getTypeSizeInChars(Ty) >
941 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000942 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000943 IsByRef = true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000944 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000945
946 return IsByRef;
947}
948
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000949unsigned Sema::getOpenMPNestingLevel() const {
950 assert(getLangOpts().OpenMP);
951 return DSAStack->getNestingLevel();
952}
953
Alexey Bataev90c228f2016-02-08 09:29:13 +0000954VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +0000955 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000956 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +0000957
958 // If we are attempting to capture a global variable in a directive with
959 // 'target' we return true so that this global is also mapped to the device.
960 //
961 // FIXME: If the declaration is enclosed in a 'declare target' directive,
962 // then it should not be captured. Therefore, an extra check has to be
963 // inserted here once support for 'declare target' is added.
964 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000965 auto *VD = dyn_cast<VarDecl>(D);
966 if (VD && !VD->hasLocalStorage()) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000967 if (DSAStack->getCurrentDirective() == OMPD_target &&
Alexey Bataev90c228f2016-02-08 09:29:13 +0000968 !DSAStack->isClauseParsingMode())
969 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +0000970 if (DSAStack->getCurScope() &&
971 DSAStack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000972 [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
973 SourceLocation) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000974 return isOpenMPTargetExecutionDirective(K);
Samuel Antao4be30e92015-10-02 17:14:03 +0000975 },
Alexey Bataev90c228f2016-02-08 09:29:13 +0000976 false))
977 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +0000978 }
979
Alexey Bataev48977c32015-08-04 08:10:48 +0000980 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
981 (!DSAStack->isClauseParsingMode() ||
982 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000983 auto &&Info = DSAStack->isLoopControlVariable(D);
984 if (Info.first ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000985 (VD && VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000986 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000987 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000988 return VD ? VD : Info.second;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000989 auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000990 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +0000991 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000992 DVarPrivate = DSAStack->hasDSA(
993 D, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
994 DSAStack->isClauseParsingMode());
Alexey Bataev90c228f2016-02-08 09:29:13 +0000995 if (DVarPrivate.CKind != OMPC_unknown)
996 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000997 }
Alexey Bataev90c228f2016-02-08 09:29:13 +0000998 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +0000999}
1000
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001001bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) {
Alexey Bataevaac108a2015-06-23 04:51:00 +00001002 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1003 return DSAStack->hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001004 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
Alexey Bataevaac108a2015-06-23 04:51:00 +00001005}
1006
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001007bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) {
Samuel Antao4be30e92015-10-02 17:14:03 +00001008 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1009 // Return true if the current level is no longer enclosed in a target region.
1010
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001011 auto *VD = dyn_cast<VarDecl>(D);
1012 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00001013 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1014 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +00001015}
1016
Alexey Bataeved09d242014-05-28 05:53:51 +00001017void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001018
1019void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
1020 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001021 Scope *CurScope, SourceLocation Loc) {
1022 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001023 PushExpressionEvaluationContext(PotentiallyEvaluated);
1024}
1025
Alexey Bataevaac108a2015-06-23 04:51:00 +00001026void Sema::StartOpenMPClause(OpenMPClauseKind K) {
1027 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001028}
1029
Alexey Bataevaac108a2015-06-23 04:51:00 +00001030void Sema::EndOpenMPClause() {
1031 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001032}
1033
Alexey Bataev758e55e2013-09-06 18:03:48 +00001034void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001035 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
1036 // A variable of class type (or array thereof) that appears in a lastprivate
1037 // clause requires an accessible, unambiguous default constructor for the
1038 // class type, unless the list item is also specified in a firstprivate
1039 // clause.
1040 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001041 for (auto *C : D->clauses()) {
1042 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
1043 SmallVector<Expr *, 8> PrivateCopies;
1044 for (auto *DE : Clause->varlists()) {
1045 if (DE->isValueDependent() || DE->isTypeDependent()) {
1046 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001047 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +00001048 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00001049 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
Alexey Bataev005248a2016-02-25 05:25:57 +00001050 VarDecl *VD = cast<VarDecl>(DRE->getDecl());
1051 QualType Type = VD->getType().getNonReferenceType();
1052 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001053 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001054 // Generate helper private variable and initialize it with the
1055 // default value. The address of the original variable is replaced
1056 // by the address of the new private variable in CodeGen. This new
1057 // variable is not added to IdResolver, so the code in the OpenMP
1058 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001059 auto *VDPrivate = buildVarDecl(
1060 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev005248a2016-02-25 05:25:57 +00001061 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00001062 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
1063 if (VDPrivate->isInvalidDecl())
1064 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001065 PrivateCopies.push_back(buildDeclRefExpr(
1066 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001067 } else {
1068 // The variable is also a firstprivate, so initialization sequence
1069 // for private copy is generated already.
1070 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001071 }
1072 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001073 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001074 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001075 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001076 }
1077 }
1078 }
1079
Alexey Bataev758e55e2013-09-06 18:03:48 +00001080 DSAStack->pop();
1081 DiscardCleanupsInEvaluationContext();
1082 PopExpressionEvaluationContext();
1083}
1084
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001085static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1086 Expr *NumIterations, Sema &SemaRef,
1087 Scope *S, DSAStackTy *Stack);
Alexander Musman3276a272015-03-21 10:12:56 +00001088
Alexey Bataeva769e072013-03-22 06:34:35 +00001089namespace {
1090
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001091class VarDeclFilterCCC : public CorrectionCandidateCallback {
1092private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001093 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001094
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001095public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001096 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001097 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001098 NamedDecl *ND = Candidate.getCorrectionDecl();
1099 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
1100 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001101 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1102 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001103 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001104 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001105 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001106};
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001107
1108class VarOrFuncDeclFilterCCC : public CorrectionCandidateCallback {
1109private:
1110 Sema &SemaRef;
1111
1112public:
1113 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
1114 bool ValidateCandidate(const TypoCorrection &Candidate) override {
1115 NamedDecl *ND = Candidate.getCorrectionDecl();
1116 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
1117 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1118 SemaRef.getCurScope());
1119 }
1120 return false;
1121 }
1122};
1123
Alexey Bataeved09d242014-05-28 05:53:51 +00001124} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001125
1126ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1127 CXXScopeSpec &ScopeSpec,
1128 const DeclarationNameInfo &Id) {
1129 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1130 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1131
1132 if (Lookup.isAmbiguous())
1133 return ExprError();
1134
1135 VarDecl *VD;
1136 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001137 if (TypoCorrection Corrected = CorrectTypo(
1138 Id, LookupOrdinaryName, CurScope, nullptr,
1139 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001140 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001141 PDiag(Lookup.empty()
1142 ? diag::err_undeclared_var_use_suggest
1143 : diag::err_omp_expected_var_arg_suggest)
1144 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001145 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001146 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001147 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1148 : diag::err_omp_expected_var_arg)
1149 << Id.getName();
1150 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001151 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001152 } else {
1153 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001154 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001155 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1156 return ExprError();
1157 }
1158 }
1159 Lookup.suppressDiagnostics();
1160
1161 // OpenMP [2.9.2, Syntax, C/C++]
1162 // Variables must be file-scope, namespace-scope, or static block-scope.
1163 if (!VD->hasGlobalStorage()) {
1164 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001165 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1166 bool IsDecl =
1167 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001168 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001169 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1170 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001171 return ExprError();
1172 }
1173
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001174 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1175 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001176 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1177 // A threadprivate directive for file-scope variables must appear outside
1178 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001179 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1180 !getCurLexicalContext()->isTranslationUnit()) {
1181 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001182 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1183 bool IsDecl =
1184 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1185 Diag(VD->getLocation(),
1186 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1187 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001188 return ExprError();
1189 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001190 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1191 // A threadprivate directive for static class member variables must appear
1192 // in the class definition, in the same scope in which the member
1193 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001194 if (CanonicalVD->isStaticDataMember() &&
1195 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1196 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001197 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1198 bool IsDecl =
1199 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1200 Diag(VD->getLocation(),
1201 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1202 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001203 return ExprError();
1204 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001205 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1206 // A threadprivate directive for namespace-scope variables must appear
1207 // outside any definition or declaration other than the namespace
1208 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001209 if (CanonicalVD->getDeclContext()->isNamespace() &&
1210 (!getCurLexicalContext()->isFileContext() ||
1211 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1212 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001213 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1214 bool IsDecl =
1215 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1216 Diag(VD->getLocation(),
1217 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1218 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001219 return ExprError();
1220 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001221 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1222 // A threadprivate directive for static block-scope variables must appear
1223 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001224 if (CanonicalVD->isStaticLocal() && CurScope &&
1225 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001226 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001227 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1228 bool IsDecl =
1229 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1230 Diag(VD->getLocation(),
1231 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1232 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001233 return ExprError();
1234 }
1235
1236 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1237 // A threadprivate directive must lexically precede all references to any
1238 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001239 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001240 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001241 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001242 return ExprError();
1243 }
1244
1245 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00001246 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1247 SourceLocation(), VD,
1248 /*RefersToEnclosingVariableOrCapture=*/false,
1249 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001250}
1251
Alexey Bataeved09d242014-05-28 05:53:51 +00001252Sema::DeclGroupPtrTy
1253Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1254 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001255 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001256 CurContext->addDecl(D);
1257 return DeclGroupPtrTy::make(DeclGroupRef(D));
1258 }
David Blaikie0403cb12016-01-15 23:43:25 +00001259 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001260}
1261
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001262namespace {
1263class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1264 Sema &SemaRef;
1265
1266public:
1267 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1268 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1269 if (VD->hasLocalStorage()) {
1270 SemaRef.Diag(E->getLocStart(),
1271 diag::err_omp_local_var_in_threadprivate_init)
1272 << E->getSourceRange();
1273 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1274 << VD << VD->getSourceRange();
1275 return true;
1276 }
1277 }
1278 return false;
1279 }
1280 bool VisitStmt(const Stmt *S) {
1281 for (auto Child : S->children()) {
1282 if (Child && Visit(Child))
1283 return true;
1284 }
1285 return false;
1286 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001287 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001288};
1289} // namespace
1290
Alexey Bataeved09d242014-05-28 05:53:51 +00001291OMPThreadPrivateDecl *
1292Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001293 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001294 for (auto &RefExpr : VarList) {
1295 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001296 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1297 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001298
Alexey Bataev376b4a42016-02-09 09:41:09 +00001299 // Mark variable as used.
1300 VD->setReferenced();
1301 VD->markUsed(Context);
1302
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001303 QualType QType = VD->getType();
1304 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1305 // It will be analyzed later.
1306 Vars.push_back(DE);
1307 continue;
1308 }
1309
Alexey Bataeva769e072013-03-22 06:34:35 +00001310 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1311 // A threadprivate variable must not have an incomplete type.
1312 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001313 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001314 continue;
1315 }
1316
1317 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1318 // A threadprivate variable must not have a reference type.
1319 if (VD->getType()->isReferenceType()) {
1320 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001321 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1322 bool IsDecl =
1323 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1324 Diag(VD->getLocation(),
1325 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1326 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001327 continue;
1328 }
1329
Samuel Antaof8b50122015-07-13 22:54:53 +00001330 // Check if this is a TLS variable. If TLS is not being supported, produce
1331 // the corresponding diagnostic.
1332 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1333 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1334 getLangOpts().OpenMPUseTLS &&
1335 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001336 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1337 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001338 Diag(ILoc, diag::err_omp_var_thread_local)
1339 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001340 bool IsDecl =
1341 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1342 Diag(VD->getLocation(),
1343 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1344 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001345 continue;
1346 }
1347
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001348 // Check if initial value of threadprivate variable reference variable with
1349 // local storage (it is not supported by runtime).
1350 if (auto Init = VD->getAnyInitializer()) {
1351 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001352 if (Checker.Visit(Init))
1353 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001354 }
1355
Alexey Bataeved09d242014-05-28 05:53:51 +00001356 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001357 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001358 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1359 Context, SourceRange(Loc, Loc)));
1360 if (auto *ML = Context.getASTMutationListener())
1361 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001362 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001363 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001364 if (!Vars.empty()) {
1365 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1366 Vars);
1367 D->setAccess(AS_public);
1368 }
1369 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001370}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001371
Alexey Bataev7ff55242014-06-19 09:13:45 +00001372static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001373 const ValueDecl *D, DSAStackTy::DSAVarData DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00001374 bool IsLoopIterVar = false) {
1375 if (DVar.RefExpr) {
1376 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1377 << getOpenMPClauseName(DVar.CKind);
1378 return;
1379 }
1380 enum {
1381 PDSA_StaticMemberShared,
1382 PDSA_StaticLocalVarShared,
1383 PDSA_LoopIterVarPrivate,
1384 PDSA_LoopIterVarLinear,
1385 PDSA_LoopIterVarLastprivate,
1386 PDSA_ConstVarShared,
1387 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001388 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001389 PDSA_LocalVarPrivate,
1390 PDSA_Implicit
1391 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001392 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001393 auto ReportLoc = D->getLocation();
1394 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001395 if (IsLoopIterVar) {
1396 if (DVar.CKind == OMPC_private)
1397 Reason = PDSA_LoopIterVarPrivate;
1398 else if (DVar.CKind == OMPC_lastprivate)
1399 Reason = PDSA_LoopIterVarLastprivate;
1400 else
1401 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev35aaee62016-04-13 13:36:48 +00001402 } else if (isOpenMPTaskingDirective(DVar.DKind) &&
1403 DVar.CKind == OMPC_firstprivate) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001404 Reason = PDSA_TaskVarFirstprivate;
1405 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001406 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001407 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001408 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001409 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001410 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001411 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001412 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00001413 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001414 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001415 ReportHint = true;
1416 Reason = PDSA_LocalVarPrivate;
1417 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001418 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001419 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001420 << Reason << ReportHint
1421 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1422 } else if (DVar.ImplicitDSALoc.isValid()) {
1423 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1424 << getOpenMPClauseName(DVar.CKind);
1425 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001426}
1427
Alexey Bataev758e55e2013-09-06 18:03:48 +00001428namespace {
1429class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1430 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001431 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001432 bool ErrorFound;
1433 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001434 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001435 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001436
Alexey Bataev758e55e2013-09-06 18:03:48 +00001437public:
1438 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00001439 if (E->isTypeDependent() || E->isValueDependent() ||
1440 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1441 return;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001442 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001443 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001444 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1445 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001446
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001447 auto DVar = Stack->getTopDSA(VD, false);
1448 // Check if the variable has explicit DSA set and stop analysis if it so.
1449 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001450
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001451 auto ELoc = E->getExprLoc();
1452 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001453 // The default(none) clause requires that each variable that is referenced
1454 // in the construct, and does not have a predetermined data-sharing
1455 // attribute, must have its data-sharing attribute explicitly determined
1456 // by being listed in a data-sharing attribute clause.
1457 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001458 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001459 VarsWithInheritedDSA.count(VD) == 0) {
1460 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001461 return;
1462 }
1463
1464 // OpenMP [2.9.3.6, Restrictions, p.2]
1465 // A list item that appears in a reduction clause of the innermost
1466 // enclosing worksharing or parallel construct may not be accessed in an
1467 // explicit task.
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001468 DVar = Stack->hasInnermostDSA(
1469 VD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
1470 [](OpenMPDirectiveKind K) -> bool {
1471 return isOpenMPParallelDirective(K) ||
1472 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
1473 },
1474 false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001475 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00001476 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001477 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1478 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001479 return;
1480 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001481
1482 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001483 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001484 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1485 !Stack->isLoopControlVariable(VD).first)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001486 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001487 }
1488 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001489 void VisitMemberExpr(MemberExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00001490 if (E->isTypeDependent() || E->isValueDependent() ||
1491 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1492 return;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001493 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1494 if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1495 auto DVar = Stack->getTopDSA(FD, false);
1496 // Check if the variable has explicit DSA set and stop analysis if it
1497 // so.
1498 if (DVar.RefExpr)
1499 return;
1500
1501 auto ELoc = E->getExprLoc();
1502 auto DKind = Stack->getCurrentDirective();
1503 // OpenMP [2.9.3.6, Restrictions, p.2]
1504 // A list item that appears in a reduction clause of the innermost
1505 // enclosing worksharing or parallel construct may not be accessed in
Alexey Bataevd985eda2016-02-10 11:29:16 +00001506 // an explicit task.
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001507 DVar = Stack->hasInnermostDSA(
1508 FD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
1509 [](OpenMPDirectiveKind K) -> bool {
1510 return isOpenMPParallelDirective(K) ||
1511 isOpenMPWorksharingDirective(K) ||
1512 isOpenMPTeamsDirective(K);
1513 },
1514 false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001515 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001516 ErrorFound = true;
1517 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1518 ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1519 return;
1520 }
1521
1522 // Define implicit data-sharing attributes for task.
1523 DVar = Stack->getImplicitDSA(FD, false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001524 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1525 !Stack->isLoopControlVariable(FD).first)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001526 ImplicitFirstprivate.push_back(E);
1527 }
1528 }
1529 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001530 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001531 for (auto *C : S->clauses()) {
1532 // Skip analysis of arguments of implicitly defined firstprivate clause
1533 // for task directives.
1534 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1535 for (auto *CC : C->children()) {
1536 if (CC)
1537 Visit(CC);
1538 }
1539 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001540 }
1541 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001542 for (auto *C : S->children()) {
1543 if (C && !isa<OMPExecutableDirective>(C))
1544 Visit(C);
1545 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001546 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001547
1548 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001549 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001550 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001551 return VarsWithInheritedDSA;
1552 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001553
Alexey Bataev7ff55242014-06-19 09:13:45 +00001554 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1555 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001556};
Alexey Bataeved09d242014-05-28 05:53:51 +00001557} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001558
Alexey Bataevbae9a792014-06-27 10:37:06 +00001559void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001560 switch (DKind) {
1561 case OMPD_parallel: {
1562 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001563 QualType KmpInt32PtrTy =
1564 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001565 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001566 std::make_pair(".global_tid.", KmpInt32PtrTy),
1567 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1568 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001569 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001570 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1571 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001572 break;
1573 }
1574 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001575 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001576 std::make_pair(StringRef(), QualType()) // __context with shared vars
1577 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001578 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1579 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001580 break;
1581 }
1582 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001583 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001584 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001585 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001586 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1587 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001588 break;
1589 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001590 case OMPD_for_simd: {
1591 Sema::CapturedParamNameType Params[] = {
1592 std::make_pair(StringRef(), QualType()) // __context with shared vars
1593 };
1594 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1595 Params);
1596 break;
1597 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001598 case OMPD_sections: {
1599 Sema::CapturedParamNameType Params[] = {
1600 std::make_pair(StringRef(), QualType()) // __context with shared vars
1601 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001602 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1603 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001604 break;
1605 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001606 case OMPD_section: {
1607 Sema::CapturedParamNameType Params[] = {
1608 std::make_pair(StringRef(), QualType()) // __context with shared vars
1609 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001610 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1611 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001612 break;
1613 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001614 case OMPD_single: {
1615 Sema::CapturedParamNameType Params[] = {
1616 std::make_pair(StringRef(), QualType()) // __context with shared vars
1617 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001618 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1619 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001620 break;
1621 }
Alexander Musman80c22892014-07-17 08:54:58 +00001622 case OMPD_master: {
1623 Sema::CapturedParamNameType Params[] = {
1624 std::make_pair(StringRef(), QualType()) // __context with shared vars
1625 };
1626 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1627 Params);
1628 break;
1629 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001630 case OMPD_critical: {
1631 Sema::CapturedParamNameType Params[] = {
1632 std::make_pair(StringRef(), QualType()) // __context with shared vars
1633 };
1634 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1635 Params);
1636 break;
1637 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001638 case OMPD_parallel_for: {
1639 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001640 QualType KmpInt32PtrTy =
1641 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001642 Sema::CapturedParamNameType Params[] = {
1643 std::make_pair(".global_tid.", KmpInt32PtrTy),
1644 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1645 std::make_pair(StringRef(), QualType()) // __context with shared vars
1646 };
1647 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1648 Params);
1649 break;
1650 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001651 case OMPD_parallel_for_simd: {
1652 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001653 QualType KmpInt32PtrTy =
1654 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001655 Sema::CapturedParamNameType Params[] = {
1656 std::make_pair(".global_tid.", KmpInt32PtrTy),
1657 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1658 std::make_pair(StringRef(), QualType()) // __context with shared vars
1659 };
1660 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1661 Params);
1662 break;
1663 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001664 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001665 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001666 QualType KmpInt32PtrTy =
1667 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001668 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001669 std::make_pair(".global_tid.", KmpInt32PtrTy),
1670 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001671 std::make_pair(StringRef(), QualType()) // __context with shared vars
1672 };
1673 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1674 Params);
1675 break;
1676 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001677 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001678 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001679 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1680 FunctionProtoType::ExtProtoInfo EPI;
1681 EPI.Variadic = true;
1682 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001683 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001684 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataev48591dd2016-04-20 04:01:36 +00001685 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1686 std::make_pair(".privates.", Context.VoidPtrTy.withConst()),
1687 std::make_pair(".copy_fn.",
1688 Context.getPointerType(CopyFnType).withConst()),
1689 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001690 std::make_pair(StringRef(), QualType()) // __context with shared vars
1691 };
1692 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1693 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001694 // Mark this captured region as inlined, because we don't use outlined
1695 // function directly.
1696 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1697 AlwaysInlineAttr::CreateImplicit(
1698 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001699 break;
1700 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001701 case OMPD_ordered: {
1702 Sema::CapturedParamNameType Params[] = {
1703 std::make_pair(StringRef(), QualType()) // __context with shared vars
1704 };
1705 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1706 Params);
1707 break;
1708 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001709 case OMPD_atomic: {
1710 Sema::CapturedParamNameType Params[] = {
1711 std::make_pair(StringRef(), QualType()) // __context with shared vars
1712 };
1713 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1714 Params);
1715 break;
1716 }
Michael Wong65f367f2015-07-21 13:44:28 +00001717 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001718 case OMPD_target:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001719 case OMPD_target_parallel:
1720 case OMPD_target_parallel_for: {
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001721 Sema::CapturedParamNameType Params[] = {
1722 std::make_pair(StringRef(), QualType()) // __context with shared vars
1723 };
1724 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1725 Params);
1726 break;
1727 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001728 case OMPD_teams: {
1729 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001730 QualType KmpInt32PtrTy =
1731 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001732 Sema::CapturedParamNameType Params[] = {
1733 std::make_pair(".global_tid.", KmpInt32PtrTy),
1734 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1735 std::make_pair(StringRef(), QualType()) // __context with shared vars
1736 };
1737 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1738 Params);
1739 break;
1740 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001741 case OMPD_taskgroup: {
1742 Sema::CapturedParamNameType Params[] = {
1743 std::make_pair(StringRef(), QualType()) // __context with shared vars
1744 };
1745 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1746 Params);
1747 break;
1748 }
Alexey Bataev1e73ef32016-04-28 12:14:51 +00001749 case OMPD_taskloop:
1750 case OMPD_taskloop_simd: {
Alexey Bataev7292c292016-04-25 12:22:29 +00001751 QualType KmpInt32Ty =
1752 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
1753 QualType KmpUInt64Ty =
1754 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
1755 QualType KmpInt64Ty =
1756 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
1757 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1758 FunctionProtoType::ExtProtoInfo EPI;
1759 EPI.Variadic = true;
1760 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev49f6e782015-12-01 04:18:41 +00001761 Sema::CapturedParamNameType Params[] = {
Alexey Bataev7292c292016-04-25 12:22:29 +00001762 std::make_pair(".global_tid.", KmpInt32Ty),
1763 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1764 std::make_pair(".privates.",
1765 Context.VoidPtrTy.withConst().withRestrict()),
1766 std::make_pair(
1767 ".copy_fn.",
1768 Context.getPointerType(CopyFnType).withConst().withRestrict()),
1769 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
1770 std::make_pair(".lb.", KmpUInt64Ty),
1771 std::make_pair(".ub.", KmpUInt64Ty), std::make_pair(".st.", KmpInt64Ty),
1772 std::make_pair(".liter.", KmpInt32Ty),
Alexey Bataev49f6e782015-12-01 04:18:41 +00001773 std::make_pair(StringRef(), QualType()) // __context with shared vars
1774 };
1775 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1776 Params);
Alexey Bataev7292c292016-04-25 12:22:29 +00001777 // Mark this captured region as inlined, because we don't use outlined
1778 // function directly.
1779 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1780 AlwaysInlineAttr::CreateImplicit(
1781 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev49f6e782015-12-01 04:18:41 +00001782 break;
1783 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001784 case OMPD_distribute: {
1785 Sema::CapturedParamNameType Params[] = {
1786 std::make_pair(StringRef(), QualType()) // __context with shared vars
1787 };
1788 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1789 Params);
1790 break;
1791 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001792 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001793 case OMPD_taskyield:
1794 case OMPD_barrier:
1795 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001796 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001797 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001798 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001799 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001800 case OMPD_target_exit_data:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001801 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001802 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001803 case OMPD_declare_target:
1804 case OMPD_end_declare_target:
Alexey Bataev9959db52014-05-06 10:08:46 +00001805 llvm_unreachable("OpenMP Directive is not allowed");
1806 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001807 llvm_unreachable("Unknown OpenMP directive");
1808 }
1809}
1810
Alexey Bataev3392d762016-02-16 11:18:12 +00001811static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00001812 Expr *CaptureExpr, bool WithInit,
1813 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001814 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00001815 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00001816 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00001817 QualType Ty = Init->getType();
1818 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1819 if (S.getLangOpts().CPlusPlus)
1820 Ty = C.getLValueReferenceType(Ty);
1821 else {
1822 Ty = C.getPointerType(Ty);
1823 ExprResult Res =
1824 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1825 if (!Res.isUsable())
1826 return nullptr;
1827 Init = Res.get();
1828 }
Alexey Bataev61205072016-03-02 04:57:40 +00001829 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00001830 }
1831 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001832 if (!WithInit)
1833 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
Alexey Bataev4244be22016-02-11 05:35:55 +00001834 S.CurContext->addHiddenDecl(CED);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001835 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false,
1836 /*TypeMayContainAuto=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00001837 return CED;
1838}
1839
Alexey Bataev61205072016-03-02 04:57:40 +00001840static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
1841 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00001842 OMPCapturedExprDecl *CD;
1843 if (auto *VD = S.IsOpenMPCapturedDecl(D))
1844 CD = cast<OMPCapturedExprDecl>(VD);
1845 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00001846 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
1847 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001848 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00001849 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00001850}
1851
Alexey Bataev5a3af132016-03-29 08:58:54 +00001852static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
1853 if (!Ref) {
1854 auto *CD =
1855 buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."),
1856 CaptureExpr, /*WithInit=*/true, /*AsExpression=*/true);
1857 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1858 CaptureExpr->getExprLoc());
1859 }
1860 ExprResult Res = Ref;
1861 if (!S.getLangOpts().CPlusPlus &&
1862 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
1863 Ref->getType()->isPointerType())
1864 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
1865 if (!Res.isUsable())
1866 return ExprError();
1867 return CaptureExpr->isGLValue() ? Res : S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00001868}
1869
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001870StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1871 ArrayRef<OMPClause *> Clauses) {
1872 if (!S.isUsable()) {
1873 ActOnCapturedRegionError();
1874 return StmtError();
1875 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001876
1877 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001878 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001879 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001880 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001881 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001882 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001883 Clause->getClauseKind() == OMPC_copyprivate ||
1884 (getLangOpts().OpenMPUseTLS &&
1885 getASTContext().getTargetInfo().isTLSSupported() &&
1886 Clause->getClauseKind() == OMPC_copyin)) {
1887 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001888 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001889 for (auto *VarRef : Clause->children()) {
1890 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001891 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001892 }
1893 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001894 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001895 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev040d5402015-05-12 08:35:28 +00001896 // Mark all variables in private list clauses as used in inner region.
1897 // Required for proper codegen of combined directives.
1898 // TODO: add processing for other clauses.
Alexey Bataev3392d762016-02-16 11:18:12 +00001899 if (auto *C = OMPClauseWithPreInit::get(Clause)) {
Alexey Bataev005248a2016-02-25 05:25:57 +00001900 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
1901 for (auto *D : DS->decls())
Alexey Bataev3392d762016-02-16 11:18:12 +00001902 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
1903 }
Alexey Bataev4244be22016-02-11 05:35:55 +00001904 }
Alexey Bataev005248a2016-02-25 05:25:57 +00001905 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
1906 if (auto *E = C->getPostUpdateExpr())
1907 MarkDeclarationsReferencedInExpr(E);
1908 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001909 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001910 if (Clause->getClauseKind() == OMPC_schedule)
1911 SC = cast<OMPScheduleClause>(Clause);
1912 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001913 OC = cast<OMPOrderedClause>(Clause);
1914 else if (Clause->getClauseKind() == OMPC_linear)
1915 LCs.push_back(cast<OMPLinearClause>(Clause));
1916 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001917 bool ErrorFound = false;
1918 // OpenMP, 2.7.1 Loop Construct, Restrictions
1919 // The nonmonotonic modifier cannot be specified if an ordered clause is
1920 // specified.
1921 if (SC &&
1922 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1923 SC->getSecondScheduleModifier() ==
1924 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1925 OC) {
1926 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1927 ? SC->getFirstScheduleModifierLoc()
1928 : SC->getSecondScheduleModifierLoc(),
1929 diag::err_omp_schedule_nonmonotonic_ordered)
1930 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1931 ErrorFound = true;
1932 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001933 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1934 for (auto *C : LCs) {
1935 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1936 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1937 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001938 ErrorFound = true;
1939 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001940 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1941 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1942 OC->getNumForLoops()) {
1943 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1944 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1945 ErrorFound = true;
1946 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001947 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001948 ActOnCapturedRegionError();
1949 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001950 }
1951 return ActOnCapturedRegionEnd(S.get());
1952}
1953
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001954static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1955 OpenMPDirectiveKind CurrentRegion,
1956 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001957 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001958 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001959 // Allowed nesting of constructs
1960 // +------------------+-----------------+------------------------------------+
1961 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1962 // +------------------+-----------------+------------------------------------+
1963 // | parallel | parallel | * |
1964 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001965 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001966 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001967 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001968 // | parallel | simd | * |
1969 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001970 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001971 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001972 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001973 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001974 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001975 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001976 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001977 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001978 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001979 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001980 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001981 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001982 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001983 // | parallel | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001984 // | parallel | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001985 // | parallel | target parallel | * |
1986 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001987 // | parallel | target enter | * |
1988 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001989 // | parallel | target exit | * |
1990 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001991 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001992 // | parallel | cancellation | |
1993 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001994 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001995 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001996 // | parallel | taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001997 // | parallel | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001998 // +------------------+-----------------+------------------------------------+
1999 // | for | parallel | * |
2000 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002001 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002002 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002003 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002004 // | for | simd | * |
2005 // | for | sections | + |
2006 // | for | section | + |
2007 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002008 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002009 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002010 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002011 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002012 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002013 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002014 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002015 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002016 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002017 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002018 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002019 // | for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002020 // | for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002021 // | for | target parallel | * |
2022 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002023 // | for | target enter | * |
2024 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002025 // | for | target exit | * |
2026 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002027 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002028 // | for | cancellation | |
2029 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002030 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002031 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002032 // | for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002033 // | for | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002034 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00002035 // | master | parallel | * |
2036 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002037 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002038 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002039 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00002040 // | master | simd | * |
2041 // | master | sections | + |
2042 // | master | section | + |
2043 // | master | single | + |
2044 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002045 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00002046 // | master |parallel sections| * |
2047 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002048 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002049 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002050 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002051 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002052 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002053 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002054 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002055 // | master | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002056 // | master | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002057 // | master | target parallel | * |
2058 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002059 // | master | target enter | * |
2060 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002061 // | master | target exit | * |
2062 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002063 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002064 // | master | cancellation | |
2065 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002066 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002067 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002068 // | master | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002069 // | master | distribute | |
Alexander Musman80c22892014-07-17 08:54:58 +00002070 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002071 // | critical | parallel | * |
2072 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002073 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002074 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002075 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002076 // | critical | simd | * |
2077 // | critical | sections | + |
2078 // | critical | section | + |
2079 // | critical | single | + |
2080 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002081 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002082 // | critical |parallel sections| * |
2083 // | critical | task | * |
2084 // | critical | taskyield | * |
2085 // | critical | barrier | + |
2086 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002087 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002088 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002089 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002090 // | critical | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002091 // | critical | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002092 // | critical | target parallel | * |
2093 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002094 // | critical | target enter | * |
2095 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002096 // | critical | target exit | * |
2097 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002098 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002099 // | critical | cancellation | |
2100 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002101 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002102 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002103 // | critical | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002104 // | critical | distribute | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002105 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002106 // | simd | parallel | |
2107 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002108 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00002109 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002110 // | simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002111 // | simd | simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002112 // | simd | sections | |
2113 // | simd | section | |
2114 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002115 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002116 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002117 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002118 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00002119 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002120 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002121 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002122 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00002123 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002124 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002125 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002126 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002127 // | simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002128 // | simd | target parallel | |
2129 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002130 // | simd | target enter | |
2131 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002132 // | simd | target exit | |
2133 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002134 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002135 // | simd | cancellation | |
2136 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002137 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002138 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002139 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002140 // | simd | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002141 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00002142 // | for simd | parallel | |
2143 // | for simd | for | |
2144 // | for simd | for simd | |
2145 // | for simd | master | |
2146 // | for simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002147 // | for simd | simd | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002148 // | for simd | sections | |
2149 // | for simd | section | |
2150 // | for simd | single | |
2151 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002152 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002153 // | for simd |parallel sections| |
2154 // | for simd | task | |
2155 // | for simd | taskyield | |
2156 // | for simd | barrier | |
2157 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002158 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002159 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002160 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002161 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002162 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002163 // | for simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002164 // | for simd | target parallel | |
2165 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002166 // | for simd | target enter | |
2167 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002168 // | for simd | target exit | |
2169 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002170 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002171 // | for simd | cancellation | |
2172 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002173 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002174 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002175 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002176 // | for simd | distribute | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002177 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00002178 // | parallel for simd| parallel | |
2179 // | parallel for simd| for | |
2180 // | parallel for simd| for simd | |
2181 // | parallel for simd| master | |
2182 // | parallel for simd| critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002183 // | parallel for simd| simd | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002184 // | parallel for simd| sections | |
2185 // | parallel for simd| section | |
2186 // | parallel for simd| single | |
2187 // | parallel for simd| parallel for | |
2188 // | parallel for simd|parallel for simd| |
2189 // | parallel for simd|parallel sections| |
2190 // | parallel for simd| task | |
2191 // | parallel for simd| taskyield | |
2192 // | parallel for simd| barrier | |
2193 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002194 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002195 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002196 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002197 // | parallel for simd| atomic | |
2198 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002199 // | parallel for simd| target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002200 // | parallel for simd| target parallel | |
2201 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002202 // | parallel for simd| target enter | |
2203 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002204 // | parallel for simd| target exit | |
2205 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002206 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002207 // | parallel for simd| cancellation | |
2208 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002209 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002210 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002211 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002212 // | parallel for simd| distribute | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002213 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002214 // | sections | parallel | * |
2215 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002216 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002217 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002218 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002219 // | sections | simd | * |
2220 // | sections | sections | + |
2221 // | sections | section | * |
2222 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002223 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002224 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002225 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002226 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002227 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002228 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002229 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002230 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002231 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002232 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002233 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002234 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002235 // | sections | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002236 // | sections | target parallel | * |
2237 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002238 // | sections | target enter | * |
2239 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002240 // | sections | target exit | * |
2241 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002242 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002243 // | sections | cancellation | |
2244 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002245 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002246 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002247 // | sections | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002248 // | sections | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002249 // +------------------+-----------------+------------------------------------+
2250 // | section | parallel | * |
2251 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002252 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002253 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002254 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002255 // | section | simd | * |
2256 // | section | sections | + |
2257 // | section | section | + |
2258 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002259 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002260 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002261 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002262 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002263 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002264 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002265 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002266 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002267 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002268 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002269 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002270 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002271 // | section | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002272 // | section | target parallel | * |
2273 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002274 // | section | target enter | * |
2275 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002276 // | section | target exit | * |
2277 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002278 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002279 // | section | cancellation | |
2280 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002281 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002282 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002283 // | section | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002284 // | section | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002285 // +------------------+-----------------+------------------------------------+
2286 // | single | parallel | * |
2287 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002288 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002289 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002290 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002291 // | single | simd | * |
2292 // | single | sections | + |
2293 // | single | section | + |
2294 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002295 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002296 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002297 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002298 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002299 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002300 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002301 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002302 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002303 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002304 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002305 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002306 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002307 // | single | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002308 // | single | target parallel | * |
2309 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002310 // | single | target enter | * |
2311 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002312 // | single | target exit | * |
2313 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002314 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002315 // | single | cancellation | |
2316 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002317 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002318 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002319 // | single | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002320 // | single | distribute | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002321 // +------------------+-----------------+------------------------------------+
2322 // | parallel for | parallel | * |
2323 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002324 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002325 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002326 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002327 // | parallel for | simd | * |
2328 // | parallel for | sections | + |
2329 // | parallel for | section | + |
2330 // | parallel for | single | + |
2331 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002332 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002333 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002334 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002335 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002336 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002337 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002338 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002339 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002340 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002341 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002342 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002343 // | parallel for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002344 // | parallel for | target parallel | * |
2345 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002346 // | parallel for | target enter | * |
2347 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002348 // | parallel for | target exit | * |
2349 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002350 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002351 // | parallel for | cancellation | |
2352 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002353 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002354 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002355 // | parallel for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002356 // | parallel for | distribute | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002357 // +------------------+-----------------+------------------------------------+
2358 // | parallel sections| parallel | * |
2359 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002360 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002361 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002362 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002363 // | parallel sections| simd | * |
2364 // | parallel sections| sections | + |
2365 // | parallel sections| section | * |
2366 // | parallel sections| single | + |
2367 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002368 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002369 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002370 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002371 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002372 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002373 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002374 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002375 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002376 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002377 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002378 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002379 // | parallel sections| target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002380 // | parallel sections| target parallel | * |
2381 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002382 // | parallel sections| target enter | * |
2383 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002384 // | parallel sections| target exit | * |
2385 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002386 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002387 // | parallel sections| cancellation | |
2388 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002389 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002390 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002391 // | parallel sections| taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002392 // | parallel sections| distribute | |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002393 // +------------------+-----------------+------------------------------------+
2394 // | task | parallel | * |
2395 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002396 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002397 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002398 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002399 // | task | simd | * |
2400 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002401 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002402 // | task | single | + |
2403 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002404 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002405 // | task |parallel sections| * |
2406 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002407 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002408 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002409 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002410 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002411 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002412 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002413 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002414 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002415 // | task | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002416 // | task | target parallel | * |
2417 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002418 // | task | target enter | * |
2419 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002420 // | task | target exit | * |
2421 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002422 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002423 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002424 // | | point | ! |
2425 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002426 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002427 // | task | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002428 // | task | distribute | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002429 // +------------------+-----------------+------------------------------------+
2430 // | ordered | parallel | * |
2431 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002432 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002433 // | ordered | master | * |
2434 // | ordered | critical | * |
2435 // | ordered | simd | * |
2436 // | ordered | sections | + |
2437 // | ordered | section | + |
2438 // | ordered | single | + |
2439 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002440 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002441 // | ordered |parallel sections| * |
2442 // | ordered | task | * |
2443 // | ordered | taskyield | * |
2444 // | ordered | barrier | + |
2445 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002446 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002447 // | ordered | flush | * |
2448 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002449 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002450 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002451 // | ordered | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002452 // | ordered | target parallel | * |
2453 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002454 // | ordered | target enter | * |
2455 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002456 // | ordered | target exit | * |
2457 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002458 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002459 // | ordered | cancellation | |
2460 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002461 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002462 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002463 // | ordered | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002464 // | ordered | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002465 // +------------------+-----------------+------------------------------------+
2466 // | atomic | parallel | |
2467 // | atomic | for | |
2468 // | atomic | for simd | |
2469 // | atomic | master | |
2470 // | atomic | critical | |
2471 // | atomic | simd | |
2472 // | atomic | sections | |
2473 // | atomic | section | |
2474 // | atomic | single | |
2475 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002476 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002477 // | atomic |parallel sections| |
2478 // | atomic | task | |
2479 // | atomic | taskyield | |
2480 // | atomic | barrier | |
2481 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002482 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002483 // | atomic | flush | |
2484 // | atomic | ordered | |
2485 // | atomic | atomic | |
2486 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002487 // | atomic | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002488 // | atomic | target parallel | |
2489 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002490 // | atomic | target enter | |
2491 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002492 // | atomic | target exit | |
2493 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002494 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002495 // | atomic | cancellation | |
2496 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002497 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002498 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002499 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002500 // | atomic | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002501 // +------------------+-----------------+------------------------------------+
2502 // | target | parallel | * |
2503 // | target | for | * |
2504 // | target | for simd | * |
2505 // | target | master | * |
2506 // | target | critical | * |
2507 // | target | simd | * |
2508 // | target | sections | * |
2509 // | target | section | * |
2510 // | target | single | * |
2511 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002512 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002513 // | target |parallel sections| * |
2514 // | target | task | * |
2515 // | target | taskyield | * |
2516 // | target | barrier | * |
2517 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002518 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002519 // | target | flush | * |
2520 // | target | ordered | * |
2521 // | target | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002522 // | target | target | |
2523 // | target | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002524 // | target | target parallel | |
2525 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002526 // | target | target enter | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002527 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002528 // | target | target exit | |
Samuel Antao72590762016-01-19 20:04:50 +00002529 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002530 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002531 // | target | cancellation | |
2532 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002533 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002534 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002535 // | target | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002536 // | target | distribute | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002537 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002538 // | target parallel | parallel | * |
2539 // | target parallel | for | * |
2540 // | target parallel | for simd | * |
2541 // | target parallel | master | * |
2542 // | target parallel | critical | * |
2543 // | target parallel | simd | * |
2544 // | target parallel | sections | * |
2545 // | target parallel | section | * |
2546 // | target parallel | single | * |
2547 // | target parallel | parallel for | * |
2548 // | target parallel |parallel for simd| * |
2549 // | target parallel |parallel sections| * |
2550 // | target parallel | task | * |
2551 // | target parallel | taskyield | * |
2552 // | target parallel | barrier | * |
2553 // | target parallel | taskwait | * |
2554 // | target parallel | taskgroup | * |
2555 // | target parallel | flush | * |
2556 // | target parallel | ordered | * |
2557 // | target parallel | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002558 // | target parallel | target | |
2559 // | target parallel | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002560 // | target parallel | target parallel | |
2561 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002562 // | target parallel | target enter | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002563 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002564 // | target parallel | target exit | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002565 // | | data | |
2566 // | target parallel | teams | |
2567 // | target parallel | cancellation | |
2568 // | | point | ! |
2569 // | target parallel | cancel | ! |
2570 // | target parallel | taskloop | * |
2571 // | target parallel | taskloop simd | * |
2572 // | target parallel | distribute | |
2573 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002574 // | target parallel | parallel | * |
2575 // | for | | |
2576 // | target parallel | for | * |
2577 // | for | | |
2578 // | target parallel | for simd | * |
2579 // | for | | |
2580 // | target parallel | master | * |
2581 // | for | | |
2582 // | target parallel | critical | * |
2583 // | for | | |
2584 // | target parallel | simd | * |
2585 // | for | | |
2586 // | target parallel | sections | * |
2587 // | for | | |
2588 // | target parallel | section | * |
2589 // | for | | |
2590 // | target parallel | single | * |
2591 // | for | | |
2592 // | target parallel | parallel for | * |
2593 // | for | | |
2594 // | target parallel |parallel for simd| * |
2595 // | for | | |
2596 // | target parallel |parallel sections| * |
2597 // | for | | |
2598 // | target parallel | task | * |
2599 // | for | | |
2600 // | target parallel | taskyield | * |
2601 // | for | | |
2602 // | target parallel | barrier | * |
2603 // | for | | |
2604 // | target parallel | taskwait | * |
2605 // | for | | |
2606 // | target parallel | taskgroup | * |
2607 // | for | | |
2608 // | target parallel | flush | * |
2609 // | for | | |
2610 // | target parallel | ordered | * |
2611 // | for | | |
2612 // | target parallel | atomic | * |
2613 // | for | | |
2614 // | target parallel | target | |
2615 // | for | | |
2616 // | target parallel | target parallel | |
2617 // | for | | |
2618 // | target parallel | target parallel | |
2619 // | for | for | |
2620 // | target parallel | target enter | |
2621 // | for | data | |
2622 // | target parallel | target exit | |
2623 // | for | data | |
2624 // | target parallel | teams | |
2625 // | for | | |
2626 // | target parallel | cancellation | |
2627 // | for | point | ! |
2628 // | target parallel | cancel | ! |
2629 // | for | | |
2630 // | target parallel | taskloop | * |
2631 // | for | | |
2632 // | target parallel | taskloop simd | * |
2633 // | for | | |
2634 // | target parallel | distribute | |
2635 // | for | | |
2636 // +------------------+-----------------+------------------------------------+
Alexey Bataev13314bf2014-10-09 04:18:56 +00002637 // | teams | parallel | * |
2638 // | teams | for | + |
2639 // | teams | for simd | + |
2640 // | teams | master | + |
2641 // | teams | critical | + |
2642 // | teams | simd | + |
2643 // | teams | sections | + |
2644 // | teams | section | + |
2645 // | teams | single | + |
2646 // | teams | parallel for | * |
2647 // | teams |parallel for simd| * |
2648 // | teams |parallel sections| * |
2649 // | teams | task | + |
2650 // | teams | taskyield | + |
2651 // | teams | barrier | + |
2652 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002653 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002654 // | teams | flush | + |
2655 // | teams | ordered | + |
2656 // | teams | atomic | + |
2657 // | teams | target | + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002658 // | teams | target parallel | + |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002659 // | teams | target parallel | + |
2660 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002661 // | teams | target enter | + |
2662 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002663 // | teams | target exit | + |
2664 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002665 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002666 // | teams | cancellation | |
2667 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002668 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002669 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002670 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002671 // | teams | distribute | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002672 // +------------------+-----------------+------------------------------------+
2673 // | taskloop | parallel | * |
2674 // | taskloop | for | + |
2675 // | taskloop | for simd | + |
2676 // | taskloop | master | + |
2677 // | taskloop | critical | * |
2678 // | taskloop | simd | * |
2679 // | taskloop | sections | + |
2680 // | taskloop | section | + |
2681 // | taskloop | single | + |
2682 // | taskloop | parallel for | * |
2683 // | taskloop |parallel for simd| * |
2684 // | taskloop |parallel sections| * |
2685 // | taskloop | task | * |
2686 // | taskloop | taskyield | * |
2687 // | taskloop | barrier | + |
2688 // | taskloop | taskwait | * |
2689 // | taskloop | taskgroup | * |
2690 // | taskloop | flush | * |
2691 // | taskloop | ordered | + |
2692 // | taskloop | atomic | * |
2693 // | taskloop | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002694 // | taskloop | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002695 // | taskloop | target parallel | * |
2696 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002697 // | taskloop | target enter | * |
2698 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002699 // | taskloop | target exit | * |
2700 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002701 // | taskloop | teams | + |
2702 // | taskloop | cancellation | |
2703 // | | point | |
2704 // | taskloop | cancel | |
2705 // | taskloop | taskloop | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002706 // | taskloop | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002707 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002708 // | taskloop simd | parallel | |
2709 // | taskloop simd | for | |
2710 // | taskloop simd | for simd | |
2711 // | taskloop simd | master | |
2712 // | taskloop simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002713 // | taskloop simd | simd | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002714 // | taskloop simd | sections | |
2715 // | taskloop simd | section | |
2716 // | taskloop simd | single | |
2717 // | taskloop simd | parallel for | |
2718 // | taskloop simd |parallel for simd| |
2719 // | taskloop simd |parallel sections| |
2720 // | taskloop simd | task | |
2721 // | taskloop simd | taskyield | |
2722 // | taskloop simd | barrier | |
2723 // | taskloop simd | taskwait | |
2724 // | taskloop simd | taskgroup | |
2725 // | taskloop simd | flush | |
2726 // | taskloop simd | ordered | + (with simd clause) |
2727 // | taskloop simd | atomic | |
2728 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002729 // | taskloop simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002730 // | taskloop simd | target parallel | |
2731 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002732 // | taskloop simd | target enter | |
2733 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002734 // | taskloop simd | target exit | |
2735 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002736 // | taskloop simd | teams | |
2737 // | taskloop simd | cancellation | |
2738 // | | point | |
2739 // | taskloop simd | cancel | |
2740 // | taskloop simd | taskloop | |
2741 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002742 // | taskloop simd | distribute | |
2743 // +------------------+-----------------+------------------------------------+
2744 // | distribute | parallel | * |
2745 // | distribute | for | * |
2746 // | distribute | for simd | * |
2747 // | distribute | master | * |
2748 // | distribute | critical | * |
2749 // | distribute | simd | * |
2750 // | distribute | sections | * |
2751 // | distribute | section | * |
2752 // | distribute | single | * |
2753 // | distribute | parallel for | * |
2754 // | distribute |parallel for simd| * |
2755 // | distribute |parallel sections| * |
2756 // | distribute | task | * |
2757 // | distribute | taskyield | * |
2758 // | distribute | barrier | * |
2759 // | distribute | taskwait | * |
2760 // | distribute | taskgroup | * |
2761 // | distribute | flush | * |
2762 // | distribute | ordered | + |
2763 // | distribute | atomic | * |
2764 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002765 // | distribute | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002766 // | distribute | target parallel | |
2767 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002768 // | distribute | target enter | |
2769 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002770 // | distribute | target exit | |
2771 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002772 // | distribute | teams | |
2773 // | distribute | cancellation | + |
2774 // | | point | |
2775 // | distribute | cancel | + |
2776 // | distribute | taskloop | * |
2777 // | distribute | taskloop simd | * |
2778 // | distribute | distribute | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002779 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002780 if (Stack->getCurScope()) {
2781 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002782 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002783 bool NestingProhibited = false;
2784 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002785 enum {
2786 NoRecommend,
2787 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002788 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002789 ShouldBeInTargetRegion,
2790 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002791 } Recommend = NoRecommend;
Alexey Bataev1f092212016-02-02 04:59:52 +00002792 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered &&
2793 CurrentRegion != OMPD_simd) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002794 // OpenMP [2.16, Nesting of Regions]
2795 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002796 // OpenMP [2.8.1,simd Construct, Restrictions]
2797 // An ordered construct with the simd clause is the only OpenMP construct
2798 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002799 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2800 return true;
2801 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002802 if (ParentRegion == OMPD_atomic) {
2803 // OpenMP [2.16, Nesting of Regions]
2804 // OpenMP constructs may not be nested inside an atomic region.
2805 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2806 return true;
2807 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002808 if (CurrentRegion == OMPD_section) {
2809 // OpenMP [2.7.2, sections Construct, Restrictions]
2810 // Orphaned section directives are prohibited. That is, the section
2811 // directives must appear within the sections construct and must not be
2812 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002813 if (ParentRegion != OMPD_sections &&
2814 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002815 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2816 << (ParentRegion != OMPD_unknown)
2817 << getOpenMPDirectiveName(ParentRegion);
2818 return true;
2819 }
2820 return false;
2821 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002822 // Allow some constructs to be orphaned (they could be used in functions,
2823 // called from OpenMP regions with the required preconditions).
2824 if (ParentRegion == OMPD_unknown)
2825 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002826 if (CurrentRegion == OMPD_cancellation_point ||
2827 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002828 // OpenMP [2.16, Nesting of Regions]
2829 // A cancellation point construct for which construct-type-clause is
2830 // taskgroup must be nested inside a task construct. A cancellation
2831 // point construct for which construct-type-clause is not taskgroup must
2832 // be closely nested inside an OpenMP construct that matches the type
2833 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002834 // A cancel construct for which construct-type-clause is taskgroup must be
2835 // nested inside a task construct. A cancel construct for which
2836 // construct-type-clause is not taskgroup must be closely nested inside an
2837 // OpenMP construct that matches the type specified in
2838 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002839 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002840 !((CancelRegion == OMPD_parallel &&
2841 (ParentRegion == OMPD_parallel ||
2842 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002843 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002844 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
2845 ParentRegion == OMPD_target_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002846 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2847 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002848 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2849 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002850 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002851 // OpenMP [2.16, Nesting of Regions]
2852 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002853 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002854 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00002855 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002856 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2857 // OpenMP [2.16, Nesting of Regions]
2858 // A critical region may not be nested (closely or otherwise) inside a
2859 // critical region with the same name. Note that this restriction is not
2860 // sufficient to prevent deadlock.
2861 SourceLocation PreviousCriticalLoc;
2862 bool DeadLock =
2863 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2864 OpenMPDirectiveKind K,
2865 const DeclarationNameInfo &DNI,
2866 SourceLocation Loc)
2867 ->bool {
2868 if (K == OMPD_critical &&
2869 DNI.getName() == CurrentName.getName()) {
2870 PreviousCriticalLoc = Loc;
2871 return true;
2872 } else
2873 return false;
2874 },
2875 false /* skip top directive */);
2876 if (DeadLock) {
2877 SemaRef.Diag(StartLoc,
2878 diag::err_omp_prohibited_region_critical_same_name)
2879 << CurrentName.getName();
2880 if (PreviousCriticalLoc.isValid())
2881 SemaRef.Diag(PreviousCriticalLoc,
2882 diag::note_omp_previous_critical_region);
2883 return true;
2884 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002885 } else if (CurrentRegion == OMPD_barrier) {
2886 // OpenMP [2.16, Nesting of Regions]
2887 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002888 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00002889 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2890 isOpenMPTaskingDirective(ParentRegion) ||
2891 ParentRegion == OMPD_master ||
2892 ParentRegion == OMPD_critical ||
2893 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00002894 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002895 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002896 // OpenMP [2.16, Nesting of Regions]
2897 // A worksharing region may not be closely nested inside a worksharing,
2898 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00002899 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2900 isOpenMPTaskingDirective(ParentRegion) ||
2901 ParentRegion == OMPD_master ||
2902 ParentRegion == OMPD_critical ||
2903 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002904 Recommend = ShouldBeInParallelRegion;
2905 } else if (CurrentRegion == OMPD_ordered) {
2906 // OpenMP [2.16, Nesting of Regions]
2907 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002908 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002909 // An ordered region must be closely nested inside a loop region (or
2910 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002911 // OpenMP [2.8.1,simd Construct, Restrictions]
2912 // An ordered construct with the simd clause is the only OpenMP construct
2913 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002914 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00002915 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002916 !(isOpenMPSimdDirective(ParentRegion) ||
2917 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002918 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002919 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2920 // OpenMP [2.16, Nesting of Regions]
2921 // If specified, a teams construct must be contained within a target
2922 // construct.
2923 NestingProhibited = ParentRegion != OMPD_target;
2924 Recommend = ShouldBeInTargetRegion;
2925 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2926 }
2927 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2928 // OpenMP [2.16, Nesting of Regions]
2929 // distribute, parallel, parallel sections, parallel workshare, and the
2930 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2931 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002932 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2933 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002934 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002935 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002936 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2937 // OpenMP 4.5 [2.17 Nesting of Regions]
2938 // The region associated with the distribute construct must be strictly
2939 // nested inside a teams region
2940 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2941 Recommend = ShouldBeInTeamsRegion;
2942 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002943 if (!NestingProhibited &&
2944 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2945 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2946 // OpenMP 4.5 [2.17 Nesting of Regions]
2947 // If a target, target update, target data, target enter data, or
2948 // target exit data construct is encountered during execution of a
2949 // target region, the behavior is unspecified.
2950 NestingProhibited = Stack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +00002951 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
2952 SourceLocation) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002953 if (isOpenMPTargetExecutionDirective(K)) {
2954 OffendingRegion = K;
2955 return true;
2956 } else
2957 return false;
2958 },
2959 false /* don't skip top directive */);
2960 CloseNesting = false;
2961 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002962 if (NestingProhibited) {
2963 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002964 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2965 << Recommend << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002966 return true;
2967 }
2968 }
2969 return false;
2970}
2971
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002972static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2973 ArrayRef<OMPClause *> Clauses,
2974 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2975 bool ErrorFound = false;
2976 unsigned NamedModifiersNumber = 0;
2977 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2978 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002979 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002980 for (const auto *C : Clauses) {
2981 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2982 // At most one if clause without a directive-name-modifier can appear on
2983 // the directive.
2984 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2985 if (FoundNameModifiers[CurNM]) {
2986 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2987 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2988 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2989 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002990 } else if (CurNM != OMPD_unknown) {
2991 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002992 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002993 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002994 FoundNameModifiers[CurNM] = IC;
2995 if (CurNM == OMPD_unknown)
2996 continue;
2997 // Check if the specified name modifier is allowed for the current
2998 // directive.
2999 // At most one if clause with the particular directive-name-modifier can
3000 // appear on the directive.
3001 bool MatchFound = false;
3002 for (auto NM : AllowedNameModifiers) {
3003 if (CurNM == NM) {
3004 MatchFound = true;
3005 break;
3006 }
3007 }
3008 if (!MatchFound) {
3009 S.Diag(IC->getNameModifierLoc(),
3010 diag::err_omp_wrong_if_directive_name_modifier)
3011 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
3012 ErrorFound = true;
3013 }
3014 }
3015 }
3016 // If any if clause on the directive includes a directive-name-modifier then
3017 // all if clauses on the directive must include a directive-name-modifier.
3018 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3019 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
3020 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
3021 diag::err_omp_no_more_if_clause);
3022 } else {
3023 std::string Values;
3024 std::string Sep(", ");
3025 unsigned AllowedCnt = 0;
3026 unsigned TotalAllowedNum =
3027 AllowedNameModifiers.size() - NamedModifiersNumber;
3028 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3029 ++Cnt) {
3030 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3031 if (!FoundNameModifiers[NM]) {
3032 Values += "'";
3033 Values += getOpenMPDirectiveName(NM);
3034 Values += "'";
3035 if (AllowedCnt + 2 == TotalAllowedNum)
3036 Values += " or ";
3037 else if (AllowedCnt + 1 != TotalAllowedNum)
3038 Values += Sep;
3039 ++AllowedCnt;
3040 }
3041 }
3042 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
3043 diag::err_omp_unnamed_if_clause)
3044 << (TotalAllowedNum > 1) << Values;
3045 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00003046 for (auto Loc : NameModifierLoc) {
3047 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3048 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003049 ErrorFound = true;
3050 }
3051 return ErrorFound;
3052}
3053
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003054StmtResult Sema::ActOnOpenMPExecutableDirective(
3055 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3056 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3057 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003058 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003059 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
3060 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00003061 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003062
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003063 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003064 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003065 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00003066 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00003067 if (AStmt) {
3068 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3069
3070 // Check default data sharing attributes for referenced variables.
3071 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
3072 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
3073 if (DSAChecker.isErrorFound())
3074 return StmtError();
3075 // Generate list of implicitly defined firstprivate variables.
3076 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00003077
3078 if (!DSAChecker.getImplicitFirstprivate().empty()) {
3079 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
3080 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
3081 SourceLocation(), SourceLocation())) {
3082 ClausesWithImplicit.push_back(Implicit);
3083 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
3084 DSAChecker.getImplicitFirstprivate().size();
3085 } else
3086 ErrorFound = true;
3087 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003088 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003089
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003090 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003091 switch (Kind) {
3092 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00003093 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3094 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003095 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003096 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003097 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003098 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3099 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003100 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003101 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003102 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3103 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003104 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003105 case OMPD_for_simd:
3106 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3107 EndLoc, VarsWithInheritedDSA);
3108 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003109 case OMPD_sections:
3110 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3111 EndLoc);
3112 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003113 case OMPD_section:
3114 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003115 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003116 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3117 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003118 case OMPD_single:
3119 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3120 EndLoc);
3121 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003122 case OMPD_master:
3123 assert(ClausesWithImplicit.empty() &&
3124 "No clauses are allowed for 'omp master' directive");
3125 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3126 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003127 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003128 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3129 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003130 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003131 case OMPD_parallel_for:
3132 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3133 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003134 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003135 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003136 case OMPD_parallel_for_simd:
3137 Res = ActOnOpenMPParallelForSimdDirective(
3138 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003139 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003140 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003141 case OMPD_parallel_sections:
3142 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3143 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003144 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003145 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003146 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003147 Res =
3148 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003149 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003150 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003151 case OMPD_taskyield:
3152 assert(ClausesWithImplicit.empty() &&
3153 "No clauses are allowed for 'omp taskyield' directive");
3154 assert(AStmt == nullptr &&
3155 "No associated statement allowed for 'omp taskyield' directive");
3156 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3157 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003158 case OMPD_barrier:
3159 assert(ClausesWithImplicit.empty() &&
3160 "No clauses are allowed for 'omp barrier' directive");
3161 assert(AStmt == nullptr &&
3162 "No associated statement allowed for 'omp barrier' directive");
3163 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3164 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003165 case OMPD_taskwait:
3166 assert(ClausesWithImplicit.empty() &&
3167 "No clauses are allowed for 'omp taskwait' directive");
3168 assert(AStmt == nullptr &&
3169 "No associated statement allowed for 'omp taskwait' directive");
3170 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3171 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003172 case OMPD_taskgroup:
3173 assert(ClausesWithImplicit.empty() &&
3174 "No clauses are allowed for 'omp taskgroup' directive");
3175 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3176 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003177 case OMPD_flush:
3178 assert(AStmt == nullptr &&
3179 "No associated statement allowed for 'omp flush' directive");
3180 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3181 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003182 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003183 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3184 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003185 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003186 case OMPD_atomic:
3187 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3188 EndLoc);
3189 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003190 case OMPD_teams:
3191 Res =
3192 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3193 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003194 case OMPD_target:
3195 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3196 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003197 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003198 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003199 case OMPD_target_parallel:
3200 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3201 StartLoc, EndLoc);
3202 AllowedNameModifiers.push_back(OMPD_target);
3203 AllowedNameModifiers.push_back(OMPD_parallel);
3204 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003205 case OMPD_target_parallel_for:
3206 Res = ActOnOpenMPTargetParallelForDirective(
3207 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3208 AllowedNameModifiers.push_back(OMPD_target);
3209 AllowedNameModifiers.push_back(OMPD_parallel);
3210 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003211 case OMPD_cancellation_point:
3212 assert(ClausesWithImplicit.empty() &&
3213 "No clauses are allowed for 'omp cancellation point' directive");
3214 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3215 "cancellation point' directive");
3216 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3217 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003218 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003219 assert(AStmt == nullptr &&
3220 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003221 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3222 CancelRegion);
3223 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003224 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003225 case OMPD_target_data:
3226 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3227 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003228 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003229 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003230 case OMPD_target_enter_data:
3231 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3232 EndLoc);
3233 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3234 break;
Samuel Antao72590762016-01-19 20:04:50 +00003235 case OMPD_target_exit_data:
3236 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3237 EndLoc);
3238 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3239 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003240 case OMPD_taskloop:
3241 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3242 EndLoc, VarsWithInheritedDSA);
3243 AllowedNameModifiers.push_back(OMPD_taskloop);
3244 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003245 case OMPD_taskloop_simd:
3246 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3247 EndLoc, VarsWithInheritedDSA);
3248 AllowedNameModifiers.push_back(OMPD_taskloop);
3249 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003250 case OMPD_distribute:
3251 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3252 EndLoc, VarsWithInheritedDSA);
3253 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003254 case OMPD_declare_target:
3255 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003256 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003257 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003258 case OMPD_declare_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003259 llvm_unreachable("OpenMP Directive is not allowed");
3260 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003261 llvm_unreachable("Unknown OpenMP directive");
3262 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003263
Alexey Bataev4acb8592014-07-07 13:01:15 +00003264 for (auto P : VarsWithInheritedDSA) {
3265 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3266 << P.first << P.second->getSourceRange();
3267 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003268 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3269
3270 if (!AllowedNameModifiers.empty())
3271 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3272 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003273
Alexey Bataeved09d242014-05-28 05:53:51 +00003274 if (ErrorFound)
3275 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003276 return Res;
3277}
3278
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003279Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3280 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00003281 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00003282 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3283 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003284 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00003285 assert(Linears.size() == LinModifiers.size());
3286 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00003287 if (!DG || DG.get().isNull())
3288 return DeclGroupPtrTy();
3289
3290 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00003291 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003292 return DG;
3293 }
3294 auto *ADecl = DG.get().getSingleDecl();
3295 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3296 ADecl = FTD->getTemplatedDecl();
3297
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003298 auto *FD = dyn_cast<FunctionDecl>(ADecl);
3299 if (!FD) {
3300 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003301 return DeclGroupPtrTy();
3302 }
3303
Alexey Bataev2af33e32016-04-07 12:45:37 +00003304 // OpenMP [2.8.2, declare simd construct, Description]
3305 // The parameter of the simdlen clause must be a constant positive integer
3306 // expression.
3307 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003308 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00003309 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003310 // OpenMP [2.8.2, declare simd construct, Description]
3311 // The special this pointer can be used as if was one of the arguments to the
3312 // function in any of the linear, aligned, or uniform clauses.
3313 // The uniform clause declares one or more arguments to have an invariant
3314 // value for all concurrent invocations of the function in the execution of a
3315 // single SIMD loop.
Alexey Bataevecba70f2016-04-12 11:02:11 +00003316 llvm::DenseMap<Decl *, Expr *> UniformedArgs;
3317 Expr *UniformedLinearThis = nullptr;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003318 for (auto *E : Uniforms) {
3319 E = E->IgnoreParenImpCasts();
3320 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3321 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
3322 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3323 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00003324 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
3325 UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E));
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003326 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003327 }
3328 if (isa<CXXThisExpr>(E)) {
3329 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003330 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003331 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003332 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3333 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00003334 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00003335 // OpenMP [2.8.2, declare simd construct, Description]
3336 // The aligned clause declares that the object to which each list item points
3337 // is aligned to the number of bytes expressed in the optional parameter of
3338 // the aligned clause.
3339 // The special this pointer can be used as if was one of the arguments to the
3340 // function in any of the linear, aligned, or uniform clauses.
3341 // The type of list items appearing in the aligned clause must be array,
3342 // pointer, reference to array, or reference to pointer.
3343 llvm::DenseMap<Decl *, Expr *> AlignedArgs;
3344 Expr *AlignedThis = nullptr;
3345 for (auto *E : Aligneds) {
3346 E = E->IgnoreParenImpCasts();
3347 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3348 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3349 auto *CanonPVD = PVD->getCanonicalDecl();
3350 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3351 FD->getParamDecl(PVD->getFunctionScopeIndex())
3352 ->getCanonicalDecl() == CanonPVD) {
3353 // OpenMP [2.8.1, simd construct, Restrictions]
3354 // A list-item cannot appear in more than one aligned clause.
3355 if (AlignedArgs.count(CanonPVD) > 0) {
3356 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3357 << 1 << E->getSourceRange();
3358 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3359 diag::note_omp_explicit_dsa)
3360 << getOpenMPClauseName(OMPC_aligned);
3361 continue;
3362 }
3363 AlignedArgs[CanonPVD] = E;
3364 QualType QTy = PVD->getType()
3365 .getNonReferenceType()
3366 .getUnqualifiedType()
3367 .getCanonicalType();
3368 const Type *Ty = QTy.getTypePtrOrNull();
3369 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3370 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3371 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3372 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3373 }
3374 continue;
3375 }
3376 }
3377 if (isa<CXXThisExpr>(E)) {
3378 if (AlignedThis) {
3379 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3380 << 2 << E->getSourceRange();
3381 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3382 << getOpenMPClauseName(OMPC_aligned);
3383 }
3384 AlignedThis = E;
3385 continue;
3386 }
3387 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3388 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3389 }
3390 // The optional parameter of the aligned clause, alignment, must be a constant
3391 // positive integer expression. If no optional parameter is specified,
3392 // implementation-defined default alignments for SIMD instructions on the
3393 // target platforms are assumed.
3394 SmallVector<Expr *, 4> NewAligns;
3395 for (auto *E : Alignments) {
3396 ExprResult Align;
3397 if (E)
3398 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3399 NewAligns.push_back(Align.get());
3400 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00003401 // OpenMP [2.8.2, declare simd construct, Description]
3402 // The linear clause declares one or more list items to be private to a SIMD
3403 // lane and to have a linear relationship with respect to the iteration space
3404 // of a loop.
3405 // The special this pointer can be used as if was one of the arguments to the
3406 // function in any of the linear, aligned, or uniform clauses.
3407 // When a linear-step expression is specified in a linear clause it must be
3408 // either a constant integer expression or an integer-typed parameter that is
3409 // specified in a uniform clause on the directive.
3410 llvm::DenseMap<Decl *, Expr *> LinearArgs;
3411 const bool IsUniformedThis = UniformedLinearThis != nullptr;
3412 auto MI = LinModifiers.begin();
3413 for (auto *E : Linears) {
3414 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3415 ++MI;
3416 E = E->IgnoreParenImpCasts();
3417 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3418 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3419 auto *CanonPVD = PVD->getCanonicalDecl();
3420 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3421 FD->getParamDecl(PVD->getFunctionScopeIndex())
3422 ->getCanonicalDecl() == CanonPVD) {
3423 // OpenMP [2.15.3.7, linear Clause, Restrictions]
3424 // A list-item cannot appear in more than one linear clause.
3425 if (LinearArgs.count(CanonPVD) > 0) {
3426 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3427 << getOpenMPClauseName(OMPC_linear)
3428 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3429 Diag(LinearArgs[CanonPVD]->getExprLoc(),
3430 diag::note_omp_explicit_dsa)
3431 << getOpenMPClauseName(OMPC_linear);
3432 continue;
3433 }
3434 // Each argument can appear in at most one uniform or linear clause.
3435 if (UniformedArgs.count(CanonPVD) > 0) {
3436 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3437 << getOpenMPClauseName(OMPC_linear)
3438 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3439 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3440 diag::note_omp_explicit_dsa)
3441 << getOpenMPClauseName(OMPC_uniform);
3442 continue;
3443 }
3444 LinearArgs[CanonPVD] = E;
3445 if (E->isValueDependent() || E->isTypeDependent() ||
3446 E->isInstantiationDependent() ||
3447 E->containsUnexpandedParameterPack())
3448 continue;
3449 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3450 PVD->getOriginalType());
3451 continue;
3452 }
3453 }
3454 if (isa<CXXThisExpr>(E)) {
3455 if (UniformedLinearThis) {
3456 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3457 << getOpenMPClauseName(OMPC_linear)
3458 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3459 << E->getSourceRange();
3460 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3461 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3462 : OMPC_linear);
3463 continue;
3464 }
3465 UniformedLinearThis = E;
3466 if (E->isValueDependent() || E->isTypeDependent() ||
3467 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3468 continue;
3469 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3470 E->getType());
3471 continue;
3472 }
3473 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3474 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3475 }
3476 Expr *Step = nullptr;
3477 Expr *NewStep = nullptr;
3478 SmallVector<Expr *, 4> NewSteps;
3479 for (auto *E : Steps) {
3480 // Skip the same step expression, it was checked already.
3481 if (Step == E || !E) {
3482 NewSteps.push_back(E ? NewStep : nullptr);
3483 continue;
3484 }
3485 Step = E;
3486 if (auto *DRE = dyn_cast<DeclRefExpr>(Step))
3487 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3488 auto *CanonPVD = PVD->getCanonicalDecl();
3489 if (UniformedArgs.count(CanonPVD) == 0) {
3490 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3491 << Step->getSourceRange();
3492 } else if (E->isValueDependent() || E->isTypeDependent() ||
3493 E->isInstantiationDependent() ||
3494 E->containsUnexpandedParameterPack() ||
3495 CanonPVD->getType()->hasIntegerRepresentation())
3496 NewSteps.push_back(Step);
3497 else {
3498 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3499 << Step->getSourceRange();
3500 }
3501 continue;
3502 }
3503 NewStep = Step;
3504 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3505 !Step->isInstantiationDependent() &&
3506 !Step->containsUnexpandedParameterPack()) {
3507 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3508 .get();
3509 if (NewStep)
3510 NewStep = VerifyIntegerConstantExpression(NewStep).get();
3511 }
3512 NewSteps.push_back(NewStep);
3513 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003514 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3515 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00003516 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00003517 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3518 const_cast<Expr **>(Linears.data()), Linears.size(),
3519 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3520 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003521 ADecl->addAttr(NewAttr);
3522 return ConvertDeclToDeclGroup(ADecl);
3523}
3524
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003525StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3526 Stmt *AStmt,
3527 SourceLocation StartLoc,
3528 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003529 if (!AStmt)
3530 return StmtError();
3531
Alexey Bataev9959db52014-05-06 10:08:46 +00003532 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3533 // 1.2.2 OpenMP Language Terminology
3534 // Structured block - An executable statement with a single entry at the
3535 // top and a single exit at the bottom.
3536 // The point of exit cannot be a branch out of the structured block.
3537 // longjmp() and throw() must not violate the entry/exit criteria.
3538 CS->getCapturedDecl()->setNothrow();
3539
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003540 getCurFunction()->setHasBranchProtectedScope();
3541
Alexey Bataev25e5b442015-09-15 12:52:43 +00003542 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3543 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003544}
3545
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003546namespace {
3547/// \brief Helper class for checking canonical form of the OpenMP loops and
3548/// extracting iteration space of each loop in the loop nest, that will be used
3549/// for IR generation.
3550class OpenMPIterationSpaceChecker {
3551 /// \brief Reference to Sema.
3552 Sema &SemaRef;
3553 /// \brief A location for diagnostics (when there is no some better location).
3554 SourceLocation DefaultLoc;
3555 /// \brief A location for diagnostics (when increment is not compatible).
3556 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003557 /// \brief A source location for referring to loop init later.
3558 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003559 /// \brief A source location for referring to condition later.
3560 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003561 /// \brief A source location for referring to increment later.
3562 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003563 /// \brief Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003564 ValueDecl *LCDecl = nullptr;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003565 /// \brief Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003566 Expr *LCRef = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003567 /// \brief Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003568 Expr *LB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003569 /// \brief Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003570 Expr *UB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003571 /// \brief Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003572 Expr *Step = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003573 /// \brief This flag is true when condition is one of:
3574 /// Var < UB
3575 /// Var <= UB
3576 /// UB > Var
3577 /// UB >= Var
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003578 bool TestIsLessOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003579 /// \brief This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003580 bool TestIsStrictOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003581 /// \brief This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003582 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003583
3584public:
3585 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003586 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003587 /// \brief Check init-expr for canonical loop form and save loop counter
3588 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00003589 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003590 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3591 /// for less/greater and for strict/non-strict comparison.
3592 bool CheckCond(Expr *S);
3593 /// \brief Check incr-expr for canonical loop form and return true if it
3594 /// does not conform, otherwise save loop step (#Step).
3595 bool CheckInc(Expr *S);
3596 /// \brief Return the loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003597 ValueDecl *GetLoopDecl() const { return LCDecl; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003598 /// \brief Return the reference expression to loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003599 Expr *GetLoopDeclRefExpr() const { return LCRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003600 /// \brief Source range of the loop init.
3601 SourceRange GetInitSrcRange() const { return InitSrcRange; }
3602 /// \brief Source range of the loop condition.
3603 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3604 /// \brief Source range of the loop increment.
3605 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3606 /// \brief True if the step should be subtracted.
3607 bool ShouldSubtractStep() const { return SubtractStep; }
3608 /// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003609 Expr *
3610 BuildNumIterations(Scope *S, const bool LimitedType,
3611 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003612 /// \brief Build the precondition expression for the loops.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003613 Expr *BuildPreCond(Scope *S, Expr *Cond,
3614 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003615 /// \brief Build reference expression to the counter be used for codegen.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00003616 DeclRefExpr *BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures,
3617 DSAStackTy &DSA) const;
Alexey Bataeva8899172015-08-06 12:30:57 +00003618 /// \brief Build reference expression to the private counter be used for
3619 /// codegen.
3620 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003621 /// \brief Build initization of the counter be used for codegen.
3622 Expr *BuildCounterInit() const;
3623 /// \brief Build step of the counter be used for codegen.
3624 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003625 /// \brief Return true if any expression is dependent.
3626 bool Dependent() const;
3627
3628private:
3629 /// \brief Check the right-hand side of an assignment in the increment
3630 /// expression.
3631 bool CheckIncRHS(Expr *RHS);
3632 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003633 bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003634 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00003635 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003636 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003637 /// \brief Helper to set loop increment.
3638 bool SetStep(Expr *NewStep, bool Subtract);
3639};
3640
3641bool OpenMPIterationSpaceChecker::Dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003642 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003643 assert(!LB && !UB && !Step);
3644 return false;
3645 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003646 return LCDecl->getType()->isDependentType() ||
3647 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
3648 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003649}
3650
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003651static Expr *getExprAsWritten(Expr *E) {
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003652 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
3653 E = ExprTemp->getSubExpr();
3654
3655 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
3656 E = MTE->GetTemporaryExpr();
3657
3658 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
3659 E = Binder->getSubExpr();
3660
3661 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
3662 E = ICE->getSubExprAsWritten();
3663 return E->IgnoreParens();
3664}
3665
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003666bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl,
3667 Expr *NewLCRefExpr,
3668 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003669 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003670 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003671 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003672 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003673 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003674 LCDecl = getCanonicalDecl(NewLCDecl);
3675 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003676 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3677 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003678 if ((Ctor->isCopyOrMoveConstructor() ||
3679 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3680 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003681 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003682 LB = NewLB;
3683 return false;
3684}
3685
3686bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00003687 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003688 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003689 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
3690 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003691 if (!NewUB)
3692 return true;
3693 UB = NewUB;
3694 TestIsLessOp = LessOp;
3695 TestIsStrictOp = StrictOp;
3696 ConditionSrcRange = SR;
3697 ConditionLoc = SL;
3698 return false;
3699}
3700
3701bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3702 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003703 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003704 if (!NewStep)
3705 return true;
3706 if (!NewStep->isValueDependent()) {
3707 // Check that the step is integer expression.
3708 SourceLocation StepLoc = NewStep->getLocStart();
3709 ExprResult Val =
3710 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
3711 if (Val.isInvalid())
3712 return true;
3713 NewStep = Val.get();
3714
3715 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3716 // If test-expr is of form var relational-op b and relational-op is < or
3717 // <= then incr-expr must cause var to increase on each iteration of the
3718 // loop. If test-expr is of form var relational-op b and relational-op is
3719 // > or >= then incr-expr must cause var to decrease on each iteration of
3720 // the loop.
3721 // If test-expr is of form b relational-op var and relational-op is < or
3722 // <= then incr-expr must cause var to decrease on each iteration of the
3723 // loop. If test-expr is of form b relational-op var and relational-op is
3724 // > or >= then incr-expr must cause var to increase on each iteration of
3725 // the loop.
3726 llvm::APSInt Result;
3727 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3728 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3729 bool IsConstNeg =
3730 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003731 bool IsConstPos =
3732 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003733 bool IsConstZero = IsConstant && !Result.getBoolValue();
3734 if (UB && (IsConstZero ||
3735 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00003736 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003737 SemaRef.Diag(NewStep->getExprLoc(),
3738 diag::err_omp_loop_incr_not_compatible)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003739 << LCDecl << TestIsLessOp << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003740 SemaRef.Diag(ConditionLoc,
3741 diag::note_omp_loop_cond_requres_compatible_incr)
3742 << TestIsLessOp << ConditionSrcRange;
3743 return true;
3744 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003745 if (TestIsLessOp == Subtract) {
3746 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
3747 NewStep).get();
3748 Subtract = !Subtract;
3749 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003750 }
3751
3752 Step = NewStep;
3753 SubtractStep = Subtract;
3754 return false;
3755}
3756
Alexey Bataev9c821032015-04-30 04:23:23 +00003757bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003758 // Check init-expr for canonical loop form and save loop counter
3759 // variable - #Var and its initialization value - #LB.
3760 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3761 // var = lb
3762 // integer-type var = lb
3763 // random-access-iterator-type var = lb
3764 // pointer-type var = lb
3765 //
3766 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003767 if (EmitDiags) {
3768 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3769 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003770 return true;
3771 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003772 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003773 if (Expr *E = dyn_cast<Expr>(S))
3774 S = E->IgnoreParens();
3775 if (auto BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003776 if (BO->getOpcode() == BO_Assign) {
3777 auto *LHS = BO->getLHS()->IgnoreParens();
3778 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
3779 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3780 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3781 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3782 return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
3783 }
3784 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3785 if (ME->isArrow() &&
3786 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3787 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3788 }
3789 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003790 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
3791 if (DS->isSingleDecl()) {
3792 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00003793 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003794 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00003795 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003796 SemaRef.Diag(S->getLocStart(),
3797 diag::ext_omp_loop_not_canonical_init)
3798 << S->getSourceRange();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003799 return SetLCDeclAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003800 }
3801 }
3802 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003803 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3804 if (CE->getOperator() == OO_Equal) {
3805 auto *LHS = CE->getArg(0);
3806 if (auto DRE = dyn_cast<DeclRefExpr>(LHS)) {
3807 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3808 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3809 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3810 return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
3811 }
3812 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3813 if (ME->isArrow() &&
3814 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3815 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3816 }
3817 }
3818 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003819
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003820 if (Dependent() || SemaRef.CurContext->isDependentContext())
3821 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00003822 if (EmitDiags) {
3823 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3824 << S->getSourceRange();
3825 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003826 return true;
3827}
3828
Alexey Bataev23b69422014-06-18 07:08:49 +00003829/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003830/// variable (which may be the loop variable) if possible.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003831static const ValueDecl *GetInitLCDecl(Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003832 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00003833 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003834 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003835 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3836 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003837 if ((Ctor->isCopyOrMoveConstructor() ||
3838 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3839 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003840 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003841 if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
3842 if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
3843 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
3844 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3845 return getCanonicalDecl(ME->getMemberDecl());
3846 return getCanonicalDecl(VD);
3847 }
3848 }
3849 if (auto *ME = dyn_cast_or_null<MemberExpr>(E))
3850 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3851 return getCanonicalDecl(ME->getMemberDecl());
3852 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003853}
3854
3855bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3856 // Check test-expr for canonical form, save upper-bound UB, flags for
3857 // less/greater and for strict/non-strict comparison.
3858 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3859 // var relational-op b
3860 // b relational-op var
3861 //
3862 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003863 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003864 return true;
3865 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003866 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003867 SourceLocation CondLoc = S->getLocStart();
3868 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3869 if (BO->isRelationalOp()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003870 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003871 return SetUB(BO->getRHS(),
3872 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3873 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3874 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003875 if (GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003876 return SetUB(BO->getLHS(),
3877 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3878 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3879 BO->getSourceRange(), BO->getOperatorLoc());
3880 }
3881 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3882 if (CE->getNumArgs() == 2) {
3883 auto Op = CE->getOperator();
3884 switch (Op) {
3885 case OO_Greater:
3886 case OO_GreaterEqual:
3887 case OO_Less:
3888 case OO_LessEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003889 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003890 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3891 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3892 CE->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003893 if (GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003894 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3895 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3896 CE->getOperatorLoc());
3897 break;
3898 default:
3899 break;
3900 }
3901 }
3902 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003903 if (Dependent() || SemaRef.CurContext->isDependentContext())
3904 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003905 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003906 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003907 return true;
3908}
3909
3910bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3911 // RHS of canonical loop form increment can be:
3912 // var + incr
3913 // incr + var
3914 // var - incr
3915 //
3916 RHS = RHS->IgnoreParenImpCasts();
3917 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3918 if (BO->isAdditiveOp()) {
3919 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003920 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003921 return SetStep(BO->getRHS(), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003922 if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003923 return SetStep(BO->getLHS(), false);
3924 }
3925 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3926 bool IsAdd = CE->getOperator() == OO_Plus;
3927 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003928 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003929 return SetStep(CE->getArg(1), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003930 if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003931 return SetStep(CE->getArg(0), false);
3932 }
3933 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003934 if (Dependent() || SemaRef.CurContext->isDependentContext())
3935 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003936 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003937 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003938 return true;
3939}
3940
3941bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3942 // Check incr-expr for canonical loop form and return true if it
3943 // does not conform.
3944 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3945 // ++var
3946 // var++
3947 // --var
3948 // var--
3949 // var += incr
3950 // var -= incr
3951 // var = var + incr
3952 // var = incr + var
3953 // var = var - incr
3954 //
3955 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003956 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003957 return true;
3958 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003959 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003960 S = S->IgnoreParens();
3961 if (auto UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003962 if (UO->isIncrementDecrementOp() &&
3963 GetInitLCDecl(UO->getSubExpr()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003964 return SetStep(
3965 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3966 (UO->isDecrementOp() ? -1 : 1)).get(),
3967 false);
3968 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3969 switch (BO->getOpcode()) {
3970 case BO_AddAssign:
3971 case BO_SubAssign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003972 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003973 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3974 break;
3975 case BO_Assign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003976 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003977 return CheckIncRHS(BO->getRHS());
3978 break;
3979 default:
3980 break;
3981 }
3982 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3983 switch (CE->getOperator()) {
3984 case OO_PlusPlus:
3985 case OO_MinusMinus:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003986 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003987 return SetStep(
3988 SemaRef.ActOnIntegerConstant(
3989 CE->getLocStart(),
3990 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3991 false);
3992 break;
3993 case OO_PlusEqual:
3994 case OO_MinusEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003995 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003996 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3997 break;
3998 case OO_Equal:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003999 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004000 return CheckIncRHS(CE->getArg(1));
4001 break;
4002 default:
4003 break;
4004 }
4005 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004006 if (Dependent() || SemaRef.CurContext->isDependentContext())
4007 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004008 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004009 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004010 return true;
4011}
Alexander Musmana5f070a2014-10-01 06:03:56 +00004012
Alexey Bataev5a3af132016-03-29 08:58:54 +00004013static ExprResult
4014tryBuildCapture(Sema &SemaRef, Expr *Capture,
4015 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4016 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
4017 return SemaRef.PerformImplicitConversion(
4018 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
4019 /*AllowExplicit=*/true);
4020 auto I = Captures.find(Capture);
4021 if (I != Captures.end())
4022 return buildCapture(SemaRef, Capture, I->second);
4023 DeclRefExpr *Ref = nullptr;
4024 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4025 Captures[Capture] = Ref;
4026 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004027}
4028
Alexander Musmana5f070a2014-10-01 06:03:56 +00004029/// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004030Expr *OpenMPIterationSpaceChecker::BuildNumIterations(
4031 Scope *S, const bool LimitedType,
4032 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004033 ExprResult Diff;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004034 auto VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004035 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004036 SemaRef.getLangOpts().CPlusPlus) {
4037 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004038 auto *UBExpr = TestIsLessOp ? UB : LB;
4039 auto *LBExpr = TestIsLessOp ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00004040 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
4041 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004042 if (!Upper || !Lower)
4043 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004044
4045 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4046
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004047 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004048 // BuildBinOp already emitted error, this one is to point user to upper
4049 // and lower bound, and to tell what is passed to 'operator-'.
4050 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
4051 << Upper->getSourceRange() << Lower->getSourceRange();
4052 return nullptr;
4053 }
4054 }
4055
4056 if (!Diff.isUsable())
4057 return nullptr;
4058
4059 // Upper - Lower [- 1]
4060 if (TestIsStrictOp)
4061 Diff = SemaRef.BuildBinOp(
4062 S, DefaultLoc, BO_Sub, Diff.get(),
4063 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4064 if (!Diff.isUsable())
4065 return nullptr;
4066
4067 // Upper - Lower [- 1] + Step
Alexey Bataev5a3af132016-03-29 08:58:54 +00004068 auto NewStep = tryBuildCapture(SemaRef, Step, Captures);
4069 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004070 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004071 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004072 if (!Diff.isUsable())
4073 return nullptr;
4074
4075 // Parentheses (for dumping/debugging purposes only).
4076 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4077 if (!Diff.isUsable())
4078 return nullptr;
4079
4080 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004081 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004082 if (!Diff.isUsable())
4083 return nullptr;
4084
Alexander Musman174b3ca2014-10-06 11:16:29 +00004085 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004086 QualType Type = Diff.get()->getType();
4087 auto &C = SemaRef.Context;
4088 bool UseVarType = VarType->hasIntegerRepresentation() &&
4089 C.getTypeSize(Type) > C.getTypeSize(VarType);
4090 if (!Type->isIntegerType() || UseVarType) {
4091 unsigned NewSize =
4092 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4093 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4094 : Type->hasSignedIntegerRepresentation();
4095 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00004096 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4097 Diff = SemaRef.PerformImplicitConversion(
4098 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4099 if (!Diff.isUsable())
4100 return nullptr;
4101 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004102 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004103 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00004104 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4105 if (NewSize != C.getTypeSize(Type)) {
4106 if (NewSize < C.getTypeSize(Type)) {
4107 assert(NewSize == 64 && "incorrect loop var size");
4108 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4109 << InitSrcRange << ConditionSrcRange;
4110 }
4111 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004112 NewSize, Type->hasSignedIntegerRepresentation() ||
4113 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00004114 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4115 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4116 Sema::AA_Converting, true);
4117 if (!Diff.isUsable())
4118 return nullptr;
4119 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004120 }
4121 }
4122
Alexander Musmana5f070a2014-10-01 06:03:56 +00004123 return Diff.get();
4124}
4125
Alexey Bataev5a3af132016-03-29 08:58:54 +00004126Expr *OpenMPIterationSpaceChecker::BuildPreCond(
4127 Scope *S, Expr *Cond,
4128 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004129 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4130 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4131 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004132
Alexey Bataev5a3af132016-03-29 08:58:54 +00004133 auto NewLB = tryBuildCapture(SemaRef, LB, Captures);
4134 auto NewUB = tryBuildCapture(SemaRef, UB, Captures);
4135 if (!NewLB.isUsable() || !NewUB.isUsable())
4136 return nullptr;
4137
Alexey Bataev62dbb972015-04-22 11:59:37 +00004138 auto CondExpr = SemaRef.BuildBinOp(
4139 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
4140 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004141 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004142 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004143 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4144 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00004145 CondExpr = SemaRef.PerformImplicitConversion(
4146 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4147 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004148 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00004149 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4150 // Otherwise use original loop conditon and evaluate it in runtime.
4151 return CondExpr.isUsable() ? CondExpr.get() : Cond;
4152}
4153
Alexander Musmana5f070a2014-10-01 06:03:56 +00004154/// \brief Build reference expression to the counter be used for codegen.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004155DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar(
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004156 llvm::MapVector<Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004157 auto *VD = dyn_cast<VarDecl>(LCDecl);
4158 if (!VD) {
4159 VD = SemaRef.IsOpenMPCapturedDecl(LCDecl);
4160 auto *Ref = buildDeclRefExpr(
4161 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004162 DSAStackTy::DSAVarData Data = DSA.getTopDSA(LCDecl, /*FromParent=*/false);
4163 // If the loop control decl is explicitly marked as private, do not mark it
4164 // as captured again.
4165 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4166 Captures.insert(std::make_pair(LCRef, Ref));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004167 return Ref;
4168 }
4169 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00004170 DefaultLoc);
4171}
4172
4173Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004174 if (LCDecl && !LCDecl->isInvalidDecl()) {
4175 auto Type = LCDecl->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00004176 auto *PrivateVar =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004177 buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(),
4178 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00004179 if (PrivateVar->isInvalidDecl())
4180 return nullptr;
4181 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4182 }
4183 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004184}
4185
4186/// \brief Build initization of the counter be used for codegen.
4187Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
4188
4189/// \brief Build step of the counter be used for codegen.
4190Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
4191
4192/// \brief Iteration space of a single for loop.
4193struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004194 /// \brief Condition of the loop.
4195 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004196 /// \brief This expression calculates the number of iterations in the loop.
4197 /// It is always possible to calculate it before starting the loop.
4198 Expr *NumIterations;
4199 /// \brief The loop counter variable.
4200 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004201 /// \brief Private loop counter variable.
4202 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004203 /// \brief This is initializer for the initial value of #CounterVar.
4204 Expr *CounterInit;
4205 /// \brief This is step for the #CounterVar used to generate its update:
4206 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
4207 Expr *CounterStep;
4208 /// \brief Should step be subtracted?
4209 bool Subtract;
4210 /// \brief Source range of the loop init.
4211 SourceRange InitSrcRange;
4212 /// \brief Source range of the loop condition.
4213 SourceRange CondSrcRange;
4214 /// \brief Source range of the loop increment.
4215 SourceRange IncSrcRange;
4216};
4217
Alexey Bataev23b69422014-06-18 07:08:49 +00004218} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004219
Alexey Bataev9c821032015-04-30 04:23:23 +00004220void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4221 assert(getLangOpts().OpenMP && "OpenMP is not active.");
4222 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004223 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4224 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00004225 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4226 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004227 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
4228 if (auto *D = ISC.GetLoopDecl()) {
4229 auto *VD = dyn_cast<VarDecl>(D);
4230 if (!VD) {
4231 if (auto *Private = IsOpenMPCapturedDecl(D))
4232 VD = Private;
4233 else {
4234 auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(),
4235 /*WithInit=*/false);
4236 VD = cast<VarDecl>(Ref->getDecl());
4237 }
4238 }
4239 DSAStack->addLoopControlVariable(D, VD);
4240 }
4241 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004242 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00004243 }
4244}
4245
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004246/// \brief Called on a for stmt to check and extract its iteration space
4247/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00004248static bool CheckOpenMPIterationSpace(
4249 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4250 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004251 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004252 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004253 LoopIterationSpace &ResultIterSpace,
4254 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004255 // OpenMP [2.6, Canonical Loop Form]
4256 // for (init-expr; test-expr; incr-expr) structured-block
4257 auto For = dyn_cast_or_null<ForStmt>(S);
4258 if (!For) {
4259 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004260 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
4261 << getOpenMPDirectiveName(DKind) << NestedLoopCount
4262 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
4263 if (NestedLoopCount > 1) {
4264 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4265 SemaRef.Diag(DSA.getConstructLoc(),
4266 diag::note_omp_collapse_ordered_expr)
4267 << 2 << CollapseLoopCountExpr->getSourceRange()
4268 << OrderedLoopCountExpr->getSourceRange();
4269 else if (CollapseLoopCountExpr)
4270 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4271 diag::note_omp_collapse_ordered_expr)
4272 << 0 << CollapseLoopCountExpr->getSourceRange();
4273 else
4274 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4275 diag::note_omp_collapse_ordered_expr)
4276 << 1 << OrderedLoopCountExpr->getSourceRange();
4277 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004278 return true;
4279 }
4280 assert(For->getBody());
4281
4282 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4283
4284 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00004285 auto Init = For->getInit();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004286 if (ISC.CheckInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004287 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004288
4289 bool HasErrors = false;
4290
4291 // Check loop variable's type.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004292 if (auto *LCDecl = ISC.GetLoopDecl()) {
4293 auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004294
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004295 // OpenMP [2.6, Canonical Loop Form]
4296 // Var is one of the following:
4297 // A variable of signed or unsigned integer type.
4298 // For C++, a variable of a random access iterator type.
4299 // For C, a variable of a pointer type.
4300 auto VarType = LCDecl->getType().getNonReferenceType();
4301 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4302 !VarType->isPointerType() &&
4303 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
4304 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
4305 << SemaRef.getLangOpts().CPlusPlus;
4306 HasErrors = true;
4307 }
4308
4309 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4310 // a Construct
4311 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4312 // parallel for construct is (are) private.
4313 // The loop iteration variable in the associated for-loop of a simd
4314 // construct with just one associated for-loop is linear with a
4315 // constant-linear-step that is the increment of the associated for-loop.
4316 // Exclude loop var from the list of variables with implicitly defined data
4317 // sharing attributes.
4318 VarsWithImplicitDSA.erase(LCDecl);
4319
4320 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4321 // in a Construct, C/C++].
4322 // The loop iteration variable in the associated for-loop of a simd
4323 // construct with just one associated for-loop may be listed in a linear
4324 // clause with a constant-linear-step that is the increment of the
4325 // associated for-loop.
4326 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4327 // parallel for construct may be listed in a private or lastprivate clause.
4328 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4329 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4330 // declared in the loop and it is predetermined as a private.
4331 auto PredeterminedCKind =
4332 isOpenMPSimdDirective(DKind)
4333 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4334 : OMPC_private;
4335 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4336 DVar.CKind != PredeterminedCKind) ||
4337 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4338 isOpenMPDistributeDirective(DKind)) &&
4339 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4340 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4341 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4342 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
4343 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4344 << getOpenMPClauseName(PredeterminedCKind);
4345 if (DVar.RefExpr == nullptr)
4346 DVar.CKind = PredeterminedCKind;
4347 ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
4348 HasErrors = true;
4349 } else if (LoopDeclRefExpr != nullptr) {
4350 // Make the loop iteration variable private (for worksharing constructs),
4351 // linear (for simd directives with the only one associated loop) or
4352 // lastprivate (for simd directives with several collapsed or ordered
4353 // loops).
4354 if (DVar.CKind == OMPC_unknown)
Alexey Bataev7ace49d2016-05-17 08:55:33 +00004355 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
4356 [](OpenMPDirectiveKind) -> bool { return true; },
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004357 /*FromParent=*/false);
4358 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4359 }
4360
4361 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4362
4363 // Check test-expr.
4364 HasErrors |= ISC.CheckCond(For->getCond());
4365
4366 // Check incr-expr.
4367 HasErrors |= ISC.CheckInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004368 }
4369
Alexander Musmana5f070a2014-10-01 06:03:56 +00004370 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004371 return HasErrors;
4372
Alexander Musmana5f070a2014-10-01 06:03:56 +00004373 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004374 ResultIterSpace.PreCond =
4375 ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures);
Alexander Musman174b3ca2014-10-06 11:16:29 +00004376 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004377 DSA.getCurScope(),
4378 (isOpenMPWorksharingDirective(DKind) ||
4379 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4380 Captures);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004381 ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures, DSA);
Alexey Bataeva8899172015-08-06 12:30:57 +00004382 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004383 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
4384 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
4385 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
4386 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4387 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4388 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4389
Alexey Bataev62dbb972015-04-22 11:59:37 +00004390 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4391 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004392 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004393 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004394 ResultIterSpace.CounterInit == nullptr ||
4395 ResultIterSpace.CounterStep == nullptr);
4396
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004397 return HasErrors;
4398}
4399
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004400/// \brief Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004401static ExprResult
4402BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4403 ExprResult Start,
4404 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004405 // Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004406 auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
4407 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004408 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004409 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00004410 VarRef.get()->getType())) {
4411 NewStart = SemaRef.PerformImplicitConversion(
4412 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4413 /*AllowExplicit=*/true);
4414 if (!NewStart.isUsable())
4415 return ExprError();
4416 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004417
4418 auto Init =
4419 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4420 return Init;
4421}
4422
Alexander Musmana5f070a2014-10-01 06:03:56 +00004423/// \brief Build 'VarRef = Start + Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004424static ExprResult
4425BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc,
4426 ExprResult VarRef, ExprResult Start, ExprResult Iter,
4427 ExprResult Step, bool Subtract,
4428 llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004429 // Add parentheses (for debugging purposes only).
4430 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4431 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4432 !Step.isUsable())
4433 return ExprError();
4434
Alexey Bataev5a3af132016-03-29 08:58:54 +00004435 ExprResult NewStep = Step;
4436 if (Captures)
4437 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004438 if (NewStep.isInvalid())
4439 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004440 ExprResult Update =
4441 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004442 if (!Update.isUsable())
4443 return ExprError();
4444
Alexey Bataevc0214e02016-02-16 12:13:49 +00004445 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4446 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004447 ExprResult NewStart = Start;
4448 if (Captures)
4449 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004450 if (NewStart.isInvalid())
4451 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004452
Alexey Bataevc0214e02016-02-16 12:13:49 +00004453 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4454 ExprResult SavedUpdate = Update;
4455 ExprResult UpdateVal;
4456 if (VarRef.get()->getType()->isOverloadableType() ||
4457 NewStart.get()->getType()->isOverloadableType() ||
4458 Update.get()->getType()->isOverloadableType()) {
4459 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4460 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4461 Update =
4462 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4463 if (Update.isUsable()) {
4464 UpdateVal =
4465 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4466 VarRef.get(), SavedUpdate.get());
4467 if (UpdateVal.isUsable()) {
4468 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4469 UpdateVal.get());
4470 }
4471 }
4472 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4473 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004474
Alexey Bataevc0214e02016-02-16 12:13:49 +00004475 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4476 if (!Update.isUsable() || !UpdateVal.isUsable()) {
4477 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4478 NewStart.get(), SavedUpdate.get());
4479 if (!Update.isUsable())
4480 return ExprError();
4481
Alexey Bataev11481f52016-02-17 10:29:05 +00004482 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4483 VarRef.get()->getType())) {
4484 Update = SemaRef.PerformImplicitConversion(
4485 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4486 if (!Update.isUsable())
4487 return ExprError();
4488 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00004489
4490 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4491 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004492 return Update;
4493}
4494
4495/// \brief Convert integer expression \a E to make it have at least \a Bits
4496/// bits.
4497static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
4498 Sema &SemaRef) {
4499 if (E == nullptr)
4500 return ExprError();
4501 auto &C = SemaRef.Context;
4502 QualType OldType = E->getType();
4503 unsigned HasBits = C.getTypeSize(OldType);
4504 if (HasBits >= Bits)
4505 return ExprResult(E);
4506 // OK to convert to signed, because new type has more bits than old.
4507 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4508 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4509 true);
4510}
4511
4512/// \brief Check if the given expression \a E is a constant integer that fits
4513/// into \a Bits bits.
4514static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4515 if (E == nullptr)
4516 return false;
4517 llvm::APSInt Result;
4518 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4519 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4520 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004521}
4522
Alexey Bataev5a3af132016-03-29 08:58:54 +00004523/// Build preinits statement for the given declarations.
4524static Stmt *buildPreInits(ASTContext &Context,
4525 SmallVectorImpl<Decl *> &PreInits) {
4526 if (!PreInits.empty()) {
4527 return new (Context) DeclStmt(
4528 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
4529 SourceLocation(), SourceLocation());
4530 }
4531 return nullptr;
4532}
4533
4534/// Build preinits statement for the given declarations.
4535static Stmt *buildPreInits(ASTContext &Context,
4536 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4537 if (!Captures.empty()) {
4538 SmallVector<Decl *, 16> PreInits;
4539 for (auto &Pair : Captures)
4540 PreInits.push_back(Pair.second->getDecl());
4541 return buildPreInits(Context, PreInits);
4542 }
4543 return nullptr;
4544}
4545
4546/// Build postupdate expression for the given list of postupdates expressions.
4547static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
4548 Expr *PostUpdate = nullptr;
4549 if (!PostUpdates.empty()) {
4550 for (auto *E : PostUpdates) {
4551 Expr *ConvE = S.BuildCStyleCastExpr(
4552 E->getExprLoc(),
4553 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
4554 E->getExprLoc(), E)
4555 .get();
4556 PostUpdate = PostUpdate
4557 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
4558 PostUpdate, ConvE)
4559 .get()
4560 : ConvE;
4561 }
4562 }
4563 return PostUpdate;
4564}
4565
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004566/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00004567/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4568/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004569static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00004570CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
4571 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4572 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004573 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00004574 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004575 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004576 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004577 // Found 'collapse' clause - calculate collapse number.
4578 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004579 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004580 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004581 }
4582 if (OrderedLoopCountExpr) {
4583 // Found 'ordered' clause - calculate collapse number.
4584 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004585 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4586 if (Result.getLimitedValue() < NestedLoopCount) {
4587 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4588 diag::err_omp_wrong_ordered_loop_count)
4589 << OrderedLoopCountExpr->getSourceRange();
4590 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4591 diag::note_collapse_loop_count)
4592 << CollapseLoopCountExpr->getSourceRange();
4593 }
4594 NestedLoopCount = Result.getLimitedValue();
4595 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004596 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004597 // This is helper routine for loop directives (e.g., 'for', 'simd',
4598 // 'for simd', etc.).
Alexey Bataev5a3af132016-03-29 08:58:54 +00004599 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004600 SmallVector<LoopIterationSpace, 4> IterSpaces;
4601 IterSpaces.resize(NestedLoopCount);
4602 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004603 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004604 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004605 NestedLoopCount, CollapseLoopCountExpr,
4606 OrderedLoopCountExpr, VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004607 IterSpaces[Cnt], Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004608 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004609 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004610 // OpenMP [2.8.1, simd construct, Restrictions]
4611 // All loops associated with the construct must be perfectly nested; that
4612 // is, there must be no intervening code nor any OpenMP directive between
4613 // any two loops.
4614 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004615 }
4616
Alexander Musmana5f070a2014-10-01 06:03:56 +00004617 Built.clear(/* size */ NestedLoopCount);
4618
4619 if (SemaRef.CurContext->isDependentContext())
4620 return NestedLoopCount;
4621
4622 // An example of what is generated for the following code:
4623 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004624 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004625 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004626 // for (k = 0; k < NK; ++k)
4627 // for (j = J0; j < NJ; j+=2) {
4628 // <loop body>
4629 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004630 //
4631 // We generate the code below.
4632 // Note: the loop body may be outlined in CodeGen.
4633 // Note: some counters may be C++ classes, operator- is used to find number of
4634 // iterations and operator+= to calculate counter value.
4635 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4636 // or i64 is currently supported).
4637 //
4638 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4639 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4640 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4641 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4642 // // similar updates for vars in clauses (e.g. 'linear')
4643 // <loop body (using local i and j)>
4644 // }
4645 // i = NI; // assign final values of counters
4646 // j = NJ;
4647 //
4648
4649 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4650 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004651 // Precondition tests if there is at least one iteration (all conditions are
4652 // true).
4653 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004654 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004655 ExprResult LastIteration32 = WidenIterationCount(
4656 32 /* Bits */, SemaRef.PerformImplicitConversion(
4657 N0->IgnoreImpCasts(), N0->getType(),
4658 Sema::AA_Converting, /*AllowExplicit=*/true)
4659 .get(),
4660 SemaRef);
4661 ExprResult LastIteration64 = WidenIterationCount(
4662 64 /* Bits */, SemaRef.PerformImplicitConversion(
4663 N0->IgnoreImpCasts(), N0->getType(),
4664 Sema::AA_Converting, /*AllowExplicit=*/true)
4665 .get(),
4666 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004667
4668 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4669 return NestedLoopCount;
4670
4671 auto &C = SemaRef.Context;
4672 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4673
4674 Scope *CurScope = DSA.getCurScope();
4675 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004676 if (PreCond.isUsable()) {
4677 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
4678 PreCond.get(), IterSpaces[Cnt].PreCond);
4679 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004680 auto N = IterSpaces[Cnt].NumIterations;
4681 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4682 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004683 LastIteration32 = SemaRef.BuildBinOp(
4684 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
4685 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4686 Sema::AA_Converting,
4687 /*AllowExplicit=*/true)
4688 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004689 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004690 LastIteration64 = SemaRef.BuildBinOp(
4691 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
4692 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4693 Sema::AA_Converting,
4694 /*AllowExplicit=*/true)
4695 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004696 }
4697
4698 // Choose either the 32-bit or 64-bit version.
4699 ExprResult LastIteration = LastIteration64;
4700 if (LastIteration32.isUsable() &&
4701 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4702 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4703 FitsInto(
4704 32 /* Bits */,
4705 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4706 LastIteration64.get(), SemaRef)))
4707 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00004708 QualType VType = LastIteration.get()->getType();
4709 QualType RealVType = VType;
4710 QualType StrideVType = VType;
4711 if (isOpenMPTaskLoopDirective(DKind)) {
4712 VType =
4713 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
4714 StrideVType =
4715 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
4716 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004717
4718 if (!LastIteration.isUsable())
4719 return 0;
4720
4721 // Save the number of iterations.
4722 ExprResult NumIterations = LastIteration;
4723 {
4724 LastIteration = SemaRef.BuildBinOp(
4725 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
4726 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4727 if (!LastIteration.isUsable())
4728 return 0;
4729 }
4730
4731 // Calculate the last iteration number beforehand instead of doing this on
4732 // each iteration. Do not do this if the number of iterations may be kfold-ed.
4733 llvm::APSInt Result;
4734 bool IsConstant =
4735 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4736 ExprResult CalcLastIteration;
4737 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004738 ExprResult SaveRef =
4739 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004740 LastIteration = SaveRef;
4741
4742 // Prepare SaveRef + 1.
4743 NumIterations = SemaRef.BuildBinOp(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004744 CurScope, SourceLocation(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00004745 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4746 if (!NumIterations.isUsable())
4747 return 0;
4748 }
4749
4750 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4751
Alexander Musmanc6388682014-12-15 07:07:06 +00004752 // Build variables passed into runtime, nesessary for worksharing directives.
4753 ExprResult LB, UB, IL, ST, EUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004754 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4755 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004756 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004757 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4758 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004759 SemaRef.AddInitializerToDecl(
4760 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4761 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4762
4763 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004764 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4765 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004766 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
4767 /*DirectInit*/ false,
4768 /*TypeMayContainAuto*/ false);
4769
4770 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4771 // This will be used to implement clause 'lastprivate'.
4772 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00004773 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4774 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004775 SemaRef.AddInitializerToDecl(
4776 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4777 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4778
4779 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00004780 VarDecl *STDecl =
4781 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
4782 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004783 SemaRef.AddInitializerToDecl(
4784 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4785 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4786
4787 // Build expression: UB = min(UB, LastIteration)
4788 // It is nesessary for CodeGen of directives with static scheduling.
4789 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4790 UB.get(), LastIteration.get());
4791 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4792 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4793 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4794 CondOp.get());
4795 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
4796 }
4797
4798 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004799 ExprResult IV;
4800 ExprResult Init;
4801 {
Alexey Bataev7292c292016-04-25 12:22:29 +00004802 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
4803 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004804 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004805 isOpenMPTaskLoopDirective(DKind) ||
4806 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004807 ? LB.get()
4808 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4809 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4810 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004811 }
4812
Alexander Musmanc6388682014-12-15 07:07:06 +00004813 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004814 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00004815 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004816 (isOpenMPWorksharingDirective(DKind) ||
4817 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004818 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4819 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4820 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004821
4822 // Loop increment (IV = IV + 1)
4823 SourceLocation IncLoc;
4824 ExprResult Inc =
4825 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4826 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4827 if (!Inc.isUsable())
4828 return 0;
4829 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00004830 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4831 if (!Inc.isUsable())
4832 return 0;
4833
4834 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4835 // Used for directives with static scheduling.
4836 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004837 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4838 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004839 // LB + ST
4840 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4841 if (!NextLB.isUsable())
4842 return 0;
4843 // LB = LB + ST
4844 NextLB =
4845 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4846 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4847 if (!NextLB.isUsable())
4848 return 0;
4849 // UB + ST
4850 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4851 if (!NextUB.isUsable())
4852 return 0;
4853 // UB = UB + ST
4854 NextUB =
4855 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4856 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4857 if (!NextUB.isUsable())
4858 return 0;
4859 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004860
4861 // Build updates and final values of the loop counters.
4862 bool HasErrors = false;
4863 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004864 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004865 Built.Updates.resize(NestedLoopCount);
4866 Built.Finals.resize(NestedLoopCount);
4867 {
4868 ExprResult Div;
4869 // Go from inner nested loop to outer.
4870 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4871 LoopIterationSpace &IS = IterSpaces[Cnt];
4872 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4873 // Build: Iter = (IV / Div) % IS.NumIters
4874 // where Div is product of previous iterations' IS.NumIters.
4875 ExprResult Iter;
4876 if (Div.isUsable()) {
4877 Iter =
4878 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4879 } else {
4880 Iter = IV;
4881 assert((Cnt == (int)NestedLoopCount - 1) &&
4882 "unusable div expected on first iteration only");
4883 }
4884
4885 if (Cnt != 0 && Iter.isUsable())
4886 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4887 IS.NumIterations);
4888 if (!Iter.isUsable()) {
4889 HasErrors = true;
4890 break;
4891 }
4892
Alexey Bataev39f915b82015-05-08 10:41:21 +00004893 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004894 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
4895 auto *CounterVar = buildDeclRefExpr(SemaRef, VD, IS.CounterVar->getType(),
4896 IS.CounterVar->getExprLoc(),
4897 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004898 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004899 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004900 if (!Init.isUsable()) {
4901 HasErrors = true;
4902 break;
4903 }
Alexey Bataev5a3af132016-03-29 08:58:54 +00004904 ExprResult Update = BuildCounterUpdate(
4905 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
4906 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004907 if (!Update.isUsable()) {
4908 HasErrors = true;
4909 break;
4910 }
4911
4912 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4913 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00004914 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004915 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004916 if (!Final.isUsable()) {
4917 HasErrors = true;
4918 break;
4919 }
4920
4921 // Build Div for the next iteration: Div <- Div * IS.NumIters
4922 if (Cnt != 0) {
4923 if (Div.isUnset())
4924 Div = IS.NumIterations;
4925 else
4926 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4927 IS.NumIterations);
4928
4929 // Add parentheses (for debugging purposes only).
4930 if (Div.isUsable())
4931 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
4932 if (!Div.isUsable()) {
4933 HasErrors = true;
4934 break;
4935 }
4936 }
4937 if (!Update.isUsable() || !Final.isUsable()) {
4938 HasErrors = true;
4939 break;
4940 }
4941 // Save results
4942 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004943 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004944 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004945 Built.Updates[Cnt] = Update.get();
4946 Built.Finals[Cnt] = Final.get();
4947 }
4948 }
4949
4950 if (HasErrors)
4951 return 0;
4952
4953 // Save results
4954 Built.IterationVarRef = IV.get();
4955 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004956 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004957 Built.CalcLastIteration =
4958 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004959 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00004960 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004961 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004962 Built.Init = Init.get();
4963 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004964 Built.LB = LB.get();
4965 Built.UB = UB.get();
4966 Built.IL = IL.get();
4967 Built.ST = ST.get();
4968 Built.EUB = EUB.get();
4969 Built.NLB = NextLB.get();
4970 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004971
Alexey Bataevabfc0692014-06-25 06:52:00 +00004972 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004973}
4974
Alexey Bataev10e775f2015-07-30 11:36:16 +00004975static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004976 auto CollapseClauses =
4977 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4978 if (CollapseClauses.begin() != CollapseClauses.end())
4979 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004980 return nullptr;
4981}
4982
Alexey Bataev10e775f2015-07-30 11:36:16 +00004983static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004984 auto OrderedClauses =
4985 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4986 if (OrderedClauses.begin() != OrderedClauses.end())
4987 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004988 return nullptr;
4989}
4990
Alexey Bataev66b15b52015-08-21 11:14:16 +00004991static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
4992 const Expr *Safelen) {
4993 llvm::APSInt SimdlenRes, SafelenRes;
4994 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
4995 Simdlen->isInstantiationDependent() ||
4996 Simdlen->containsUnexpandedParameterPack())
4997 return false;
4998 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
4999 Safelen->isInstantiationDependent() ||
5000 Safelen->containsUnexpandedParameterPack())
5001 return false;
5002 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
5003 Safelen->EvaluateAsInt(SafelenRes, S.Context);
5004 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5005 // If both simdlen and safelen clauses are specified, the value of the simdlen
5006 // parameter must be less than or equal to the value of the safelen parameter.
5007 if (SimdlenRes > SafelenRes) {
5008 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
5009 << Simdlen->getSourceRange() << Safelen->getSourceRange();
5010 return true;
5011 }
5012 return false;
5013}
5014
Alexey Bataev4acb8592014-07-07 13:01:15 +00005015StmtResult Sema::ActOnOpenMPSimdDirective(
5016 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5017 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005018 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005019 if (!AStmt)
5020 return StmtError();
5021
5022 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005023 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005024 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5025 // define the nested loops number.
5026 unsigned NestedLoopCount = CheckOpenMPLoop(
5027 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5028 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005029 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005030 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005031
Alexander Musmana5f070a2014-10-01 06:03:56 +00005032 assert((CurContext->isDependentContext() || B.builtAll()) &&
5033 "omp simd loop exprs were not built");
5034
Alexander Musman3276a272015-03-21 10:12:56 +00005035 if (!CurContext->isDependentContext()) {
5036 // Finalize the clauses that need pre-built expressions for CodeGen.
5037 for (auto C : Clauses) {
5038 if (auto LC = dyn_cast<OMPLinearClause>(C))
5039 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005040 B.NumIterations, *this, CurScope,
5041 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00005042 return StmtError();
5043 }
5044 }
5045
Alexey Bataev66b15b52015-08-21 11:14:16 +00005046 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5047 // If both simdlen and safelen clauses are specified, the value of the simdlen
5048 // parameter must be less than or equal to the value of the safelen parameter.
5049 OMPSafelenClause *Safelen = nullptr;
5050 OMPSimdlenClause *Simdlen = nullptr;
5051 for (auto *Clause : Clauses) {
5052 if (Clause->getClauseKind() == OMPC_safelen)
5053 Safelen = cast<OMPSafelenClause>(Clause);
5054 else if (Clause->getClauseKind() == OMPC_simdlen)
5055 Simdlen = cast<OMPSimdlenClause>(Clause);
5056 if (Safelen && Simdlen)
5057 break;
5058 }
5059 if (Simdlen && Safelen &&
5060 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5061 Safelen->getSafelen()))
5062 return StmtError();
5063
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005064 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005065 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5066 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005067}
5068
Alexey Bataev4acb8592014-07-07 13:01:15 +00005069StmtResult Sema::ActOnOpenMPForDirective(
5070 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5071 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005072 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005073 if (!AStmt)
5074 return StmtError();
5075
5076 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005077 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005078 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5079 // define the nested loops number.
5080 unsigned NestedLoopCount = CheckOpenMPLoop(
5081 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5082 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005083 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005084 return StmtError();
5085
Alexander Musmana5f070a2014-10-01 06:03:56 +00005086 assert((CurContext->isDependentContext() || B.builtAll()) &&
5087 "omp for loop exprs were not built");
5088
Alexey Bataev54acd402015-08-04 11:18:19 +00005089 if (!CurContext->isDependentContext()) {
5090 // Finalize the clauses that need pre-built expressions for CodeGen.
5091 for (auto C : Clauses) {
5092 if (auto LC = dyn_cast<OMPLinearClause>(C))
5093 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005094 B.NumIterations, *this, CurScope,
5095 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005096 return StmtError();
5097 }
5098 }
5099
Alexey Bataevf29276e2014-06-18 04:14:57 +00005100 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005101 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005102 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005103}
5104
Alexander Musmanf82886e2014-09-18 05:12:34 +00005105StmtResult Sema::ActOnOpenMPForSimdDirective(
5106 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5107 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005108 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005109 if (!AStmt)
5110 return StmtError();
5111
5112 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005113 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005114 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5115 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00005116 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005117 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
5118 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5119 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005120 if (NestedLoopCount == 0)
5121 return StmtError();
5122
Alexander Musmanc6388682014-12-15 07:07:06 +00005123 assert((CurContext->isDependentContext() || B.builtAll()) &&
5124 "omp for simd loop exprs were not built");
5125
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005126 if (!CurContext->isDependentContext()) {
5127 // Finalize the clauses that need pre-built expressions for CodeGen.
5128 for (auto C : Clauses) {
5129 if (auto LC = dyn_cast<OMPLinearClause>(C))
5130 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005131 B.NumIterations, *this, CurScope,
5132 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005133 return StmtError();
5134 }
5135 }
5136
Alexey Bataev66b15b52015-08-21 11:14:16 +00005137 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5138 // If both simdlen and safelen clauses are specified, the value of the simdlen
5139 // parameter must be less than or equal to the value of the safelen parameter.
5140 OMPSafelenClause *Safelen = nullptr;
5141 OMPSimdlenClause *Simdlen = nullptr;
5142 for (auto *Clause : Clauses) {
5143 if (Clause->getClauseKind() == OMPC_safelen)
5144 Safelen = cast<OMPSafelenClause>(Clause);
5145 else if (Clause->getClauseKind() == OMPC_simdlen)
5146 Simdlen = cast<OMPSimdlenClause>(Clause);
5147 if (Safelen && Simdlen)
5148 break;
5149 }
5150 if (Simdlen && Safelen &&
5151 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5152 Safelen->getSafelen()))
5153 return StmtError();
5154
Alexander Musmanf82886e2014-09-18 05:12:34 +00005155 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005156 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5157 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005158}
5159
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005160StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5161 Stmt *AStmt,
5162 SourceLocation StartLoc,
5163 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005164 if (!AStmt)
5165 return StmtError();
5166
5167 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005168 auto BaseStmt = AStmt;
5169 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5170 BaseStmt = CS->getCapturedStmt();
5171 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5172 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005173 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005174 return StmtError();
5175 // All associated statements must be '#pragma omp section' except for
5176 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005177 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005178 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5179 if (SectionStmt)
5180 Diag(SectionStmt->getLocStart(),
5181 diag::err_omp_sections_substmt_not_section);
5182 return StmtError();
5183 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005184 cast<OMPSectionDirective>(SectionStmt)
5185 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005186 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005187 } else {
5188 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
5189 return StmtError();
5190 }
5191
5192 getCurFunction()->setHasBranchProtectedScope();
5193
Alexey Bataev25e5b442015-09-15 12:52:43 +00005194 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5195 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005196}
5197
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005198StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5199 SourceLocation StartLoc,
5200 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005201 if (!AStmt)
5202 return StmtError();
5203
5204 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005205
5206 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00005207 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005208
Alexey Bataev25e5b442015-09-15 12:52:43 +00005209 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5210 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005211}
5212
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005213StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5214 Stmt *AStmt,
5215 SourceLocation StartLoc,
5216 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005217 if (!AStmt)
5218 return StmtError();
5219
5220 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00005221
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005222 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00005223
Alexey Bataev3255bf32015-01-19 05:20:46 +00005224 // OpenMP [2.7.3, single Construct, Restrictions]
5225 // The copyprivate clause must not be used with the nowait clause.
5226 OMPClause *Nowait = nullptr;
5227 OMPClause *Copyprivate = nullptr;
5228 for (auto *Clause : Clauses) {
5229 if (Clause->getClauseKind() == OMPC_nowait)
5230 Nowait = Clause;
5231 else if (Clause->getClauseKind() == OMPC_copyprivate)
5232 Copyprivate = Clause;
5233 if (Copyprivate && Nowait) {
5234 Diag(Copyprivate->getLocStart(),
5235 diag::err_omp_single_copyprivate_with_nowait);
5236 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
5237 return StmtError();
5238 }
5239 }
5240
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005241 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5242}
5243
Alexander Musman80c22892014-07-17 08:54:58 +00005244StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5245 SourceLocation StartLoc,
5246 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005247 if (!AStmt)
5248 return StmtError();
5249
5250 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00005251
5252 getCurFunction()->setHasBranchProtectedScope();
5253
5254 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5255}
5256
Alexey Bataev28c75412015-12-15 08:19:24 +00005257StmtResult Sema::ActOnOpenMPCriticalDirective(
5258 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5259 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005260 if (!AStmt)
5261 return StmtError();
5262
5263 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005264
Alexey Bataev28c75412015-12-15 08:19:24 +00005265 bool ErrorFound = false;
5266 llvm::APSInt Hint;
5267 SourceLocation HintLoc;
5268 bool DependentHint = false;
5269 for (auto *C : Clauses) {
5270 if (C->getClauseKind() == OMPC_hint) {
5271 if (!DirName.getName()) {
5272 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
5273 ErrorFound = true;
5274 }
5275 Expr *E = cast<OMPHintClause>(C)->getHint();
5276 if (E->isTypeDependent() || E->isValueDependent() ||
5277 E->isInstantiationDependent())
5278 DependentHint = true;
5279 else {
5280 Hint = E->EvaluateKnownConstInt(Context);
5281 HintLoc = C->getLocStart();
5282 }
5283 }
5284 }
5285 if (ErrorFound)
5286 return StmtError();
5287 auto Pair = DSAStack->getCriticalWithHint(DirName);
5288 if (Pair.first && DirName.getName() && !DependentHint) {
5289 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5290 Diag(StartLoc, diag::err_omp_critical_with_hint);
5291 if (HintLoc.isValid()) {
5292 Diag(HintLoc, diag::note_omp_critical_hint_here)
5293 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
5294 } else
5295 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
5296 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
5297 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
5298 << 1
5299 << C->getHint()->EvaluateKnownConstInt(Context).toString(
5300 /*Radix=*/10, /*Signed=*/false);
5301 } else
5302 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
5303 }
5304 }
5305
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005306 getCurFunction()->setHasBranchProtectedScope();
5307
Alexey Bataev28c75412015-12-15 08:19:24 +00005308 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5309 Clauses, AStmt);
5310 if (!Pair.first && DirName.getName() && !DependentHint)
5311 DSAStack->addCriticalWithHint(Dir, Hint);
5312 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005313}
5314
Alexey Bataev4acb8592014-07-07 13:01:15 +00005315StmtResult Sema::ActOnOpenMPParallelForDirective(
5316 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5317 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005318 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005319 if (!AStmt)
5320 return StmtError();
5321
Alexey Bataev4acb8592014-07-07 13:01:15 +00005322 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5323 // 1.2.2 OpenMP Language Terminology
5324 // Structured block - An executable statement with a single entry at the
5325 // top and a single exit at the bottom.
5326 // The point of exit cannot be a branch out of the structured block.
5327 // longjmp() and throw() must not violate the entry/exit criteria.
5328 CS->getCapturedDecl()->setNothrow();
5329
Alexander Musmanc6388682014-12-15 07:07:06 +00005330 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005331 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5332 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005333 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005334 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
5335 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5336 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005337 if (NestedLoopCount == 0)
5338 return StmtError();
5339
Alexander Musmana5f070a2014-10-01 06:03:56 +00005340 assert((CurContext->isDependentContext() || B.builtAll()) &&
5341 "omp parallel for loop exprs were not built");
5342
Alexey Bataev54acd402015-08-04 11:18:19 +00005343 if (!CurContext->isDependentContext()) {
5344 // Finalize the clauses that need pre-built expressions for CodeGen.
5345 for (auto C : Clauses) {
5346 if (auto LC = dyn_cast<OMPLinearClause>(C))
5347 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005348 B.NumIterations, *this, CurScope,
5349 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005350 return StmtError();
5351 }
5352 }
5353
Alexey Bataev4acb8592014-07-07 13:01:15 +00005354 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005355 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005356 NestedLoopCount, Clauses, AStmt, B,
5357 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00005358}
5359
Alexander Musmane4e893b2014-09-23 09:33:00 +00005360StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5361 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5362 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005363 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005364 if (!AStmt)
5365 return StmtError();
5366
Alexander Musmane4e893b2014-09-23 09:33:00 +00005367 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5368 // 1.2.2 OpenMP Language Terminology
5369 // Structured block - An executable statement with a single entry at the
5370 // top and a single exit at the bottom.
5371 // The point of exit cannot be a branch out of the structured block.
5372 // longjmp() and throw() must not violate the entry/exit criteria.
5373 CS->getCapturedDecl()->setNothrow();
5374
Alexander Musmanc6388682014-12-15 07:07:06 +00005375 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005376 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5377 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00005378 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005379 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
5380 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5381 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005382 if (NestedLoopCount == 0)
5383 return StmtError();
5384
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005385 if (!CurContext->isDependentContext()) {
5386 // Finalize the clauses that need pre-built expressions for CodeGen.
5387 for (auto C : Clauses) {
5388 if (auto LC = dyn_cast<OMPLinearClause>(C))
5389 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005390 B.NumIterations, *this, CurScope,
5391 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005392 return StmtError();
5393 }
5394 }
5395
Alexey Bataev66b15b52015-08-21 11:14:16 +00005396 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5397 // If both simdlen and safelen clauses are specified, the value of the simdlen
5398 // parameter must be less than or equal to the value of the safelen parameter.
5399 OMPSafelenClause *Safelen = nullptr;
5400 OMPSimdlenClause *Simdlen = nullptr;
5401 for (auto *Clause : Clauses) {
5402 if (Clause->getClauseKind() == OMPC_safelen)
5403 Safelen = cast<OMPSafelenClause>(Clause);
5404 else if (Clause->getClauseKind() == OMPC_simdlen)
5405 Simdlen = cast<OMPSimdlenClause>(Clause);
5406 if (Safelen && Simdlen)
5407 break;
5408 }
5409 if (Simdlen && Safelen &&
5410 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5411 Safelen->getSafelen()))
5412 return StmtError();
5413
Alexander Musmane4e893b2014-09-23 09:33:00 +00005414 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005415 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00005416 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005417}
5418
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005419StmtResult
5420Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
5421 Stmt *AStmt, SourceLocation StartLoc,
5422 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005423 if (!AStmt)
5424 return StmtError();
5425
5426 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005427 auto BaseStmt = AStmt;
5428 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5429 BaseStmt = CS->getCapturedStmt();
5430 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5431 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005432 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005433 return StmtError();
5434 // All associated statements must be '#pragma omp section' except for
5435 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005436 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005437 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5438 if (SectionStmt)
5439 Diag(SectionStmt->getLocStart(),
5440 diag::err_omp_parallel_sections_substmt_not_section);
5441 return StmtError();
5442 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005443 cast<OMPSectionDirective>(SectionStmt)
5444 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005445 }
5446 } else {
5447 Diag(AStmt->getLocStart(),
5448 diag::err_omp_parallel_sections_not_compound_stmt);
5449 return StmtError();
5450 }
5451
5452 getCurFunction()->setHasBranchProtectedScope();
5453
Alexey Bataev25e5b442015-09-15 12:52:43 +00005454 return OMPParallelSectionsDirective::Create(
5455 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005456}
5457
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005458StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5459 Stmt *AStmt, SourceLocation StartLoc,
5460 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005461 if (!AStmt)
5462 return StmtError();
5463
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005464 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5465 // 1.2.2 OpenMP Language Terminology
5466 // Structured block - An executable statement with a single entry at the
5467 // top and a single exit at the bottom.
5468 // The point of exit cannot be a branch out of the structured block.
5469 // longjmp() and throw() must not violate the entry/exit criteria.
5470 CS->getCapturedDecl()->setNothrow();
5471
5472 getCurFunction()->setHasBranchProtectedScope();
5473
Alexey Bataev25e5b442015-09-15 12:52:43 +00005474 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5475 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005476}
5477
Alexey Bataev68446b72014-07-18 07:47:19 +00005478StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5479 SourceLocation EndLoc) {
5480 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5481}
5482
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00005483StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5484 SourceLocation EndLoc) {
5485 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5486}
5487
Alexey Bataev2df347a2014-07-18 10:17:07 +00005488StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5489 SourceLocation EndLoc) {
5490 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5491}
5492
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005493StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
5494 SourceLocation StartLoc,
5495 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005496 if (!AStmt)
5497 return StmtError();
5498
5499 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005500
5501 getCurFunction()->setHasBranchProtectedScope();
5502
5503 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
5504}
5505
Alexey Bataev6125da92014-07-21 11:26:11 +00005506StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5507 SourceLocation StartLoc,
5508 SourceLocation EndLoc) {
5509 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5510 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5511}
5512
Alexey Bataev346265e2015-09-25 10:37:12 +00005513StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5514 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005515 SourceLocation StartLoc,
5516 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00005517 OMPClause *DependFound = nullptr;
5518 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005519 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005520 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00005521 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005522 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005523 for (auto *C : Clauses) {
5524 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5525 DependFound = C;
5526 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5527 if (DependSourceClause) {
5528 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5529 << getOpenMPDirectiveName(OMPD_ordered)
5530 << getOpenMPClauseName(OMPC_depend) << 2;
5531 ErrorFound = true;
5532 } else
5533 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005534 if (DependSinkClause) {
5535 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5536 << 0;
5537 ErrorFound = true;
5538 }
5539 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5540 if (DependSourceClause) {
5541 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5542 << 1;
5543 ErrorFound = true;
5544 }
5545 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00005546 }
5547 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00005548 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005549 else if (C->getClauseKind() == OMPC_simd)
5550 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00005551 }
Alexey Bataeveb482352015-12-18 05:05:56 +00005552 if (!ErrorFound && !SC &&
5553 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005554 // OpenMP [2.8.1,simd Construct, Restrictions]
5555 // An ordered construct with the simd clause is the only OpenMP construct
5556 // that can appear in the simd region.
5557 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00005558 ErrorFound = true;
5559 } else if (DependFound && (TC || SC)) {
5560 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
5561 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
5562 ErrorFound = true;
5563 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
5564 Diag(DependFound->getLocStart(),
5565 diag::err_omp_ordered_directive_without_param);
5566 ErrorFound = true;
5567 } else if (TC || Clauses.empty()) {
5568 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
5569 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
5570 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
5571 << (TC != nullptr);
5572 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
5573 ErrorFound = true;
5574 }
5575 }
5576 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005577 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00005578
5579 if (AStmt) {
5580 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5581
5582 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005583 }
Alexey Bataev346265e2015-09-25 10:37:12 +00005584
5585 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005586}
5587
Alexey Bataev1d160b12015-03-13 12:27:31 +00005588namespace {
5589/// \brief Helper class for checking expression in 'omp atomic [update]'
5590/// construct.
5591class OpenMPAtomicUpdateChecker {
5592 /// \brief Error results for atomic update expressions.
5593 enum ExprAnalysisErrorCode {
5594 /// \brief A statement is not an expression statement.
5595 NotAnExpression,
5596 /// \brief Expression is not builtin binary or unary operation.
5597 NotABinaryOrUnaryExpression,
5598 /// \brief Unary operation is not post-/pre- increment/decrement operation.
5599 NotAnUnaryIncDecExpression,
5600 /// \brief An expression is not of scalar type.
5601 NotAScalarType,
5602 /// \brief A binary operation is not an assignment operation.
5603 NotAnAssignmentOp,
5604 /// \brief RHS part of the binary operation is not a binary expression.
5605 NotABinaryExpression,
5606 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
5607 /// expression.
5608 NotABinaryOperator,
5609 /// \brief RHS binary operation does not have reference to the updated LHS
5610 /// part.
5611 NotAnUpdateExpression,
5612 /// \brief No errors is found.
5613 NoError
5614 };
5615 /// \brief Reference to Sema.
5616 Sema &SemaRef;
5617 /// \brief A location for note diagnostics (when error is found).
5618 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005619 /// \brief 'x' lvalue part of the source atomic expression.
5620 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005621 /// \brief 'expr' rvalue part of the source atomic expression.
5622 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005623 /// \brief Helper expression of the form
5624 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5625 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5626 Expr *UpdateExpr;
5627 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
5628 /// important for non-associative operations.
5629 bool IsXLHSInRHSPart;
5630 BinaryOperatorKind Op;
5631 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005632 /// \brief true if the source expression is a postfix unary operation, false
5633 /// if it is a prefix unary operation.
5634 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005635
5636public:
5637 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00005638 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00005639 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00005640 /// \brief Check specified statement that it is suitable for 'atomic update'
5641 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00005642 /// expression. If DiagId and NoteId == 0, then only check is performed
5643 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005644 /// \param DiagId Diagnostic which should be emitted if error is found.
5645 /// \param NoteId Diagnostic note for the main error message.
5646 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00005647 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005648 /// \brief Return the 'x' lvalue part of the source atomic expression.
5649 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00005650 /// \brief Return the 'expr' rvalue part of the source atomic expression.
5651 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00005652 /// \brief Return the update expression used in calculation of the updated
5653 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5654 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5655 Expr *getUpdateExpr() const { return UpdateExpr; }
5656 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
5657 /// false otherwise.
5658 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5659
Alexey Bataevb78ca832015-04-01 03:33:17 +00005660 /// \brief true if the source expression is a postfix unary operation, false
5661 /// if it is a prefix unary operation.
5662 bool isPostfixUpdate() const { return IsPostfixUpdate; }
5663
Alexey Bataev1d160b12015-03-13 12:27:31 +00005664private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00005665 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5666 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005667};
5668} // namespace
5669
5670bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5671 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5672 ExprAnalysisErrorCode ErrorFound = NoError;
5673 SourceLocation ErrorLoc, NoteLoc;
5674 SourceRange ErrorRange, NoteRange;
5675 // Allowed constructs are:
5676 // x = x binop expr;
5677 // x = expr binop x;
5678 if (AtomicBinOp->getOpcode() == BO_Assign) {
5679 X = AtomicBinOp->getLHS();
5680 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
5681 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5682 if (AtomicInnerBinOp->isMultiplicativeOp() ||
5683 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5684 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005685 Op = AtomicInnerBinOp->getOpcode();
5686 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005687 auto *LHS = AtomicInnerBinOp->getLHS();
5688 auto *RHS = AtomicInnerBinOp->getRHS();
5689 llvm::FoldingSetNodeID XId, LHSId, RHSId;
5690 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
5691 /*Canonical=*/true);
5692 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
5693 /*Canonical=*/true);
5694 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
5695 /*Canonical=*/true);
5696 if (XId == LHSId) {
5697 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005698 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005699 } else if (XId == RHSId) {
5700 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005701 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005702 } else {
5703 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5704 ErrorRange = AtomicInnerBinOp->getSourceRange();
5705 NoteLoc = X->getExprLoc();
5706 NoteRange = X->getSourceRange();
5707 ErrorFound = NotAnUpdateExpression;
5708 }
5709 } else {
5710 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5711 ErrorRange = AtomicInnerBinOp->getSourceRange();
5712 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5713 NoteRange = SourceRange(NoteLoc, NoteLoc);
5714 ErrorFound = NotABinaryOperator;
5715 }
5716 } else {
5717 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5718 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5719 ErrorFound = NotABinaryExpression;
5720 }
5721 } else {
5722 ErrorLoc = AtomicBinOp->getExprLoc();
5723 ErrorRange = AtomicBinOp->getSourceRange();
5724 NoteLoc = AtomicBinOp->getOperatorLoc();
5725 NoteRange = SourceRange(NoteLoc, NoteLoc);
5726 ErrorFound = NotAnAssignmentOp;
5727 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005728 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005729 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5730 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5731 return true;
5732 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005733 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005734 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005735}
5736
5737bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5738 unsigned NoteId) {
5739 ExprAnalysisErrorCode ErrorFound = NoError;
5740 SourceLocation ErrorLoc, NoteLoc;
5741 SourceRange ErrorRange, NoteRange;
5742 // Allowed constructs are:
5743 // x++;
5744 // x--;
5745 // ++x;
5746 // --x;
5747 // x binop= expr;
5748 // x = x binop expr;
5749 // x = expr binop x;
5750 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5751 AtomicBody = AtomicBody->IgnoreParenImpCasts();
5752 if (AtomicBody->getType()->isScalarType() ||
5753 AtomicBody->isInstantiationDependent()) {
5754 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5755 AtomicBody->IgnoreParenImpCasts())) {
5756 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005757 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00005758 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00005759 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005760 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005761 X = AtomicCompAssignOp->getLHS();
5762 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005763 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5764 AtomicBody->IgnoreParenImpCasts())) {
5765 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005766 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
5767 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005768 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00005769 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
5770 // Check for Unary Operation
5771 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005772 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005773 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5774 OpLoc = AtomicUnaryOp->getOperatorLoc();
5775 X = AtomicUnaryOp->getSubExpr();
5776 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5777 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005778 } else {
5779 ErrorFound = NotAnUnaryIncDecExpression;
5780 ErrorLoc = AtomicUnaryOp->getExprLoc();
5781 ErrorRange = AtomicUnaryOp->getSourceRange();
5782 NoteLoc = AtomicUnaryOp->getOperatorLoc();
5783 NoteRange = SourceRange(NoteLoc, NoteLoc);
5784 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005785 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005786 ErrorFound = NotABinaryOrUnaryExpression;
5787 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5788 NoteRange = ErrorRange = AtomicBody->getSourceRange();
5789 }
5790 } else {
5791 ErrorFound = NotAScalarType;
5792 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
5793 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5794 }
5795 } else {
5796 ErrorFound = NotAnExpression;
5797 NoteLoc = ErrorLoc = S->getLocStart();
5798 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5799 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005800 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005801 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5802 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5803 return true;
5804 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005805 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005806 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005807 // Build an update expression of form 'OpaqueValueExpr(x) binop
5808 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
5809 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
5810 auto *OVEX = new (SemaRef.getASTContext())
5811 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
5812 auto *OVEExpr = new (SemaRef.getASTContext())
5813 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
5814 auto Update =
5815 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
5816 IsXLHSInRHSPart ? OVEExpr : OVEX);
5817 if (Update.isInvalid())
5818 return true;
5819 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
5820 Sema::AA_Casting);
5821 if (Update.isInvalid())
5822 return true;
5823 UpdateExpr = Update.get();
5824 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00005825 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005826}
5827
Alexey Bataev0162e452014-07-22 10:10:35 +00005828StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5829 Stmt *AStmt,
5830 SourceLocation StartLoc,
5831 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005832 if (!AStmt)
5833 return StmtError();
5834
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005835 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00005836 // 1.2.2 OpenMP Language Terminology
5837 // Structured block - An executable statement with a single entry at the
5838 // top and a single exit at the bottom.
5839 // The point of exit cannot be a branch out of the structured block.
5840 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00005841 OpenMPClauseKind AtomicKind = OMPC_unknown;
5842 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005843 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00005844 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00005845 C->getClauseKind() == OMPC_update ||
5846 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00005847 if (AtomicKind != OMPC_unknown) {
5848 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5849 << SourceRange(C->getLocStart(), C->getLocEnd());
5850 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5851 << getOpenMPClauseName(AtomicKind);
5852 } else {
5853 AtomicKind = C->getClauseKind();
5854 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005855 }
5856 }
5857 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005858
Alexey Bataev459dec02014-07-24 06:46:57 +00005859 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00005860 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5861 Body = EWC->getSubExpr();
5862
Alexey Bataev62cec442014-11-18 10:14:22 +00005863 Expr *X = nullptr;
5864 Expr *V = nullptr;
5865 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005866 Expr *UE = nullptr;
5867 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005868 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00005869 // OpenMP [2.12.6, atomic Construct]
5870 // In the next expressions:
5871 // * x and v (as applicable) are both l-value expressions with scalar type.
5872 // * During the execution of an atomic region, multiple syntactic
5873 // occurrences of x must designate the same storage location.
5874 // * Neither of v and expr (as applicable) may access the storage location
5875 // designated by x.
5876 // * Neither of x and expr (as applicable) may access the storage location
5877 // designated by v.
5878 // * expr is an expression with scalar type.
5879 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5880 // * binop, binop=, ++, and -- are not overloaded operators.
5881 // * The expression x binop expr must be numerically equivalent to x binop
5882 // (expr). This requirement is satisfied if the operators in expr have
5883 // precedence greater than binop, or by using parentheses around expr or
5884 // subexpressions of expr.
5885 // * The expression expr binop x must be numerically equivalent to (expr)
5886 // binop x. This requirement is satisfied if the operators in expr have
5887 // precedence equal to or greater than binop, or by using parentheses around
5888 // expr or subexpressions of expr.
5889 // * For forms that allow multiple occurrences of x, the number of times
5890 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00005891 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005892 enum {
5893 NotAnExpression,
5894 NotAnAssignmentOp,
5895 NotAScalarType,
5896 NotAnLValue,
5897 NoError
5898 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00005899 SourceLocation ErrorLoc, NoteLoc;
5900 SourceRange ErrorRange, NoteRange;
5901 // If clause is read:
5902 // v = x;
5903 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5904 auto AtomicBinOp =
5905 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5906 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5907 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5908 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5909 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5910 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5911 if (!X->isLValue() || !V->isLValue()) {
5912 auto NotLValueExpr = X->isLValue() ? V : X;
5913 ErrorFound = NotAnLValue;
5914 ErrorLoc = AtomicBinOp->getExprLoc();
5915 ErrorRange = AtomicBinOp->getSourceRange();
5916 NoteLoc = NotLValueExpr->getExprLoc();
5917 NoteRange = NotLValueExpr->getSourceRange();
5918 }
5919 } else if (!X->isInstantiationDependent() ||
5920 !V->isInstantiationDependent()) {
5921 auto NotScalarExpr =
5922 (X->isInstantiationDependent() || X->getType()->isScalarType())
5923 ? V
5924 : X;
5925 ErrorFound = NotAScalarType;
5926 ErrorLoc = AtomicBinOp->getExprLoc();
5927 ErrorRange = AtomicBinOp->getSourceRange();
5928 NoteLoc = NotScalarExpr->getExprLoc();
5929 NoteRange = NotScalarExpr->getSourceRange();
5930 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005931 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00005932 ErrorFound = NotAnAssignmentOp;
5933 ErrorLoc = AtomicBody->getExprLoc();
5934 ErrorRange = AtomicBody->getSourceRange();
5935 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5936 : AtomicBody->getExprLoc();
5937 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5938 : AtomicBody->getSourceRange();
5939 }
5940 } else {
5941 ErrorFound = NotAnExpression;
5942 NoteLoc = ErrorLoc = Body->getLocStart();
5943 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005944 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005945 if (ErrorFound != NoError) {
5946 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
5947 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005948 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5949 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00005950 return StmtError();
5951 } else if (CurContext->isDependentContext())
5952 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00005953 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005954 enum {
5955 NotAnExpression,
5956 NotAnAssignmentOp,
5957 NotAScalarType,
5958 NotAnLValue,
5959 NoError
5960 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005961 SourceLocation ErrorLoc, NoteLoc;
5962 SourceRange ErrorRange, NoteRange;
5963 // If clause is write:
5964 // x = expr;
5965 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5966 auto AtomicBinOp =
5967 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5968 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00005969 X = AtomicBinOp->getLHS();
5970 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00005971 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5972 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
5973 if (!X->isLValue()) {
5974 ErrorFound = NotAnLValue;
5975 ErrorLoc = AtomicBinOp->getExprLoc();
5976 ErrorRange = AtomicBinOp->getSourceRange();
5977 NoteLoc = X->getExprLoc();
5978 NoteRange = X->getSourceRange();
5979 }
5980 } else if (!X->isInstantiationDependent() ||
5981 !E->isInstantiationDependent()) {
5982 auto NotScalarExpr =
5983 (X->isInstantiationDependent() || X->getType()->isScalarType())
5984 ? E
5985 : X;
5986 ErrorFound = NotAScalarType;
5987 ErrorLoc = AtomicBinOp->getExprLoc();
5988 ErrorRange = AtomicBinOp->getSourceRange();
5989 NoteLoc = NotScalarExpr->getExprLoc();
5990 NoteRange = NotScalarExpr->getSourceRange();
5991 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005992 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00005993 ErrorFound = NotAnAssignmentOp;
5994 ErrorLoc = AtomicBody->getExprLoc();
5995 ErrorRange = AtomicBody->getSourceRange();
5996 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5997 : AtomicBody->getExprLoc();
5998 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5999 : AtomicBody->getSourceRange();
6000 }
6001 } else {
6002 ErrorFound = NotAnExpression;
6003 NoteLoc = ErrorLoc = Body->getLocStart();
6004 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006005 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00006006 if (ErrorFound != NoError) {
6007 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
6008 << ErrorRange;
6009 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6010 << NoteRange;
6011 return StmtError();
6012 } else if (CurContext->isDependentContext())
6013 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006014 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006015 // If clause is update:
6016 // x++;
6017 // x--;
6018 // ++x;
6019 // --x;
6020 // x binop= expr;
6021 // x = x binop expr;
6022 // x = expr binop x;
6023 OpenMPAtomicUpdateChecker Checker(*this);
6024 if (Checker.checkStatement(
6025 Body, (AtomicKind == OMPC_update)
6026 ? diag::err_omp_atomic_update_not_expression_statement
6027 : diag::err_omp_atomic_not_expression_statement,
6028 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00006029 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006030 if (!CurContext->isDependentContext()) {
6031 E = Checker.getExpr();
6032 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006033 UE = Checker.getUpdateExpr();
6034 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00006035 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006036 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006037 enum {
6038 NotAnAssignmentOp,
6039 NotACompoundStatement,
6040 NotTwoSubstatements,
6041 NotASpecificExpression,
6042 NoError
6043 } ErrorFound = NoError;
6044 SourceLocation ErrorLoc, NoteLoc;
6045 SourceRange ErrorRange, NoteRange;
6046 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6047 // If clause is a capture:
6048 // v = x++;
6049 // v = x--;
6050 // v = ++x;
6051 // v = --x;
6052 // v = x binop= expr;
6053 // v = x = x binop expr;
6054 // v = x = expr binop x;
6055 auto *AtomicBinOp =
6056 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6057 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6058 V = AtomicBinOp->getLHS();
6059 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6060 OpenMPAtomicUpdateChecker Checker(*this);
6061 if (Checker.checkStatement(
6062 Body, diag::err_omp_atomic_capture_not_expression_statement,
6063 diag::note_omp_atomic_update))
6064 return StmtError();
6065 E = Checker.getExpr();
6066 X = Checker.getX();
6067 UE = Checker.getUpdateExpr();
6068 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6069 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00006070 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006071 ErrorLoc = AtomicBody->getExprLoc();
6072 ErrorRange = AtomicBody->getSourceRange();
6073 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6074 : AtomicBody->getExprLoc();
6075 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6076 : AtomicBody->getSourceRange();
6077 ErrorFound = NotAnAssignmentOp;
6078 }
6079 if (ErrorFound != NoError) {
6080 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6081 << ErrorRange;
6082 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6083 return StmtError();
6084 } else if (CurContext->isDependentContext()) {
6085 UE = V = E = X = nullptr;
6086 }
6087 } else {
6088 // If clause is a capture:
6089 // { v = x; x = expr; }
6090 // { v = x; x++; }
6091 // { v = x; x--; }
6092 // { v = x; ++x; }
6093 // { v = x; --x; }
6094 // { v = x; x binop= expr; }
6095 // { v = x; x = x binop expr; }
6096 // { v = x; x = expr binop x; }
6097 // { x++; v = x; }
6098 // { x--; v = x; }
6099 // { ++x; v = x; }
6100 // { --x; v = x; }
6101 // { x binop= expr; v = x; }
6102 // { x = x binop expr; v = x; }
6103 // { x = expr binop x; v = x; }
6104 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6105 // Check that this is { expr1; expr2; }
6106 if (CS->size() == 2) {
6107 auto *First = CS->body_front();
6108 auto *Second = CS->body_back();
6109 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6110 First = EWC->getSubExpr()->IgnoreParenImpCasts();
6111 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6112 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6113 // Need to find what subexpression is 'v' and what is 'x'.
6114 OpenMPAtomicUpdateChecker Checker(*this);
6115 bool IsUpdateExprFound = !Checker.checkStatement(Second);
6116 BinaryOperator *BinOp = nullptr;
6117 if (IsUpdateExprFound) {
6118 BinOp = dyn_cast<BinaryOperator>(First);
6119 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6120 }
6121 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6122 // { v = x; x++; }
6123 // { v = x; x--; }
6124 // { v = x; ++x; }
6125 // { v = x; --x; }
6126 // { v = x; x binop= expr; }
6127 // { v = x; x = x binop expr; }
6128 // { v = x; x = expr binop x; }
6129 // Check that the first expression has form v = x.
6130 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6131 llvm::FoldingSetNodeID XId, PossibleXId;
6132 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6133 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6134 IsUpdateExprFound = XId == PossibleXId;
6135 if (IsUpdateExprFound) {
6136 V = BinOp->getLHS();
6137 X = Checker.getX();
6138 E = Checker.getExpr();
6139 UE = Checker.getUpdateExpr();
6140 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006141 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006142 }
6143 }
6144 if (!IsUpdateExprFound) {
6145 IsUpdateExprFound = !Checker.checkStatement(First);
6146 BinOp = nullptr;
6147 if (IsUpdateExprFound) {
6148 BinOp = dyn_cast<BinaryOperator>(Second);
6149 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6150 }
6151 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6152 // { x++; v = x; }
6153 // { x--; v = x; }
6154 // { ++x; v = x; }
6155 // { --x; v = x; }
6156 // { x binop= expr; v = x; }
6157 // { x = x binop expr; v = x; }
6158 // { x = expr binop x; v = x; }
6159 // Check that the second expression has form v = x.
6160 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6161 llvm::FoldingSetNodeID XId, PossibleXId;
6162 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6163 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6164 IsUpdateExprFound = XId == PossibleXId;
6165 if (IsUpdateExprFound) {
6166 V = BinOp->getLHS();
6167 X = Checker.getX();
6168 E = Checker.getExpr();
6169 UE = Checker.getUpdateExpr();
6170 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006171 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006172 }
6173 }
6174 }
6175 if (!IsUpdateExprFound) {
6176 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00006177 auto *FirstExpr = dyn_cast<Expr>(First);
6178 auto *SecondExpr = dyn_cast<Expr>(Second);
6179 if (!FirstExpr || !SecondExpr ||
6180 !(FirstExpr->isInstantiationDependent() ||
6181 SecondExpr->isInstantiationDependent())) {
6182 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6183 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006184 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00006185 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
6186 : First->getLocStart();
6187 NoteRange = ErrorRange = FirstBinOp
6188 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00006189 : SourceRange(ErrorLoc, ErrorLoc);
6190 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006191 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6192 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6193 ErrorFound = NotAnAssignmentOp;
6194 NoteLoc = ErrorLoc = SecondBinOp
6195 ? SecondBinOp->getOperatorLoc()
6196 : Second->getLocStart();
6197 NoteRange = ErrorRange =
6198 SecondBinOp ? SecondBinOp->getSourceRange()
6199 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00006200 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006201 auto *PossibleXRHSInFirst =
6202 FirstBinOp->getRHS()->IgnoreParenImpCasts();
6203 auto *PossibleXLHSInSecond =
6204 SecondBinOp->getLHS()->IgnoreParenImpCasts();
6205 llvm::FoldingSetNodeID X1Id, X2Id;
6206 PossibleXRHSInFirst->Profile(X1Id, Context,
6207 /*Canonical=*/true);
6208 PossibleXLHSInSecond->Profile(X2Id, Context,
6209 /*Canonical=*/true);
6210 IsUpdateExprFound = X1Id == X2Id;
6211 if (IsUpdateExprFound) {
6212 V = FirstBinOp->getLHS();
6213 X = SecondBinOp->getLHS();
6214 E = SecondBinOp->getRHS();
6215 UE = nullptr;
6216 IsXLHSInRHSPart = false;
6217 IsPostfixUpdate = true;
6218 } else {
6219 ErrorFound = NotASpecificExpression;
6220 ErrorLoc = FirstBinOp->getExprLoc();
6221 ErrorRange = FirstBinOp->getSourceRange();
6222 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6223 NoteRange = SecondBinOp->getRHS()->getSourceRange();
6224 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006225 }
6226 }
6227 }
6228 }
6229 } else {
6230 NoteLoc = ErrorLoc = Body->getLocStart();
6231 NoteRange = ErrorRange =
6232 SourceRange(Body->getLocStart(), Body->getLocStart());
6233 ErrorFound = NotTwoSubstatements;
6234 }
6235 } else {
6236 NoteLoc = ErrorLoc = Body->getLocStart();
6237 NoteRange = ErrorRange =
6238 SourceRange(Body->getLocStart(), Body->getLocStart());
6239 ErrorFound = NotACompoundStatement;
6240 }
6241 if (ErrorFound != NoError) {
6242 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6243 << ErrorRange;
6244 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6245 return StmtError();
6246 } else if (CurContext->isDependentContext()) {
6247 UE = V = E = X = nullptr;
6248 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006249 }
Alexey Bataevdea47612014-07-23 07:46:59 +00006250 }
Alexey Bataev0162e452014-07-22 10:10:35 +00006251
6252 getCurFunction()->setHasBranchProtectedScope();
6253
Alexey Bataev62cec442014-11-18 10:14:22 +00006254 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00006255 X, V, E, UE, IsXLHSInRHSPart,
6256 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00006257}
6258
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006259StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6260 Stmt *AStmt,
6261 SourceLocation StartLoc,
6262 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006263 if (!AStmt)
6264 return StmtError();
6265
Samuel Antao4af1b7b2015-12-02 17:44:43 +00006266 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6267 // 1.2.2 OpenMP Language Terminology
6268 // Structured block - An executable statement with a single entry at the
6269 // top and a single exit at the bottom.
6270 // The point of exit cannot be a branch out of the structured block.
6271 // longjmp() and throw() must not violate the entry/exit criteria.
6272 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006273
Alexey Bataev13314bf2014-10-09 04:18:56 +00006274 // OpenMP [2.16, Nesting of Regions]
6275 // If specified, a teams construct must be contained within a target
6276 // construct. That target construct must contain no statements or directives
6277 // outside of the teams construct.
6278 if (DSAStack->hasInnerTeamsRegion()) {
6279 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
6280 bool OMPTeamsFound = true;
6281 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
6282 auto I = CS->body_begin();
6283 while (I != CS->body_end()) {
6284 auto OED = dyn_cast<OMPExecutableDirective>(*I);
6285 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6286 OMPTeamsFound = false;
6287 break;
6288 }
6289 ++I;
6290 }
6291 assert(I != CS->body_end() && "Not found statement");
6292 S = *I;
6293 }
6294 if (!OMPTeamsFound) {
6295 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6296 Diag(DSAStack->getInnerTeamsRegionLoc(),
6297 diag::note_omp_nested_teams_construct_here);
6298 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
6299 << isa<OMPExecutableDirective>(S);
6300 return StmtError();
6301 }
6302 }
6303
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006304 getCurFunction()->setHasBranchProtectedScope();
6305
6306 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6307}
6308
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006309StmtResult
6310Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6311 Stmt *AStmt, SourceLocation StartLoc,
6312 SourceLocation EndLoc) {
6313 if (!AStmt)
6314 return StmtError();
6315
6316 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6317 // 1.2.2 OpenMP Language Terminology
6318 // Structured block - An executable statement with a single entry at the
6319 // top and a single exit at the bottom.
6320 // The point of exit cannot be a branch out of the structured block.
6321 // longjmp() and throw() must not violate the entry/exit criteria.
6322 CS->getCapturedDecl()->setNothrow();
6323
6324 getCurFunction()->setHasBranchProtectedScope();
6325
6326 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6327 AStmt);
6328}
6329
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006330StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6331 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6332 SourceLocation EndLoc,
6333 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6334 if (!AStmt)
6335 return StmtError();
6336
6337 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6338 // 1.2.2 OpenMP Language Terminology
6339 // Structured block - An executable statement with a single entry at the
6340 // top and a single exit at the bottom.
6341 // The point of exit cannot be a branch out of the structured block.
6342 // longjmp() and throw() must not violate the entry/exit criteria.
6343 CS->getCapturedDecl()->setNothrow();
6344
6345 OMPLoopDirective::HelperExprs B;
6346 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6347 // define the nested loops number.
6348 unsigned NestedLoopCount =
6349 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
6350 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6351 VarsWithImplicitDSA, B);
6352 if (NestedLoopCount == 0)
6353 return StmtError();
6354
6355 assert((CurContext->isDependentContext() || B.builtAll()) &&
6356 "omp target parallel for loop exprs were not built");
6357
6358 if (!CurContext->isDependentContext()) {
6359 // Finalize the clauses that need pre-built expressions for CodeGen.
6360 for (auto C : Clauses) {
6361 if (auto LC = dyn_cast<OMPLinearClause>(C))
6362 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006363 B.NumIterations, *this, CurScope,
6364 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006365 return StmtError();
6366 }
6367 }
6368
6369 getCurFunction()->setHasBranchProtectedScope();
6370 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
6371 NestedLoopCount, Clauses, AStmt,
6372 B, DSAStack->isCancelRegion());
6373}
6374
Samuel Antaodf67fc42016-01-19 19:15:56 +00006375/// \brief Check for existence of a map clause in the list of clauses.
6376static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
6377 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
6378 I != E; ++I) {
6379 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
6380 return true;
6381 }
6382 }
6383
6384 return false;
6385}
6386
Michael Wong65f367f2015-07-21 13:44:28 +00006387StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6388 Stmt *AStmt,
6389 SourceLocation StartLoc,
6390 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006391 if (!AStmt)
6392 return StmtError();
6393
6394 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6395
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006396 // OpenMP [2.10.1, Restrictions, p. 97]
6397 // At least one map clause must appear on the directive.
6398 if (!HasMapClause(Clauses)) {
6399 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
6400 getOpenMPDirectiveName(OMPD_target_data);
6401 return StmtError();
6402 }
6403
Michael Wong65f367f2015-07-21 13:44:28 +00006404 getCurFunction()->setHasBranchProtectedScope();
6405
6406 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6407 AStmt);
6408}
6409
Samuel Antaodf67fc42016-01-19 19:15:56 +00006410StmtResult
6411Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
6412 SourceLocation StartLoc,
6413 SourceLocation EndLoc) {
6414 // OpenMP [2.10.2, Restrictions, p. 99]
6415 // At least one map clause must appear on the directive.
6416 if (!HasMapClause(Clauses)) {
6417 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6418 << getOpenMPDirectiveName(OMPD_target_enter_data);
6419 return StmtError();
6420 }
6421
6422 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
6423 Clauses);
6424}
6425
Samuel Antao72590762016-01-19 20:04:50 +00006426StmtResult
6427Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6428 SourceLocation StartLoc,
6429 SourceLocation EndLoc) {
6430 // OpenMP [2.10.3, Restrictions, p. 102]
6431 // At least one map clause must appear on the directive.
6432 if (!HasMapClause(Clauses)) {
6433 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6434 << getOpenMPDirectiveName(OMPD_target_exit_data);
6435 return StmtError();
6436 }
6437
6438 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
6439}
6440
Alexey Bataev13314bf2014-10-09 04:18:56 +00006441StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6442 Stmt *AStmt, SourceLocation StartLoc,
6443 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006444 if (!AStmt)
6445 return StmtError();
6446
Alexey Bataev13314bf2014-10-09 04:18:56 +00006447 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6448 // 1.2.2 OpenMP Language Terminology
6449 // Structured block - An executable statement with a single entry at the
6450 // top and a single exit at the bottom.
6451 // The point of exit cannot be a branch out of the structured block.
6452 // longjmp() and throw() must not violate the entry/exit criteria.
6453 CS->getCapturedDecl()->setNothrow();
6454
6455 getCurFunction()->setHasBranchProtectedScope();
6456
6457 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6458}
6459
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006460StmtResult
6461Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6462 SourceLocation EndLoc,
6463 OpenMPDirectiveKind CancelRegion) {
6464 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6465 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6466 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6467 << getOpenMPDirectiveName(CancelRegion);
6468 return StmtError();
6469 }
6470 if (DSAStack->isParentNowaitRegion()) {
6471 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6472 return StmtError();
6473 }
6474 if (DSAStack->isParentOrderedRegion()) {
6475 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
6476 return StmtError();
6477 }
6478 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
6479 CancelRegion);
6480}
6481
Alexey Bataev87933c72015-09-18 08:07:34 +00006482StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
6483 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00006484 SourceLocation EndLoc,
6485 OpenMPDirectiveKind CancelRegion) {
6486 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6487 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6488 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6489 << getOpenMPDirectiveName(CancelRegion);
6490 return StmtError();
6491 }
6492 if (DSAStack->isParentNowaitRegion()) {
6493 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6494 return StmtError();
6495 }
6496 if (DSAStack->isParentOrderedRegion()) {
6497 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6498 return StmtError();
6499 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006500 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00006501 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6502 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00006503}
6504
Alexey Bataev382967a2015-12-08 12:06:20 +00006505static bool checkGrainsizeNumTasksClauses(Sema &S,
6506 ArrayRef<OMPClause *> Clauses) {
6507 OMPClause *PrevClause = nullptr;
6508 bool ErrorFound = false;
6509 for (auto *C : Clauses) {
6510 if (C->getClauseKind() == OMPC_grainsize ||
6511 C->getClauseKind() == OMPC_num_tasks) {
6512 if (!PrevClause)
6513 PrevClause = C;
6514 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6515 S.Diag(C->getLocStart(),
6516 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6517 << getOpenMPClauseName(C->getClauseKind())
6518 << getOpenMPClauseName(PrevClause->getClauseKind());
6519 S.Diag(PrevClause->getLocStart(),
6520 diag::note_omp_previous_grainsize_num_tasks)
6521 << getOpenMPClauseName(PrevClause->getClauseKind());
6522 ErrorFound = true;
6523 }
6524 }
6525 }
6526 return ErrorFound;
6527}
6528
Alexey Bataev49f6e782015-12-01 04:18:41 +00006529StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6530 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6531 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006532 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00006533 if (!AStmt)
6534 return StmtError();
6535
6536 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6537 OMPLoopDirective::HelperExprs B;
6538 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6539 // define the nested loops number.
6540 unsigned NestedLoopCount =
6541 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006542 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00006543 VarsWithImplicitDSA, B);
6544 if (NestedLoopCount == 0)
6545 return StmtError();
6546
6547 assert((CurContext->isDependentContext() || B.builtAll()) &&
6548 "omp for loop exprs were not built");
6549
Alexey Bataev382967a2015-12-08 12:06:20 +00006550 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6551 // The grainsize clause and num_tasks clause are mutually exclusive and may
6552 // not appear on the same taskloop directive.
6553 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6554 return StmtError();
6555
Alexey Bataev49f6e782015-12-01 04:18:41 +00006556 getCurFunction()->setHasBranchProtectedScope();
6557 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
6558 NestedLoopCount, Clauses, AStmt, B);
6559}
6560
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006561StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
6562 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6563 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006564 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006565 if (!AStmt)
6566 return StmtError();
6567
6568 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6569 OMPLoopDirective::HelperExprs B;
6570 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6571 // define the nested loops number.
6572 unsigned NestedLoopCount =
6573 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
6574 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6575 VarsWithImplicitDSA, B);
6576 if (NestedLoopCount == 0)
6577 return StmtError();
6578
6579 assert((CurContext->isDependentContext() || B.builtAll()) &&
6580 "omp for loop exprs were not built");
6581
Alexey Bataev5a3af132016-03-29 08:58:54 +00006582 if (!CurContext->isDependentContext()) {
6583 // Finalize the clauses that need pre-built expressions for CodeGen.
6584 for (auto C : Clauses) {
6585 if (auto LC = dyn_cast<OMPLinearClause>(C))
6586 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006587 B.NumIterations, *this, CurScope,
6588 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00006589 return StmtError();
6590 }
6591 }
6592
Alexey Bataev382967a2015-12-08 12:06:20 +00006593 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6594 // The grainsize clause and num_tasks clause are mutually exclusive and may
6595 // not appear on the same taskloop directive.
6596 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6597 return StmtError();
6598
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006599 getCurFunction()->setHasBranchProtectedScope();
6600 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
6601 NestedLoopCount, Clauses, AStmt, B);
6602}
6603
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006604StmtResult Sema::ActOnOpenMPDistributeDirective(
6605 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6606 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006607 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006608 if (!AStmt)
6609 return StmtError();
6610
6611 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6612 OMPLoopDirective::HelperExprs B;
6613 // In presence of clause 'collapse' with number of loops, it will
6614 // define the nested loops number.
6615 unsigned NestedLoopCount =
6616 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
6617 nullptr /*ordered not a clause on distribute*/, AStmt,
6618 *this, *DSAStack, VarsWithImplicitDSA, B);
6619 if (NestedLoopCount == 0)
6620 return StmtError();
6621
6622 assert((CurContext->isDependentContext() || B.builtAll()) &&
6623 "omp for loop exprs were not built");
6624
6625 getCurFunction()->setHasBranchProtectedScope();
6626 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
6627 NestedLoopCount, Clauses, AStmt, B);
6628}
6629
Alexey Bataeved09d242014-05-28 05:53:51 +00006630OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006631 SourceLocation StartLoc,
6632 SourceLocation LParenLoc,
6633 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006634 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006635 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00006636 case OMPC_final:
6637 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
6638 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00006639 case OMPC_num_threads:
6640 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
6641 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006642 case OMPC_safelen:
6643 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
6644 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00006645 case OMPC_simdlen:
6646 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
6647 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00006648 case OMPC_collapse:
6649 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
6650 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006651 case OMPC_ordered:
6652 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
6653 break;
Michael Wonge710d542015-08-07 16:16:36 +00006654 case OMPC_device:
6655 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
6656 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00006657 case OMPC_num_teams:
6658 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
6659 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006660 case OMPC_thread_limit:
6661 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
6662 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00006663 case OMPC_priority:
6664 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
6665 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006666 case OMPC_grainsize:
6667 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
6668 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00006669 case OMPC_num_tasks:
6670 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
6671 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00006672 case OMPC_hint:
6673 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
6674 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006675 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006676 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006677 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006678 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006679 case OMPC_private:
6680 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006681 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006682 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006683 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006684 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006685 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006686 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006687 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00006688 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006689 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006690 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006691 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006692 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006693 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006694 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006695 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006696 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006697 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006698 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00006699 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006700 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006701 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00006702 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006703 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006704 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006705 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00006706 case OMPC_uniform:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006707 llvm_unreachable("Clause is not allowed.");
6708 }
6709 return Res;
6710}
6711
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006712OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
6713 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006714 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006715 SourceLocation NameModifierLoc,
6716 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006717 SourceLocation EndLoc) {
6718 Expr *ValExpr = Condition;
6719 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6720 !Condition->isInstantiationDependent() &&
6721 !Condition->containsUnexpandedParameterPack()) {
6722 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00006723 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006724 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006725 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006726
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006727 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006728 }
6729
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006730 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
6731 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006732}
6733
Alexey Bataev3778b602014-07-17 07:32:53 +00006734OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
6735 SourceLocation StartLoc,
6736 SourceLocation LParenLoc,
6737 SourceLocation EndLoc) {
6738 Expr *ValExpr = Condition;
6739 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6740 !Condition->isInstantiationDependent() &&
6741 !Condition->containsUnexpandedParameterPack()) {
6742 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
6743 Condition->getExprLoc(), Condition);
6744 if (Val.isInvalid())
6745 return nullptr;
6746
6747 ValExpr = Val.get();
6748 }
6749
6750 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
6751}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006752ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
6753 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00006754 if (!Op)
6755 return ExprError();
6756
6757 class IntConvertDiagnoser : public ICEConvertDiagnoser {
6758 public:
6759 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00006760 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00006761 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
6762 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006763 return S.Diag(Loc, diag::err_omp_not_integral) << T;
6764 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006765 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
6766 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006767 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
6768 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006769 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
6770 QualType T,
6771 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006772 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
6773 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006774 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
6775 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006776 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006777 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006778 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006779 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
6780 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006781 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
6782 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006783 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
6784 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006785 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006786 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006787 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006788 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
6789 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006790 llvm_unreachable("conversion functions are permitted");
6791 }
6792 } ConvertDiagnoser;
6793 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
6794}
6795
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006796static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00006797 OpenMPClauseKind CKind,
6798 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006799 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
6800 !ValExpr->isInstantiationDependent()) {
6801 SourceLocation Loc = ValExpr->getExprLoc();
6802 ExprResult Value =
6803 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
6804 if (Value.isInvalid())
6805 return false;
6806
6807 ValExpr = Value.get();
6808 // The expression must evaluate to a non-negative integer value.
6809 llvm::APSInt Result;
6810 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00006811 Result.isSigned() &&
6812 !((!StrictlyPositive && Result.isNonNegative()) ||
6813 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006814 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006815 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6816 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006817 return false;
6818 }
6819 }
6820 return true;
6821}
6822
Alexey Bataev568a8332014-03-06 06:15:19 +00006823OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
6824 SourceLocation StartLoc,
6825 SourceLocation LParenLoc,
6826 SourceLocation EndLoc) {
6827 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00006828
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006829 // OpenMP [2.5, Restrictions]
6830 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00006831 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
6832 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006833 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00006834
Alexey Bataeved09d242014-05-28 05:53:51 +00006835 return new (Context)
6836 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00006837}
6838
Alexey Bataev62c87d22014-03-21 04:51:18 +00006839ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006840 OpenMPClauseKind CKind,
6841 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006842 if (!E)
6843 return ExprError();
6844 if (E->isValueDependent() || E->isTypeDependent() ||
6845 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00006846 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006847 llvm::APSInt Result;
6848 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
6849 if (ICE.isInvalid())
6850 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006851 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
6852 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006853 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006854 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6855 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00006856 return ExprError();
6857 }
Alexander Musman09184fe2014-09-30 05:29:28 +00006858 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
6859 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
6860 << E->getSourceRange();
6861 return ExprError();
6862 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006863 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
6864 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00006865 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006866 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00006867 return ICE;
6868}
6869
6870OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
6871 SourceLocation LParenLoc,
6872 SourceLocation EndLoc) {
6873 // OpenMP [2.8.1, simd construct, Description]
6874 // The parameter of the safelen clause must be a constant
6875 // positive integer expression.
6876 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
6877 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006878 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006879 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006880 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00006881}
6882
Alexey Bataev66b15b52015-08-21 11:14:16 +00006883OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
6884 SourceLocation LParenLoc,
6885 SourceLocation EndLoc) {
6886 // OpenMP [2.8.1, simd construct, Description]
6887 // The parameter of the simdlen clause must be a constant
6888 // positive integer expression.
6889 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
6890 if (Simdlen.isInvalid())
6891 return nullptr;
6892 return new (Context)
6893 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
6894}
6895
Alexander Musman64d33f12014-06-04 07:53:32 +00006896OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
6897 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00006898 SourceLocation LParenLoc,
6899 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00006900 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006901 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00006902 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006903 // The parameter of the collapse clause must be a constant
6904 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00006905 ExprResult NumForLoopsResult =
6906 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
6907 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00006908 return nullptr;
6909 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00006910 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00006911}
6912
Alexey Bataev10e775f2015-07-30 11:36:16 +00006913OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
6914 SourceLocation EndLoc,
6915 SourceLocation LParenLoc,
6916 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00006917 // OpenMP [2.7.1, loop construct, Description]
6918 // OpenMP [2.8.1, simd construct, Description]
6919 // OpenMP [2.9.6, distribute construct, Description]
6920 // The parameter of the ordered clause must be a constant
6921 // positive integer expression if any.
6922 if (NumForLoops && LParenLoc.isValid()) {
6923 ExprResult NumForLoopsResult =
6924 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
6925 if (NumForLoopsResult.isInvalid())
6926 return nullptr;
6927 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00006928 } else
6929 NumForLoops = nullptr;
6930 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00006931 return new (Context)
6932 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
6933}
6934
Alexey Bataeved09d242014-05-28 05:53:51 +00006935OMPClause *Sema::ActOnOpenMPSimpleClause(
6936 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
6937 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006938 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006939 switch (Kind) {
6940 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006941 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00006942 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
6943 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006944 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006945 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00006946 Res = ActOnOpenMPProcBindClause(
6947 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
6948 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006949 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006950 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006951 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006952 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006953 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006954 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006955 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006956 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006957 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006958 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006959 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006960 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006961 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006962 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006963 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006964 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006965 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006966 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006967 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006968 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006969 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006970 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006971 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006972 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006973 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006974 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006975 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006976 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006977 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006978 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006979 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006980 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006981 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006982 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006983 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006984 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006985 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006986 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006987 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006988 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006989 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006990 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006991 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00006992 case OMPC_uniform:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006993 llvm_unreachable("Clause is not allowed.");
6994 }
6995 return Res;
6996}
6997
Alexey Bataev6402bca2015-12-28 07:25:51 +00006998static std::string
6999getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
7000 ArrayRef<unsigned> Exclude = llvm::None) {
7001 std::string Values;
7002 unsigned Bound = Last >= 2 ? Last - 2 : 0;
7003 unsigned Skipped = Exclude.size();
7004 auto S = Exclude.begin(), E = Exclude.end();
7005 for (unsigned i = First; i < Last; ++i) {
7006 if (std::find(S, E, i) != E) {
7007 --Skipped;
7008 continue;
7009 }
7010 Values += "'";
7011 Values += getOpenMPSimpleClauseTypeName(K, i);
7012 Values += "'";
7013 if (i == Bound - Skipped)
7014 Values += " or ";
7015 else if (i != Bound + 1 - Skipped)
7016 Values += ", ";
7017 }
7018 return Values;
7019}
7020
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007021OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
7022 SourceLocation KindKwLoc,
7023 SourceLocation StartLoc,
7024 SourceLocation LParenLoc,
7025 SourceLocation EndLoc) {
7026 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00007027 static_assert(OMPC_DEFAULT_unknown > 0,
7028 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007029 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00007030 << getListOfPossibleValues(OMPC_default, /*First=*/0,
7031 /*Last=*/OMPC_DEFAULT_unknown)
7032 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007033 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007034 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00007035 switch (Kind) {
7036 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007037 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007038 break;
7039 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007040 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007041 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007042 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007043 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00007044 break;
7045 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007046 return new (Context)
7047 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007048}
7049
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007050OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
7051 SourceLocation KindKwLoc,
7052 SourceLocation StartLoc,
7053 SourceLocation LParenLoc,
7054 SourceLocation EndLoc) {
7055 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007056 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00007057 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
7058 /*Last=*/OMPC_PROC_BIND_unknown)
7059 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007060 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007061 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007062 return new (Context)
7063 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007064}
7065
Alexey Bataev56dafe82014-06-20 07:16:17 +00007066OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007067 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007068 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007069 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007070 SourceLocation EndLoc) {
7071 OMPClause *Res = nullptr;
7072 switch (Kind) {
7073 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00007074 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
7075 assert(Argument.size() == NumberOfElements &&
7076 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007077 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007078 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
7079 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
7080 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
7081 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
7082 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007083 break;
7084 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00007085 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
7086 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
7087 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
7088 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007089 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00007090 case OMPC_dist_schedule:
7091 Res = ActOnOpenMPDistScheduleClause(
7092 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
7093 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
7094 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007095 case OMPC_defaultmap:
7096 enum { Modifier, DefaultmapKind };
7097 Res = ActOnOpenMPDefaultmapClause(
7098 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
7099 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
7100 StartLoc, LParenLoc, ArgumentLoc[Modifier],
7101 ArgumentLoc[DefaultmapKind], EndLoc);
7102 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00007103 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007104 case OMPC_num_threads:
7105 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007106 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007107 case OMPC_collapse:
7108 case OMPC_default:
7109 case OMPC_proc_bind:
7110 case OMPC_private:
7111 case OMPC_firstprivate:
7112 case OMPC_lastprivate:
7113 case OMPC_shared:
7114 case OMPC_reduction:
7115 case OMPC_linear:
7116 case OMPC_aligned:
7117 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007118 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007119 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007120 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007121 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007122 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007123 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007124 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007125 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007126 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007127 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007128 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007129 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007130 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007131 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007132 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007133 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007134 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007135 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007136 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007137 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007138 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007139 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007140 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007141 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007142 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007143 case OMPC_uniform:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007144 llvm_unreachable("Clause is not allowed.");
7145 }
7146 return Res;
7147}
7148
Alexey Bataev6402bca2015-12-28 07:25:51 +00007149static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
7150 OpenMPScheduleClauseModifier M2,
7151 SourceLocation M1Loc, SourceLocation M2Loc) {
7152 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
7153 SmallVector<unsigned, 2> Excluded;
7154 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
7155 Excluded.push_back(M2);
7156 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
7157 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
7158 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
7159 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
7160 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
7161 << getListOfPossibleValues(OMPC_schedule,
7162 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
7163 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7164 Excluded)
7165 << getOpenMPClauseName(OMPC_schedule);
7166 return true;
7167 }
7168 return false;
7169}
7170
Alexey Bataev56dafe82014-06-20 07:16:17 +00007171OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007172 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007173 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007174 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
7175 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
7176 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
7177 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
7178 return nullptr;
7179 // OpenMP, 2.7.1, Loop Construct, Restrictions
7180 // Either the monotonic modifier or the nonmonotonic modifier can be specified
7181 // but not both.
7182 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
7183 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
7184 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
7185 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
7186 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
7187 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
7188 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
7189 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
7190 return nullptr;
7191 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007192 if (Kind == OMPC_SCHEDULE_unknown) {
7193 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00007194 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
7195 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
7196 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7197 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7198 Exclude);
7199 } else {
7200 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7201 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007202 }
7203 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
7204 << Values << getOpenMPClauseName(OMPC_schedule);
7205 return nullptr;
7206 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00007207 // OpenMP, 2.7.1, Loop Construct, Restrictions
7208 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
7209 // schedule(guided).
7210 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
7211 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
7212 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
7213 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
7214 diag::err_omp_schedule_nonmonotonic_static);
7215 return nullptr;
7216 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007217 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00007218 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00007219 if (ChunkSize) {
7220 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
7221 !ChunkSize->isInstantiationDependent() &&
7222 !ChunkSize->containsUnexpandedParameterPack()) {
7223 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
7224 ExprResult Val =
7225 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
7226 if (Val.isInvalid())
7227 return nullptr;
7228
7229 ValExpr = Val.get();
7230
7231 // OpenMP [2.7.1, Restrictions]
7232 // chunk_size must be a loop invariant integer expression with a positive
7233 // value.
7234 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00007235 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
7236 if (Result.isSigned() && !Result.isStrictlyPositive()) {
7237 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00007238 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00007239 return nullptr;
7240 }
7241 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00007242 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
7243 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
7244 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007245 }
7246 }
7247 }
7248
Alexey Bataev6402bca2015-12-28 07:25:51 +00007249 return new (Context)
7250 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00007251 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007252}
7253
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007254OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
7255 SourceLocation StartLoc,
7256 SourceLocation EndLoc) {
7257 OMPClause *Res = nullptr;
7258 switch (Kind) {
7259 case OMPC_ordered:
7260 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
7261 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00007262 case OMPC_nowait:
7263 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
7264 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007265 case OMPC_untied:
7266 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
7267 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007268 case OMPC_mergeable:
7269 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
7270 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007271 case OMPC_read:
7272 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
7273 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00007274 case OMPC_write:
7275 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
7276 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00007277 case OMPC_update:
7278 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
7279 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00007280 case OMPC_capture:
7281 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
7282 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007283 case OMPC_seq_cst:
7284 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
7285 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00007286 case OMPC_threads:
7287 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
7288 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007289 case OMPC_simd:
7290 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
7291 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00007292 case OMPC_nogroup:
7293 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
7294 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007295 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007296 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007297 case OMPC_num_threads:
7298 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007299 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007300 case OMPC_collapse:
7301 case OMPC_schedule:
7302 case OMPC_private:
7303 case OMPC_firstprivate:
7304 case OMPC_lastprivate:
7305 case OMPC_shared:
7306 case OMPC_reduction:
7307 case OMPC_linear:
7308 case OMPC_aligned:
7309 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007310 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007311 case OMPC_default:
7312 case OMPC_proc_bind:
7313 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007314 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007315 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007316 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007317 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007318 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007319 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007320 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007321 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00007322 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007323 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007324 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007325 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007326 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007327 case OMPC_uniform:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007328 llvm_unreachable("Clause is not allowed.");
7329 }
7330 return Res;
7331}
7332
Alexey Bataev236070f2014-06-20 11:19:47 +00007333OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
7334 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007335 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00007336 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
7337}
7338
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007339OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
7340 SourceLocation EndLoc) {
7341 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
7342}
7343
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007344OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
7345 SourceLocation EndLoc) {
7346 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
7347}
7348
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007349OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
7350 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007351 return new (Context) OMPReadClause(StartLoc, EndLoc);
7352}
7353
Alexey Bataevdea47612014-07-23 07:46:59 +00007354OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
7355 SourceLocation EndLoc) {
7356 return new (Context) OMPWriteClause(StartLoc, EndLoc);
7357}
7358
Alexey Bataev67a4f222014-07-23 10:25:33 +00007359OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
7360 SourceLocation EndLoc) {
7361 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
7362}
7363
Alexey Bataev459dec02014-07-24 06:46:57 +00007364OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
7365 SourceLocation EndLoc) {
7366 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
7367}
7368
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007369OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
7370 SourceLocation EndLoc) {
7371 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
7372}
7373
Alexey Bataev346265e2015-09-25 10:37:12 +00007374OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
7375 SourceLocation EndLoc) {
7376 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
7377}
7378
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007379OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
7380 SourceLocation EndLoc) {
7381 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
7382}
7383
Alexey Bataevb825de12015-12-07 10:51:44 +00007384OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
7385 SourceLocation EndLoc) {
7386 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
7387}
7388
Alexey Bataevc5e02582014-06-16 07:08:35 +00007389OMPClause *Sema::ActOnOpenMPVarListClause(
7390 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
7391 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
7392 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007393 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00007394 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
7395 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
7396 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007397 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007398 switch (Kind) {
7399 case OMPC_private:
7400 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7401 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007402 case OMPC_firstprivate:
7403 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7404 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007405 case OMPC_lastprivate:
7406 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7407 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007408 case OMPC_shared:
7409 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
7410 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007411 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00007412 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
7413 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007414 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00007415 case OMPC_linear:
7416 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00007417 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00007418 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007419 case OMPC_aligned:
7420 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
7421 ColonLoc, EndLoc);
7422 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007423 case OMPC_copyin:
7424 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
7425 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007426 case OMPC_copyprivate:
7427 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7428 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00007429 case OMPC_flush:
7430 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
7431 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007432 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007433 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
7434 StartLoc, LParenLoc, EndLoc);
7435 break;
7436 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00007437 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
7438 DepLinMapLoc, ColonLoc, VarList, StartLoc,
7439 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007440 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007441 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007442 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00007443 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00007444 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007445 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00007446 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007447 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007448 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007449 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007450 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007451 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007452 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007453 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007454 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007455 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007456 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007457 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007458 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007459 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00007460 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007461 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007462 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007463 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007464 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007465 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007466 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007467 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007468 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007469 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007470 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007471 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007472 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007473 case OMPC_uniform:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007474 llvm_unreachable("Clause is not allowed.");
7475 }
7476 return Res;
7477}
7478
Alexey Bataev90c228f2016-02-08 09:29:13 +00007479ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00007480 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00007481 ExprResult Res = BuildDeclRefExpr(
7482 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
7483 if (!Res.isUsable())
7484 return ExprError();
7485 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
7486 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
7487 if (!Res.isUsable())
7488 return ExprError();
7489 }
7490 if (VK != VK_LValue && Res.get()->isGLValue()) {
7491 Res = DefaultLvalueConversion(Res.get());
7492 if (!Res.isUsable())
7493 return ExprError();
7494 }
7495 return Res;
7496}
7497
Alexey Bataev60da77e2016-02-29 05:54:20 +00007498static std::pair<ValueDecl *, bool>
7499getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
7500 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007501 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7502 RefExpr->containsUnexpandedParameterPack())
7503 return std::make_pair(nullptr, true);
7504
Alexey Bataevd985eda2016-02-10 11:29:16 +00007505 // OpenMP [3.1, C/C++]
7506 // A list item is a variable name.
7507 // OpenMP [2.9.3.3, Restrictions, p.1]
7508 // A variable that is part of another variable (as an array or
7509 // structure element) cannot appear in a private clause.
7510 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00007511 enum {
7512 NoArrayExpr = -1,
7513 ArraySubscript = 0,
7514 OMPArraySection = 1
7515 } IsArrayExpr = NoArrayExpr;
7516 if (AllowArraySection) {
7517 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
7518 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7519 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7520 Base = TempASE->getBase()->IgnoreParenImpCasts();
7521 RefExpr = Base;
7522 IsArrayExpr = ArraySubscript;
7523 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
7524 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7525 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7526 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7527 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7528 Base = TempASE->getBase()->IgnoreParenImpCasts();
7529 RefExpr = Base;
7530 IsArrayExpr = OMPArraySection;
7531 }
7532 }
7533 ELoc = RefExpr->getExprLoc();
7534 ERange = RefExpr->getSourceRange();
7535 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007536 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
7537 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
7538 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
7539 (S.getCurrentThisType().isNull() || !ME ||
7540 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
7541 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00007542 if (IsArrayExpr != NoArrayExpr)
7543 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
7544 << ERange;
7545 else {
7546 S.Diag(ELoc,
7547 AllowArraySection
7548 ? diag::err_omp_expected_var_name_member_expr_or_array_item
7549 : diag::err_omp_expected_var_name_member_expr)
7550 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
7551 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007552 return std::make_pair(nullptr, false);
7553 }
7554 return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
7555}
7556
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007557OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
7558 SourceLocation StartLoc,
7559 SourceLocation LParenLoc,
7560 SourceLocation EndLoc) {
7561 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00007562 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00007563 for (auto &RefExpr : VarList) {
7564 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007565 SourceLocation ELoc;
7566 SourceRange ERange;
7567 Expr *SimpleRefExpr = RefExpr;
7568 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007569 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007570 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007571 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007572 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007573 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007574 ValueDecl *D = Res.first;
7575 if (!D)
7576 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007577
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007578 QualType Type = D->getType();
7579 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007580
7581 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7582 // A variable that appears in a private clause must not have an incomplete
7583 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007584 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007585 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007586 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007587
Alexey Bataev758e55e2013-09-06 18:03:48 +00007588 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7589 // in a Construct]
7590 // Variables with the predetermined data-sharing attributes may not be
7591 // listed in data-sharing attributes clauses, except for the cases
7592 // listed below. For these exceptions only, listing a predetermined
7593 // variable in a data-sharing attribute clause is allowed and overrides
7594 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007595 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007596 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007597 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7598 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007599 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007600 continue;
7601 }
7602
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007603 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007604 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00007605 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007606 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7607 << getOpenMPClauseName(OMPC_private) << Type
7608 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7609 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007610 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007611 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007612 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007613 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007614 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007615 continue;
7616 }
7617
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007618 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
7619 // A list item cannot appear in both a map clause and a data-sharing
7620 // attribute clause on the same construct
7621 if (DSAStack->getCurrentDirective() == OMPD_target) {
Samuel Antao90927002016-04-26 14:54:23 +00007622 if (DSAStack->checkMappableExprComponentListsForDecl(
7623 VD, /* CurrentRegionOnly = */ true,
7624 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef)
7625 -> bool { return true; })) {
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007626 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
7627 << getOpenMPClauseName(OMPC_private)
7628 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7629 ReportOriginalDSA(*this, DSAStack, D, DVar);
7630 continue;
7631 }
7632 }
7633
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007634 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
7635 // A variable of class type (or array thereof) that appears in a private
7636 // clause requires an accessible, unambiguous default constructor for the
7637 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00007638 // Generate helper private variable and initialize it with the default
7639 // value. The address of the original variable is replaced by the address of
7640 // the new private variable in CodeGen. This new variable is not added to
7641 // IdResolver, so the code in the OpenMP region uses original variable for
7642 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007643 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007644 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7645 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007646 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007647 if (VDPrivate->isInvalidDecl())
7648 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007649 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007650 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007651
Alexey Bataev90c228f2016-02-08 09:29:13 +00007652 DeclRefExpr *Ref = nullptr;
7653 if (!VD)
Alexey Bataev61205072016-03-02 04:57:40 +00007654 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00007655 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
7656 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007657 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007658 }
7659
Alexey Bataeved09d242014-05-28 05:53:51 +00007660 if (Vars.empty())
7661 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007662
Alexey Bataev03b340a2014-10-21 03:16:40 +00007663 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7664 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007665}
7666
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007667namespace {
7668class DiagsUninitializedSeveretyRAII {
7669private:
7670 DiagnosticsEngine &Diags;
7671 SourceLocation SavedLoc;
7672 bool IsIgnored;
7673
7674public:
7675 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
7676 bool IsIgnored)
7677 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
7678 if (!IsIgnored) {
7679 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
7680 /*Map*/ diag::Severity::Ignored, Loc);
7681 }
7682 }
7683 ~DiagsUninitializedSeveretyRAII() {
7684 if (!IsIgnored)
7685 Diags.popMappings(SavedLoc);
7686 }
7687};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007688}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007689
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007690OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
7691 SourceLocation StartLoc,
7692 SourceLocation LParenLoc,
7693 SourceLocation EndLoc) {
7694 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007695 SmallVector<Expr *, 8> PrivateCopies;
7696 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00007697 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007698 bool IsImplicitClause =
7699 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
7700 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
7701
Alexey Bataeved09d242014-05-28 05:53:51 +00007702 for (auto &RefExpr : VarList) {
7703 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007704 SourceLocation ELoc;
7705 SourceRange ERange;
7706 Expr *SimpleRefExpr = RefExpr;
7707 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007708 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007709 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007710 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007711 PrivateCopies.push_back(nullptr);
7712 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007713 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007714 ValueDecl *D = Res.first;
7715 if (!D)
7716 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007717
Alexey Bataev60da77e2016-02-29 05:54:20 +00007718 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00007719 QualType Type = D->getType();
7720 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007721
7722 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7723 // A variable that appears in a private clause must not have an incomplete
7724 // type or a reference type.
7725 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00007726 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007727 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007728 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007729
7730 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
7731 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00007732 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007733 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007734 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007735
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007736 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00007737 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007738 if (!IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007739 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev005248a2016-02-25 05:25:57 +00007740 TopDVar = DVar;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007741 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007742 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
7743 // A list item that specifies a given variable may not appear in more
7744 // than one clause on the same directive, except that a variable may be
7745 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007746 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00007747 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007748 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007749 << getOpenMPClauseName(DVar.CKind)
7750 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007751 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007752 continue;
7753 }
7754
7755 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7756 // in a Construct]
7757 // Variables with the predetermined data-sharing attributes may not be
7758 // listed in data-sharing attributes clauses, except for the cases
7759 // listed below. For these exceptions only, listing a predetermined
7760 // variable in a data-sharing attribute clause is allowed and overrides
7761 // the variable's predetermined data-sharing attributes.
7762 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7763 // in a Construct, C/C++, p.2]
7764 // Variables with const-qualified type having no mutable member may be
7765 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00007766 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007767 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
7768 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007769 << getOpenMPClauseName(DVar.CKind)
7770 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007771 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007772 continue;
7773 }
7774
Alexey Bataevf29276e2014-06-18 04:14:57 +00007775 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007776 // OpenMP [2.9.3.4, Restrictions, p.2]
7777 // A list item that is private within a parallel region must not appear
7778 // in a firstprivate clause on a worksharing construct if any of the
7779 // worksharing regions arising from the worksharing construct ever bind
7780 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00007781 if (isOpenMPWorksharingDirective(CurrDir) &&
7782 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007783 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007784 if (DVar.CKind != OMPC_shared &&
7785 (isOpenMPParallelDirective(DVar.DKind) ||
7786 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00007787 Diag(ELoc, diag::err_omp_required_access)
7788 << getOpenMPClauseName(OMPC_firstprivate)
7789 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007790 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007791 continue;
7792 }
7793 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007794 // OpenMP [2.9.3.4, Restrictions, p.3]
7795 // A list item that appears in a reduction clause of a parallel construct
7796 // must not appear in a firstprivate clause on a worksharing or task
7797 // construct if any of the worksharing or task regions arising from the
7798 // worksharing or task construct ever bind to any of the parallel regions
7799 // arising from the parallel construct.
7800 // OpenMP [2.9.3.4, Restrictions, p.4]
7801 // A list item that appears in a reduction clause in worksharing
7802 // construct must not appear in a firstprivate clause in a task construct
7803 // encountered during execution of any of the worksharing regions arising
7804 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +00007805 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00007806 DVar = DSAStack->hasInnermostDSA(
7807 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
7808 [](OpenMPDirectiveKind K) -> bool {
7809 return isOpenMPParallelDirective(K) ||
7810 isOpenMPWorksharingDirective(K);
7811 },
7812 false);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007813 if (DVar.CKind == OMPC_reduction &&
7814 (isOpenMPParallelDirective(DVar.DKind) ||
7815 isOpenMPWorksharingDirective(DVar.DKind))) {
7816 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
7817 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007818 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007819 continue;
7820 }
7821 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007822
7823 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7824 // A list item that is private within a teams region must not appear in a
7825 // firstprivate clause on a distribute construct if any of the distribute
7826 // regions arising from the distribute construct ever bind to any of the
7827 // teams regions arising from the teams construct.
7828 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7829 // A list item that appears in a reduction clause of a teams construct
7830 // must not appear in a firstprivate clause on a distribute construct if
7831 // any of the distribute regions arising from the distribute construct
7832 // ever bind to any of the teams regions arising from the teams construct.
7833 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7834 // A list item may appear in a firstprivate or lastprivate clause but not
7835 // both.
7836 if (CurrDir == OMPD_distribute) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00007837 DVar = DSAStack->hasInnermostDSA(
7838 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_private; },
7839 [](OpenMPDirectiveKind K) -> bool {
7840 return isOpenMPTeamsDirective(K);
7841 },
7842 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007843 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
7844 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007845 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007846 continue;
7847 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00007848 DVar = DSAStack->hasInnermostDSA(
7849 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
7850 [](OpenMPDirectiveKind K) -> bool {
7851 return isOpenMPTeamsDirective(K);
7852 },
7853 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007854 if (DVar.CKind == OMPC_reduction &&
7855 isOpenMPTeamsDirective(DVar.DKind)) {
7856 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007857 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007858 continue;
7859 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007860 DVar = DSAStack->getTopDSA(D, false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007861 if (DVar.CKind == OMPC_lastprivate) {
7862 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007863 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007864 continue;
7865 }
7866 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007867 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
7868 // A list item cannot appear in both a map clause and a data-sharing
7869 // attribute clause on the same construct
7870 if (CurrDir == OMPD_target) {
Samuel Antao90927002016-04-26 14:54:23 +00007871 if (DSAStack->checkMappableExprComponentListsForDecl(
7872 VD, /* CurrentRegionOnly = */ true,
7873 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef)
7874 -> bool { return true; })) {
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007875 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
7876 << getOpenMPClauseName(OMPC_firstprivate)
7877 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7878 ReportOriginalDSA(*this, DSAStack, D, DVar);
7879 continue;
7880 }
7881 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007882 }
7883
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007884 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007885 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00007886 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007887 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7888 << getOpenMPClauseName(OMPC_firstprivate) << Type
7889 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7890 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007891 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007892 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00007893 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007894 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00007895 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007896 continue;
7897 }
7898
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007899 Type = Type.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007900 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7901 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007902 // Generate helper private variable and initialize it with the value of the
7903 // original variable. The address of the original variable is replaced by
7904 // the address of the new private variable in the CodeGen. This new variable
7905 // is not added to IdResolver, so the code in the OpenMP region uses
7906 // original variable for proper diagnostics and variable capturing.
7907 Expr *VDInitRefExpr = nullptr;
7908 // For arrays generate initializer for single element and replace it by the
7909 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007910 if (Type->isArrayType()) {
7911 auto VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007912 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007913 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007914 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007915 ElemType = ElemType.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007916 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007917 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00007918 InitializedEntity Entity =
7919 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007920 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
7921
7922 InitializationSequence InitSeq(*this, Entity, Kind, Init);
7923 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
7924 if (Result.isInvalid())
7925 VDPrivate->setInvalidDecl();
7926 else
7927 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007928 // Remove temp variable declaration.
7929 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007930 } else {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007931 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
7932 ".firstprivate.temp");
7933 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
7934 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00007935 AddInitializerToDecl(VDPrivate,
7936 DefaultLvalueConversion(VDInitRefExpr).get(),
7937 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007938 }
7939 if (VDPrivate->isInvalidDecl()) {
7940 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007941 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007942 diag::note_omp_task_predetermined_firstprivate_here);
7943 }
7944 continue;
7945 }
7946 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007947 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00007948 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
7949 RefExpr->getExprLoc());
7950 DeclRefExpr *Ref = nullptr;
Alexey Bataev417089f2016-02-17 13:19:37 +00007951 if (!VD) {
Alexey Bataev005248a2016-02-25 05:25:57 +00007952 if (TopDVar.CKind == OMPC_lastprivate)
7953 Ref = TopDVar.PrivateCopy;
7954 else {
Alexey Bataev61205072016-03-02 04:57:40 +00007955 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataev005248a2016-02-25 05:25:57 +00007956 if (!IsOpenMPCapturedDecl(D))
7957 ExprCaptures.push_back(Ref->getDecl());
7958 }
Alexey Bataev417089f2016-02-17 13:19:37 +00007959 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007960 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
7961 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007962 PrivateCopies.push_back(VDPrivateRefExpr);
7963 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007964 }
7965
Alexey Bataeved09d242014-05-28 05:53:51 +00007966 if (Vars.empty())
7967 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007968
7969 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +00007970 Vars, PrivateCopies, Inits,
7971 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007972}
7973
Alexander Musman1bb328c2014-06-04 13:06:39 +00007974OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
7975 SourceLocation StartLoc,
7976 SourceLocation LParenLoc,
7977 SourceLocation EndLoc) {
7978 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00007979 SmallVector<Expr *, 8> SrcExprs;
7980 SmallVector<Expr *, 8> DstExprs;
7981 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +00007982 SmallVector<Decl *, 4> ExprCaptures;
7983 SmallVector<Expr *, 4> ExprPostUpdates;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007984 for (auto &RefExpr : VarList) {
7985 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007986 SourceLocation ELoc;
7987 SourceRange ERange;
7988 Expr *SimpleRefExpr = RefExpr;
7989 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007990 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00007991 // It will be analyzed later.
7992 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007993 SrcExprs.push_back(nullptr);
7994 DstExprs.push_back(nullptr);
7995 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007996 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00007997 ValueDecl *D = Res.first;
7998 if (!D)
7999 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008000
Alexey Bataev74caaf22016-02-20 04:09:36 +00008001 QualType Type = D->getType();
8002 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008003
8004 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
8005 // A variable that appears in a lastprivate clause must not have an
8006 // incomplete type or a reference type.
8007 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +00008008 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +00008009 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008010 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00008011
8012 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
8013 // in a Construct]
8014 // Variables with the predetermined data-sharing attributes may not be
8015 // listed in data-sharing attributes clauses, except for the cases
8016 // listed below.
Alexey Bataev74caaf22016-02-20 04:09:36 +00008017 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008018 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
8019 DVar.CKind != OMPC_firstprivate &&
8020 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
8021 Diag(ELoc, diag::err_omp_wrong_dsa)
8022 << getOpenMPClauseName(DVar.CKind)
8023 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008024 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008025 continue;
8026 }
8027
Alexey Bataevf29276e2014-06-18 04:14:57 +00008028 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8029 // OpenMP [2.14.3.5, Restrictions, p.2]
8030 // A list item that is private within a parallel region, or that appears in
8031 // the reduction clause of a parallel construct, must not appear in a
8032 // lastprivate clause on a worksharing construct if any of the corresponding
8033 // worksharing regions ever binds to any of the corresponding parallel
8034 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00008035 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00008036 if (isOpenMPWorksharingDirective(CurrDir) &&
8037 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +00008038 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008039 if (DVar.CKind != OMPC_shared) {
8040 Diag(ELoc, diag::err_omp_required_access)
8041 << getOpenMPClauseName(OMPC_lastprivate)
8042 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008043 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008044 continue;
8045 }
8046 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00008047
8048 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8049 // A list item may appear in a firstprivate or lastprivate clause but not
8050 // both.
8051 if (CurrDir == OMPD_distribute) {
8052 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8053 if (DVar.CKind == OMPC_firstprivate) {
8054 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
8055 ReportOriginalDSA(*this, DSAStack, D, DVar);
8056 continue;
8057 }
8058 }
8059
Alexander Musman1bb328c2014-06-04 13:06:39 +00008060 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00008061 // A variable of class type (or array thereof) that appears in a
8062 // lastprivate clause requires an accessible, unambiguous default
8063 // constructor for the class type, unless the list item is also specified
8064 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00008065 // A variable of class type (or array thereof) that appears in a
8066 // lastprivate clause requires an accessible, unambiguous copy assignment
8067 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00008068 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008069 auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008070 Type.getUnqualifiedType(), ".lastprivate.src",
Alexey Bataev74caaf22016-02-20 04:09:36 +00008071 D->hasAttrs() ? &D->getAttrs() : nullptr);
8072 auto *PseudoSrcExpr =
8073 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00008074 auto *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008075 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +00008076 D->hasAttrs() ? &D->getAttrs() : nullptr);
8077 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00008078 // For arrays generate assignment operation for single element and replace
8079 // it by the original array element in CodeGen.
Alexey Bataev74caaf22016-02-20 04:09:36 +00008080 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
Alexey Bataev38e89532015-04-16 04:54:05 +00008081 PseudoDstExpr, PseudoSrcExpr);
8082 if (AssignmentOp.isInvalid())
8083 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +00008084 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00008085 /*DiscardedValue=*/true);
8086 if (AssignmentOp.isInvalid())
8087 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008088
Alexey Bataev74caaf22016-02-20 04:09:36 +00008089 DeclRefExpr *Ref = nullptr;
Alexey Bataev005248a2016-02-25 05:25:57 +00008090 if (!VD) {
8091 if (TopDVar.CKind == OMPC_firstprivate)
8092 Ref = TopDVar.PrivateCopy;
8093 else {
Alexey Bataev61205072016-03-02 04:57:40 +00008094 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00008095 if (!IsOpenMPCapturedDecl(D))
8096 ExprCaptures.push_back(Ref->getDecl());
8097 }
8098 if (TopDVar.CKind == OMPC_firstprivate ||
8099 (!IsOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008100 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008101 ExprResult RefRes = DefaultLvalueConversion(Ref);
8102 if (!RefRes.isUsable())
8103 continue;
8104 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008105 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
8106 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008107 if (!PostUpdateRes.isUsable())
8108 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008109 ExprPostUpdates.push_back(
8110 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008111 }
8112 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008113 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008114 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +00008115 SrcExprs.push_back(PseudoSrcExpr);
8116 DstExprs.push_back(PseudoDstExpr);
8117 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00008118 }
8119
8120 if (Vars.empty())
8121 return nullptr;
8122
8123 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +00008124 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008125 buildPreInits(Context, ExprCaptures),
8126 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +00008127}
8128
Alexey Bataev758e55e2013-09-06 18:03:48 +00008129OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
8130 SourceLocation StartLoc,
8131 SourceLocation LParenLoc,
8132 SourceLocation EndLoc) {
8133 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00008134 for (auto &RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008135 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008136 SourceLocation ELoc;
8137 SourceRange ERange;
8138 Expr *SimpleRefExpr = RefExpr;
8139 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008140 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00008141 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008142 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008143 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008144 ValueDecl *D = Res.first;
8145 if (!D)
8146 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008147
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008148 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008149 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8150 // in a Construct]
8151 // Variables with the predetermined data-sharing attributes may not be
8152 // listed in data-sharing attributes clauses, except for the cases
8153 // listed below. For these exceptions only, listing a predetermined
8154 // variable in a data-sharing attribute clause is allowed and overrides
8155 // the variable's predetermined data-sharing attributes.
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008156 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00008157 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
8158 DVar.RefExpr) {
8159 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8160 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008161 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008162 continue;
8163 }
8164
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008165 DeclRefExpr *Ref = nullptr;
Alexey Bataev1efd1662016-03-29 10:59:56 +00008166 if (!VD && IsOpenMPCapturedDecl(D))
Alexey Bataev61205072016-03-02 04:57:40 +00008167 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008168 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataev1efd1662016-03-29 10:59:56 +00008169 Vars.push_back((VD || !Ref) ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008170 }
8171
Alexey Bataeved09d242014-05-28 05:53:51 +00008172 if (Vars.empty())
8173 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008174
8175 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
8176}
8177
Alexey Bataevc5e02582014-06-16 07:08:35 +00008178namespace {
8179class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
8180 DSAStackTy *Stack;
8181
8182public:
8183 bool VisitDeclRefExpr(DeclRefExpr *E) {
8184 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008185 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008186 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
8187 return false;
8188 if (DVar.CKind != OMPC_unknown)
8189 return true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008190 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
8191 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
8192 false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008193 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00008194 return true;
8195 return false;
8196 }
8197 return false;
8198 }
8199 bool VisitStmt(Stmt *S) {
8200 for (auto Child : S->children()) {
8201 if (Child && Visit(Child))
8202 return true;
8203 }
8204 return false;
8205 }
Alexey Bataev23b69422014-06-18 07:08:49 +00008206 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00008207};
Alexey Bataev23b69422014-06-18 07:08:49 +00008208} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00008209
Alexey Bataev60da77e2016-02-29 05:54:20 +00008210namespace {
8211// Transform MemberExpression for specified FieldDecl of current class to
8212// DeclRefExpr to specified OMPCapturedExprDecl.
8213class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
8214 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
8215 ValueDecl *Field;
8216 DeclRefExpr *CapturedExpr;
8217
8218public:
8219 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
8220 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
8221
8222 ExprResult TransformMemberExpr(MemberExpr *E) {
8223 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
8224 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +00008225 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008226 return CapturedExpr;
8227 }
8228 return BaseTransform::TransformMemberExpr(E);
8229 }
8230 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
8231};
8232} // namespace
8233
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008234template <typename T>
8235static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups,
8236 const llvm::function_ref<T(ValueDecl *)> &Gen) {
8237 for (auto &Set : Lookups) {
8238 for (auto *D : Set) {
8239 if (auto Res = Gen(cast<ValueDecl>(D)))
8240 return Res;
8241 }
8242 }
8243 return T();
8244}
8245
8246static ExprResult
8247buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
8248 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
8249 const DeclarationNameInfo &ReductionId, QualType Ty,
8250 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
8251 if (ReductionIdScopeSpec.isInvalid())
8252 return ExprError();
8253 SmallVector<UnresolvedSet<8>, 4> Lookups;
8254 if (S) {
8255 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
8256 Lookup.suppressDiagnostics();
8257 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
8258 auto *D = Lookup.getRepresentativeDecl();
8259 do {
8260 S = S->getParent();
8261 } while (S && !S->isDeclScope(D));
8262 if (S)
8263 S = S->getParent();
8264 Lookups.push_back(UnresolvedSet<8>());
8265 Lookups.back().append(Lookup.begin(), Lookup.end());
8266 Lookup.clear();
8267 }
8268 } else if (auto *ULE =
8269 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
8270 Lookups.push_back(UnresolvedSet<8>());
8271 Decl *PrevD = nullptr;
8272 for(auto *D : ULE->decls()) {
8273 if (D == PrevD)
8274 Lookups.push_back(UnresolvedSet<8>());
8275 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
8276 Lookups.back().addDecl(DRD);
8277 PrevD = D;
8278 }
8279 }
8280 if (Ty->isDependentType() || Ty->isInstantiationDependentType() ||
8281 Ty->containsUnexpandedParameterPack() ||
8282 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool {
8283 return !D->isInvalidDecl() &&
8284 (D->getType()->isDependentType() ||
8285 D->getType()->isInstantiationDependentType() ||
8286 D->getType()->containsUnexpandedParameterPack());
8287 })) {
8288 UnresolvedSet<8> ResSet;
8289 for (auto &Set : Lookups) {
8290 ResSet.append(Set.begin(), Set.end());
8291 // The last item marks the end of all declarations at the specified scope.
8292 ResSet.addDecl(Set[Set.size() - 1]);
8293 }
8294 return UnresolvedLookupExpr::Create(
8295 SemaRef.Context, /*NamingClass=*/nullptr,
8296 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
8297 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
8298 }
8299 if (auto *VD = filterLookupForUDR<ValueDecl *>(
8300 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
8301 if (!D->isInvalidDecl() &&
8302 SemaRef.Context.hasSameType(D->getType(), Ty))
8303 return D;
8304 return nullptr;
8305 }))
8306 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8307 if (auto *VD = filterLookupForUDR<ValueDecl *>(
8308 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
8309 if (!D->isInvalidDecl() &&
8310 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
8311 !Ty.isMoreQualifiedThan(D->getType()))
8312 return D;
8313 return nullptr;
8314 })) {
8315 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
8316 /*DetectVirtual=*/false);
8317 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
8318 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
8319 VD->getType().getUnqualifiedType()))) {
8320 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
8321 /*DiagID=*/0) !=
8322 Sema::AR_inaccessible) {
8323 SemaRef.BuildBasePathArray(Paths, BasePath);
8324 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8325 }
8326 }
8327 }
8328 }
8329 if (ReductionIdScopeSpec.isSet()) {
8330 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
8331 return ExprError();
8332 }
8333 return ExprEmpty();
8334}
8335
Alexey Bataevc5e02582014-06-16 07:08:35 +00008336OMPClause *Sema::ActOnOpenMPReductionClause(
8337 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
8338 SourceLocation ColonLoc, SourceLocation EndLoc,
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008339 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
8340 ArrayRef<Expr *> UnresolvedReductions) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00008341 auto DN = ReductionId.getName();
8342 auto OOK = DN.getCXXOverloadedOperator();
8343 BinaryOperatorKind BOK = BO_Comma;
8344
8345 // OpenMP [2.14.3.6, reduction clause]
8346 // C
8347 // reduction-identifier is either an identifier or one of the following
8348 // operators: +, -, *, &, |, ^, && and ||
8349 // C++
8350 // reduction-identifier is either an id-expression or one of the following
8351 // operators: +, -, *, &, |, ^, && and ||
8352 // FIXME: Only 'min' and 'max' identifiers are supported for now.
8353 switch (OOK) {
8354 case OO_Plus:
8355 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008356 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008357 break;
8358 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008359 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008360 break;
8361 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008362 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008363 break;
8364 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008365 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008366 break;
8367 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008368 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008369 break;
8370 case OO_AmpAmp:
8371 BOK = BO_LAnd;
8372 break;
8373 case OO_PipePipe:
8374 BOK = BO_LOr;
8375 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008376 case OO_New:
8377 case OO_Delete:
8378 case OO_Array_New:
8379 case OO_Array_Delete:
8380 case OO_Slash:
8381 case OO_Percent:
8382 case OO_Tilde:
8383 case OO_Exclaim:
8384 case OO_Equal:
8385 case OO_Less:
8386 case OO_Greater:
8387 case OO_LessEqual:
8388 case OO_GreaterEqual:
8389 case OO_PlusEqual:
8390 case OO_MinusEqual:
8391 case OO_StarEqual:
8392 case OO_SlashEqual:
8393 case OO_PercentEqual:
8394 case OO_CaretEqual:
8395 case OO_AmpEqual:
8396 case OO_PipeEqual:
8397 case OO_LessLess:
8398 case OO_GreaterGreater:
8399 case OO_LessLessEqual:
8400 case OO_GreaterGreaterEqual:
8401 case OO_EqualEqual:
8402 case OO_ExclaimEqual:
8403 case OO_PlusPlus:
8404 case OO_MinusMinus:
8405 case OO_Comma:
8406 case OO_ArrowStar:
8407 case OO_Arrow:
8408 case OO_Call:
8409 case OO_Subscript:
8410 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00008411 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008412 case NUM_OVERLOADED_OPERATORS:
8413 llvm_unreachable("Unexpected reduction identifier");
8414 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00008415 if (auto II = DN.getAsIdentifierInfo()) {
8416 if (II->isStr("max"))
8417 BOK = BO_GT;
8418 else if (II->isStr("min"))
8419 BOK = BO_LT;
8420 }
8421 break;
8422 }
8423 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008424 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +00008425 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008426 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008427
8428 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008429 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008430 SmallVector<Expr *, 8> LHSs;
8431 SmallVector<Expr *, 8> RHSs;
8432 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev61205072016-03-02 04:57:40 +00008433 SmallVector<Decl *, 4> ExprCaptures;
8434 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008435 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
8436 bool FirstIter = true;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008437 for (auto RefExpr : VarList) {
8438 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +00008439 // OpenMP [2.1, C/C++]
8440 // A list item is a variable or array section, subject to the restrictions
8441 // specified in Section 2.4 on page 42 and in each of the sections
8442 // describing clauses and directives for which a list appears.
8443 // OpenMP [2.14.3.3, Restrictions, p.1]
8444 // A variable that is part of another variable (as an array or
8445 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008446 if (!FirstIter && IR != ER)
8447 ++IR;
8448 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008449 SourceLocation ELoc;
8450 SourceRange ERange;
8451 Expr *SimpleRefExpr = RefExpr;
8452 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8453 /*AllowArraySection=*/true);
8454 if (Res.second) {
8455 // It will be analyzed later.
8456 Vars.push_back(RefExpr);
8457 Privates.push_back(nullptr);
8458 LHSs.push_back(nullptr);
8459 RHSs.push_back(nullptr);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008460 // Try to find 'declare reduction' corresponding construct before using
8461 // builtin/overloaded operators.
8462 QualType Type = Context.DependentTy;
8463 CXXCastPath BasePath;
8464 ExprResult DeclareReductionRef = buildDeclareReductionRef(
8465 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
8466 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
8467 if (CurContext->isDependentContext() &&
8468 (DeclareReductionRef.isUnset() ||
8469 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
8470 ReductionOps.push_back(DeclareReductionRef.get());
8471 else
8472 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008473 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00008474 ValueDecl *D = Res.first;
8475 if (!D)
8476 continue;
8477
Alexey Bataeva1764212015-09-30 09:22:36 +00008478 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008479 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
8480 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
8481 if (ASE)
Alexey Bataev31300ed2016-02-04 11:27:03 +00008482 Type = ASE->getType().getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008483 else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008484 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
8485 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
8486 Type = ATy->getElementType();
8487 else
8488 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +00008489 Type = Type.getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008490 } else
8491 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
8492 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +00008493
Alexey Bataevc5e02582014-06-16 07:08:35 +00008494 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8495 // A variable that appears in a private clause must not have an incomplete
8496 // type or a reference type.
8497 if (RequireCompleteType(ELoc, Type,
8498 diag::err_omp_reduction_incomplete_type))
8499 continue;
8500 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00008501 // A list item that appears in a reduction clause must not be
8502 // const-qualified.
8503 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008504 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00008505 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008506 if (!ASE && !OASE) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008507 bool IsDecl = !VD ||
8508 VD->isThisDeclarationADefinition(Context) ==
8509 VarDecl::DeclarationOnly;
8510 Diag(D->getLocation(),
Alexey Bataeva1764212015-09-30 09:22:36 +00008511 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +00008512 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +00008513 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008514 continue;
8515 }
8516 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
8517 // If a list-item is a reference type then it must bind to the same object
8518 // for all threads of the team.
Alexey Bataev60da77e2016-02-29 05:54:20 +00008519 if (!ASE && !OASE && VD) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008520 VarDecl *VDDef = VD->getDefinition();
Alexey Bataev31300ed2016-02-04 11:27:03 +00008521 if (VD->getType()->isReferenceType() && VDDef) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008522 DSARefChecker Check(DSAStack);
8523 if (Check.Visit(VDDef->getInit())) {
8524 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
8525 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
8526 continue;
8527 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008528 }
8529 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008530
Alexey Bataevc5e02582014-06-16 07:08:35 +00008531 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
8532 // in a Construct]
8533 // Variables with the predetermined data-sharing attributes may not be
8534 // listed in data-sharing attributes clauses, except for the cases
8535 // listed below. For these exceptions only, listing a predetermined
8536 // variable in a data-sharing attribute clause is allowed and overrides
8537 // the variable's predetermined data-sharing attributes.
8538 // OpenMP [2.14.3.6, Restrictions, p.3]
8539 // Any number of reduction clauses can be specified on the directive,
8540 // but a list item can appear only once in the reduction clauses for that
8541 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00008542 DSAStackTy::DSAVarData DVar;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008543 DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008544 if (DVar.CKind == OMPC_reduction) {
8545 Diag(ELoc, diag::err_omp_once_referenced)
8546 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008547 if (DVar.RefExpr)
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008548 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008549 } else if (DVar.CKind != OMPC_unknown) {
8550 Diag(ELoc, diag::err_omp_wrong_dsa)
8551 << getOpenMPClauseName(DVar.CKind)
8552 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008553 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008554 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008555 }
8556
8557 // OpenMP [2.14.3.6, Restrictions, p.1]
8558 // A list item that appears in a reduction clause of a worksharing
8559 // construct must be shared in the parallel regions to which any of the
8560 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008561 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8562 if (isOpenMPWorksharingDirective(CurrDir) &&
8563 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008564 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008565 if (DVar.CKind != OMPC_shared) {
8566 Diag(ELoc, diag::err_omp_required_access)
8567 << getOpenMPClauseName(OMPC_reduction)
8568 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008569 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008570 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00008571 }
8572 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008573
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008574 // Try to find 'declare reduction' corresponding construct before using
8575 // builtin/overloaded operators.
8576 CXXCastPath BasePath;
8577 ExprResult DeclareReductionRef = buildDeclareReductionRef(
8578 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
8579 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
8580 if (DeclareReductionRef.isInvalid())
8581 continue;
8582 if (CurContext->isDependentContext() &&
8583 (DeclareReductionRef.isUnset() ||
8584 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
8585 Vars.push_back(RefExpr);
8586 Privates.push_back(nullptr);
8587 LHSs.push_back(nullptr);
8588 RHSs.push_back(nullptr);
8589 ReductionOps.push_back(DeclareReductionRef.get());
8590 continue;
8591 }
8592 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
8593 // Not allowed reduction identifier is found.
8594 Diag(ReductionId.getLocStart(),
8595 diag::err_omp_unknown_reduction_identifier)
8596 << Type << ReductionIdRange;
8597 continue;
8598 }
8599
8600 // OpenMP [2.14.3.6, reduction clause, Restrictions]
8601 // The type of a list item that appears in a reduction clause must be valid
8602 // for the reduction-identifier. For a max or min reduction in C, the type
8603 // of the list item must be an allowed arithmetic data type: char, int,
8604 // float, double, or _Bool, possibly modified with long, short, signed, or
8605 // unsigned. For a max or min reduction in C++, the type of the list item
8606 // must be an allowed arithmetic data type: char, wchar_t, int, float,
8607 // double, or bool, possibly modified with long, short, signed, or unsigned.
8608 if (DeclareReductionRef.isUnset()) {
8609 if ((BOK == BO_GT || BOK == BO_LT) &&
8610 !(Type->isScalarType() ||
8611 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
8612 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
8613 << getLangOpts().CPlusPlus;
8614 if (!ASE && !OASE) {
8615 bool IsDecl = !VD ||
8616 VD->isThisDeclarationADefinition(Context) ==
8617 VarDecl::DeclarationOnly;
8618 Diag(D->getLocation(),
8619 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8620 << D;
8621 }
8622 continue;
8623 }
8624 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
8625 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
8626 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
8627 if (!ASE && !OASE) {
8628 bool IsDecl = !VD ||
8629 VD->isThisDeclarationADefinition(Context) ==
8630 VarDecl::DeclarationOnly;
8631 Diag(D->getLocation(),
8632 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8633 << D;
8634 }
8635 continue;
8636 }
8637 }
8638
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008639 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008640 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
Alexey Bataev60da77e2016-02-29 05:54:20 +00008641 D->hasAttrs() ? &D->getAttrs() : nullptr);
8642 auto *RHSVD = buildVarDecl(*this, ELoc, Type, D->getName(),
8643 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008644 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00008645 if (OASE ||
Alexey Bataev60da77e2016-02-29 05:54:20 +00008646 (!ASE &&
8647 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Alexey Bataev1189bd02016-01-26 12:20:39 +00008648 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008649 // Create pseudo array type for private copy. The size for this array will
8650 // be generated during codegen.
8651 // For array subscripts or single variables Private Ty is the same as Type
8652 // (type of the variable or single array element).
8653 PrivateTy = Context.getVariableArrayType(
8654 Type, new (Context) OpaqueValueExpr(SourceLocation(),
8655 Context.getSizeType(), VK_RValue),
8656 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +00008657 } else if (!ASE && !OASE &&
8658 Context.getAsArrayType(D->getType().getNonReferenceType()))
8659 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008660 // Private copy.
Alexey Bataev60da77e2016-02-29 05:54:20 +00008661 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, D->getName(),
8662 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008663 // Add initializer for private variable.
8664 Expr *Init = nullptr;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008665 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
8666 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
8667 if (DeclareReductionRef.isUsable()) {
8668 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
8669 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
8670 if (DRD->getInitializer()) {
8671 Init = DRDRef;
8672 RHSVD->setInit(DRDRef);
8673 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008674 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008675 } else {
8676 switch (BOK) {
8677 case BO_Add:
8678 case BO_Xor:
8679 case BO_Or:
8680 case BO_LOr:
8681 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
8682 if (Type->isScalarType() || Type->isAnyComplexType())
8683 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
8684 break;
8685 case BO_Mul:
8686 case BO_LAnd:
8687 if (Type->isScalarType() || Type->isAnyComplexType()) {
8688 // '*' and '&&' reduction ops - initializer is '1'.
8689 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00008690 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008691 break;
8692 case BO_And: {
8693 // '&' reduction op - initializer is '~0'.
8694 QualType OrigType = Type;
8695 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
8696 Type = ComplexTy->getElementType();
8697 if (Type->isRealFloatingType()) {
8698 llvm::APFloat InitValue =
8699 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
8700 /*isIEEE=*/true);
8701 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8702 Type, ELoc);
8703 } else if (Type->isScalarType()) {
8704 auto Size = Context.getTypeSize(Type);
8705 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
8706 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
8707 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8708 }
8709 if (Init && OrigType->isAnyComplexType()) {
8710 // Init = 0xFFFF + 0xFFFFi;
8711 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
8712 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
8713 }
8714 Type = OrigType;
8715 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008716 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008717 case BO_LT:
8718 case BO_GT: {
8719 // 'min' reduction op - initializer is 'Largest representable number in
8720 // the reduction list item type'.
8721 // 'max' reduction op - initializer is 'Least representable number in
8722 // the reduction list item type'.
8723 if (Type->isIntegerType() || Type->isPointerType()) {
8724 bool IsSigned = Type->hasSignedIntegerRepresentation();
8725 auto Size = Context.getTypeSize(Type);
8726 QualType IntTy =
8727 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
8728 llvm::APInt InitValue =
8729 (BOK != BO_LT)
8730 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
8731 : llvm::APInt::getMinValue(Size)
8732 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
8733 : llvm::APInt::getMaxValue(Size);
8734 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8735 if (Type->isPointerType()) {
8736 // Cast to pointer type.
8737 auto CastExpr = BuildCStyleCastExpr(
8738 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
8739 SourceLocation(), Init);
8740 if (CastExpr.isInvalid())
8741 continue;
8742 Init = CastExpr.get();
8743 }
8744 } else if (Type->isRealFloatingType()) {
8745 llvm::APFloat InitValue = llvm::APFloat::getLargest(
8746 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
8747 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8748 Type, ELoc);
8749 }
8750 break;
8751 }
8752 case BO_PtrMemD:
8753 case BO_PtrMemI:
8754 case BO_MulAssign:
8755 case BO_Div:
8756 case BO_Rem:
8757 case BO_Sub:
8758 case BO_Shl:
8759 case BO_Shr:
8760 case BO_LE:
8761 case BO_GE:
8762 case BO_EQ:
8763 case BO_NE:
8764 case BO_AndAssign:
8765 case BO_XorAssign:
8766 case BO_OrAssign:
8767 case BO_Assign:
8768 case BO_AddAssign:
8769 case BO_SubAssign:
8770 case BO_DivAssign:
8771 case BO_RemAssign:
8772 case BO_ShlAssign:
8773 case BO_ShrAssign:
8774 case BO_Comma:
8775 llvm_unreachable("Unexpected reduction operation");
8776 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008777 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008778 if (Init && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008779 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
8780 /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008781 } else if (!Init)
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008782 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008783 if (RHSVD->isInvalidDecl())
8784 continue;
8785 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008786 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
8787 << ReductionIdRange;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008788 bool IsDecl =
8789 !VD ||
8790 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8791 Diag(D->getLocation(),
8792 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8793 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008794 continue;
8795 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008796 // Store initializer for single element in private copy. Will be used during
8797 // codegen.
8798 PrivateVD->setInit(RHSVD->getInit());
8799 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008800 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008801 ExprResult ReductionOp;
8802 if (DeclareReductionRef.isUsable()) {
8803 QualType RedTy = DeclareReductionRef.get()->getType();
8804 QualType PtrRedTy = Context.getPointerType(RedTy);
8805 ExprResult LHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
8806 ExprResult RHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
8807 if (!BasePath.empty()) {
8808 LHS = DefaultLvalueConversion(LHS.get());
8809 RHS = DefaultLvalueConversion(RHS.get());
8810 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
8811 CK_UncheckedDerivedToBase, LHS.get(),
8812 &BasePath, LHS.get()->getValueKind());
8813 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
8814 CK_UncheckedDerivedToBase, RHS.get(),
8815 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008816 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008817 FunctionProtoType::ExtProtoInfo EPI;
8818 QualType Params[] = {PtrRedTy, PtrRedTy};
8819 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
8820 auto *OVE = new (Context) OpaqueValueExpr(
8821 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
8822 DefaultLvalueConversion(DeclareReductionRef.get()).get());
8823 Expr *Args[] = {LHS.get(), RHS.get()};
8824 ReductionOp = new (Context)
8825 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
8826 } else {
8827 ReductionOp = BuildBinOp(DSAStack->getCurScope(),
8828 ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE);
8829 if (ReductionOp.isUsable()) {
8830 if (BOK != BO_LT && BOK != BO_GT) {
8831 ReductionOp =
8832 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8833 BO_Assign, LHSDRE, ReductionOp.get());
8834 } else {
8835 auto *ConditionalOp = new (Context) ConditionalOperator(
8836 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
8837 RHSDRE, Type, VK_LValue, OK_Ordinary);
8838 ReductionOp =
8839 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8840 BO_Assign, LHSDRE, ConditionalOp);
8841 }
8842 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
8843 }
8844 if (ReductionOp.isInvalid())
8845 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008846 }
8847
Alexey Bataev60da77e2016-02-29 05:54:20 +00008848 DeclRefExpr *Ref = nullptr;
8849 Expr *VarsExpr = RefExpr->IgnoreParens();
8850 if (!VD) {
8851 if (ASE || OASE) {
8852 TransformExprToCaptures RebuildToCapture(*this, D);
8853 VarsExpr =
8854 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
8855 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +00008856 } else {
8857 VarsExpr = Ref =
8858 buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +00008859 }
8860 if (!IsOpenMPCapturedDecl(D)) {
8861 ExprCaptures.push_back(Ref->getDecl());
8862 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
8863 ExprResult RefRes = DefaultLvalueConversion(Ref);
8864 if (!RefRes.isUsable())
8865 continue;
8866 ExprResult PostUpdateRes =
8867 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
8868 SimpleRefExpr, RefRes.get());
8869 if (!PostUpdateRes.isUsable())
8870 continue;
8871 ExprPostUpdates.push_back(
8872 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +00008873 }
8874 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00008875 }
8876 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
8877 Vars.push_back(VarsExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008878 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008879 LHSs.push_back(LHSDRE);
8880 RHSs.push_back(RHSDRE);
8881 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008882 }
8883
8884 if (Vars.empty())
8885 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +00008886
Alexey Bataevc5e02582014-06-16 07:08:35 +00008887 return OMPReductionClause::Create(
8888 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008889 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008890 LHSs, RHSs, ReductionOps, buildPreInits(Context, ExprCaptures),
8891 buildPostUpdate(*this, ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +00008892}
8893
Alexey Bataevecba70f2016-04-12 11:02:11 +00008894bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
8895 SourceLocation LinLoc) {
8896 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
8897 LinKind == OMPC_LINEAR_unknown) {
8898 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
8899 return true;
8900 }
8901 return false;
8902}
8903
8904bool Sema::CheckOpenMPLinearDecl(ValueDecl *D, SourceLocation ELoc,
8905 OpenMPLinearClauseKind LinKind,
8906 QualType Type) {
8907 auto *VD = dyn_cast_or_null<VarDecl>(D);
8908 // A variable must not have an incomplete type or a reference type.
8909 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
8910 return true;
8911 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
8912 !Type->isReferenceType()) {
8913 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
8914 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
8915 return true;
8916 }
8917 Type = Type.getNonReferenceType();
8918
8919 // A list item must not be const-qualified.
8920 if (Type.isConstant(Context)) {
8921 Diag(ELoc, diag::err_omp_const_variable)
8922 << getOpenMPClauseName(OMPC_linear);
8923 if (D) {
8924 bool IsDecl =
8925 !VD ||
8926 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8927 Diag(D->getLocation(),
8928 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8929 << D;
8930 }
8931 return true;
8932 }
8933
8934 // A list item must be of integral or pointer type.
8935 Type = Type.getUnqualifiedType().getCanonicalType();
8936 const auto *Ty = Type.getTypePtrOrNull();
8937 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
8938 !Ty->isPointerType())) {
8939 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
8940 if (D) {
8941 bool IsDecl =
8942 !VD ||
8943 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8944 Diag(D->getLocation(),
8945 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8946 << D;
8947 }
8948 return true;
8949 }
8950 return false;
8951}
8952
Alexey Bataev182227b2015-08-20 10:54:39 +00008953OMPClause *Sema::ActOnOpenMPLinearClause(
8954 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
8955 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
8956 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008957 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008958 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00008959 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008960 SmallVector<Decl *, 4> ExprCaptures;
8961 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +00008962 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +00008963 LinKind = OMPC_LINEAR_val;
Alexey Bataeved09d242014-05-28 05:53:51 +00008964 for (auto &RefExpr : VarList) {
8965 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008966 SourceLocation ELoc;
8967 SourceRange ERange;
8968 Expr *SimpleRefExpr = RefExpr;
8969 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8970 /*AllowArraySection=*/false);
8971 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008972 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008973 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008974 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00008975 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008976 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008977 ValueDecl *D = Res.first;
8978 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +00008979 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +00008980
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008981 QualType Type = D->getType();
8982 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +00008983
8984 // OpenMP [2.14.3.7, linear clause]
8985 // A list-item cannot appear in more than one linear clause.
8986 // A list-item that appears in a linear clause cannot appear in any
8987 // other data-sharing attribute clause.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008988 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00008989 if (DVar.RefExpr) {
8990 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8991 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008992 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00008993 continue;
8994 }
8995
Alexey Bataevecba70f2016-04-12 11:02:11 +00008996 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +00008997 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00008998 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +00008999
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009000 // Build private copy of original var.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009001 auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(),
9002 D->hasAttrs() ? &D->getAttrs() : nullptr);
9003 auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +00009004 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009005 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009006 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009007 DeclRefExpr *Ref = nullptr;
Alexey Bataev78849fb2016-03-09 09:49:00 +00009008 if (!VD) {
9009 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
9010 if (!IsOpenMPCapturedDecl(D)) {
9011 ExprCaptures.push_back(Ref->getDecl());
9012 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
9013 ExprResult RefRes = DefaultLvalueConversion(Ref);
9014 if (!RefRes.isUsable())
9015 continue;
9016 ExprResult PostUpdateRes =
9017 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
9018 SimpleRefExpr, RefRes.get());
9019 if (!PostUpdateRes.isUsable())
9020 continue;
9021 ExprPostUpdates.push_back(
9022 IgnoredValueConversions(PostUpdateRes.get()).get());
9023 }
9024 }
9025 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009026 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009027 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009028 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009029 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009030 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009031 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
9032 auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
9033
9034 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
9035 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009036 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00009037 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00009038 }
9039
9040 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009041 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00009042
9043 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00009044 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00009045 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
9046 !Step->isInstantiationDependent() &&
9047 !Step->containsUnexpandedParameterPack()) {
9048 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00009049 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00009050 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009051 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00009052 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00009053
Alexander Musman3276a272015-03-21 10:12:56 +00009054 // Build var to save the step value.
9055 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00009056 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00009057 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00009058 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00009059 ExprResult CalcStep =
9060 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009061 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00009062
Alexander Musman8dba6642014-04-22 13:09:42 +00009063 // Warn about zero linear step (it would be probably better specified as
9064 // making corresponding variables 'const').
9065 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00009066 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
9067 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00009068 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
9069 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00009070 if (!IsConstant && CalcStep.isUsable()) {
9071 // Calculate the step beforehand instead of doing this on each iteration.
9072 // (This is not used if the number of iterations may be kfold-ed).
9073 CalcStepExpr = CalcStep.get();
9074 }
Alexander Musman8dba6642014-04-22 13:09:42 +00009075 }
9076
Alexey Bataev182227b2015-08-20 10:54:39 +00009077 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
9078 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009079 StepExpr, CalcStepExpr,
9080 buildPreInits(Context, ExprCaptures),
9081 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +00009082}
9083
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009084static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
9085 Expr *NumIterations, Sema &SemaRef,
9086 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +00009087 // Walk the vars and build update/final expressions for the CodeGen.
9088 SmallVector<Expr *, 8> Updates;
9089 SmallVector<Expr *, 8> Finals;
9090 Expr *Step = Clause.getStep();
9091 Expr *CalcStep = Clause.getCalcStep();
9092 // OpenMP [2.14.3.7, linear clause]
9093 // If linear-step is not specified it is assumed to be 1.
9094 if (Step == nullptr)
9095 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009096 else if (CalcStep) {
Alexander Musman3276a272015-03-21 10:12:56 +00009097 Step = cast<BinaryOperator>(CalcStep)->getLHS();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009098 }
Alexander Musman3276a272015-03-21 10:12:56 +00009099 bool HasErrors = false;
9100 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009101 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009102 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00009103 for (auto &RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009104 SourceLocation ELoc;
9105 SourceRange ERange;
9106 Expr *SimpleRefExpr = RefExpr;
9107 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange,
9108 /*AllowArraySection=*/false);
9109 ValueDecl *D = Res.first;
9110 if (Res.second || !D) {
9111 Updates.push_back(nullptr);
9112 Finals.push_back(nullptr);
9113 HasErrors = true;
9114 continue;
9115 }
9116 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) {
9117 D = cast<MemberExpr>(CED->getInit()->IgnoreParenImpCasts())
9118 ->getMemberDecl();
9119 }
9120 auto &&Info = Stack->isLoopControlVariable(D);
Alexander Musman3276a272015-03-21 10:12:56 +00009121 Expr *InitExpr = *CurInit;
9122
9123 // Build privatized reference to the current linear var.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009124 auto DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009125 Expr *CapturedRef;
9126 if (LinKind == OMPC_LINEAR_uval)
9127 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
9128 else
9129 CapturedRef =
9130 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
9131 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
9132 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009133
9134 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009135 ExprResult Update;
9136 if (!Info.first) {
9137 Update =
9138 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
9139 InitExpr, IV, Step, /* Subtract */ false);
9140 } else
9141 Update = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009142 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
9143 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009144
9145 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009146 ExprResult Final;
9147 if (!Info.first) {
9148 Final = BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
9149 InitExpr, NumIterations, Step,
9150 /* Subtract */ false);
9151 } else
9152 Final = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009153 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
9154 /*DiscardedValue=*/true);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009155
Alexander Musman3276a272015-03-21 10:12:56 +00009156 if (!Update.isUsable() || !Final.isUsable()) {
9157 Updates.push_back(nullptr);
9158 Finals.push_back(nullptr);
9159 HasErrors = true;
9160 } else {
9161 Updates.push_back(Update.get());
9162 Finals.push_back(Final.get());
9163 }
Richard Trieucc3949d2016-02-18 22:34:54 +00009164 ++CurInit;
9165 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00009166 }
9167 Clause.setUpdates(Updates);
9168 Clause.setFinals(Finals);
9169 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00009170}
9171
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009172OMPClause *Sema::ActOnOpenMPAlignedClause(
9173 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
9174 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
9175
9176 SmallVector<Expr *, 8> Vars;
9177 for (auto &RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +00009178 assert(RefExpr && "NULL expr in OpenMP linear clause.");
9179 SourceLocation ELoc;
9180 SourceRange ERange;
9181 Expr *SimpleRefExpr = RefExpr;
9182 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9183 /*AllowArraySection=*/false);
9184 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009185 // It will be analyzed later.
9186 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009187 }
Alexey Bataev1efd1662016-03-29 10:59:56 +00009188 ValueDecl *D = Res.first;
9189 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009190 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009191
Alexey Bataev1efd1662016-03-29 10:59:56 +00009192 QualType QType = D->getType();
9193 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009194
9195 // OpenMP [2.8.1, simd construct, Restrictions]
9196 // The type of list items appearing in the aligned clause must be
9197 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009198 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009199 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +00009200 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009201 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009202 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009203 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +00009204 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009205 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +00009206 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009207 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009208 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009209 continue;
9210 }
9211
9212 // OpenMP [2.8.1, simd construct, Restrictions]
9213 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev1efd1662016-03-29 10:59:56 +00009214 if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00009215 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009216 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
9217 << getOpenMPClauseName(OMPC_aligned);
9218 continue;
9219 }
9220
Alexey Bataev1efd1662016-03-29 10:59:56 +00009221 DeclRefExpr *Ref = nullptr;
9222 if (!VD && IsOpenMPCapturedDecl(D))
9223 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
9224 Vars.push_back(DefaultFunctionArrayConversion(
9225 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
9226 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009227 }
9228
9229 // OpenMP [2.8.1, simd construct, Description]
9230 // The parameter of the aligned clause, alignment, must be a constant
9231 // positive integer expression.
9232 // If no optional parameter is specified, implementation-defined default
9233 // alignments for SIMD instructions on the target platforms are assumed.
9234 if (Alignment != nullptr) {
9235 ExprResult AlignResult =
9236 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
9237 if (AlignResult.isInvalid())
9238 return nullptr;
9239 Alignment = AlignResult.get();
9240 }
9241 if (Vars.empty())
9242 return nullptr;
9243
9244 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
9245 EndLoc, Vars, Alignment);
9246}
9247
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009248OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
9249 SourceLocation StartLoc,
9250 SourceLocation LParenLoc,
9251 SourceLocation EndLoc) {
9252 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009253 SmallVector<Expr *, 8> SrcExprs;
9254 SmallVector<Expr *, 8> DstExprs;
9255 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00009256 for (auto &RefExpr : VarList) {
9257 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
9258 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009259 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009260 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009261 SrcExprs.push_back(nullptr);
9262 DstExprs.push_back(nullptr);
9263 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009264 continue;
9265 }
9266
Alexey Bataeved09d242014-05-28 05:53:51 +00009267 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009268 // OpenMP [2.1, C/C++]
9269 // A list item is a variable name.
9270 // OpenMP [2.14.4.1, Restrictions, p.1]
9271 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00009272 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009273 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009274 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
9275 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009276 continue;
9277 }
9278
9279 Decl *D = DE->getDecl();
9280 VarDecl *VD = cast<VarDecl>(D);
9281
9282 QualType Type = VD->getType();
9283 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
9284 // It will be analyzed later.
9285 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009286 SrcExprs.push_back(nullptr);
9287 DstExprs.push_back(nullptr);
9288 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009289 continue;
9290 }
9291
9292 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
9293 // A list item that appears in a copyin clause must be threadprivate.
9294 if (!DSAStack->isThreadPrivate(VD)) {
9295 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00009296 << getOpenMPClauseName(OMPC_copyin)
9297 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009298 continue;
9299 }
9300
9301 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9302 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00009303 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009304 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009305 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009306 auto *SrcVD =
9307 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
9308 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00009309 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009310 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
9311 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009312 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
9313 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009314 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009315 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009316 // For arrays generate assignment operation for single element and replace
9317 // it by the original array element in CodeGen.
9318 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
9319 PseudoDstExpr, PseudoSrcExpr);
9320 if (AssignmentOp.isInvalid())
9321 continue;
9322 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
9323 /*DiscardedValue=*/true);
9324 if (AssignmentOp.isInvalid())
9325 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009326
9327 DSAStack->addDSA(VD, DE, OMPC_copyin);
9328 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009329 SrcExprs.push_back(PseudoSrcExpr);
9330 DstExprs.push_back(PseudoDstExpr);
9331 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009332 }
9333
Alexey Bataeved09d242014-05-28 05:53:51 +00009334 if (Vars.empty())
9335 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009336
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009337 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9338 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009339}
9340
Alexey Bataevbae9a792014-06-27 10:37:06 +00009341OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
9342 SourceLocation StartLoc,
9343 SourceLocation LParenLoc,
9344 SourceLocation EndLoc) {
9345 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00009346 SmallVector<Expr *, 8> SrcExprs;
9347 SmallVector<Expr *, 8> DstExprs;
9348 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009349 for (auto &RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +00009350 assert(RefExpr && "NULL expr in OpenMP linear clause.");
9351 SourceLocation ELoc;
9352 SourceRange ERange;
9353 Expr *SimpleRefExpr = RefExpr;
9354 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9355 /*AllowArraySection=*/false);
9356 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00009357 // It will be analyzed later.
9358 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00009359 SrcExprs.push_back(nullptr);
9360 DstExprs.push_back(nullptr);
9361 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009362 }
Alexey Bataeve122da12016-03-17 10:50:17 +00009363 ValueDecl *D = Res.first;
9364 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +00009365 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009366
Alexey Bataeve122da12016-03-17 10:50:17 +00009367 QualType Type = D->getType();
9368 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009369
9370 // OpenMP [2.14.4.2, Restrictions, p.2]
9371 // A list item that appears in a copyprivate clause may not appear in a
9372 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +00009373 if (!VD || !DSAStack->isThreadPrivate(VD)) {
9374 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00009375 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
9376 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00009377 Diag(ELoc, diag::err_omp_wrong_dsa)
9378 << getOpenMPClauseName(DVar.CKind)
9379 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve122da12016-03-17 10:50:17 +00009380 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009381 continue;
9382 }
9383
9384 // OpenMP [2.11.4.2, Restrictions, p.1]
9385 // All list items that appear in a copyprivate clause must be either
9386 // threadprivate or private in the enclosing context.
9387 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +00009388 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009389 if (DVar.CKind == OMPC_shared) {
9390 Diag(ELoc, diag::err_omp_required_access)
9391 << getOpenMPClauseName(OMPC_copyprivate)
9392 << "threadprivate or private in the enclosing context";
Alexey Bataeve122da12016-03-17 10:50:17 +00009393 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009394 continue;
9395 }
9396 }
9397 }
9398
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009399 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009400 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009401 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009402 << getOpenMPClauseName(OMPC_copyprivate) << Type
9403 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009404 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +00009405 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009406 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +00009407 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009408 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +00009409 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009410 continue;
9411 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009412
Alexey Bataevbae9a792014-06-27 10:37:06 +00009413 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9414 // A variable of class type (or array thereof) that appears in a
9415 // copyin clause requires an accessible, unambiguous copy assignment
9416 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009417 Type = Context.getBaseElementType(Type.getNonReferenceType())
9418 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00009419 auto *SrcVD =
Alexey Bataeve122da12016-03-17 10:50:17 +00009420 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src",
9421 D->hasAttrs() ? &D->getAttrs() : nullptr);
9422 auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
Alexey Bataev420d45b2015-04-14 05:11:24 +00009423 auto *DstVD =
Alexey Bataeve122da12016-03-17 10:50:17 +00009424 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst",
9425 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00009426 auto *PseudoDstExpr =
Alexey Bataeve122da12016-03-17 10:50:17 +00009427 buildDeclRefExpr(*this, DstVD, Type, ELoc);
9428 auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
Alexey Bataeva63048e2015-03-23 06:18:07 +00009429 PseudoDstExpr, PseudoSrcExpr);
9430 if (AssignmentOp.isInvalid())
9431 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +00009432 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +00009433 /*DiscardedValue=*/true);
9434 if (AssignmentOp.isInvalid())
9435 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009436
9437 // No need to mark vars as copyprivate, they are already threadprivate or
9438 // implicitly private.
Alexey Bataeve122da12016-03-17 10:50:17 +00009439 assert(VD || IsOpenMPCapturedDecl(D));
9440 Vars.push_back(
9441 VD ? RefExpr->IgnoreParens()
9442 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +00009443 SrcExprs.push_back(PseudoSrcExpr);
9444 DstExprs.push_back(PseudoDstExpr);
9445 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00009446 }
9447
9448 if (Vars.empty())
9449 return nullptr;
9450
Alexey Bataeva63048e2015-03-23 06:18:07 +00009451 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
9452 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009453}
9454
Alexey Bataev6125da92014-07-21 11:26:11 +00009455OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
9456 SourceLocation StartLoc,
9457 SourceLocation LParenLoc,
9458 SourceLocation EndLoc) {
9459 if (VarList.empty())
9460 return nullptr;
9461
9462 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
9463}
Alexey Bataevdea47612014-07-23 07:46:59 +00009464
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009465OMPClause *
9466Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
9467 SourceLocation DepLoc, SourceLocation ColonLoc,
9468 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
9469 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00009470 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009471 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +00009472 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009473 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +00009474 return nullptr;
9475 }
9476 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009477 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
9478 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00009479 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009480 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009481 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
9482 /*Last=*/OMPC_DEPEND_unknown, Except)
9483 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009484 return nullptr;
9485 }
9486 SmallVector<Expr *, 8> Vars;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009487 llvm::APSInt DepCounter(/*BitWidth=*/32);
9488 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
9489 if (DepKind == OMPC_DEPEND_sink) {
9490 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
9491 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
9492 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009493 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009494 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009495 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
9496 DSAStack->getParentOrderedRegionParam()) {
9497 for (auto &RefExpr : VarList) {
9498 assert(RefExpr && "NULL expr in OpenMP shared clause.");
9499 if (isa<DependentScopeDeclRefExpr>(RefExpr) ||
9500 (DepKind == OMPC_DEPEND_sink && CurContext->isDependentContext())) {
9501 // It will be analyzed later.
9502 Vars.push_back(RefExpr);
9503 continue;
9504 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009505
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009506 SourceLocation ELoc = RefExpr->getExprLoc();
9507 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
9508 if (DepKind == OMPC_DEPEND_sink) {
9509 if (DepCounter >= TotalDepCount) {
9510 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
9511 continue;
9512 }
9513 ++DepCounter;
9514 // OpenMP [2.13.9, Summary]
9515 // depend(dependence-type : vec), where dependence-type is:
9516 // 'sink' and where vec is the iteration vector, which has the form:
9517 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
9518 // where n is the value specified by the ordered clause in the loop
9519 // directive, xi denotes the loop iteration variable of the i-th nested
9520 // loop associated with the loop directive, and di is a constant
9521 // non-negative integer.
9522 SimpleExpr = SimpleExpr->IgnoreImplicit();
9523 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
9524 if (!DE) {
9525 OverloadedOperatorKind OOK = OO_None;
9526 SourceLocation OOLoc;
9527 Expr *LHS, *RHS;
9528 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
9529 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
9530 OOLoc = BO->getOperatorLoc();
9531 LHS = BO->getLHS()->IgnoreParenImpCasts();
9532 RHS = BO->getRHS()->IgnoreParenImpCasts();
9533 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
9534 OOK = OCE->getOperator();
9535 OOLoc = OCE->getOperatorLoc();
9536 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
9537 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
9538 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
9539 OOK = MCE->getMethodDecl()
9540 ->getNameInfo()
9541 .getName()
9542 .getCXXOverloadedOperator();
9543 OOLoc = MCE->getCallee()->getExprLoc();
9544 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
9545 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
9546 } else {
9547 Diag(ELoc, diag::err_omp_depend_sink_wrong_expr);
9548 continue;
9549 }
9550 DE = dyn_cast<DeclRefExpr>(LHS);
9551 if (!DE) {
9552 Diag(LHS->getExprLoc(),
9553 diag::err_omp_depend_sink_expected_loop_iteration)
9554 << DSAStack->getParentLoopControlVariable(
9555 DepCounter.getZExtValue());
9556 continue;
9557 }
9558 if (OOK != OO_Plus && OOK != OO_Minus) {
9559 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
9560 continue;
9561 }
9562 ExprResult Res = VerifyPositiveIntegerConstantInClause(
9563 RHS, OMPC_depend, /*StrictlyPositive=*/false);
9564 if (Res.isInvalid())
9565 continue;
9566 }
9567 auto *VD = dyn_cast<VarDecl>(DE->getDecl());
9568 if (!CurContext->isDependentContext() &&
9569 DSAStack->getParentOrderedRegionParam() &&
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00009570 (!VD ||
9571 DepCounter != DSAStack->isParentLoopControlVariable(VD).first)) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009572 Diag(DE->getExprLoc(),
9573 diag::err_omp_depend_sink_expected_loop_iteration)
9574 << DSAStack->getParentLoopControlVariable(
9575 DepCounter.getZExtValue());
9576 continue;
9577 }
9578 } else {
9579 // OpenMP [2.11.1.1, Restrictions, p.3]
9580 // A variable that is part of another variable (such as a field of a
9581 // structure) but is not an array element or an array section cannot
9582 // appear in a depend clause.
9583 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
9584 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
9585 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
9586 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
9587 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev31300ed2016-02-04 11:27:03 +00009588 (ASE &&
9589 !ASE->getBase()
9590 ->getType()
9591 .getNonReferenceType()
9592 ->isPointerType() &&
9593 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009594 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
9595 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009596 continue;
9597 }
9598 }
9599
9600 Vars.push_back(RefExpr->IgnoreParenImpCasts());
9601 }
9602
9603 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
9604 TotalDepCount > VarList.size() &&
9605 DSAStack->getParentOrderedRegionParam()) {
9606 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
9607 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
9608 }
9609 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
9610 Vars.empty())
9611 return nullptr;
9612 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009613
9614 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
9615 DepLoc, ColonLoc, Vars);
9616}
Michael Wonge710d542015-08-07 16:16:36 +00009617
9618OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
9619 SourceLocation LParenLoc,
9620 SourceLocation EndLoc) {
9621 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00009622
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009623 // OpenMP [2.9.1, Restrictions]
9624 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009625 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
9626 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009627 return nullptr;
9628
Michael Wonge710d542015-08-07 16:16:36 +00009629 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9630}
Kelvin Li0bff7af2015-11-23 05:32:03 +00009631
9632static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
9633 DSAStackTy *Stack, CXXRecordDecl *RD) {
9634 if (!RD || RD->isInvalidDecl())
9635 return true;
9636
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00009637 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
9638 if (auto *CTD = CTSD->getSpecializedTemplate())
9639 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009640 auto QTy = SemaRef.Context.getRecordType(RD);
9641 if (RD->isDynamicClass()) {
9642 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9643 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
9644 return false;
9645 }
9646 auto *DC = RD;
9647 bool IsCorrect = true;
9648 for (auto *I : DC->decls()) {
9649 if (I) {
9650 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
9651 if (MD->isStatic()) {
9652 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9653 SemaRef.Diag(MD->getLocation(),
9654 diag::note_omp_static_member_in_target);
9655 IsCorrect = false;
9656 }
9657 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
9658 if (VD->isStaticDataMember()) {
9659 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9660 SemaRef.Diag(VD->getLocation(),
9661 diag::note_omp_static_member_in_target);
9662 IsCorrect = false;
9663 }
9664 }
9665 }
9666 }
9667
9668 for (auto &I : RD->bases()) {
9669 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
9670 I.getType()->getAsCXXRecordDecl()))
9671 IsCorrect = false;
9672 }
9673 return IsCorrect;
9674}
9675
9676static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
9677 DSAStackTy *Stack, QualType QTy) {
9678 NamedDecl *ND;
9679 if (QTy->isIncompleteType(&ND)) {
9680 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
9681 return false;
9682 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
9683 if (!RD->isInvalidDecl() &&
9684 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
9685 return false;
9686 }
9687 return true;
9688}
9689
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009690/// \brief Return true if it can be proven that the provided array expression
9691/// (array section or array subscript) does NOT specify the whole size of the
9692/// array whose base type is \a BaseQTy.
9693static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
9694 const Expr *E,
9695 QualType BaseQTy) {
9696 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
9697
9698 // If this is an array subscript, it refers to the whole size if the size of
9699 // the dimension is constant and equals 1. Also, an array section assumes the
9700 // format of an array subscript if no colon is used.
9701 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
9702 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
9703 return ATy->getSize().getSExtValue() != 1;
9704 // Size can't be evaluated statically.
9705 return false;
9706 }
9707
9708 assert(OASE && "Expecting array section if not an array subscript.");
9709 auto *LowerBound = OASE->getLowerBound();
9710 auto *Length = OASE->getLength();
9711
9712 // If there is a lower bound that does not evaluates to zero, we are not
9713 // convering the whole dimension.
9714 if (LowerBound) {
9715 llvm::APSInt ConstLowerBound;
9716 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
9717 return false; // Can't get the integer value as a constant.
9718 if (ConstLowerBound.getSExtValue())
9719 return true;
9720 }
9721
9722 // If we don't have a length we covering the whole dimension.
9723 if (!Length)
9724 return false;
9725
9726 // If the base is a pointer, we don't have a way to get the size of the
9727 // pointee.
9728 if (BaseQTy->isPointerType())
9729 return false;
9730
9731 // We can only check if the length is the same as the size of the dimension
9732 // if we have a constant array.
9733 auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
9734 if (!CATy)
9735 return false;
9736
9737 llvm::APSInt ConstLength;
9738 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
9739 return false; // Can't get the integer value as a constant.
9740
9741 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
9742}
9743
9744// Return true if it can be proven that the provided array expression (array
9745// section or array subscript) does NOT specify a single element of the array
9746// whose base type is \a BaseQTy.
9747static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
9748 const Expr *E,
9749 QualType BaseQTy) {
9750 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
9751
9752 // An array subscript always refer to a single element. Also, an array section
9753 // assumes the format of an array subscript if no colon is used.
9754 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
9755 return false;
9756
9757 assert(OASE && "Expecting array section if not an array subscript.");
9758 auto *Length = OASE->getLength();
9759
9760 // If we don't have a length we have to check if the array has unitary size
9761 // for this dimension. Also, we should always expect a length if the base type
9762 // is pointer.
9763 if (!Length) {
9764 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
9765 return ATy->getSize().getSExtValue() != 1;
9766 // We cannot assume anything.
9767 return false;
9768 }
9769
9770 // Check if the length evaluates to 1.
9771 llvm::APSInt ConstLength;
9772 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
9773 return false; // Can't get the integer value as a constant.
9774
9775 return ConstLength.getSExtValue() != 1;
9776}
9777
Samuel Antao5de996e2016-01-22 20:21:36 +00009778// Return the expression of the base of the map clause or null if it cannot
9779// be determined and do all the necessary checks to see if the expression is
Samuel Antao90927002016-04-26 14:54:23 +00009780// valid as a standalone map clause expression. In the process, record all the
9781// components of the expression.
9782static Expr *CheckMapClauseExpressionBase(
9783 Sema &SemaRef, Expr *E,
9784 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009785 SourceLocation ELoc = E->getExprLoc();
9786 SourceRange ERange = E->getSourceRange();
9787
9788 // The base of elements of list in a map clause have to be either:
9789 // - a reference to variable or field.
9790 // - a member expression.
9791 // - an array expression.
9792 //
9793 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
9794 // reference to 'r'.
9795 //
9796 // If we have:
9797 //
9798 // struct SS {
9799 // Bla S;
9800 // foo() {
9801 // #pragma omp target map (S.Arr[:12]);
9802 // }
9803 // }
9804 //
9805 // We want to retrieve the member expression 'this->S';
9806
9807 Expr *RelevantExpr = nullptr;
9808
Samuel Antao5de996e2016-01-22 20:21:36 +00009809 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
9810 // If a list item is an array section, it must specify contiguous storage.
9811 //
9812 // For this restriction it is sufficient that we make sure only references
9813 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009814 // exist except in the rightmost expression (unless they cover the whole
9815 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +00009816 //
9817 // r.ArrS[3:5].Arr[6:7]
9818 //
9819 // r.ArrS[3:5].x
9820 //
9821 // but these would be valid:
9822 // r.ArrS[3].Arr[6:7]
9823 //
9824 // r.ArrS[3].x
9825
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009826 bool AllowUnitySizeArraySection = true;
9827 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +00009828
Dmitry Polukhin644a9252016-03-11 07:58:34 +00009829 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009830 E = E->IgnoreParenImpCasts();
9831
9832 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
9833 if (!isa<VarDecl>(CurE->getDecl()))
9834 break;
9835
9836 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009837
9838 // If we got a reference to a declaration, we should not expect any array
9839 // section before that.
9840 AllowUnitySizeArraySection = false;
9841 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +00009842
9843 // Record the component.
9844 CurComponents.push_back(OMPClauseMappableExprCommon::MappableComponent(
9845 CurE, CurE->getDecl()));
Samuel Antao5de996e2016-01-22 20:21:36 +00009846 continue;
9847 }
9848
9849 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
9850 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
9851
9852 if (isa<CXXThisExpr>(BaseE))
9853 // We found a base expression: this->Val.
9854 RelevantExpr = CurE;
9855 else
9856 E = BaseE;
9857
9858 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
9859 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
9860 << CurE->getSourceRange();
9861 break;
9862 }
9863
9864 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
9865
9866 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
9867 // A bit-field cannot appear in a map clause.
9868 //
9869 if (FD->isBitField()) {
9870 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_map_clause)
9871 << CurE->getSourceRange();
9872 break;
9873 }
9874
9875 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9876 // If the type of a list item is a reference to a type T then the type
9877 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009878 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +00009879
9880 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
9881 // A list item cannot be a variable that is a member of a structure with
9882 // a union type.
9883 //
9884 if (auto *RT = CurType->getAs<RecordType>())
9885 if (RT->isUnionType()) {
9886 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
9887 << CurE->getSourceRange();
9888 break;
9889 }
9890
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009891 // If we got a member expression, we should not expect any array section
9892 // before that:
9893 //
9894 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
9895 // If a list item is an element of a structure, only the rightmost symbol
9896 // of the variable reference can be an array section.
9897 //
9898 AllowUnitySizeArraySection = false;
9899 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +00009900
9901 // Record the component.
9902 CurComponents.push_back(
9903 OMPClauseMappableExprCommon::MappableComponent(CurE, FD));
Samuel Antao5de996e2016-01-22 20:21:36 +00009904 continue;
9905 }
9906
9907 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
9908 E = CurE->getBase()->IgnoreParenImpCasts();
9909
9910 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
9911 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9912 << 0 << CurE->getSourceRange();
9913 break;
9914 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009915
9916 // If we got an array subscript that express the whole dimension we
9917 // can have any array expressions before. If it only expressing part of
9918 // the dimension, we can only have unitary-size array expressions.
9919 if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
9920 E->getType()))
9921 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +00009922
9923 // Record the component - we don't have any declaration associated.
9924 CurComponents.push_back(
9925 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +00009926 continue;
9927 }
9928
9929 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009930 E = CurE->getBase()->IgnoreParenImpCasts();
9931
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009932 auto CurType =
9933 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
9934
Samuel Antao5de996e2016-01-22 20:21:36 +00009935 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9936 // If the type of a list item is a reference to a type T then the type
9937 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +00009938 if (CurType->isReferenceType())
9939 CurType = CurType->getPointeeType();
9940
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009941 bool IsPointer = CurType->isAnyPointerType();
9942
9943 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009944 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9945 << 0 << CurE->getSourceRange();
9946 break;
9947 }
9948
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009949 bool NotWhole =
9950 CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
9951 bool NotUnity =
9952 CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
9953
9954 if (AllowWholeSizeArraySection && AllowUnitySizeArraySection) {
9955 // Any array section is currently allowed.
9956 //
9957 // If this array section refers to the whole dimension we can still
9958 // accept other array sections before this one, except if the base is a
9959 // pointer. Otherwise, only unitary sections are accepted.
9960 if (NotWhole || IsPointer)
9961 AllowWholeSizeArraySection = false;
9962 } else if ((AllowUnitySizeArraySection && NotUnity) ||
9963 (AllowWholeSizeArraySection && NotWhole)) {
9964 // A unity or whole array section is not allowed and that is not
9965 // compatible with the properties of the current array section.
9966 SemaRef.Diag(
9967 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
9968 << CurE->getSourceRange();
9969 break;
9970 }
Samuel Antao90927002016-04-26 14:54:23 +00009971
9972 // Record the component - we don't have any declaration associated.
9973 CurComponents.push_back(
9974 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +00009975 continue;
9976 }
9977
9978 // If nothing else worked, this is not a valid map clause expression.
9979 SemaRef.Diag(ELoc,
9980 diag::err_omp_expected_named_var_member_or_array_expression)
9981 << ERange;
9982 break;
9983 }
9984
9985 return RelevantExpr;
9986}
9987
9988// Return true if expression E associated with value VD has conflicts with other
9989// map information.
Samuel Antao90927002016-04-26 14:54:23 +00009990static bool CheckMapConflicts(
9991 Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD, Expr *E,
9992 bool CurrentRegionOnly,
9993 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009994 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +00009995 SourceLocation ELoc = E->getExprLoc();
9996 SourceRange ERange = E->getSourceRange();
9997
9998 // In order to easily check the conflicts we need to match each component of
9999 // the expression under test with the components of the expressions that are
10000 // already in the stack.
10001
Samuel Antao5de996e2016-01-22 20:21:36 +000010002 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000010003 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000010004 "Map clause expression with unexpected base!");
10005
10006 // Variables to help detecting enclosing problems in data environment nests.
10007 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +000010008 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000010009
Samuel Antao90927002016-04-26 14:54:23 +000010010 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
10011 VD, CurrentRegionOnly,
10012 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
10013 StackComponents) -> bool {
10014
Samuel Antao5de996e2016-01-22 20:21:36 +000010015 assert(!StackComponents.empty() &&
10016 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000010017 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000010018 "Map clause expression with unexpected base!");
10019
Samuel Antao90927002016-04-26 14:54:23 +000010020 // The whole expression in the stack.
10021 auto *RE = StackComponents.front().getAssociatedExpression();
10022
Samuel Antao5de996e2016-01-22 20:21:36 +000010023 // Expressions must start from the same base. Here we detect at which
10024 // point both expressions diverge from each other and see if we can
10025 // detect if the memory referred to both expressions is contiguous and
10026 // do not overlap.
10027 auto CI = CurComponents.rbegin();
10028 auto CE = CurComponents.rend();
10029 auto SI = StackComponents.rbegin();
10030 auto SE = StackComponents.rend();
10031 for (; CI != CE && SI != SE; ++CI, ++SI) {
10032
10033 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
10034 // At most one list item can be an array item derived from a given
10035 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +000010036 if (CurrentRegionOnly &&
10037 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
10038 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
10039 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
10040 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
10041 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +000010042 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +000010043 << CI->getAssociatedExpression()->getSourceRange();
10044 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
10045 diag::note_used_here)
10046 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +000010047 return true;
10048 }
10049
10050 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +000010051 if (CI->getAssociatedExpression()->getStmtClass() !=
10052 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +000010053 break;
10054
10055 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +000010056 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +000010057 break;
10058 }
10059
10060 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10061 // List items of map clauses in the same construct must not share
10062 // original storage.
10063 //
10064 // If the expressions are exactly the same or one is a subset of the
10065 // other, it means they are sharing storage.
10066 if (CI == CE && SI == SE) {
10067 if (CurrentRegionOnly) {
10068 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10069 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10070 << RE->getSourceRange();
10071 return true;
10072 } else {
10073 // If we find the same expression in the enclosing data environment,
10074 // that is legal.
10075 IsEnclosedByDataEnvironmentExpr = true;
10076 return false;
10077 }
10078 }
10079
Samuel Antao90927002016-04-26 14:54:23 +000010080 QualType DerivedType =
10081 std::prev(CI)->getAssociatedDeclaration()->getType();
10082 SourceLocation DerivedLoc =
10083 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000010084
10085 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10086 // If the type of a list item is a reference to a type T then the type
10087 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000010088 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010089
10090 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
10091 // A variable for which the type is pointer and an array section
10092 // derived from that variable must not appear as list items of map
10093 // clauses of the same construct.
10094 //
10095 // Also, cover one of the cases in:
10096 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10097 // If any part of the original storage of a list item has corresponding
10098 // storage in the device data environment, all of the original storage
10099 // must have corresponding storage in the device data environment.
10100 //
10101 if (DerivedType->isAnyPointerType()) {
10102 if (CI == CE || SI == SE) {
10103 SemaRef.Diag(
10104 DerivedLoc,
10105 diag::err_omp_pointer_mapped_along_with_derived_section)
10106 << DerivedLoc;
10107 } else {
10108 assert(CI != CE && SI != SE);
10109 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
10110 << DerivedLoc;
10111 }
10112 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10113 << RE->getSourceRange();
10114 return true;
10115 }
10116
10117 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10118 // List items of map clauses in the same construct must not share
10119 // original storage.
10120 //
10121 // An expression is a subset of the other.
10122 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
10123 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10124 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10125 << RE->getSourceRange();
10126 return true;
10127 }
10128
10129 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000010130 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000010131 if (!CurrentRegionOnly && SI != SE)
10132 EnclosingExpr = RE;
10133
10134 // The current expression is a subset of the expression in the data
10135 // environment.
10136 IsEnclosedByDataEnvironmentExpr |=
10137 (!CurrentRegionOnly && CI != CE && SI == SE);
10138
10139 return false;
10140 });
10141
10142 if (CurrentRegionOnly)
10143 return FoundError;
10144
10145 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10146 // If any part of the original storage of a list item has corresponding
10147 // storage in the device data environment, all of the original storage must
10148 // have corresponding storage in the device data environment.
10149 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
10150 // If a list item is an element of a structure, and a different element of
10151 // the structure has a corresponding list item in the device data environment
10152 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000010153 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000010154 // data environment prior to the task encountering the construct.
10155 //
10156 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
10157 SemaRef.Diag(ELoc,
10158 diag::err_omp_original_storage_is_shared_and_does_not_contain)
10159 << ERange;
10160 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
10161 << EnclosingExpr->getSourceRange();
10162 return true;
10163 }
10164
10165 return FoundError;
10166}
10167
Samuel Antao23abd722016-01-19 20:40:49 +000010168OMPClause *
10169Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
10170 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
10171 SourceLocation MapLoc, SourceLocation ColonLoc,
10172 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
10173 SourceLocation LParenLoc, SourceLocation EndLoc) {
Kelvin Li0bff7af2015-11-23 05:32:03 +000010174 SmallVector<Expr *, 4> Vars;
10175
Samuel Antao90927002016-04-26 14:54:23 +000010176 // Keep track of the mappable components and base declarations in this clause.
10177 // Each entry in the list is going to have a list of components associated. We
10178 // record each set of the components so that we can build the clause later on.
10179 // In the end we should have the same amount of declarations and component
10180 // lists.
10181 OMPClauseMappableExprCommon::MappableExprComponentLists ClauseComponents;
10182 SmallVector<ValueDecl *, 16> ClauseBaseDeclarations;
10183
10184 ClauseComponents.reserve(VarList.size());
10185 ClauseBaseDeclarations.reserve(VarList.size());
10186
Kelvin Li0bff7af2015-11-23 05:32:03 +000010187 for (auto &RE : VarList) {
10188 assert(RE && "Null expr in omp map");
10189 if (isa<DependentScopeDeclRefExpr>(RE)) {
10190 // It will be analyzed later.
10191 Vars.push_back(RE);
10192 continue;
10193 }
10194 SourceLocation ELoc = RE->getExprLoc();
10195
Kelvin Li0bff7af2015-11-23 05:32:03 +000010196 auto *VE = RE->IgnoreParenLValueCasts();
10197
10198 if (VE->isValueDependent() || VE->isTypeDependent() ||
10199 VE->isInstantiationDependent() ||
10200 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010201 // We can only analyze this information once the missing information is
10202 // resolved.
Kelvin Li0bff7af2015-11-23 05:32:03 +000010203 Vars.push_back(RE);
10204 continue;
10205 }
10206
10207 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010208
Samuel Antao5de996e2016-01-22 20:21:36 +000010209 if (!RE->IgnoreParenImpCasts()->isLValue()) {
10210 Diag(ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
10211 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010212 continue;
10213 }
10214
Samuel Antao90927002016-04-26 14:54:23 +000010215 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
10216 ValueDecl *CurDeclaration = nullptr;
10217
10218 // Obtain the array or member expression bases if required. Also, fill the
10219 // components array with all the components identified in the process.
10220 auto *BE = CheckMapClauseExpressionBase(*this, SimpleExpr, CurComponents);
Samuel Antao5de996e2016-01-22 20:21:36 +000010221 if (!BE)
10222 continue;
10223
Samuel Antao90927002016-04-26 14:54:23 +000010224 assert(!CurComponents.empty() &&
10225 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000010226
Samuel Antao90927002016-04-26 14:54:23 +000010227 // For the following checks, we rely on the base declaration which is
10228 // expected to be associated with the last component. The declaration is
10229 // expected to be a variable or a field (if 'this' is being mapped).
10230 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
10231 assert(CurDeclaration && "Null decl on map clause.");
10232 assert(
10233 CurDeclaration->isCanonicalDecl() &&
10234 "Expecting components to have associated only canonical declarations.");
10235
10236 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
10237 auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000010238
10239 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000010240 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000010241
10242 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
10243 // threadprivate variables cannot appear in a map clause.
10244 if (VD && DSAStack->isThreadPrivate(VD)) {
Kelvin Li0bff7af2015-11-23 05:32:03 +000010245 auto DVar = DSAStack->getTopDSA(VD, false);
10246 Diag(ELoc, diag::err_omp_threadprivate_in_map);
10247 ReportOriginalDSA(*this, DSAStack, VD, DVar);
10248 continue;
10249 }
10250
Samuel Antao5de996e2016-01-22 20:21:36 +000010251 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
10252 // A list item cannot appear in both a map clause and a data-sharing
10253 // attribute clause on the same construct.
10254 //
10255 // TODO: Implement this check - it cannot currently be tested because of
10256 // missing implementation of the other data sharing clauses in target
10257 // directives.
Kelvin Li0bff7af2015-11-23 05:32:03 +000010258
Samuel Antao5de996e2016-01-22 20:21:36 +000010259 // Check conflicts with other map clause expressions. We check the conflicts
10260 // with the current construct separately from the enclosing data
10261 // environment, because the restrictions are different.
Samuel Antao90927002016-04-26 14:54:23 +000010262 if (CheckMapConflicts(*this, DSAStack, CurDeclaration, SimpleExpr,
10263 /*CurrentRegionOnly=*/true, CurComponents))
Samuel Antao5de996e2016-01-22 20:21:36 +000010264 break;
Samuel Antao90927002016-04-26 14:54:23 +000010265 if (CheckMapConflicts(*this, DSAStack, CurDeclaration, SimpleExpr,
10266 /*CurrentRegionOnly=*/false, CurComponents))
Samuel Antao5de996e2016-01-22 20:21:36 +000010267 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000010268
Samuel Antao5de996e2016-01-22 20:21:36 +000010269 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10270 // If the type of a list item is a reference to a type T then the type will
10271 // be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000010272 QualType Type = CurDeclaration->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010273
10274 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000010275 // A list item must have a mappable type.
10276 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
10277 DSAStack, Type))
10278 continue;
10279
Samuel Antaodf67fc42016-01-19 19:15:56 +000010280 // target enter data
10281 // OpenMP [2.10.2, Restrictions, p. 99]
10282 // A map-type must be specified in all map clauses and must be either
10283 // to or alloc.
10284 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
10285 if (DKind == OMPD_target_enter_data &&
10286 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
10287 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +000010288 << (IsMapTypeImplicit ? 1 : 0)
10289 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antaodf67fc42016-01-19 19:15:56 +000010290 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010291 continue;
Samuel Antaodf67fc42016-01-19 19:15:56 +000010292 }
10293
Samuel Antao72590762016-01-19 20:04:50 +000010294 // target exit_data
10295 // OpenMP [2.10.3, Restrictions, p. 102]
10296 // A map-type must be specified in all map clauses and must be either
10297 // from, release, or delete.
10298 DKind = DSAStack->getCurrentDirective();
10299 if (DKind == OMPD_target_exit_data &&
10300 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
10301 MapType == OMPC_MAP_delete)) {
10302 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +000010303 << (IsMapTypeImplicit ? 1 : 0)
10304 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antao72590762016-01-19 20:04:50 +000010305 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010306 continue;
Samuel Antao72590762016-01-19 20:04:50 +000010307 }
10308
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010309 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
10310 // A list item cannot appear in both a map clause and a data-sharing
10311 // attribute clause on the same construct
10312 if (DKind == OMPD_target && VD) {
10313 auto DVar = DSAStack->getTopDSA(VD, false);
10314 if (isOpenMPPrivate(DVar.CKind)) {
10315 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
10316 << getOpenMPClauseName(DVar.CKind)
10317 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Samuel Antao90927002016-04-26 14:54:23 +000010318 ReportOriginalDSA(*this, DSAStack, CurDeclaration, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010319 continue;
10320 }
10321 }
10322
Samuel Antao90927002016-04-26 14:54:23 +000010323 // Save the current expression.
Kelvin Li0bff7af2015-11-23 05:32:03 +000010324 Vars.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000010325
10326 // Store the components in the stack so that they can be used to check
10327 // against other clauses later on.
10328 DSAStack->addMappableExpressionComponents(CurDeclaration, CurComponents);
10329
10330 // Save the components and declaration to create the clause. For purposes of
10331 // the clause creation, any component list that has has base 'this' uses
10332 // null has
10333 ClauseComponents.resize(ClauseComponents.size() + 1);
10334 ClauseComponents.back().append(CurComponents.begin(), CurComponents.end());
10335 ClauseBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
10336 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010337 }
Kelvin Li0bff7af2015-11-23 05:32:03 +000010338
Samuel Antao5de996e2016-01-22 20:21:36 +000010339 // We need to produce a map clause even if we don't have variables so that
10340 // other diagnostics related with non-existing map clauses are accurate.
Samuel Antao90927002016-04-26 14:54:23 +000010341 return OMPMapClause::Create(
10342 Context, StartLoc, LParenLoc, EndLoc, Vars, ClauseBaseDeclarations,
10343 ClauseComponents, MapTypeModifier, MapType, IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010344}
Kelvin Li099bb8c2015-11-24 20:50:12 +000010345
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010346QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
10347 TypeResult ParsedType) {
10348 assert(ParsedType.isUsable());
10349
10350 QualType ReductionType = GetTypeFromParser(ParsedType.get());
10351 if (ReductionType.isNull())
10352 return QualType();
10353
10354 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
10355 // A type name in a declare reduction directive cannot be a function type, an
10356 // array type, a reference type, or a type qualified with const, volatile or
10357 // restrict.
10358 if (ReductionType.hasQualifiers()) {
10359 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
10360 return QualType();
10361 }
10362
10363 if (ReductionType->isFunctionType()) {
10364 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
10365 return QualType();
10366 }
10367 if (ReductionType->isReferenceType()) {
10368 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
10369 return QualType();
10370 }
10371 if (ReductionType->isArrayType()) {
10372 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
10373 return QualType();
10374 }
10375 return ReductionType;
10376}
10377
10378Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
10379 Scope *S, DeclContext *DC, DeclarationName Name,
10380 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
10381 AccessSpecifier AS, Decl *PrevDeclInScope) {
10382 SmallVector<Decl *, 8> Decls;
10383 Decls.reserve(ReductionTypes.size());
10384
10385 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
10386 ForRedeclaration);
10387 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
10388 // A reduction-identifier may not be re-declared in the current scope for the
10389 // same type or for a type that is compatible according to the base language
10390 // rules.
10391 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
10392 OMPDeclareReductionDecl *PrevDRD = nullptr;
10393 bool InCompoundScope = true;
10394 if (S != nullptr) {
10395 // Find previous declaration with the same name not referenced in other
10396 // declarations.
10397 FunctionScopeInfo *ParentFn = getEnclosingFunction();
10398 InCompoundScope =
10399 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
10400 LookupName(Lookup, S);
10401 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
10402 /*AllowInlineNamespace=*/false);
10403 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
10404 auto Filter = Lookup.makeFilter();
10405 while (Filter.hasNext()) {
10406 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
10407 if (InCompoundScope) {
10408 auto I = UsedAsPrevious.find(PrevDecl);
10409 if (I == UsedAsPrevious.end())
10410 UsedAsPrevious[PrevDecl] = false;
10411 if (auto *D = PrevDecl->getPrevDeclInScope())
10412 UsedAsPrevious[D] = true;
10413 }
10414 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
10415 PrevDecl->getLocation();
10416 }
10417 Filter.done();
10418 if (InCompoundScope) {
10419 for (auto &PrevData : UsedAsPrevious) {
10420 if (!PrevData.second) {
10421 PrevDRD = PrevData.first;
10422 break;
10423 }
10424 }
10425 }
10426 } else if (PrevDeclInScope != nullptr) {
10427 auto *PrevDRDInScope = PrevDRD =
10428 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
10429 do {
10430 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
10431 PrevDRDInScope->getLocation();
10432 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
10433 } while (PrevDRDInScope != nullptr);
10434 }
10435 for (auto &TyData : ReductionTypes) {
10436 auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
10437 bool Invalid = false;
10438 if (I != PreviousRedeclTypes.end()) {
10439 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
10440 << TyData.first;
10441 Diag(I->second, diag::note_previous_definition);
10442 Invalid = true;
10443 }
10444 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
10445 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
10446 Name, TyData.first, PrevDRD);
10447 DC->addDecl(DRD);
10448 DRD->setAccess(AS);
10449 Decls.push_back(DRD);
10450 if (Invalid)
10451 DRD->setInvalidDecl();
10452 else
10453 PrevDRD = DRD;
10454 }
10455
10456 return DeclGroupPtrTy::make(
10457 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
10458}
10459
10460void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
10461 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10462
10463 // Enter new function scope.
10464 PushFunctionScope();
10465 getCurFunction()->setHasBranchProtectedScope();
10466 getCurFunction()->setHasOMPDeclareReductionCombiner();
10467
10468 if (S != nullptr)
10469 PushDeclContext(S, DRD);
10470 else
10471 CurContext = DRD;
10472
10473 PushExpressionEvaluationContext(PotentiallyEvaluated);
10474
10475 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010476 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
10477 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
10478 // uses semantics of argument handles by value, but it should be passed by
10479 // reference. C lang does not support references, so pass all parameters as
10480 // pointers.
10481 // Create 'T omp_in;' variable.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010482 auto *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010483 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010484 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
10485 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
10486 // uses semantics of argument handles by value, but it should be passed by
10487 // reference. C lang does not support references, so pass all parameters as
10488 // pointers.
10489 // Create 'T omp_out;' variable.
10490 auto *OmpOutParm =
10491 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
10492 if (S != nullptr) {
10493 PushOnScopeChains(OmpInParm, S);
10494 PushOnScopeChains(OmpOutParm, S);
10495 } else {
10496 DRD->addDecl(OmpInParm);
10497 DRD->addDecl(OmpOutParm);
10498 }
10499}
10500
10501void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
10502 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10503 DiscardCleanupsInEvaluationContext();
10504 PopExpressionEvaluationContext();
10505
10506 PopDeclContext();
10507 PopFunctionScopeInfo();
10508
10509 if (Combiner != nullptr)
10510 DRD->setCombiner(Combiner);
10511 else
10512 DRD->setInvalidDecl();
10513}
10514
10515void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
10516 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10517
10518 // Enter new function scope.
10519 PushFunctionScope();
10520 getCurFunction()->setHasBranchProtectedScope();
10521
10522 if (S != nullptr)
10523 PushDeclContext(S, DRD);
10524 else
10525 CurContext = DRD;
10526
10527 PushExpressionEvaluationContext(PotentiallyEvaluated);
10528
10529 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010530 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
10531 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
10532 // uses semantics of argument handles by value, but it should be passed by
10533 // reference. C lang does not support references, so pass all parameters as
10534 // pointers.
10535 // Create 'T omp_priv;' variable.
10536 auto *OmpPrivParm =
10537 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010538 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
10539 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
10540 // uses semantics of argument handles by value, but it should be passed by
10541 // reference. C lang does not support references, so pass all parameters as
10542 // pointers.
10543 // Create 'T omp_orig;' variable.
10544 auto *OmpOrigParm =
10545 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010546 if (S != nullptr) {
10547 PushOnScopeChains(OmpPrivParm, S);
10548 PushOnScopeChains(OmpOrigParm, S);
10549 } else {
10550 DRD->addDecl(OmpPrivParm);
10551 DRD->addDecl(OmpOrigParm);
10552 }
10553}
10554
10555void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D,
10556 Expr *Initializer) {
10557 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10558 DiscardCleanupsInEvaluationContext();
10559 PopExpressionEvaluationContext();
10560
10561 PopDeclContext();
10562 PopFunctionScopeInfo();
10563
10564 if (Initializer != nullptr)
10565 DRD->setInitializer(Initializer);
10566 else
10567 DRD->setInvalidDecl();
10568}
10569
10570Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
10571 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
10572 for (auto *D : DeclReductions.get()) {
10573 if (IsValid) {
10574 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10575 if (S != nullptr)
10576 PushOnScopeChains(DRD, S, /*AddToContext=*/false);
10577 } else
10578 D->setInvalidDecl();
10579 }
10580 return DeclReductions;
10581}
10582
Kelvin Li099bb8c2015-11-24 20:50:12 +000010583OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
10584 SourceLocation StartLoc,
10585 SourceLocation LParenLoc,
10586 SourceLocation EndLoc) {
10587 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +000010588
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010589 // OpenMP [teams Constrcut, Restrictions]
10590 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010591 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
10592 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010593 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000010594
10595 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10596}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010597
10598OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
10599 SourceLocation StartLoc,
10600 SourceLocation LParenLoc,
10601 SourceLocation EndLoc) {
10602 Expr *ValExpr = ThreadLimit;
10603
10604 // OpenMP [teams Constrcut, Restrictions]
10605 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010606 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
10607 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010608 return nullptr;
10609
10610 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
10611 EndLoc);
10612}
Alexey Bataeva0569352015-12-01 10:17:31 +000010613
10614OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
10615 SourceLocation StartLoc,
10616 SourceLocation LParenLoc,
10617 SourceLocation EndLoc) {
10618 Expr *ValExpr = Priority;
10619
10620 // OpenMP [2.9.1, task Constrcut]
10621 // The priority-value is a non-negative numerical scalar expression.
10622 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
10623 /*StrictlyPositive=*/false))
10624 return nullptr;
10625
10626 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10627}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000010628
10629OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
10630 SourceLocation StartLoc,
10631 SourceLocation LParenLoc,
10632 SourceLocation EndLoc) {
10633 Expr *ValExpr = Grainsize;
10634
10635 // OpenMP [2.9.2, taskloop Constrcut]
10636 // The parameter of the grainsize clause must be a positive integer
10637 // expression.
10638 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
10639 /*StrictlyPositive=*/true))
10640 return nullptr;
10641
10642 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10643}
Alexey Bataev382967a2015-12-08 12:06:20 +000010644
10645OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
10646 SourceLocation StartLoc,
10647 SourceLocation LParenLoc,
10648 SourceLocation EndLoc) {
10649 Expr *ValExpr = NumTasks;
10650
10651 // OpenMP [2.9.2, taskloop Constrcut]
10652 // The parameter of the num_tasks clause must be a positive integer
10653 // expression.
10654 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
10655 /*StrictlyPositive=*/true))
10656 return nullptr;
10657
10658 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10659}
10660
Alexey Bataev28c75412015-12-15 08:19:24 +000010661OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
10662 SourceLocation LParenLoc,
10663 SourceLocation EndLoc) {
10664 // OpenMP [2.13.2, critical construct, Description]
10665 // ... where hint-expression is an integer constant expression that evaluates
10666 // to a valid lock hint.
10667 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
10668 if (HintExpr.isInvalid())
10669 return nullptr;
10670 return new (Context)
10671 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
10672}
10673
Carlo Bertollib4adf552016-01-15 18:50:31 +000010674OMPClause *Sema::ActOnOpenMPDistScheduleClause(
10675 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
10676 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
10677 SourceLocation EndLoc) {
10678 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
10679 std::string Values;
10680 Values += "'";
10681 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
10682 Values += "'";
10683 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
10684 << Values << getOpenMPClauseName(OMPC_dist_schedule);
10685 return nullptr;
10686 }
10687 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000010688 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000010689 if (ChunkSize) {
10690 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
10691 !ChunkSize->isInstantiationDependent() &&
10692 !ChunkSize->containsUnexpandedParameterPack()) {
10693 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
10694 ExprResult Val =
10695 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
10696 if (Val.isInvalid())
10697 return nullptr;
10698
10699 ValExpr = Val.get();
10700
10701 // OpenMP [2.7.1, Restrictions]
10702 // chunk_size must be a loop invariant integer expression with a positive
10703 // value.
10704 llvm::APSInt Result;
10705 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
10706 if (Result.isSigned() && !Result.isStrictlyPositive()) {
10707 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
10708 << "dist_schedule" << ChunkSize->getSourceRange();
10709 return nullptr;
10710 }
10711 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev5a3af132016-03-29 08:58:54 +000010712 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
10713 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
10714 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000010715 }
10716 }
10717 }
10718
10719 return new (Context)
10720 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000010721 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000010722}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000010723
10724OMPClause *Sema::ActOnOpenMPDefaultmapClause(
10725 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
10726 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
10727 SourceLocation KindLoc, SourceLocation EndLoc) {
10728 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
10729 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
10730 Kind != OMPC_DEFAULTMAP_scalar) {
10731 std::string Value;
10732 SourceLocation Loc;
10733 Value += "'";
10734 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
10735 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
10736 OMPC_DEFAULTMAP_MODIFIER_tofrom);
10737 Loc = MLoc;
10738 } else {
10739 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
10740 OMPC_DEFAULTMAP_scalar);
10741 Loc = KindLoc;
10742 }
10743 Value += "'";
10744 Diag(Loc, diag::err_omp_unexpected_clause_value)
10745 << Value << getOpenMPClauseName(OMPC_defaultmap);
10746 return nullptr;
10747 }
10748
10749 return new (Context)
10750 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
10751}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010752
10753bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
10754 DeclContext *CurLexicalContext = getCurLexicalContext();
10755 if (!CurLexicalContext->isFileContext() &&
10756 !CurLexicalContext->isExternCContext() &&
10757 !CurLexicalContext->isExternCXXContext()) {
10758 Diag(Loc, diag::err_omp_region_not_file_context);
10759 return false;
10760 }
10761 if (IsInOpenMPDeclareTargetContext) {
10762 Diag(Loc, diag::err_omp_enclosed_declare_target);
10763 return false;
10764 }
10765
10766 IsInOpenMPDeclareTargetContext = true;
10767 return true;
10768}
10769
10770void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
10771 assert(IsInOpenMPDeclareTargetContext &&
10772 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
10773
10774 IsInOpenMPDeclareTargetContext = false;
10775}
10776
Dmitry Polukhind69b5052016-05-09 14:59:13 +000010777void
10778Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope, CXXScopeSpec &ScopeSpec,
10779 const DeclarationNameInfo &Id,
10780 OMPDeclareTargetDeclAttr::MapTypeTy MT,
10781 NamedDeclSetType &SameDirectiveDecls) {
10782 LookupResult Lookup(*this, Id, LookupOrdinaryName);
10783 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
10784
10785 if (Lookup.isAmbiguous())
10786 return;
10787 Lookup.suppressDiagnostics();
10788
10789 if (!Lookup.isSingleResult()) {
10790 if (TypoCorrection Corrected =
10791 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
10792 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
10793 CTK_ErrorRecovery)) {
10794 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
10795 << Id.getName());
10796 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
10797 return;
10798 }
10799
10800 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
10801 return;
10802 }
10803
10804 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
10805 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
10806 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
10807 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
10808
10809 if (!ND->hasAttr<OMPDeclareTargetDeclAttr>()) {
10810 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
10811 ND->addAttr(A);
10812 if (ASTMutationListener *ML = Context.getASTMutationListener())
10813 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
10814 checkDeclIsAllowedInOpenMPTarget(nullptr, ND);
10815 } else if (ND->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() != MT) {
10816 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
10817 << Id.getName();
10818 }
10819 } else
10820 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
10821}
10822
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010823static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
10824 Sema &SemaRef, Decl *D) {
10825 if (!D)
10826 return;
10827 Decl *LD = nullptr;
10828 if (isa<TagDecl>(D)) {
10829 LD = cast<TagDecl>(D)->getDefinition();
10830 } else if (isa<VarDecl>(D)) {
10831 LD = cast<VarDecl>(D)->getDefinition();
10832
10833 // If this is an implicit variable that is legal and we do not need to do
10834 // anything.
10835 if (cast<VarDecl>(D)->isImplicit()) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000010836 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
10837 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
10838 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010839 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000010840 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010841 return;
10842 }
10843
10844 } else if (isa<FunctionDecl>(D)) {
10845 const FunctionDecl *FD = nullptr;
10846 if (cast<FunctionDecl>(D)->hasBody(FD))
10847 LD = const_cast<FunctionDecl *>(FD);
10848
10849 // If the definition is associated with the current declaration in the
10850 // target region (it can be e.g. a lambda) that is legal and we do not need
10851 // to do anything else.
10852 if (LD == D) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000010853 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
10854 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
10855 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010856 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000010857 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010858 return;
10859 }
10860 }
10861 if (!LD)
10862 LD = D;
10863 if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() &&
10864 (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) {
10865 // Outlined declaration is not declared target.
10866 if (LD->isOutOfLine()) {
10867 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
10868 SemaRef.Diag(SL, diag::note_used_here) << SR;
10869 } else {
10870 DeclContext *DC = LD->getDeclContext();
10871 while (DC) {
10872 if (isa<FunctionDecl>(DC) &&
10873 cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>())
10874 break;
10875 DC = DC->getParent();
10876 }
10877 if (DC)
10878 return;
10879
10880 // Is not declared in target context.
10881 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
10882 SemaRef.Diag(SL, diag::note_used_here) << SR;
10883 }
10884 // Mark decl as declared target to prevent further diagnostic.
Dmitry Polukhind69b5052016-05-09 14:59:13 +000010885 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
10886 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
10887 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010888 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000010889 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010890 }
10891}
10892
10893static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
10894 Sema &SemaRef, DSAStackTy *Stack,
10895 ValueDecl *VD) {
10896 if (VD->hasAttr<OMPDeclareTargetDeclAttr>())
10897 return true;
10898 if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType()))
10899 return false;
10900 return true;
10901}
10902
10903void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D) {
10904 if (!D || D->isInvalidDecl())
10905 return;
10906 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
10907 SourceLocation SL = E ? E->getLocStart() : D->getLocation();
10908 // 2.10.6: threadprivate variable cannot appear in a declare target directive.
10909 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
10910 if (DSAStack->isThreadPrivate(VD)) {
10911 Diag(SL, diag::err_omp_threadprivate_in_target);
10912 ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
10913 return;
10914 }
10915 }
10916 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
10917 // Problem if any with var declared with incomplete type will be reported
10918 // as normal, so no need to check it here.
10919 if ((E || !VD->getType()->isIncompleteType()) &&
10920 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) {
10921 // Mark decl as declared target to prevent further diagnostic.
10922 if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000010923 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
10924 Context, OMPDeclareTargetDeclAttr::MT_To);
10925 VD->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010926 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000010927 ML->DeclarationMarkedOpenMPDeclareTarget(VD, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010928 }
10929 return;
10930 }
10931 }
10932 if (!E) {
10933 // Checking declaration inside declare target region.
10934 if (!D->hasAttr<OMPDeclareTargetDeclAttr>() &&
10935 (isa<VarDecl>(D) || isa<FunctionDecl>(D))) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000010936 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
10937 Context, OMPDeclareTargetDeclAttr::MT_To);
10938 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010939 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000010940 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010941 }
10942 return;
10943 }
10944 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
10945}