blob: 79c4d1c4aab1f97917e1a734c1cba92cad4148c4 [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 Bataev8b427062016-05-25 12:36:08 +000060 typedef llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>
61 OperatorOffsetTy;
Alexey Bataeved09d242014-05-28 05:53:51 +000062
Alexey Bataev758e55e2013-09-06 18:03:48 +000063private:
Alexey Bataev7ace49d2016-05-17 08:55:33 +000064 struct DSAInfo final {
65 OpenMPClauseKind Attributes = OMPC_unknown;
66 /// Pointer to a reference expression and a flag which shows that the
67 /// variable is marked as lastprivate(true) or not (false).
68 llvm::PointerIntPair<Expr *, 1, bool> RefExpr;
69 DeclRefExpr *PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +000070 };
Alexey Bataev90c228f2016-02-08 09:29:13 +000071 typedef llvm::DenseMap<ValueDecl *, DSAInfo> DeclSAMapTy;
72 typedef llvm::DenseMap<ValueDecl *, Expr *> AlignedMapTy;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +000073 typedef std::pair<unsigned, VarDecl *> LCDeclInfo;
74 typedef llvm::DenseMap<ValueDecl *, LCDeclInfo> LoopControlVariablesMapTy;
Samuel Antao6890b092016-07-28 14:25:09 +000075 /// Struct that associates a component with the clause kind where they are
76 /// found.
77 struct MappedExprComponentTy {
78 OMPClauseMappableExprCommon::MappableExprComponentLists Components;
79 OpenMPClauseKind Kind = OMPC_unknown;
80 };
81 typedef llvm::DenseMap<ValueDecl *, MappedExprComponentTy>
Samuel Antao90927002016-04-26 14:54:23 +000082 MappedExprComponentsTy;
Alexey Bataev28c75412015-12-15 08:19:24 +000083 typedef llvm::StringMap<std::pair<OMPCriticalDirective *, llvm::APSInt>>
84 CriticalsWithHintsTy;
Alexey Bataev8b427062016-05-25 12:36:08 +000085 typedef llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>
86 DoacrossDependMapTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000087
Alexey Bataev7ace49d2016-05-17 08:55:33 +000088 struct SharingMapTy final {
Alexey Bataev758e55e2013-09-06 18:03:48 +000089 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000090 AlignedMapTy AlignedMap;
Samuel Antao90927002016-04-26 14:54:23 +000091 MappedExprComponentsTy MappedExprComponents;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000092 LoopControlVariablesMapTy LCVMap;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000093 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified;
Alexey Bataevbae9a792014-06-27 10:37:06 +000094 SourceLocation DefaultAttrLoc;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000095 OpenMPDirectiveKind Directive = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +000096 DeclarationNameInfo DirectiveName;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000097 Scope *CurScope = nullptr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000098 SourceLocation ConstructLoc;
Alexey Bataev8b427062016-05-25 12:36:08 +000099 /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to
100 /// get the data (loop counters etc.) about enclosing loop-based construct.
101 /// This data is required during codegen.
102 DoacrossDependMapTy DoacrossDepends;
Alexey Bataev346265e2015-09-25 10:37:12 +0000103 /// \brief first argument (Expr *) contains optional argument of the
104 /// 'ordered' clause, the second one is true if the regions has 'ordered'
105 /// clause, false otherwise.
106 llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000107 bool NowaitRegion = false;
108 bool CancelRegion = false;
109 unsigned AssociatedLoops = 1;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000110 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +0000111 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000112 Scope *CurScope, SourceLocation Loc)
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000113 : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
114 ConstructLoc(Loc) {}
115 SharingMapTy() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000116 };
117
Axel Naumann323862e2016-02-03 10:45:22 +0000118 typedef SmallVector<SharingMapTy, 4> StackTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000119
120 /// \brief Stack of used declaration and their data-sharing attributes.
121 StackTy Stack;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000122 /// \brief true, if check for DSA must be from parent directive, false, if
123 /// from current directive.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000124 OpenMPClauseKind ClauseKindMode = OMPC_unknown;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000125 Sema &SemaRef;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000126 bool ForceCapturing = false;
Alexey Bataev28c75412015-12-15 08:19:24 +0000127 CriticalsWithHintsTy Criticals;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000128
129 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
130
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000131 DSAVarData getDSA(StackTy::reverse_iterator& Iter, ValueDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000132
133 /// \brief Checks if the variable is a local for OpenMP region.
134 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000135
Alexey Bataev758e55e2013-09-06 18:03:48 +0000136public:
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000137 explicit DSAStackTy(Sema &S) : Stack(1), SemaRef(S) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000138
Alexey Bataevaac108a2015-06-23 04:51:00 +0000139 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
140 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000141
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000142 bool isForceVarCapturing() const { return ForceCapturing; }
143 void setForceVarCapturing(bool V) { ForceCapturing = V; }
144
Alexey Bataev758e55e2013-09-06 18:03:48 +0000145 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000146 Scope *CurScope, SourceLocation Loc) {
147 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
148 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000149 }
150
151 void pop() {
152 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
153 Stack.pop_back();
154 }
155
Alexey Bataev28c75412015-12-15 08:19:24 +0000156 void addCriticalWithHint(OMPCriticalDirective *D, llvm::APSInt Hint) {
157 Criticals[D->getDirectiveName().getAsString()] = std::make_pair(D, Hint);
158 }
159 const std::pair<OMPCriticalDirective *, llvm::APSInt>
160 getCriticalWithHint(const DeclarationNameInfo &Name) const {
161 auto I = Criticals.find(Name.getAsString());
162 if (I != Criticals.end())
163 return I->second;
164 return std::make_pair(nullptr, llvm::APSInt());
165 }
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000166 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000167 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000168 /// for diagnostics.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000169 Expr *addUniqueAligned(ValueDecl *D, Expr *NewDE);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000170
Alexey Bataev9c821032015-04-30 04:23:23 +0000171 /// \brief Register specified variable as loop control variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000172 void addLoopControlVariable(ValueDecl *D, VarDecl *Capture);
Alexey Bataev9c821032015-04-30 04:23:23 +0000173 /// \brief Check if the specified variable is a loop control variable for
174 /// current region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000175 /// \return The index of the loop control variable in the list of associated
176 /// for-loops (from outer to inner).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000177 LCDeclInfo isLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000178 /// \brief Check if the specified variable is a loop control variable for
179 /// parent region.
180 /// \return The index of the loop control variable in the list of associated
181 /// for-loops (from outer to inner).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000182 LCDeclInfo isParentLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000183 /// \brief Get the loop control variable for the I-th loop (or nullptr) in
184 /// parent directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000185 ValueDecl *getParentLoopControlVariable(unsigned I);
Alexey Bataev9c821032015-04-30 04:23:23 +0000186
Alexey Bataev758e55e2013-09-06 18:03:48 +0000187 /// \brief Adds explicit data sharing attribute to the specified declaration.
Alexey Bataev90c228f2016-02-08 09:29:13 +0000188 void addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
189 DeclRefExpr *PrivateCopy = nullptr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000190
Alexey Bataev758e55e2013-09-06 18:03:48 +0000191 /// \brief Returns data sharing attributes from top of the stack for the
192 /// specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000193 DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000194 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000195 DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000196 /// \brief Checks if the specified variables has data-sharing attributes which
197 /// match specified \a CPred predicate in any directive which matches \a DPred
198 /// predicate.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000199 DSAVarData hasDSA(ValueDecl *D,
200 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
201 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
202 bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000203 /// \brief Checks if the specified variables has data-sharing attributes which
204 /// match specified \a CPred predicate in any innermost directive which
205 /// matches \a DPred predicate.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000206 DSAVarData
207 hasInnermostDSA(ValueDecl *D,
208 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
209 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
210 bool FromParent);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000211 /// \brief Checks if the specified variables has explicit data-sharing
212 /// attributes which match specified \a CPred predicate at the specified
213 /// OpenMP region.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000214 bool hasExplicitDSA(ValueDecl *D,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000215 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000216 unsigned Level, bool NotLastprivate = false);
Samuel Antao4be30e92015-10-02 17:14:03 +0000217
218 /// \brief Returns true if the directive at level \Level matches in the
219 /// specified \a DPred predicate.
220 bool hasExplicitDirective(
221 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
222 unsigned Level);
223
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000224 /// \brief Finds a directive which matches specified \a DPred predicate.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000225 bool hasDirective(const llvm::function_ref<bool(OpenMPDirectiveKind,
226 const DeclarationNameInfo &,
227 SourceLocation)> &DPred,
228 bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000229
Alexey Bataev758e55e2013-09-06 18:03:48 +0000230 /// \brief Returns currently analyzed directive.
231 OpenMPDirectiveKind getCurrentDirective() const {
232 return Stack.back().Directive;
233 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000234 /// \brief Returns parent directive.
235 OpenMPDirectiveKind getParentDirective() const {
236 if (Stack.size() > 2)
237 return Stack[Stack.size() - 2].Directive;
238 return OMPD_unknown;
239 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000240
241 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000242 void setDefaultDSANone(SourceLocation Loc) {
243 Stack.back().DefaultAttr = DSA_none;
244 Stack.back().DefaultAttrLoc = Loc;
245 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000246 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000247 void setDefaultDSAShared(SourceLocation Loc) {
248 Stack.back().DefaultAttr = DSA_shared;
249 Stack.back().DefaultAttrLoc = Loc;
250 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000251
252 DefaultDataSharingAttributes getDefaultDSA() const {
253 return Stack.back().DefaultAttr;
254 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000255 SourceLocation getDefaultDSALocation() const {
256 return Stack.back().DefaultAttrLoc;
257 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000258
Alexey Bataevf29276e2014-06-18 04:14:57 +0000259 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000260 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000261 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000262 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000263 }
264
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000265 /// \brief Marks current region as ordered (it has an 'ordered' clause).
Alexey Bataev346265e2015-09-25 10:37:12 +0000266 void setOrderedRegion(bool IsOrdered, Expr *Param) {
267 Stack.back().OrderedRegion.setInt(IsOrdered);
268 Stack.back().OrderedRegion.setPointer(Param);
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000269 }
270 /// \brief Returns true, if parent region is ordered (has associated
271 /// 'ordered' clause), false - otherwise.
272 bool isParentOrderedRegion() const {
273 if (Stack.size() > 2)
Alexey Bataev346265e2015-09-25 10:37:12 +0000274 return Stack[Stack.size() - 2].OrderedRegion.getInt();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000275 return false;
276 }
Alexey Bataev346265e2015-09-25 10:37:12 +0000277 /// \brief Returns optional parameter for the ordered region.
278 Expr *getParentOrderedRegionParam() const {
279 if (Stack.size() > 2)
280 return Stack[Stack.size() - 2].OrderedRegion.getPointer();
281 return nullptr;
282 }
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000283 /// \brief Marks current region as nowait (it has a 'nowait' clause).
284 void setNowaitRegion(bool IsNowait = true) {
285 Stack.back().NowaitRegion = IsNowait;
286 }
287 /// \brief Returns true, if parent region is nowait (has associated
288 /// 'nowait' clause), false - otherwise.
289 bool isParentNowaitRegion() const {
290 if (Stack.size() > 2)
291 return Stack[Stack.size() - 2].NowaitRegion;
292 return false;
293 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000294 /// \brief Marks parent region as cancel region.
295 void setParentCancelRegion(bool Cancel = true) {
296 if (Stack.size() > 2)
297 Stack[Stack.size() - 2].CancelRegion =
298 Stack[Stack.size() - 2].CancelRegion || Cancel;
299 }
300 /// \brief Return true if current region has inner cancel construct.
301 bool isCancelRegion() const {
302 return Stack.back().CancelRegion;
303 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000304
Alexey Bataev9c821032015-04-30 04:23:23 +0000305 /// \brief Set collapse value for the region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000306 void setAssociatedLoops(unsigned Val) { Stack.back().AssociatedLoops = Val; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000307 /// \brief Return collapse value for region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000308 unsigned getAssociatedLoops() const { return Stack.back().AssociatedLoops; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000309
Alexey Bataev13314bf2014-10-09 04:18:56 +0000310 /// \brief Marks current target region as one with closely nested teams
311 /// region.
312 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
313 if (Stack.size() > 2)
314 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
315 }
316 /// \brief Returns true, if current region has closely nested teams region.
317 bool hasInnerTeamsRegion() const {
318 return getInnerTeamsRegionLoc().isValid();
319 }
320 /// \brief Returns location of the nested teams region (if any).
321 SourceLocation getInnerTeamsRegionLoc() const {
322 if (Stack.size() > 1)
323 return Stack.back().InnerTeamsRegionLoc;
324 return SourceLocation();
325 }
326
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000327 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000328 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000329 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000330
Samuel Antao90927002016-04-26 14:54:23 +0000331 // Do the check specified in \a Check to all component lists and return true
332 // if any issue is found.
333 bool checkMappableExprComponentListsForDecl(
334 ValueDecl *VD, bool CurrentRegionOnly,
Samuel Antao6890b092016-07-28 14:25:09 +0000335 const llvm::function_ref<
336 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
337 OpenMPClauseKind)> &Check) {
Samuel Antao5de996e2016-01-22 20:21:36 +0000338 auto SI = Stack.rbegin();
339 auto SE = Stack.rend();
340
341 if (SI == SE)
342 return false;
343
344 if (CurrentRegionOnly) {
345 SE = std::next(SI);
346 } else {
347 ++SI;
348 }
349
350 for (; SI != SE; ++SI) {
Samuel Antao90927002016-04-26 14:54:23 +0000351 auto MI = SI->MappedExprComponents.find(VD);
352 if (MI != SI->MappedExprComponents.end())
Samuel Antao6890b092016-07-28 14:25:09 +0000353 for (auto &L : MI->second.Components)
354 if (Check(L, MI->second.Kind))
Samuel Antao5de996e2016-01-22 20:21:36 +0000355 return true;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000356 }
Samuel Antao5de996e2016-01-22 20:21:36 +0000357 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000358 }
359
Samuel Antao90927002016-04-26 14:54:23 +0000360 // Create a new mappable expression component list associated with a given
361 // declaration and initialize it with the provided list of components.
362 void addMappableExpressionComponents(
363 ValueDecl *VD,
Samuel Antao6890b092016-07-28 14:25:09 +0000364 OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
365 OpenMPClauseKind WhereFoundClauseKind) {
Samuel Antao90927002016-04-26 14:54:23 +0000366 assert(Stack.size() > 1 &&
367 "Not expecting to retrieve components from a empty stack!");
368 auto &MEC = Stack.back().MappedExprComponents[VD];
369 // Create new entry and append the new components there.
Samuel Antao6890b092016-07-28 14:25:09 +0000370 MEC.Components.resize(MEC.Components.size() + 1);
371 MEC.Components.back().append(Components.begin(), Components.end());
372 MEC.Kind = WhereFoundClauseKind;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000373 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000374
375 unsigned getNestingLevel() const {
376 assert(Stack.size() > 1);
377 return Stack.size() - 2;
378 }
Alexey Bataev8b427062016-05-25 12:36:08 +0000379 void addDoacrossDependClause(OMPDependClause *C, OperatorOffsetTy &OpsOffs) {
380 assert(Stack.size() > 2);
381 assert(isOpenMPWorksharingDirective(Stack[Stack.size() - 2].Directive));
382 Stack[Stack.size() - 2].DoacrossDepends.insert({C, OpsOffs});
383 }
384 llvm::iterator_range<DoacrossDependMapTy::const_iterator>
385 getDoacrossDependClauses() const {
386 assert(Stack.size() > 1);
387 if (isOpenMPWorksharingDirective(Stack[Stack.size() - 1].Directive)) {
388 auto &Ref = Stack[Stack.size() - 1].DoacrossDepends;
389 return llvm::make_range(Ref.begin(), Ref.end());
390 }
391 return llvm::make_range(Stack[0].DoacrossDepends.end(),
392 Stack[0].DoacrossDepends.end());
393 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000394};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000395bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
Alexey Bataev35aaee62016-04-13 13:36:48 +0000396 return isOpenMPParallelDirective(DKind) || isOpenMPTaskingDirective(DKind) ||
397 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000398}
Alexey Bataeved09d242014-05-28 05:53:51 +0000399} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000400
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000401static ValueDecl *getCanonicalDecl(ValueDecl *D) {
402 auto *VD = dyn_cast<VarDecl>(D);
403 auto *FD = dyn_cast<FieldDecl>(D);
404 if (VD != nullptr) {
405 VD = VD->getCanonicalDecl();
406 D = VD;
407 } else {
408 assert(FD);
409 FD = FD->getCanonicalDecl();
410 D = FD;
411 }
412 return D;
413}
414
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000415DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator& Iter,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000416 ValueDecl *D) {
417 D = getCanonicalDecl(D);
418 auto *VD = dyn_cast<VarDecl>(D);
419 auto *FD = dyn_cast<FieldDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000420 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000421 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000422 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
423 // in a region but not in construct]
424 // File-scope or namespace-scope variables referenced in called routines
425 // in the region are shared unless they appear in a threadprivate
426 // directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000427 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000428 DVar.CKind = OMPC_shared;
429
430 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
431 // in a region but not in construct]
432 // Variables with static storage duration that are declared in called
433 // routines in the region are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000434 if (VD && VD->hasGlobalStorage())
435 DVar.CKind = OMPC_shared;
436
437 // Non-static data members are shared by default.
438 if (FD)
Alexey Bataev750a58b2014-03-18 12:19:12 +0000439 DVar.CKind = OMPC_shared;
440
Alexey Bataev758e55e2013-09-06 18:03:48 +0000441 return DVar;
442 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000443
Alexey Bataev758e55e2013-09-06 18:03:48 +0000444 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000445 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
446 // in a Construct, C/C++, predetermined, p.1]
447 // Variables with automatic storage duration that are declared in a scope
448 // inside the construct are private.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000449 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
450 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000451 DVar.CKind = OMPC_private;
452 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000453 }
454
Alexey Bataev758e55e2013-09-06 18:03:48 +0000455 // Explicitly specified attributes and local variables with predetermined
456 // attributes.
457 if (Iter->SharingMap.count(D)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000458 DVar.RefExpr = Iter->SharingMap[D].RefExpr.getPointer();
Alexey Bataev90c228f2016-02-08 09:29:13 +0000459 DVar.PrivateCopy = Iter->SharingMap[D].PrivateCopy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000460 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000461 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000462 return DVar;
463 }
464
465 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
466 // in a Construct, C/C++, implicitly determined, p.1]
467 // In a parallel or task construct, the data-sharing attributes of these
468 // variables are determined by the default clause, if present.
469 switch (Iter->DefaultAttr) {
470 case DSA_shared:
471 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000472 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000473 return DVar;
474 case DSA_none:
475 return DVar;
476 case DSA_unspecified:
477 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
478 // in a Construct, implicitly determined, p.2]
479 // In a parallel construct, if no default clause is present, these
480 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000481 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000482 if (isOpenMPParallelDirective(DVar.DKind) ||
483 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000484 DVar.CKind = OMPC_shared;
485 return DVar;
486 }
487
488 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
489 // in a Construct, implicitly determined, p.4]
490 // In a task construct, if no default clause is present, a variable that in
491 // the enclosing context is determined to be shared by all implicit tasks
492 // bound to the current team is shared.
Alexey Bataev35aaee62016-04-13 13:36:48 +0000493 if (isOpenMPTaskingDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000494 DSAVarData DVarTemp;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000495 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000496 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000497 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
Alexey Bataev35aaee62016-04-13 13:36:48 +0000498 // Referenced in a Construct, implicitly determined, p.6]
Alexey Bataev758e55e2013-09-06 18:03:48 +0000499 // In a task construct, if no default clause is present, a variable
500 // whose data-sharing attribute is not determined by the rules above is
501 // firstprivate.
502 DVarTemp = getDSA(I, D);
503 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000504 DVar.RefExpr = nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000505 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000506 return DVar;
507 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000508 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000509 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000510 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000511 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000512 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000513 return DVar;
514 }
515 }
516 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
517 // in a Construct, implicitly determined, p.3]
518 // For constructs other than task, if no default clause is present, these
519 // variables inherit their data-sharing attributes from the enclosing
520 // context.
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000521 return getDSA(++Iter, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000522}
523
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000524Expr *DSAStackTy::addUniqueAligned(ValueDecl *D, Expr *NewDE) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000525 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000526 D = getCanonicalDecl(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000527 auto It = Stack.back().AlignedMap.find(D);
528 if (It == Stack.back().AlignedMap.end()) {
529 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
530 Stack.back().AlignedMap[D] = NewDE;
531 return nullptr;
532 } else {
533 assert(It->second && "Unexpected nullptr expr in the aligned map");
534 return It->second;
535 }
536 return nullptr;
537}
538
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000539void DSAStackTy::addLoopControlVariable(ValueDecl *D, VarDecl *Capture) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000540 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000541 D = getCanonicalDecl(D);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000542 Stack.back().LCVMap.insert(
543 std::make_pair(D, LCDeclInfo(Stack.back().LCVMap.size() + 1, Capture)));
Alexey Bataev9c821032015-04-30 04:23:23 +0000544}
545
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000546DSAStackTy::LCDeclInfo DSAStackTy::isLoopControlVariable(ValueDecl *D) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000547 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000548 D = getCanonicalDecl(D);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000549 return Stack.back().LCVMap.count(D) > 0 ? Stack.back().LCVMap[D]
550 : LCDeclInfo(0, nullptr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000551}
552
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000553DSAStackTy::LCDeclInfo DSAStackTy::isParentLoopControlVariable(ValueDecl *D) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000554 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000555 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000556 return Stack[Stack.size() - 2].LCVMap.count(D) > 0
557 ? Stack[Stack.size() - 2].LCVMap[D]
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000558 : LCDeclInfo(0, nullptr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000559}
560
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000561ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000562 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
563 if (Stack[Stack.size() - 2].LCVMap.size() < I)
564 return nullptr;
565 for (auto &Pair : Stack[Stack.size() - 2].LCVMap) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000566 if (Pair.second.first == I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000567 return Pair.first;
568 }
569 return nullptr;
Alexey Bataev9c821032015-04-30 04:23:23 +0000570}
571
Alexey Bataev90c228f2016-02-08 09:29:13 +0000572void DSAStackTy::addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
573 DeclRefExpr *PrivateCopy) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000574 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000575 if (A == OMPC_threadprivate) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000576 auto &Data = Stack[0].SharingMap[D];
577 Data.Attributes = A;
578 Data.RefExpr.setPointer(E);
579 Data.PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000580 } else {
581 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000582 auto &Data = Stack.back().SharingMap[D];
583 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
584 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
585 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
586 (isLoopControlVariable(D).first && A == OMPC_private));
587 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
588 Data.RefExpr.setInt(/*IntVal=*/true);
589 return;
590 }
591 const bool IsLastprivate =
592 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
593 Data.Attributes = A;
594 Data.RefExpr.setPointerAndInt(E, IsLastprivate);
595 Data.PrivateCopy = PrivateCopy;
596 if (PrivateCopy) {
597 auto &Data = Stack.back().SharingMap[PrivateCopy->getDecl()];
598 Data.Attributes = A;
599 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
600 Data.PrivateCopy = nullptr;
601 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000602 }
603}
604
Alexey Bataeved09d242014-05-28 05:53:51 +0000605bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000606 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000607 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000608 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000609 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000610 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000611 ++I;
612 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000613 if (I == E)
614 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000615 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000616 Scope *CurScope = getCurScope();
617 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000618 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000619 }
620 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000621 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000622 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000623}
624
Alexey Bataev39f915b82015-05-08 10:41:21 +0000625/// \brief Build a variable declaration for OpenMP loop iteration variable.
626static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000627 StringRef Name, const AttrVec *Attrs = nullptr) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000628 DeclContext *DC = SemaRef.CurContext;
629 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
630 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
631 VarDecl *Decl =
632 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000633 if (Attrs) {
634 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
635 I != E; ++I)
636 Decl->addAttr(*I);
637 }
Alexey Bataev39f915b82015-05-08 10:41:21 +0000638 Decl->setImplicit();
639 return Decl;
640}
641
642static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
643 SourceLocation Loc,
644 bool RefersToCapture = false) {
645 D->setReferenced();
646 D->markUsed(S.Context);
647 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
648 SourceLocation(), D, RefersToCapture, Loc, Ty,
649 VK_LValue);
650}
651
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000652DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, bool FromParent) {
653 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000654 DSAVarData DVar;
655
656 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
657 // in a Construct, C/C++, predetermined, p.1]
658 // Variables appearing in threadprivate directives are threadprivate.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000659 auto *VD = dyn_cast<VarDecl>(D);
660 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
661 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
Samuel Antaof8b50122015-07-13 22:54:53 +0000662 SemaRef.getLangOpts().OpenMPUseTLS &&
663 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000664 (VD && VD->getStorageClass() == SC_Register &&
665 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
666 addDSA(D, buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
Alexey Bataev39f915b82015-05-08 10:41:21 +0000667 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000668 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000669 }
670 if (Stack[0].SharingMap.count(D)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000671 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr.getPointer();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000672 DVar.CKind = OMPC_threadprivate;
673 return DVar;
674 }
675
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000676 if (Stack.size() == 1) {
677 // Not in OpenMP execution region and top scope was already checked.
678 return DVar;
679 }
680
Alexey Bataev758e55e2013-09-06 18:03:48 +0000681 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000682 // in a Construct, C/C++, predetermined, p.4]
683 // Static data members are shared.
684 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
685 // in a Construct, C/C++, predetermined, p.7]
686 // Variables with static storage duration that are declared in a scope
687 // inside the construct are shared.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000688 auto &&MatchesAlways = [](OpenMPDirectiveKind) -> bool { return true; };
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000689 if (VD && VD->isStaticDataMember()) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000690 DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent);
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000691 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +0000692 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000693
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000694 DVar.CKind = OMPC_shared;
695 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000696 }
697
698 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000699 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
700 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000701 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
702 // in a Construct, C/C++, predetermined, p.6]
703 // Variables with const qualified type having no mutable member are
704 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000705 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000706 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataevc9bd03d2015-12-17 06:55:08 +0000707 if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
708 if (auto *CTD = CTSD->getSpecializedTemplate())
709 RD = CTD->getTemplatedDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000710 if (IsConstant &&
Alexey Bataev4bcad7f2016-02-10 10:50:12 +0000711 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
712 RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000713 // Variables with const-qualified type having no mutable member may be
714 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000715 DSAVarData DVarTemp = hasDSA(
716 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_firstprivate; },
717 MatchesAlways, FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000718 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
719 return DVar;
720
Alexey Bataev758e55e2013-09-06 18:03:48 +0000721 DVar.CKind = OMPC_shared;
722 return DVar;
723 }
724
Alexey Bataev758e55e2013-09-06 18:03:48 +0000725 // Explicitly specified attributes and local variables with predetermined
726 // attributes.
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000727 auto StartI = std::next(Stack.rbegin());
728 auto EndI = std::prev(Stack.rend());
729 if (FromParent && StartI != EndI) {
730 StartI = std::next(StartI);
731 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000732 auto I = std::prev(StartI);
733 if (I->SharingMap.count(D)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000734 DVar.RefExpr = I->SharingMap[D].RefExpr.getPointer();
Alexey Bataev90c228f2016-02-08 09:29:13 +0000735 DVar.PrivateCopy = I->SharingMap[D].PrivateCopy;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000736 DVar.CKind = I->SharingMap[D].Attributes;
737 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000738 }
739
740 return DVar;
741}
742
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000743DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
744 bool FromParent) {
745 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000746 auto StartI = Stack.rbegin();
747 auto EndI = std::prev(Stack.rend());
748 if (FromParent && StartI != EndI) {
749 StartI = std::next(StartI);
750 }
751 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000752}
753
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000754DSAStackTy::DSAVarData
755DSAStackTy::hasDSA(ValueDecl *D,
756 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
757 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
758 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000759 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000760 auto StartI = std::next(Stack.rbegin());
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000761 auto EndI = Stack.rend();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000762 if (FromParent && StartI != EndI) {
763 StartI = std::next(StartI);
764 }
765 for (auto I = StartI, EE = EndI; I != EE; ++I) {
766 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000767 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000768 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000769 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000770 return DVar;
771 }
772 return DSAVarData();
773}
774
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000775DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
776 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
777 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
778 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000779 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000780 auto StartI = std::next(Stack.rbegin());
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000781 auto EndI = Stack.rend();
Alexey Bataeve3978122016-07-19 05:06:39 +0000782 if (FromParent && StartI != EndI)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000783 StartI = std::next(StartI);
Alexey Bataeve3978122016-07-19 05:06:39 +0000784 if (StartI == EndI || !DPred(StartI->Directive))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000785 return DSAVarData();
Alexey Bataeve3978122016-07-19 05:06:39 +0000786 DSAVarData DVar = getDSA(StartI, D);
787 return CPred(DVar.CKind) ? DVar : DSAVarData();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000788}
789
Alexey Bataevaac108a2015-06-23 04:51:00 +0000790bool DSAStackTy::hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000791 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000792 unsigned Level, bool NotLastprivate) {
Alexey Bataevaac108a2015-06-23 04:51:00 +0000793 if (CPred(ClauseKindMode))
794 return true;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000795 D = getCanonicalDecl(D);
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000796 auto StartI = std::next(Stack.begin());
797 auto EndI = Stack.end();
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000798 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000799 return false;
800 std::advance(StartI, Level);
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000801 return (StartI->SharingMap.count(D) > 0) &&
802 StartI->SharingMap[D].RefExpr.getPointer() &&
803 CPred(StartI->SharingMap[D].Attributes) &&
804 (!NotLastprivate || !StartI->SharingMap[D].RefExpr.getInt());
Alexey Bataevaac108a2015-06-23 04:51:00 +0000805}
806
Samuel Antao4be30e92015-10-02 17:14:03 +0000807bool DSAStackTy::hasExplicitDirective(
808 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
809 unsigned Level) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000810 auto StartI = std::next(Stack.begin());
811 auto EndI = Stack.end();
Samuel Antao4be30e92015-10-02 17:14:03 +0000812 if (std::distance(StartI, EndI) <= (int)Level)
813 return false;
814 std::advance(StartI, Level);
815 return DPred(StartI->Directive);
816}
817
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000818bool DSAStackTy::hasDirective(
819 const llvm::function_ref<bool(OpenMPDirectiveKind,
820 const DeclarationNameInfo &, SourceLocation)>
821 &DPred,
822 bool FromParent) {
Samuel Antaof0d79752016-05-27 15:21:27 +0000823 // We look only in the enclosing region.
824 if (Stack.size() < 2)
825 return false;
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000826 auto StartI = std::next(Stack.rbegin());
827 auto EndI = std::prev(Stack.rend());
828 if (FromParent && StartI != EndI) {
829 StartI = std::next(StartI);
830 }
831 for (auto I = StartI, EE = EndI; I != EE; ++I) {
832 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
833 return true;
834 }
835 return false;
836}
837
Alexey Bataev758e55e2013-09-06 18:03:48 +0000838void Sema::InitDataSharingAttributesStack() {
839 VarDataSharingAttributesStack = new DSAStackTy(*this);
840}
841
842#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
843
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000844bool Sema::IsOpenMPCapturedByRef(ValueDecl *D, unsigned Level) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000845 assert(LangOpts.OpenMP && "OpenMP is not allowed");
846
847 auto &Ctx = getASTContext();
848 bool IsByRef = true;
849
850 // Find the directive that is associated with the provided scope.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000851 auto Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000852
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000853 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000854 // This table summarizes how a given variable should be passed to the device
855 // given its type and the clauses where it appears. This table is based on
856 // the description in OpenMP 4.5 [2.10.4, target Construct] and
857 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
858 //
859 // =========================================================================
860 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
861 // | |(tofrom:scalar)| | pvt | | | |
862 // =========================================================================
863 // | scl | | | | - | | bycopy|
864 // | scl | | - | x | - | - | bycopy|
865 // | scl | | x | - | - | - | null |
866 // | scl | x | | | - | | byref |
867 // | scl | x | - | x | - | - | bycopy|
868 // | scl | x | x | - | - | - | null |
869 // | scl | | - | - | - | x | byref |
870 // | scl | x | - | - | - | x | byref |
871 //
872 // | agg | n.a. | | | - | | byref |
873 // | agg | n.a. | - | x | - | - | byref |
874 // | agg | n.a. | x | - | - | - | null |
875 // | agg | n.a. | - | - | - | x | byref |
876 // | agg | n.a. | - | - | - | x[] | byref |
877 //
878 // | ptr | n.a. | | | - | | bycopy|
879 // | ptr | n.a. | - | x | - | - | bycopy|
880 // | ptr | n.a. | x | - | - | - | null |
881 // | ptr | n.a. | - | - | - | x | byref |
882 // | ptr | n.a. | - | - | - | x[] | bycopy|
883 // | ptr | n.a. | - | - | x | | bycopy|
884 // | ptr | n.a. | - | - | x | x | bycopy|
885 // | ptr | n.a. | - | - | x | x[] | bycopy|
886 // =========================================================================
887 // Legend:
888 // scl - scalar
889 // ptr - pointer
890 // agg - aggregate
891 // x - applies
892 // - - invalid in this combination
893 // [] - mapped with an array section
894 // byref - should be mapped by reference
895 // byval - should be mapped by value
896 // null - initialize a local variable to null on the device
897 //
898 // Observations:
899 // - All scalar declarations that show up in a map clause have to be passed
900 // by reference, because they may have been mapped in the enclosing data
901 // environment.
902 // - If the scalar value does not fit the size of uintptr, it has to be
903 // passed by reference, regardless the result in the table above.
904 // - For pointers mapped by value that have either an implicit map or an
905 // array section, the runtime library may pass the NULL value to the
906 // device instead of the value passed to it by the compiler.
907
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000908
909 if (Ty->isReferenceType())
910 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
Samuel Antao86ace552016-04-27 22:40:57 +0000911
912 // Locate map clauses and see if the variable being captured is referred to
913 // in any of those clauses. Here we only care about variables, not fields,
914 // because fields are part of aggregates.
915 bool IsVariableUsedInMapClause = false;
916 bool IsVariableAssociatedWithSection = false;
917
918 DSAStack->checkMappableExprComponentListsForDecl(
919 D, /*CurrentRegionOnly=*/true,
920 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
Samuel Antao6890b092016-07-28 14:25:09 +0000921 MapExprComponents,
922 OpenMPClauseKind WhereFoundClauseKind) {
923 // Only the map clause information influences how a variable is
924 // captured. E.g. is_device_ptr does not require changing the default
925 // behaviour.
926 if (WhereFoundClauseKind != OMPC_map)
927 return false;
Samuel Antao86ace552016-04-27 22:40:57 +0000928
929 auto EI = MapExprComponents.rbegin();
930 auto EE = MapExprComponents.rend();
931
932 assert(EI != EE && "Invalid map expression!");
933
934 if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
935 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
936
937 ++EI;
938 if (EI == EE)
939 return false;
940
941 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
942 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
943 isa<MemberExpr>(EI->getAssociatedExpression())) {
944 IsVariableAssociatedWithSection = true;
945 // There is nothing more we need to know about this variable.
946 return true;
947 }
948
949 // Keep looking for more map info.
950 return false;
951 });
952
953 if (IsVariableUsedInMapClause) {
954 // If variable is identified in a map clause it is always captured by
955 // reference except if it is a pointer that is dereferenced somehow.
956 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
957 } else {
958 // By default, all the data that has a scalar type is mapped by copy.
959 IsByRef = !Ty->isScalarType();
960 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000961 }
962
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000963 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
964 IsByRef = !DSAStack->hasExplicitDSA(
965 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
966 Level, /*NotLastprivate=*/true);
967 }
968
Samuel Antao86ace552016-04-27 22:40:57 +0000969 // When passing data by copy, we need to make sure it fits the uintptr size
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000970 // and alignment, because the runtime library only deals with uintptr types.
971 // If it does not fit the uintptr size, we need to pass the data by reference
972 // instead.
973 if (!IsByRef &&
974 (Ctx.getTypeSizeInChars(Ty) >
975 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000976 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000977 IsByRef = true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000978 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000979
980 return IsByRef;
981}
982
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000983unsigned Sema::getOpenMPNestingLevel() const {
984 assert(getLangOpts().OpenMP);
985 return DSAStack->getNestingLevel();
986}
987
Alexey Bataev90c228f2016-02-08 09:29:13 +0000988VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +0000989 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000990 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +0000991
992 // If we are attempting to capture a global variable in a directive with
993 // 'target' we return true so that this global is also mapped to the device.
994 //
995 // FIXME: If the declaration is enclosed in a 'declare target' directive,
996 // then it should not be captured. Therefore, an extra check has to be
997 // inserted here once support for 'declare target' is added.
998 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000999 auto *VD = dyn_cast<VarDecl>(D);
1000 if (VD && !VD->hasLocalStorage()) {
Samuel Antao4be30e92015-10-02 17:14:03 +00001001 if (DSAStack->getCurrentDirective() == OMPD_target &&
Alexey Bataev90c228f2016-02-08 09:29:13 +00001002 !DSAStack->isClauseParsingMode())
1003 return VD;
Samuel Antaof0d79752016-05-27 15:21:27 +00001004 if (DSAStack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001005 [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
1006 SourceLocation) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00001007 return isOpenMPTargetExecutionDirective(K);
Samuel Antao4be30e92015-10-02 17:14:03 +00001008 },
Alexey Bataev90c228f2016-02-08 09:29:13 +00001009 false))
1010 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +00001011 }
1012
Alexey Bataev48977c32015-08-04 08:10:48 +00001013 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
1014 (!DSAStack->isClauseParsingMode() ||
1015 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001016 auto &&Info = DSAStack->isLoopControlVariable(D);
1017 if (Info.first ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001018 (VD && VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001019 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001020 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001021 return VD ? VD : Info.second;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001022 auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001023 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +00001024 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001025 DVarPrivate = DSAStack->hasDSA(
1026 D, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
1027 DSAStack->isClauseParsingMode());
Alexey Bataev90c228f2016-02-08 09:29:13 +00001028 if (DVarPrivate.CKind != OMPC_unknown)
1029 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001030 }
Alexey Bataev90c228f2016-02-08 09:29:13 +00001031 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +00001032}
1033
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001034bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) {
Alexey Bataevaac108a2015-06-23 04:51:00 +00001035 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1036 return DSAStack->hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001037 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
Alexey Bataevaac108a2015-06-23 04:51:00 +00001038}
1039
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001040bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) {
Samuel Antao4be30e92015-10-02 17:14:03 +00001041 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1042 // Return true if the current level is no longer enclosed in a target region.
1043
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001044 auto *VD = dyn_cast<VarDecl>(D);
1045 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00001046 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1047 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +00001048}
1049
Alexey Bataeved09d242014-05-28 05:53:51 +00001050void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001051
1052void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
1053 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001054 Scope *CurScope, SourceLocation Loc) {
1055 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001056 PushExpressionEvaluationContext(PotentiallyEvaluated);
1057}
1058
Alexey Bataevaac108a2015-06-23 04:51:00 +00001059void Sema::StartOpenMPClause(OpenMPClauseKind K) {
1060 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001061}
1062
Alexey Bataevaac108a2015-06-23 04:51:00 +00001063void Sema::EndOpenMPClause() {
1064 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001065}
1066
Alexey Bataev758e55e2013-09-06 18:03:48 +00001067void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001068 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
1069 // A variable of class type (or array thereof) that appears in a lastprivate
1070 // clause requires an accessible, unambiguous default constructor for the
1071 // class type, unless the list item is also specified in a firstprivate
1072 // clause.
1073 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001074 for (auto *C : D->clauses()) {
1075 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
1076 SmallVector<Expr *, 8> PrivateCopies;
1077 for (auto *DE : Clause->varlists()) {
1078 if (DE->isValueDependent() || DE->isTypeDependent()) {
1079 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001080 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +00001081 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00001082 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
Alexey Bataev005248a2016-02-25 05:25:57 +00001083 VarDecl *VD = cast<VarDecl>(DRE->getDecl());
1084 QualType Type = VD->getType().getNonReferenceType();
1085 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001086 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001087 // Generate helper private variable and initialize it with the
1088 // default value. The address of the original variable is replaced
1089 // by the address of the new private variable in CodeGen. This new
1090 // variable is not added to IdResolver, so the code in the OpenMP
1091 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001092 auto *VDPrivate = buildVarDecl(
1093 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev005248a2016-02-25 05:25:57 +00001094 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00001095 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
1096 if (VDPrivate->isInvalidDecl())
1097 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001098 PrivateCopies.push_back(buildDeclRefExpr(
1099 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001100 } else {
1101 // The variable is also a firstprivate, so initialization sequence
1102 // for private copy is generated already.
1103 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001104 }
1105 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001106 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001107 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001108 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001109 }
1110 }
1111 }
1112
Alexey Bataev758e55e2013-09-06 18:03:48 +00001113 DSAStack->pop();
1114 DiscardCleanupsInEvaluationContext();
1115 PopExpressionEvaluationContext();
1116}
1117
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001118static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1119 Expr *NumIterations, Sema &SemaRef,
1120 Scope *S, DSAStackTy *Stack);
Alexander Musman3276a272015-03-21 10:12:56 +00001121
Alexey Bataeva769e072013-03-22 06:34:35 +00001122namespace {
1123
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001124class VarDeclFilterCCC : public CorrectionCandidateCallback {
1125private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001126 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001127
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001128public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001129 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001130 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001131 NamedDecl *ND = Candidate.getCorrectionDecl();
1132 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
1133 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001134 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1135 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001136 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001137 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001138 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001139};
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001140
1141class VarOrFuncDeclFilterCCC : public CorrectionCandidateCallback {
1142private:
1143 Sema &SemaRef;
1144
1145public:
1146 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
1147 bool ValidateCandidate(const TypoCorrection &Candidate) override {
1148 NamedDecl *ND = Candidate.getCorrectionDecl();
1149 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
1150 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1151 SemaRef.getCurScope());
1152 }
1153 return false;
1154 }
1155};
1156
Alexey Bataeved09d242014-05-28 05:53:51 +00001157} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001158
1159ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1160 CXXScopeSpec &ScopeSpec,
1161 const DeclarationNameInfo &Id) {
1162 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1163 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1164
1165 if (Lookup.isAmbiguous())
1166 return ExprError();
1167
1168 VarDecl *VD;
1169 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001170 if (TypoCorrection Corrected = CorrectTypo(
1171 Id, LookupOrdinaryName, CurScope, nullptr,
1172 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001173 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001174 PDiag(Lookup.empty()
1175 ? diag::err_undeclared_var_use_suggest
1176 : diag::err_omp_expected_var_arg_suggest)
1177 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001178 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001179 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001180 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1181 : diag::err_omp_expected_var_arg)
1182 << Id.getName();
1183 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001184 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001185 } else {
1186 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001187 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001188 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1189 return ExprError();
1190 }
1191 }
1192 Lookup.suppressDiagnostics();
1193
1194 // OpenMP [2.9.2, Syntax, C/C++]
1195 // Variables must be file-scope, namespace-scope, or static block-scope.
1196 if (!VD->hasGlobalStorage()) {
1197 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001198 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1199 bool IsDecl =
1200 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001201 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001202 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1203 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001204 return ExprError();
1205 }
1206
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001207 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1208 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001209 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1210 // A threadprivate directive for file-scope variables must appear outside
1211 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001212 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1213 !getCurLexicalContext()->isTranslationUnit()) {
1214 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001215 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1216 bool IsDecl =
1217 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1218 Diag(VD->getLocation(),
1219 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1220 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001221 return ExprError();
1222 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001223 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1224 // A threadprivate directive for static class member variables must appear
1225 // in the class definition, in the same scope in which the member
1226 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001227 if (CanonicalVD->isStaticDataMember() &&
1228 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1229 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001230 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1231 bool IsDecl =
1232 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1233 Diag(VD->getLocation(),
1234 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1235 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001236 return ExprError();
1237 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001238 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1239 // A threadprivate directive for namespace-scope variables must appear
1240 // outside any definition or declaration other than the namespace
1241 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001242 if (CanonicalVD->getDeclContext()->isNamespace() &&
1243 (!getCurLexicalContext()->isFileContext() ||
1244 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1245 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001246 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1247 bool IsDecl =
1248 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1249 Diag(VD->getLocation(),
1250 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1251 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001252 return ExprError();
1253 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001254 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1255 // A threadprivate directive for static block-scope variables must appear
1256 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001257 if (CanonicalVD->isStaticLocal() && CurScope &&
1258 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001259 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001260 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1261 bool IsDecl =
1262 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1263 Diag(VD->getLocation(),
1264 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1265 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001266 return ExprError();
1267 }
1268
1269 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1270 // A threadprivate directive must lexically precede all references to any
1271 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001272 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001273 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001274 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001275 return ExprError();
1276 }
1277
1278 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00001279 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1280 SourceLocation(), VD,
1281 /*RefersToEnclosingVariableOrCapture=*/false,
1282 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001283}
1284
Alexey Bataeved09d242014-05-28 05:53:51 +00001285Sema::DeclGroupPtrTy
1286Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1287 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001288 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001289 CurContext->addDecl(D);
1290 return DeclGroupPtrTy::make(DeclGroupRef(D));
1291 }
David Blaikie0403cb12016-01-15 23:43:25 +00001292 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001293}
1294
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001295namespace {
1296class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1297 Sema &SemaRef;
1298
1299public:
1300 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1301 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1302 if (VD->hasLocalStorage()) {
1303 SemaRef.Diag(E->getLocStart(),
1304 diag::err_omp_local_var_in_threadprivate_init)
1305 << E->getSourceRange();
1306 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1307 << VD << VD->getSourceRange();
1308 return true;
1309 }
1310 }
1311 return false;
1312 }
1313 bool VisitStmt(const Stmt *S) {
1314 for (auto Child : S->children()) {
1315 if (Child && Visit(Child))
1316 return true;
1317 }
1318 return false;
1319 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001320 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001321};
1322} // namespace
1323
Alexey Bataeved09d242014-05-28 05:53:51 +00001324OMPThreadPrivateDecl *
1325Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001326 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001327 for (auto &RefExpr : VarList) {
1328 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001329 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1330 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001331
Alexey Bataev376b4a42016-02-09 09:41:09 +00001332 // Mark variable as used.
1333 VD->setReferenced();
1334 VD->markUsed(Context);
1335
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001336 QualType QType = VD->getType();
1337 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1338 // It will be analyzed later.
1339 Vars.push_back(DE);
1340 continue;
1341 }
1342
Alexey Bataeva769e072013-03-22 06:34:35 +00001343 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1344 // A threadprivate variable must not have an incomplete type.
1345 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001346 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001347 continue;
1348 }
1349
1350 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1351 // A threadprivate variable must not have a reference type.
1352 if (VD->getType()->isReferenceType()) {
1353 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001354 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1355 bool IsDecl =
1356 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1357 Diag(VD->getLocation(),
1358 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1359 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001360 continue;
1361 }
1362
Samuel Antaof8b50122015-07-13 22:54:53 +00001363 // Check if this is a TLS variable. If TLS is not being supported, produce
1364 // the corresponding diagnostic.
1365 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1366 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1367 getLangOpts().OpenMPUseTLS &&
1368 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001369 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1370 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001371 Diag(ILoc, diag::err_omp_var_thread_local)
1372 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001373 bool IsDecl =
1374 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1375 Diag(VD->getLocation(),
1376 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1377 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001378 continue;
1379 }
1380
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001381 // Check if initial value of threadprivate variable reference variable with
1382 // local storage (it is not supported by runtime).
1383 if (auto Init = VD->getAnyInitializer()) {
1384 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001385 if (Checker.Visit(Init))
1386 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001387 }
1388
Alexey Bataeved09d242014-05-28 05:53:51 +00001389 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001390 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001391 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1392 Context, SourceRange(Loc, Loc)));
1393 if (auto *ML = Context.getASTMutationListener())
1394 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001395 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001396 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001397 if (!Vars.empty()) {
1398 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1399 Vars);
1400 D->setAccess(AS_public);
1401 }
1402 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001403}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001404
Alexey Bataev7ff55242014-06-19 09:13:45 +00001405static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001406 const ValueDecl *D, DSAStackTy::DSAVarData DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00001407 bool IsLoopIterVar = false) {
1408 if (DVar.RefExpr) {
1409 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1410 << getOpenMPClauseName(DVar.CKind);
1411 return;
1412 }
1413 enum {
1414 PDSA_StaticMemberShared,
1415 PDSA_StaticLocalVarShared,
1416 PDSA_LoopIterVarPrivate,
1417 PDSA_LoopIterVarLinear,
1418 PDSA_LoopIterVarLastprivate,
1419 PDSA_ConstVarShared,
1420 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001421 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001422 PDSA_LocalVarPrivate,
1423 PDSA_Implicit
1424 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001425 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001426 auto ReportLoc = D->getLocation();
1427 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001428 if (IsLoopIterVar) {
1429 if (DVar.CKind == OMPC_private)
1430 Reason = PDSA_LoopIterVarPrivate;
1431 else if (DVar.CKind == OMPC_lastprivate)
1432 Reason = PDSA_LoopIterVarLastprivate;
1433 else
1434 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev35aaee62016-04-13 13:36:48 +00001435 } else if (isOpenMPTaskingDirective(DVar.DKind) &&
1436 DVar.CKind == OMPC_firstprivate) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001437 Reason = PDSA_TaskVarFirstprivate;
1438 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001439 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001440 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001441 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001442 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001443 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001444 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001445 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00001446 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001447 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001448 ReportHint = true;
1449 Reason = PDSA_LocalVarPrivate;
1450 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001451 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001452 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001453 << Reason << ReportHint
1454 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1455 } else if (DVar.ImplicitDSALoc.isValid()) {
1456 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1457 << getOpenMPClauseName(DVar.CKind);
1458 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001459}
1460
Alexey Bataev758e55e2013-09-06 18:03:48 +00001461namespace {
1462class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1463 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001464 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001465 bool ErrorFound;
1466 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001467 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001468 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001469
Alexey Bataev758e55e2013-09-06 18:03:48 +00001470public:
1471 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00001472 if (E->isTypeDependent() || E->isValueDependent() ||
1473 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1474 return;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001475 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001476 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001477 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1478 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001479
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001480 auto DVar = Stack->getTopDSA(VD, false);
1481 // Check if the variable has explicit DSA set and stop analysis if it so.
1482 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001483
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001484 auto ELoc = E->getExprLoc();
1485 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001486 // The default(none) clause requires that each variable that is referenced
1487 // in the construct, and does not have a predetermined data-sharing
1488 // attribute, must have its data-sharing attribute explicitly determined
1489 // by being listed in a data-sharing attribute clause.
1490 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001491 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001492 VarsWithInheritedDSA.count(VD) == 0) {
1493 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001494 return;
1495 }
1496
1497 // OpenMP [2.9.3.6, Restrictions, p.2]
1498 // A list item that appears in a reduction clause of the innermost
1499 // enclosing worksharing or parallel construct may not be accessed in an
1500 // explicit task.
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001501 DVar = Stack->hasInnermostDSA(
1502 VD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
1503 [](OpenMPDirectiveKind K) -> bool {
1504 return isOpenMPParallelDirective(K) ||
1505 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
1506 },
1507 false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001508 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00001509 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001510 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1511 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001512 return;
1513 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001514
1515 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001516 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001517 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1518 !Stack->isLoopControlVariable(VD).first)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001519 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001520 }
1521 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001522 void VisitMemberExpr(MemberExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00001523 if (E->isTypeDependent() || E->isValueDependent() ||
1524 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1525 return;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001526 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1527 if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1528 auto DVar = Stack->getTopDSA(FD, false);
1529 // Check if the variable has explicit DSA set and stop analysis if it
1530 // so.
1531 if (DVar.RefExpr)
1532 return;
1533
1534 auto ELoc = E->getExprLoc();
1535 auto DKind = Stack->getCurrentDirective();
1536 // OpenMP [2.9.3.6, Restrictions, p.2]
1537 // A list item that appears in a reduction clause of the innermost
1538 // enclosing worksharing or parallel construct may not be accessed in
Alexey Bataevd985eda2016-02-10 11:29:16 +00001539 // an explicit task.
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001540 DVar = Stack->hasInnermostDSA(
1541 FD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
1542 [](OpenMPDirectiveKind K) -> bool {
1543 return isOpenMPParallelDirective(K) ||
1544 isOpenMPWorksharingDirective(K) ||
1545 isOpenMPTeamsDirective(K);
1546 },
1547 false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001548 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001549 ErrorFound = true;
1550 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1551 ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1552 return;
1553 }
1554
1555 // Define implicit data-sharing attributes for task.
1556 DVar = Stack->getImplicitDSA(FD, false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001557 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1558 !Stack->isLoopControlVariable(FD).first)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001559 ImplicitFirstprivate.push_back(E);
1560 }
1561 }
1562 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001563 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001564 for (auto *C : S->clauses()) {
1565 // Skip analysis of arguments of implicitly defined firstprivate clause
1566 // for task directives.
1567 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1568 for (auto *CC : C->children()) {
1569 if (CC)
1570 Visit(CC);
1571 }
1572 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001573 }
1574 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001575 for (auto *C : S->children()) {
1576 if (C && !isa<OMPExecutableDirective>(C))
1577 Visit(C);
1578 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001579 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001580
1581 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001582 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001583 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001584 return VarsWithInheritedDSA;
1585 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001586
Alexey Bataev7ff55242014-06-19 09:13:45 +00001587 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1588 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001589};
Alexey Bataeved09d242014-05-28 05:53:51 +00001590} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001591
Alexey Bataevbae9a792014-06-27 10:37:06 +00001592void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001593 switch (DKind) {
Kelvin Li70a12c52016-07-13 21:51:49 +00001594 case OMPD_parallel:
1595 case OMPD_parallel_for:
1596 case OMPD_parallel_for_simd:
1597 case OMPD_parallel_sections:
1598 case OMPD_teams: {
Alexey Bataev9959db52014-05-06 10:08:46 +00001599 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001600 QualType KmpInt32PtrTy =
1601 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001602 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001603 std::make_pair(".global_tid.", KmpInt32PtrTy),
1604 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1605 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001606 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001607 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1608 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001609 break;
1610 }
Kelvin Li70a12c52016-07-13 21:51:49 +00001611 case OMPD_simd:
1612 case OMPD_for:
1613 case OMPD_for_simd:
1614 case OMPD_sections:
1615 case OMPD_section:
1616 case OMPD_single:
1617 case OMPD_master:
1618 case OMPD_critical:
Kelvin Lia579b912016-07-14 02:54:56 +00001619 case OMPD_taskgroup:
1620 case OMPD_distribute:
Kelvin Li70a12c52016-07-13 21:51:49 +00001621 case OMPD_ordered:
1622 case OMPD_atomic:
1623 case OMPD_target_data:
1624 case OMPD_target:
1625 case OMPD_target_parallel:
1626 case OMPD_target_parallel_for:
Kelvin Li986330c2016-07-20 22:57:10 +00001627 case OMPD_target_parallel_for_simd:
1628 case OMPD_target_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001629 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001630 std::make_pair(StringRef(), QualType()) // __context with shared vars
1631 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001632 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1633 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001634 break;
1635 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001636 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001637 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001638 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1639 FunctionProtoType::ExtProtoInfo EPI;
1640 EPI.Variadic = true;
1641 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001642 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001643 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataev48591dd2016-04-20 04:01:36 +00001644 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1645 std::make_pair(".privates.", Context.VoidPtrTy.withConst()),
1646 std::make_pair(".copy_fn.",
1647 Context.getPointerType(CopyFnType).withConst()),
1648 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001649 std::make_pair(StringRef(), QualType()) // __context with shared vars
1650 };
1651 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1652 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001653 // Mark this captured region as inlined, because we don't use outlined
1654 // function directly.
1655 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1656 AlwaysInlineAttr::CreateImplicit(
1657 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001658 break;
1659 }
Alexey Bataev1e73ef32016-04-28 12:14:51 +00001660 case OMPD_taskloop:
1661 case OMPD_taskloop_simd: {
Alexey Bataev7292c292016-04-25 12:22:29 +00001662 QualType KmpInt32Ty =
1663 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
1664 QualType KmpUInt64Ty =
1665 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
1666 QualType KmpInt64Ty =
1667 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
1668 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1669 FunctionProtoType::ExtProtoInfo EPI;
1670 EPI.Variadic = true;
1671 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev49f6e782015-12-01 04:18:41 +00001672 Sema::CapturedParamNameType Params[] = {
Alexey Bataev7292c292016-04-25 12:22:29 +00001673 std::make_pair(".global_tid.", KmpInt32Ty),
1674 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1675 std::make_pair(".privates.",
1676 Context.VoidPtrTy.withConst().withRestrict()),
1677 std::make_pair(
1678 ".copy_fn.",
1679 Context.getPointerType(CopyFnType).withConst().withRestrict()),
1680 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
1681 std::make_pair(".lb.", KmpUInt64Ty),
1682 std::make_pair(".ub.", KmpUInt64Ty), std::make_pair(".st.", KmpInt64Ty),
1683 std::make_pair(".liter.", KmpInt32Ty),
Alexey Bataev49f6e782015-12-01 04:18:41 +00001684 std::make_pair(StringRef(), QualType()) // __context with shared vars
1685 };
1686 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1687 Params);
Alexey Bataev7292c292016-04-25 12:22:29 +00001688 // Mark this captured region as inlined, because we don't use outlined
1689 // function directly.
1690 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1691 AlwaysInlineAttr::CreateImplicit(
1692 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev49f6e782015-12-01 04:18:41 +00001693 break;
1694 }
Kelvin Li4a39add2016-07-05 05:00:15 +00001695 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001696 case OMPD_distribute_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001697 case OMPD_distribute_parallel_for: {
1698 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1699 QualType KmpInt32PtrTy =
1700 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
1701 Sema::CapturedParamNameType Params[] = {
1702 std::make_pair(".global_tid.", KmpInt32PtrTy),
1703 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1704 std::make_pair(".previous.lb.", Context.getSizeType()),
1705 std::make_pair(".previous.ub.", Context.getSizeType()),
1706 std::make_pair(StringRef(), QualType()) // __context with shared vars
1707 };
1708 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1709 Params);
1710 break;
1711 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001712 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001713 case OMPD_taskyield:
1714 case OMPD_barrier:
1715 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001716 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001717 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001718 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001719 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001720 case OMPD_target_exit_data:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001721 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001722 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001723 case OMPD_declare_target:
1724 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +00001725 case OMPD_target_update:
Alexey Bataev9959db52014-05-06 10:08:46 +00001726 llvm_unreachable("OpenMP Directive is not allowed");
1727 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001728 llvm_unreachable("Unknown OpenMP directive");
1729 }
1730}
1731
Alexey Bataev3392d762016-02-16 11:18:12 +00001732static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00001733 Expr *CaptureExpr, bool WithInit,
1734 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001735 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00001736 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00001737 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00001738 QualType Ty = Init->getType();
1739 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1740 if (S.getLangOpts().CPlusPlus)
1741 Ty = C.getLValueReferenceType(Ty);
1742 else {
1743 Ty = C.getPointerType(Ty);
1744 ExprResult Res =
1745 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1746 if (!Res.isUsable())
1747 return nullptr;
1748 Init = Res.get();
1749 }
Alexey Bataev61205072016-03-02 04:57:40 +00001750 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00001751 }
1752 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001753 if (!WithInit)
1754 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
Alexey Bataev4244be22016-02-11 05:35:55 +00001755 S.CurContext->addHiddenDecl(CED);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001756 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false,
1757 /*TypeMayContainAuto=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00001758 return CED;
1759}
1760
Alexey Bataev61205072016-03-02 04:57:40 +00001761static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
1762 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00001763 OMPCapturedExprDecl *CD;
1764 if (auto *VD = S.IsOpenMPCapturedDecl(D))
1765 CD = cast<OMPCapturedExprDecl>(VD);
1766 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00001767 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
1768 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001769 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00001770 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00001771}
1772
Alexey Bataev5a3af132016-03-29 08:58:54 +00001773static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
1774 if (!Ref) {
1775 auto *CD =
1776 buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."),
1777 CaptureExpr, /*WithInit=*/true, /*AsExpression=*/true);
1778 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1779 CaptureExpr->getExprLoc());
1780 }
1781 ExprResult Res = Ref;
1782 if (!S.getLangOpts().CPlusPlus &&
1783 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
1784 Ref->getType()->isPointerType())
1785 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
1786 if (!Res.isUsable())
1787 return ExprError();
1788 return CaptureExpr->isGLValue() ? Res : S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00001789}
1790
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001791StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1792 ArrayRef<OMPClause *> Clauses) {
1793 if (!S.isUsable()) {
1794 ActOnCapturedRegionError();
1795 return StmtError();
1796 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001797
1798 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001799 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001800 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001801 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001802 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001803 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001804 Clause->getClauseKind() == OMPC_copyprivate ||
1805 (getLangOpts().OpenMPUseTLS &&
1806 getASTContext().getTargetInfo().isTLSSupported() &&
1807 Clause->getClauseKind() == OMPC_copyin)) {
1808 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001809 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001810 for (auto *VarRef : Clause->children()) {
1811 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001812 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001813 }
1814 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001815 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001816 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev040d5402015-05-12 08:35:28 +00001817 // Mark all variables in private list clauses as used in inner region.
1818 // Required for proper codegen of combined directives.
1819 // TODO: add processing for other clauses.
Alexey Bataev3392d762016-02-16 11:18:12 +00001820 if (auto *C = OMPClauseWithPreInit::get(Clause)) {
Alexey Bataev005248a2016-02-25 05:25:57 +00001821 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
1822 for (auto *D : DS->decls())
Alexey Bataev3392d762016-02-16 11:18:12 +00001823 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
1824 }
Alexey Bataev4244be22016-02-11 05:35:55 +00001825 }
Alexey Bataev005248a2016-02-25 05:25:57 +00001826 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
1827 if (auto *E = C->getPostUpdateExpr())
1828 MarkDeclarationsReferencedInExpr(E);
1829 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001830 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001831 if (Clause->getClauseKind() == OMPC_schedule)
1832 SC = cast<OMPScheduleClause>(Clause);
1833 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001834 OC = cast<OMPOrderedClause>(Clause);
1835 else if (Clause->getClauseKind() == OMPC_linear)
1836 LCs.push_back(cast<OMPLinearClause>(Clause));
1837 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001838 bool ErrorFound = false;
1839 // OpenMP, 2.7.1 Loop Construct, Restrictions
1840 // The nonmonotonic modifier cannot be specified if an ordered clause is
1841 // specified.
1842 if (SC &&
1843 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1844 SC->getSecondScheduleModifier() ==
1845 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1846 OC) {
1847 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1848 ? SC->getFirstScheduleModifierLoc()
1849 : SC->getSecondScheduleModifierLoc(),
1850 diag::err_omp_schedule_nonmonotonic_ordered)
1851 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1852 ErrorFound = true;
1853 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001854 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1855 for (auto *C : LCs) {
1856 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1857 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1858 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001859 ErrorFound = true;
1860 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001861 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1862 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1863 OC->getNumForLoops()) {
1864 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1865 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1866 ErrorFound = true;
1867 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001868 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001869 ActOnCapturedRegionError();
1870 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001871 }
1872 return ActOnCapturedRegionEnd(S.get());
1873}
1874
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001875static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1876 OpenMPDirectiveKind CurrentRegion,
1877 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001878 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001879 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001880 // Allowed nesting of constructs
1881 // +------------------+-----------------+------------------------------------+
1882 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1883 // +------------------+-----------------+------------------------------------+
1884 // | parallel | parallel | * |
1885 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001886 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001887 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001888 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001889 // | parallel | simd | * |
1890 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001891 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001892 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001893 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001894 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001895 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001896 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001897 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001898 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001899 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001900 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001901 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001902 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001903 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001904 // | parallel | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001905 // | parallel | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001906 // | parallel | target parallel | * |
1907 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001908 // | parallel | target enter | * |
1909 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001910 // | parallel | target exit | * |
1911 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001912 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001913 // | parallel | cancellation | |
1914 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001915 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001916 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001917 // | parallel | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00001918 // | parallel | distribute | + |
1919 // | parallel | distribute | + |
1920 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00001921 // | parallel | distribute | + |
1922 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00001923 // | parallel | distribute simd | + |
Kelvin Li986330c2016-07-20 22:57:10 +00001924 // | parallel | target simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001925 // +------------------+-----------------+------------------------------------+
1926 // | for | parallel | * |
1927 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001928 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001929 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001930 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001931 // | for | simd | * |
1932 // | for | sections | + |
1933 // | for | section | + |
1934 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001935 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001936 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001937 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001938 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001939 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001940 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001941 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001942 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001943 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001944 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001945 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001946 // | for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001947 // | for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001948 // | for | target parallel | * |
1949 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001950 // | for | target enter | * |
1951 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001952 // | for | target exit | * |
1953 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001954 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001955 // | for | cancellation | |
1956 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001957 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001958 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001959 // | for | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00001960 // | for | distribute | + |
1961 // | for | distribute | + |
1962 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00001963 // | for | distribute | + |
1964 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00001965 // | for | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00001966 // | for | target parallel | + |
1967 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00001968 // | for | target simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001969 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001970 // | master | parallel | * |
1971 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001972 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001973 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001974 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001975 // | master | simd | * |
1976 // | master | sections | + |
1977 // | master | section | + |
1978 // | master | single | + |
1979 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001980 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001981 // | master |parallel sections| * |
1982 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001983 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001984 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001985 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001986 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001987 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001988 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001989 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001990 // | master | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001991 // | master | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001992 // | master | target parallel | * |
1993 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001994 // | master | target enter | * |
1995 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001996 // | master | target exit | * |
1997 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001998 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001999 // | master | cancellation | |
2000 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002001 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002002 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002003 // | master | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002004 // | master | distribute | + |
2005 // | master | distribute | + |
2006 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002007 // | master | distribute | + |
2008 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002009 // | master | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002010 // | master | target parallel | + |
2011 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002012 // | master | target simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00002013 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002014 // | critical | parallel | * |
2015 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002016 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002017 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002018 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002019 // | critical | simd | * |
2020 // | critical | sections | + |
2021 // | critical | section | + |
2022 // | critical | single | + |
2023 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002024 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002025 // | critical |parallel sections| * |
2026 // | critical | task | * |
2027 // | critical | taskyield | * |
2028 // | critical | barrier | + |
2029 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002030 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002031 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002032 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002033 // | critical | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002034 // | critical | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002035 // | critical | target parallel | * |
2036 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002037 // | critical | target enter | * |
2038 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002039 // | critical | target exit | * |
2040 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002041 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002042 // | critical | cancellation | |
2043 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002044 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002045 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002046 // | critical | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002047 // | critical | distribute | + |
2048 // | critical | distribute | + |
2049 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002050 // | critical | distribute | + |
2051 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002052 // | critical | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002053 // | critical | target parallel | + |
2054 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002055 // | critical | target simd | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002056 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002057 // | simd | parallel | |
2058 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002059 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00002060 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002061 // | simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002062 // | simd | simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002063 // | simd | sections | |
2064 // | simd | section | |
2065 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002066 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002067 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002068 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002069 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00002070 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002071 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002072 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002073 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00002074 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002075 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002076 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002077 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002078 // | simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002079 // | simd | target parallel | |
2080 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002081 // | simd | target enter | |
2082 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002083 // | simd | target exit | |
2084 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002085 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002086 // | simd | cancellation | |
2087 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002088 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002089 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002090 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002091 // | simd | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002092 // | simd | distribute | |
2093 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002094 // | simd | distribute | |
2095 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002096 // | simd | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002097 // | simd | target parallel | |
2098 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002099 // | simd | target simd | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002100 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00002101 // | for simd | parallel | |
2102 // | for simd | for | |
2103 // | for simd | for simd | |
2104 // | for simd | master | |
2105 // | for simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002106 // | for simd | simd | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002107 // | for simd | sections | |
2108 // | for simd | section | |
2109 // | for simd | single | |
2110 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002111 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002112 // | for simd |parallel sections| |
2113 // | for simd | task | |
2114 // | for simd | taskyield | |
2115 // | for simd | barrier | |
2116 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002117 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002118 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002119 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002120 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002121 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002122 // | for simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002123 // | for simd | target parallel | |
2124 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002125 // | for simd | target enter | |
2126 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002127 // | for simd | target exit | |
2128 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002129 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002130 // | for simd | cancellation | |
2131 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002132 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002133 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002134 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002135 // | for simd | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002136 // | for simd | distribute | |
2137 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002138 // | for simd | distribute | |
2139 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002140 // | for simd | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002141 // | for simd | target parallel | |
2142 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002143 // | for simd | target simd | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002144 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00002145 // | parallel for simd| parallel | |
2146 // | parallel for simd| for | |
2147 // | parallel for simd| for simd | |
2148 // | parallel for simd| master | |
2149 // | parallel for simd| critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002150 // | parallel for simd| simd | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002151 // | parallel for simd| sections | |
2152 // | parallel for simd| section | |
2153 // | parallel for simd| single | |
2154 // | parallel for simd| parallel for | |
2155 // | parallel for simd|parallel for simd| |
2156 // | parallel for simd|parallel sections| |
2157 // | parallel for simd| task | |
2158 // | parallel for simd| taskyield | |
2159 // | parallel for simd| barrier | |
2160 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002161 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002162 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002163 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002164 // | parallel for simd| atomic | |
2165 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002166 // | parallel for simd| target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002167 // | parallel for simd| target parallel | |
2168 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002169 // | parallel for simd| target enter | |
2170 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002171 // | parallel for simd| target exit | |
2172 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002173 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002174 // | parallel for simd| cancellation | |
2175 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002176 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002177 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002178 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002179 // | parallel for simd| distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002180 // | parallel for simd| distribute | |
2181 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002182 // | parallel for simd| distribute | |
2183 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002184 // | parallel for simd| distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002185 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002186 // | parallel for simd| target simd | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002187 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002188 // | sections | parallel | * |
2189 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002190 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002191 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002192 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002193 // | sections | simd | * |
2194 // | sections | sections | + |
2195 // | sections | section | * |
2196 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002197 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002198 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002199 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002200 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002201 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002202 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002203 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002204 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002205 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002206 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002207 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002208 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002209 // | sections | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002210 // | sections | target parallel | * |
2211 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002212 // | sections | target enter | * |
2213 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002214 // | sections | target exit | * |
2215 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002216 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002217 // | sections | cancellation | |
2218 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002219 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002220 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002221 // | sections | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002222 // | sections | distribute | + |
2223 // | sections | distribute | + |
2224 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002225 // | sections | distribute | + |
2226 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002227 // | sections | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002228 // | sections | target parallel | + |
2229 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002230 // | sections | target simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002231 // +------------------+-----------------+------------------------------------+
2232 // | section | parallel | * |
2233 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002234 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002235 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002236 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002237 // | section | simd | * |
2238 // | section | sections | + |
2239 // | section | section | + |
2240 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002241 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002242 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002243 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002244 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002245 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002246 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002247 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002248 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002249 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002250 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002251 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002252 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002253 // | section | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002254 // | section | target parallel | * |
2255 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002256 // | section | target enter | * |
2257 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002258 // | section | target exit | * |
2259 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002260 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002261 // | section | cancellation | |
2262 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002263 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002264 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002265 // | section | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002266 // | section | distribute | + |
2267 // | section | distribute | + |
2268 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002269 // | section | distribute | + |
2270 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002271 // | section | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002272 // | section | target parallel | + |
2273 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002274 // | section | target simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002275 // +------------------+-----------------+------------------------------------+
2276 // | single | parallel | * |
2277 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002278 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002279 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002280 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002281 // | single | simd | * |
2282 // | single | sections | + |
2283 // | single | section | + |
2284 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002285 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002286 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002287 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002288 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002289 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002290 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002291 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002292 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002293 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002294 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002295 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002296 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002297 // | single | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002298 // | single | target parallel | * |
2299 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002300 // | single | target enter | * |
2301 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002302 // | single | target exit | * |
2303 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002304 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002305 // | single | cancellation | |
2306 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002307 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002308 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002309 // | single | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002310 // | single | distribute | + |
2311 // | single | distribute | + |
2312 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002313 // | single | distribute | + |
2314 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002315 // | single | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002316 // | single | target parallel | + |
2317 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002318 // | single | target simd | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002319 // +------------------+-----------------+------------------------------------+
2320 // | parallel for | parallel | * |
2321 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002322 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002323 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002324 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002325 // | parallel for | simd | * |
2326 // | parallel for | sections | + |
2327 // | parallel for | section | + |
2328 // | parallel for | single | + |
2329 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002330 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002331 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002332 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002333 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002334 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002335 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002336 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002337 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002338 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002339 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002340 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002341 // | parallel for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002342 // | parallel for | target parallel | * |
2343 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002344 // | parallel for | target enter | * |
2345 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002346 // | parallel for | target exit | * |
2347 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002348 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002349 // | parallel for | cancellation | |
2350 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002351 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002352 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002353 // | parallel for | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002354 // | parallel for | distribute | + |
2355 // | parallel for | distribute | + |
2356 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002357 // | parallel for | distribute | + |
2358 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002359 // | parallel for | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002360 // | parallel for | target parallel | + |
2361 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002362 // | parallel for | target simd | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002363 // +------------------+-----------------+------------------------------------+
2364 // | parallel sections| parallel | * |
2365 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002366 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002367 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002368 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002369 // | parallel sections| simd | * |
2370 // | parallel sections| sections | + |
2371 // | parallel sections| section | * |
2372 // | parallel sections| single | + |
2373 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002374 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002375 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002376 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002377 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002378 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002379 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002380 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002381 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002382 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002383 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002384 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002385 // | parallel sections| target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002386 // | parallel sections| target parallel | * |
2387 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002388 // | parallel sections| target enter | * |
2389 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002390 // | parallel sections| target exit | * |
2391 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002392 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002393 // | parallel sections| cancellation | |
2394 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002395 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002396 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002397 // | parallel sections| taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002398 // | parallel sections| distribute | + |
2399 // | parallel sections| distribute | + |
2400 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002401 // | parallel sections| distribute | + |
2402 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002403 // | parallel sections| distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002404 // | parallel sections| target parallel | + |
2405 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002406 // | parallel sections| target simd | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002407 // +------------------+-----------------+------------------------------------+
2408 // | task | parallel | * |
2409 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002410 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002411 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002412 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002413 // | task | simd | * |
2414 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002415 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002416 // | task | single | + |
2417 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002418 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002419 // | task |parallel sections| * |
2420 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002421 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002422 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002423 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002424 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002425 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002426 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002427 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002428 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002429 // | task | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002430 // | task | target parallel | * |
2431 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002432 // | task | target enter | * |
2433 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002434 // | task | target exit | * |
2435 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002436 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002437 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002438 // | | point | ! |
2439 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002440 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002441 // | task | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002442 // | task | distribute | + |
2443 // | task | distribute | + |
2444 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002445 // | task | distribute | + |
2446 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002447 // | task | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002448 // | task | target parallel | + |
2449 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002450 // | task | target simd | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002451 // +------------------+-----------------+------------------------------------+
2452 // | ordered | parallel | * |
2453 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002454 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002455 // | ordered | master | * |
2456 // | ordered | critical | * |
2457 // | ordered | simd | * |
2458 // | ordered | sections | + |
2459 // | ordered | section | + |
2460 // | ordered | single | + |
2461 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002462 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002463 // | ordered |parallel sections| * |
2464 // | ordered | task | * |
2465 // | ordered | taskyield | * |
2466 // | ordered | barrier | + |
2467 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002468 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002469 // | ordered | flush | * |
2470 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002471 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002472 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002473 // | ordered | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002474 // | ordered | target parallel | * |
2475 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002476 // | ordered | target enter | * |
2477 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002478 // | ordered | target exit | * |
2479 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002480 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002481 // | ordered | cancellation | |
2482 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002483 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002484 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002485 // | ordered | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002486 // | ordered | distribute | + |
2487 // | ordered | distribute | + |
2488 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002489 // | ordered | distribute | + |
2490 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002491 // | ordered | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002492 // | ordered | target parallel | + |
2493 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002494 // | ordered | target simd | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002495 // +------------------+-----------------+------------------------------------+
2496 // | atomic | parallel | |
2497 // | atomic | for | |
2498 // | atomic | for simd | |
2499 // | atomic | master | |
2500 // | atomic | critical | |
2501 // | atomic | simd | |
2502 // | atomic | sections | |
2503 // | atomic | section | |
2504 // | atomic | single | |
2505 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002506 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002507 // | atomic |parallel sections| |
2508 // | atomic | task | |
2509 // | atomic | taskyield | |
2510 // | atomic | barrier | |
2511 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002512 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002513 // | atomic | flush | |
2514 // | atomic | ordered | |
2515 // | atomic | atomic | |
2516 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002517 // | atomic | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002518 // | atomic | target parallel | |
2519 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002520 // | atomic | target enter | |
2521 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002522 // | atomic | target exit | |
2523 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002524 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002525 // | atomic | cancellation | |
2526 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002527 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002528 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002529 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002530 // | atomic | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002531 // | atomic | distribute | |
2532 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002533 // | atomic | distribute | |
2534 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002535 // | atomic | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002536 // | atomic | target parallel | |
2537 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002538 // | atomic | target simd | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002539 // +------------------+-----------------+------------------------------------+
2540 // | target | parallel | * |
2541 // | target | for | * |
2542 // | target | for simd | * |
2543 // | target | master | * |
2544 // | target | critical | * |
2545 // | target | simd | * |
2546 // | target | sections | * |
2547 // | target | section | * |
2548 // | target | single | * |
2549 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002550 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002551 // | target |parallel sections| * |
2552 // | target | task | * |
2553 // | target | taskyield | * |
2554 // | target | barrier | * |
2555 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002556 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002557 // | target | flush | * |
2558 // | target | ordered | * |
2559 // | target | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002560 // | target | target | |
2561 // | target | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002562 // | target | target parallel | |
2563 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002564 // | target | target enter | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002565 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002566 // | target | target exit | |
Samuel Antao72590762016-01-19 20:04:50 +00002567 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002568 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002569 // | target | cancellation | |
2570 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002571 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002572 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002573 // | target | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002574 // | target | distribute | + |
2575 // | target | distribute | + |
2576 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002577 // | target | distribute | + |
2578 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002579 // | target | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002580 // | target | target parallel | |
2581 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002582 // | target | target simd | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002583 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002584 // | target parallel | parallel | * |
2585 // | target parallel | for | * |
2586 // | target parallel | for simd | * |
2587 // | target parallel | master | * |
2588 // | target parallel | critical | * |
2589 // | target parallel | simd | * |
2590 // | target parallel | sections | * |
2591 // | target parallel | section | * |
2592 // | target parallel | single | * |
2593 // | target parallel | parallel for | * |
2594 // | target parallel |parallel for simd| * |
2595 // | target parallel |parallel sections| * |
2596 // | target parallel | task | * |
2597 // | target parallel | taskyield | * |
2598 // | target parallel | barrier | * |
2599 // | target parallel | taskwait | * |
2600 // | target parallel | taskgroup | * |
2601 // | target parallel | flush | * |
2602 // | target parallel | ordered | * |
2603 // | target parallel | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002604 // | target parallel | target | |
2605 // | target parallel | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002606 // | target parallel | target parallel | |
2607 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002608 // | target parallel | target enter | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002609 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002610 // | target parallel | target exit | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002611 // | | data | |
2612 // | target parallel | teams | |
2613 // | target parallel | cancellation | |
2614 // | | point | ! |
2615 // | target parallel | cancel | ! |
2616 // | target parallel | taskloop | * |
2617 // | target parallel | taskloop simd | * |
2618 // | target parallel | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002619 // | target parallel | distribute | |
2620 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002621 // | target parallel | distribute | |
2622 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002623 // | target parallel | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002624 // | target parallel | target parallel | |
2625 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002626 // | target parallel | target simd | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002627 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002628 // | target parallel | parallel | * |
2629 // | for | | |
2630 // | target parallel | for | * |
2631 // | for | | |
2632 // | target parallel | for simd | * |
2633 // | for | | |
2634 // | target parallel | master | * |
2635 // | for | | |
2636 // | target parallel | critical | * |
2637 // | for | | |
2638 // | target parallel | simd | * |
2639 // | for | | |
2640 // | target parallel | sections | * |
2641 // | for | | |
2642 // | target parallel | section | * |
2643 // | for | | |
2644 // | target parallel | single | * |
2645 // | for | | |
2646 // | target parallel | parallel for | * |
2647 // | for | | |
2648 // | target parallel |parallel for simd| * |
2649 // | for | | |
2650 // | target parallel |parallel sections| * |
2651 // | for | | |
2652 // | target parallel | task | * |
2653 // | for | | |
2654 // | target parallel | taskyield | * |
2655 // | for | | |
2656 // | target parallel | barrier | * |
2657 // | for | | |
2658 // | target parallel | taskwait | * |
2659 // | for | | |
2660 // | target parallel | taskgroup | * |
2661 // | for | | |
2662 // | target parallel | flush | * |
2663 // | for | | |
2664 // | target parallel | ordered | * |
2665 // | for | | |
2666 // | target parallel | atomic | * |
2667 // | for | | |
2668 // | target parallel | target | |
2669 // | for | | |
2670 // | target parallel | target parallel | |
2671 // | for | | |
2672 // | target parallel | target parallel | |
2673 // | for | for | |
2674 // | target parallel | target enter | |
2675 // | for | data | |
2676 // | target parallel | target exit | |
2677 // | for | data | |
2678 // | target parallel | teams | |
2679 // | for | | |
2680 // | target parallel | cancellation | |
2681 // | for | point | ! |
2682 // | target parallel | cancel | ! |
2683 // | for | | |
2684 // | target parallel | taskloop | * |
2685 // | for | | |
2686 // | target parallel | taskloop simd | * |
2687 // | for | | |
2688 // | target parallel | distribute | |
2689 // | for | | |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002690 // | target parallel | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002691 // | for | parallel for | |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002692 // | target parallel | distribute | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002693 // | for |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002694 // | target parallel | distribute simd | |
2695 // | for | | |
Kelvin Lia579b912016-07-14 02:54:56 +00002696 // | target parallel | target parallel | |
2697 // | for | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002698 // | target parallel | target simd | |
2699 // | for | | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002700 // +------------------+-----------------+------------------------------------+
Alexey Bataev13314bf2014-10-09 04:18:56 +00002701 // | teams | parallel | * |
2702 // | teams | for | + |
2703 // | teams | for simd | + |
2704 // | teams | master | + |
2705 // | teams | critical | + |
2706 // | teams | simd | + |
2707 // | teams | sections | + |
2708 // | teams | section | + |
2709 // | teams | single | + |
2710 // | teams | parallel for | * |
2711 // | teams |parallel for simd| * |
2712 // | teams |parallel sections| * |
2713 // | teams | task | + |
2714 // | teams | taskyield | + |
2715 // | teams | barrier | + |
2716 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002717 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002718 // | teams | flush | + |
2719 // | teams | ordered | + |
2720 // | teams | atomic | + |
2721 // | teams | target | + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002722 // | teams | target parallel | + |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002723 // | teams | target parallel | + |
2724 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002725 // | teams | target enter | + |
2726 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002727 // | teams | target exit | + |
2728 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002729 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002730 // | teams | cancellation | |
2731 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002732 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002733 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002734 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002735 // | teams | distribute | ! |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002736 // | teams | distribute | ! |
2737 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002738 // | teams | distribute | ! |
2739 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002740 // | teams | distribute simd | ! |
Kelvin Lia579b912016-07-14 02:54:56 +00002741 // | teams | target parallel | + |
2742 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002743 // | teams | target simd | + |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002744 // +------------------+-----------------+------------------------------------+
2745 // | taskloop | parallel | * |
2746 // | taskloop | for | + |
2747 // | taskloop | for simd | + |
2748 // | taskloop | master | + |
2749 // | taskloop | critical | * |
2750 // | taskloop | simd | * |
2751 // | taskloop | sections | + |
2752 // | taskloop | section | + |
2753 // | taskloop | single | + |
2754 // | taskloop | parallel for | * |
2755 // | taskloop |parallel for simd| * |
2756 // | taskloop |parallel sections| * |
2757 // | taskloop | task | * |
2758 // | taskloop | taskyield | * |
2759 // | taskloop | barrier | + |
2760 // | taskloop | taskwait | * |
2761 // | taskloop | taskgroup | * |
2762 // | taskloop | flush | * |
2763 // | taskloop | ordered | + |
2764 // | taskloop | atomic | * |
2765 // | taskloop | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002766 // | taskloop | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002767 // | taskloop | target parallel | * |
2768 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002769 // | taskloop | target enter | * |
2770 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002771 // | taskloop | target exit | * |
2772 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002773 // | taskloop | teams | + |
2774 // | taskloop | cancellation | |
2775 // | | point | |
2776 // | taskloop | cancel | |
2777 // | taskloop | taskloop | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002778 // | taskloop | distribute | + |
2779 // | taskloop | distribute | + |
2780 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002781 // | taskloop | distribute | + |
2782 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002783 // | taskloop | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002784 // | taskloop | target parallel | * |
2785 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002786 // | taskloop | target simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002787 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002788 // | taskloop simd | parallel | |
2789 // | taskloop simd | for | |
2790 // | taskloop simd | for simd | |
2791 // | taskloop simd | master | |
2792 // | taskloop simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002793 // | taskloop simd | simd | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002794 // | taskloop simd | sections | |
2795 // | taskloop simd | section | |
2796 // | taskloop simd | single | |
2797 // | taskloop simd | parallel for | |
2798 // | taskloop simd |parallel for simd| |
2799 // | taskloop simd |parallel sections| |
2800 // | taskloop simd | task | |
2801 // | taskloop simd | taskyield | |
2802 // | taskloop simd | barrier | |
2803 // | taskloop simd | taskwait | |
2804 // | taskloop simd | taskgroup | |
2805 // | taskloop simd | flush | |
2806 // | taskloop simd | ordered | + (with simd clause) |
2807 // | taskloop simd | atomic | |
2808 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002809 // | taskloop simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002810 // | taskloop simd | target parallel | |
2811 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002812 // | taskloop simd | target enter | |
2813 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002814 // | taskloop simd | target exit | |
2815 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002816 // | taskloop simd | teams | |
2817 // | taskloop simd | cancellation | |
2818 // | | point | |
2819 // | taskloop simd | cancel | |
2820 // | taskloop simd | taskloop | |
2821 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002822 // | taskloop simd | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002823 // | taskloop simd | distribute | |
2824 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002825 // | taskloop simd | distribute | |
2826 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002827 // | taskloop simd | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002828 // | taskloop simd | target parallel | |
2829 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002830 // | taskloop simd | target simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002831 // +------------------+-----------------+------------------------------------+
2832 // | distribute | parallel | * |
2833 // | distribute | for | * |
2834 // | distribute | for simd | * |
2835 // | distribute | master | * |
2836 // | distribute | critical | * |
2837 // | distribute | simd | * |
2838 // | distribute | sections | * |
2839 // | distribute | section | * |
2840 // | distribute | single | * |
2841 // | distribute | parallel for | * |
2842 // | distribute |parallel for simd| * |
2843 // | distribute |parallel sections| * |
2844 // | distribute | task | * |
2845 // | distribute | taskyield | * |
2846 // | distribute | barrier | * |
2847 // | distribute | taskwait | * |
2848 // | distribute | taskgroup | * |
2849 // | distribute | flush | * |
2850 // | distribute | ordered | + |
2851 // | distribute | atomic | * |
2852 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002853 // | distribute | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002854 // | distribute | target parallel | |
2855 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002856 // | distribute | target enter | |
2857 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002858 // | distribute | target exit | |
2859 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002860 // | distribute | teams | |
2861 // | distribute | cancellation | + |
2862 // | | point | |
2863 // | distribute | cancel | + |
2864 // | distribute | taskloop | * |
2865 // | distribute | taskloop simd | * |
2866 // | distribute | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002867 // | distribute | distribute | |
2868 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002869 // | distribute | distribute | |
2870 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002871 // | distribute | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002872 // | distribute | target parallel | |
2873 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002874 // | distribute | target simd | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002875 // +------------------+-----------------+------------------------------------+
2876 // | distribute | parallel | * |
2877 // | parallel for | | |
2878 // | distribute | for | * |
2879 // | parallel for | | |
2880 // | distribute | for simd | * |
2881 // | parallel for | | |
2882 // | distribute | master | * |
2883 // | parallel for | | |
2884 // | distribute | critical | * |
2885 // | parallel for | | |
2886 // | distribute | simd | * |
2887 // | parallel for | | |
2888 // | distribute | sections | * |
2889 // | parallel for | | |
2890 // | distribute | section | * |
2891 // | parallel for | | |
2892 // | distribute | single | * |
2893 // | parallel for | | |
2894 // | distribute | parallel for | * |
2895 // | parallel for | | |
2896 // | distribute |parallel for simd| * |
2897 // | parallel for | | |
2898 // | distribute |parallel sections| * |
2899 // | parallel for | | |
2900 // | distribute | task | * |
2901 // | parallel for | | |
2902 // | parallel for | | |
2903 // | distribute | taskyield | * |
2904 // | parallel for | | |
2905 // | distribute | barrier | * |
2906 // | parallel for | | |
2907 // | distribute | taskwait | * |
2908 // | parallel for | | |
2909 // | distribute | taskgroup | * |
2910 // | parallel for | | |
2911 // | distribute | flush | * |
2912 // | parallel for | | |
2913 // | distribute | ordered | + |
2914 // | parallel for | | |
2915 // | distribute | atomic | * |
2916 // | parallel for | | |
2917 // | distribute | target | |
2918 // | parallel for | | |
2919 // | distribute | target parallel | |
2920 // | parallel for | | |
2921 // | distribute | target parallel | |
2922 // | parallel for | for | |
2923 // | distribute | target enter | |
2924 // | parallel for | data | |
2925 // | distribute | target exit | |
2926 // | parallel for | data | |
2927 // | distribute | teams | |
2928 // | parallel for | | |
2929 // | distribute | cancellation | + |
2930 // | parallel for | point | |
2931 // | distribute | cancel | + |
2932 // | parallel for | | |
2933 // | distribute | taskloop | * |
2934 // | parallel for | | |
2935 // | distribute | taskloop simd | * |
2936 // | parallel for | | |
2937 // | distribute | distribute | |
2938 // | parallel for | | |
2939 // | distribute | distribute | |
2940 // | parallel for | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002941 // | distribute | distribute | |
2942 // | parallel for |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002943 // | distribute | distribute simd | |
2944 // | parallel for | | |
Kelvin Lia579b912016-07-14 02:54:56 +00002945 // | distribute | target parallel | |
2946 // | parallel for | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002947 // | distribute | target simd | |
2948 // | parallel for | | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002949 // +------------------+-----------------+------------------------------------+
2950 // | distribute | parallel | * |
2951 // | parallel for simd| | |
2952 // | distribute | for | * |
2953 // | parallel for simd| | |
2954 // | distribute | for simd | * |
2955 // | parallel for simd| | |
2956 // | distribute | master | * |
2957 // | parallel for simd| | |
2958 // | distribute | critical | * |
2959 // | parallel for simd| | |
2960 // | distribute | simd | * |
2961 // | parallel for simd| | |
2962 // | distribute | sections | * |
2963 // | parallel for simd| | |
2964 // | distribute | section | * |
2965 // | parallel for simd| | |
2966 // | distribute | single | * |
2967 // | parallel for simd| | |
2968 // | distribute | parallel for | * |
2969 // | parallel for simd| | |
2970 // | distribute |parallel for simd| * |
2971 // | parallel for simd| | |
2972 // | distribute |parallel sections| * |
2973 // | parallel for simd| | |
2974 // | distribute | task | * |
2975 // | parallel for simd| | |
2976 // | distribute | taskyield | * |
2977 // | parallel for simd| | |
2978 // | distribute | barrier | * |
2979 // | parallel for simd| | |
2980 // | distribute | taskwait | * |
2981 // | parallel for simd| | |
2982 // | distribute | taskgroup | * |
2983 // | parallel for simd| | |
2984 // | distribute | flush | * |
2985 // | parallel for simd| | |
2986 // | distribute | ordered | + |
2987 // | parallel for simd| | |
2988 // | distribute | atomic | * |
2989 // | parallel for simd| | |
2990 // | distribute | target | |
2991 // | parallel for simd| | |
2992 // | distribute | target parallel | |
2993 // | parallel for simd| | |
2994 // | distribute | target parallel | |
2995 // | parallel for simd| for | |
2996 // | distribute | target enter | |
2997 // | parallel for simd| data | |
2998 // | distribute | target exit | |
2999 // | parallel for simd| data | |
3000 // | distribute | teams | |
3001 // | parallel for simd| | |
3002 // | distribute | cancellation | + |
3003 // | parallel for simd| point | |
3004 // | distribute | cancel | + |
3005 // | parallel for simd| | |
3006 // | distribute | taskloop | * |
3007 // | parallel for simd| | |
3008 // | distribute | taskloop simd | * |
3009 // | parallel for simd| | |
3010 // | distribute | distribute | |
3011 // | parallel for simd| | |
3012 // | distribute | distribute | * |
3013 // | parallel for simd| parallel for | |
3014 // | distribute | distribute | * |
3015 // | parallel for simd|parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00003016 // | distribute | distribute simd | * |
3017 // | parallel for simd| | |
Kelvin Lia579b912016-07-14 02:54:56 +00003018 // | distribute | target parallel | |
3019 // | parallel for simd| for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00003020 // | distribute | target simd | |
3021 // | parallel for simd| | |
Kelvin Li787f3fc2016-07-06 04:45:38 +00003022 // +------------------+-----------------+------------------------------------+
3023 // | distribute simd | parallel | * |
3024 // | distribute simd | for | * |
3025 // | distribute simd | for simd | * |
3026 // | distribute simd | master | * |
3027 // | distribute simd | critical | * |
3028 // | distribute simd | simd | * |
3029 // | distribute simd | sections | * |
3030 // | distribute simd | section | * |
3031 // | distribute simd | single | * |
3032 // | distribute simd | parallel for | * |
3033 // | distribute simd |parallel for simd| * |
3034 // | distribute simd |parallel sections| * |
3035 // | distribute simd | task | * |
3036 // | distribute simd | taskyield | * |
3037 // | distribute simd | barrier | * |
3038 // | distribute simd | taskwait | * |
3039 // | distribute simd | taskgroup | * |
3040 // | distribute simd | flush | * |
3041 // | distribute simd | ordered | + |
3042 // | distribute simd | atomic | * |
3043 // | distribute simd | target | * |
3044 // | distribute simd | target parallel | * |
3045 // | distribute simd | target parallel | * |
3046 // | | for | |
3047 // | distribute simd | target enter | * |
3048 // | | data | |
3049 // | distribute simd | target exit | * |
3050 // | | data | |
3051 // | distribute simd | teams | * |
3052 // | distribute simd | cancellation | + |
3053 // | | point | |
3054 // | distribute simd | cancel | + |
3055 // | distribute simd | taskloop | * |
3056 // | distribute simd | taskloop simd | * |
3057 // | distribute simd | distribute | |
3058 // | distribute simd | distribute | * |
3059 // | | parallel for | |
3060 // | distribute simd | distribute | * |
3061 // | |parallel for simd| |
3062 // | distribute simd | distribute simd | * |
Kelvin Lia579b912016-07-14 02:54:56 +00003063 // | distribute simd | target parallel | * |
3064 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00003065 // | distribute simd | target simd | * |
Kelvin Lia579b912016-07-14 02:54:56 +00003066 // +------------------+-----------------+------------------------------------+
3067 // | target parallel | parallel | * |
3068 // | for simd | | |
3069 // | target parallel | for | * |
3070 // | for simd | | |
3071 // | target parallel | for simd | * |
3072 // | for simd | | |
3073 // | target parallel | master | * |
3074 // | for simd | | |
3075 // | target parallel | critical | * |
3076 // | for simd | | |
3077 // | target parallel | simd | ! |
3078 // | for simd | | |
3079 // | target parallel | sections | * |
3080 // | for simd | | |
3081 // | target parallel | section | * |
3082 // | for simd | | |
3083 // | target parallel | single | * |
3084 // | for simd | | |
3085 // | target parallel | parallel for | * |
3086 // | for simd | | |
3087 // | target parallel |parallel for simd| * |
3088 // | for simd | | |
3089 // | target parallel |parallel sections| * |
3090 // | for simd | | |
3091 // | target parallel | task | * |
3092 // | for simd | | |
3093 // | target parallel | taskyield | * |
3094 // | for simd | | |
3095 // | target parallel | barrier | * |
3096 // | for simd | | |
3097 // | target parallel | taskwait | * |
3098 // | for simd | | |
3099 // | target parallel | taskgroup | * |
3100 // | for simd | | |
3101 // | target parallel | flush | * |
3102 // | for simd | | |
3103 // | target parallel | ordered | + (with simd clause) |
3104 // | for simd | | |
3105 // | target parallel | atomic | * |
3106 // | for simd | | |
3107 // | target parallel | target | * |
3108 // | for simd | | |
3109 // | target parallel | target parallel | * |
3110 // | for simd | | |
3111 // | target parallel | target parallel | * |
3112 // | for simd | for | |
3113 // | target parallel | target enter | * |
3114 // | for simd | data | |
3115 // | target parallel | target exit | * |
3116 // | for simd | data | |
3117 // | target parallel | teams | * |
3118 // | for simd | | |
3119 // | target parallel | cancellation | * |
3120 // | for simd | point | |
3121 // | target parallel | cancel | * |
3122 // | for simd | | |
3123 // | target parallel | taskloop | * |
3124 // | for simd | | |
3125 // | target parallel | taskloop simd | * |
3126 // | for simd | | |
3127 // | target parallel | distribute | * |
3128 // | for simd | | |
3129 // | target parallel | distribute | * |
3130 // | for simd | parallel for | |
3131 // | target parallel | distribute | * |
3132 // | for simd |parallel for simd| |
3133 // | target parallel | distribute simd | * |
3134 // | for simd | | |
3135 // | target parallel | target parallel | * |
3136 // | for simd | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00003137 // | target parallel | target simd | * |
3138 // | for simd | | |
3139 // +------------------+-----------------+------------------------------------+
3140 // | target simd | parallel | |
3141 // | target simd | for | |
3142 // | target simd | for simd | |
3143 // | target simd | master | |
3144 // | target simd | critical | |
3145 // | target simd | simd | |
3146 // | target simd | sections | |
3147 // | target simd | section | |
3148 // | target simd | single | |
3149 // | target simd | parallel for | |
3150 // | target simd |parallel for simd| |
3151 // | target simd |parallel sections| |
3152 // | target simd | task | |
3153 // | target simd | taskyield | |
3154 // | target simd | barrier | |
3155 // | target simd | taskwait | |
3156 // | target simd | taskgroup | |
3157 // | target simd | flush | |
3158 // | target simd | ordered | + (with simd clause) |
3159 // | target simd | atomic | |
3160 // | target simd | target | |
3161 // | target simd | target parallel | |
3162 // | target simd | target parallel | |
3163 // | | for | |
3164 // | target simd | target enter | |
3165 // | | data | |
3166 // | target simd | target exit | |
3167 // | | data | |
3168 // | target simd | teams | |
3169 // | target simd | cancellation | |
3170 // | | point | |
3171 // | target simd | cancel | |
3172 // | target simd | taskloop | |
3173 // | target simd | taskloop simd | |
3174 // | target simd | distribute | |
3175 // | target simd | distribute | |
3176 // | | parallel for | |
3177 // | target simd | distribute | |
3178 // | |parallel for simd| |
3179 // | target simd | distribute simd | |
3180 // | target simd | target parallel | |
3181 // | | for simd | |
3182 // | target simd | target simd | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003183 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00003184 if (Stack->getCurScope()) {
3185 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003186 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003187 bool NestingProhibited = false;
3188 bool CloseNesting = true;
Kelvin Li2b51f722016-07-26 04:32:50 +00003189 bool OrphanSeen = false;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003190 enum {
3191 NoRecommend,
3192 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00003193 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003194 ShouldBeInTargetRegion,
3195 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003196 } Recommend = NoRecommend;
Kelvin Lifd8b5742016-07-01 14:30:25 +00003197 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003198 // OpenMP [2.16, Nesting of Regions]
3199 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003200 // OpenMP [2.8.1,simd Construct, Restrictions]
Kelvin Lifd8b5742016-07-01 14:30:25 +00003201 // An ordered construct with the simd clause is the only OpenMP
3202 // construct that can appear in the simd region.
3203 // Allowing a SIMD consruct nested in another SIMD construct is an
3204 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
3205 // message.
3206 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
3207 ? diag::err_omp_prohibited_region_simd
3208 : diag::warn_omp_nesting_simd);
3209 return CurrentRegion != OMPD_simd;
Alexey Bataev549210e2014-06-24 04:39:47 +00003210 }
Alexey Bataev0162e452014-07-22 10:10:35 +00003211 if (ParentRegion == OMPD_atomic) {
3212 // OpenMP [2.16, Nesting of Regions]
3213 // OpenMP constructs may not be nested inside an atomic region.
3214 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
3215 return true;
3216 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003217 if (CurrentRegion == OMPD_section) {
3218 // OpenMP [2.7.2, sections Construct, Restrictions]
3219 // Orphaned section directives are prohibited. That is, the section
3220 // directives must appear within the sections construct and must not be
3221 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003222 if (ParentRegion != OMPD_sections &&
3223 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003224 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
3225 << (ParentRegion != OMPD_unknown)
3226 << getOpenMPDirectiveName(ParentRegion);
3227 return true;
3228 }
3229 return false;
3230 }
Kelvin Li2b51f722016-07-26 04:32:50 +00003231 // Allow some constructs (except teams) to be orphaned (they could be
3232 // used in functions, called from OpenMP regions with the required
3233 // preconditions).
3234 if (ParentRegion == OMPD_unknown && !isOpenMPTeamsDirective(CurrentRegion))
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003235 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00003236 if (CurrentRegion == OMPD_cancellation_point ||
3237 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003238 // OpenMP [2.16, Nesting of Regions]
3239 // A cancellation point construct for which construct-type-clause is
3240 // taskgroup must be nested inside a task construct. A cancellation
3241 // point construct for which construct-type-clause is not taskgroup must
3242 // be closely nested inside an OpenMP construct that matches the type
3243 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00003244 // A cancel construct for which construct-type-clause is taskgroup must be
3245 // nested inside a task construct. A cancel construct for which
3246 // construct-type-clause is not taskgroup must be closely nested inside an
3247 // OpenMP construct that matches the type specified in
3248 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003249 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003250 !((CancelRegion == OMPD_parallel &&
3251 (ParentRegion == OMPD_parallel ||
3252 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00003253 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003254 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
3255 ParentRegion == OMPD_target_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003256 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
3257 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00003258 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
3259 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003260 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00003261 // OpenMP [2.16, Nesting of Regions]
3262 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003263 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00003264 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003265 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003266 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
3267 // OpenMP [2.16, Nesting of Regions]
3268 // A critical region may not be nested (closely or otherwise) inside a
3269 // critical region with the same name. Note that this restriction is not
3270 // sufficient to prevent deadlock.
3271 SourceLocation PreviousCriticalLoc;
3272 bool DeadLock =
3273 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
3274 OpenMPDirectiveKind K,
3275 const DeclarationNameInfo &DNI,
3276 SourceLocation Loc)
3277 ->bool {
3278 if (K == OMPD_critical &&
3279 DNI.getName() == CurrentName.getName()) {
3280 PreviousCriticalLoc = Loc;
3281 return true;
3282 } else
3283 return false;
3284 },
3285 false /* skip top directive */);
3286 if (DeadLock) {
3287 SemaRef.Diag(StartLoc,
3288 diag::err_omp_prohibited_region_critical_same_name)
3289 << CurrentName.getName();
3290 if (PreviousCriticalLoc.isValid())
3291 SemaRef.Diag(PreviousCriticalLoc,
3292 diag::note_omp_previous_critical_region);
3293 return true;
3294 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003295 } else if (CurrentRegion == OMPD_barrier) {
3296 // OpenMP [2.16, Nesting of Regions]
3297 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003298 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003299 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3300 isOpenMPTaskingDirective(ParentRegion) ||
3301 ParentRegion == OMPD_master ||
3302 ParentRegion == OMPD_critical ||
3303 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00003304 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00003305 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003306 // OpenMP [2.16, Nesting of Regions]
3307 // A worksharing region may not be closely nested inside a worksharing,
3308 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003309 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3310 isOpenMPTaskingDirective(ParentRegion) ||
3311 ParentRegion == OMPD_master ||
3312 ParentRegion == OMPD_critical ||
3313 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003314 Recommend = ShouldBeInParallelRegion;
3315 } else if (CurrentRegion == OMPD_ordered) {
3316 // OpenMP [2.16, Nesting of Regions]
3317 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00003318 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003319 // An ordered region must be closely nested inside a loop region (or
3320 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003321 // OpenMP [2.8.1,simd Construct, Restrictions]
3322 // An ordered construct with the simd clause is the only OpenMP construct
3323 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003324 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003325 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003326 !(isOpenMPSimdDirective(ParentRegion) ||
3327 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003328 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003329 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
3330 // OpenMP [2.16, Nesting of Regions]
3331 // If specified, a teams construct must be contained within a target
3332 // construct.
3333 NestingProhibited = ParentRegion != OMPD_target;
Kelvin Li2b51f722016-07-26 04:32:50 +00003334 OrphanSeen = ParentRegion == OMPD_unknown;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003335 Recommend = ShouldBeInTargetRegion;
3336 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
3337 }
3338 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
3339 // OpenMP [2.16, Nesting of Regions]
3340 // distribute, parallel, parallel sections, parallel workshare, and the
3341 // parallel loop and parallel loop SIMD constructs are the only OpenMP
3342 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003343 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
3344 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00003345 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003346 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003347 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
3348 // OpenMP 4.5 [2.17 Nesting of Regions]
3349 // The region associated with the distribute construct must be strictly
3350 // nested inside a teams region
3351 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
3352 Recommend = ShouldBeInTeamsRegion;
3353 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003354 if (!NestingProhibited &&
3355 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
3356 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
3357 // OpenMP 4.5 [2.17 Nesting of Regions]
3358 // If a target, target update, target data, target enter data, or
3359 // target exit data construct is encountered during execution of a
3360 // target region, the behavior is unspecified.
3361 NestingProhibited = Stack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +00003362 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
3363 SourceLocation) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003364 if (isOpenMPTargetExecutionDirective(K)) {
3365 OffendingRegion = K;
3366 return true;
3367 } else
3368 return false;
3369 },
3370 false /* don't skip top directive */);
3371 CloseNesting = false;
3372 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003373 if (NestingProhibited) {
Kelvin Li2b51f722016-07-26 04:32:50 +00003374 if (OrphanSeen) {
3375 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
3376 << getOpenMPDirectiveName(CurrentRegion) << Recommend;
3377 } else {
3378 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
3379 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
3380 << Recommend << getOpenMPDirectiveName(CurrentRegion);
3381 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003382 return true;
3383 }
3384 }
3385 return false;
3386}
3387
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003388static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
3389 ArrayRef<OMPClause *> Clauses,
3390 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
3391 bool ErrorFound = false;
3392 unsigned NamedModifiersNumber = 0;
3393 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
3394 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00003395 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003396 for (const auto *C : Clauses) {
3397 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
3398 // At most one if clause without a directive-name-modifier can appear on
3399 // the directive.
3400 OpenMPDirectiveKind CurNM = IC->getNameModifier();
3401 if (FoundNameModifiers[CurNM]) {
3402 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
3403 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
3404 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
3405 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003406 } else if (CurNM != OMPD_unknown) {
3407 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003408 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003409 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003410 FoundNameModifiers[CurNM] = IC;
3411 if (CurNM == OMPD_unknown)
3412 continue;
3413 // Check if the specified name modifier is allowed for the current
3414 // directive.
3415 // At most one if clause with the particular directive-name-modifier can
3416 // appear on the directive.
3417 bool MatchFound = false;
3418 for (auto NM : AllowedNameModifiers) {
3419 if (CurNM == NM) {
3420 MatchFound = true;
3421 break;
3422 }
3423 }
3424 if (!MatchFound) {
3425 S.Diag(IC->getNameModifierLoc(),
3426 diag::err_omp_wrong_if_directive_name_modifier)
3427 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
3428 ErrorFound = true;
3429 }
3430 }
3431 }
3432 // If any if clause on the directive includes a directive-name-modifier then
3433 // all if clauses on the directive must include a directive-name-modifier.
3434 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3435 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
3436 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
3437 diag::err_omp_no_more_if_clause);
3438 } else {
3439 std::string Values;
3440 std::string Sep(", ");
3441 unsigned AllowedCnt = 0;
3442 unsigned TotalAllowedNum =
3443 AllowedNameModifiers.size() - NamedModifiersNumber;
3444 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3445 ++Cnt) {
3446 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3447 if (!FoundNameModifiers[NM]) {
3448 Values += "'";
3449 Values += getOpenMPDirectiveName(NM);
3450 Values += "'";
3451 if (AllowedCnt + 2 == TotalAllowedNum)
3452 Values += " or ";
3453 else if (AllowedCnt + 1 != TotalAllowedNum)
3454 Values += Sep;
3455 ++AllowedCnt;
3456 }
3457 }
3458 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
3459 diag::err_omp_unnamed_if_clause)
3460 << (TotalAllowedNum > 1) << Values;
3461 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00003462 for (auto Loc : NameModifierLoc) {
3463 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3464 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003465 ErrorFound = true;
3466 }
3467 return ErrorFound;
3468}
3469
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003470StmtResult Sema::ActOnOpenMPExecutableDirective(
3471 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3472 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3473 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003474 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003475 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
3476 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00003477 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003478
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003479 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003480 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003481 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00003482 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00003483 if (AStmt) {
3484 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3485
3486 // Check default data sharing attributes for referenced variables.
3487 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
3488 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
3489 if (DSAChecker.isErrorFound())
3490 return StmtError();
3491 // Generate list of implicitly defined firstprivate variables.
3492 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00003493
3494 if (!DSAChecker.getImplicitFirstprivate().empty()) {
3495 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
3496 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
3497 SourceLocation(), SourceLocation())) {
3498 ClausesWithImplicit.push_back(Implicit);
3499 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
3500 DSAChecker.getImplicitFirstprivate().size();
3501 } else
3502 ErrorFound = true;
3503 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003504 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003505
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003506 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003507 switch (Kind) {
3508 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00003509 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3510 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003511 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003512 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003513 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003514 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3515 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003516 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003517 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003518 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3519 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003520 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003521 case OMPD_for_simd:
3522 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3523 EndLoc, VarsWithInheritedDSA);
3524 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003525 case OMPD_sections:
3526 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3527 EndLoc);
3528 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003529 case OMPD_section:
3530 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003531 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003532 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3533 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003534 case OMPD_single:
3535 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3536 EndLoc);
3537 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003538 case OMPD_master:
3539 assert(ClausesWithImplicit.empty() &&
3540 "No clauses are allowed for 'omp master' directive");
3541 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3542 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003543 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003544 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3545 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003546 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003547 case OMPD_parallel_for:
3548 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3549 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003550 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003551 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003552 case OMPD_parallel_for_simd:
3553 Res = ActOnOpenMPParallelForSimdDirective(
3554 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003555 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003556 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003557 case OMPD_parallel_sections:
3558 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3559 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003560 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003561 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003562 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003563 Res =
3564 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003565 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003566 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003567 case OMPD_taskyield:
3568 assert(ClausesWithImplicit.empty() &&
3569 "No clauses are allowed for 'omp taskyield' directive");
3570 assert(AStmt == nullptr &&
3571 "No associated statement allowed for 'omp taskyield' directive");
3572 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3573 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003574 case OMPD_barrier:
3575 assert(ClausesWithImplicit.empty() &&
3576 "No clauses are allowed for 'omp barrier' directive");
3577 assert(AStmt == nullptr &&
3578 "No associated statement allowed for 'omp barrier' directive");
3579 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3580 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003581 case OMPD_taskwait:
3582 assert(ClausesWithImplicit.empty() &&
3583 "No clauses are allowed for 'omp taskwait' directive");
3584 assert(AStmt == nullptr &&
3585 "No associated statement allowed for 'omp taskwait' directive");
3586 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3587 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003588 case OMPD_taskgroup:
3589 assert(ClausesWithImplicit.empty() &&
3590 "No clauses are allowed for 'omp taskgroup' directive");
3591 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3592 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003593 case OMPD_flush:
3594 assert(AStmt == nullptr &&
3595 "No associated statement allowed for 'omp flush' directive");
3596 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3597 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003598 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003599 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3600 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003601 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003602 case OMPD_atomic:
3603 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3604 EndLoc);
3605 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003606 case OMPD_teams:
3607 Res =
3608 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3609 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003610 case OMPD_target:
3611 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3612 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003613 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003614 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003615 case OMPD_target_parallel:
3616 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3617 StartLoc, EndLoc);
3618 AllowedNameModifiers.push_back(OMPD_target);
3619 AllowedNameModifiers.push_back(OMPD_parallel);
3620 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003621 case OMPD_target_parallel_for:
3622 Res = ActOnOpenMPTargetParallelForDirective(
3623 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3624 AllowedNameModifiers.push_back(OMPD_target);
3625 AllowedNameModifiers.push_back(OMPD_parallel);
3626 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003627 case OMPD_cancellation_point:
3628 assert(ClausesWithImplicit.empty() &&
3629 "No clauses are allowed for 'omp cancellation point' directive");
3630 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3631 "cancellation point' directive");
3632 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3633 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003634 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003635 assert(AStmt == nullptr &&
3636 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003637 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3638 CancelRegion);
3639 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003640 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003641 case OMPD_target_data:
3642 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3643 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003644 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003645 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003646 case OMPD_target_enter_data:
3647 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3648 EndLoc);
3649 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3650 break;
Samuel Antao72590762016-01-19 20:04:50 +00003651 case OMPD_target_exit_data:
3652 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3653 EndLoc);
3654 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3655 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003656 case OMPD_taskloop:
3657 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3658 EndLoc, VarsWithInheritedDSA);
3659 AllowedNameModifiers.push_back(OMPD_taskloop);
3660 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003661 case OMPD_taskloop_simd:
3662 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3663 EndLoc, VarsWithInheritedDSA);
3664 AllowedNameModifiers.push_back(OMPD_taskloop);
3665 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003666 case OMPD_distribute:
3667 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3668 EndLoc, VarsWithInheritedDSA);
3669 break;
Samuel Antao686c70c2016-05-26 17:30:50 +00003670 case OMPD_target_update:
3671 assert(!AStmt && "Statement is not allowed for target update");
3672 Res =
3673 ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, EndLoc);
3674 AllowedNameModifiers.push_back(OMPD_target_update);
3675 break;
Carlo Bertolli9925f152016-06-27 14:55:37 +00003676 case OMPD_distribute_parallel_for:
3677 Res = ActOnOpenMPDistributeParallelForDirective(
3678 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3679 AllowedNameModifiers.push_back(OMPD_parallel);
3680 break;
Kelvin Li4a39add2016-07-05 05:00:15 +00003681 case OMPD_distribute_parallel_for_simd:
3682 Res = ActOnOpenMPDistributeParallelForSimdDirective(
3683 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3684 AllowedNameModifiers.push_back(OMPD_parallel);
3685 break;
Kelvin Li787f3fc2016-07-06 04:45:38 +00003686 case OMPD_distribute_simd:
3687 Res = ActOnOpenMPDistributeSimdDirective(
3688 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3689 break;
Kelvin Lia579b912016-07-14 02:54:56 +00003690 case OMPD_target_parallel_for_simd:
3691 Res = ActOnOpenMPTargetParallelForSimdDirective(
3692 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3693 AllowedNameModifiers.push_back(OMPD_target);
3694 AllowedNameModifiers.push_back(OMPD_parallel);
3695 break;
Kelvin Li986330c2016-07-20 22:57:10 +00003696 case OMPD_target_simd:
3697 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3698 EndLoc, VarsWithInheritedDSA);
3699 AllowedNameModifiers.push_back(OMPD_target);
3700 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003701 case OMPD_declare_target:
3702 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003703 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003704 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003705 case OMPD_declare_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003706 llvm_unreachable("OpenMP Directive is not allowed");
3707 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003708 llvm_unreachable("Unknown OpenMP directive");
3709 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003710
Alexey Bataev4acb8592014-07-07 13:01:15 +00003711 for (auto P : VarsWithInheritedDSA) {
3712 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3713 << P.first << P.second->getSourceRange();
3714 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003715 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3716
3717 if (!AllowedNameModifiers.empty())
3718 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3719 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003720
Alexey Bataeved09d242014-05-28 05:53:51 +00003721 if (ErrorFound)
3722 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003723 return Res;
3724}
3725
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003726Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3727 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00003728 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00003729 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3730 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003731 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00003732 assert(Linears.size() == LinModifiers.size());
3733 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00003734 if (!DG || DG.get().isNull())
3735 return DeclGroupPtrTy();
3736
3737 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00003738 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003739 return DG;
3740 }
3741 auto *ADecl = DG.get().getSingleDecl();
3742 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3743 ADecl = FTD->getTemplatedDecl();
3744
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003745 auto *FD = dyn_cast<FunctionDecl>(ADecl);
3746 if (!FD) {
3747 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003748 return DeclGroupPtrTy();
3749 }
3750
Alexey Bataev2af33e32016-04-07 12:45:37 +00003751 // OpenMP [2.8.2, declare simd construct, Description]
3752 // The parameter of the simdlen clause must be a constant positive integer
3753 // expression.
3754 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003755 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00003756 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003757 // OpenMP [2.8.2, declare simd construct, Description]
3758 // The special this pointer can be used as if was one of the arguments to the
3759 // function in any of the linear, aligned, or uniform clauses.
3760 // The uniform clause declares one or more arguments to have an invariant
3761 // value for all concurrent invocations of the function in the execution of a
3762 // single SIMD loop.
Alexey Bataevecba70f2016-04-12 11:02:11 +00003763 llvm::DenseMap<Decl *, Expr *> UniformedArgs;
3764 Expr *UniformedLinearThis = nullptr;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003765 for (auto *E : Uniforms) {
3766 E = E->IgnoreParenImpCasts();
3767 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3768 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
3769 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3770 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00003771 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
3772 UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E));
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003773 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003774 }
3775 if (isa<CXXThisExpr>(E)) {
3776 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003777 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003778 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003779 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3780 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00003781 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00003782 // OpenMP [2.8.2, declare simd construct, Description]
3783 // The aligned clause declares that the object to which each list item points
3784 // is aligned to the number of bytes expressed in the optional parameter of
3785 // the aligned clause.
3786 // The special this pointer can be used as if was one of the arguments to the
3787 // function in any of the linear, aligned, or uniform clauses.
3788 // The type of list items appearing in the aligned clause must be array,
3789 // pointer, reference to array, or reference to pointer.
3790 llvm::DenseMap<Decl *, Expr *> AlignedArgs;
3791 Expr *AlignedThis = nullptr;
3792 for (auto *E : Aligneds) {
3793 E = E->IgnoreParenImpCasts();
3794 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3795 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3796 auto *CanonPVD = PVD->getCanonicalDecl();
3797 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3798 FD->getParamDecl(PVD->getFunctionScopeIndex())
3799 ->getCanonicalDecl() == CanonPVD) {
3800 // OpenMP [2.8.1, simd construct, Restrictions]
3801 // A list-item cannot appear in more than one aligned clause.
3802 if (AlignedArgs.count(CanonPVD) > 0) {
3803 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3804 << 1 << E->getSourceRange();
3805 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3806 diag::note_omp_explicit_dsa)
3807 << getOpenMPClauseName(OMPC_aligned);
3808 continue;
3809 }
3810 AlignedArgs[CanonPVD] = E;
3811 QualType QTy = PVD->getType()
3812 .getNonReferenceType()
3813 .getUnqualifiedType()
3814 .getCanonicalType();
3815 const Type *Ty = QTy.getTypePtrOrNull();
3816 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3817 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3818 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3819 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3820 }
3821 continue;
3822 }
3823 }
3824 if (isa<CXXThisExpr>(E)) {
3825 if (AlignedThis) {
3826 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3827 << 2 << E->getSourceRange();
3828 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3829 << getOpenMPClauseName(OMPC_aligned);
3830 }
3831 AlignedThis = E;
3832 continue;
3833 }
3834 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3835 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3836 }
3837 // The optional parameter of the aligned clause, alignment, must be a constant
3838 // positive integer expression. If no optional parameter is specified,
3839 // implementation-defined default alignments for SIMD instructions on the
3840 // target platforms are assumed.
3841 SmallVector<Expr *, 4> NewAligns;
3842 for (auto *E : Alignments) {
3843 ExprResult Align;
3844 if (E)
3845 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3846 NewAligns.push_back(Align.get());
3847 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00003848 // OpenMP [2.8.2, declare simd construct, Description]
3849 // The linear clause declares one or more list items to be private to a SIMD
3850 // lane and to have a linear relationship with respect to the iteration space
3851 // of a loop.
3852 // The special this pointer can be used as if was one of the arguments to the
3853 // function in any of the linear, aligned, or uniform clauses.
3854 // When a linear-step expression is specified in a linear clause it must be
3855 // either a constant integer expression or an integer-typed parameter that is
3856 // specified in a uniform clause on the directive.
3857 llvm::DenseMap<Decl *, Expr *> LinearArgs;
3858 const bool IsUniformedThis = UniformedLinearThis != nullptr;
3859 auto MI = LinModifiers.begin();
3860 for (auto *E : Linears) {
3861 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3862 ++MI;
3863 E = E->IgnoreParenImpCasts();
3864 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3865 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3866 auto *CanonPVD = PVD->getCanonicalDecl();
3867 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3868 FD->getParamDecl(PVD->getFunctionScopeIndex())
3869 ->getCanonicalDecl() == CanonPVD) {
3870 // OpenMP [2.15.3.7, linear Clause, Restrictions]
3871 // A list-item cannot appear in more than one linear clause.
3872 if (LinearArgs.count(CanonPVD) > 0) {
3873 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3874 << getOpenMPClauseName(OMPC_linear)
3875 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3876 Diag(LinearArgs[CanonPVD]->getExprLoc(),
3877 diag::note_omp_explicit_dsa)
3878 << getOpenMPClauseName(OMPC_linear);
3879 continue;
3880 }
3881 // Each argument can appear in at most one uniform or linear clause.
3882 if (UniformedArgs.count(CanonPVD) > 0) {
3883 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3884 << getOpenMPClauseName(OMPC_linear)
3885 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3886 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3887 diag::note_omp_explicit_dsa)
3888 << getOpenMPClauseName(OMPC_uniform);
3889 continue;
3890 }
3891 LinearArgs[CanonPVD] = E;
3892 if (E->isValueDependent() || E->isTypeDependent() ||
3893 E->isInstantiationDependent() ||
3894 E->containsUnexpandedParameterPack())
3895 continue;
3896 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3897 PVD->getOriginalType());
3898 continue;
3899 }
3900 }
3901 if (isa<CXXThisExpr>(E)) {
3902 if (UniformedLinearThis) {
3903 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3904 << getOpenMPClauseName(OMPC_linear)
3905 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3906 << E->getSourceRange();
3907 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3908 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3909 : OMPC_linear);
3910 continue;
3911 }
3912 UniformedLinearThis = E;
3913 if (E->isValueDependent() || E->isTypeDependent() ||
3914 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3915 continue;
3916 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3917 E->getType());
3918 continue;
3919 }
3920 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3921 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3922 }
3923 Expr *Step = nullptr;
3924 Expr *NewStep = nullptr;
3925 SmallVector<Expr *, 4> NewSteps;
3926 for (auto *E : Steps) {
3927 // Skip the same step expression, it was checked already.
3928 if (Step == E || !E) {
3929 NewSteps.push_back(E ? NewStep : nullptr);
3930 continue;
3931 }
3932 Step = E;
3933 if (auto *DRE = dyn_cast<DeclRefExpr>(Step))
3934 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3935 auto *CanonPVD = PVD->getCanonicalDecl();
3936 if (UniformedArgs.count(CanonPVD) == 0) {
3937 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3938 << Step->getSourceRange();
3939 } else if (E->isValueDependent() || E->isTypeDependent() ||
3940 E->isInstantiationDependent() ||
3941 E->containsUnexpandedParameterPack() ||
3942 CanonPVD->getType()->hasIntegerRepresentation())
3943 NewSteps.push_back(Step);
3944 else {
3945 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3946 << Step->getSourceRange();
3947 }
3948 continue;
3949 }
3950 NewStep = Step;
3951 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3952 !Step->isInstantiationDependent() &&
3953 !Step->containsUnexpandedParameterPack()) {
3954 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3955 .get();
3956 if (NewStep)
3957 NewStep = VerifyIntegerConstantExpression(NewStep).get();
3958 }
3959 NewSteps.push_back(NewStep);
3960 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003961 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3962 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00003963 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00003964 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3965 const_cast<Expr **>(Linears.data()), Linears.size(),
3966 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3967 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003968 ADecl->addAttr(NewAttr);
3969 return ConvertDeclToDeclGroup(ADecl);
3970}
3971
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003972StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3973 Stmt *AStmt,
3974 SourceLocation StartLoc,
3975 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003976 if (!AStmt)
3977 return StmtError();
3978
Alexey Bataev9959db52014-05-06 10:08:46 +00003979 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3980 // 1.2.2 OpenMP Language Terminology
3981 // Structured block - An executable statement with a single entry at the
3982 // top and a single exit at the bottom.
3983 // The point of exit cannot be a branch out of the structured block.
3984 // longjmp() and throw() must not violate the entry/exit criteria.
3985 CS->getCapturedDecl()->setNothrow();
3986
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003987 getCurFunction()->setHasBranchProtectedScope();
3988
Alexey Bataev25e5b442015-09-15 12:52:43 +00003989 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3990 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003991}
3992
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003993namespace {
3994/// \brief Helper class for checking canonical form of the OpenMP loops and
3995/// extracting iteration space of each loop in the loop nest, that will be used
3996/// for IR generation.
3997class OpenMPIterationSpaceChecker {
3998 /// \brief Reference to Sema.
3999 Sema &SemaRef;
4000 /// \brief A location for diagnostics (when there is no some better location).
4001 SourceLocation DefaultLoc;
4002 /// \brief A location for diagnostics (when increment is not compatible).
4003 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004004 /// \brief A source location for referring to loop init later.
4005 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004006 /// \brief A source location for referring to condition later.
4007 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004008 /// \brief A source location for referring to increment later.
4009 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004010 /// \brief Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004011 ValueDecl *LCDecl = nullptr;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00004012 /// \brief Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004013 Expr *LCRef = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004014 /// \brief Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004015 Expr *LB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004016 /// \brief Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004017 Expr *UB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004018 /// \brief Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004019 Expr *Step = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004020 /// \brief This flag is true when condition is one of:
4021 /// Var < UB
4022 /// Var <= UB
4023 /// UB > Var
4024 /// UB >= Var
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004025 bool TestIsLessOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004026 /// \brief This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004027 bool TestIsStrictOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004028 /// \brief This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004029 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004030
4031public:
4032 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004033 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004034 /// \brief Check init-expr for canonical loop form and save loop counter
4035 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00004036 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004037 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
4038 /// for less/greater and for strict/non-strict comparison.
4039 bool CheckCond(Expr *S);
4040 /// \brief Check incr-expr for canonical loop form and return true if it
4041 /// does not conform, otherwise save loop step (#Step).
4042 bool CheckInc(Expr *S);
4043 /// \brief Return the loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004044 ValueDecl *GetLoopDecl() const { return LCDecl; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00004045 /// \brief Return the reference expression to loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004046 Expr *GetLoopDeclRefExpr() const { return LCRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004047 /// \brief Source range of the loop init.
4048 SourceRange GetInitSrcRange() const { return InitSrcRange; }
4049 /// \brief Source range of the loop condition.
4050 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
4051 /// \brief Source range of the loop increment.
4052 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
4053 /// \brief True if the step should be subtracted.
4054 bool ShouldSubtractStep() const { return SubtractStep; }
4055 /// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004056 Expr *
4057 BuildNumIterations(Scope *S, const bool LimitedType,
4058 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00004059 /// \brief Build the precondition expression for the loops.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004060 Expr *BuildPreCond(Scope *S, Expr *Cond,
4061 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004062 /// \brief Build reference expression to the counter be used for codegen.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004063 DeclRefExpr *BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures,
4064 DSAStackTy &DSA) const;
Alexey Bataeva8899172015-08-06 12:30:57 +00004065 /// \brief Build reference expression to the private counter be used for
4066 /// codegen.
4067 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004068 /// \brief Build initization of the counter be used for codegen.
4069 Expr *BuildCounterInit() const;
4070 /// \brief Build step of the counter be used for codegen.
4071 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004072 /// \brief Return true if any expression is dependent.
4073 bool Dependent() const;
4074
4075private:
4076 /// \brief Check the right-hand side of an assignment in the increment
4077 /// expression.
4078 bool CheckIncRHS(Expr *RHS);
4079 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004080 bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004081 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00004082 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00004083 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004084 /// \brief Helper to set loop increment.
4085 bool SetStep(Expr *NewStep, bool Subtract);
4086};
4087
4088bool OpenMPIterationSpaceChecker::Dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004089 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004090 assert(!LB && !UB && !Step);
4091 return false;
4092 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004093 return LCDecl->getType()->isDependentType() ||
4094 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
4095 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004096}
4097
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004098static Expr *getExprAsWritten(Expr *E) {
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004099 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
4100 E = ExprTemp->getSubExpr();
4101
4102 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
4103 E = MTE->GetTemporaryExpr();
4104
4105 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
4106 E = Binder->getSubExpr();
4107
4108 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
4109 E = ICE->getSubExprAsWritten();
4110 return E->IgnoreParens();
4111}
4112
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004113bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl,
4114 Expr *NewLCRefExpr,
4115 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004116 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004117 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00004118 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004119 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004120 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004121 LCDecl = getCanonicalDecl(NewLCDecl);
4122 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004123 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
4124 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004125 if ((Ctor->isCopyOrMoveConstructor() ||
4126 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4127 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004128 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004129 LB = NewLB;
4130 return false;
4131}
4132
4133bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00004134 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004135 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004136 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
4137 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004138 if (!NewUB)
4139 return true;
4140 UB = NewUB;
4141 TestIsLessOp = LessOp;
4142 TestIsStrictOp = StrictOp;
4143 ConditionSrcRange = SR;
4144 ConditionLoc = SL;
4145 return false;
4146}
4147
4148bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
4149 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004150 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004151 if (!NewStep)
4152 return true;
4153 if (!NewStep->isValueDependent()) {
4154 // Check that the step is integer expression.
4155 SourceLocation StepLoc = NewStep->getLocStart();
4156 ExprResult Val =
4157 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
4158 if (Val.isInvalid())
4159 return true;
4160 NewStep = Val.get();
4161
4162 // OpenMP [2.6, Canonical Loop Form, Restrictions]
4163 // If test-expr is of form var relational-op b and relational-op is < or
4164 // <= then incr-expr must cause var to increase on each iteration of the
4165 // loop. If test-expr is of form var relational-op b and relational-op is
4166 // > or >= then incr-expr must cause var to decrease on each iteration of
4167 // the loop.
4168 // If test-expr is of form b relational-op var and relational-op is < or
4169 // <= then incr-expr must cause var to decrease on each iteration of the
4170 // loop. If test-expr is of form b relational-op var and relational-op is
4171 // > or >= then incr-expr must cause var to increase on each iteration of
4172 // the loop.
4173 llvm::APSInt Result;
4174 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
4175 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
4176 bool IsConstNeg =
4177 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004178 bool IsConstPos =
4179 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004180 bool IsConstZero = IsConstant && !Result.getBoolValue();
4181 if (UB && (IsConstZero ||
4182 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00004183 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004184 SemaRef.Diag(NewStep->getExprLoc(),
4185 diag::err_omp_loop_incr_not_compatible)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004186 << LCDecl << TestIsLessOp << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004187 SemaRef.Diag(ConditionLoc,
4188 diag::note_omp_loop_cond_requres_compatible_incr)
4189 << TestIsLessOp << ConditionSrcRange;
4190 return true;
4191 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004192 if (TestIsLessOp == Subtract) {
4193 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
4194 NewStep).get();
4195 Subtract = !Subtract;
4196 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004197 }
4198
4199 Step = NewStep;
4200 SubtractStep = Subtract;
4201 return false;
4202}
4203
Alexey Bataev9c821032015-04-30 04:23:23 +00004204bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004205 // Check init-expr for canonical loop form and save loop counter
4206 // variable - #Var and its initialization value - #LB.
4207 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
4208 // var = lb
4209 // integer-type var = lb
4210 // random-access-iterator-type var = lb
4211 // pointer-type var = lb
4212 //
4213 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00004214 if (EmitDiags) {
4215 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
4216 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004217 return true;
4218 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004219 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4220 if (!ExprTemp->cleanupsHaveSideEffects())
4221 S = ExprTemp->getSubExpr();
4222
Alexander Musmana5f070a2014-10-01 06:03:56 +00004223 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004224 if (Expr *E = dyn_cast<Expr>(S))
4225 S = E->IgnoreParens();
4226 if (auto BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004227 if (BO->getOpcode() == BO_Assign) {
4228 auto *LHS = BO->getLHS()->IgnoreParens();
4229 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
4230 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4231 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4232 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4233 return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
4234 }
4235 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4236 if (ME->isArrow() &&
4237 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4238 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4239 }
4240 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004241 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
4242 if (DS->isSingleDecl()) {
4243 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00004244 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004245 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00004246 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004247 SemaRef.Diag(S->getLocStart(),
4248 diag::ext_omp_loop_not_canonical_init)
4249 << S->getSourceRange();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004250 return SetLCDeclAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004251 }
4252 }
4253 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004254 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4255 if (CE->getOperator() == OO_Equal) {
4256 auto *LHS = CE->getArg(0);
4257 if (auto DRE = dyn_cast<DeclRefExpr>(LHS)) {
4258 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4259 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4260 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4261 return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
4262 }
4263 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4264 if (ME->isArrow() &&
4265 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4266 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4267 }
4268 }
4269 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004270
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004271 if (Dependent() || SemaRef.CurContext->isDependentContext())
4272 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00004273 if (EmitDiags) {
4274 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
4275 << S->getSourceRange();
4276 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004277 return true;
4278}
4279
Alexey Bataev23b69422014-06-18 07:08:49 +00004280/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004281/// variable (which may be the loop variable) if possible.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004282static const ValueDecl *GetInitLCDecl(Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004283 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00004284 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004285 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004286 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
4287 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004288 if ((Ctor->isCopyOrMoveConstructor() ||
4289 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4290 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004291 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004292 if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
4293 if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
4294 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
4295 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4296 return getCanonicalDecl(ME->getMemberDecl());
4297 return getCanonicalDecl(VD);
4298 }
4299 }
4300 if (auto *ME = dyn_cast_or_null<MemberExpr>(E))
4301 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4302 return getCanonicalDecl(ME->getMemberDecl());
4303 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004304}
4305
4306bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
4307 // Check test-expr for canonical form, save upper-bound UB, flags for
4308 // less/greater and for strict/non-strict comparison.
4309 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4310 // var relational-op b
4311 // b relational-op var
4312 //
4313 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004314 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004315 return true;
4316 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004317 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004318 SourceLocation CondLoc = S->getLocStart();
4319 if (auto BO = dyn_cast<BinaryOperator>(S)) {
4320 if (BO->isRelationalOp()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004321 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004322 return SetUB(BO->getRHS(),
4323 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
4324 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4325 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004326 if (GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004327 return SetUB(BO->getLHS(),
4328 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
4329 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4330 BO->getSourceRange(), BO->getOperatorLoc());
4331 }
4332 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4333 if (CE->getNumArgs() == 2) {
4334 auto Op = CE->getOperator();
4335 switch (Op) {
4336 case OO_Greater:
4337 case OO_GreaterEqual:
4338 case OO_Less:
4339 case OO_LessEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004340 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004341 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
4342 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4343 CE->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004344 if (GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004345 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
4346 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4347 CE->getOperatorLoc());
4348 break;
4349 default:
4350 break;
4351 }
4352 }
4353 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004354 if (Dependent() || SemaRef.CurContext->isDependentContext())
4355 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004356 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004357 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004358 return true;
4359}
4360
4361bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
4362 // RHS of canonical loop form increment can be:
4363 // var + incr
4364 // incr + var
4365 // var - incr
4366 //
4367 RHS = RHS->IgnoreParenImpCasts();
4368 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
4369 if (BO->isAdditiveOp()) {
4370 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004371 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004372 return SetStep(BO->getRHS(), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004373 if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004374 return SetStep(BO->getLHS(), false);
4375 }
4376 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
4377 bool IsAdd = CE->getOperator() == OO_Plus;
4378 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004379 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004380 return SetStep(CE->getArg(1), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004381 if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004382 return SetStep(CE->getArg(0), false);
4383 }
4384 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004385 if (Dependent() || SemaRef.CurContext->isDependentContext())
4386 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004387 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004388 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004389 return true;
4390}
4391
4392bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
4393 // Check incr-expr for canonical loop form and return true if it
4394 // does not conform.
4395 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4396 // ++var
4397 // var++
4398 // --var
4399 // var--
4400 // var += incr
4401 // var -= incr
4402 // var = var + incr
4403 // var = incr + var
4404 // var = var - incr
4405 //
4406 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004407 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004408 return true;
4409 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004410 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4411 if (!ExprTemp->cleanupsHaveSideEffects())
4412 S = ExprTemp->getSubExpr();
4413
Alexander Musmana5f070a2014-10-01 06:03:56 +00004414 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004415 S = S->IgnoreParens();
4416 if (auto UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004417 if (UO->isIncrementDecrementOp() &&
4418 GetInitLCDecl(UO->getSubExpr()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004419 return SetStep(
4420 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
4421 (UO->isDecrementOp() ? -1 : 1)).get(),
4422 false);
4423 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
4424 switch (BO->getOpcode()) {
4425 case BO_AddAssign:
4426 case BO_SubAssign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004427 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004428 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
4429 break;
4430 case BO_Assign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004431 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004432 return CheckIncRHS(BO->getRHS());
4433 break;
4434 default:
4435 break;
4436 }
4437 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4438 switch (CE->getOperator()) {
4439 case OO_PlusPlus:
4440 case OO_MinusMinus:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004441 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004442 return SetStep(
4443 SemaRef.ActOnIntegerConstant(
4444 CE->getLocStart(),
4445 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
4446 false);
4447 break;
4448 case OO_PlusEqual:
4449 case OO_MinusEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004450 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004451 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
4452 break;
4453 case OO_Equal:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004454 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004455 return CheckIncRHS(CE->getArg(1));
4456 break;
4457 default:
4458 break;
4459 }
4460 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004461 if (Dependent() || SemaRef.CurContext->isDependentContext())
4462 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004463 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004464 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004465 return true;
4466}
Alexander Musmana5f070a2014-10-01 06:03:56 +00004467
Alexey Bataev5a3af132016-03-29 08:58:54 +00004468static ExprResult
4469tryBuildCapture(Sema &SemaRef, Expr *Capture,
4470 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00004471 if (SemaRef.CurContext->isDependentContext())
4472 return ExprResult(Capture);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004473 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
4474 return SemaRef.PerformImplicitConversion(
4475 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
4476 /*AllowExplicit=*/true);
4477 auto I = Captures.find(Capture);
4478 if (I != Captures.end())
4479 return buildCapture(SemaRef, Capture, I->second);
4480 DeclRefExpr *Ref = nullptr;
4481 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4482 Captures[Capture] = Ref;
4483 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004484}
4485
Alexander Musmana5f070a2014-10-01 06:03:56 +00004486/// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004487Expr *OpenMPIterationSpaceChecker::BuildNumIterations(
4488 Scope *S, const bool LimitedType,
4489 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004490 ExprResult Diff;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004491 auto VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004492 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004493 SemaRef.getLangOpts().CPlusPlus) {
4494 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004495 auto *UBExpr = TestIsLessOp ? UB : LB;
4496 auto *LBExpr = TestIsLessOp ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00004497 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
4498 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004499 if (!Upper || !Lower)
4500 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004501
4502 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4503
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004504 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004505 // BuildBinOp already emitted error, this one is to point user to upper
4506 // and lower bound, and to tell what is passed to 'operator-'.
4507 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
4508 << Upper->getSourceRange() << Lower->getSourceRange();
4509 return nullptr;
4510 }
4511 }
4512
4513 if (!Diff.isUsable())
4514 return nullptr;
4515
4516 // Upper - Lower [- 1]
4517 if (TestIsStrictOp)
4518 Diff = SemaRef.BuildBinOp(
4519 S, DefaultLoc, BO_Sub, Diff.get(),
4520 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4521 if (!Diff.isUsable())
4522 return nullptr;
4523
4524 // Upper - Lower [- 1] + Step
Alexey Bataev5a3af132016-03-29 08:58:54 +00004525 auto NewStep = tryBuildCapture(SemaRef, Step, Captures);
4526 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004527 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004528 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004529 if (!Diff.isUsable())
4530 return nullptr;
4531
4532 // Parentheses (for dumping/debugging purposes only).
4533 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4534 if (!Diff.isUsable())
4535 return nullptr;
4536
4537 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004538 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004539 if (!Diff.isUsable())
4540 return nullptr;
4541
Alexander Musman174b3ca2014-10-06 11:16:29 +00004542 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004543 QualType Type = Diff.get()->getType();
4544 auto &C = SemaRef.Context;
4545 bool UseVarType = VarType->hasIntegerRepresentation() &&
4546 C.getTypeSize(Type) > C.getTypeSize(VarType);
4547 if (!Type->isIntegerType() || UseVarType) {
4548 unsigned NewSize =
4549 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4550 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4551 : Type->hasSignedIntegerRepresentation();
4552 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00004553 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4554 Diff = SemaRef.PerformImplicitConversion(
4555 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4556 if (!Diff.isUsable())
4557 return nullptr;
4558 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004559 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004560 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00004561 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4562 if (NewSize != C.getTypeSize(Type)) {
4563 if (NewSize < C.getTypeSize(Type)) {
4564 assert(NewSize == 64 && "incorrect loop var size");
4565 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4566 << InitSrcRange << ConditionSrcRange;
4567 }
4568 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004569 NewSize, Type->hasSignedIntegerRepresentation() ||
4570 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00004571 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4572 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4573 Sema::AA_Converting, true);
4574 if (!Diff.isUsable())
4575 return nullptr;
4576 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004577 }
4578 }
4579
Alexander Musmana5f070a2014-10-01 06:03:56 +00004580 return Diff.get();
4581}
4582
Alexey Bataev5a3af132016-03-29 08:58:54 +00004583Expr *OpenMPIterationSpaceChecker::BuildPreCond(
4584 Scope *S, Expr *Cond,
4585 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004586 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4587 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4588 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004589
Alexey Bataev5a3af132016-03-29 08:58:54 +00004590 auto NewLB = tryBuildCapture(SemaRef, LB, Captures);
4591 auto NewUB = tryBuildCapture(SemaRef, UB, Captures);
4592 if (!NewLB.isUsable() || !NewUB.isUsable())
4593 return nullptr;
4594
Alexey Bataev62dbb972015-04-22 11:59:37 +00004595 auto CondExpr = SemaRef.BuildBinOp(
4596 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
4597 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004598 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004599 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004600 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4601 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00004602 CondExpr = SemaRef.PerformImplicitConversion(
4603 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4604 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004605 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00004606 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4607 // Otherwise use original loop conditon and evaluate it in runtime.
4608 return CondExpr.isUsable() ? CondExpr.get() : Cond;
4609}
4610
Alexander Musmana5f070a2014-10-01 06:03:56 +00004611/// \brief Build reference expression to the counter be used for codegen.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004612DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar(
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004613 llvm::MapVector<Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004614 auto *VD = dyn_cast<VarDecl>(LCDecl);
4615 if (!VD) {
4616 VD = SemaRef.IsOpenMPCapturedDecl(LCDecl);
4617 auto *Ref = buildDeclRefExpr(
4618 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004619 DSAStackTy::DSAVarData Data = DSA.getTopDSA(LCDecl, /*FromParent=*/false);
4620 // If the loop control decl is explicitly marked as private, do not mark it
4621 // as captured again.
4622 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4623 Captures.insert(std::make_pair(LCRef, Ref));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004624 return Ref;
4625 }
4626 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00004627 DefaultLoc);
4628}
4629
4630Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004631 if (LCDecl && !LCDecl->isInvalidDecl()) {
4632 auto Type = LCDecl->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00004633 auto *PrivateVar =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004634 buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(),
4635 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00004636 if (PrivateVar->isInvalidDecl())
4637 return nullptr;
4638 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4639 }
4640 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004641}
4642
4643/// \brief Build initization of the counter be used for codegen.
4644Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
4645
4646/// \brief Build step of the counter be used for codegen.
4647Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
4648
4649/// \brief Iteration space of a single for loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004650struct LoopIterationSpace final {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004651 /// \brief Condition of the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004652 Expr *PreCond = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004653 /// \brief This expression calculates the number of iterations in the loop.
4654 /// It is always possible to calculate it before starting the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004655 Expr *NumIterations = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004656 /// \brief The loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004657 Expr *CounterVar = nullptr;
Alexey Bataeva8899172015-08-06 12:30:57 +00004658 /// \brief Private loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004659 Expr *PrivateCounterVar = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004660 /// \brief This is initializer for the initial value of #CounterVar.
Alexey Bataev8b427062016-05-25 12:36:08 +00004661 Expr *CounterInit = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004662 /// \brief This is step for the #CounterVar used to generate its update:
4663 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
Alexey Bataev8b427062016-05-25 12:36:08 +00004664 Expr *CounterStep = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004665 /// \brief Should step be subtracted?
Alexey Bataev8b427062016-05-25 12:36:08 +00004666 bool Subtract = false;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004667 /// \brief Source range of the loop init.
4668 SourceRange InitSrcRange;
4669 /// \brief Source range of the loop condition.
4670 SourceRange CondSrcRange;
4671 /// \brief Source range of the loop increment.
4672 SourceRange IncSrcRange;
4673};
4674
Alexey Bataev23b69422014-06-18 07:08:49 +00004675} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004676
Alexey Bataev9c821032015-04-30 04:23:23 +00004677void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4678 assert(getLangOpts().OpenMP && "OpenMP is not active.");
4679 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004680 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4681 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00004682 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4683 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004684 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
4685 if (auto *D = ISC.GetLoopDecl()) {
4686 auto *VD = dyn_cast<VarDecl>(D);
4687 if (!VD) {
4688 if (auto *Private = IsOpenMPCapturedDecl(D))
4689 VD = Private;
4690 else {
4691 auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(),
4692 /*WithInit=*/false);
4693 VD = cast<VarDecl>(Ref->getDecl());
4694 }
4695 }
4696 DSAStack->addLoopControlVariable(D, VD);
4697 }
4698 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004699 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00004700 }
4701}
4702
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004703/// \brief Called on a for stmt to check and extract its iteration space
4704/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00004705static bool CheckOpenMPIterationSpace(
4706 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4707 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004708 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004709 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004710 LoopIterationSpace &ResultIterSpace,
4711 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004712 // OpenMP [2.6, Canonical Loop Form]
4713 // for (init-expr; test-expr; incr-expr) structured-block
4714 auto For = dyn_cast_or_null<ForStmt>(S);
4715 if (!For) {
4716 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004717 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
4718 << getOpenMPDirectiveName(DKind) << NestedLoopCount
4719 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
4720 if (NestedLoopCount > 1) {
4721 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4722 SemaRef.Diag(DSA.getConstructLoc(),
4723 diag::note_omp_collapse_ordered_expr)
4724 << 2 << CollapseLoopCountExpr->getSourceRange()
4725 << OrderedLoopCountExpr->getSourceRange();
4726 else if (CollapseLoopCountExpr)
4727 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4728 diag::note_omp_collapse_ordered_expr)
4729 << 0 << CollapseLoopCountExpr->getSourceRange();
4730 else
4731 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4732 diag::note_omp_collapse_ordered_expr)
4733 << 1 << OrderedLoopCountExpr->getSourceRange();
4734 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004735 return true;
4736 }
4737 assert(For->getBody());
4738
4739 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4740
4741 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00004742 auto Init = For->getInit();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004743 if (ISC.CheckInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004744 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004745
4746 bool HasErrors = false;
4747
4748 // Check loop variable's type.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004749 if (auto *LCDecl = ISC.GetLoopDecl()) {
4750 auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004751
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004752 // OpenMP [2.6, Canonical Loop Form]
4753 // Var is one of the following:
4754 // A variable of signed or unsigned integer type.
4755 // For C++, a variable of a random access iterator type.
4756 // For C, a variable of a pointer type.
4757 auto VarType = LCDecl->getType().getNonReferenceType();
4758 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4759 !VarType->isPointerType() &&
4760 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
4761 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
4762 << SemaRef.getLangOpts().CPlusPlus;
4763 HasErrors = true;
4764 }
4765
4766 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4767 // a Construct
4768 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4769 // parallel for construct is (are) private.
4770 // The loop iteration variable in the associated for-loop of a simd
4771 // construct with just one associated for-loop is linear with a
4772 // constant-linear-step that is the increment of the associated for-loop.
4773 // Exclude loop var from the list of variables with implicitly defined data
4774 // sharing attributes.
4775 VarsWithImplicitDSA.erase(LCDecl);
4776
4777 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4778 // in a Construct, C/C++].
4779 // The loop iteration variable in the associated for-loop of a simd
4780 // construct with just one associated for-loop may be listed in a linear
4781 // clause with a constant-linear-step that is the increment of the
4782 // associated for-loop.
4783 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4784 // parallel for construct may be listed in a private or lastprivate clause.
4785 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4786 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4787 // declared in the loop and it is predetermined as a private.
4788 auto PredeterminedCKind =
4789 isOpenMPSimdDirective(DKind)
4790 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4791 : OMPC_private;
4792 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4793 DVar.CKind != PredeterminedCKind) ||
4794 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4795 isOpenMPDistributeDirective(DKind)) &&
4796 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4797 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4798 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4799 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
4800 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4801 << getOpenMPClauseName(PredeterminedCKind);
4802 if (DVar.RefExpr == nullptr)
4803 DVar.CKind = PredeterminedCKind;
4804 ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
4805 HasErrors = true;
4806 } else if (LoopDeclRefExpr != nullptr) {
4807 // Make the loop iteration variable private (for worksharing constructs),
4808 // linear (for simd directives with the only one associated loop) or
4809 // lastprivate (for simd directives with several collapsed or ordered
4810 // loops).
4811 if (DVar.CKind == OMPC_unknown)
Alexey Bataev7ace49d2016-05-17 08:55:33 +00004812 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
4813 [](OpenMPDirectiveKind) -> bool { return true; },
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004814 /*FromParent=*/false);
4815 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4816 }
4817
4818 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4819
4820 // Check test-expr.
4821 HasErrors |= ISC.CheckCond(For->getCond());
4822
4823 // Check incr-expr.
4824 HasErrors |= ISC.CheckInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004825 }
4826
Alexander Musmana5f070a2014-10-01 06:03:56 +00004827 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004828 return HasErrors;
4829
Alexander Musmana5f070a2014-10-01 06:03:56 +00004830 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004831 ResultIterSpace.PreCond =
4832 ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures);
Alexander Musman174b3ca2014-10-06 11:16:29 +00004833 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004834 DSA.getCurScope(),
4835 (isOpenMPWorksharingDirective(DKind) ||
4836 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4837 Captures);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004838 ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures, DSA);
Alexey Bataeva8899172015-08-06 12:30:57 +00004839 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004840 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
4841 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
4842 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
4843 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4844 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4845 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4846
Alexey Bataev62dbb972015-04-22 11:59:37 +00004847 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4848 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004849 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004850 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004851 ResultIterSpace.CounterInit == nullptr ||
4852 ResultIterSpace.CounterStep == nullptr);
4853
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004854 return HasErrors;
4855}
4856
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004857/// \brief Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004858static ExprResult
4859BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4860 ExprResult Start,
4861 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004862 // Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004863 auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
4864 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004865 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004866 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00004867 VarRef.get()->getType())) {
4868 NewStart = SemaRef.PerformImplicitConversion(
4869 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4870 /*AllowExplicit=*/true);
4871 if (!NewStart.isUsable())
4872 return ExprError();
4873 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004874
4875 auto Init =
4876 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4877 return Init;
4878}
4879
Alexander Musmana5f070a2014-10-01 06:03:56 +00004880/// \brief Build 'VarRef = Start + Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004881static ExprResult
4882BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc,
4883 ExprResult VarRef, ExprResult Start, ExprResult Iter,
4884 ExprResult Step, bool Subtract,
4885 llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004886 // Add parentheses (for debugging purposes only).
4887 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4888 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4889 !Step.isUsable())
4890 return ExprError();
4891
Alexey Bataev5a3af132016-03-29 08:58:54 +00004892 ExprResult NewStep = Step;
4893 if (Captures)
4894 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004895 if (NewStep.isInvalid())
4896 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004897 ExprResult Update =
4898 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004899 if (!Update.isUsable())
4900 return ExprError();
4901
Alexey Bataevc0214e02016-02-16 12:13:49 +00004902 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4903 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004904 ExprResult NewStart = Start;
4905 if (Captures)
4906 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004907 if (NewStart.isInvalid())
4908 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004909
Alexey Bataevc0214e02016-02-16 12:13:49 +00004910 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4911 ExprResult SavedUpdate = Update;
4912 ExprResult UpdateVal;
4913 if (VarRef.get()->getType()->isOverloadableType() ||
4914 NewStart.get()->getType()->isOverloadableType() ||
4915 Update.get()->getType()->isOverloadableType()) {
4916 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4917 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4918 Update =
4919 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4920 if (Update.isUsable()) {
4921 UpdateVal =
4922 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4923 VarRef.get(), SavedUpdate.get());
4924 if (UpdateVal.isUsable()) {
4925 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4926 UpdateVal.get());
4927 }
4928 }
4929 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4930 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004931
Alexey Bataevc0214e02016-02-16 12:13:49 +00004932 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4933 if (!Update.isUsable() || !UpdateVal.isUsable()) {
4934 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4935 NewStart.get(), SavedUpdate.get());
4936 if (!Update.isUsable())
4937 return ExprError();
4938
Alexey Bataev11481f52016-02-17 10:29:05 +00004939 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4940 VarRef.get()->getType())) {
4941 Update = SemaRef.PerformImplicitConversion(
4942 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4943 if (!Update.isUsable())
4944 return ExprError();
4945 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00004946
4947 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4948 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004949 return Update;
4950}
4951
4952/// \brief Convert integer expression \a E to make it have at least \a Bits
4953/// bits.
4954static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
4955 Sema &SemaRef) {
4956 if (E == nullptr)
4957 return ExprError();
4958 auto &C = SemaRef.Context;
4959 QualType OldType = E->getType();
4960 unsigned HasBits = C.getTypeSize(OldType);
4961 if (HasBits >= Bits)
4962 return ExprResult(E);
4963 // OK to convert to signed, because new type has more bits than old.
4964 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4965 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4966 true);
4967}
4968
4969/// \brief Check if the given expression \a E is a constant integer that fits
4970/// into \a Bits bits.
4971static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4972 if (E == nullptr)
4973 return false;
4974 llvm::APSInt Result;
4975 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4976 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4977 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004978}
4979
Alexey Bataev5a3af132016-03-29 08:58:54 +00004980/// Build preinits statement for the given declarations.
4981static Stmt *buildPreInits(ASTContext &Context,
4982 SmallVectorImpl<Decl *> &PreInits) {
4983 if (!PreInits.empty()) {
4984 return new (Context) DeclStmt(
4985 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
4986 SourceLocation(), SourceLocation());
4987 }
4988 return nullptr;
4989}
4990
4991/// Build preinits statement for the given declarations.
4992static Stmt *buildPreInits(ASTContext &Context,
4993 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4994 if (!Captures.empty()) {
4995 SmallVector<Decl *, 16> PreInits;
4996 for (auto &Pair : Captures)
4997 PreInits.push_back(Pair.second->getDecl());
4998 return buildPreInits(Context, PreInits);
4999 }
5000 return nullptr;
5001}
5002
5003/// Build postupdate expression for the given list of postupdates expressions.
5004static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
5005 Expr *PostUpdate = nullptr;
5006 if (!PostUpdates.empty()) {
5007 for (auto *E : PostUpdates) {
5008 Expr *ConvE = S.BuildCStyleCastExpr(
5009 E->getExprLoc(),
5010 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
5011 E->getExprLoc(), E)
5012 .get();
5013 PostUpdate = PostUpdate
5014 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
5015 PostUpdate, ConvE)
5016 .get()
5017 : ConvE;
5018 }
5019 }
5020 return PostUpdate;
5021}
5022
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005023/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00005024/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
5025/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005026static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00005027CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
5028 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
5029 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005030 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00005031 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005032 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005033 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005034 // Found 'collapse' clause - calculate collapse number.
5035 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005036 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005037 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005038 }
5039 if (OrderedLoopCountExpr) {
5040 // Found 'ordered' clause - calculate collapse number.
5041 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005042 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
5043 if (Result.getLimitedValue() < NestedLoopCount) {
5044 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
5045 diag::err_omp_wrong_ordered_loop_count)
5046 << OrderedLoopCountExpr->getSourceRange();
5047 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
5048 diag::note_collapse_loop_count)
5049 << CollapseLoopCountExpr->getSourceRange();
5050 }
5051 NestedLoopCount = Result.getLimitedValue();
5052 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005053 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005054 // This is helper routine for loop directives (e.g., 'for', 'simd',
5055 // 'for simd', etc.).
Alexey Bataev5a3af132016-03-29 08:58:54 +00005056 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005057 SmallVector<LoopIterationSpace, 4> IterSpaces;
5058 IterSpaces.resize(NestedLoopCount);
5059 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005060 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005061 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00005062 NestedLoopCount, CollapseLoopCountExpr,
5063 OrderedLoopCountExpr, VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005064 IterSpaces[Cnt], Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00005065 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005066 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005067 // OpenMP [2.8.1, simd construct, Restrictions]
5068 // All loops associated with the construct must be perfectly nested; that
5069 // is, there must be no intervening code nor any OpenMP directive between
5070 // any two loops.
5071 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005072 }
5073
Alexander Musmana5f070a2014-10-01 06:03:56 +00005074 Built.clear(/* size */ NestedLoopCount);
5075
5076 if (SemaRef.CurContext->isDependentContext())
5077 return NestedLoopCount;
5078
5079 // An example of what is generated for the following code:
5080 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00005081 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00005082 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00005083 // for (k = 0; k < NK; ++k)
5084 // for (j = J0; j < NJ; j+=2) {
5085 // <loop body>
5086 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005087 //
5088 // We generate the code below.
5089 // Note: the loop body may be outlined in CodeGen.
5090 // Note: some counters may be C++ classes, operator- is used to find number of
5091 // iterations and operator+= to calculate counter value.
5092 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
5093 // or i64 is currently supported).
5094 //
5095 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
5096 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
5097 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
5098 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
5099 // // similar updates for vars in clauses (e.g. 'linear')
5100 // <loop body (using local i and j)>
5101 // }
5102 // i = NI; // assign final values of counters
5103 // j = NJ;
5104 //
5105
5106 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
5107 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00005108 // Precondition tests if there is at least one iteration (all conditions are
5109 // true).
5110 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005111 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005112 ExprResult LastIteration32 = WidenIterationCount(
5113 32 /* Bits */, SemaRef.PerformImplicitConversion(
5114 N0->IgnoreImpCasts(), N0->getType(),
5115 Sema::AA_Converting, /*AllowExplicit=*/true)
5116 .get(),
5117 SemaRef);
5118 ExprResult LastIteration64 = WidenIterationCount(
5119 64 /* Bits */, SemaRef.PerformImplicitConversion(
5120 N0->IgnoreImpCasts(), N0->getType(),
5121 Sema::AA_Converting, /*AllowExplicit=*/true)
5122 .get(),
5123 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005124
5125 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
5126 return NestedLoopCount;
5127
5128 auto &C = SemaRef.Context;
5129 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
5130
5131 Scope *CurScope = DSA.getCurScope();
5132 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00005133 if (PreCond.isUsable()) {
5134 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
5135 PreCond.get(), IterSpaces[Cnt].PreCond);
5136 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005137 auto N = IterSpaces[Cnt].NumIterations;
5138 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
5139 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005140 LastIteration32 = SemaRef.BuildBinOp(
5141 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
5142 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5143 Sema::AA_Converting,
5144 /*AllowExplicit=*/true)
5145 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005146 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005147 LastIteration64 = SemaRef.BuildBinOp(
5148 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
5149 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5150 Sema::AA_Converting,
5151 /*AllowExplicit=*/true)
5152 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005153 }
5154
5155 // Choose either the 32-bit or 64-bit version.
5156 ExprResult LastIteration = LastIteration64;
5157 if (LastIteration32.isUsable() &&
5158 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
5159 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
5160 FitsInto(
5161 32 /* Bits */,
5162 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
5163 LastIteration64.get(), SemaRef)))
5164 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00005165 QualType VType = LastIteration.get()->getType();
5166 QualType RealVType = VType;
5167 QualType StrideVType = VType;
5168 if (isOpenMPTaskLoopDirective(DKind)) {
5169 VType =
5170 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
5171 StrideVType =
5172 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
5173 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005174
5175 if (!LastIteration.isUsable())
5176 return 0;
5177
5178 // Save the number of iterations.
5179 ExprResult NumIterations = LastIteration;
5180 {
5181 LastIteration = SemaRef.BuildBinOp(
5182 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
5183 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5184 if (!LastIteration.isUsable())
5185 return 0;
5186 }
5187
5188 // Calculate the last iteration number beforehand instead of doing this on
5189 // each iteration. Do not do this if the number of iterations may be kfold-ed.
5190 llvm::APSInt Result;
5191 bool IsConstant =
5192 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
5193 ExprResult CalcLastIteration;
5194 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005195 ExprResult SaveRef =
5196 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005197 LastIteration = SaveRef;
5198
5199 // Prepare SaveRef + 1.
5200 NumIterations = SemaRef.BuildBinOp(
Alexey Bataev5a3af132016-03-29 08:58:54 +00005201 CurScope, SourceLocation(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00005202 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5203 if (!NumIterations.isUsable())
5204 return 0;
5205 }
5206
5207 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
5208
Alexander Musmanc6388682014-12-15 07:07:06 +00005209 // Build variables passed into runtime, nesessary for worksharing directives.
Carlo Bertolli9925f152016-06-27 14:55:37 +00005210 ExprResult LB, UB, IL, ST, EUB, PrevLB, PrevUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005211 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5212 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005213 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005214 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
5215 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005216 SemaRef.AddInitializerToDecl(
5217 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5218 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5219
5220 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005221 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
5222 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005223 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
5224 /*DirectInit*/ false,
5225 /*TypeMayContainAuto*/ false);
5226
5227 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
5228 // This will be used to implement clause 'lastprivate'.
5229 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005230 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
5231 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005232 SemaRef.AddInitializerToDecl(
5233 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5234 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5235
5236 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00005237 VarDecl *STDecl =
5238 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
5239 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005240 SemaRef.AddInitializerToDecl(
5241 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
5242 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5243
5244 // Build expression: UB = min(UB, LastIteration)
5245 // It is nesessary for CodeGen of directives with static scheduling.
5246 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
5247 UB.get(), LastIteration.get());
5248 ExprResult CondOp = SemaRef.ActOnConditionalOp(
5249 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
5250 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
5251 CondOp.get());
5252 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
Carlo Bertolli9925f152016-06-27 14:55:37 +00005253
5254 // If we have a combined directive that combines 'distribute', 'for' or
5255 // 'simd' we need to be able to access the bounds of the schedule of the
5256 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
5257 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
5258 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5259 auto *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
5260
5261 // We expect to have at least 2 more parameters than the 'parallel'
5262 // directive does - the lower and upper bounds of the previous schedule.
5263 assert(CD->getNumParams() >= 4 &&
5264 "Unexpected number of parameters in loop combined directive");
5265
5266 // Set the proper type for the bounds given what we learned from the
5267 // enclosed loops.
5268 auto *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
5269 auto *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
5270
5271 // Previous lower and upper bounds are obtained from the region
5272 // parameters.
5273 PrevLB =
5274 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
5275 PrevUB =
5276 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
5277 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005278 }
5279
5280 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005281 ExprResult IV;
5282 ExprResult Init;
5283 {
Alexey Bataev7292c292016-04-25 12:22:29 +00005284 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
5285 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005286 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005287 isOpenMPTaskLoopDirective(DKind) ||
5288 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00005289 ? LB.get()
5290 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
5291 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
5292 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005293 }
5294
Alexander Musmanc6388682014-12-15 07:07:06 +00005295 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005296 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00005297 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005298 (isOpenMPWorksharingDirective(DKind) ||
5299 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00005300 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
5301 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
5302 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005303
5304 // Loop increment (IV = IV + 1)
5305 SourceLocation IncLoc;
5306 ExprResult Inc =
5307 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
5308 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
5309 if (!Inc.isUsable())
5310 return 0;
5311 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00005312 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
5313 if (!Inc.isUsable())
5314 return 0;
5315
5316 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
5317 // Used for directives with static scheduling.
5318 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005319 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5320 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005321 // LB + ST
5322 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
5323 if (!NextLB.isUsable())
5324 return 0;
5325 // LB = LB + ST
5326 NextLB =
5327 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
5328 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
5329 if (!NextLB.isUsable())
5330 return 0;
5331 // UB + ST
5332 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
5333 if (!NextUB.isUsable())
5334 return 0;
5335 // UB = UB + ST
5336 NextUB =
5337 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
5338 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
5339 if (!NextUB.isUsable())
5340 return 0;
5341 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005342
5343 // Build updates and final values of the loop counters.
5344 bool HasErrors = false;
5345 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005346 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005347 Built.Updates.resize(NestedLoopCount);
5348 Built.Finals.resize(NestedLoopCount);
Alexey Bataev8b427062016-05-25 12:36:08 +00005349 SmallVector<Expr *, 4> LoopMultipliers;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005350 {
5351 ExprResult Div;
5352 // Go from inner nested loop to outer.
5353 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5354 LoopIterationSpace &IS = IterSpaces[Cnt];
5355 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
5356 // Build: Iter = (IV / Div) % IS.NumIters
5357 // where Div is product of previous iterations' IS.NumIters.
5358 ExprResult Iter;
5359 if (Div.isUsable()) {
5360 Iter =
5361 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
5362 } else {
5363 Iter = IV;
5364 assert((Cnt == (int)NestedLoopCount - 1) &&
5365 "unusable div expected on first iteration only");
5366 }
5367
5368 if (Cnt != 0 && Iter.isUsable())
5369 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
5370 IS.NumIterations);
5371 if (!Iter.isUsable()) {
5372 HasErrors = true;
5373 break;
5374 }
5375
Alexey Bataev39f915b82015-05-08 10:41:21 +00005376 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005377 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
5378 auto *CounterVar = buildDeclRefExpr(SemaRef, VD, IS.CounterVar->getType(),
5379 IS.CounterVar->getExprLoc(),
5380 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005381 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005382 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005383 if (!Init.isUsable()) {
5384 HasErrors = true;
5385 break;
5386 }
Alexey Bataev5a3af132016-03-29 08:58:54 +00005387 ExprResult Update = BuildCounterUpdate(
5388 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
5389 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005390 if (!Update.isUsable()) {
5391 HasErrors = true;
5392 break;
5393 }
5394
5395 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
5396 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00005397 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005398 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005399 if (!Final.isUsable()) {
5400 HasErrors = true;
5401 break;
5402 }
5403
5404 // Build Div for the next iteration: Div <- Div * IS.NumIters
5405 if (Cnt != 0) {
5406 if (Div.isUnset())
5407 Div = IS.NumIterations;
5408 else
5409 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
5410 IS.NumIterations);
5411
5412 // Add parentheses (for debugging purposes only).
5413 if (Div.isUsable())
Alexey Bataev8b427062016-05-25 12:36:08 +00005414 Div = tryBuildCapture(SemaRef, Div.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005415 if (!Div.isUsable()) {
5416 HasErrors = true;
5417 break;
5418 }
Alexey Bataev8b427062016-05-25 12:36:08 +00005419 LoopMultipliers.push_back(Div.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005420 }
5421 if (!Update.isUsable() || !Final.isUsable()) {
5422 HasErrors = true;
5423 break;
5424 }
5425 // Save results
5426 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00005427 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005428 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005429 Built.Updates[Cnt] = Update.get();
5430 Built.Finals[Cnt] = Final.get();
5431 }
5432 }
5433
5434 if (HasErrors)
5435 return 0;
5436
5437 // Save results
5438 Built.IterationVarRef = IV.get();
5439 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00005440 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00005441 Built.CalcLastIteration =
5442 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005443 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00005444 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005445 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005446 Built.Init = Init.get();
5447 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005448 Built.LB = LB.get();
5449 Built.UB = UB.get();
5450 Built.IL = IL.get();
5451 Built.ST = ST.get();
5452 Built.EUB = EUB.get();
5453 Built.NLB = NextLB.get();
5454 Built.NUB = NextUB.get();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005455 Built.PrevLB = PrevLB.get();
5456 Built.PrevUB = PrevUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005457
Alexey Bataev8b427062016-05-25 12:36:08 +00005458 Expr *CounterVal = SemaRef.DefaultLvalueConversion(IV.get()).get();
5459 // Fill data for doacross depend clauses.
5460 for (auto Pair : DSA.getDoacrossDependClauses()) {
5461 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
5462 Pair.first->setCounterValue(CounterVal);
5463 else {
5464 if (NestedLoopCount != Pair.second.size() ||
5465 NestedLoopCount != LoopMultipliers.size() + 1) {
5466 // Erroneous case - clause has some problems.
5467 Pair.first->setCounterValue(CounterVal);
5468 continue;
5469 }
5470 assert(Pair.first->getDependencyKind() == OMPC_DEPEND_sink);
5471 auto I = Pair.second.rbegin();
5472 auto IS = IterSpaces.rbegin();
5473 auto ILM = LoopMultipliers.rbegin();
5474 Expr *UpCounterVal = CounterVal;
5475 Expr *Multiplier = nullptr;
5476 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5477 if (I->first) {
5478 assert(IS->CounterStep);
5479 Expr *NormalizedOffset =
5480 SemaRef
5481 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Div,
5482 I->first, IS->CounterStep)
5483 .get();
5484 if (Multiplier) {
5485 NormalizedOffset =
5486 SemaRef
5487 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Mul,
5488 NormalizedOffset, Multiplier)
5489 .get();
5490 }
5491 assert(I->second == OO_Plus || I->second == OO_Minus);
5492 BinaryOperatorKind BOK = (I->second == OO_Plus) ? BO_Add : BO_Sub;
5493 UpCounterVal =
5494 SemaRef.BuildBinOp(CurScope, I->first->getExprLoc(), BOK,
5495 UpCounterVal, NormalizedOffset).get();
5496 }
5497 Multiplier = *ILM;
5498 ++I;
5499 ++IS;
5500 ++ILM;
5501 }
5502 Pair.first->setCounterValue(UpCounterVal);
5503 }
5504 }
5505
Alexey Bataevabfc0692014-06-25 06:52:00 +00005506 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005507}
5508
Alexey Bataev10e775f2015-07-30 11:36:16 +00005509static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005510 auto CollapseClauses =
5511 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
5512 if (CollapseClauses.begin() != CollapseClauses.end())
5513 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005514 return nullptr;
5515}
5516
Alexey Bataev10e775f2015-07-30 11:36:16 +00005517static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005518 auto OrderedClauses =
5519 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
5520 if (OrderedClauses.begin() != OrderedClauses.end())
5521 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005522 return nullptr;
5523}
5524
Kelvin Lic5609492016-07-15 04:39:07 +00005525static bool checkSimdlenSafelenSpecified(Sema &S,
5526 const ArrayRef<OMPClause *> Clauses) {
5527 OMPSafelenClause *Safelen = nullptr;
5528 OMPSimdlenClause *Simdlen = nullptr;
5529
5530 for (auto *Clause : Clauses) {
5531 if (Clause->getClauseKind() == OMPC_safelen)
5532 Safelen = cast<OMPSafelenClause>(Clause);
5533 else if (Clause->getClauseKind() == OMPC_simdlen)
5534 Simdlen = cast<OMPSimdlenClause>(Clause);
5535 if (Safelen && Simdlen)
5536 break;
5537 }
5538
5539 if (Simdlen && Safelen) {
5540 llvm::APSInt SimdlenRes, SafelenRes;
5541 auto SimdlenLength = Simdlen->getSimdlen();
5542 auto SafelenLength = Safelen->getSafelen();
5543 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
5544 SimdlenLength->isInstantiationDependent() ||
5545 SimdlenLength->containsUnexpandedParameterPack())
5546 return false;
5547 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
5548 SafelenLength->isInstantiationDependent() ||
5549 SafelenLength->containsUnexpandedParameterPack())
5550 return false;
5551 SimdlenLength->EvaluateAsInt(SimdlenRes, S.Context);
5552 SafelenLength->EvaluateAsInt(SafelenRes, S.Context);
5553 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
5554 // If both simdlen and safelen clauses are specified, the value of the
5555 // simdlen parameter must be less than or equal to the value of the safelen
5556 // parameter.
5557 if (SimdlenRes > SafelenRes) {
5558 S.Diag(SimdlenLength->getExprLoc(),
5559 diag::err_omp_wrong_simdlen_safelen_values)
5560 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
5561 return true;
5562 }
Alexey Bataev66b15b52015-08-21 11:14:16 +00005563 }
5564 return false;
5565}
5566
Alexey Bataev4acb8592014-07-07 13:01:15 +00005567StmtResult Sema::ActOnOpenMPSimdDirective(
5568 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5569 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005570 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005571 if (!AStmt)
5572 return StmtError();
5573
5574 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005575 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005576 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5577 // define the nested loops number.
5578 unsigned NestedLoopCount = CheckOpenMPLoop(
5579 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5580 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005581 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005582 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005583
Alexander Musmana5f070a2014-10-01 06:03:56 +00005584 assert((CurContext->isDependentContext() || B.builtAll()) &&
5585 "omp simd loop exprs were not built");
5586
Alexander Musman3276a272015-03-21 10:12:56 +00005587 if (!CurContext->isDependentContext()) {
5588 // Finalize the clauses that need pre-built expressions for CodeGen.
5589 for (auto C : Clauses) {
5590 if (auto LC = dyn_cast<OMPLinearClause>(C))
5591 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005592 B.NumIterations, *this, CurScope,
5593 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00005594 return StmtError();
5595 }
5596 }
5597
Kelvin Lic5609492016-07-15 04:39:07 +00005598 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005599 return StmtError();
5600
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005601 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005602 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5603 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005604}
5605
Alexey Bataev4acb8592014-07-07 13:01:15 +00005606StmtResult Sema::ActOnOpenMPForDirective(
5607 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5608 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005609 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005610 if (!AStmt)
5611 return StmtError();
5612
5613 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005614 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005615 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5616 // define the nested loops number.
5617 unsigned NestedLoopCount = CheckOpenMPLoop(
5618 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5619 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005620 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005621 return StmtError();
5622
Alexander Musmana5f070a2014-10-01 06:03:56 +00005623 assert((CurContext->isDependentContext() || B.builtAll()) &&
5624 "omp for loop exprs were not built");
5625
Alexey Bataev54acd402015-08-04 11:18:19 +00005626 if (!CurContext->isDependentContext()) {
5627 // Finalize the clauses that need pre-built expressions for CodeGen.
5628 for (auto C : Clauses) {
5629 if (auto LC = dyn_cast<OMPLinearClause>(C))
5630 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005631 B.NumIterations, *this, CurScope,
5632 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005633 return StmtError();
5634 }
5635 }
5636
Alexey Bataevf29276e2014-06-18 04:14:57 +00005637 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005638 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005639 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005640}
5641
Alexander Musmanf82886e2014-09-18 05:12:34 +00005642StmtResult Sema::ActOnOpenMPForSimdDirective(
5643 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5644 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005645 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005646 if (!AStmt)
5647 return StmtError();
5648
5649 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005650 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005651 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5652 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00005653 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005654 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
5655 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5656 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005657 if (NestedLoopCount == 0)
5658 return StmtError();
5659
Alexander Musmanc6388682014-12-15 07:07:06 +00005660 assert((CurContext->isDependentContext() || B.builtAll()) &&
5661 "omp for simd loop exprs were not built");
5662
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005663 if (!CurContext->isDependentContext()) {
5664 // Finalize the clauses that need pre-built expressions for CodeGen.
5665 for (auto C : Clauses) {
5666 if (auto LC = dyn_cast<OMPLinearClause>(C))
5667 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005668 B.NumIterations, *this, CurScope,
5669 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005670 return StmtError();
5671 }
5672 }
5673
Kelvin Lic5609492016-07-15 04:39:07 +00005674 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005675 return StmtError();
5676
Alexander Musmanf82886e2014-09-18 05:12:34 +00005677 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005678 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5679 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005680}
5681
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005682StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5683 Stmt *AStmt,
5684 SourceLocation StartLoc,
5685 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005686 if (!AStmt)
5687 return StmtError();
5688
5689 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005690 auto BaseStmt = AStmt;
5691 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5692 BaseStmt = CS->getCapturedStmt();
5693 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5694 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005695 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005696 return StmtError();
5697 // All associated statements must be '#pragma omp section' except for
5698 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005699 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005700 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5701 if (SectionStmt)
5702 Diag(SectionStmt->getLocStart(),
5703 diag::err_omp_sections_substmt_not_section);
5704 return StmtError();
5705 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005706 cast<OMPSectionDirective>(SectionStmt)
5707 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005708 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005709 } else {
5710 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
5711 return StmtError();
5712 }
5713
5714 getCurFunction()->setHasBranchProtectedScope();
5715
Alexey Bataev25e5b442015-09-15 12:52:43 +00005716 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5717 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005718}
5719
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005720StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5721 SourceLocation StartLoc,
5722 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005723 if (!AStmt)
5724 return StmtError();
5725
5726 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005727
5728 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00005729 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005730
Alexey Bataev25e5b442015-09-15 12:52:43 +00005731 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5732 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005733}
5734
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005735StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5736 Stmt *AStmt,
5737 SourceLocation StartLoc,
5738 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005739 if (!AStmt)
5740 return StmtError();
5741
5742 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00005743
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005744 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00005745
Alexey Bataev3255bf32015-01-19 05:20:46 +00005746 // OpenMP [2.7.3, single Construct, Restrictions]
5747 // The copyprivate clause must not be used with the nowait clause.
5748 OMPClause *Nowait = nullptr;
5749 OMPClause *Copyprivate = nullptr;
5750 for (auto *Clause : Clauses) {
5751 if (Clause->getClauseKind() == OMPC_nowait)
5752 Nowait = Clause;
5753 else if (Clause->getClauseKind() == OMPC_copyprivate)
5754 Copyprivate = Clause;
5755 if (Copyprivate && Nowait) {
5756 Diag(Copyprivate->getLocStart(),
5757 diag::err_omp_single_copyprivate_with_nowait);
5758 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
5759 return StmtError();
5760 }
5761 }
5762
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005763 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5764}
5765
Alexander Musman80c22892014-07-17 08:54:58 +00005766StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5767 SourceLocation StartLoc,
5768 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005769 if (!AStmt)
5770 return StmtError();
5771
5772 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00005773
5774 getCurFunction()->setHasBranchProtectedScope();
5775
5776 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5777}
5778
Alexey Bataev28c75412015-12-15 08:19:24 +00005779StmtResult Sema::ActOnOpenMPCriticalDirective(
5780 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5781 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005782 if (!AStmt)
5783 return StmtError();
5784
5785 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005786
Alexey Bataev28c75412015-12-15 08:19:24 +00005787 bool ErrorFound = false;
5788 llvm::APSInt Hint;
5789 SourceLocation HintLoc;
5790 bool DependentHint = false;
5791 for (auto *C : Clauses) {
5792 if (C->getClauseKind() == OMPC_hint) {
5793 if (!DirName.getName()) {
5794 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
5795 ErrorFound = true;
5796 }
5797 Expr *E = cast<OMPHintClause>(C)->getHint();
5798 if (E->isTypeDependent() || E->isValueDependent() ||
5799 E->isInstantiationDependent())
5800 DependentHint = true;
5801 else {
5802 Hint = E->EvaluateKnownConstInt(Context);
5803 HintLoc = C->getLocStart();
5804 }
5805 }
5806 }
5807 if (ErrorFound)
5808 return StmtError();
5809 auto Pair = DSAStack->getCriticalWithHint(DirName);
5810 if (Pair.first && DirName.getName() && !DependentHint) {
5811 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5812 Diag(StartLoc, diag::err_omp_critical_with_hint);
5813 if (HintLoc.isValid()) {
5814 Diag(HintLoc, diag::note_omp_critical_hint_here)
5815 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
5816 } else
5817 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
5818 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
5819 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
5820 << 1
5821 << C->getHint()->EvaluateKnownConstInt(Context).toString(
5822 /*Radix=*/10, /*Signed=*/false);
5823 } else
5824 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
5825 }
5826 }
5827
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005828 getCurFunction()->setHasBranchProtectedScope();
5829
Alexey Bataev28c75412015-12-15 08:19:24 +00005830 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5831 Clauses, AStmt);
5832 if (!Pair.first && DirName.getName() && !DependentHint)
5833 DSAStack->addCriticalWithHint(Dir, Hint);
5834 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005835}
5836
Alexey Bataev4acb8592014-07-07 13:01:15 +00005837StmtResult Sema::ActOnOpenMPParallelForDirective(
5838 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5839 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005840 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005841 if (!AStmt)
5842 return StmtError();
5843
Alexey Bataev4acb8592014-07-07 13:01:15 +00005844 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5845 // 1.2.2 OpenMP Language Terminology
5846 // Structured block - An executable statement with a single entry at the
5847 // top and a single exit at the bottom.
5848 // The point of exit cannot be a branch out of the structured block.
5849 // longjmp() and throw() must not violate the entry/exit criteria.
5850 CS->getCapturedDecl()->setNothrow();
5851
Alexander Musmanc6388682014-12-15 07:07:06 +00005852 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005853 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5854 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005855 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005856 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
5857 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5858 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005859 if (NestedLoopCount == 0)
5860 return StmtError();
5861
Alexander Musmana5f070a2014-10-01 06:03:56 +00005862 assert((CurContext->isDependentContext() || B.builtAll()) &&
5863 "omp parallel for loop exprs were not built");
5864
Alexey Bataev54acd402015-08-04 11:18:19 +00005865 if (!CurContext->isDependentContext()) {
5866 // Finalize the clauses that need pre-built expressions for CodeGen.
5867 for (auto C : Clauses) {
5868 if (auto LC = dyn_cast<OMPLinearClause>(C))
5869 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005870 B.NumIterations, *this, CurScope,
5871 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005872 return StmtError();
5873 }
5874 }
5875
Alexey Bataev4acb8592014-07-07 13:01:15 +00005876 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005877 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005878 NestedLoopCount, Clauses, AStmt, B,
5879 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00005880}
5881
Alexander Musmane4e893b2014-09-23 09:33:00 +00005882StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5883 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5884 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005885 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005886 if (!AStmt)
5887 return StmtError();
5888
Alexander Musmane4e893b2014-09-23 09:33:00 +00005889 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5890 // 1.2.2 OpenMP Language Terminology
5891 // Structured block - An executable statement with a single entry at the
5892 // top and a single exit at the bottom.
5893 // The point of exit cannot be a branch out of the structured block.
5894 // longjmp() and throw() must not violate the entry/exit criteria.
5895 CS->getCapturedDecl()->setNothrow();
5896
Alexander Musmanc6388682014-12-15 07:07:06 +00005897 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005898 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5899 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00005900 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005901 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
5902 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5903 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005904 if (NestedLoopCount == 0)
5905 return StmtError();
5906
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005907 if (!CurContext->isDependentContext()) {
5908 // Finalize the clauses that need pre-built expressions for CodeGen.
5909 for (auto C : Clauses) {
5910 if (auto LC = dyn_cast<OMPLinearClause>(C))
5911 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005912 B.NumIterations, *this, CurScope,
5913 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005914 return StmtError();
5915 }
5916 }
5917
Kelvin Lic5609492016-07-15 04:39:07 +00005918 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005919 return StmtError();
5920
Alexander Musmane4e893b2014-09-23 09:33:00 +00005921 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005922 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00005923 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005924}
5925
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005926StmtResult
5927Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
5928 Stmt *AStmt, SourceLocation StartLoc,
5929 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005930 if (!AStmt)
5931 return StmtError();
5932
5933 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005934 auto BaseStmt = AStmt;
5935 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5936 BaseStmt = CS->getCapturedStmt();
5937 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5938 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005939 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005940 return StmtError();
5941 // All associated statements must be '#pragma omp section' except for
5942 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005943 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005944 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5945 if (SectionStmt)
5946 Diag(SectionStmt->getLocStart(),
5947 diag::err_omp_parallel_sections_substmt_not_section);
5948 return StmtError();
5949 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005950 cast<OMPSectionDirective>(SectionStmt)
5951 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005952 }
5953 } else {
5954 Diag(AStmt->getLocStart(),
5955 diag::err_omp_parallel_sections_not_compound_stmt);
5956 return StmtError();
5957 }
5958
5959 getCurFunction()->setHasBranchProtectedScope();
5960
Alexey Bataev25e5b442015-09-15 12:52:43 +00005961 return OMPParallelSectionsDirective::Create(
5962 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005963}
5964
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005965StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5966 Stmt *AStmt, SourceLocation StartLoc,
5967 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005968 if (!AStmt)
5969 return StmtError();
5970
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005971 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5972 // 1.2.2 OpenMP Language Terminology
5973 // Structured block - An executable statement with a single entry at the
5974 // top and a single exit at the bottom.
5975 // The point of exit cannot be a branch out of the structured block.
5976 // longjmp() and throw() must not violate the entry/exit criteria.
5977 CS->getCapturedDecl()->setNothrow();
5978
5979 getCurFunction()->setHasBranchProtectedScope();
5980
Alexey Bataev25e5b442015-09-15 12:52:43 +00005981 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5982 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005983}
5984
Alexey Bataev68446b72014-07-18 07:47:19 +00005985StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5986 SourceLocation EndLoc) {
5987 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5988}
5989
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00005990StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5991 SourceLocation EndLoc) {
5992 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5993}
5994
Alexey Bataev2df347a2014-07-18 10:17:07 +00005995StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5996 SourceLocation EndLoc) {
5997 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5998}
5999
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006000StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
6001 SourceLocation StartLoc,
6002 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006003 if (!AStmt)
6004 return StmtError();
6005
6006 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006007
6008 getCurFunction()->setHasBranchProtectedScope();
6009
6010 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
6011}
6012
Alexey Bataev6125da92014-07-21 11:26:11 +00006013StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
6014 SourceLocation StartLoc,
6015 SourceLocation EndLoc) {
6016 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
6017 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
6018}
6019
Alexey Bataev346265e2015-09-25 10:37:12 +00006020StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
6021 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006022 SourceLocation StartLoc,
6023 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00006024 OMPClause *DependFound = nullptr;
6025 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006026 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00006027 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00006028 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006029 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00006030 for (auto *C : Clauses) {
6031 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
6032 DependFound = C;
6033 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
6034 if (DependSourceClause) {
6035 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
6036 << getOpenMPDirectiveName(OMPD_ordered)
6037 << getOpenMPClauseName(OMPC_depend) << 2;
6038 ErrorFound = true;
6039 } else
6040 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006041 if (DependSinkClause) {
6042 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
6043 << 0;
6044 ErrorFound = true;
6045 }
6046 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
6047 if (DependSourceClause) {
6048 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
6049 << 1;
6050 ErrorFound = true;
6051 }
6052 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00006053 }
6054 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00006055 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006056 else if (C->getClauseKind() == OMPC_simd)
6057 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00006058 }
Alexey Bataeveb482352015-12-18 05:05:56 +00006059 if (!ErrorFound && !SC &&
6060 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006061 // OpenMP [2.8.1,simd Construct, Restrictions]
6062 // An ordered construct with the simd clause is the only OpenMP construct
6063 // that can appear in the simd region.
6064 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00006065 ErrorFound = true;
6066 } else if (DependFound && (TC || SC)) {
6067 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
6068 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
6069 ErrorFound = true;
6070 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
6071 Diag(DependFound->getLocStart(),
6072 diag::err_omp_ordered_directive_without_param);
6073 ErrorFound = true;
6074 } else if (TC || Clauses.empty()) {
6075 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
6076 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
6077 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
6078 << (TC != nullptr);
6079 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
6080 ErrorFound = true;
6081 }
6082 }
6083 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006084 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00006085
6086 if (AStmt) {
6087 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6088
6089 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006090 }
Alexey Bataev346265e2015-09-25 10:37:12 +00006091
6092 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006093}
6094
Alexey Bataev1d160b12015-03-13 12:27:31 +00006095namespace {
6096/// \brief Helper class for checking expression in 'omp atomic [update]'
6097/// construct.
6098class OpenMPAtomicUpdateChecker {
6099 /// \brief Error results for atomic update expressions.
6100 enum ExprAnalysisErrorCode {
6101 /// \brief A statement is not an expression statement.
6102 NotAnExpression,
6103 /// \brief Expression is not builtin binary or unary operation.
6104 NotABinaryOrUnaryExpression,
6105 /// \brief Unary operation is not post-/pre- increment/decrement operation.
6106 NotAnUnaryIncDecExpression,
6107 /// \brief An expression is not of scalar type.
6108 NotAScalarType,
6109 /// \brief A binary operation is not an assignment operation.
6110 NotAnAssignmentOp,
6111 /// \brief RHS part of the binary operation is not a binary expression.
6112 NotABinaryExpression,
6113 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
6114 /// expression.
6115 NotABinaryOperator,
6116 /// \brief RHS binary operation does not have reference to the updated LHS
6117 /// part.
6118 NotAnUpdateExpression,
6119 /// \brief No errors is found.
6120 NoError
6121 };
6122 /// \brief Reference to Sema.
6123 Sema &SemaRef;
6124 /// \brief A location for note diagnostics (when error is found).
6125 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006126 /// \brief 'x' lvalue part of the source atomic expression.
6127 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006128 /// \brief 'expr' rvalue part of the source atomic expression.
6129 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006130 /// \brief Helper expression of the form
6131 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6132 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6133 Expr *UpdateExpr;
6134 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
6135 /// important for non-associative operations.
6136 bool IsXLHSInRHSPart;
6137 BinaryOperatorKind Op;
6138 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006139 /// \brief true if the source expression is a postfix unary operation, false
6140 /// if it is a prefix unary operation.
6141 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006142
6143public:
6144 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00006145 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00006146 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00006147 /// \brief Check specified statement that it is suitable for 'atomic update'
6148 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00006149 /// expression. If DiagId and NoteId == 0, then only check is performed
6150 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006151 /// \param DiagId Diagnostic which should be emitted if error is found.
6152 /// \param NoteId Diagnostic note for the main error message.
6153 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00006154 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00006155 /// \brief Return the 'x' lvalue part of the source atomic expression.
6156 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00006157 /// \brief Return the 'expr' rvalue part of the source atomic expression.
6158 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00006159 /// \brief Return the update expression used in calculation of the updated
6160 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6161 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6162 Expr *getUpdateExpr() const { return UpdateExpr; }
6163 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
6164 /// false otherwise.
6165 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
6166
Alexey Bataevb78ca832015-04-01 03:33:17 +00006167 /// \brief true if the source expression is a postfix unary operation, false
6168 /// if it is a prefix unary operation.
6169 bool isPostfixUpdate() const { return IsPostfixUpdate; }
6170
Alexey Bataev1d160b12015-03-13 12:27:31 +00006171private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00006172 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
6173 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00006174};
6175} // namespace
6176
6177bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
6178 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
6179 ExprAnalysisErrorCode ErrorFound = NoError;
6180 SourceLocation ErrorLoc, NoteLoc;
6181 SourceRange ErrorRange, NoteRange;
6182 // Allowed constructs are:
6183 // x = x binop expr;
6184 // x = expr binop x;
6185 if (AtomicBinOp->getOpcode() == BO_Assign) {
6186 X = AtomicBinOp->getLHS();
6187 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
6188 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
6189 if (AtomicInnerBinOp->isMultiplicativeOp() ||
6190 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
6191 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006192 Op = AtomicInnerBinOp->getOpcode();
6193 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006194 auto *LHS = AtomicInnerBinOp->getLHS();
6195 auto *RHS = AtomicInnerBinOp->getRHS();
6196 llvm::FoldingSetNodeID XId, LHSId, RHSId;
6197 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
6198 /*Canonical=*/true);
6199 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
6200 /*Canonical=*/true);
6201 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
6202 /*Canonical=*/true);
6203 if (XId == LHSId) {
6204 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006205 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006206 } else if (XId == RHSId) {
6207 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006208 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006209 } else {
6210 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6211 ErrorRange = AtomicInnerBinOp->getSourceRange();
6212 NoteLoc = X->getExprLoc();
6213 NoteRange = X->getSourceRange();
6214 ErrorFound = NotAnUpdateExpression;
6215 }
6216 } else {
6217 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6218 ErrorRange = AtomicInnerBinOp->getSourceRange();
6219 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
6220 NoteRange = SourceRange(NoteLoc, NoteLoc);
6221 ErrorFound = NotABinaryOperator;
6222 }
6223 } else {
6224 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
6225 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
6226 ErrorFound = NotABinaryExpression;
6227 }
6228 } else {
6229 ErrorLoc = AtomicBinOp->getExprLoc();
6230 ErrorRange = AtomicBinOp->getSourceRange();
6231 NoteLoc = AtomicBinOp->getOperatorLoc();
6232 NoteRange = SourceRange(NoteLoc, NoteLoc);
6233 ErrorFound = NotAnAssignmentOp;
6234 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006235 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006236 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6237 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6238 return true;
6239 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006240 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006241 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006242}
6243
6244bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
6245 unsigned NoteId) {
6246 ExprAnalysisErrorCode ErrorFound = NoError;
6247 SourceLocation ErrorLoc, NoteLoc;
6248 SourceRange ErrorRange, NoteRange;
6249 // Allowed constructs are:
6250 // x++;
6251 // x--;
6252 // ++x;
6253 // --x;
6254 // x binop= expr;
6255 // x = x binop expr;
6256 // x = expr binop x;
6257 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
6258 AtomicBody = AtomicBody->IgnoreParenImpCasts();
6259 if (AtomicBody->getType()->isScalarType() ||
6260 AtomicBody->isInstantiationDependent()) {
6261 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
6262 AtomicBody->IgnoreParenImpCasts())) {
6263 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00006264 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006265 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00006266 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006267 E = AtomicCompAssignOp->getRHS();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006268 X = AtomicCompAssignOp->getLHS()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006269 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006270 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
6271 AtomicBody->IgnoreParenImpCasts())) {
6272 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00006273 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
6274 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006275 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00006276 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
6277 // Check for Unary Operation
6278 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006279 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006280 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
6281 OpLoc = AtomicUnaryOp->getOperatorLoc();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006282 X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006283 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
6284 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006285 } else {
6286 ErrorFound = NotAnUnaryIncDecExpression;
6287 ErrorLoc = AtomicUnaryOp->getExprLoc();
6288 ErrorRange = AtomicUnaryOp->getSourceRange();
6289 NoteLoc = AtomicUnaryOp->getOperatorLoc();
6290 NoteRange = SourceRange(NoteLoc, NoteLoc);
6291 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006292 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006293 ErrorFound = NotABinaryOrUnaryExpression;
6294 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
6295 NoteRange = ErrorRange = AtomicBody->getSourceRange();
6296 }
6297 } else {
6298 ErrorFound = NotAScalarType;
6299 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
6300 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6301 }
6302 } else {
6303 ErrorFound = NotAnExpression;
6304 NoteLoc = ErrorLoc = S->getLocStart();
6305 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6306 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006307 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006308 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6309 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6310 return true;
6311 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006312 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006313 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006314 // Build an update expression of form 'OpaqueValueExpr(x) binop
6315 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
6316 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
6317 auto *OVEX = new (SemaRef.getASTContext())
6318 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
6319 auto *OVEExpr = new (SemaRef.getASTContext())
6320 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
6321 auto Update =
6322 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
6323 IsXLHSInRHSPart ? OVEExpr : OVEX);
6324 if (Update.isInvalid())
6325 return true;
6326 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
6327 Sema::AA_Casting);
6328 if (Update.isInvalid())
6329 return true;
6330 UpdateExpr = Update.get();
6331 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00006332 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006333}
6334
Alexey Bataev0162e452014-07-22 10:10:35 +00006335StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
6336 Stmt *AStmt,
6337 SourceLocation StartLoc,
6338 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006339 if (!AStmt)
6340 return StmtError();
6341
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006342 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00006343 // 1.2.2 OpenMP Language Terminology
6344 // Structured block - An executable statement with a single entry at the
6345 // top and a single exit at the bottom.
6346 // The point of exit cannot be a branch out of the structured block.
6347 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00006348 OpenMPClauseKind AtomicKind = OMPC_unknown;
6349 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006350 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00006351 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00006352 C->getClauseKind() == OMPC_update ||
6353 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00006354 if (AtomicKind != OMPC_unknown) {
6355 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
6356 << SourceRange(C->getLocStart(), C->getLocEnd());
6357 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
6358 << getOpenMPClauseName(AtomicKind);
6359 } else {
6360 AtomicKind = C->getClauseKind();
6361 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006362 }
6363 }
6364 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006365
Alexey Bataev459dec02014-07-24 06:46:57 +00006366 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00006367 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
6368 Body = EWC->getSubExpr();
6369
Alexey Bataev62cec442014-11-18 10:14:22 +00006370 Expr *X = nullptr;
6371 Expr *V = nullptr;
6372 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006373 Expr *UE = nullptr;
6374 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006375 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00006376 // OpenMP [2.12.6, atomic Construct]
6377 // In the next expressions:
6378 // * x and v (as applicable) are both l-value expressions with scalar type.
6379 // * During the execution of an atomic region, multiple syntactic
6380 // occurrences of x must designate the same storage location.
6381 // * Neither of v and expr (as applicable) may access the storage location
6382 // designated by x.
6383 // * Neither of x and expr (as applicable) may access the storage location
6384 // designated by v.
6385 // * expr is an expression with scalar type.
6386 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
6387 // * binop, binop=, ++, and -- are not overloaded operators.
6388 // * The expression x binop expr must be numerically equivalent to x binop
6389 // (expr). This requirement is satisfied if the operators in expr have
6390 // precedence greater than binop, or by using parentheses around expr or
6391 // subexpressions of expr.
6392 // * The expression expr binop x must be numerically equivalent to (expr)
6393 // binop x. This requirement is satisfied if the operators in expr have
6394 // precedence equal to or greater than binop, or by using parentheses around
6395 // expr or subexpressions of expr.
6396 // * For forms that allow multiple occurrences of x, the number of times
6397 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00006398 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006399 enum {
6400 NotAnExpression,
6401 NotAnAssignmentOp,
6402 NotAScalarType,
6403 NotAnLValue,
6404 NoError
6405 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00006406 SourceLocation ErrorLoc, NoteLoc;
6407 SourceRange ErrorRange, NoteRange;
6408 // If clause is read:
6409 // v = x;
6410 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
6411 auto AtomicBinOp =
6412 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6413 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6414 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6415 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
6416 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6417 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
6418 if (!X->isLValue() || !V->isLValue()) {
6419 auto NotLValueExpr = X->isLValue() ? V : X;
6420 ErrorFound = NotAnLValue;
6421 ErrorLoc = AtomicBinOp->getExprLoc();
6422 ErrorRange = AtomicBinOp->getSourceRange();
6423 NoteLoc = NotLValueExpr->getExprLoc();
6424 NoteRange = NotLValueExpr->getSourceRange();
6425 }
6426 } else if (!X->isInstantiationDependent() ||
6427 !V->isInstantiationDependent()) {
6428 auto NotScalarExpr =
6429 (X->isInstantiationDependent() || X->getType()->isScalarType())
6430 ? V
6431 : X;
6432 ErrorFound = NotAScalarType;
6433 ErrorLoc = AtomicBinOp->getExprLoc();
6434 ErrorRange = AtomicBinOp->getSourceRange();
6435 NoteLoc = NotScalarExpr->getExprLoc();
6436 NoteRange = NotScalarExpr->getSourceRange();
6437 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006438 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00006439 ErrorFound = NotAnAssignmentOp;
6440 ErrorLoc = AtomicBody->getExprLoc();
6441 ErrorRange = AtomicBody->getSourceRange();
6442 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6443 : AtomicBody->getExprLoc();
6444 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6445 : AtomicBody->getSourceRange();
6446 }
6447 } else {
6448 ErrorFound = NotAnExpression;
6449 NoteLoc = ErrorLoc = Body->getLocStart();
6450 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006451 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006452 if (ErrorFound != NoError) {
6453 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
6454 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006455 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6456 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00006457 return StmtError();
6458 } else if (CurContext->isDependentContext())
6459 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00006460 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006461 enum {
6462 NotAnExpression,
6463 NotAnAssignmentOp,
6464 NotAScalarType,
6465 NotAnLValue,
6466 NoError
6467 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006468 SourceLocation ErrorLoc, NoteLoc;
6469 SourceRange ErrorRange, NoteRange;
6470 // If clause is write:
6471 // x = expr;
6472 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
6473 auto AtomicBinOp =
6474 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6475 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00006476 X = AtomicBinOp->getLHS();
6477 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006478 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6479 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
6480 if (!X->isLValue()) {
6481 ErrorFound = NotAnLValue;
6482 ErrorLoc = AtomicBinOp->getExprLoc();
6483 ErrorRange = AtomicBinOp->getSourceRange();
6484 NoteLoc = X->getExprLoc();
6485 NoteRange = X->getSourceRange();
6486 }
6487 } else if (!X->isInstantiationDependent() ||
6488 !E->isInstantiationDependent()) {
6489 auto NotScalarExpr =
6490 (X->isInstantiationDependent() || X->getType()->isScalarType())
6491 ? E
6492 : X;
6493 ErrorFound = NotAScalarType;
6494 ErrorLoc = AtomicBinOp->getExprLoc();
6495 ErrorRange = AtomicBinOp->getSourceRange();
6496 NoteLoc = NotScalarExpr->getExprLoc();
6497 NoteRange = NotScalarExpr->getSourceRange();
6498 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006499 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00006500 ErrorFound = NotAnAssignmentOp;
6501 ErrorLoc = AtomicBody->getExprLoc();
6502 ErrorRange = AtomicBody->getSourceRange();
6503 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6504 : AtomicBody->getExprLoc();
6505 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6506 : AtomicBody->getSourceRange();
6507 }
6508 } else {
6509 ErrorFound = NotAnExpression;
6510 NoteLoc = ErrorLoc = Body->getLocStart();
6511 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006512 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00006513 if (ErrorFound != NoError) {
6514 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
6515 << ErrorRange;
6516 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6517 << NoteRange;
6518 return StmtError();
6519 } else if (CurContext->isDependentContext())
6520 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006521 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006522 // If clause is update:
6523 // x++;
6524 // x--;
6525 // ++x;
6526 // --x;
6527 // x binop= expr;
6528 // x = x binop expr;
6529 // x = expr binop x;
6530 OpenMPAtomicUpdateChecker Checker(*this);
6531 if (Checker.checkStatement(
6532 Body, (AtomicKind == OMPC_update)
6533 ? diag::err_omp_atomic_update_not_expression_statement
6534 : diag::err_omp_atomic_not_expression_statement,
6535 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00006536 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006537 if (!CurContext->isDependentContext()) {
6538 E = Checker.getExpr();
6539 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006540 UE = Checker.getUpdateExpr();
6541 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00006542 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006543 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006544 enum {
6545 NotAnAssignmentOp,
6546 NotACompoundStatement,
6547 NotTwoSubstatements,
6548 NotASpecificExpression,
6549 NoError
6550 } ErrorFound = NoError;
6551 SourceLocation ErrorLoc, NoteLoc;
6552 SourceRange ErrorRange, NoteRange;
6553 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6554 // If clause is a capture:
6555 // v = x++;
6556 // v = x--;
6557 // v = ++x;
6558 // v = --x;
6559 // v = x binop= expr;
6560 // v = x = x binop expr;
6561 // v = x = expr binop x;
6562 auto *AtomicBinOp =
6563 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6564 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6565 V = AtomicBinOp->getLHS();
6566 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6567 OpenMPAtomicUpdateChecker Checker(*this);
6568 if (Checker.checkStatement(
6569 Body, diag::err_omp_atomic_capture_not_expression_statement,
6570 diag::note_omp_atomic_update))
6571 return StmtError();
6572 E = Checker.getExpr();
6573 X = Checker.getX();
6574 UE = Checker.getUpdateExpr();
6575 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6576 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00006577 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006578 ErrorLoc = AtomicBody->getExprLoc();
6579 ErrorRange = AtomicBody->getSourceRange();
6580 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6581 : AtomicBody->getExprLoc();
6582 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6583 : AtomicBody->getSourceRange();
6584 ErrorFound = NotAnAssignmentOp;
6585 }
6586 if (ErrorFound != NoError) {
6587 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6588 << ErrorRange;
6589 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6590 return StmtError();
6591 } else if (CurContext->isDependentContext()) {
6592 UE = V = E = X = nullptr;
6593 }
6594 } else {
6595 // If clause is a capture:
6596 // { v = x; x = expr; }
6597 // { v = x; x++; }
6598 // { v = x; x--; }
6599 // { v = x; ++x; }
6600 // { v = x; --x; }
6601 // { v = x; x binop= expr; }
6602 // { v = x; x = x binop expr; }
6603 // { v = x; x = expr binop x; }
6604 // { x++; v = x; }
6605 // { x--; v = x; }
6606 // { ++x; v = x; }
6607 // { --x; v = x; }
6608 // { x binop= expr; v = x; }
6609 // { x = x binop expr; v = x; }
6610 // { x = expr binop x; v = x; }
6611 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6612 // Check that this is { expr1; expr2; }
6613 if (CS->size() == 2) {
6614 auto *First = CS->body_front();
6615 auto *Second = CS->body_back();
6616 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6617 First = EWC->getSubExpr()->IgnoreParenImpCasts();
6618 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6619 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6620 // Need to find what subexpression is 'v' and what is 'x'.
6621 OpenMPAtomicUpdateChecker Checker(*this);
6622 bool IsUpdateExprFound = !Checker.checkStatement(Second);
6623 BinaryOperator *BinOp = nullptr;
6624 if (IsUpdateExprFound) {
6625 BinOp = dyn_cast<BinaryOperator>(First);
6626 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6627 }
6628 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6629 // { v = x; x++; }
6630 // { v = x; x--; }
6631 // { v = x; ++x; }
6632 // { v = x; --x; }
6633 // { v = x; x binop= expr; }
6634 // { v = x; x = x binop expr; }
6635 // { v = x; x = expr binop x; }
6636 // Check that the first expression has form v = x.
6637 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6638 llvm::FoldingSetNodeID XId, PossibleXId;
6639 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6640 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6641 IsUpdateExprFound = XId == PossibleXId;
6642 if (IsUpdateExprFound) {
6643 V = BinOp->getLHS();
6644 X = Checker.getX();
6645 E = Checker.getExpr();
6646 UE = Checker.getUpdateExpr();
6647 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006648 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006649 }
6650 }
6651 if (!IsUpdateExprFound) {
6652 IsUpdateExprFound = !Checker.checkStatement(First);
6653 BinOp = nullptr;
6654 if (IsUpdateExprFound) {
6655 BinOp = dyn_cast<BinaryOperator>(Second);
6656 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6657 }
6658 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6659 // { x++; v = x; }
6660 // { x--; v = x; }
6661 // { ++x; v = x; }
6662 // { --x; v = x; }
6663 // { x binop= expr; v = x; }
6664 // { x = x binop expr; v = x; }
6665 // { x = expr binop x; v = x; }
6666 // Check that the second expression has form v = x.
6667 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6668 llvm::FoldingSetNodeID XId, PossibleXId;
6669 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6670 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6671 IsUpdateExprFound = XId == PossibleXId;
6672 if (IsUpdateExprFound) {
6673 V = BinOp->getLHS();
6674 X = Checker.getX();
6675 E = Checker.getExpr();
6676 UE = Checker.getUpdateExpr();
6677 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006678 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006679 }
6680 }
6681 }
6682 if (!IsUpdateExprFound) {
6683 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00006684 auto *FirstExpr = dyn_cast<Expr>(First);
6685 auto *SecondExpr = dyn_cast<Expr>(Second);
6686 if (!FirstExpr || !SecondExpr ||
6687 !(FirstExpr->isInstantiationDependent() ||
6688 SecondExpr->isInstantiationDependent())) {
6689 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6690 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006691 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00006692 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
6693 : First->getLocStart();
6694 NoteRange = ErrorRange = FirstBinOp
6695 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00006696 : SourceRange(ErrorLoc, ErrorLoc);
6697 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006698 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6699 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6700 ErrorFound = NotAnAssignmentOp;
6701 NoteLoc = ErrorLoc = SecondBinOp
6702 ? SecondBinOp->getOperatorLoc()
6703 : Second->getLocStart();
6704 NoteRange = ErrorRange =
6705 SecondBinOp ? SecondBinOp->getSourceRange()
6706 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00006707 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006708 auto *PossibleXRHSInFirst =
6709 FirstBinOp->getRHS()->IgnoreParenImpCasts();
6710 auto *PossibleXLHSInSecond =
6711 SecondBinOp->getLHS()->IgnoreParenImpCasts();
6712 llvm::FoldingSetNodeID X1Id, X2Id;
6713 PossibleXRHSInFirst->Profile(X1Id, Context,
6714 /*Canonical=*/true);
6715 PossibleXLHSInSecond->Profile(X2Id, Context,
6716 /*Canonical=*/true);
6717 IsUpdateExprFound = X1Id == X2Id;
6718 if (IsUpdateExprFound) {
6719 V = FirstBinOp->getLHS();
6720 X = SecondBinOp->getLHS();
6721 E = SecondBinOp->getRHS();
6722 UE = nullptr;
6723 IsXLHSInRHSPart = false;
6724 IsPostfixUpdate = true;
6725 } else {
6726 ErrorFound = NotASpecificExpression;
6727 ErrorLoc = FirstBinOp->getExprLoc();
6728 ErrorRange = FirstBinOp->getSourceRange();
6729 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6730 NoteRange = SecondBinOp->getRHS()->getSourceRange();
6731 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006732 }
6733 }
6734 }
6735 }
6736 } else {
6737 NoteLoc = ErrorLoc = Body->getLocStart();
6738 NoteRange = ErrorRange =
6739 SourceRange(Body->getLocStart(), Body->getLocStart());
6740 ErrorFound = NotTwoSubstatements;
6741 }
6742 } else {
6743 NoteLoc = ErrorLoc = Body->getLocStart();
6744 NoteRange = ErrorRange =
6745 SourceRange(Body->getLocStart(), Body->getLocStart());
6746 ErrorFound = NotACompoundStatement;
6747 }
6748 if (ErrorFound != NoError) {
6749 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6750 << ErrorRange;
6751 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6752 return StmtError();
6753 } else if (CurContext->isDependentContext()) {
6754 UE = V = E = X = nullptr;
6755 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006756 }
Alexey Bataevdea47612014-07-23 07:46:59 +00006757 }
Alexey Bataev0162e452014-07-22 10:10:35 +00006758
6759 getCurFunction()->setHasBranchProtectedScope();
6760
Alexey Bataev62cec442014-11-18 10:14:22 +00006761 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00006762 X, V, E, UE, IsXLHSInRHSPart,
6763 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00006764}
6765
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006766StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6767 Stmt *AStmt,
6768 SourceLocation StartLoc,
6769 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006770 if (!AStmt)
6771 return StmtError();
6772
Samuel Antao4af1b7b2015-12-02 17:44:43 +00006773 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6774 // 1.2.2 OpenMP Language Terminology
6775 // Structured block - An executable statement with a single entry at the
6776 // top and a single exit at the bottom.
6777 // The point of exit cannot be a branch out of the structured block.
6778 // longjmp() and throw() must not violate the entry/exit criteria.
6779 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006780
Alexey Bataev13314bf2014-10-09 04:18:56 +00006781 // OpenMP [2.16, Nesting of Regions]
6782 // If specified, a teams construct must be contained within a target
6783 // construct. That target construct must contain no statements or directives
6784 // outside of the teams construct.
6785 if (DSAStack->hasInnerTeamsRegion()) {
6786 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
6787 bool OMPTeamsFound = true;
6788 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
6789 auto I = CS->body_begin();
6790 while (I != CS->body_end()) {
6791 auto OED = dyn_cast<OMPExecutableDirective>(*I);
6792 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6793 OMPTeamsFound = false;
6794 break;
6795 }
6796 ++I;
6797 }
6798 assert(I != CS->body_end() && "Not found statement");
6799 S = *I;
Kelvin Li3834dce2016-06-27 19:15:43 +00006800 } else {
6801 auto *OED = dyn_cast<OMPExecutableDirective>(S);
6802 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
Alexey Bataev13314bf2014-10-09 04:18:56 +00006803 }
6804 if (!OMPTeamsFound) {
6805 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6806 Diag(DSAStack->getInnerTeamsRegionLoc(),
6807 diag::note_omp_nested_teams_construct_here);
6808 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
6809 << isa<OMPExecutableDirective>(S);
6810 return StmtError();
6811 }
6812 }
6813
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006814 getCurFunction()->setHasBranchProtectedScope();
6815
6816 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6817}
6818
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006819StmtResult
6820Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6821 Stmt *AStmt, SourceLocation StartLoc,
6822 SourceLocation EndLoc) {
6823 if (!AStmt)
6824 return StmtError();
6825
6826 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6827 // 1.2.2 OpenMP Language Terminology
6828 // Structured block - An executable statement with a single entry at the
6829 // top and a single exit at the bottom.
6830 // The point of exit cannot be a branch out of the structured block.
6831 // longjmp() and throw() must not violate the entry/exit criteria.
6832 CS->getCapturedDecl()->setNothrow();
6833
6834 getCurFunction()->setHasBranchProtectedScope();
6835
6836 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6837 AStmt);
6838}
6839
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006840StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6841 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6842 SourceLocation EndLoc,
6843 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6844 if (!AStmt)
6845 return StmtError();
6846
6847 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6848 // 1.2.2 OpenMP Language Terminology
6849 // Structured block - An executable statement with a single entry at the
6850 // top and a single exit at the bottom.
6851 // The point of exit cannot be a branch out of the structured block.
6852 // longjmp() and throw() must not violate the entry/exit criteria.
6853 CS->getCapturedDecl()->setNothrow();
6854
6855 OMPLoopDirective::HelperExprs B;
6856 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6857 // define the nested loops number.
6858 unsigned NestedLoopCount =
6859 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
6860 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6861 VarsWithImplicitDSA, B);
6862 if (NestedLoopCount == 0)
6863 return StmtError();
6864
6865 assert((CurContext->isDependentContext() || B.builtAll()) &&
6866 "omp target parallel for loop exprs were not built");
6867
6868 if (!CurContext->isDependentContext()) {
6869 // Finalize the clauses that need pre-built expressions for CodeGen.
6870 for (auto C : Clauses) {
6871 if (auto LC = dyn_cast<OMPLinearClause>(C))
6872 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006873 B.NumIterations, *this, CurScope,
6874 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006875 return StmtError();
6876 }
6877 }
6878
6879 getCurFunction()->setHasBranchProtectedScope();
6880 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
6881 NestedLoopCount, Clauses, AStmt,
6882 B, DSAStack->isCancelRegion());
6883}
6884
Samuel Antaodf67fc42016-01-19 19:15:56 +00006885/// \brief Check for existence of a map clause in the list of clauses.
6886static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
6887 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
6888 I != E; ++I) {
6889 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
6890 return true;
6891 }
6892 }
6893
6894 return false;
6895}
6896
Michael Wong65f367f2015-07-21 13:44:28 +00006897StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6898 Stmt *AStmt,
6899 SourceLocation StartLoc,
6900 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006901 if (!AStmt)
6902 return StmtError();
6903
6904 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6905
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006906 // OpenMP [2.10.1, Restrictions, p. 97]
6907 // At least one map clause must appear on the directive.
6908 if (!HasMapClause(Clauses)) {
6909 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
6910 getOpenMPDirectiveName(OMPD_target_data);
6911 return StmtError();
6912 }
6913
Michael Wong65f367f2015-07-21 13:44:28 +00006914 getCurFunction()->setHasBranchProtectedScope();
6915
6916 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6917 AStmt);
6918}
6919
Samuel Antaodf67fc42016-01-19 19:15:56 +00006920StmtResult
6921Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
6922 SourceLocation StartLoc,
6923 SourceLocation EndLoc) {
6924 // OpenMP [2.10.2, Restrictions, p. 99]
6925 // At least one map clause must appear on the directive.
6926 if (!HasMapClause(Clauses)) {
6927 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6928 << getOpenMPDirectiveName(OMPD_target_enter_data);
6929 return StmtError();
6930 }
6931
6932 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
6933 Clauses);
6934}
6935
Samuel Antao72590762016-01-19 20:04:50 +00006936StmtResult
6937Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6938 SourceLocation StartLoc,
6939 SourceLocation EndLoc) {
6940 // OpenMP [2.10.3, Restrictions, p. 102]
6941 // At least one map clause must appear on the directive.
6942 if (!HasMapClause(Clauses)) {
6943 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6944 << getOpenMPDirectiveName(OMPD_target_exit_data);
6945 return StmtError();
6946 }
6947
6948 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
6949}
6950
Samuel Antao686c70c2016-05-26 17:30:50 +00006951StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
6952 SourceLocation StartLoc,
6953 SourceLocation EndLoc) {
Samuel Antao686c70c2016-05-26 17:30:50 +00006954 bool seenMotionClause = false;
Samuel Antao661c0902016-05-26 17:39:58 +00006955 for (auto *C : Clauses) {
Samuel Antaoec172c62016-05-26 17:49:04 +00006956 if (C->getClauseKind() == OMPC_to || C->getClauseKind() == OMPC_from)
Samuel Antao661c0902016-05-26 17:39:58 +00006957 seenMotionClause = true;
6958 }
Samuel Antao686c70c2016-05-26 17:30:50 +00006959 if (!seenMotionClause) {
6960 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
6961 return StmtError();
6962 }
6963 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses);
6964}
6965
Alexey Bataev13314bf2014-10-09 04:18:56 +00006966StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6967 Stmt *AStmt, SourceLocation StartLoc,
6968 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006969 if (!AStmt)
6970 return StmtError();
6971
Alexey Bataev13314bf2014-10-09 04:18:56 +00006972 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6973 // 1.2.2 OpenMP Language Terminology
6974 // Structured block - An executable statement with a single entry at the
6975 // top and a single exit at the bottom.
6976 // The point of exit cannot be a branch out of the structured block.
6977 // longjmp() and throw() must not violate the entry/exit criteria.
6978 CS->getCapturedDecl()->setNothrow();
6979
6980 getCurFunction()->setHasBranchProtectedScope();
6981
6982 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6983}
6984
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006985StmtResult
6986Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6987 SourceLocation EndLoc,
6988 OpenMPDirectiveKind CancelRegion) {
6989 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6990 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6991 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6992 << getOpenMPDirectiveName(CancelRegion);
6993 return StmtError();
6994 }
6995 if (DSAStack->isParentNowaitRegion()) {
6996 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6997 return StmtError();
6998 }
6999 if (DSAStack->isParentOrderedRegion()) {
7000 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
7001 return StmtError();
7002 }
7003 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
7004 CancelRegion);
7005}
7006
Alexey Bataev87933c72015-09-18 08:07:34 +00007007StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
7008 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00007009 SourceLocation EndLoc,
7010 OpenMPDirectiveKind CancelRegion) {
7011 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
7012 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
7013 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
7014 << getOpenMPDirectiveName(CancelRegion);
7015 return StmtError();
7016 }
7017 if (DSAStack->isParentNowaitRegion()) {
7018 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
7019 return StmtError();
7020 }
7021 if (DSAStack->isParentOrderedRegion()) {
7022 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
7023 return StmtError();
7024 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00007025 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00007026 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
7027 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00007028}
7029
Alexey Bataev382967a2015-12-08 12:06:20 +00007030static bool checkGrainsizeNumTasksClauses(Sema &S,
7031 ArrayRef<OMPClause *> Clauses) {
7032 OMPClause *PrevClause = nullptr;
7033 bool ErrorFound = false;
7034 for (auto *C : Clauses) {
7035 if (C->getClauseKind() == OMPC_grainsize ||
7036 C->getClauseKind() == OMPC_num_tasks) {
7037 if (!PrevClause)
7038 PrevClause = C;
7039 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
7040 S.Diag(C->getLocStart(),
7041 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
7042 << getOpenMPClauseName(C->getClauseKind())
7043 << getOpenMPClauseName(PrevClause->getClauseKind());
7044 S.Diag(PrevClause->getLocStart(),
7045 diag::note_omp_previous_grainsize_num_tasks)
7046 << getOpenMPClauseName(PrevClause->getClauseKind());
7047 ErrorFound = true;
7048 }
7049 }
7050 }
7051 return ErrorFound;
7052}
7053
Alexey Bataev49f6e782015-12-01 04:18:41 +00007054StmtResult Sema::ActOnOpenMPTaskLoopDirective(
7055 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7056 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007057 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00007058 if (!AStmt)
7059 return StmtError();
7060
7061 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7062 OMPLoopDirective::HelperExprs B;
7063 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7064 // define the nested loops number.
7065 unsigned NestedLoopCount =
7066 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007067 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00007068 VarsWithImplicitDSA, B);
7069 if (NestedLoopCount == 0)
7070 return StmtError();
7071
7072 assert((CurContext->isDependentContext() || B.builtAll()) &&
7073 "omp for loop exprs were not built");
7074
Alexey Bataev382967a2015-12-08 12:06:20 +00007075 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7076 // The grainsize clause and num_tasks clause are mutually exclusive and may
7077 // not appear on the same taskloop directive.
7078 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7079 return StmtError();
7080
Alexey Bataev49f6e782015-12-01 04:18:41 +00007081 getCurFunction()->setHasBranchProtectedScope();
7082 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
7083 NestedLoopCount, Clauses, AStmt, B);
7084}
7085
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007086StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
7087 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7088 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007089 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007090 if (!AStmt)
7091 return StmtError();
7092
7093 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7094 OMPLoopDirective::HelperExprs B;
7095 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7096 // define the nested loops number.
7097 unsigned NestedLoopCount =
7098 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
7099 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
7100 VarsWithImplicitDSA, B);
7101 if (NestedLoopCount == 0)
7102 return StmtError();
7103
7104 assert((CurContext->isDependentContext() || B.builtAll()) &&
7105 "omp for loop exprs were not built");
7106
Alexey Bataev5a3af132016-03-29 08:58:54 +00007107 if (!CurContext->isDependentContext()) {
7108 // Finalize the clauses that need pre-built expressions for CodeGen.
7109 for (auto C : Clauses) {
7110 if (auto LC = dyn_cast<OMPLinearClause>(C))
7111 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007112 B.NumIterations, *this, CurScope,
7113 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007114 return StmtError();
7115 }
7116 }
7117
Alexey Bataev382967a2015-12-08 12:06:20 +00007118 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7119 // The grainsize clause and num_tasks clause are mutually exclusive and may
7120 // not appear on the same taskloop directive.
7121 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7122 return StmtError();
7123
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007124 getCurFunction()->setHasBranchProtectedScope();
7125 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
7126 NestedLoopCount, Clauses, AStmt, B);
7127}
7128
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007129StmtResult Sema::ActOnOpenMPDistributeDirective(
7130 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7131 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007132 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007133 if (!AStmt)
7134 return StmtError();
7135
7136 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7137 OMPLoopDirective::HelperExprs B;
7138 // In presence of clause 'collapse' with number of loops, it will
7139 // define the nested loops number.
7140 unsigned NestedLoopCount =
7141 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
7142 nullptr /*ordered not a clause on distribute*/, AStmt,
7143 *this, *DSAStack, VarsWithImplicitDSA, B);
7144 if (NestedLoopCount == 0)
7145 return StmtError();
7146
7147 assert((CurContext->isDependentContext() || B.builtAll()) &&
7148 "omp for loop exprs were not built");
7149
7150 getCurFunction()->setHasBranchProtectedScope();
7151 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
7152 NestedLoopCount, Clauses, AStmt, B);
7153}
7154
Carlo Bertolli9925f152016-06-27 14:55:37 +00007155StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
7156 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7157 SourceLocation EndLoc,
7158 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7159 if (!AStmt)
7160 return StmtError();
7161
7162 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7163 // 1.2.2 OpenMP Language Terminology
7164 // Structured block - An executable statement with a single entry at the
7165 // top and a single exit at the bottom.
7166 // The point of exit cannot be a branch out of the structured block.
7167 // longjmp() and throw() must not violate the entry/exit criteria.
7168 CS->getCapturedDecl()->setNothrow();
7169
7170 OMPLoopDirective::HelperExprs B;
7171 // In presence of clause 'collapse' with number of loops, it will
7172 // define the nested loops number.
7173 unsigned NestedLoopCount = CheckOpenMPLoop(
7174 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
7175 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
7176 VarsWithImplicitDSA, B);
7177 if (NestedLoopCount == 0)
7178 return StmtError();
7179
7180 assert((CurContext->isDependentContext() || B.builtAll()) &&
7181 "omp for loop exprs were not built");
7182
7183 getCurFunction()->setHasBranchProtectedScope();
7184 return OMPDistributeParallelForDirective::Create(
7185 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7186}
7187
Kelvin Li4a39add2016-07-05 05:00:15 +00007188StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
7189 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7190 SourceLocation EndLoc,
7191 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7192 if (!AStmt)
7193 return StmtError();
7194
7195 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7196 // 1.2.2 OpenMP Language Terminology
7197 // Structured block - An executable statement with a single entry at the
7198 // top and a single exit at the bottom.
7199 // The point of exit cannot be a branch out of the structured block.
7200 // longjmp() and throw() must not violate the entry/exit criteria.
7201 CS->getCapturedDecl()->setNothrow();
7202
7203 OMPLoopDirective::HelperExprs B;
7204 // In presence of clause 'collapse' with number of loops, it will
7205 // define the nested loops number.
7206 unsigned NestedLoopCount = CheckOpenMPLoop(
7207 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
7208 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
7209 VarsWithImplicitDSA, B);
7210 if (NestedLoopCount == 0)
7211 return StmtError();
7212
7213 assert((CurContext->isDependentContext() || B.builtAll()) &&
7214 "omp for loop exprs were not built");
7215
Kelvin Lic5609492016-07-15 04:39:07 +00007216 if (checkSimdlenSafelenSpecified(*this, Clauses))
7217 return StmtError();
7218
Kelvin Li4a39add2016-07-05 05:00:15 +00007219 getCurFunction()->setHasBranchProtectedScope();
7220 return OMPDistributeParallelForSimdDirective::Create(
7221 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7222}
7223
Kelvin Li787f3fc2016-07-06 04:45:38 +00007224StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
7225 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7226 SourceLocation EndLoc,
7227 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7228 if (!AStmt)
7229 return StmtError();
7230
7231 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7232 // 1.2.2 OpenMP Language Terminology
7233 // Structured block - An executable statement with a single entry at the
7234 // top and a single exit at the bottom.
7235 // The point of exit cannot be a branch out of the structured block.
7236 // longjmp() and throw() must not violate the entry/exit criteria.
7237 CS->getCapturedDecl()->setNothrow();
7238
7239 OMPLoopDirective::HelperExprs B;
7240 // In presence of clause 'collapse' with number of loops, it will
7241 // define the nested loops number.
7242 unsigned NestedLoopCount =
7243 CheckOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
7244 nullptr /*ordered not a clause on distribute*/, AStmt,
7245 *this, *DSAStack, VarsWithImplicitDSA, B);
7246 if (NestedLoopCount == 0)
7247 return StmtError();
7248
7249 assert((CurContext->isDependentContext() || B.builtAll()) &&
7250 "omp for loop exprs were not built");
7251
Kelvin Lic5609492016-07-15 04:39:07 +00007252 if (checkSimdlenSafelenSpecified(*this, Clauses))
7253 return StmtError();
7254
Kelvin Li787f3fc2016-07-06 04:45:38 +00007255 getCurFunction()->setHasBranchProtectedScope();
7256 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
7257 NestedLoopCount, Clauses, AStmt, B);
7258}
7259
Kelvin Lia579b912016-07-14 02:54:56 +00007260StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
7261 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7262 SourceLocation EndLoc,
7263 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7264 if (!AStmt)
7265 return StmtError();
7266
7267 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7268 // 1.2.2 OpenMP Language Terminology
7269 // Structured block - An executable statement with a single entry at the
7270 // top and a single exit at the bottom.
7271 // The point of exit cannot be a branch out of the structured block.
7272 // longjmp() and throw() must not violate the entry/exit criteria.
7273 CS->getCapturedDecl()->setNothrow();
7274
7275 OMPLoopDirective::HelperExprs B;
7276 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7277 // define the nested loops number.
7278 unsigned NestedLoopCount = CheckOpenMPLoop(
7279 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
7280 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
7281 VarsWithImplicitDSA, B);
7282 if (NestedLoopCount == 0)
7283 return StmtError();
7284
7285 assert((CurContext->isDependentContext() || B.builtAll()) &&
7286 "omp target parallel for simd loop exprs were not built");
7287
7288 if (!CurContext->isDependentContext()) {
7289 // Finalize the clauses that need pre-built expressions for CodeGen.
7290 for (auto C : Clauses) {
7291 if (auto LC = dyn_cast<OMPLinearClause>(C))
7292 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7293 B.NumIterations, *this, CurScope,
7294 DSAStack))
7295 return StmtError();
7296 }
7297 }
Kelvin Lic5609492016-07-15 04:39:07 +00007298 if (checkSimdlenSafelenSpecified(*this, Clauses))
Kelvin Lia579b912016-07-14 02:54:56 +00007299 return StmtError();
7300
7301 getCurFunction()->setHasBranchProtectedScope();
7302 return OMPTargetParallelForSimdDirective::Create(
7303 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7304}
7305
Kelvin Li986330c2016-07-20 22:57:10 +00007306StmtResult Sema::ActOnOpenMPTargetSimdDirective(
7307 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7308 SourceLocation EndLoc,
7309 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7310 if (!AStmt)
7311 return StmtError();
7312
7313 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7314 // 1.2.2 OpenMP Language Terminology
7315 // Structured block - An executable statement with a single entry at the
7316 // top and a single exit at the bottom.
7317 // The point of exit cannot be a branch out of the structured block.
7318 // longjmp() and throw() must not violate the entry/exit criteria.
7319 CS->getCapturedDecl()->setNothrow();
7320
7321 OMPLoopDirective::HelperExprs B;
7322 // In presence of clause 'collapse' with number of loops, it will define the
7323 // nested loops number.
7324 unsigned NestedLoopCount =
7325 CheckOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
7326 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
7327 VarsWithImplicitDSA, B);
7328 if (NestedLoopCount == 0)
7329 return StmtError();
7330
7331 assert((CurContext->isDependentContext() || B.builtAll()) &&
7332 "omp target simd loop exprs were not built");
7333
7334 if (!CurContext->isDependentContext()) {
7335 // Finalize the clauses that need pre-built expressions for CodeGen.
7336 for (auto C : Clauses) {
7337 if (auto LC = dyn_cast<OMPLinearClause>(C))
7338 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7339 B.NumIterations, *this, CurScope,
7340 DSAStack))
7341 return StmtError();
7342 }
7343 }
7344
7345 if (checkSimdlenSafelenSpecified(*this, Clauses))
7346 return StmtError();
7347
7348 getCurFunction()->setHasBranchProtectedScope();
7349 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
7350 NestedLoopCount, Clauses, AStmt, B);
7351}
7352
Alexey Bataeved09d242014-05-28 05:53:51 +00007353OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007354 SourceLocation StartLoc,
7355 SourceLocation LParenLoc,
7356 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007357 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007358 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00007359 case OMPC_final:
7360 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
7361 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00007362 case OMPC_num_threads:
7363 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
7364 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007365 case OMPC_safelen:
7366 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
7367 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00007368 case OMPC_simdlen:
7369 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
7370 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00007371 case OMPC_collapse:
7372 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
7373 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00007374 case OMPC_ordered:
7375 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
7376 break;
Michael Wonge710d542015-08-07 16:16:36 +00007377 case OMPC_device:
7378 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
7379 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00007380 case OMPC_num_teams:
7381 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
7382 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007383 case OMPC_thread_limit:
7384 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
7385 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00007386 case OMPC_priority:
7387 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
7388 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007389 case OMPC_grainsize:
7390 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
7391 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00007392 case OMPC_num_tasks:
7393 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
7394 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00007395 case OMPC_hint:
7396 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
7397 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007398 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007399 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007400 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007401 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007402 case OMPC_private:
7403 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00007404 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007405 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007406 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00007407 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007408 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007409 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007410 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00007411 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007412 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007413 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007414 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007415 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007416 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007417 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007418 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007419 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007420 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007421 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00007422 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007423 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007424 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00007425 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007426 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007427 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007428 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007429 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007430 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007431 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007432 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007433 case OMPC_is_device_ptr:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007434 llvm_unreachable("Clause is not allowed.");
7435 }
7436 return Res;
7437}
7438
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007439OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
7440 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007441 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007442 SourceLocation NameModifierLoc,
7443 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007444 SourceLocation EndLoc) {
7445 Expr *ValExpr = Condition;
7446 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
7447 !Condition->isInstantiationDependent() &&
7448 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00007449 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007450 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007451 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007452
Richard Smith03a4aa32016-06-23 19:02:52 +00007453 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007454 }
7455
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007456 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
7457 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007458}
7459
Alexey Bataev3778b602014-07-17 07:32:53 +00007460OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
7461 SourceLocation StartLoc,
7462 SourceLocation LParenLoc,
7463 SourceLocation EndLoc) {
7464 Expr *ValExpr = Condition;
7465 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
7466 !Condition->isInstantiationDependent() &&
7467 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00007468 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataev3778b602014-07-17 07:32:53 +00007469 if (Val.isInvalid())
7470 return nullptr;
7471
Richard Smith03a4aa32016-06-23 19:02:52 +00007472 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataev3778b602014-07-17 07:32:53 +00007473 }
7474
7475 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
7476}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00007477ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
7478 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00007479 if (!Op)
7480 return ExprError();
7481
7482 class IntConvertDiagnoser : public ICEConvertDiagnoser {
7483 public:
7484 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00007485 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00007486 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
7487 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007488 return S.Diag(Loc, diag::err_omp_not_integral) << T;
7489 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007490 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
7491 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007492 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
7493 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007494 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
7495 QualType T,
7496 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007497 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
7498 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007499 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
7500 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007501 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00007502 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00007503 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007504 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
7505 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007506 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
7507 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007508 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
7509 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007510 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00007511 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00007512 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007513 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
7514 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007515 llvm_unreachable("conversion functions are permitted");
7516 }
7517 } ConvertDiagnoser;
7518 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
7519}
7520
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007521static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00007522 OpenMPClauseKind CKind,
7523 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007524 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
7525 !ValExpr->isInstantiationDependent()) {
7526 SourceLocation Loc = ValExpr->getExprLoc();
7527 ExprResult Value =
7528 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
7529 if (Value.isInvalid())
7530 return false;
7531
7532 ValExpr = Value.get();
7533 // The expression must evaluate to a non-negative integer value.
7534 llvm::APSInt Result;
7535 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00007536 Result.isSigned() &&
7537 !((!StrictlyPositive && Result.isNonNegative()) ||
7538 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007539 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00007540 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
7541 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007542 return false;
7543 }
7544 }
7545 return true;
7546}
7547
Alexey Bataev568a8332014-03-06 06:15:19 +00007548OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
7549 SourceLocation StartLoc,
7550 SourceLocation LParenLoc,
7551 SourceLocation EndLoc) {
7552 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00007553
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007554 // OpenMP [2.5, Restrictions]
7555 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00007556 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
7557 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007558 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00007559
Alexey Bataeved09d242014-05-28 05:53:51 +00007560 return new (Context)
7561 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00007562}
7563
Alexey Bataev62c87d22014-03-21 04:51:18 +00007564ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007565 OpenMPClauseKind CKind,
7566 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00007567 if (!E)
7568 return ExprError();
7569 if (E->isValueDependent() || E->isTypeDependent() ||
7570 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00007571 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007572 llvm::APSInt Result;
7573 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
7574 if (ICE.isInvalid())
7575 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007576 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
7577 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00007578 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007579 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
7580 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00007581 return ExprError();
7582 }
Alexander Musman09184fe2014-09-30 05:29:28 +00007583 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
7584 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
7585 << E->getSourceRange();
7586 return ExprError();
7587 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007588 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
7589 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00007590 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007591 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00007592 return ICE;
7593}
7594
7595OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
7596 SourceLocation LParenLoc,
7597 SourceLocation EndLoc) {
7598 // OpenMP [2.8.1, simd construct, Description]
7599 // The parameter of the safelen clause must be a constant
7600 // positive integer expression.
7601 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
7602 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007603 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007604 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00007605 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00007606}
7607
Alexey Bataev66b15b52015-08-21 11:14:16 +00007608OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
7609 SourceLocation LParenLoc,
7610 SourceLocation EndLoc) {
7611 // OpenMP [2.8.1, simd construct, Description]
7612 // The parameter of the simdlen clause must be a constant
7613 // positive integer expression.
7614 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
7615 if (Simdlen.isInvalid())
7616 return nullptr;
7617 return new (Context)
7618 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
7619}
7620
Alexander Musman64d33f12014-06-04 07:53:32 +00007621OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
7622 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00007623 SourceLocation LParenLoc,
7624 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00007625 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00007626 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00007627 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00007628 // The parameter of the collapse clause must be a constant
7629 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00007630 ExprResult NumForLoopsResult =
7631 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
7632 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00007633 return nullptr;
7634 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00007635 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00007636}
7637
Alexey Bataev10e775f2015-07-30 11:36:16 +00007638OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
7639 SourceLocation EndLoc,
7640 SourceLocation LParenLoc,
7641 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00007642 // OpenMP [2.7.1, loop construct, Description]
7643 // OpenMP [2.8.1, simd construct, Description]
7644 // OpenMP [2.9.6, distribute construct, Description]
7645 // The parameter of the ordered clause must be a constant
7646 // positive integer expression if any.
7647 if (NumForLoops && LParenLoc.isValid()) {
7648 ExprResult NumForLoopsResult =
7649 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
7650 if (NumForLoopsResult.isInvalid())
7651 return nullptr;
7652 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00007653 } else
7654 NumForLoops = nullptr;
7655 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00007656 return new (Context)
7657 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
7658}
7659
Alexey Bataeved09d242014-05-28 05:53:51 +00007660OMPClause *Sema::ActOnOpenMPSimpleClause(
7661 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
7662 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007663 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007664 switch (Kind) {
7665 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00007666 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00007667 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
7668 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007669 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007670 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00007671 Res = ActOnOpenMPProcBindClause(
7672 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
7673 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007674 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007675 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007676 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00007677 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00007678 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007679 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00007680 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007681 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007682 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007683 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00007684 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00007685 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007686 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00007687 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007688 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007689 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007690 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007691 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007692 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007693 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007694 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007695 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007696 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007697 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007698 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007699 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007700 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007701 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007702 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007703 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007704 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007705 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007706 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007707 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007708 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007709 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007710 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007711 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007712 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007713 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007714 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007715 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007716 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007717 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007718 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007719 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007720 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007721 case OMPC_is_device_ptr:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007722 llvm_unreachable("Clause is not allowed.");
7723 }
7724 return Res;
7725}
7726
Alexey Bataev6402bca2015-12-28 07:25:51 +00007727static std::string
7728getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
7729 ArrayRef<unsigned> Exclude = llvm::None) {
7730 std::string Values;
7731 unsigned Bound = Last >= 2 ? Last - 2 : 0;
7732 unsigned Skipped = Exclude.size();
7733 auto S = Exclude.begin(), E = Exclude.end();
7734 for (unsigned i = First; i < Last; ++i) {
7735 if (std::find(S, E, i) != E) {
7736 --Skipped;
7737 continue;
7738 }
7739 Values += "'";
7740 Values += getOpenMPSimpleClauseTypeName(K, i);
7741 Values += "'";
7742 if (i == Bound - Skipped)
7743 Values += " or ";
7744 else if (i != Bound + 1 - Skipped)
7745 Values += ", ";
7746 }
7747 return Values;
7748}
7749
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007750OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
7751 SourceLocation KindKwLoc,
7752 SourceLocation StartLoc,
7753 SourceLocation LParenLoc,
7754 SourceLocation EndLoc) {
7755 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00007756 static_assert(OMPC_DEFAULT_unknown > 0,
7757 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007758 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00007759 << getListOfPossibleValues(OMPC_default, /*First=*/0,
7760 /*Last=*/OMPC_DEFAULT_unknown)
7761 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007762 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007763 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00007764 switch (Kind) {
7765 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007766 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007767 break;
7768 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007769 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007770 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007771 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007772 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00007773 break;
7774 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007775 return new (Context)
7776 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007777}
7778
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007779OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
7780 SourceLocation KindKwLoc,
7781 SourceLocation StartLoc,
7782 SourceLocation LParenLoc,
7783 SourceLocation EndLoc) {
7784 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007785 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00007786 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
7787 /*Last=*/OMPC_PROC_BIND_unknown)
7788 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007789 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007790 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007791 return new (Context)
7792 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007793}
7794
Alexey Bataev56dafe82014-06-20 07:16:17 +00007795OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007796 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007797 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007798 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007799 SourceLocation EndLoc) {
7800 OMPClause *Res = nullptr;
7801 switch (Kind) {
7802 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00007803 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
7804 assert(Argument.size() == NumberOfElements &&
7805 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007806 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007807 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
7808 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
7809 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
7810 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
7811 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007812 break;
7813 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00007814 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
7815 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
7816 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
7817 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007818 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00007819 case OMPC_dist_schedule:
7820 Res = ActOnOpenMPDistScheduleClause(
7821 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
7822 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
7823 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007824 case OMPC_defaultmap:
7825 enum { Modifier, DefaultmapKind };
7826 Res = ActOnOpenMPDefaultmapClause(
7827 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
7828 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
7829 StartLoc, LParenLoc, ArgumentLoc[Modifier],
7830 ArgumentLoc[DefaultmapKind], EndLoc);
7831 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00007832 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007833 case OMPC_num_threads:
7834 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007835 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007836 case OMPC_collapse:
7837 case OMPC_default:
7838 case OMPC_proc_bind:
7839 case OMPC_private:
7840 case OMPC_firstprivate:
7841 case OMPC_lastprivate:
7842 case OMPC_shared:
7843 case OMPC_reduction:
7844 case OMPC_linear:
7845 case OMPC_aligned:
7846 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007847 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007848 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007849 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007850 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007851 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007852 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007853 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007854 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007855 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007856 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007857 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007858 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007859 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007860 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007861 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007862 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007863 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007864 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007865 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007866 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007867 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007868 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007869 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007870 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007871 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007872 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007873 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007874 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007875 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007876 case OMPC_is_device_ptr:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007877 llvm_unreachable("Clause is not allowed.");
7878 }
7879 return Res;
7880}
7881
Alexey Bataev6402bca2015-12-28 07:25:51 +00007882static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
7883 OpenMPScheduleClauseModifier M2,
7884 SourceLocation M1Loc, SourceLocation M2Loc) {
7885 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
7886 SmallVector<unsigned, 2> Excluded;
7887 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
7888 Excluded.push_back(M2);
7889 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
7890 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
7891 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
7892 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
7893 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
7894 << getListOfPossibleValues(OMPC_schedule,
7895 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
7896 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7897 Excluded)
7898 << getOpenMPClauseName(OMPC_schedule);
7899 return true;
7900 }
7901 return false;
7902}
7903
Alexey Bataev56dafe82014-06-20 07:16:17 +00007904OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007905 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007906 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007907 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
7908 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
7909 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
7910 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
7911 return nullptr;
7912 // OpenMP, 2.7.1, Loop Construct, Restrictions
7913 // Either the monotonic modifier or the nonmonotonic modifier can be specified
7914 // but not both.
7915 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
7916 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
7917 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
7918 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
7919 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
7920 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
7921 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
7922 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
7923 return nullptr;
7924 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007925 if (Kind == OMPC_SCHEDULE_unknown) {
7926 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00007927 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
7928 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
7929 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7930 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7931 Exclude);
7932 } else {
7933 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7934 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007935 }
7936 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
7937 << Values << getOpenMPClauseName(OMPC_schedule);
7938 return nullptr;
7939 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00007940 // OpenMP, 2.7.1, Loop Construct, Restrictions
7941 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
7942 // schedule(guided).
7943 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
7944 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
7945 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
7946 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
7947 diag::err_omp_schedule_nonmonotonic_static);
7948 return nullptr;
7949 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007950 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00007951 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00007952 if (ChunkSize) {
7953 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
7954 !ChunkSize->isInstantiationDependent() &&
7955 !ChunkSize->containsUnexpandedParameterPack()) {
7956 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
7957 ExprResult Val =
7958 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
7959 if (Val.isInvalid())
7960 return nullptr;
7961
7962 ValExpr = Val.get();
7963
7964 // OpenMP [2.7.1, Restrictions]
7965 // chunk_size must be a loop invariant integer expression with a positive
7966 // value.
7967 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00007968 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
7969 if (Result.isSigned() && !Result.isStrictlyPositive()) {
7970 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00007971 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00007972 return nullptr;
7973 }
Alexey Bataevb46cdea2016-06-15 11:20:48 +00007974 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
7975 !CurContext->isDependentContext()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00007976 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
7977 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
7978 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007979 }
7980 }
7981 }
7982
Alexey Bataev6402bca2015-12-28 07:25:51 +00007983 return new (Context)
7984 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00007985 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007986}
7987
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007988OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
7989 SourceLocation StartLoc,
7990 SourceLocation EndLoc) {
7991 OMPClause *Res = nullptr;
7992 switch (Kind) {
7993 case OMPC_ordered:
7994 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
7995 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00007996 case OMPC_nowait:
7997 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
7998 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007999 case OMPC_untied:
8000 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
8001 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008002 case OMPC_mergeable:
8003 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
8004 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008005 case OMPC_read:
8006 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
8007 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00008008 case OMPC_write:
8009 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
8010 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00008011 case OMPC_update:
8012 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
8013 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00008014 case OMPC_capture:
8015 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
8016 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008017 case OMPC_seq_cst:
8018 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
8019 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00008020 case OMPC_threads:
8021 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
8022 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008023 case OMPC_simd:
8024 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
8025 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00008026 case OMPC_nogroup:
8027 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
8028 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008029 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00008030 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008031 case OMPC_num_threads:
8032 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008033 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008034 case OMPC_collapse:
8035 case OMPC_schedule:
8036 case OMPC_private:
8037 case OMPC_firstprivate:
8038 case OMPC_lastprivate:
8039 case OMPC_shared:
8040 case OMPC_reduction:
8041 case OMPC_linear:
8042 case OMPC_aligned:
8043 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008044 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008045 case OMPC_default:
8046 case OMPC_proc_bind:
8047 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00008048 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008049 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00008050 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008051 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00008052 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008053 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00008054 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008055 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00008056 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00008057 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008058 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008059 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008060 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008061 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00008062 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00008063 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00008064 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00008065 case OMPC_is_device_ptr:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008066 llvm_unreachable("Clause is not allowed.");
8067 }
8068 return Res;
8069}
8070
Alexey Bataev236070f2014-06-20 11:19:47 +00008071OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
8072 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00008073 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00008074 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
8075}
8076
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008077OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
8078 SourceLocation EndLoc) {
8079 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
8080}
8081
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008082OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
8083 SourceLocation EndLoc) {
8084 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
8085}
8086
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008087OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
8088 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008089 return new (Context) OMPReadClause(StartLoc, EndLoc);
8090}
8091
Alexey Bataevdea47612014-07-23 07:46:59 +00008092OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
8093 SourceLocation EndLoc) {
8094 return new (Context) OMPWriteClause(StartLoc, EndLoc);
8095}
8096
Alexey Bataev67a4f222014-07-23 10:25:33 +00008097OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
8098 SourceLocation EndLoc) {
8099 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
8100}
8101
Alexey Bataev459dec02014-07-24 06:46:57 +00008102OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
8103 SourceLocation EndLoc) {
8104 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
8105}
8106
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008107OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
8108 SourceLocation EndLoc) {
8109 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
8110}
8111
Alexey Bataev346265e2015-09-25 10:37:12 +00008112OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
8113 SourceLocation EndLoc) {
8114 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
8115}
8116
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008117OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
8118 SourceLocation EndLoc) {
8119 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
8120}
8121
Alexey Bataevb825de12015-12-07 10:51:44 +00008122OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
8123 SourceLocation EndLoc) {
8124 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
8125}
8126
Alexey Bataevc5e02582014-06-16 07:08:35 +00008127OMPClause *Sema::ActOnOpenMPVarListClause(
8128 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
8129 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
8130 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008131 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00008132 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
8133 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
8134 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008135 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008136 switch (Kind) {
8137 case OMPC_private:
8138 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8139 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008140 case OMPC_firstprivate:
8141 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8142 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008143 case OMPC_lastprivate:
8144 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8145 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008146 case OMPC_shared:
8147 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
8148 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008149 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00008150 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
8151 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008152 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00008153 case OMPC_linear:
8154 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00008155 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00008156 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008157 case OMPC_aligned:
8158 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
8159 ColonLoc, EndLoc);
8160 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008161 case OMPC_copyin:
8162 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
8163 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008164 case OMPC_copyprivate:
8165 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8166 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00008167 case OMPC_flush:
8168 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
8169 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008170 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008171 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
8172 StartLoc, LParenLoc, EndLoc);
8173 break;
8174 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00008175 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
8176 DepLinMapLoc, ColonLoc, VarList, StartLoc,
8177 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008178 break;
Samuel Antao661c0902016-05-26 17:39:58 +00008179 case OMPC_to:
8180 Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc);
8181 break;
Samuel Antaoec172c62016-05-26 17:49:04 +00008182 case OMPC_from:
8183 Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc);
8184 break;
Carlo Bertolli2404b172016-07-13 15:37:16 +00008185 case OMPC_use_device_ptr:
8186 Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
8187 break;
Carlo Bertolli70594e92016-07-13 17:16:49 +00008188 case OMPC_is_device_ptr:
8189 Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
8190 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008191 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00008192 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00008193 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00008194 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008195 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00008196 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008197 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008198 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008199 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008200 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00008201 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008202 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008203 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008204 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008205 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008206 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008207 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008208 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008209 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00008210 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00008211 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008212 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00008213 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008214 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00008215 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008216 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00008217 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00008218 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00008219 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008220 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008221 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008222 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008223 case OMPC_uniform:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008224 llvm_unreachable("Clause is not allowed.");
8225 }
8226 return Res;
8227}
8228
Alexey Bataev90c228f2016-02-08 09:29:13 +00008229ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00008230 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00008231 ExprResult Res = BuildDeclRefExpr(
8232 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
8233 if (!Res.isUsable())
8234 return ExprError();
8235 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
8236 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
8237 if (!Res.isUsable())
8238 return ExprError();
8239 }
8240 if (VK != VK_LValue && Res.get()->isGLValue()) {
8241 Res = DefaultLvalueConversion(Res.get());
8242 if (!Res.isUsable())
8243 return ExprError();
8244 }
8245 return Res;
8246}
8247
Alexey Bataev60da77e2016-02-29 05:54:20 +00008248static std::pair<ValueDecl *, bool>
8249getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
8250 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008251 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
8252 RefExpr->containsUnexpandedParameterPack())
8253 return std::make_pair(nullptr, true);
8254
Alexey Bataevd985eda2016-02-10 11:29:16 +00008255 // OpenMP [3.1, C/C++]
8256 // A list item is a variable name.
8257 // OpenMP [2.9.3.3, Restrictions, p.1]
8258 // A variable that is part of another variable (as an array or
8259 // structure element) cannot appear in a private clause.
8260 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008261 enum {
8262 NoArrayExpr = -1,
8263 ArraySubscript = 0,
8264 OMPArraySection = 1
8265 } IsArrayExpr = NoArrayExpr;
8266 if (AllowArraySection) {
8267 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
8268 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
8269 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
8270 Base = TempASE->getBase()->IgnoreParenImpCasts();
8271 RefExpr = Base;
8272 IsArrayExpr = ArraySubscript;
8273 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
8274 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
8275 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
8276 Base = TempOASE->getBase()->IgnoreParenImpCasts();
8277 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
8278 Base = TempASE->getBase()->IgnoreParenImpCasts();
8279 RefExpr = Base;
8280 IsArrayExpr = OMPArraySection;
8281 }
8282 }
8283 ELoc = RefExpr->getExprLoc();
8284 ERange = RefExpr->getSourceRange();
8285 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008286 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
8287 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
8288 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
8289 (S.getCurrentThisType().isNull() || !ME ||
8290 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
8291 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008292 if (IsArrayExpr != NoArrayExpr)
8293 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
8294 << ERange;
8295 else {
8296 S.Diag(ELoc,
8297 AllowArraySection
8298 ? diag::err_omp_expected_var_name_member_expr_or_array_item
8299 : diag::err_omp_expected_var_name_member_expr)
8300 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
8301 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008302 return std::make_pair(nullptr, false);
8303 }
8304 return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
8305}
8306
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008307OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
8308 SourceLocation StartLoc,
8309 SourceLocation LParenLoc,
8310 SourceLocation EndLoc) {
8311 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00008312 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00008313 for (auto &RefExpr : VarList) {
8314 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008315 SourceLocation ELoc;
8316 SourceRange ERange;
8317 Expr *SimpleRefExpr = RefExpr;
8318 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008319 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008320 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008321 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008322 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008323 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008324 ValueDecl *D = Res.first;
8325 if (!D)
8326 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008327
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008328 QualType Type = D->getType();
8329 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008330
8331 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8332 // A variable that appears in a private clause must not have an incomplete
8333 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008334 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008335 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008336 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008337
Alexey Bataev758e55e2013-09-06 18:03:48 +00008338 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8339 // in a Construct]
8340 // Variables with the predetermined data-sharing attributes may not be
8341 // listed in data-sharing attributes clauses, except for the cases
8342 // listed below. For these exceptions only, listing a predetermined
8343 // variable in a data-sharing attribute clause is allowed and overrides
8344 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008345 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008346 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00008347 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8348 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008349 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008350 continue;
8351 }
8352
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008353 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008354 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00008355 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008356 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
8357 << getOpenMPClauseName(OMPC_private) << Type
8358 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8359 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008360 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008361 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008362 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008363 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008364 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008365 continue;
8366 }
8367
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008368 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
8369 // A list item cannot appear in both a map clause and a data-sharing
8370 // attribute clause on the same construct
8371 if (DSAStack->getCurrentDirective() == OMPD_target) {
Samuel Antao6890b092016-07-28 14:25:09 +00008372 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +00008373 if (DSAStack->checkMappableExprComponentListsForDecl(
8374 VD, /* CurrentRegionOnly = */ true,
Samuel Antao6890b092016-07-28 14:25:09 +00008375 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
8376 OpenMPClauseKind WhereFoundClauseKind) -> bool {
8377 ConflictKind = WhereFoundClauseKind;
8378 return true;
8379 })) {
8380 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008381 << getOpenMPClauseName(OMPC_private)
Samuel Antao6890b092016-07-28 14:25:09 +00008382 << getOpenMPClauseName(ConflictKind)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008383 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8384 ReportOriginalDSA(*this, DSAStack, D, DVar);
8385 continue;
8386 }
8387 }
8388
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008389 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
8390 // A variable of class type (or array thereof) that appears in a private
8391 // clause requires an accessible, unambiguous default constructor for the
8392 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00008393 // Generate helper private variable and initialize it with the default
8394 // value. The address of the original variable is replaced by the address of
8395 // the new private variable in CodeGen. This new variable is not added to
8396 // IdResolver, so the code in the OpenMP region uses original variable for
8397 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008398 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008399 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
8400 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008401 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008402 if (VDPrivate->isInvalidDecl())
8403 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008404 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008405 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008406
Alexey Bataev90c228f2016-02-08 09:29:13 +00008407 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008408 if (!VD && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00008409 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00008410 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008411 Vars.push_back((VD || CurContext->isDependentContext())
8412 ? RefExpr->IgnoreParens()
8413 : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008414 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008415 }
8416
Alexey Bataeved09d242014-05-28 05:53:51 +00008417 if (Vars.empty())
8418 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008419
Alexey Bataev03b340a2014-10-21 03:16:40 +00008420 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8421 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008422}
8423
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008424namespace {
8425class DiagsUninitializedSeveretyRAII {
8426private:
8427 DiagnosticsEngine &Diags;
8428 SourceLocation SavedLoc;
8429 bool IsIgnored;
8430
8431public:
8432 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
8433 bool IsIgnored)
8434 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
8435 if (!IsIgnored) {
8436 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
8437 /*Map*/ diag::Severity::Ignored, Loc);
8438 }
8439 }
8440 ~DiagsUninitializedSeveretyRAII() {
8441 if (!IsIgnored)
8442 Diags.popMappings(SavedLoc);
8443 }
8444};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00008445}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008446
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008447OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
8448 SourceLocation StartLoc,
8449 SourceLocation LParenLoc,
8450 SourceLocation EndLoc) {
8451 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008452 SmallVector<Expr *, 8> PrivateCopies;
8453 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00008454 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008455 bool IsImplicitClause =
8456 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
8457 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
8458
Alexey Bataeved09d242014-05-28 05:53:51 +00008459 for (auto &RefExpr : VarList) {
8460 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008461 SourceLocation ELoc;
8462 SourceRange ERange;
8463 Expr *SimpleRefExpr = RefExpr;
8464 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008465 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008466 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008467 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008468 PrivateCopies.push_back(nullptr);
8469 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008470 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008471 ValueDecl *D = Res.first;
8472 if (!D)
8473 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008474
Alexey Bataev60da77e2016-02-29 05:54:20 +00008475 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00008476 QualType Type = D->getType();
8477 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008478
8479 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8480 // A variable that appears in a private clause must not have an incomplete
8481 // type or a reference type.
8482 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00008483 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008484 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008485 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008486
8487 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
8488 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00008489 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008490 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008491 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008492
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008493 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00008494 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008495 if (!IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008496 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev005248a2016-02-25 05:25:57 +00008497 TopDVar = DVar;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008498 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008499 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
8500 // A list item that specifies a given variable may not appear in more
8501 // than one clause on the same directive, except that a variable may be
8502 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008503 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00008504 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008505 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00008506 << getOpenMPClauseName(DVar.CKind)
8507 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008508 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008509 continue;
8510 }
8511
8512 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8513 // in a Construct]
8514 // Variables with the predetermined data-sharing attributes may not be
8515 // listed in data-sharing attributes clauses, except for the cases
8516 // listed below. For these exceptions only, listing a predetermined
8517 // variable in a data-sharing attribute clause is allowed and overrides
8518 // the variable's predetermined data-sharing attributes.
8519 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8520 // in a Construct, C/C++, p.2]
8521 // Variables with const-qualified type having no mutable member may be
8522 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00008523 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008524 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
8525 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00008526 << getOpenMPClauseName(DVar.CKind)
8527 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008528 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008529 continue;
8530 }
8531
Alexey Bataevf29276e2014-06-18 04:14:57 +00008532 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008533 // OpenMP [2.9.3.4, Restrictions, p.2]
8534 // A list item that is private within a parallel region must not appear
8535 // in a firstprivate clause on a worksharing construct if any of the
8536 // worksharing regions arising from the worksharing construct ever bind
8537 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00008538 if (isOpenMPWorksharingDirective(CurrDir) &&
8539 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008540 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008541 if (DVar.CKind != OMPC_shared &&
8542 (isOpenMPParallelDirective(DVar.DKind) ||
8543 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00008544 Diag(ELoc, diag::err_omp_required_access)
8545 << getOpenMPClauseName(OMPC_firstprivate)
8546 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008547 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008548 continue;
8549 }
8550 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008551 // OpenMP [2.9.3.4, Restrictions, p.3]
8552 // A list item that appears in a reduction clause of a parallel construct
8553 // must not appear in a firstprivate clause on a worksharing or task
8554 // construct if any of the worksharing or task regions arising from the
8555 // worksharing or task construct ever bind to any of the parallel regions
8556 // arising from the parallel construct.
8557 // OpenMP [2.9.3.4, Restrictions, p.4]
8558 // A list item that appears in a reduction clause in worksharing
8559 // construct must not appear in a firstprivate clause in a task construct
8560 // encountered during execution of any of the worksharing regions arising
8561 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +00008562 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008563 DVar = DSAStack->hasInnermostDSA(
8564 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
8565 [](OpenMPDirectiveKind K) -> bool {
8566 return isOpenMPParallelDirective(K) ||
8567 isOpenMPWorksharingDirective(K);
8568 },
8569 false);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008570 if (DVar.CKind == OMPC_reduction &&
8571 (isOpenMPParallelDirective(DVar.DKind) ||
8572 isOpenMPWorksharingDirective(DVar.DKind))) {
8573 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
8574 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008575 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008576 continue;
8577 }
8578 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008579
8580 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
8581 // A list item that is private within a teams region must not appear in a
8582 // firstprivate clause on a distribute construct if any of the distribute
8583 // regions arising from the distribute construct ever bind to any of the
8584 // teams regions arising from the teams construct.
8585 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
8586 // A list item that appears in a reduction clause of a teams construct
8587 // must not appear in a firstprivate clause on a distribute construct if
8588 // any of the distribute regions arising from the distribute construct
8589 // ever bind to any of the teams regions arising from the teams construct.
8590 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8591 // A list item may appear in a firstprivate or lastprivate clause but not
8592 // both.
8593 if (CurrDir == OMPD_distribute) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008594 DVar = DSAStack->hasInnermostDSA(
8595 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_private; },
8596 [](OpenMPDirectiveKind K) -> bool {
8597 return isOpenMPTeamsDirective(K);
8598 },
8599 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008600 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
8601 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008602 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008603 continue;
8604 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008605 DVar = DSAStack->hasInnermostDSA(
8606 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
8607 [](OpenMPDirectiveKind K) -> bool {
8608 return isOpenMPTeamsDirective(K);
8609 },
8610 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008611 if (DVar.CKind == OMPC_reduction &&
8612 isOpenMPTeamsDirective(DVar.DKind)) {
8613 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008614 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008615 continue;
8616 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008617 DVar = DSAStack->getTopDSA(D, false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008618 if (DVar.CKind == OMPC_lastprivate) {
8619 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008620 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008621 continue;
8622 }
8623 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008624 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
8625 // A list item cannot appear in both a map clause and a data-sharing
8626 // attribute clause on the same construct
8627 if (CurrDir == OMPD_target) {
Samuel Antao6890b092016-07-28 14:25:09 +00008628 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +00008629 if (DSAStack->checkMappableExprComponentListsForDecl(
8630 VD, /* CurrentRegionOnly = */ true,
Samuel Antao6890b092016-07-28 14:25:09 +00008631 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
8632 OpenMPClauseKind WhereFoundClauseKind) -> bool {
8633 ConflictKind = WhereFoundClauseKind;
8634 return true;
8635 })) {
8636 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008637 << getOpenMPClauseName(OMPC_firstprivate)
Samuel Antao6890b092016-07-28 14:25:09 +00008638 << getOpenMPClauseName(ConflictKind)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008639 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8640 ReportOriginalDSA(*this, DSAStack, D, DVar);
8641 continue;
8642 }
8643 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008644 }
8645
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008646 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008647 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00008648 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008649 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
8650 << getOpenMPClauseName(OMPC_firstprivate) << Type
8651 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8652 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00008653 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008654 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00008655 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008656 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00008657 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008658 continue;
8659 }
8660
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008661 Type = Type.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008662 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
8663 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008664 // Generate helper private variable and initialize it with the value of the
8665 // original variable. The address of the original variable is replaced by
8666 // the address of the new private variable in the CodeGen. This new variable
8667 // is not added to IdResolver, so the code in the OpenMP region uses
8668 // original variable for proper diagnostics and variable capturing.
8669 Expr *VDInitRefExpr = nullptr;
8670 // For arrays generate initializer for single element and replace it by the
8671 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008672 if (Type->isArrayType()) {
8673 auto VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00008674 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008675 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008676 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008677 ElemType = ElemType.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008678 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008679 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00008680 InitializedEntity Entity =
8681 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008682 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
8683
8684 InitializationSequence InitSeq(*this, Entity, Kind, Init);
8685 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
8686 if (Result.isInvalid())
8687 VDPrivate->setInvalidDecl();
8688 else
8689 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008690 // Remove temp variable declaration.
8691 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008692 } else {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008693 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
8694 ".firstprivate.temp");
8695 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
8696 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00008697 AddInitializerToDecl(VDPrivate,
8698 DefaultLvalueConversion(VDInitRefExpr).get(),
8699 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008700 }
8701 if (VDPrivate->isInvalidDecl()) {
8702 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008703 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008704 diag::note_omp_task_predetermined_firstprivate_here);
8705 }
8706 continue;
8707 }
8708 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008709 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00008710 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
8711 RefExpr->getExprLoc());
8712 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008713 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008714 if (TopDVar.CKind == OMPC_lastprivate)
8715 Ref = TopDVar.PrivateCopy;
8716 else {
Alexey Bataev61205072016-03-02 04:57:40 +00008717 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataev005248a2016-02-25 05:25:57 +00008718 if (!IsOpenMPCapturedDecl(D))
8719 ExprCaptures.push_back(Ref->getDecl());
8720 }
Alexey Bataev417089f2016-02-17 13:19:37 +00008721 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008722 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008723 Vars.push_back((VD || CurContext->isDependentContext())
8724 ? RefExpr->IgnoreParens()
8725 : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008726 PrivateCopies.push_back(VDPrivateRefExpr);
8727 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008728 }
8729
Alexey Bataeved09d242014-05-28 05:53:51 +00008730 if (Vars.empty())
8731 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008732
8733 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008734 Vars, PrivateCopies, Inits,
8735 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008736}
8737
Alexander Musman1bb328c2014-06-04 13:06:39 +00008738OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
8739 SourceLocation StartLoc,
8740 SourceLocation LParenLoc,
8741 SourceLocation EndLoc) {
8742 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00008743 SmallVector<Expr *, 8> SrcExprs;
8744 SmallVector<Expr *, 8> DstExprs;
8745 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +00008746 SmallVector<Decl *, 4> ExprCaptures;
8747 SmallVector<Expr *, 4> ExprPostUpdates;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008748 for (auto &RefExpr : VarList) {
8749 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008750 SourceLocation ELoc;
8751 SourceRange ERange;
8752 Expr *SimpleRefExpr = RefExpr;
8753 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008754 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00008755 // It will be analyzed later.
8756 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00008757 SrcExprs.push_back(nullptr);
8758 DstExprs.push_back(nullptr);
8759 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008760 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00008761 ValueDecl *D = Res.first;
8762 if (!D)
8763 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008764
Alexey Bataev74caaf22016-02-20 04:09:36 +00008765 QualType Type = D->getType();
8766 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008767
8768 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
8769 // A variable that appears in a lastprivate clause must not have an
8770 // incomplete type or a reference type.
8771 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +00008772 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +00008773 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008774 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00008775
8776 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
8777 // in a Construct]
8778 // Variables with the predetermined data-sharing attributes may not be
8779 // listed in data-sharing attributes clauses, except for the cases
8780 // listed below.
Alexey Bataev74caaf22016-02-20 04:09:36 +00008781 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008782 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
8783 DVar.CKind != OMPC_firstprivate &&
8784 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
8785 Diag(ELoc, diag::err_omp_wrong_dsa)
8786 << getOpenMPClauseName(DVar.CKind)
8787 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008788 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008789 continue;
8790 }
8791
Alexey Bataevf29276e2014-06-18 04:14:57 +00008792 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8793 // OpenMP [2.14.3.5, Restrictions, p.2]
8794 // A list item that is private within a parallel region, or that appears in
8795 // the reduction clause of a parallel construct, must not appear in a
8796 // lastprivate clause on a worksharing construct if any of the corresponding
8797 // worksharing regions ever binds to any of the corresponding parallel
8798 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00008799 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00008800 if (isOpenMPWorksharingDirective(CurrDir) &&
8801 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +00008802 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008803 if (DVar.CKind != OMPC_shared) {
8804 Diag(ELoc, diag::err_omp_required_access)
8805 << getOpenMPClauseName(OMPC_lastprivate)
8806 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008807 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008808 continue;
8809 }
8810 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00008811
8812 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8813 // A list item may appear in a firstprivate or lastprivate clause but not
8814 // both.
8815 if (CurrDir == OMPD_distribute) {
8816 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8817 if (DVar.CKind == OMPC_firstprivate) {
8818 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
8819 ReportOriginalDSA(*this, DSAStack, D, DVar);
8820 continue;
8821 }
8822 }
8823
Alexander Musman1bb328c2014-06-04 13:06:39 +00008824 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00008825 // A variable of class type (or array thereof) that appears in a
8826 // lastprivate clause requires an accessible, unambiguous default
8827 // constructor for the class type, unless the list item is also specified
8828 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00008829 // A variable of class type (or array thereof) that appears in a
8830 // lastprivate clause requires an accessible, unambiguous copy assignment
8831 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00008832 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008833 auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008834 Type.getUnqualifiedType(), ".lastprivate.src",
Alexey Bataev74caaf22016-02-20 04:09:36 +00008835 D->hasAttrs() ? &D->getAttrs() : nullptr);
8836 auto *PseudoSrcExpr =
8837 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00008838 auto *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008839 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +00008840 D->hasAttrs() ? &D->getAttrs() : nullptr);
8841 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00008842 // For arrays generate assignment operation for single element and replace
8843 // it by the original array element in CodeGen.
Alexey Bataev74caaf22016-02-20 04:09:36 +00008844 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
Alexey Bataev38e89532015-04-16 04:54:05 +00008845 PseudoDstExpr, PseudoSrcExpr);
8846 if (AssignmentOp.isInvalid())
8847 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +00008848 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00008849 /*DiscardedValue=*/true);
8850 if (AssignmentOp.isInvalid())
8851 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008852
Alexey Bataev74caaf22016-02-20 04:09:36 +00008853 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008854 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008855 if (TopDVar.CKind == OMPC_firstprivate)
8856 Ref = TopDVar.PrivateCopy;
8857 else {
Alexey Bataev61205072016-03-02 04:57:40 +00008858 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00008859 if (!IsOpenMPCapturedDecl(D))
8860 ExprCaptures.push_back(Ref->getDecl());
8861 }
8862 if (TopDVar.CKind == OMPC_firstprivate ||
8863 (!IsOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008864 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008865 ExprResult RefRes = DefaultLvalueConversion(Ref);
8866 if (!RefRes.isUsable())
8867 continue;
8868 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008869 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
8870 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008871 if (!PostUpdateRes.isUsable())
8872 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008873 ExprPostUpdates.push_back(
8874 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008875 }
8876 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008877 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008878 Vars.push_back((VD || CurContext->isDependentContext())
8879 ? RefExpr->IgnoreParens()
8880 : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +00008881 SrcExprs.push_back(PseudoSrcExpr);
8882 DstExprs.push_back(PseudoDstExpr);
8883 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00008884 }
8885
8886 if (Vars.empty())
8887 return nullptr;
8888
8889 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +00008890 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008891 buildPreInits(Context, ExprCaptures),
8892 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +00008893}
8894
Alexey Bataev758e55e2013-09-06 18:03:48 +00008895OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
8896 SourceLocation StartLoc,
8897 SourceLocation LParenLoc,
8898 SourceLocation EndLoc) {
8899 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00008900 for (auto &RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008901 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008902 SourceLocation ELoc;
8903 SourceRange ERange;
8904 Expr *SimpleRefExpr = RefExpr;
8905 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008906 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00008907 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008908 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008909 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008910 ValueDecl *D = Res.first;
8911 if (!D)
8912 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008913
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008914 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008915 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8916 // in a Construct]
8917 // Variables with the predetermined data-sharing attributes may not be
8918 // listed in data-sharing attributes clauses, except for the cases
8919 // listed below. For these exceptions only, listing a predetermined
8920 // variable in a data-sharing attribute clause is allowed and overrides
8921 // the variable's predetermined data-sharing attributes.
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008922 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00008923 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
8924 DVar.RefExpr) {
8925 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8926 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008927 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008928 continue;
8929 }
8930
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008931 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008932 if (!VD && IsOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00008933 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008934 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008935 Vars.push_back((VD || !Ref || CurContext->isDependentContext())
8936 ? RefExpr->IgnoreParens()
8937 : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008938 }
8939
Alexey Bataeved09d242014-05-28 05:53:51 +00008940 if (Vars.empty())
8941 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008942
8943 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
8944}
8945
Alexey Bataevc5e02582014-06-16 07:08:35 +00008946namespace {
8947class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
8948 DSAStackTy *Stack;
8949
8950public:
8951 bool VisitDeclRefExpr(DeclRefExpr *E) {
8952 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008953 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008954 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
8955 return false;
8956 if (DVar.CKind != OMPC_unknown)
8957 return true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008958 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
8959 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
8960 false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008961 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00008962 return true;
8963 return false;
8964 }
8965 return false;
8966 }
8967 bool VisitStmt(Stmt *S) {
8968 for (auto Child : S->children()) {
8969 if (Child && Visit(Child))
8970 return true;
8971 }
8972 return false;
8973 }
Alexey Bataev23b69422014-06-18 07:08:49 +00008974 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00008975};
Alexey Bataev23b69422014-06-18 07:08:49 +00008976} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00008977
Alexey Bataev60da77e2016-02-29 05:54:20 +00008978namespace {
8979// Transform MemberExpression for specified FieldDecl of current class to
8980// DeclRefExpr to specified OMPCapturedExprDecl.
8981class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
8982 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
8983 ValueDecl *Field;
8984 DeclRefExpr *CapturedExpr;
8985
8986public:
8987 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
8988 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
8989
8990 ExprResult TransformMemberExpr(MemberExpr *E) {
8991 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
8992 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +00008993 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008994 return CapturedExpr;
8995 }
8996 return BaseTransform::TransformMemberExpr(E);
8997 }
8998 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
8999};
9000} // namespace
9001
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009002template <typename T>
9003static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups,
9004 const llvm::function_ref<T(ValueDecl *)> &Gen) {
9005 for (auto &Set : Lookups) {
9006 for (auto *D : Set) {
9007 if (auto Res = Gen(cast<ValueDecl>(D)))
9008 return Res;
9009 }
9010 }
9011 return T();
9012}
9013
9014static ExprResult
9015buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
9016 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
9017 const DeclarationNameInfo &ReductionId, QualType Ty,
9018 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
9019 if (ReductionIdScopeSpec.isInvalid())
9020 return ExprError();
9021 SmallVector<UnresolvedSet<8>, 4> Lookups;
9022 if (S) {
9023 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
9024 Lookup.suppressDiagnostics();
9025 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
9026 auto *D = Lookup.getRepresentativeDecl();
9027 do {
9028 S = S->getParent();
9029 } while (S && !S->isDeclScope(D));
9030 if (S)
9031 S = S->getParent();
9032 Lookups.push_back(UnresolvedSet<8>());
9033 Lookups.back().append(Lookup.begin(), Lookup.end());
9034 Lookup.clear();
9035 }
9036 } else if (auto *ULE =
9037 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
9038 Lookups.push_back(UnresolvedSet<8>());
9039 Decl *PrevD = nullptr;
9040 for(auto *D : ULE->decls()) {
9041 if (D == PrevD)
9042 Lookups.push_back(UnresolvedSet<8>());
9043 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
9044 Lookups.back().addDecl(DRD);
9045 PrevD = D;
9046 }
9047 }
9048 if (Ty->isDependentType() || Ty->isInstantiationDependentType() ||
9049 Ty->containsUnexpandedParameterPack() ||
9050 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool {
9051 return !D->isInvalidDecl() &&
9052 (D->getType()->isDependentType() ||
9053 D->getType()->isInstantiationDependentType() ||
9054 D->getType()->containsUnexpandedParameterPack());
9055 })) {
9056 UnresolvedSet<8> ResSet;
9057 for (auto &Set : Lookups) {
9058 ResSet.append(Set.begin(), Set.end());
9059 // The last item marks the end of all declarations at the specified scope.
9060 ResSet.addDecl(Set[Set.size() - 1]);
9061 }
9062 return UnresolvedLookupExpr::Create(
9063 SemaRef.Context, /*NamingClass=*/nullptr,
9064 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
9065 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
9066 }
9067 if (auto *VD = filterLookupForUDR<ValueDecl *>(
9068 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
9069 if (!D->isInvalidDecl() &&
9070 SemaRef.Context.hasSameType(D->getType(), Ty))
9071 return D;
9072 return nullptr;
9073 }))
9074 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
9075 if (auto *VD = filterLookupForUDR<ValueDecl *>(
9076 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
9077 if (!D->isInvalidDecl() &&
9078 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
9079 !Ty.isMoreQualifiedThan(D->getType()))
9080 return D;
9081 return nullptr;
9082 })) {
9083 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
9084 /*DetectVirtual=*/false);
9085 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
9086 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
9087 VD->getType().getUnqualifiedType()))) {
9088 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
9089 /*DiagID=*/0) !=
9090 Sema::AR_inaccessible) {
9091 SemaRef.BuildBasePathArray(Paths, BasePath);
9092 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
9093 }
9094 }
9095 }
9096 }
9097 if (ReductionIdScopeSpec.isSet()) {
9098 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
9099 return ExprError();
9100 }
9101 return ExprEmpty();
9102}
9103
Alexey Bataevc5e02582014-06-16 07:08:35 +00009104OMPClause *Sema::ActOnOpenMPReductionClause(
9105 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
9106 SourceLocation ColonLoc, SourceLocation EndLoc,
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009107 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
9108 ArrayRef<Expr *> UnresolvedReductions) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00009109 auto DN = ReductionId.getName();
9110 auto OOK = DN.getCXXOverloadedOperator();
9111 BinaryOperatorKind BOK = BO_Comma;
9112
9113 // OpenMP [2.14.3.6, reduction clause]
9114 // C
9115 // reduction-identifier is either an identifier or one of the following
9116 // operators: +, -, *, &, |, ^, && and ||
9117 // C++
9118 // reduction-identifier is either an id-expression or one of the following
9119 // operators: +, -, *, &, |, ^, && and ||
9120 // FIXME: Only 'min' and 'max' identifiers are supported for now.
9121 switch (OOK) {
9122 case OO_Plus:
9123 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009124 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009125 break;
9126 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009127 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009128 break;
9129 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009130 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009131 break;
9132 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009133 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009134 break;
9135 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009136 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009137 break;
9138 case OO_AmpAmp:
9139 BOK = BO_LAnd;
9140 break;
9141 case OO_PipePipe:
9142 BOK = BO_LOr;
9143 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009144 case OO_New:
9145 case OO_Delete:
9146 case OO_Array_New:
9147 case OO_Array_Delete:
9148 case OO_Slash:
9149 case OO_Percent:
9150 case OO_Tilde:
9151 case OO_Exclaim:
9152 case OO_Equal:
9153 case OO_Less:
9154 case OO_Greater:
9155 case OO_LessEqual:
9156 case OO_GreaterEqual:
9157 case OO_PlusEqual:
9158 case OO_MinusEqual:
9159 case OO_StarEqual:
9160 case OO_SlashEqual:
9161 case OO_PercentEqual:
9162 case OO_CaretEqual:
9163 case OO_AmpEqual:
9164 case OO_PipeEqual:
9165 case OO_LessLess:
9166 case OO_GreaterGreater:
9167 case OO_LessLessEqual:
9168 case OO_GreaterGreaterEqual:
9169 case OO_EqualEqual:
9170 case OO_ExclaimEqual:
9171 case OO_PlusPlus:
9172 case OO_MinusMinus:
9173 case OO_Comma:
9174 case OO_ArrowStar:
9175 case OO_Arrow:
9176 case OO_Call:
9177 case OO_Subscript:
9178 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00009179 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009180 case NUM_OVERLOADED_OPERATORS:
9181 llvm_unreachable("Unexpected reduction identifier");
9182 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00009183 if (auto II = DN.getAsIdentifierInfo()) {
9184 if (II->isStr("max"))
9185 BOK = BO_GT;
9186 else if (II->isStr("min"))
9187 BOK = BO_LT;
9188 }
9189 break;
9190 }
9191 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009192 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +00009193 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009194 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009195
9196 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009197 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009198 SmallVector<Expr *, 8> LHSs;
9199 SmallVector<Expr *, 8> RHSs;
9200 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev61205072016-03-02 04:57:40 +00009201 SmallVector<Decl *, 4> ExprCaptures;
9202 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009203 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
9204 bool FirstIter = true;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009205 for (auto RefExpr : VarList) {
9206 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +00009207 // OpenMP [2.1, C/C++]
9208 // A list item is a variable or array section, subject to the restrictions
9209 // specified in Section 2.4 on page 42 and in each of the sections
9210 // describing clauses and directives for which a list appears.
9211 // OpenMP [2.14.3.3, Restrictions, p.1]
9212 // A variable that is part of another variable (as an array or
9213 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009214 if (!FirstIter && IR != ER)
9215 ++IR;
9216 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009217 SourceLocation ELoc;
9218 SourceRange ERange;
9219 Expr *SimpleRefExpr = RefExpr;
9220 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9221 /*AllowArraySection=*/true);
9222 if (Res.second) {
9223 // It will be analyzed later.
9224 Vars.push_back(RefExpr);
9225 Privates.push_back(nullptr);
9226 LHSs.push_back(nullptr);
9227 RHSs.push_back(nullptr);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009228 // Try to find 'declare reduction' corresponding construct before using
9229 // builtin/overloaded operators.
9230 QualType Type = Context.DependentTy;
9231 CXXCastPath BasePath;
9232 ExprResult DeclareReductionRef = buildDeclareReductionRef(
9233 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
9234 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
9235 if (CurContext->isDependentContext() &&
9236 (DeclareReductionRef.isUnset() ||
9237 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
9238 ReductionOps.push_back(DeclareReductionRef.get());
9239 else
9240 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00009241 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00009242 ValueDecl *D = Res.first;
9243 if (!D)
9244 continue;
9245
Alexey Bataeva1764212015-09-30 09:22:36 +00009246 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009247 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
9248 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
9249 if (ASE)
Alexey Bataev31300ed2016-02-04 11:27:03 +00009250 Type = ASE->getType().getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009251 else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009252 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
9253 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
9254 Type = ATy->getElementType();
9255 else
9256 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +00009257 Type = Type.getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009258 } else
9259 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
9260 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +00009261
Alexey Bataevc5e02582014-06-16 07:08:35 +00009262 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9263 // A variable that appears in a private clause must not have an incomplete
9264 // type or a reference type.
9265 if (RequireCompleteType(ELoc, Type,
9266 diag::err_omp_reduction_incomplete_type))
9267 continue;
9268 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00009269 // A list item that appears in a reduction clause must not be
9270 // const-qualified.
9271 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009272 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00009273 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009274 if (!ASE && !OASE) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009275 bool IsDecl = !VD ||
9276 VD->isThisDeclarationADefinition(Context) ==
9277 VarDecl::DeclarationOnly;
9278 Diag(D->getLocation(),
Alexey Bataeva1764212015-09-30 09:22:36 +00009279 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +00009280 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +00009281 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00009282 continue;
9283 }
9284 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
9285 // If a list-item is a reference type then it must bind to the same object
9286 // for all threads of the team.
Alexey Bataev60da77e2016-02-29 05:54:20 +00009287 if (!ASE && !OASE && VD) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009288 VarDecl *VDDef = VD->getDefinition();
Alexey Bataev31300ed2016-02-04 11:27:03 +00009289 if (VD->getType()->isReferenceType() && VDDef) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009290 DSARefChecker Check(DSAStack);
9291 if (Check.Visit(VDDef->getInit())) {
9292 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
9293 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
9294 continue;
9295 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00009296 }
9297 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009298
Alexey Bataevc5e02582014-06-16 07:08:35 +00009299 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
9300 // in a Construct]
9301 // Variables with the predetermined data-sharing attributes may not be
9302 // listed in data-sharing attributes clauses, except for the cases
9303 // listed below. For these exceptions only, listing a predetermined
9304 // variable in a data-sharing attribute clause is allowed and overrides
9305 // the variable's predetermined data-sharing attributes.
9306 // OpenMP [2.14.3.6, Restrictions, p.3]
9307 // Any number of reduction clauses can be specified on the directive,
9308 // but a list item can appear only once in the reduction clauses for that
9309 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00009310 DSAStackTy::DSAVarData DVar;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009311 DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009312 if (DVar.CKind == OMPC_reduction) {
9313 Diag(ELoc, diag::err_omp_once_referenced)
9314 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009315 if (DVar.RefExpr)
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009316 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009317 } else if (DVar.CKind != OMPC_unknown) {
9318 Diag(ELoc, diag::err_omp_wrong_dsa)
9319 << getOpenMPClauseName(DVar.CKind)
9320 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009321 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009322 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009323 }
9324
9325 // OpenMP [2.14.3.6, Restrictions, p.1]
9326 // A list item that appears in a reduction clause of a worksharing
9327 // construct must be shared in the parallel regions to which any of the
9328 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009329 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
9330 if (isOpenMPWorksharingDirective(CurrDir) &&
9331 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009332 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009333 if (DVar.CKind != OMPC_shared) {
9334 Diag(ELoc, diag::err_omp_required_access)
9335 << getOpenMPClauseName(OMPC_reduction)
9336 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009337 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009338 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00009339 }
9340 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009341
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009342 // Try to find 'declare reduction' corresponding construct before using
9343 // builtin/overloaded operators.
9344 CXXCastPath BasePath;
9345 ExprResult DeclareReductionRef = buildDeclareReductionRef(
9346 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
9347 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
9348 if (DeclareReductionRef.isInvalid())
9349 continue;
9350 if (CurContext->isDependentContext() &&
9351 (DeclareReductionRef.isUnset() ||
9352 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
9353 Vars.push_back(RefExpr);
9354 Privates.push_back(nullptr);
9355 LHSs.push_back(nullptr);
9356 RHSs.push_back(nullptr);
9357 ReductionOps.push_back(DeclareReductionRef.get());
9358 continue;
9359 }
9360 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
9361 // Not allowed reduction identifier is found.
9362 Diag(ReductionId.getLocStart(),
9363 diag::err_omp_unknown_reduction_identifier)
9364 << Type << ReductionIdRange;
9365 continue;
9366 }
9367
9368 // OpenMP [2.14.3.6, reduction clause, Restrictions]
9369 // The type of a list item that appears in a reduction clause must be valid
9370 // for the reduction-identifier. For a max or min reduction in C, the type
9371 // of the list item must be an allowed arithmetic data type: char, int,
9372 // float, double, or _Bool, possibly modified with long, short, signed, or
9373 // unsigned. For a max or min reduction in C++, the type of the list item
9374 // must be an allowed arithmetic data type: char, wchar_t, int, float,
9375 // double, or bool, possibly modified with long, short, signed, or unsigned.
9376 if (DeclareReductionRef.isUnset()) {
9377 if ((BOK == BO_GT || BOK == BO_LT) &&
9378 !(Type->isScalarType() ||
9379 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
9380 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
9381 << getLangOpts().CPlusPlus;
9382 if (!ASE && !OASE) {
9383 bool IsDecl = !VD ||
9384 VD->isThisDeclarationADefinition(Context) ==
9385 VarDecl::DeclarationOnly;
9386 Diag(D->getLocation(),
9387 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9388 << D;
9389 }
9390 continue;
9391 }
9392 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
9393 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
9394 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
9395 if (!ASE && !OASE) {
9396 bool IsDecl = !VD ||
9397 VD->isThisDeclarationADefinition(Context) ==
9398 VarDecl::DeclarationOnly;
9399 Diag(D->getLocation(),
9400 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9401 << D;
9402 }
9403 continue;
9404 }
9405 }
9406
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009407 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009408 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
Alexey Bataev60da77e2016-02-29 05:54:20 +00009409 D->hasAttrs() ? &D->getAttrs() : nullptr);
9410 auto *RHSVD = buildVarDecl(*this, ELoc, Type, D->getName(),
9411 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009412 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00009413 if (OASE ||
Alexey Bataev60da77e2016-02-29 05:54:20 +00009414 (!ASE &&
9415 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Alexey Bataev1189bd02016-01-26 12:20:39 +00009416 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009417 // Create pseudo array type for private copy. The size for this array will
9418 // be generated during codegen.
9419 // For array subscripts or single variables Private Ty is the same as Type
9420 // (type of the variable or single array element).
9421 PrivateTy = Context.getVariableArrayType(
9422 Type, new (Context) OpaqueValueExpr(SourceLocation(),
9423 Context.getSizeType(), VK_RValue),
9424 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +00009425 } else if (!ASE && !OASE &&
9426 Context.getAsArrayType(D->getType().getNonReferenceType()))
9427 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009428 // Private copy.
Alexey Bataev60da77e2016-02-29 05:54:20 +00009429 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, D->getName(),
9430 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009431 // Add initializer for private variable.
9432 Expr *Init = nullptr;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009433 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
9434 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
9435 if (DeclareReductionRef.isUsable()) {
9436 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
9437 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
9438 if (DRD->getInitializer()) {
9439 Init = DRDRef;
9440 RHSVD->setInit(DRDRef);
9441 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009442 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009443 } else {
9444 switch (BOK) {
9445 case BO_Add:
9446 case BO_Xor:
9447 case BO_Or:
9448 case BO_LOr:
9449 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
9450 if (Type->isScalarType() || Type->isAnyComplexType())
9451 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
9452 break;
9453 case BO_Mul:
9454 case BO_LAnd:
9455 if (Type->isScalarType() || Type->isAnyComplexType()) {
9456 // '*' and '&&' reduction ops - initializer is '1'.
9457 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00009458 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009459 break;
9460 case BO_And: {
9461 // '&' reduction op - initializer is '~0'.
9462 QualType OrigType = Type;
9463 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
9464 Type = ComplexTy->getElementType();
9465 if (Type->isRealFloatingType()) {
9466 llvm::APFloat InitValue =
9467 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
9468 /*isIEEE=*/true);
9469 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
9470 Type, ELoc);
9471 } else if (Type->isScalarType()) {
9472 auto Size = Context.getTypeSize(Type);
9473 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
9474 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
9475 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
9476 }
9477 if (Init && OrigType->isAnyComplexType()) {
9478 // Init = 0xFFFF + 0xFFFFi;
9479 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
9480 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
9481 }
9482 Type = OrigType;
9483 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009484 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009485 case BO_LT:
9486 case BO_GT: {
9487 // 'min' reduction op - initializer is 'Largest representable number in
9488 // the reduction list item type'.
9489 // 'max' reduction op - initializer is 'Least representable number in
9490 // the reduction list item type'.
9491 if (Type->isIntegerType() || Type->isPointerType()) {
9492 bool IsSigned = Type->hasSignedIntegerRepresentation();
9493 auto Size = Context.getTypeSize(Type);
9494 QualType IntTy =
9495 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
9496 llvm::APInt InitValue =
9497 (BOK != BO_LT)
9498 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
9499 : llvm::APInt::getMinValue(Size)
9500 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
9501 : llvm::APInt::getMaxValue(Size);
9502 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
9503 if (Type->isPointerType()) {
9504 // Cast to pointer type.
9505 auto CastExpr = BuildCStyleCastExpr(
9506 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
9507 SourceLocation(), Init);
9508 if (CastExpr.isInvalid())
9509 continue;
9510 Init = CastExpr.get();
9511 }
9512 } else if (Type->isRealFloatingType()) {
9513 llvm::APFloat InitValue = llvm::APFloat::getLargest(
9514 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
9515 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
9516 Type, ELoc);
9517 }
9518 break;
9519 }
9520 case BO_PtrMemD:
9521 case BO_PtrMemI:
9522 case BO_MulAssign:
9523 case BO_Div:
9524 case BO_Rem:
9525 case BO_Sub:
9526 case BO_Shl:
9527 case BO_Shr:
9528 case BO_LE:
9529 case BO_GE:
9530 case BO_EQ:
9531 case BO_NE:
9532 case BO_AndAssign:
9533 case BO_XorAssign:
9534 case BO_OrAssign:
9535 case BO_Assign:
9536 case BO_AddAssign:
9537 case BO_SubAssign:
9538 case BO_DivAssign:
9539 case BO_RemAssign:
9540 case BO_ShlAssign:
9541 case BO_ShrAssign:
9542 case BO_Comma:
9543 llvm_unreachable("Unexpected reduction operation");
9544 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009545 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009546 if (Init && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009547 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
9548 /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009549 } else if (!Init)
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009550 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009551 if (RHSVD->isInvalidDecl())
9552 continue;
9553 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009554 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
9555 << ReductionIdRange;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009556 bool IsDecl =
9557 !VD ||
9558 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9559 Diag(D->getLocation(),
9560 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9561 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009562 continue;
9563 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009564 // Store initializer for single element in private copy. Will be used during
9565 // codegen.
9566 PrivateVD->setInit(RHSVD->getInit());
9567 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009568 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009569 ExprResult ReductionOp;
9570 if (DeclareReductionRef.isUsable()) {
9571 QualType RedTy = DeclareReductionRef.get()->getType();
9572 QualType PtrRedTy = Context.getPointerType(RedTy);
9573 ExprResult LHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
9574 ExprResult RHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
9575 if (!BasePath.empty()) {
9576 LHS = DefaultLvalueConversion(LHS.get());
9577 RHS = DefaultLvalueConversion(RHS.get());
9578 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
9579 CK_UncheckedDerivedToBase, LHS.get(),
9580 &BasePath, LHS.get()->getValueKind());
9581 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
9582 CK_UncheckedDerivedToBase, RHS.get(),
9583 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009584 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009585 FunctionProtoType::ExtProtoInfo EPI;
9586 QualType Params[] = {PtrRedTy, PtrRedTy};
9587 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
9588 auto *OVE = new (Context) OpaqueValueExpr(
9589 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
9590 DefaultLvalueConversion(DeclareReductionRef.get()).get());
9591 Expr *Args[] = {LHS.get(), RHS.get()};
9592 ReductionOp = new (Context)
9593 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
9594 } else {
9595 ReductionOp = BuildBinOp(DSAStack->getCurScope(),
9596 ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE);
9597 if (ReductionOp.isUsable()) {
9598 if (BOK != BO_LT && BOK != BO_GT) {
9599 ReductionOp =
9600 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
9601 BO_Assign, LHSDRE, ReductionOp.get());
9602 } else {
9603 auto *ConditionalOp = new (Context) ConditionalOperator(
9604 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
9605 RHSDRE, Type, VK_LValue, OK_Ordinary);
9606 ReductionOp =
9607 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
9608 BO_Assign, LHSDRE, ConditionalOp);
9609 }
9610 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
9611 }
9612 if (ReductionOp.isInvalid())
9613 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009614 }
9615
Alexey Bataev60da77e2016-02-29 05:54:20 +00009616 DeclRefExpr *Ref = nullptr;
9617 Expr *VarsExpr = RefExpr->IgnoreParens();
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009618 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009619 if (ASE || OASE) {
9620 TransformExprToCaptures RebuildToCapture(*this, D);
9621 VarsExpr =
9622 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
9623 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +00009624 } else {
9625 VarsExpr = Ref =
9626 buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +00009627 }
9628 if (!IsOpenMPCapturedDecl(D)) {
9629 ExprCaptures.push_back(Ref->getDecl());
9630 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
9631 ExprResult RefRes = DefaultLvalueConversion(Ref);
9632 if (!RefRes.isUsable())
9633 continue;
9634 ExprResult PostUpdateRes =
9635 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
9636 SimpleRefExpr, RefRes.get());
9637 if (!PostUpdateRes.isUsable())
9638 continue;
9639 ExprPostUpdates.push_back(
9640 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +00009641 }
9642 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00009643 }
9644 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
9645 Vars.push_back(VarsExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009646 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009647 LHSs.push_back(LHSDRE);
9648 RHSs.push_back(RHSDRE);
9649 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009650 }
9651
9652 if (Vars.empty())
9653 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +00009654
Alexey Bataevc5e02582014-06-16 07:08:35 +00009655 return OMPReductionClause::Create(
9656 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009657 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009658 LHSs, RHSs, ReductionOps, buildPreInits(Context, ExprCaptures),
9659 buildPostUpdate(*this, ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +00009660}
9661
Alexey Bataevecba70f2016-04-12 11:02:11 +00009662bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
9663 SourceLocation LinLoc) {
9664 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
9665 LinKind == OMPC_LINEAR_unknown) {
9666 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
9667 return true;
9668 }
9669 return false;
9670}
9671
9672bool Sema::CheckOpenMPLinearDecl(ValueDecl *D, SourceLocation ELoc,
9673 OpenMPLinearClauseKind LinKind,
9674 QualType Type) {
9675 auto *VD = dyn_cast_or_null<VarDecl>(D);
9676 // A variable must not have an incomplete type or a reference type.
9677 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
9678 return true;
9679 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
9680 !Type->isReferenceType()) {
9681 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
9682 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
9683 return true;
9684 }
9685 Type = Type.getNonReferenceType();
9686
9687 // A list item must not be const-qualified.
9688 if (Type.isConstant(Context)) {
9689 Diag(ELoc, diag::err_omp_const_variable)
9690 << getOpenMPClauseName(OMPC_linear);
9691 if (D) {
9692 bool IsDecl =
9693 !VD ||
9694 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9695 Diag(D->getLocation(),
9696 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9697 << D;
9698 }
9699 return true;
9700 }
9701
9702 // A list item must be of integral or pointer type.
9703 Type = Type.getUnqualifiedType().getCanonicalType();
9704 const auto *Ty = Type.getTypePtrOrNull();
9705 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
9706 !Ty->isPointerType())) {
9707 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
9708 if (D) {
9709 bool IsDecl =
9710 !VD ||
9711 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9712 Diag(D->getLocation(),
9713 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9714 << D;
9715 }
9716 return true;
9717 }
9718 return false;
9719}
9720
Alexey Bataev182227b2015-08-20 10:54:39 +00009721OMPClause *Sema::ActOnOpenMPLinearClause(
9722 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
9723 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
9724 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00009725 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009726 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00009727 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +00009728 SmallVector<Decl *, 4> ExprCaptures;
9729 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +00009730 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +00009731 LinKind = OMPC_LINEAR_val;
Alexey Bataeved09d242014-05-28 05:53:51 +00009732 for (auto &RefExpr : VarList) {
9733 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009734 SourceLocation ELoc;
9735 SourceRange ERange;
9736 Expr *SimpleRefExpr = RefExpr;
9737 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9738 /*AllowArraySection=*/false);
9739 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +00009740 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009741 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009742 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00009743 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00009744 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009745 ValueDecl *D = Res.first;
9746 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +00009747 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +00009748
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009749 QualType Type = D->getType();
9750 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +00009751
9752 // OpenMP [2.14.3.7, linear clause]
9753 // A list-item cannot appear in more than one linear clause.
9754 // A list-item that appears in a linear clause cannot appear in any
9755 // other data-sharing attribute clause.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009756 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00009757 if (DVar.RefExpr) {
9758 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9759 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009760 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00009761 continue;
9762 }
9763
Alexey Bataevecba70f2016-04-12 11:02:11 +00009764 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +00009765 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00009766 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +00009767
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009768 // Build private copy of original var.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009769 auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(),
9770 D->hasAttrs() ? &D->getAttrs() : nullptr);
9771 auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +00009772 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009773 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009774 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009775 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009776 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev78849fb2016-03-09 09:49:00 +00009777 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
9778 if (!IsOpenMPCapturedDecl(D)) {
9779 ExprCaptures.push_back(Ref->getDecl());
9780 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
9781 ExprResult RefRes = DefaultLvalueConversion(Ref);
9782 if (!RefRes.isUsable())
9783 continue;
9784 ExprResult PostUpdateRes =
9785 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
9786 SimpleRefExpr, RefRes.get());
9787 if (!PostUpdateRes.isUsable())
9788 continue;
9789 ExprPostUpdates.push_back(
9790 IgnoredValueConversions(PostUpdateRes.get()).get());
9791 }
9792 }
9793 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009794 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009795 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009796 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009797 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009798 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009799 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
9800 auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
9801
9802 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009803 Vars.push_back((VD || CurContext->isDependentContext())
9804 ? RefExpr->IgnoreParens()
9805 : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009806 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00009807 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00009808 }
9809
9810 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009811 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00009812
9813 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00009814 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00009815 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
9816 !Step->isInstantiationDependent() &&
9817 !Step->containsUnexpandedParameterPack()) {
9818 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00009819 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00009820 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009821 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00009822 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00009823
Alexander Musman3276a272015-03-21 10:12:56 +00009824 // Build var to save the step value.
9825 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00009826 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00009827 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00009828 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00009829 ExprResult CalcStep =
9830 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009831 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00009832
Alexander Musman8dba6642014-04-22 13:09:42 +00009833 // Warn about zero linear step (it would be probably better specified as
9834 // making corresponding variables 'const').
9835 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00009836 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
9837 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00009838 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
9839 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00009840 if (!IsConstant && CalcStep.isUsable()) {
9841 // Calculate the step beforehand instead of doing this on each iteration.
9842 // (This is not used if the number of iterations may be kfold-ed).
9843 CalcStepExpr = CalcStep.get();
9844 }
Alexander Musman8dba6642014-04-22 13:09:42 +00009845 }
9846
Alexey Bataev182227b2015-08-20 10:54:39 +00009847 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
9848 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009849 StepExpr, CalcStepExpr,
9850 buildPreInits(Context, ExprCaptures),
9851 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +00009852}
9853
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009854static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
9855 Expr *NumIterations, Sema &SemaRef,
9856 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +00009857 // Walk the vars and build update/final expressions for the CodeGen.
9858 SmallVector<Expr *, 8> Updates;
9859 SmallVector<Expr *, 8> Finals;
9860 Expr *Step = Clause.getStep();
9861 Expr *CalcStep = Clause.getCalcStep();
9862 // OpenMP [2.14.3.7, linear clause]
9863 // If linear-step is not specified it is assumed to be 1.
9864 if (Step == nullptr)
9865 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009866 else if (CalcStep) {
Alexander Musman3276a272015-03-21 10:12:56 +00009867 Step = cast<BinaryOperator>(CalcStep)->getLHS();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009868 }
Alexander Musman3276a272015-03-21 10:12:56 +00009869 bool HasErrors = false;
9870 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009871 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009872 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00009873 for (auto &RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009874 SourceLocation ELoc;
9875 SourceRange ERange;
9876 Expr *SimpleRefExpr = RefExpr;
9877 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange,
9878 /*AllowArraySection=*/false);
9879 ValueDecl *D = Res.first;
9880 if (Res.second || !D) {
9881 Updates.push_back(nullptr);
9882 Finals.push_back(nullptr);
9883 HasErrors = true;
9884 continue;
9885 }
9886 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) {
9887 D = cast<MemberExpr>(CED->getInit()->IgnoreParenImpCasts())
9888 ->getMemberDecl();
9889 }
9890 auto &&Info = Stack->isLoopControlVariable(D);
Alexander Musman3276a272015-03-21 10:12:56 +00009891 Expr *InitExpr = *CurInit;
9892
9893 // Build privatized reference to the current linear var.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009894 auto DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009895 Expr *CapturedRef;
9896 if (LinKind == OMPC_LINEAR_uval)
9897 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
9898 else
9899 CapturedRef =
9900 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
9901 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
9902 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009903
9904 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009905 ExprResult Update;
9906 if (!Info.first) {
9907 Update =
9908 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
9909 InitExpr, IV, Step, /* Subtract */ false);
9910 } else
9911 Update = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009912 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
9913 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009914
9915 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009916 ExprResult Final;
9917 if (!Info.first) {
9918 Final = BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
9919 InitExpr, NumIterations, Step,
9920 /* Subtract */ false);
9921 } else
9922 Final = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009923 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
9924 /*DiscardedValue=*/true);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009925
Alexander Musman3276a272015-03-21 10:12:56 +00009926 if (!Update.isUsable() || !Final.isUsable()) {
9927 Updates.push_back(nullptr);
9928 Finals.push_back(nullptr);
9929 HasErrors = true;
9930 } else {
9931 Updates.push_back(Update.get());
9932 Finals.push_back(Final.get());
9933 }
Richard Trieucc3949d2016-02-18 22:34:54 +00009934 ++CurInit;
9935 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00009936 }
9937 Clause.setUpdates(Updates);
9938 Clause.setFinals(Finals);
9939 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00009940}
9941
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009942OMPClause *Sema::ActOnOpenMPAlignedClause(
9943 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
9944 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
9945
9946 SmallVector<Expr *, 8> Vars;
9947 for (auto &RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +00009948 assert(RefExpr && "NULL expr in OpenMP linear clause.");
9949 SourceLocation ELoc;
9950 SourceRange ERange;
9951 Expr *SimpleRefExpr = RefExpr;
9952 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9953 /*AllowArraySection=*/false);
9954 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009955 // It will be analyzed later.
9956 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009957 }
Alexey Bataev1efd1662016-03-29 10:59:56 +00009958 ValueDecl *D = Res.first;
9959 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009960 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009961
Alexey Bataev1efd1662016-03-29 10:59:56 +00009962 QualType QType = D->getType();
9963 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009964
9965 // OpenMP [2.8.1, simd construct, Restrictions]
9966 // The type of list items appearing in the aligned clause must be
9967 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009968 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009969 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +00009970 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009971 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009972 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009973 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +00009974 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009975 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +00009976 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009977 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009978 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009979 continue;
9980 }
9981
9982 // OpenMP [2.8.1, simd construct, Restrictions]
9983 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev1efd1662016-03-29 10:59:56 +00009984 if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00009985 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009986 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
9987 << getOpenMPClauseName(OMPC_aligned);
9988 continue;
9989 }
9990
Alexey Bataev1efd1662016-03-29 10:59:56 +00009991 DeclRefExpr *Ref = nullptr;
9992 if (!VD && IsOpenMPCapturedDecl(D))
9993 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
9994 Vars.push_back(DefaultFunctionArrayConversion(
9995 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
9996 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009997 }
9998
9999 // OpenMP [2.8.1, simd construct, Description]
10000 // The parameter of the aligned clause, alignment, must be a constant
10001 // positive integer expression.
10002 // If no optional parameter is specified, implementation-defined default
10003 // alignments for SIMD instructions on the target platforms are assumed.
10004 if (Alignment != nullptr) {
10005 ExprResult AlignResult =
10006 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
10007 if (AlignResult.isInvalid())
10008 return nullptr;
10009 Alignment = AlignResult.get();
10010 }
10011 if (Vars.empty())
10012 return nullptr;
10013
10014 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
10015 EndLoc, Vars, Alignment);
10016}
10017
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010018OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
10019 SourceLocation StartLoc,
10020 SourceLocation LParenLoc,
10021 SourceLocation EndLoc) {
10022 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010023 SmallVector<Expr *, 8> SrcExprs;
10024 SmallVector<Expr *, 8> DstExprs;
10025 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +000010026 for (auto &RefExpr : VarList) {
10027 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
10028 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010029 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000010030 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010031 SrcExprs.push_back(nullptr);
10032 DstExprs.push_back(nullptr);
10033 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010034 continue;
10035 }
10036
Alexey Bataeved09d242014-05-28 05:53:51 +000010037 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010038 // OpenMP [2.1, C/C++]
10039 // A list item is a variable name.
10040 // OpenMP [2.14.4.1, Restrictions, p.1]
10041 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +000010042 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010043 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000010044 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
10045 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010046 continue;
10047 }
10048
10049 Decl *D = DE->getDecl();
10050 VarDecl *VD = cast<VarDecl>(D);
10051
10052 QualType Type = VD->getType();
10053 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
10054 // It will be analyzed later.
10055 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010056 SrcExprs.push_back(nullptr);
10057 DstExprs.push_back(nullptr);
10058 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010059 continue;
10060 }
10061
10062 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
10063 // A list item that appears in a copyin clause must be threadprivate.
10064 if (!DSAStack->isThreadPrivate(VD)) {
10065 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +000010066 << getOpenMPClauseName(OMPC_copyin)
10067 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010068 continue;
10069 }
10070
10071 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
10072 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +000010073 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010074 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010075 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000010076 auto *SrcVD =
10077 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
10078 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +000010079 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010080 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
10081 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000010082 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
10083 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010084 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010085 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010086 // For arrays generate assignment operation for single element and replace
10087 // it by the original array element in CodeGen.
10088 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
10089 PseudoDstExpr, PseudoSrcExpr);
10090 if (AssignmentOp.isInvalid())
10091 continue;
10092 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
10093 /*DiscardedValue=*/true);
10094 if (AssignmentOp.isInvalid())
10095 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010096
10097 DSAStack->addDSA(VD, DE, OMPC_copyin);
10098 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010099 SrcExprs.push_back(PseudoSrcExpr);
10100 DstExprs.push_back(PseudoDstExpr);
10101 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010102 }
10103
Alexey Bataeved09d242014-05-28 05:53:51 +000010104 if (Vars.empty())
10105 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010106
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010107 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
10108 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010109}
10110
Alexey Bataevbae9a792014-06-27 10:37:06 +000010111OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
10112 SourceLocation StartLoc,
10113 SourceLocation LParenLoc,
10114 SourceLocation EndLoc) {
10115 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +000010116 SmallVector<Expr *, 8> SrcExprs;
10117 SmallVector<Expr *, 8> DstExprs;
10118 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +000010119 for (auto &RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +000010120 assert(RefExpr && "NULL expr in OpenMP linear clause.");
10121 SourceLocation ELoc;
10122 SourceRange ERange;
10123 Expr *SimpleRefExpr = RefExpr;
10124 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
10125 /*AllowArraySection=*/false);
10126 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000010127 // It will be analyzed later.
10128 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000010129 SrcExprs.push_back(nullptr);
10130 DstExprs.push_back(nullptr);
10131 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010132 }
Alexey Bataeve122da12016-03-17 10:50:17 +000010133 ValueDecl *D = Res.first;
10134 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +000010135 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000010136
Alexey Bataeve122da12016-03-17 10:50:17 +000010137 QualType Type = D->getType();
10138 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010139
10140 // OpenMP [2.14.4.2, Restrictions, p.2]
10141 // A list item that appears in a copyprivate clause may not appear in a
10142 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +000010143 if (!VD || !DSAStack->isThreadPrivate(VD)) {
10144 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +000010145 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
10146 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000010147 Diag(ELoc, diag::err_omp_wrong_dsa)
10148 << getOpenMPClauseName(DVar.CKind)
10149 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve122da12016-03-17 10:50:17 +000010150 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010151 continue;
10152 }
10153
10154 // OpenMP [2.11.4.2, Restrictions, p.1]
10155 // All list items that appear in a copyprivate clause must be either
10156 // threadprivate or private in the enclosing context.
10157 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +000010158 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010159 if (DVar.CKind == OMPC_shared) {
10160 Diag(ELoc, diag::err_omp_required_access)
10161 << getOpenMPClauseName(OMPC_copyprivate)
10162 << "threadprivate or private in the enclosing context";
Alexey Bataeve122da12016-03-17 10:50:17 +000010163 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010164 continue;
10165 }
10166 }
10167 }
10168
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010169 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000010170 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010171 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010172 << getOpenMPClauseName(OMPC_copyprivate) << Type
10173 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010174 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +000010175 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010176 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +000010177 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010178 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +000010179 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010180 continue;
10181 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010182
Alexey Bataevbae9a792014-06-27 10:37:06 +000010183 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
10184 // A variable of class type (or array thereof) that appears in a
10185 // copyin clause requires an accessible, unambiguous copy assignment
10186 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010187 Type = Context.getBaseElementType(Type.getNonReferenceType())
10188 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +000010189 auto *SrcVD =
Alexey Bataeve122da12016-03-17 10:50:17 +000010190 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src",
10191 D->hasAttrs() ? &D->getAttrs() : nullptr);
10192 auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
Alexey Bataev420d45b2015-04-14 05:11:24 +000010193 auto *DstVD =
Alexey Bataeve122da12016-03-17 10:50:17 +000010194 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst",
10195 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +000010196 auto *PseudoDstExpr =
Alexey Bataeve122da12016-03-17 10:50:17 +000010197 buildDeclRefExpr(*this, DstVD, Type, ELoc);
10198 auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
Alexey Bataeva63048e2015-03-23 06:18:07 +000010199 PseudoDstExpr, PseudoSrcExpr);
10200 if (AssignmentOp.isInvalid())
10201 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +000010202 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +000010203 /*DiscardedValue=*/true);
10204 if (AssignmentOp.isInvalid())
10205 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000010206
10207 // No need to mark vars as copyprivate, they are already threadprivate or
10208 // implicitly private.
Alexey Bataeve122da12016-03-17 10:50:17 +000010209 assert(VD || IsOpenMPCapturedDecl(D));
10210 Vars.push_back(
10211 VD ? RefExpr->IgnoreParens()
10212 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +000010213 SrcExprs.push_back(PseudoSrcExpr);
10214 DstExprs.push_back(PseudoDstExpr);
10215 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +000010216 }
10217
10218 if (Vars.empty())
10219 return nullptr;
10220
Alexey Bataeva63048e2015-03-23 06:18:07 +000010221 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10222 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010223}
10224
Alexey Bataev6125da92014-07-21 11:26:11 +000010225OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
10226 SourceLocation StartLoc,
10227 SourceLocation LParenLoc,
10228 SourceLocation EndLoc) {
10229 if (VarList.empty())
10230 return nullptr;
10231
10232 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
10233}
Alexey Bataevdea47612014-07-23 07:46:59 +000010234
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010235OMPClause *
10236Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
10237 SourceLocation DepLoc, SourceLocation ColonLoc,
10238 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
10239 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +000010240 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010241 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +000010242 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000010243 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +000010244 return nullptr;
10245 }
10246 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010247 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
10248 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +000010249 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010250 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000010251 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
10252 /*Last=*/OMPC_DEPEND_unknown, Except)
10253 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010254 return nullptr;
10255 }
10256 SmallVector<Expr *, 8> Vars;
Alexey Bataev8b427062016-05-25 12:36:08 +000010257 DSAStackTy::OperatorOffsetTy OpsOffs;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010258 llvm::APSInt DepCounter(/*BitWidth=*/32);
10259 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
10260 if (DepKind == OMPC_DEPEND_sink) {
10261 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
10262 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
10263 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010264 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010265 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010266 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
10267 DSAStack->getParentOrderedRegionParam()) {
10268 for (auto &RefExpr : VarList) {
10269 assert(RefExpr && "NULL expr in OpenMP shared clause.");
Alexey Bataev8b427062016-05-25 12:36:08 +000010270 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010271 // It will be analyzed later.
10272 Vars.push_back(RefExpr);
10273 continue;
10274 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010275
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010276 SourceLocation ELoc = RefExpr->getExprLoc();
10277 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
10278 if (DepKind == OMPC_DEPEND_sink) {
10279 if (DepCounter >= TotalDepCount) {
10280 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
10281 continue;
10282 }
10283 ++DepCounter;
10284 // OpenMP [2.13.9, Summary]
10285 // depend(dependence-type : vec), where dependence-type is:
10286 // 'sink' and where vec is the iteration vector, which has the form:
10287 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
10288 // where n is the value specified by the ordered clause in the loop
10289 // directive, xi denotes the loop iteration variable of the i-th nested
10290 // loop associated with the loop directive, and di is a constant
10291 // non-negative integer.
Alexey Bataev8b427062016-05-25 12:36:08 +000010292 if (CurContext->isDependentContext()) {
10293 // It will be analyzed later.
10294 Vars.push_back(RefExpr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010295 continue;
10296 }
Alexey Bataev8b427062016-05-25 12:36:08 +000010297 SimpleExpr = SimpleExpr->IgnoreImplicit();
10298 OverloadedOperatorKind OOK = OO_None;
10299 SourceLocation OOLoc;
10300 Expr *LHS = SimpleExpr;
10301 Expr *RHS = nullptr;
10302 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
10303 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
10304 OOLoc = BO->getOperatorLoc();
10305 LHS = BO->getLHS()->IgnoreParenImpCasts();
10306 RHS = BO->getRHS()->IgnoreParenImpCasts();
10307 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
10308 OOK = OCE->getOperator();
10309 OOLoc = OCE->getOperatorLoc();
10310 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
10311 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
10312 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
10313 OOK = MCE->getMethodDecl()
10314 ->getNameInfo()
10315 .getName()
10316 .getCXXOverloadedOperator();
10317 OOLoc = MCE->getCallee()->getExprLoc();
10318 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
10319 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
10320 }
10321 SourceLocation ELoc;
10322 SourceRange ERange;
10323 auto Res = getPrivateItem(*this, LHS, ELoc, ERange,
10324 /*AllowArraySection=*/false);
10325 if (Res.second) {
10326 // It will be analyzed later.
10327 Vars.push_back(RefExpr);
10328 }
10329 ValueDecl *D = Res.first;
10330 if (!D)
10331 continue;
10332
10333 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
10334 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
10335 continue;
10336 }
10337 if (RHS) {
10338 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
10339 RHS, OMPC_depend, /*StrictlyPositive=*/false);
10340 if (RHSRes.isInvalid())
10341 continue;
10342 }
10343 if (!CurContext->isDependentContext() &&
10344 DSAStack->getParentOrderedRegionParam() &&
10345 DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
10346 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
10347 << DSAStack->getParentLoopControlVariable(
10348 DepCounter.getZExtValue());
10349 continue;
10350 }
10351 OpsOffs.push_back({RHS, OOK});
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010352 } else {
10353 // OpenMP [2.11.1.1, Restrictions, p.3]
10354 // A variable that is part of another variable (such as a field of a
10355 // structure) but is not an array element or an array section cannot
10356 // appear in a depend clause.
10357 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
10358 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
10359 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
10360 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
10361 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev31300ed2016-02-04 11:27:03 +000010362 (ASE &&
10363 !ASE->getBase()
10364 ->getType()
10365 .getNonReferenceType()
10366 ->isPointerType() &&
10367 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000010368 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
10369 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010370 continue;
10371 }
10372 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010373 Vars.push_back(RefExpr->IgnoreParenImpCasts());
10374 }
10375
10376 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
10377 TotalDepCount > VarList.size() &&
10378 DSAStack->getParentOrderedRegionParam()) {
10379 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
10380 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
10381 }
10382 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
10383 Vars.empty())
10384 return nullptr;
10385 }
Alexey Bataev8b427062016-05-25 12:36:08 +000010386 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10387 DepKind, DepLoc, ColonLoc, Vars);
10388 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source)
10389 DSAStack->addDoacrossDependClause(C, OpsOffs);
10390 return C;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010391}
Michael Wonge710d542015-08-07 16:16:36 +000010392
10393OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
10394 SourceLocation LParenLoc,
10395 SourceLocation EndLoc) {
10396 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +000010397
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010398 // OpenMP [2.9.1, Restrictions]
10399 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010400 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
10401 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010402 return nullptr;
10403
Michael Wonge710d542015-08-07 16:16:36 +000010404 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10405}
Kelvin Li0bff7af2015-11-23 05:32:03 +000010406
10407static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
10408 DSAStackTy *Stack, CXXRecordDecl *RD) {
10409 if (!RD || RD->isInvalidDecl())
10410 return true;
10411
Alexey Bataevc9bd03d2015-12-17 06:55:08 +000010412 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
10413 if (auto *CTD = CTSD->getSpecializedTemplate())
10414 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010415 auto QTy = SemaRef.Context.getRecordType(RD);
10416 if (RD->isDynamicClass()) {
10417 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10418 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
10419 return false;
10420 }
10421 auto *DC = RD;
10422 bool IsCorrect = true;
10423 for (auto *I : DC->decls()) {
10424 if (I) {
10425 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
10426 if (MD->isStatic()) {
10427 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10428 SemaRef.Diag(MD->getLocation(),
10429 diag::note_omp_static_member_in_target);
10430 IsCorrect = false;
10431 }
10432 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
10433 if (VD->isStaticDataMember()) {
10434 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10435 SemaRef.Diag(VD->getLocation(),
10436 diag::note_omp_static_member_in_target);
10437 IsCorrect = false;
10438 }
10439 }
10440 }
10441 }
10442
10443 for (auto &I : RD->bases()) {
10444 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
10445 I.getType()->getAsCXXRecordDecl()))
10446 IsCorrect = false;
10447 }
10448 return IsCorrect;
10449}
10450
10451static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
10452 DSAStackTy *Stack, QualType QTy) {
10453 NamedDecl *ND;
10454 if (QTy->isIncompleteType(&ND)) {
10455 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
10456 return false;
10457 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
10458 if (!RD->isInvalidDecl() &&
10459 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
10460 return false;
10461 }
10462 return true;
10463}
10464
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010465/// \brief Return true if it can be proven that the provided array expression
10466/// (array section or array subscript) does NOT specify the whole size of the
10467/// array whose base type is \a BaseQTy.
10468static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
10469 const Expr *E,
10470 QualType BaseQTy) {
10471 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
10472
10473 // If this is an array subscript, it refers to the whole size if the size of
10474 // the dimension is constant and equals 1. Also, an array section assumes the
10475 // format of an array subscript if no colon is used.
10476 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
10477 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
10478 return ATy->getSize().getSExtValue() != 1;
10479 // Size can't be evaluated statically.
10480 return false;
10481 }
10482
10483 assert(OASE && "Expecting array section if not an array subscript.");
10484 auto *LowerBound = OASE->getLowerBound();
10485 auto *Length = OASE->getLength();
10486
10487 // If there is a lower bound that does not evaluates to zero, we are not
10488 // convering the whole dimension.
10489 if (LowerBound) {
10490 llvm::APSInt ConstLowerBound;
10491 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
10492 return false; // Can't get the integer value as a constant.
10493 if (ConstLowerBound.getSExtValue())
10494 return true;
10495 }
10496
10497 // If we don't have a length we covering the whole dimension.
10498 if (!Length)
10499 return false;
10500
10501 // If the base is a pointer, we don't have a way to get the size of the
10502 // pointee.
10503 if (BaseQTy->isPointerType())
10504 return false;
10505
10506 // We can only check if the length is the same as the size of the dimension
10507 // if we have a constant array.
10508 auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
10509 if (!CATy)
10510 return false;
10511
10512 llvm::APSInt ConstLength;
10513 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
10514 return false; // Can't get the integer value as a constant.
10515
10516 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
10517}
10518
10519// Return true if it can be proven that the provided array expression (array
10520// section or array subscript) does NOT specify a single element of the array
10521// whose base type is \a BaseQTy.
10522static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
10523 const Expr *E,
10524 QualType BaseQTy) {
10525 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
10526
10527 // An array subscript always refer to a single element. Also, an array section
10528 // assumes the format of an array subscript if no colon is used.
10529 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
10530 return false;
10531
10532 assert(OASE && "Expecting array section if not an array subscript.");
10533 auto *Length = OASE->getLength();
10534
10535 // If we don't have a length we have to check if the array has unitary size
10536 // for this dimension. Also, we should always expect a length if the base type
10537 // is pointer.
10538 if (!Length) {
10539 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
10540 return ATy->getSize().getSExtValue() != 1;
10541 // We cannot assume anything.
10542 return false;
10543 }
10544
10545 // Check if the length evaluates to 1.
10546 llvm::APSInt ConstLength;
10547 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
10548 return false; // Can't get the integer value as a constant.
10549
10550 return ConstLength.getSExtValue() != 1;
10551}
10552
Samuel Antao661c0902016-05-26 17:39:58 +000010553// Return the expression of the base of the mappable expression or null if it
10554// cannot be determined and do all the necessary checks to see if the expression
10555// is valid as a standalone mappable expression. In the process, record all the
Samuel Antao90927002016-04-26 14:54:23 +000010556// components of the expression.
10557static Expr *CheckMapClauseExpressionBase(
10558 Sema &SemaRef, Expr *E,
Samuel Antao661c0902016-05-26 17:39:58 +000010559 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
10560 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010561 SourceLocation ELoc = E->getExprLoc();
10562 SourceRange ERange = E->getSourceRange();
10563
10564 // The base of elements of list in a map clause have to be either:
10565 // - a reference to variable or field.
10566 // - a member expression.
10567 // - an array expression.
10568 //
10569 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
10570 // reference to 'r'.
10571 //
10572 // If we have:
10573 //
10574 // struct SS {
10575 // Bla S;
10576 // foo() {
10577 // #pragma omp target map (S.Arr[:12]);
10578 // }
10579 // }
10580 //
10581 // We want to retrieve the member expression 'this->S';
10582
10583 Expr *RelevantExpr = nullptr;
10584
Samuel Antao5de996e2016-01-22 20:21:36 +000010585 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
10586 // If a list item is an array section, it must specify contiguous storage.
10587 //
10588 // For this restriction it is sufficient that we make sure only references
10589 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010590 // exist except in the rightmost expression (unless they cover the whole
10591 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +000010592 //
10593 // r.ArrS[3:5].Arr[6:7]
10594 //
10595 // r.ArrS[3:5].x
10596 //
10597 // but these would be valid:
10598 // r.ArrS[3].Arr[6:7]
10599 //
10600 // r.ArrS[3].x
10601
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010602 bool AllowUnitySizeArraySection = true;
10603 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +000010604
Dmitry Polukhin644a9252016-03-11 07:58:34 +000010605 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010606 E = E->IgnoreParenImpCasts();
10607
10608 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
10609 if (!isa<VarDecl>(CurE->getDecl()))
10610 break;
10611
10612 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010613
10614 // If we got a reference to a declaration, we should not expect any array
10615 // section before that.
10616 AllowUnitySizeArraySection = false;
10617 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010618
10619 // Record the component.
10620 CurComponents.push_back(OMPClauseMappableExprCommon::MappableComponent(
10621 CurE, CurE->getDecl()));
Samuel Antao5de996e2016-01-22 20:21:36 +000010622 continue;
10623 }
10624
10625 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
10626 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
10627
10628 if (isa<CXXThisExpr>(BaseE))
10629 // We found a base expression: this->Val.
10630 RelevantExpr = CurE;
10631 else
10632 E = BaseE;
10633
10634 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
10635 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
10636 << CurE->getSourceRange();
10637 break;
10638 }
10639
10640 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
10641
10642 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
10643 // A bit-field cannot appear in a map clause.
10644 //
10645 if (FD->isBitField()) {
Samuel Antao661c0902016-05-26 17:39:58 +000010646 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
10647 << CurE->getSourceRange() << getOpenMPClauseName(CKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010648 break;
10649 }
10650
10651 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10652 // If the type of a list item is a reference to a type T then the type
10653 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010654 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010655
10656 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
10657 // A list item cannot be a variable that is a member of a structure with
10658 // a union type.
10659 //
10660 if (auto *RT = CurType->getAs<RecordType>())
10661 if (RT->isUnionType()) {
10662 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
10663 << CurE->getSourceRange();
10664 break;
10665 }
10666
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010667 // If we got a member expression, we should not expect any array section
10668 // before that:
10669 //
10670 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
10671 // If a list item is an element of a structure, only the rightmost symbol
10672 // of the variable reference can be an array section.
10673 //
10674 AllowUnitySizeArraySection = false;
10675 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010676
10677 // Record the component.
10678 CurComponents.push_back(
10679 OMPClauseMappableExprCommon::MappableComponent(CurE, FD));
Samuel Antao5de996e2016-01-22 20:21:36 +000010680 continue;
10681 }
10682
10683 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
10684 E = CurE->getBase()->IgnoreParenImpCasts();
10685
10686 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
10687 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
10688 << 0 << CurE->getSourceRange();
10689 break;
10690 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010691
10692 // If we got an array subscript that express the whole dimension we
10693 // can have any array expressions before. If it only expressing part of
10694 // the dimension, we can only have unitary-size array expressions.
10695 if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
10696 E->getType()))
10697 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010698
10699 // Record the component - we don't have any declaration associated.
10700 CurComponents.push_back(
10701 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +000010702 continue;
10703 }
10704
10705 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010706 E = CurE->getBase()->IgnoreParenImpCasts();
10707
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010708 auto CurType =
10709 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
10710
Samuel Antao5de996e2016-01-22 20:21:36 +000010711 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10712 // If the type of a list item is a reference to a type T then the type
10713 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +000010714 if (CurType->isReferenceType())
10715 CurType = CurType->getPointeeType();
10716
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010717 bool IsPointer = CurType->isAnyPointerType();
10718
10719 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010720 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
10721 << 0 << CurE->getSourceRange();
10722 break;
10723 }
10724
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010725 bool NotWhole =
10726 CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
10727 bool NotUnity =
10728 CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
10729
Samuel Antaodab51bb2016-07-18 23:22:11 +000010730 if (AllowWholeSizeArraySection) {
10731 // Any array section is currently allowed. Allowing a whole size array
10732 // section implies allowing a unity array section as well.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010733 //
10734 // If this array section refers to the whole dimension we can still
10735 // accept other array sections before this one, except if the base is a
10736 // pointer. Otherwise, only unitary sections are accepted.
10737 if (NotWhole || IsPointer)
10738 AllowWholeSizeArraySection = false;
Samuel Antaodab51bb2016-07-18 23:22:11 +000010739 } else if (AllowUnitySizeArraySection && NotUnity) {
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010740 // A unity or whole array section is not allowed and that is not
10741 // compatible with the properties of the current array section.
10742 SemaRef.Diag(
10743 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
10744 << CurE->getSourceRange();
10745 break;
10746 }
Samuel Antao90927002016-04-26 14:54:23 +000010747
10748 // Record the component - we don't have any declaration associated.
10749 CurComponents.push_back(
10750 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +000010751 continue;
10752 }
10753
10754 // If nothing else worked, this is not a valid map clause expression.
10755 SemaRef.Diag(ELoc,
10756 diag::err_omp_expected_named_var_member_or_array_expression)
10757 << ERange;
10758 break;
10759 }
10760
10761 return RelevantExpr;
10762}
10763
10764// Return true if expression E associated with value VD has conflicts with other
10765// map information.
Samuel Antao90927002016-04-26 14:54:23 +000010766static bool CheckMapConflicts(
10767 Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD, Expr *E,
10768 bool CurrentRegionOnly,
Samuel Antao661c0902016-05-26 17:39:58 +000010769 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
10770 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010771 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +000010772 SourceLocation ELoc = E->getExprLoc();
10773 SourceRange ERange = E->getSourceRange();
10774
10775 // In order to easily check the conflicts we need to match each component of
10776 // the expression under test with the components of the expressions that are
10777 // already in the stack.
10778
Samuel Antao5de996e2016-01-22 20:21:36 +000010779 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000010780 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000010781 "Map clause expression with unexpected base!");
10782
10783 // Variables to help detecting enclosing problems in data environment nests.
10784 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +000010785 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000010786
Samuel Antao90927002016-04-26 14:54:23 +000010787 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
10788 VD, CurrentRegionOnly,
10789 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
Samuel Antao6890b092016-07-28 14:25:09 +000010790 StackComponents,
10791 OpenMPClauseKind) -> bool {
Samuel Antao90927002016-04-26 14:54:23 +000010792
Samuel Antao5de996e2016-01-22 20:21:36 +000010793 assert(!StackComponents.empty() &&
10794 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000010795 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000010796 "Map clause expression with unexpected base!");
10797
Samuel Antao90927002016-04-26 14:54:23 +000010798 // The whole expression in the stack.
10799 auto *RE = StackComponents.front().getAssociatedExpression();
10800
Samuel Antao5de996e2016-01-22 20:21:36 +000010801 // Expressions must start from the same base. Here we detect at which
10802 // point both expressions diverge from each other and see if we can
10803 // detect if the memory referred to both expressions is contiguous and
10804 // do not overlap.
10805 auto CI = CurComponents.rbegin();
10806 auto CE = CurComponents.rend();
10807 auto SI = StackComponents.rbegin();
10808 auto SE = StackComponents.rend();
10809 for (; CI != CE && SI != SE; ++CI, ++SI) {
10810
10811 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
10812 // At most one list item can be an array item derived from a given
10813 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +000010814 if (CurrentRegionOnly &&
10815 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
10816 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
10817 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
10818 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
10819 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +000010820 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +000010821 << CI->getAssociatedExpression()->getSourceRange();
10822 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
10823 diag::note_used_here)
10824 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +000010825 return true;
10826 }
10827
10828 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +000010829 if (CI->getAssociatedExpression()->getStmtClass() !=
10830 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +000010831 break;
10832
10833 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +000010834 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +000010835 break;
10836 }
Kelvin Li9f645ae2016-07-18 22:49:16 +000010837 // Check if the extra components of the expressions in the enclosing
10838 // data environment are redundant for the current base declaration.
10839 // If they are, the maps completely overlap, which is legal.
10840 for (; SI != SE; ++SI) {
10841 QualType Type;
10842 if (auto *ASE =
10843 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
10844 Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
10845 } else if (auto *OASE =
10846 dyn_cast<OMPArraySectionExpr>(SI->getAssociatedExpression())) {
10847 auto *E = OASE->getBase()->IgnoreParenImpCasts();
10848 Type =
10849 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
10850 }
10851 if (Type.isNull() || Type->isAnyPointerType() ||
10852 CheckArrayExpressionDoesNotReferToWholeSize(
10853 SemaRef, SI->getAssociatedExpression(), Type))
10854 break;
10855 }
Samuel Antao5de996e2016-01-22 20:21:36 +000010856
10857 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10858 // List items of map clauses in the same construct must not share
10859 // original storage.
10860 //
10861 // If the expressions are exactly the same or one is a subset of the
10862 // other, it means they are sharing storage.
10863 if (CI == CE && SI == SE) {
10864 if (CurrentRegionOnly) {
Samuel Antao661c0902016-05-26 17:39:58 +000010865 if (CKind == OMPC_map)
10866 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10867 else {
Samuel Antaoec172c62016-05-26 17:49:04 +000010868 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000010869 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
10870 << ERange;
10871 }
Samuel Antao5de996e2016-01-22 20:21:36 +000010872 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10873 << RE->getSourceRange();
10874 return true;
10875 } else {
10876 // If we find the same expression in the enclosing data environment,
10877 // that is legal.
10878 IsEnclosedByDataEnvironmentExpr = true;
10879 return false;
10880 }
10881 }
10882
Samuel Antao90927002016-04-26 14:54:23 +000010883 QualType DerivedType =
10884 std::prev(CI)->getAssociatedDeclaration()->getType();
10885 SourceLocation DerivedLoc =
10886 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000010887
10888 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10889 // If the type of a list item is a reference to a type T then the type
10890 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000010891 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010892
10893 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
10894 // A variable for which the type is pointer and an array section
10895 // derived from that variable must not appear as list items of map
10896 // clauses of the same construct.
10897 //
10898 // Also, cover one of the cases in:
10899 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10900 // If any part of the original storage of a list item has corresponding
10901 // storage in the device data environment, all of the original storage
10902 // must have corresponding storage in the device data environment.
10903 //
10904 if (DerivedType->isAnyPointerType()) {
10905 if (CI == CE || SI == SE) {
10906 SemaRef.Diag(
10907 DerivedLoc,
10908 diag::err_omp_pointer_mapped_along_with_derived_section)
10909 << DerivedLoc;
10910 } else {
10911 assert(CI != CE && SI != SE);
10912 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
10913 << DerivedLoc;
10914 }
10915 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10916 << RE->getSourceRange();
10917 return true;
10918 }
10919
10920 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10921 // List items of map clauses in the same construct must not share
10922 // original storage.
10923 //
10924 // An expression is a subset of the other.
10925 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
Samuel Antao661c0902016-05-26 17:39:58 +000010926 if (CKind == OMPC_map)
10927 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10928 else {
Samuel Antaoec172c62016-05-26 17:49:04 +000010929 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000010930 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
10931 << ERange;
10932 }
Samuel Antao5de996e2016-01-22 20:21:36 +000010933 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10934 << RE->getSourceRange();
10935 return true;
10936 }
10937
10938 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000010939 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000010940 if (!CurrentRegionOnly && SI != SE)
10941 EnclosingExpr = RE;
10942
10943 // The current expression is a subset of the expression in the data
10944 // environment.
10945 IsEnclosedByDataEnvironmentExpr |=
10946 (!CurrentRegionOnly && CI != CE && SI == SE);
10947
10948 return false;
10949 });
10950
10951 if (CurrentRegionOnly)
10952 return FoundError;
10953
10954 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10955 // If any part of the original storage of a list item has corresponding
10956 // storage in the device data environment, all of the original storage must
10957 // have corresponding storage in the device data environment.
10958 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
10959 // If a list item is an element of a structure, and a different element of
10960 // the structure has a corresponding list item in the device data environment
10961 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000010962 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000010963 // data environment prior to the task encountering the construct.
10964 //
10965 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
10966 SemaRef.Diag(ELoc,
10967 diag::err_omp_original_storage_is_shared_and_does_not_contain)
10968 << ERange;
10969 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
10970 << EnclosingExpr->getSourceRange();
10971 return true;
10972 }
10973
10974 return FoundError;
10975}
10976
Samuel Antao661c0902016-05-26 17:39:58 +000010977namespace {
10978// Utility struct that gathers all the related lists associated with a mappable
10979// expression.
10980struct MappableVarListInfo final {
10981 // The list of expressions.
10982 ArrayRef<Expr *> VarList;
10983 // The list of processed expressions.
10984 SmallVector<Expr *, 16> ProcessedVarList;
10985 // The mappble components for each expression.
10986 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
10987 // The base declaration of the variable.
10988 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
10989
10990 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
10991 // We have a list of components and base declarations for each entry in the
10992 // variable list.
10993 VarComponents.reserve(VarList.size());
10994 VarBaseDeclarations.reserve(VarList.size());
10995 }
10996};
10997}
10998
10999// Check the validity of the provided variable list for the provided clause kind
11000// \a CKind. In the check process the valid expressions, and mappable expression
11001// components and variables are extracted and used to fill \a Vars,
11002// \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and
11003// \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'.
11004static void
11005checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS,
11006 OpenMPClauseKind CKind, MappableVarListInfo &MVLI,
11007 SourceLocation StartLoc,
11008 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
11009 bool IsMapTypeImplicit = false) {
Samuel Antaoec172c62016-05-26 17:49:04 +000011010 // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
11011 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
Samuel Antao661c0902016-05-26 17:39:58 +000011012 "Unexpected clause kind with mappable expressions!");
Kelvin Li0bff7af2015-11-23 05:32:03 +000011013
Samuel Antao90927002016-04-26 14:54:23 +000011014 // Keep track of the mappable components and base declarations in this clause.
11015 // Each entry in the list is going to have a list of components associated. We
11016 // record each set of the components so that we can build the clause later on.
11017 // In the end we should have the same amount of declarations and component
11018 // lists.
Samuel Antao90927002016-04-26 14:54:23 +000011019
Samuel Antao661c0902016-05-26 17:39:58 +000011020 for (auto &RE : MVLI.VarList) {
Samuel Antaoec172c62016-05-26 17:49:04 +000011021 assert(RE && "Null expr in omp to/from/map clause");
Kelvin Li0bff7af2015-11-23 05:32:03 +000011022 SourceLocation ELoc = RE->getExprLoc();
11023
Kelvin Li0bff7af2015-11-23 05:32:03 +000011024 auto *VE = RE->IgnoreParenLValueCasts();
11025
11026 if (VE->isValueDependent() || VE->isTypeDependent() ||
11027 VE->isInstantiationDependent() ||
11028 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000011029 // We can only analyze this information once the missing information is
11030 // resolved.
Samuel Antao661c0902016-05-26 17:39:58 +000011031 MVLI.ProcessedVarList.push_back(RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011032 continue;
11033 }
11034
11035 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000011036
Samuel Antao5de996e2016-01-22 20:21:36 +000011037 if (!RE->IgnoreParenImpCasts()->isLValue()) {
Samuel Antao661c0902016-05-26 17:39:58 +000011038 SemaRef.Diag(ELoc,
11039 diag::err_omp_expected_named_var_member_or_array_expression)
Samuel Antao5de996e2016-01-22 20:21:36 +000011040 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000011041 continue;
11042 }
11043
Samuel Antao90927002016-04-26 14:54:23 +000011044 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
11045 ValueDecl *CurDeclaration = nullptr;
11046
11047 // Obtain the array or member expression bases if required. Also, fill the
11048 // components array with all the components identified in the process.
Samuel Antao661c0902016-05-26 17:39:58 +000011049 auto *BE =
11050 CheckMapClauseExpressionBase(SemaRef, SimpleExpr, CurComponents, CKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000011051 if (!BE)
11052 continue;
11053
Samuel Antao90927002016-04-26 14:54:23 +000011054 assert(!CurComponents.empty() &&
11055 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000011056
Samuel Antao90927002016-04-26 14:54:23 +000011057 // For the following checks, we rely on the base declaration which is
11058 // expected to be associated with the last component. The declaration is
11059 // expected to be a variable or a field (if 'this' is being mapped).
11060 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
11061 assert(CurDeclaration && "Null decl on map clause.");
11062 assert(
11063 CurDeclaration->isCanonicalDecl() &&
11064 "Expecting components to have associated only canonical declarations.");
11065
11066 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
11067 auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000011068
11069 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000011070 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000011071
11072 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
Samuel Antao661c0902016-05-26 17:39:58 +000011073 // threadprivate variables cannot appear in a map clause.
11074 // OpenMP 4.5 [2.10.5, target update Construct]
11075 // threadprivate variables cannot appear in a from clause.
11076 if (VD && DSAS->isThreadPrivate(VD)) {
11077 auto DVar = DSAS->getTopDSA(VD, false);
11078 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
11079 << getOpenMPClauseName(CKind);
11080 ReportOriginalDSA(SemaRef, DSAS, VD, DVar);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011081 continue;
11082 }
11083
Samuel Antao5de996e2016-01-22 20:21:36 +000011084 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
11085 // A list item cannot appear in both a map clause and a data-sharing
11086 // attribute clause on the same construct.
Kelvin Li0bff7af2015-11-23 05:32:03 +000011087
Samuel Antao5de996e2016-01-22 20:21:36 +000011088 // Check conflicts with other map clause expressions. We check the conflicts
11089 // with the current construct separately from the enclosing data
Samuel Antao661c0902016-05-26 17:39:58 +000011090 // environment, because the restrictions are different. We only have to
11091 // check conflicts across regions for the map clauses.
11092 if (CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
11093 /*CurrentRegionOnly=*/true, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000011094 break;
Samuel Antao661c0902016-05-26 17:39:58 +000011095 if (CKind == OMPC_map &&
11096 CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
11097 /*CurrentRegionOnly=*/false, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000011098 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000011099
Samuel Antao661c0902016-05-26 17:39:58 +000011100 // OpenMP 4.5 [2.10.5, target update Construct]
Samuel Antao5de996e2016-01-22 20:21:36 +000011101 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
11102 // If the type of a list item is a reference to a type T then the type will
11103 // be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000011104 QualType Type = CurDeclaration->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000011105
Samuel Antao661c0902016-05-26 17:39:58 +000011106 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
11107 // A list item in a to or from clause must have a mappable type.
Samuel Antao5de996e2016-01-22 20:21:36 +000011108 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000011109 // A list item must have a mappable type.
Samuel Antao661c0902016-05-26 17:39:58 +000011110 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
11111 DSAS, Type))
Kelvin Li0bff7af2015-11-23 05:32:03 +000011112 continue;
11113
Samuel Antao661c0902016-05-26 17:39:58 +000011114 if (CKind == OMPC_map) {
11115 // target enter data
11116 // OpenMP [2.10.2, Restrictions, p. 99]
11117 // A map-type must be specified in all map clauses and must be either
11118 // to or alloc.
11119 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
11120 if (DKind == OMPD_target_enter_data &&
11121 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
11122 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
11123 << (IsMapTypeImplicit ? 1 : 0)
11124 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
11125 << getOpenMPDirectiveName(DKind);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000011126 continue;
11127 }
Samuel Antao661c0902016-05-26 17:39:58 +000011128
11129 // target exit_data
11130 // OpenMP [2.10.3, Restrictions, p. 102]
11131 // A map-type must be specified in all map clauses and must be either
11132 // from, release, or delete.
11133 if (DKind == OMPD_target_exit_data &&
11134 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
11135 MapType == OMPC_MAP_delete)) {
11136 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
11137 << (IsMapTypeImplicit ? 1 : 0)
11138 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
11139 << getOpenMPDirectiveName(DKind);
11140 continue;
11141 }
11142
11143 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
11144 // A list item cannot appear in both a map clause and a data-sharing
11145 // attribute clause on the same construct
11146 if (DKind == OMPD_target && VD) {
11147 auto DVar = DSAS->getTopDSA(VD, false);
11148 if (isOpenMPPrivate(DVar.CKind)) {
Samuel Antao6890b092016-07-28 14:25:09 +000011149 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Samuel Antao661c0902016-05-26 17:39:58 +000011150 << getOpenMPClauseName(DVar.CKind)
Samuel Antao6890b092016-07-28 14:25:09 +000011151 << getOpenMPClauseName(OMPC_map)
Samuel Antao661c0902016-05-26 17:39:58 +000011152 << getOpenMPDirectiveName(DSAS->getCurrentDirective());
11153 ReportOriginalDSA(SemaRef, DSAS, CurDeclaration, DVar);
11154 continue;
11155 }
11156 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +000011157 }
11158
Samuel Antao90927002016-04-26 14:54:23 +000011159 // Save the current expression.
Samuel Antao661c0902016-05-26 17:39:58 +000011160 MVLI.ProcessedVarList.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000011161
11162 // Store the components in the stack so that they can be used to check
11163 // against other clauses later on.
Samuel Antao6890b092016-07-28 14:25:09 +000011164 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
11165 /*WhereFoundClauseKind=*/OMPC_map);
Samuel Antao90927002016-04-26 14:54:23 +000011166
11167 // Save the components and declaration to create the clause. For purposes of
11168 // the clause creation, any component list that has has base 'this' uses
Samuel Antao686c70c2016-05-26 17:30:50 +000011169 // null as base declaration.
Samuel Antao661c0902016-05-26 17:39:58 +000011170 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
11171 MVLI.VarComponents.back().append(CurComponents.begin(),
11172 CurComponents.end());
11173 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
11174 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011175 }
Samuel Antao661c0902016-05-26 17:39:58 +000011176}
11177
11178OMPClause *
11179Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
11180 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
11181 SourceLocation MapLoc, SourceLocation ColonLoc,
11182 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
11183 SourceLocation LParenLoc, SourceLocation EndLoc) {
11184 MappableVarListInfo MVLI(VarList);
11185 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc,
11186 MapType, IsMapTypeImplicit);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011187
Samuel Antao5de996e2016-01-22 20:21:36 +000011188 // We need to produce a map clause even if we don't have variables so that
11189 // other diagnostics related with non-existing map clauses are accurate.
Samuel Antao661c0902016-05-26 17:39:58 +000011190 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11191 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11192 MVLI.VarComponents, MapTypeModifier, MapType,
11193 IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011194}
Kelvin Li099bb8c2015-11-24 20:50:12 +000011195
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011196QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
11197 TypeResult ParsedType) {
11198 assert(ParsedType.isUsable());
11199
11200 QualType ReductionType = GetTypeFromParser(ParsedType.get());
11201 if (ReductionType.isNull())
11202 return QualType();
11203
11204 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
11205 // A type name in a declare reduction directive cannot be a function type, an
11206 // array type, a reference type, or a type qualified with const, volatile or
11207 // restrict.
11208 if (ReductionType.hasQualifiers()) {
11209 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
11210 return QualType();
11211 }
11212
11213 if (ReductionType->isFunctionType()) {
11214 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
11215 return QualType();
11216 }
11217 if (ReductionType->isReferenceType()) {
11218 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
11219 return QualType();
11220 }
11221 if (ReductionType->isArrayType()) {
11222 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
11223 return QualType();
11224 }
11225 return ReductionType;
11226}
11227
11228Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
11229 Scope *S, DeclContext *DC, DeclarationName Name,
11230 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
11231 AccessSpecifier AS, Decl *PrevDeclInScope) {
11232 SmallVector<Decl *, 8> Decls;
11233 Decls.reserve(ReductionTypes.size());
11234
11235 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
11236 ForRedeclaration);
11237 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
11238 // A reduction-identifier may not be re-declared in the current scope for the
11239 // same type or for a type that is compatible according to the base language
11240 // rules.
11241 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
11242 OMPDeclareReductionDecl *PrevDRD = nullptr;
11243 bool InCompoundScope = true;
11244 if (S != nullptr) {
11245 // Find previous declaration with the same name not referenced in other
11246 // declarations.
11247 FunctionScopeInfo *ParentFn = getEnclosingFunction();
11248 InCompoundScope =
11249 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
11250 LookupName(Lookup, S);
11251 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
11252 /*AllowInlineNamespace=*/false);
11253 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
11254 auto Filter = Lookup.makeFilter();
11255 while (Filter.hasNext()) {
11256 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
11257 if (InCompoundScope) {
11258 auto I = UsedAsPrevious.find(PrevDecl);
11259 if (I == UsedAsPrevious.end())
11260 UsedAsPrevious[PrevDecl] = false;
11261 if (auto *D = PrevDecl->getPrevDeclInScope())
11262 UsedAsPrevious[D] = true;
11263 }
11264 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
11265 PrevDecl->getLocation();
11266 }
11267 Filter.done();
11268 if (InCompoundScope) {
11269 for (auto &PrevData : UsedAsPrevious) {
11270 if (!PrevData.second) {
11271 PrevDRD = PrevData.first;
11272 break;
11273 }
11274 }
11275 }
11276 } else if (PrevDeclInScope != nullptr) {
11277 auto *PrevDRDInScope = PrevDRD =
11278 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
11279 do {
11280 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
11281 PrevDRDInScope->getLocation();
11282 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
11283 } while (PrevDRDInScope != nullptr);
11284 }
11285 for (auto &TyData : ReductionTypes) {
11286 auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
11287 bool Invalid = false;
11288 if (I != PreviousRedeclTypes.end()) {
11289 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
11290 << TyData.first;
11291 Diag(I->second, diag::note_previous_definition);
11292 Invalid = true;
11293 }
11294 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
11295 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
11296 Name, TyData.first, PrevDRD);
11297 DC->addDecl(DRD);
11298 DRD->setAccess(AS);
11299 Decls.push_back(DRD);
11300 if (Invalid)
11301 DRD->setInvalidDecl();
11302 else
11303 PrevDRD = DRD;
11304 }
11305
11306 return DeclGroupPtrTy::make(
11307 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
11308}
11309
11310void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
11311 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11312
11313 // Enter new function scope.
11314 PushFunctionScope();
11315 getCurFunction()->setHasBranchProtectedScope();
11316 getCurFunction()->setHasOMPDeclareReductionCombiner();
11317
11318 if (S != nullptr)
11319 PushDeclContext(S, DRD);
11320 else
11321 CurContext = DRD;
11322
11323 PushExpressionEvaluationContext(PotentiallyEvaluated);
11324
11325 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011326 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
11327 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
11328 // uses semantics of argument handles by value, but it should be passed by
11329 // reference. C lang does not support references, so pass all parameters as
11330 // pointers.
11331 // Create 'T omp_in;' variable.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011332 auto *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011333 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011334 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
11335 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
11336 // uses semantics of argument handles by value, but it should be passed by
11337 // reference. C lang does not support references, so pass all parameters as
11338 // pointers.
11339 // Create 'T omp_out;' variable.
11340 auto *OmpOutParm =
11341 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
11342 if (S != nullptr) {
11343 PushOnScopeChains(OmpInParm, S);
11344 PushOnScopeChains(OmpOutParm, S);
11345 } else {
11346 DRD->addDecl(OmpInParm);
11347 DRD->addDecl(OmpOutParm);
11348 }
11349}
11350
11351void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
11352 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11353 DiscardCleanupsInEvaluationContext();
11354 PopExpressionEvaluationContext();
11355
11356 PopDeclContext();
11357 PopFunctionScopeInfo();
11358
11359 if (Combiner != nullptr)
11360 DRD->setCombiner(Combiner);
11361 else
11362 DRD->setInvalidDecl();
11363}
11364
11365void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
11366 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11367
11368 // Enter new function scope.
11369 PushFunctionScope();
11370 getCurFunction()->setHasBranchProtectedScope();
11371
11372 if (S != nullptr)
11373 PushDeclContext(S, DRD);
11374 else
11375 CurContext = DRD;
11376
11377 PushExpressionEvaluationContext(PotentiallyEvaluated);
11378
11379 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011380 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
11381 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
11382 // uses semantics of argument handles by value, but it should be passed by
11383 // reference. C lang does not support references, so pass all parameters as
11384 // pointers.
11385 // Create 'T omp_priv;' variable.
11386 auto *OmpPrivParm =
11387 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011388 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
11389 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
11390 // uses semantics of argument handles by value, but it should be passed by
11391 // reference. C lang does not support references, so pass all parameters as
11392 // pointers.
11393 // Create 'T omp_orig;' variable.
11394 auto *OmpOrigParm =
11395 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011396 if (S != nullptr) {
11397 PushOnScopeChains(OmpPrivParm, S);
11398 PushOnScopeChains(OmpOrigParm, S);
11399 } else {
11400 DRD->addDecl(OmpPrivParm);
11401 DRD->addDecl(OmpOrigParm);
11402 }
11403}
11404
11405void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D,
11406 Expr *Initializer) {
11407 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11408 DiscardCleanupsInEvaluationContext();
11409 PopExpressionEvaluationContext();
11410
11411 PopDeclContext();
11412 PopFunctionScopeInfo();
11413
11414 if (Initializer != nullptr)
11415 DRD->setInitializer(Initializer);
11416 else
11417 DRD->setInvalidDecl();
11418}
11419
11420Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
11421 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
11422 for (auto *D : DeclReductions.get()) {
11423 if (IsValid) {
11424 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11425 if (S != nullptr)
11426 PushOnScopeChains(DRD, S, /*AddToContext=*/false);
11427 } else
11428 D->setInvalidDecl();
11429 }
11430 return DeclReductions;
11431}
11432
Kelvin Li099bb8c2015-11-24 20:50:12 +000011433OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
11434 SourceLocation StartLoc,
11435 SourceLocation LParenLoc,
11436 SourceLocation EndLoc) {
11437 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +000011438
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011439 // OpenMP [teams Constrcut, Restrictions]
11440 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000011441 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
11442 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011443 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000011444
11445 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11446}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011447
11448OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
11449 SourceLocation StartLoc,
11450 SourceLocation LParenLoc,
11451 SourceLocation EndLoc) {
11452 Expr *ValExpr = ThreadLimit;
11453
11454 // OpenMP [teams Constrcut, Restrictions]
11455 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000011456 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
11457 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011458 return nullptr;
11459
11460 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
11461 EndLoc);
11462}
Alexey Bataeva0569352015-12-01 10:17:31 +000011463
11464OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
11465 SourceLocation StartLoc,
11466 SourceLocation LParenLoc,
11467 SourceLocation EndLoc) {
11468 Expr *ValExpr = Priority;
11469
11470 // OpenMP [2.9.1, task Constrcut]
11471 // The priority-value is a non-negative numerical scalar expression.
11472 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
11473 /*StrictlyPositive=*/false))
11474 return nullptr;
11475
11476 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11477}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000011478
11479OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
11480 SourceLocation StartLoc,
11481 SourceLocation LParenLoc,
11482 SourceLocation EndLoc) {
11483 Expr *ValExpr = Grainsize;
11484
11485 // OpenMP [2.9.2, taskloop Constrcut]
11486 // The parameter of the grainsize clause must be a positive integer
11487 // expression.
11488 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
11489 /*StrictlyPositive=*/true))
11490 return nullptr;
11491
11492 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11493}
Alexey Bataev382967a2015-12-08 12:06:20 +000011494
11495OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
11496 SourceLocation StartLoc,
11497 SourceLocation LParenLoc,
11498 SourceLocation EndLoc) {
11499 Expr *ValExpr = NumTasks;
11500
11501 // OpenMP [2.9.2, taskloop Constrcut]
11502 // The parameter of the num_tasks clause must be a positive integer
11503 // expression.
11504 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
11505 /*StrictlyPositive=*/true))
11506 return nullptr;
11507
11508 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11509}
11510
Alexey Bataev28c75412015-12-15 08:19:24 +000011511OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
11512 SourceLocation LParenLoc,
11513 SourceLocation EndLoc) {
11514 // OpenMP [2.13.2, critical construct, Description]
11515 // ... where hint-expression is an integer constant expression that evaluates
11516 // to a valid lock hint.
11517 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
11518 if (HintExpr.isInvalid())
11519 return nullptr;
11520 return new (Context)
11521 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
11522}
11523
Carlo Bertollib4adf552016-01-15 18:50:31 +000011524OMPClause *Sema::ActOnOpenMPDistScheduleClause(
11525 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
11526 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
11527 SourceLocation EndLoc) {
11528 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
11529 std::string Values;
11530 Values += "'";
11531 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
11532 Values += "'";
11533 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
11534 << Values << getOpenMPClauseName(OMPC_dist_schedule);
11535 return nullptr;
11536 }
11537 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000011538 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000011539 if (ChunkSize) {
11540 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
11541 !ChunkSize->isInstantiationDependent() &&
11542 !ChunkSize->containsUnexpandedParameterPack()) {
11543 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
11544 ExprResult Val =
11545 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
11546 if (Val.isInvalid())
11547 return nullptr;
11548
11549 ValExpr = Val.get();
11550
11551 // OpenMP [2.7.1, Restrictions]
11552 // chunk_size must be a loop invariant integer expression with a positive
11553 // value.
11554 llvm::APSInt Result;
11555 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
11556 if (Result.isSigned() && !Result.isStrictlyPositive()) {
11557 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
11558 << "dist_schedule" << ChunkSize->getSourceRange();
11559 return nullptr;
11560 }
Alexey Bataevb46cdea2016-06-15 11:20:48 +000011561 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
11562 !CurContext->isDependentContext()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +000011563 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
11564 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
11565 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000011566 }
11567 }
11568 }
11569
11570 return new (Context)
11571 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000011572 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000011573}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000011574
11575OMPClause *Sema::ActOnOpenMPDefaultmapClause(
11576 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
11577 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
11578 SourceLocation KindLoc, SourceLocation EndLoc) {
11579 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
11580 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
11581 Kind != OMPC_DEFAULTMAP_scalar) {
11582 std::string Value;
11583 SourceLocation Loc;
11584 Value += "'";
11585 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
11586 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
11587 OMPC_DEFAULTMAP_MODIFIER_tofrom);
11588 Loc = MLoc;
11589 } else {
11590 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
11591 OMPC_DEFAULTMAP_scalar);
11592 Loc = KindLoc;
11593 }
11594 Value += "'";
11595 Diag(Loc, diag::err_omp_unexpected_clause_value)
11596 << Value << getOpenMPClauseName(OMPC_defaultmap);
11597 return nullptr;
11598 }
11599
11600 return new (Context)
11601 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
11602}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011603
11604bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
11605 DeclContext *CurLexicalContext = getCurLexicalContext();
11606 if (!CurLexicalContext->isFileContext() &&
11607 !CurLexicalContext->isExternCContext() &&
11608 !CurLexicalContext->isExternCXXContext()) {
11609 Diag(Loc, diag::err_omp_region_not_file_context);
11610 return false;
11611 }
11612 if (IsInOpenMPDeclareTargetContext) {
11613 Diag(Loc, diag::err_omp_enclosed_declare_target);
11614 return false;
11615 }
11616
11617 IsInOpenMPDeclareTargetContext = true;
11618 return true;
11619}
11620
11621void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
11622 assert(IsInOpenMPDeclareTargetContext &&
11623 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
11624
11625 IsInOpenMPDeclareTargetContext = false;
11626}
11627
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011628void
11629Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope, CXXScopeSpec &ScopeSpec,
11630 const DeclarationNameInfo &Id,
11631 OMPDeclareTargetDeclAttr::MapTypeTy MT,
11632 NamedDeclSetType &SameDirectiveDecls) {
11633 LookupResult Lookup(*this, Id, LookupOrdinaryName);
11634 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
11635
11636 if (Lookup.isAmbiguous())
11637 return;
11638 Lookup.suppressDiagnostics();
11639
11640 if (!Lookup.isSingleResult()) {
11641 if (TypoCorrection Corrected =
11642 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
11643 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
11644 CTK_ErrorRecovery)) {
11645 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
11646 << Id.getName());
11647 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
11648 return;
11649 }
11650
11651 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
11652 return;
11653 }
11654
11655 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
11656 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
11657 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
11658 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
11659
11660 if (!ND->hasAttr<OMPDeclareTargetDeclAttr>()) {
11661 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
11662 ND->addAttr(A);
11663 if (ASTMutationListener *ML = Context.getASTMutationListener())
11664 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
11665 checkDeclIsAllowedInOpenMPTarget(nullptr, ND);
11666 } else if (ND->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() != MT) {
11667 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
11668 << Id.getName();
11669 }
11670 } else
11671 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
11672}
11673
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011674static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
11675 Sema &SemaRef, Decl *D) {
11676 if (!D)
11677 return;
11678 Decl *LD = nullptr;
11679 if (isa<TagDecl>(D)) {
11680 LD = cast<TagDecl>(D)->getDefinition();
11681 } else if (isa<VarDecl>(D)) {
11682 LD = cast<VarDecl>(D)->getDefinition();
11683
11684 // If this is an implicit variable that is legal and we do not need to do
11685 // anything.
11686 if (cast<VarDecl>(D)->isImplicit()) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011687 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11688 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11689 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011690 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011691 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011692 return;
11693 }
11694
11695 } else if (isa<FunctionDecl>(D)) {
11696 const FunctionDecl *FD = nullptr;
11697 if (cast<FunctionDecl>(D)->hasBody(FD))
11698 LD = const_cast<FunctionDecl *>(FD);
11699
11700 // If the definition is associated with the current declaration in the
11701 // target region (it can be e.g. a lambda) that is legal and we do not need
11702 // to do anything else.
11703 if (LD == D) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011704 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11705 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11706 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011707 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011708 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011709 return;
11710 }
11711 }
11712 if (!LD)
11713 LD = D;
11714 if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() &&
11715 (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) {
11716 // Outlined declaration is not declared target.
11717 if (LD->isOutOfLine()) {
11718 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
11719 SemaRef.Diag(SL, diag::note_used_here) << SR;
11720 } else {
11721 DeclContext *DC = LD->getDeclContext();
11722 while (DC) {
11723 if (isa<FunctionDecl>(DC) &&
11724 cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>())
11725 break;
11726 DC = DC->getParent();
11727 }
11728 if (DC)
11729 return;
11730
11731 // Is not declared in target context.
11732 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
11733 SemaRef.Diag(SL, diag::note_used_here) << SR;
11734 }
11735 // Mark decl as declared target to prevent further diagnostic.
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011736 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11737 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11738 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011739 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011740 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011741 }
11742}
11743
11744static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
11745 Sema &SemaRef, DSAStackTy *Stack,
11746 ValueDecl *VD) {
11747 if (VD->hasAttr<OMPDeclareTargetDeclAttr>())
11748 return true;
11749 if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType()))
11750 return false;
11751 return true;
11752}
11753
11754void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D) {
11755 if (!D || D->isInvalidDecl())
11756 return;
11757 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
11758 SourceLocation SL = E ? E->getLocStart() : D->getLocation();
11759 // 2.10.6: threadprivate variable cannot appear in a declare target directive.
11760 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
11761 if (DSAStack->isThreadPrivate(VD)) {
11762 Diag(SL, diag::err_omp_threadprivate_in_target);
11763 ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
11764 return;
11765 }
11766 }
11767 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
11768 // Problem if any with var declared with incomplete type will be reported
11769 // as normal, so no need to check it here.
11770 if ((E || !VD->getType()->isIncompleteType()) &&
11771 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) {
11772 // Mark decl as declared target to prevent further diagnostic.
11773 if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011774 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11775 Context, OMPDeclareTargetDeclAttr::MT_To);
11776 VD->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011777 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011778 ML->DeclarationMarkedOpenMPDeclareTarget(VD, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011779 }
11780 return;
11781 }
11782 }
11783 if (!E) {
11784 // Checking declaration inside declare target region.
11785 if (!D->hasAttr<OMPDeclareTargetDeclAttr>() &&
11786 (isa<VarDecl>(D) || isa<FunctionDecl>(D))) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011787 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11788 Context, OMPDeclareTargetDeclAttr::MT_To);
11789 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011790 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011791 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011792 }
11793 return;
11794 }
11795 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
11796}
Samuel Antao661c0902016-05-26 17:39:58 +000011797
11798OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
11799 SourceLocation StartLoc,
11800 SourceLocation LParenLoc,
11801 SourceLocation EndLoc) {
11802 MappableVarListInfo MVLI(VarList);
11803 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc);
11804 if (MVLI.ProcessedVarList.empty())
11805 return nullptr;
11806
11807 return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11808 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11809 MVLI.VarComponents);
11810}
Samuel Antaoec172c62016-05-26 17:49:04 +000011811
11812OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
11813 SourceLocation StartLoc,
11814 SourceLocation LParenLoc,
11815 SourceLocation EndLoc) {
11816 MappableVarListInfo MVLI(VarList);
11817 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc);
11818 if (MVLI.ProcessedVarList.empty())
11819 return nullptr;
11820
11821 return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11822 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11823 MVLI.VarComponents);
11824}
Carlo Bertolli2404b172016-07-13 15:37:16 +000011825
11826OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
11827 SourceLocation StartLoc,
11828 SourceLocation LParenLoc,
11829 SourceLocation EndLoc) {
Samuel Antaocc10b852016-07-28 14:23:26 +000011830 MappableVarListInfo MVLI(VarList);
11831 SmallVector<Expr *, 8> PrivateCopies;
11832 SmallVector<Expr *, 8> Inits;
11833
Carlo Bertolli2404b172016-07-13 15:37:16 +000011834 for (auto &RefExpr : VarList) {
11835 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
11836 SourceLocation ELoc;
11837 SourceRange ERange;
11838 Expr *SimpleRefExpr = RefExpr;
11839 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
11840 if (Res.second) {
11841 // It will be analyzed later.
Samuel Antaocc10b852016-07-28 14:23:26 +000011842 MVLI.ProcessedVarList.push_back(RefExpr);
11843 PrivateCopies.push_back(nullptr);
11844 Inits.push_back(nullptr);
Carlo Bertolli2404b172016-07-13 15:37:16 +000011845 }
11846 ValueDecl *D = Res.first;
11847 if (!D)
11848 continue;
11849
11850 QualType Type = D->getType();
Samuel Antaocc10b852016-07-28 14:23:26 +000011851 Type = Type.getNonReferenceType().getUnqualifiedType();
11852
11853 auto *VD = dyn_cast<VarDecl>(D);
11854
11855 // Item should be a pointer or reference to pointer.
11856 if (!Type->isPointerType()) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000011857 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
11858 << 0 << RefExpr->getSourceRange();
11859 continue;
11860 }
Samuel Antaocc10b852016-07-28 14:23:26 +000011861
11862 // Build the private variable and the expression that refers to it.
11863 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
11864 D->hasAttrs() ? &D->getAttrs() : nullptr);
11865 if (VDPrivate->isInvalidDecl())
11866 continue;
11867
11868 CurContext->addDecl(VDPrivate);
11869 auto VDPrivateRefExpr = buildDeclRefExpr(
11870 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
11871
11872 // Add temporary variable to initialize the private copy of the pointer.
11873 auto *VDInit =
11874 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
11875 auto *VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
11876 RefExpr->getExprLoc());
11877 AddInitializerToDecl(VDPrivate,
11878 DefaultLvalueConversion(VDInitRefExpr).get(),
11879 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
11880
11881 // If required, build a capture to implement the privatization initialized
11882 // with the current list item value.
11883 DeclRefExpr *Ref = nullptr;
11884 if (!VD)
11885 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
11886 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
11887 PrivateCopies.push_back(VDPrivateRefExpr);
11888 Inits.push_back(VDInitRefExpr);
11889
11890 // We need to add a data sharing attribute for this variable to make sure it
11891 // is correctly captured. A variable that shows up in a use_device_ptr has
11892 // similar properties of a first private variable.
11893 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
11894
11895 // Create a mappable component for the list item. List items in this clause
11896 // only need a component.
11897 MVLI.VarBaseDeclarations.push_back(D);
11898 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
11899 MVLI.VarComponents.back().push_back(
11900 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D));
Carlo Bertolli2404b172016-07-13 15:37:16 +000011901 }
11902
Samuel Antaocc10b852016-07-28 14:23:26 +000011903 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli2404b172016-07-13 15:37:16 +000011904 return nullptr;
11905
Samuel Antaocc10b852016-07-28 14:23:26 +000011906 return OMPUseDevicePtrClause::Create(
11907 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
11908 PrivateCopies, Inits, MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli2404b172016-07-13 15:37:16 +000011909}
Carlo Bertolli70594e92016-07-13 17:16:49 +000011910
11911OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
11912 SourceLocation StartLoc,
11913 SourceLocation LParenLoc,
11914 SourceLocation EndLoc) {
Samuel Antao6890b092016-07-28 14:25:09 +000011915 MappableVarListInfo MVLI(VarList);
Carlo Bertolli70594e92016-07-13 17:16:49 +000011916 for (auto &RefExpr : VarList) {
11917 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
11918 SourceLocation ELoc;
11919 SourceRange ERange;
11920 Expr *SimpleRefExpr = RefExpr;
11921 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
11922 if (Res.second) {
11923 // It will be analyzed later.
Samuel Antao6890b092016-07-28 14:25:09 +000011924 MVLI.ProcessedVarList.push_back(RefExpr);
Carlo Bertolli70594e92016-07-13 17:16:49 +000011925 }
11926 ValueDecl *D = Res.first;
11927 if (!D)
11928 continue;
11929
11930 QualType Type = D->getType();
11931 // item should be a pointer or array or reference to pointer or array
11932 if (!Type.getNonReferenceType()->isPointerType() &&
11933 !Type.getNonReferenceType()->isArrayType()) {
11934 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
11935 << 0 << RefExpr->getSourceRange();
11936 continue;
11937 }
Samuel Antao6890b092016-07-28 14:25:09 +000011938
11939 // Check if the declaration in the clause does not show up in any data
11940 // sharing attribute.
11941 auto DVar = DSAStack->getTopDSA(D, false);
11942 if (isOpenMPPrivate(DVar.CKind)) {
11943 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
11944 << getOpenMPClauseName(DVar.CKind)
11945 << getOpenMPClauseName(OMPC_is_device_ptr)
11946 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
11947 ReportOriginalDSA(*this, DSAStack, D, DVar);
11948 continue;
11949 }
11950
11951 Expr *ConflictExpr;
11952 if (DSAStack->checkMappableExprComponentListsForDecl(
11953 D, /* CurrentRegionOnly = */ true,
11954 [&ConflictExpr](
11955 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
11956 OpenMPClauseKind) -> bool {
11957 ConflictExpr = R.front().getAssociatedExpression();
11958 return true;
11959 })) {
11960 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
11961 Diag(ConflictExpr->getExprLoc(), diag::note_used_here)
11962 << ConflictExpr->getSourceRange();
11963 continue;
11964 }
11965
11966 // Store the components in the stack so that they can be used to check
11967 // against other clauses later on.
11968 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D);
11969 DSAStack->addMappableExpressionComponents(
11970 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
11971
11972 // Record the expression we've just processed.
11973 MVLI.ProcessedVarList.push_back(SimpleRefExpr);
11974
11975 // Create a mappable component for the list item. List items in this clause
11976 // only need a component. We use a null declaration to signal fields in
11977 // 'this'.
11978 assert((isa<DeclRefExpr>(SimpleRefExpr) ||
11979 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
11980 "Unexpected device pointer expression!");
11981 MVLI.VarBaseDeclarations.push_back(
11982 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr);
11983 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
11984 MVLI.VarComponents.back().push_back(MC);
Carlo Bertolli70594e92016-07-13 17:16:49 +000011985 }
11986
Samuel Antao6890b092016-07-28 14:25:09 +000011987 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli70594e92016-07-13 17:16:49 +000011988 return nullptr;
11989
Samuel Antao6890b092016-07-28 14:25:09 +000011990 return OMPIsDevicePtrClause::Create(
11991 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
11992 MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli70594e92016-07-13 17:16:49 +000011993}