blob: 8194e161eae8647b0170eccd7381f4bb68151fc8 [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:
Kelvin Li02532872016-08-05 14:37:37 +00001697 case OMPD_distribute_parallel_for:
1698 case OMPD_teams_distribute: {
Carlo Bertolli9925f152016-06-27 14:55:37 +00001699 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1700 QualType KmpInt32PtrTy =
1701 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
1702 Sema::CapturedParamNameType Params[] = {
1703 std::make_pair(".global_tid.", KmpInt32PtrTy),
1704 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1705 std::make_pair(".previous.lb.", Context.getSizeType()),
1706 std::make_pair(".previous.ub.", Context.getSizeType()),
1707 std::make_pair(StringRef(), QualType()) // __context with shared vars
1708 };
1709 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1710 Params);
1711 break;
1712 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001713 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001714 case OMPD_taskyield:
1715 case OMPD_barrier:
1716 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001717 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001718 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001719 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001720 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001721 case OMPD_target_exit_data:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001722 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001723 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001724 case OMPD_declare_target:
1725 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +00001726 case OMPD_target_update:
Alexey Bataev9959db52014-05-06 10:08:46 +00001727 llvm_unreachable("OpenMP Directive is not allowed");
1728 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001729 llvm_unreachable("Unknown OpenMP directive");
1730 }
1731}
1732
Alexey Bataev3392d762016-02-16 11:18:12 +00001733static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00001734 Expr *CaptureExpr, bool WithInit,
1735 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001736 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00001737 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00001738 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00001739 QualType Ty = Init->getType();
1740 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1741 if (S.getLangOpts().CPlusPlus)
1742 Ty = C.getLValueReferenceType(Ty);
1743 else {
1744 Ty = C.getPointerType(Ty);
1745 ExprResult Res =
1746 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1747 if (!Res.isUsable())
1748 return nullptr;
1749 Init = Res.get();
1750 }
Alexey Bataev61205072016-03-02 04:57:40 +00001751 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00001752 }
1753 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001754 if (!WithInit)
1755 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
Alexey Bataev4244be22016-02-11 05:35:55 +00001756 S.CurContext->addHiddenDecl(CED);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001757 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false,
1758 /*TypeMayContainAuto=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00001759 return CED;
1760}
1761
Alexey Bataev61205072016-03-02 04:57:40 +00001762static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
1763 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00001764 OMPCapturedExprDecl *CD;
1765 if (auto *VD = S.IsOpenMPCapturedDecl(D))
1766 CD = cast<OMPCapturedExprDecl>(VD);
1767 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00001768 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
1769 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001770 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00001771 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00001772}
1773
Alexey Bataev5a3af132016-03-29 08:58:54 +00001774static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
1775 if (!Ref) {
1776 auto *CD =
1777 buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."),
1778 CaptureExpr, /*WithInit=*/true, /*AsExpression=*/true);
1779 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1780 CaptureExpr->getExprLoc());
1781 }
1782 ExprResult Res = Ref;
1783 if (!S.getLangOpts().CPlusPlus &&
1784 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
1785 Ref->getType()->isPointerType())
1786 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
1787 if (!Res.isUsable())
1788 return ExprError();
1789 return CaptureExpr->isGLValue() ? Res : S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00001790}
1791
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001792StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1793 ArrayRef<OMPClause *> Clauses) {
1794 if (!S.isUsable()) {
1795 ActOnCapturedRegionError();
1796 return StmtError();
1797 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001798
1799 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001800 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001801 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001802 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001803 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001804 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001805 Clause->getClauseKind() == OMPC_copyprivate ||
1806 (getLangOpts().OpenMPUseTLS &&
1807 getASTContext().getTargetInfo().isTLSSupported() &&
1808 Clause->getClauseKind() == OMPC_copyin)) {
1809 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001810 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001811 for (auto *VarRef : Clause->children()) {
1812 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001813 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001814 }
1815 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001816 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001817 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev040d5402015-05-12 08:35:28 +00001818 // Mark all variables in private list clauses as used in inner region.
1819 // Required for proper codegen of combined directives.
1820 // TODO: add processing for other clauses.
Alexey Bataev3392d762016-02-16 11:18:12 +00001821 if (auto *C = OMPClauseWithPreInit::get(Clause)) {
Alexey Bataev005248a2016-02-25 05:25:57 +00001822 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
1823 for (auto *D : DS->decls())
Alexey Bataev3392d762016-02-16 11:18:12 +00001824 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
1825 }
Alexey Bataev4244be22016-02-11 05:35:55 +00001826 }
Alexey Bataev005248a2016-02-25 05:25:57 +00001827 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
1828 if (auto *E = C->getPostUpdateExpr())
1829 MarkDeclarationsReferencedInExpr(E);
1830 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001831 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001832 if (Clause->getClauseKind() == OMPC_schedule)
1833 SC = cast<OMPScheduleClause>(Clause);
1834 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001835 OC = cast<OMPOrderedClause>(Clause);
1836 else if (Clause->getClauseKind() == OMPC_linear)
1837 LCs.push_back(cast<OMPLinearClause>(Clause));
1838 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001839 bool ErrorFound = false;
1840 // OpenMP, 2.7.1 Loop Construct, Restrictions
1841 // The nonmonotonic modifier cannot be specified if an ordered clause is
1842 // specified.
1843 if (SC &&
1844 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1845 SC->getSecondScheduleModifier() ==
1846 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1847 OC) {
1848 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1849 ? SC->getFirstScheduleModifierLoc()
1850 : SC->getSecondScheduleModifierLoc(),
1851 diag::err_omp_schedule_nonmonotonic_ordered)
1852 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1853 ErrorFound = true;
1854 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001855 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1856 for (auto *C : LCs) {
1857 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1858 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1859 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001860 ErrorFound = true;
1861 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001862 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1863 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1864 OC->getNumForLoops()) {
1865 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1866 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1867 ErrorFound = true;
1868 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001869 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001870 ActOnCapturedRegionError();
1871 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001872 }
1873 return ActOnCapturedRegionEnd(S.get());
1874}
1875
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001876static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1877 OpenMPDirectiveKind CurrentRegion,
1878 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001879 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001880 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001881 // Allowed nesting of constructs
1882 // +------------------+-----------------+------------------------------------+
1883 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1884 // +------------------+-----------------+------------------------------------+
1885 // | parallel | parallel | * |
1886 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001887 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001888 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001889 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001890 // | parallel | simd | * |
1891 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001892 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001893 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001894 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001895 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001896 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001897 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001898 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001899 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001900 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001901 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001902 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001903 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001904 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001905 // | parallel | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001906 // | parallel | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001907 // | parallel | target parallel | * |
1908 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001909 // | parallel | target enter | * |
1910 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001911 // | parallel | target exit | * |
1912 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001913 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001914 // | parallel | cancellation | |
1915 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001916 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001917 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001918 // | parallel | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00001919 // | parallel | distribute | + |
1920 // | parallel | distribute | + |
1921 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00001922 // | parallel | distribute | + |
1923 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00001924 // | parallel | distribute simd | + |
Kelvin Li986330c2016-07-20 22:57:10 +00001925 // | parallel | target simd | * |
Kelvin Li02532872016-08-05 14:37:37 +00001926 // | parallel | teams distribute| + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001927 // +------------------+-----------------+------------------------------------+
1928 // | for | parallel | * |
1929 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001930 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001931 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001932 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001933 // | for | simd | * |
1934 // | for | sections | + |
1935 // | for | section | + |
1936 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001937 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001938 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001939 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001940 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001941 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001942 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001943 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001944 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001945 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001946 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001947 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001948 // | for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001949 // | for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001950 // | for | target parallel | * |
1951 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001952 // | for | target enter | * |
1953 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001954 // | for | target exit | * |
1955 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001956 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001957 // | for | cancellation | |
1958 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001959 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001960 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001961 // | for | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00001962 // | for | distribute | + |
1963 // | for | distribute | + |
1964 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00001965 // | for | distribute | + |
1966 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00001967 // | for | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00001968 // | for | target parallel | + |
1969 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00001970 // | for | target simd | * |
Kelvin Li02532872016-08-05 14:37:37 +00001971 // | for | teams distribute| + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001972 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001973 // | master | parallel | * |
1974 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001975 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001976 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001977 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001978 // | master | simd | * |
1979 // | master | sections | + |
1980 // | master | section | + |
1981 // | master | single | + |
1982 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001983 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001984 // | master |parallel sections| * |
1985 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001986 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001987 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001988 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001989 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001990 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001991 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001992 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001993 // | master | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001994 // | master | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001995 // | master | target parallel | * |
1996 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001997 // | master | target enter | * |
1998 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001999 // | master | target exit | * |
2000 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002001 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002002 // | master | cancellation | |
2003 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002004 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002005 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002006 // | master | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002007 // | master | distribute | + |
2008 // | master | distribute | + |
2009 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002010 // | master | distribute | + |
2011 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002012 // | master | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002013 // | master | target parallel | + |
2014 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002015 // | master | target simd | * |
Kelvin Li02532872016-08-05 14:37:37 +00002016 // | master | teams distribute| + |
Alexander Musman80c22892014-07-17 08:54:58 +00002017 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002018 // | critical | parallel | * |
2019 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002020 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002021 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002022 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002023 // | critical | simd | * |
2024 // | critical | sections | + |
2025 // | critical | section | + |
2026 // | critical | single | + |
2027 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002028 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002029 // | critical |parallel sections| * |
2030 // | critical | task | * |
2031 // | critical | taskyield | * |
2032 // | critical | barrier | + |
2033 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002034 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002035 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002036 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002037 // | critical | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002038 // | critical | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002039 // | critical | target parallel | * |
2040 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002041 // | critical | target enter | * |
2042 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002043 // | critical | target exit | * |
2044 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002045 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002046 // | critical | cancellation | |
2047 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002048 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002049 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002050 // | critical | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002051 // | critical | distribute | + |
2052 // | critical | distribute | + |
2053 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002054 // | critical | distribute | + |
2055 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002056 // | critical | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002057 // | critical | target parallel | + |
2058 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002059 // | critical | target simd | * |
Kelvin Li02532872016-08-05 14:37:37 +00002060 // | critical | teams distribute| + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002061 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002062 // | simd | parallel | |
2063 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002064 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00002065 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002066 // | simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002067 // | simd | simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002068 // | simd | sections | |
2069 // | simd | section | |
2070 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002071 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002072 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002073 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002074 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00002075 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002076 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002077 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002078 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00002079 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002080 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002081 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002082 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002083 // | simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002084 // | simd | target parallel | |
2085 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002086 // | simd | target enter | |
2087 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002088 // | simd | target exit | |
2089 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002090 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002091 // | simd | cancellation | |
2092 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002093 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002094 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002095 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002096 // | simd | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002097 // | simd | distribute | |
2098 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002099 // | simd | distribute | |
2100 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002101 // | simd | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002102 // | simd | target parallel | |
2103 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002104 // | simd | target simd | |
Kelvin Li02532872016-08-05 14:37:37 +00002105 // | simd | teams distribute| |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002106 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00002107 // | for simd | parallel | |
2108 // | for simd | for | |
2109 // | for simd | for simd | |
2110 // | for simd | master | |
2111 // | for simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002112 // | for simd | simd | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002113 // | for simd | sections | |
2114 // | for simd | section | |
2115 // | for simd | single | |
2116 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002117 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002118 // | for simd |parallel sections| |
2119 // | for simd | task | |
2120 // | for simd | taskyield | |
2121 // | for simd | barrier | |
2122 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002123 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002124 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002125 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002126 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002127 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002128 // | for simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002129 // | for simd | target parallel | |
2130 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002131 // | for simd | target enter | |
2132 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002133 // | for simd | target exit | |
2134 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002135 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002136 // | for simd | cancellation | |
2137 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002138 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002139 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002140 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002141 // | for simd | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002142 // | for simd | distribute | |
2143 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002144 // | for simd | distribute | |
2145 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002146 // | for simd | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002147 // | for simd | target parallel | |
2148 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002149 // | for simd | target simd | |
Kelvin Li02532872016-08-05 14:37:37 +00002150 // | for simd | teams distribute| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002151 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00002152 // | parallel for simd| parallel | |
2153 // | parallel for simd| for | |
2154 // | parallel for simd| for simd | |
2155 // | parallel for simd| master | |
2156 // | parallel for simd| critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002157 // | parallel for simd| simd | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002158 // | parallel for simd| sections | |
2159 // | parallel for simd| section | |
2160 // | parallel for simd| single | |
2161 // | parallel for simd| parallel for | |
2162 // | parallel for simd|parallel for simd| |
2163 // | parallel for simd|parallel sections| |
2164 // | parallel for simd| task | |
2165 // | parallel for simd| taskyield | |
2166 // | parallel for simd| barrier | |
2167 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002168 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002169 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002170 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002171 // | parallel for simd| atomic | |
2172 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002173 // | parallel for simd| target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002174 // | parallel for simd| target parallel | |
2175 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002176 // | parallel for simd| target enter | |
2177 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002178 // | parallel for simd| target exit | |
2179 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002180 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002181 // | parallel for simd| cancellation | |
2182 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002183 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002184 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002185 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002186 // | parallel for simd| distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002187 // | parallel for simd| distribute | |
2188 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002189 // | parallel for simd| distribute | |
2190 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002191 // | parallel for simd| distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002192 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002193 // | parallel for simd| target simd | |
Kelvin Li02532872016-08-05 14:37:37 +00002194 // | parallel for simd| teams distribute| |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002195 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002196 // | sections | parallel | * |
2197 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002198 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002199 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002200 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002201 // | sections | simd | * |
2202 // | sections | sections | + |
2203 // | sections | section | * |
2204 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002205 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002206 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002207 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002208 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002209 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002210 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002211 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002212 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002213 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002214 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002215 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002216 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002217 // | sections | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002218 // | sections | target parallel | * |
2219 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002220 // | sections | target enter | * |
2221 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002222 // | sections | target exit | * |
2223 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002224 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002225 // | sections | cancellation | |
2226 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002227 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002228 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002229 // | sections | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002230 // | sections | distribute | + |
2231 // | sections | distribute | + |
2232 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002233 // | sections | distribute | + |
2234 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002235 // | sections | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002236 // | sections | target parallel | + |
2237 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002238 // | sections | target simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002239 // +------------------+-----------------+------------------------------------+
2240 // | section | parallel | * |
2241 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002242 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002243 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002244 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002245 // | section | simd | * |
2246 // | section | sections | + |
2247 // | section | section | + |
2248 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002249 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002250 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002251 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002252 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002253 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002254 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002255 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002256 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002257 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002258 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002259 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002260 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002261 // | section | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002262 // | section | target parallel | * |
2263 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002264 // | section | target enter | * |
2265 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002266 // | section | target exit | * |
2267 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002268 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002269 // | section | cancellation | |
2270 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002271 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002272 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002273 // | section | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002274 // | section | distribute | + |
2275 // | section | distribute | + |
2276 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002277 // | section | distribute | + |
2278 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002279 // | section | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002280 // | section | target parallel | + |
2281 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002282 // | section | target simd | * |
Kelvin Li02532872016-08-05 14:37:37 +00002283 // | section | teams distrubte | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002284 // +------------------+-----------------+------------------------------------+
2285 // | single | parallel | * |
2286 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002287 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002288 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002289 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002290 // | single | simd | * |
2291 // | single | sections | + |
2292 // | single | section | + |
2293 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002294 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002295 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002296 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002297 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002298 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002299 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002300 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002301 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002302 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002303 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002304 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002305 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002306 // | single | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002307 // | single | target parallel | * |
2308 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002309 // | single | target enter | * |
2310 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002311 // | single | target exit | * |
2312 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002313 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002314 // | single | cancellation | |
2315 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002316 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002317 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002318 // | single | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002319 // | single | distribute | + |
2320 // | single | distribute | + |
2321 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002322 // | single | distribute | + |
2323 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002324 // | single | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002325 // | single | target parallel | + |
2326 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002327 // | single | target simd | * |
Kelvin Li02532872016-08-05 14:37:37 +00002328 // | single | teams distrubte | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002329 // +------------------+-----------------+------------------------------------+
2330 // | parallel for | parallel | * |
2331 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002332 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002333 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002334 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002335 // | parallel for | simd | * |
2336 // | parallel for | sections | + |
2337 // | parallel for | section | + |
2338 // | parallel for | single | + |
2339 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002340 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002341 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002342 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002343 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002344 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002345 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002346 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002347 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002348 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002349 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002350 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002351 // | parallel for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002352 // | parallel for | target parallel | * |
2353 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002354 // | parallel for | target enter | * |
2355 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002356 // | parallel for | target exit | * |
2357 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002358 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002359 // | parallel for | cancellation | |
2360 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002361 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002362 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002363 // | parallel for | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002364 // | parallel for | distribute | + |
2365 // | parallel for | distribute | + |
2366 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002367 // | parallel for | distribute | + |
2368 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002369 // | parallel for | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002370 // | parallel for | target parallel | + |
2371 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002372 // | parallel for | target simd | * |
Kelvin Li02532872016-08-05 14:37:37 +00002373 // | parallel for | teams distribute| + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002374 // +------------------+-----------------+------------------------------------+
2375 // | parallel sections| parallel | * |
2376 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002377 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002378 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002379 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002380 // | parallel sections| simd | * |
2381 // | parallel sections| sections | + |
2382 // | parallel sections| section | * |
2383 // | parallel sections| single | + |
2384 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002385 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002386 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002387 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002388 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002389 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002390 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002391 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002392 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002393 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002394 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002395 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002396 // | parallel sections| target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002397 // | parallel sections| target parallel | * |
2398 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002399 // | parallel sections| target enter | * |
2400 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002401 // | parallel sections| target exit | * |
2402 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002403 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002404 // | parallel sections| cancellation | |
2405 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002406 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002407 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002408 // | parallel sections| taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002409 // | parallel sections| distribute | + |
2410 // | parallel sections| distribute | + |
2411 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002412 // | parallel sections| distribute | + |
2413 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002414 // | parallel sections| distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002415 // | parallel sections| target parallel | + |
2416 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002417 // | parallel sections| target simd | * |
Kelvin Li02532872016-08-05 14:37:37 +00002418 // | parallel sections| teams distribute| + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002419 // +------------------+-----------------+------------------------------------+
2420 // | task | parallel | * |
2421 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002422 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002423 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002424 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002425 // | task | simd | * |
2426 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002427 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002428 // | task | single | + |
2429 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002430 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002431 // | task |parallel sections| * |
2432 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002433 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002434 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002435 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002436 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002437 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002438 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002439 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002440 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002441 // | task | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002442 // | task | target parallel | * |
2443 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002444 // | task | target enter | * |
2445 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002446 // | task | target exit | * |
2447 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002448 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002449 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002450 // | | point | ! |
2451 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002452 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002453 // | task | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002454 // | task | distribute | + |
2455 // | task | distribute | + |
2456 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002457 // | task | distribute | + |
2458 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002459 // | task | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002460 // | task | target parallel | + |
2461 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002462 // | task | target simd | * |
Kelvin Li02532872016-08-05 14:37:37 +00002463 // | task | teams distribute| + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002464 // +------------------+-----------------+------------------------------------+
2465 // | ordered | parallel | * |
2466 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002467 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002468 // | ordered | master | * |
2469 // | ordered | critical | * |
2470 // | ordered | simd | * |
2471 // | ordered | sections | + |
2472 // | ordered | section | + |
2473 // | ordered | single | + |
2474 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002475 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002476 // | ordered |parallel sections| * |
2477 // | ordered | task | * |
2478 // | ordered | taskyield | * |
2479 // | ordered | barrier | + |
2480 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002481 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002482 // | ordered | flush | * |
2483 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002484 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002485 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002486 // | ordered | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002487 // | ordered | target parallel | * |
2488 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002489 // | ordered | target enter | * |
2490 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002491 // | ordered | target exit | * |
2492 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002493 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002494 // | ordered | cancellation | |
2495 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002496 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002497 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002498 // | ordered | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002499 // | ordered | distribute | + |
2500 // | ordered | distribute | + |
2501 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002502 // | ordered | distribute | + |
2503 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002504 // | ordered | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002505 // | ordered | target parallel | + |
2506 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002507 // | ordered | target simd | * |
Kelvin Li02532872016-08-05 14:37:37 +00002508 // | ordered | teams distribute| + |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002509 // +------------------+-----------------+------------------------------------+
2510 // | atomic | parallel | |
2511 // | atomic | for | |
2512 // | atomic | for simd | |
2513 // | atomic | master | |
2514 // | atomic | critical | |
2515 // | atomic | simd | |
2516 // | atomic | sections | |
2517 // | atomic | section | |
2518 // | atomic | single | |
2519 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002520 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002521 // | atomic |parallel sections| |
2522 // | atomic | task | |
2523 // | atomic | taskyield | |
2524 // | atomic | barrier | |
2525 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002526 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002527 // | atomic | flush | |
2528 // | atomic | ordered | |
2529 // | atomic | atomic | |
2530 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002531 // | atomic | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002532 // | atomic | target parallel | |
2533 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002534 // | atomic | target enter | |
2535 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002536 // | atomic | target exit | |
2537 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002538 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002539 // | atomic | cancellation | |
2540 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002541 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002542 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002543 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002544 // | atomic | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002545 // | atomic | distribute | |
2546 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002547 // | atomic | distribute | |
2548 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002549 // | atomic | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002550 // | atomic | target parallel | |
2551 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002552 // | atomic | target simd | |
Kelvin Li02532872016-08-05 14:37:37 +00002553 // | atomic | teams distribute| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002554 // +------------------+-----------------+------------------------------------+
2555 // | target | parallel | * |
2556 // | target | for | * |
2557 // | target | for simd | * |
2558 // | target | master | * |
2559 // | target | critical | * |
2560 // | target | simd | * |
2561 // | target | sections | * |
2562 // | target | section | * |
2563 // | target | single | * |
2564 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002565 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002566 // | target |parallel sections| * |
2567 // | target | task | * |
2568 // | target | taskyield | * |
2569 // | target | barrier | * |
2570 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002571 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002572 // | target | flush | * |
2573 // | target | ordered | * |
2574 // | target | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002575 // | target | target | |
2576 // | target | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002577 // | target | target parallel | |
2578 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002579 // | target | target enter | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002580 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002581 // | target | target exit | |
Samuel Antao72590762016-01-19 20:04:50 +00002582 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002583 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002584 // | target | cancellation | |
2585 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002586 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002587 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002588 // | target | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002589 // | target | distribute | + |
2590 // | target | distribute | + |
2591 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002592 // | target | distribute | + |
2593 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002594 // | target | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002595 // | target | target parallel | |
2596 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002597 // | target | target simd | |
Kelvin Li02532872016-08-05 14:37:37 +00002598 // | target | teams distribute| |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002599 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002600 // | target parallel | parallel | * |
2601 // | target parallel | for | * |
2602 // | target parallel | for simd | * |
2603 // | target parallel | master | * |
2604 // | target parallel | critical | * |
2605 // | target parallel | simd | * |
2606 // | target parallel | sections | * |
2607 // | target parallel | section | * |
2608 // | target parallel | single | * |
2609 // | target parallel | parallel for | * |
2610 // | target parallel |parallel for simd| * |
2611 // | target parallel |parallel sections| * |
2612 // | target parallel | task | * |
2613 // | target parallel | taskyield | * |
2614 // | target parallel | barrier | * |
2615 // | target parallel | taskwait | * |
2616 // | target parallel | taskgroup | * |
2617 // | target parallel | flush | * |
2618 // | target parallel | ordered | * |
2619 // | target parallel | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002620 // | target parallel | target | |
2621 // | target parallel | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002622 // | target parallel | target parallel | |
2623 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002624 // | target parallel | target enter | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002625 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002626 // | target parallel | target exit | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002627 // | | data | |
2628 // | target parallel | teams | |
2629 // | target parallel | cancellation | |
2630 // | | point | ! |
2631 // | target parallel | cancel | ! |
2632 // | target parallel | taskloop | * |
2633 // | target parallel | taskloop simd | * |
2634 // | target parallel | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002635 // | target parallel | distribute | |
2636 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002637 // | target parallel | distribute | |
2638 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002639 // | target parallel | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002640 // | target parallel | target parallel | |
2641 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002642 // | target parallel | target simd | |
Kelvin Li02532872016-08-05 14:37:37 +00002643 // | target parallel | teams distribute| + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002644 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002645 // | target parallel | parallel | * |
2646 // | for | | |
2647 // | target parallel | for | * |
2648 // | for | | |
2649 // | target parallel | for simd | * |
2650 // | for | | |
2651 // | target parallel | master | * |
2652 // | for | | |
2653 // | target parallel | critical | * |
2654 // | for | | |
2655 // | target parallel | simd | * |
2656 // | for | | |
2657 // | target parallel | sections | * |
2658 // | for | | |
2659 // | target parallel | section | * |
2660 // | for | | |
2661 // | target parallel | single | * |
2662 // | for | | |
2663 // | target parallel | parallel for | * |
2664 // | for | | |
2665 // | target parallel |parallel for simd| * |
2666 // | for | | |
2667 // | target parallel |parallel sections| * |
2668 // | for | | |
2669 // | target parallel | task | * |
2670 // | for | | |
2671 // | target parallel | taskyield | * |
2672 // | for | | |
2673 // | target parallel | barrier | * |
2674 // | for | | |
2675 // | target parallel | taskwait | * |
2676 // | for | | |
2677 // | target parallel | taskgroup | * |
2678 // | for | | |
2679 // | target parallel | flush | * |
2680 // | for | | |
2681 // | target parallel | ordered | * |
2682 // | for | | |
2683 // | target parallel | atomic | * |
2684 // | for | | |
2685 // | target parallel | target | |
2686 // | for | | |
2687 // | target parallel | target parallel | |
2688 // | for | | |
2689 // | target parallel | target parallel | |
2690 // | for | for | |
2691 // | target parallel | target enter | |
2692 // | for | data | |
2693 // | target parallel | target exit | |
2694 // | for | data | |
2695 // | target parallel | teams | |
2696 // | for | | |
2697 // | target parallel | cancellation | |
2698 // | for | point | ! |
2699 // | target parallel | cancel | ! |
2700 // | for | | |
2701 // | target parallel | taskloop | * |
2702 // | for | | |
2703 // | target parallel | taskloop simd | * |
2704 // | for | | |
2705 // | target parallel | distribute | |
2706 // | for | | |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002707 // | target parallel | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002708 // | for | parallel for | |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002709 // | target parallel | distribute | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002710 // | for |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002711 // | target parallel | distribute simd | |
2712 // | for | | |
Kelvin Lia579b912016-07-14 02:54:56 +00002713 // | target parallel | target parallel | |
2714 // | for | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002715 // | target parallel | target simd | |
2716 // | for | | |
Kelvin Li02532872016-08-05 14:37:37 +00002717 // | target parallel | teams distribute| |
2718 // | for | | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002719 // +------------------+-----------------+------------------------------------+
Alexey Bataev13314bf2014-10-09 04:18:56 +00002720 // | teams | parallel | * |
2721 // | teams | for | + |
2722 // | teams | for simd | + |
2723 // | teams | master | + |
2724 // | teams | critical | + |
2725 // | teams | simd | + |
2726 // | teams | sections | + |
2727 // | teams | section | + |
2728 // | teams | single | + |
2729 // | teams | parallel for | * |
2730 // | teams |parallel for simd| * |
2731 // | teams |parallel sections| * |
2732 // | teams | task | + |
2733 // | teams | taskyield | + |
2734 // | teams | barrier | + |
2735 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002736 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002737 // | teams | flush | + |
2738 // | teams | ordered | + |
2739 // | teams | atomic | + |
2740 // | teams | target | + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002741 // | teams | target parallel | + |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002742 // | teams | target parallel | + |
2743 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002744 // | teams | target enter | + |
2745 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002746 // | teams | target exit | + |
2747 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002748 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002749 // | teams | cancellation | |
2750 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002751 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002752 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002753 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002754 // | teams | distribute | ! |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002755 // | teams | distribute | ! |
2756 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002757 // | teams | distribute | ! |
2758 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002759 // | teams | distribute simd | ! |
Kelvin Lia579b912016-07-14 02:54:56 +00002760 // | teams | target parallel | + |
2761 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002762 // | teams | target simd | + |
Kelvin Li02532872016-08-05 14:37:37 +00002763 // | teams | teams distribute| + |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002764 // +------------------+-----------------+------------------------------------+
2765 // | taskloop | parallel | * |
2766 // | taskloop | for | + |
2767 // | taskloop | for simd | + |
2768 // | taskloop | master | + |
2769 // | taskloop | critical | * |
2770 // | taskloop | simd | * |
2771 // | taskloop | sections | + |
2772 // | taskloop | section | + |
2773 // | taskloop | single | + |
2774 // | taskloop | parallel for | * |
2775 // | taskloop |parallel for simd| * |
2776 // | taskloop |parallel sections| * |
2777 // | taskloop | task | * |
2778 // | taskloop | taskyield | * |
2779 // | taskloop | barrier | + |
2780 // | taskloop | taskwait | * |
2781 // | taskloop | taskgroup | * |
2782 // | taskloop | flush | * |
2783 // | taskloop | ordered | + |
2784 // | taskloop | atomic | * |
2785 // | taskloop | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002786 // | taskloop | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002787 // | taskloop | target parallel | * |
2788 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002789 // | taskloop | target enter | * |
2790 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002791 // | taskloop | target exit | * |
2792 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002793 // | taskloop | teams | + |
2794 // | taskloop | cancellation | |
2795 // | | point | |
2796 // | taskloop | cancel | |
2797 // | taskloop | taskloop | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002798 // | taskloop | distribute | + |
2799 // | taskloop | distribute | + |
2800 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002801 // | taskloop | distribute | + |
2802 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002803 // | taskloop | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002804 // | taskloop | target parallel | * |
2805 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002806 // | taskloop | target simd | * |
Kelvin Li02532872016-08-05 14:37:37 +00002807 // | taskloop | teams distribute| + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002808 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002809 // | taskloop simd | parallel | |
2810 // | taskloop simd | for | |
2811 // | taskloop simd | for simd | |
2812 // | taskloop simd | master | |
2813 // | taskloop simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002814 // | taskloop simd | simd | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002815 // | taskloop simd | sections | |
2816 // | taskloop simd | section | |
2817 // | taskloop simd | single | |
2818 // | taskloop simd | parallel for | |
2819 // | taskloop simd |parallel for simd| |
2820 // | taskloop simd |parallel sections| |
2821 // | taskloop simd | task | |
2822 // | taskloop simd | taskyield | |
2823 // | taskloop simd | barrier | |
2824 // | taskloop simd | taskwait | |
2825 // | taskloop simd | taskgroup | |
2826 // | taskloop simd | flush | |
2827 // | taskloop simd | ordered | + (with simd clause) |
2828 // | taskloop simd | atomic | |
2829 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002830 // | taskloop simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002831 // | taskloop simd | target parallel | |
2832 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002833 // | taskloop simd | target enter | |
2834 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002835 // | taskloop simd | target exit | |
2836 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002837 // | taskloop simd | teams | |
2838 // | taskloop simd | cancellation | |
2839 // | | point | |
2840 // | taskloop simd | cancel | |
2841 // | taskloop simd | taskloop | |
2842 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002843 // | taskloop simd | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002844 // | taskloop simd | distribute | |
2845 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002846 // | taskloop simd | distribute | |
2847 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002848 // | taskloop simd | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002849 // | taskloop simd | target parallel | |
2850 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002851 // | taskloop simd | target simd | |
Kelvin Li02532872016-08-05 14:37:37 +00002852 // | taskloop simd | teams distribute| |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002853 // +------------------+-----------------+------------------------------------+
2854 // | distribute | parallel | * |
2855 // | distribute | for | * |
2856 // | distribute | for simd | * |
2857 // | distribute | master | * |
2858 // | distribute | critical | * |
2859 // | distribute | simd | * |
2860 // | distribute | sections | * |
2861 // | distribute | section | * |
2862 // | distribute | single | * |
2863 // | distribute | parallel for | * |
2864 // | distribute |parallel for simd| * |
2865 // | distribute |parallel sections| * |
2866 // | distribute | task | * |
2867 // | distribute | taskyield | * |
2868 // | distribute | barrier | * |
2869 // | distribute | taskwait | * |
2870 // | distribute | taskgroup | * |
2871 // | distribute | flush | * |
2872 // | distribute | ordered | + |
2873 // | distribute | atomic | * |
2874 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002875 // | distribute | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002876 // | distribute | target parallel | |
2877 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002878 // | distribute | target enter | |
2879 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002880 // | distribute | target exit | |
2881 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002882 // | distribute | teams | |
2883 // | distribute | cancellation | + |
2884 // | | point | |
2885 // | distribute | cancel | + |
2886 // | distribute | taskloop | * |
2887 // | distribute | taskloop simd | * |
2888 // | distribute | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002889 // | distribute | distribute | |
2890 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002891 // | distribute | distribute | |
2892 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002893 // | distribute | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002894 // | distribute | target parallel | |
2895 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002896 // | distribute | target simd | |
Kelvin Li02532872016-08-05 14:37:37 +00002897 // | distribute | teams distribute| |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002898 // +------------------+-----------------+------------------------------------+
2899 // | distribute | parallel | * |
2900 // | parallel for | | |
2901 // | distribute | for | * |
2902 // | parallel for | | |
2903 // | distribute | for simd | * |
2904 // | parallel for | | |
2905 // | distribute | master | * |
2906 // | parallel for | | |
2907 // | distribute | critical | * |
2908 // | parallel for | | |
2909 // | distribute | simd | * |
2910 // | parallel for | | |
2911 // | distribute | sections | * |
2912 // | parallel for | | |
2913 // | distribute | section | * |
2914 // | parallel for | | |
2915 // | distribute | single | * |
2916 // | parallel for | | |
2917 // | distribute | parallel for | * |
2918 // | parallel for | | |
2919 // | distribute |parallel for simd| * |
2920 // | parallel for | | |
2921 // | distribute |parallel sections| * |
2922 // | parallel for | | |
2923 // | distribute | task | * |
2924 // | parallel for | | |
2925 // | parallel for | | |
2926 // | distribute | taskyield | * |
2927 // | parallel for | | |
2928 // | distribute | barrier | * |
2929 // | parallel for | | |
2930 // | distribute | taskwait | * |
2931 // | parallel for | | |
2932 // | distribute | taskgroup | * |
2933 // | parallel for | | |
2934 // | distribute | flush | * |
2935 // | parallel for | | |
2936 // | distribute | ordered | + |
2937 // | parallel for | | |
2938 // | distribute | atomic | * |
2939 // | parallel for | | |
2940 // | distribute | target | |
2941 // | parallel for | | |
2942 // | distribute | target parallel | |
2943 // | parallel for | | |
2944 // | distribute | target parallel | |
2945 // | parallel for | for | |
2946 // | distribute | target enter | |
2947 // | parallel for | data | |
2948 // | distribute | target exit | |
2949 // | parallel for | data | |
2950 // | distribute | teams | |
2951 // | parallel for | | |
2952 // | distribute | cancellation | + |
2953 // | parallel for | point | |
2954 // | distribute | cancel | + |
2955 // | parallel for | | |
2956 // | distribute | taskloop | * |
2957 // | parallel for | | |
2958 // | distribute | taskloop simd | * |
2959 // | parallel for | | |
2960 // | distribute | distribute | |
2961 // | parallel for | | |
2962 // | distribute | distribute | |
2963 // | parallel for | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002964 // | distribute | distribute | |
2965 // | parallel for |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002966 // | distribute | distribute simd | |
2967 // | parallel for | | |
Kelvin Lia579b912016-07-14 02:54:56 +00002968 // | distribute | target parallel | |
2969 // | parallel for | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002970 // | distribute | target simd | |
2971 // | parallel for | | |
Kelvin Li02532872016-08-05 14:37:37 +00002972 // | distribute | teams distribute| |
2973 // | parallel for | | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002974 // +------------------+-----------------+------------------------------------+
2975 // | distribute | parallel | * |
2976 // | parallel for simd| | |
2977 // | distribute | for | * |
2978 // | parallel for simd| | |
2979 // | distribute | for simd | * |
2980 // | parallel for simd| | |
2981 // | distribute | master | * |
2982 // | parallel for simd| | |
2983 // | distribute | critical | * |
2984 // | parallel for simd| | |
2985 // | distribute | simd | * |
2986 // | parallel for simd| | |
2987 // | distribute | sections | * |
2988 // | parallel for simd| | |
2989 // | distribute | section | * |
2990 // | parallel for simd| | |
2991 // | distribute | single | * |
2992 // | parallel for simd| | |
2993 // | distribute | parallel for | * |
2994 // | parallel for simd| | |
2995 // | distribute |parallel for simd| * |
2996 // | parallel for simd| | |
2997 // | distribute |parallel sections| * |
2998 // | parallel for simd| | |
2999 // | distribute | task | * |
3000 // | parallel for simd| | |
3001 // | distribute | taskyield | * |
3002 // | parallel for simd| | |
3003 // | distribute | barrier | * |
3004 // | parallel for simd| | |
3005 // | distribute | taskwait | * |
3006 // | parallel for simd| | |
3007 // | distribute | taskgroup | * |
3008 // | parallel for simd| | |
3009 // | distribute | flush | * |
3010 // | parallel for simd| | |
3011 // | distribute | ordered | + |
3012 // | parallel for simd| | |
3013 // | distribute | atomic | * |
3014 // | parallel for simd| | |
3015 // | distribute | target | |
3016 // | parallel for simd| | |
3017 // | distribute | target parallel | |
3018 // | parallel for simd| | |
3019 // | distribute | target parallel | |
3020 // | parallel for simd| for | |
3021 // | distribute | target enter | |
3022 // | parallel for simd| data | |
3023 // | distribute | target exit | |
3024 // | parallel for simd| data | |
3025 // | distribute | teams | |
3026 // | parallel for simd| | |
3027 // | distribute | cancellation | + |
3028 // | parallel for simd| point | |
3029 // | distribute | cancel | + |
3030 // | parallel for simd| | |
3031 // | distribute | taskloop | * |
3032 // | parallel for simd| | |
3033 // | distribute | taskloop simd | * |
3034 // | parallel for simd| | |
3035 // | distribute | distribute | |
3036 // | parallel for simd| | |
3037 // | distribute | distribute | * |
3038 // | parallel for simd| parallel for | |
3039 // | distribute | distribute | * |
3040 // | parallel for simd|parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00003041 // | distribute | distribute simd | * |
3042 // | parallel for simd| | |
Kelvin Lia579b912016-07-14 02:54:56 +00003043 // | distribute | target parallel | |
3044 // | parallel for simd| for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00003045 // | distribute | target simd | |
3046 // | parallel for simd| | |
Kelvin Li02532872016-08-05 14:37:37 +00003047 // | distribute | teams distribute| |
3048 // | parallel for simd| | |
Kelvin Li787f3fc2016-07-06 04:45:38 +00003049 // +------------------+-----------------+------------------------------------+
3050 // | distribute simd | parallel | * |
3051 // | distribute simd | for | * |
3052 // | distribute simd | for simd | * |
3053 // | distribute simd | master | * |
3054 // | distribute simd | critical | * |
3055 // | distribute simd | simd | * |
3056 // | distribute simd | sections | * |
3057 // | distribute simd | section | * |
3058 // | distribute simd | single | * |
3059 // | distribute simd | parallel for | * |
3060 // | distribute simd |parallel for simd| * |
3061 // | distribute simd |parallel sections| * |
3062 // | distribute simd | task | * |
3063 // | distribute simd | taskyield | * |
3064 // | distribute simd | barrier | * |
3065 // | distribute simd | taskwait | * |
3066 // | distribute simd | taskgroup | * |
3067 // | distribute simd | flush | * |
3068 // | distribute simd | ordered | + |
3069 // | distribute simd | atomic | * |
3070 // | distribute simd | target | * |
3071 // | distribute simd | target parallel | * |
3072 // | distribute simd | target parallel | * |
3073 // | | for | |
3074 // | distribute simd | target enter | * |
3075 // | | data | |
3076 // | distribute simd | target exit | * |
3077 // | | data | |
3078 // | distribute simd | teams | * |
3079 // | distribute simd | cancellation | + |
3080 // | | point | |
3081 // | distribute simd | cancel | + |
3082 // | distribute simd | taskloop | * |
3083 // | distribute simd | taskloop simd | * |
3084 // | distribute simd | distribute | |
3085 // | distribute simd | distribute | * |
3086 // | | parallel for | |
3087 // | distribute simd | distribute | * |
3088 // | |parallel for simd| |
3089 // | distribute simd | distribute simd | * |
Kelvin Lia579b912016-07-14 02:54:56 +00003090 // | distribute simd | target parallel | * |
3091 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00003092 // | distribute simd | target simd | * |
Kelvin Li02532872016-08-05 14:37:37 +00003093 // | distribute simd | teams distribute| * |
Kelvin Lia579b912016-07-14 02:54:56 +00003094 // +------------------+-----------------+------------------------------------+
3095 // | target parallel | parallel | * |
3096 // | for simd | | |
3097 // | target parallel | for | * |
3098 // | for simd | | |
3099 // | target parallel | for simd | * |
3100 // | for simd | | |
3101 // | target parallel | master | * |
3102 // | for simd | | |
3103 // | target parallel | critical | * |
3104 // | for simd | | |
3105 // | target parallel | simd | ! |
3106 // | for simd | | |
3107 // | target parallel | sections | * |
3108 // | for simd | | |
3109 // | target parallel | section | * |
3110 // | for simd | | |
3111 // | target parallel | single | * |
3112 // | for simd | | |
3113 // | target parallel | parallel for | * |
3114 // | for simd | | |
3115 // | target parallel |parallel for simd| * |
3116 // | for simd | | |
3117 // | target parallel |parallel sections| * |
3118 // | for simd | | |
3119 // | target parallel | task | * |
3120 // | for simd | | |
3121 // | target parallel | taskyield | * |
3122 // | for simd | | |
3123 // | target parallel | barrier | * |
3124 // | for simd | | |
3125 // | target parallel | taskwait | * |
3126 // | for simd | | |
3127 // | target parallel | taskgroup | * |
3128 // | for simd | | |
3129 // | target parallel | flush | * |
3130 // | for simd | | |
3131 // | target parallel | ordered | + (with simd clause) |
3132 // | for simd | | |
3133 // | target parallel | atomic | * |
3134 // | for simd | | |
3135 // | target parallel | target | * |
3136 // | for simd | | |
3137 // | target parallel | target parallel | * |
3138 // | for simd | | |
3139 // | target parallel | target parallel | * |
3140 // | for simd | for | |
3141 // | target parallel | target enter | * |
3142 // | for simd | data | |
3143 // | target parallel | target exit | * |
3144 // | for simd | data | |
3145 // | target parallel | teams | * |
3146 // | for simd | | |
3147 // | target parallel | cancellation | * |
3148 // | for simd | point | |
3149 // | target parallel | cancel | * |
3150 // | for simd | | |
3151 // | target parallel | taskloop | * |
3152 // | for simd | | |
3153 // | target parallel | taskloop simd | * |
3154 // | for simd | | |
3155 // | target parallel | distribute | * |
3156 // | for simd | | |
3157 // | target parallel | distribute | * |
3158 // | for simd | parallel for | |
3159 // | target parallel | distribute | * |
3160 // | for simd |parallel for simd| |
3161 // | target parallel | distribute simd | * |
3162 // | for simd | | |
3163 // | target parallel | target parallel | * |
3164 // | for simd | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00003165 // | target parallel | target simd | * |
3166 // | for simd | | |
Kelvin Li02532872016-08-05 14:37:37 +00003167 // | target parallel | teams distribute| * |
3168 // | for simd | | |
Kelvin Li986330c2016-07-20 22:57:10 +00003169 // +------------------+-----------------+------------------------------------+
3170 // | target simd | parallel | |
3171 // | target simd | for | |
3172 // | target simd | for simd | |
3173 // | target simd | master | |
3174 // | target simd | critical | |
3175 // | target simd | simd | |
3176 // | target simd | sections | |
3177 // | target simd | section | |
3178 // | target simd | single | |
3179 // | target simd | parallel for | |
3180 // | target simd |parallel for simd| |
3181 // | target simd |parallel sections| |
3182 // | target simd | task | |
3183 // | target simd | taskyield | |
3184 // | target simd | barrier | |
3185 // | target simd | taskwait | |
3186 // | target simd | taskgroup | |
3187 // | target simd | flush | |
3188 // | target simd | ordered | + (with simd clause) |
3189 // | target simd | atomic | |
3190 // | target simd | target | |
3191 // | target simd | target parallel | |
3192 // | target simd | target parallel | |
3193 // | | for | |
3194 // | target simd | target enter | |
3195 // | | data | |
3196 // | target simd | target exit | |
3197 // | | data | |
3198 // | target simd | teams | |
3199 // | target simd | cancellation | |
3200 // | | point | |
3201 // | target simd | cancel | |
3202 // | target simd | taskloop | |
3203 // | target simd | taskloop simd | |
3204 // | target simd | distribute | |
3205 // | target simd | distribute | |
3206 // | | parallel for | |
3207 // | target simd | distribute | |
3208 // | |parallel for simd| |
3209 // | target simd | distribute simd | |
3210 // | target simd | target parallel | |
3211 // | | for simd | |
3212 // | target simd | target simd | |
Kelvin Li02532872016-08-05 14:37:37 +00003213 // | target simd | teams distribute| |
3214 // +------------------+-----------------+------------------------------------+
3215 // | teams distribute | parallel | |
3216 // | teams distribute | for | |
3217 // | teams distribute | for simd | |
3218 // | teams distribute | master | |
3219 // | teams distribute | critical | |
3220 // | teams distribute | simd | |
3221 // | teams distribute | sections | |
3222 // | teams distribute | section | |
3223 // | teams distribute | single | |
3224 // | teams distribute | parallel for | |
3225 // | teams distribute |parallel for simd| |
3226 // | teams distribute |parallel sections| |
3227 // | teams distribute | task | |
3228 // | teams distribute | taskyield | |
3229 // | teams distribute | barrier | |
3230 // | teams distribute | taskwait | |
3231 // | teams distribute | taskgroup | |
3232 // | teams distribute | flush | |
3233 // | teams distribute | ordered | + (with simd clause) |
3234 // | teams distribute | atomic | |
3235 // | teams distribute | target | |
3236 // | teams distribute | target parallel | |
3237 // | teams distribute | target parallel | |
3238 // | | for | |
3239 // | teams distribute | target enter | |
3240 // | | data | |
3241 // | teams distribute | target exit | |
3242 // | | data | |
3243 // | teams distribute | teams | |
3244 // | teams distribute | cancellation | |
3245 // | | point | |
3246 // | teams distribute | cancel | |
3247 // | teams distribute | taskloop | |
3248 // | teams distribute | taskloop simd | |
3249 // | teams distribute | distribute | |
3250 // | teams distribute | distribute | |
3251 // | | parallel for | |
3252 // | teams distribute | distribute | |
3253 // | |parallel for simd| |
3254 // | teams distribute | distribute simd | |
3255 // | teams distribute | target parallel | |
3256 // | | for simd | |
3257 // | teams distribute | teams distribute| |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003258 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00003259 if (Stack->getCurScope()) {
3260 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003261 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003262 bool NestingProhibited = false;
3263 bool CloseNesting = true;
Kelvin Li2b51f722016-07-26 04:32:50 +00003264 bool OrphanSeen = false;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003265 enum {
3266 NoRecommend,
3267 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00003268 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003269 ShouldBeInTargetRegion,
3270 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003271 } Recommend = NoRecommend;
Kelvin Lifd8b5742016-07-01 14:30:25 +00003272 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003273 // OpenMP [2.16, Nesting of Regions]
3274 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003275 // OpenMP [2.8.1,simd Construct, Restrictions]
Kelvin Lifd8b5742016-07-01 14:30:25 +00003276 // An ordered construct with the simd clause is the only OpenMP
3277 // construct that can appear in the simd region.
3278 // Allowing a SIMD consruct nested in another SIMD construct is an
3279 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
3280 // message.
3281 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
3282 ? diag::err_omp_prohibited_region_simd
3283 : diag::warn_omp_nesting_simd);
3284 return CurrentRegion != OMPD_simd;
Alexey Bataev549210e2014-06-24 04:39:47 +00003285 }
Alexey Bataev0162e452014-07-22 10:10:35 +00003286 if (ParentRegion == OMPD_atomic) {
3287 // OpenMP [2.16, Nesting of Regions]
3288 // OpenMP constructs may not be nested inside an atomic region.
3289 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
3290 return true;
3291 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003292 if (CurrentRegion == OMPD_section) {
3293 // OpenMP [2.7.2, sections Construct, Restrictions]
3294 // Orphaned section directives are prohibited. That is, the section
3295 // directives must appear within the sections construct and must not be
3296 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003297 if (ParentRegion != OMPD_sections &&
3298 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003299 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
3300 << (ParentRegion != OMPD_unknown)
3301 << getOpenMPDirectiveName(ParentRegion);
3302 return true;
3303 }
3304 return false;
3305 }
Kelvin Li2b51f722016-07-26 04:32:50 +00003306 // Allow some constructs (except teams) to be orphaned (they could be
3307 // used in functions, called from OpenMP regions with the required
3308 // preconditions).
3309 if (ParentRegion == OMPD_unknown && !isOpenMPTeamsDirective(CurrentRegion))
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003310 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00003311 if (CurrentRegion == OMPD_cancellation_point ||
3312 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003313 // OpenMP [2.16, Nesting of Regions]
3314 // A cancellation point construct for which construct-type-clause is
3315 // taskgroup must be nested inside a task construct. A cancellation
3316 // point construct for which construct-type-clause is not taskgroup must
3317 // be closely nested inside an OpenMP construct that matches the type
3318 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00003319 // A cancel construct for which construct-type-clause is taskgroup must be
3320 // nested inside a task construct. A cancel construct for which
3321 // construct-type-clause is not taskgroup must be closely nested inside an
3322 // OpenMP construct that matches the type specified in
3323 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003324 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003325 !((CancelRegion == OMPD_parallel &&
3326 (ParentRegion == OMPD_parallel ||
3327 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00003328 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003329 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
3330 ParentRegion == OMPD_target_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003331 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
3332 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00003333 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
3334 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003335 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00003336 // OpenMP [2.16, Nesting of Regions]
3337 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003338 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00003339 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003340 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003341 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
3342 // OpenMP [2.16, Nesting of Regions]
3343 // A critical region may not be nested (closely or otherwise) inside a
3344 // critical region with the same name. Note that this restriction is not
3345 // sufficient to prevent deadlock.
3346 SourceLocation PreviousCriticalLoc;
3347 bool DeadLock =
3348 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
3349 OpenMPDirectiveKind K,
3350 const DeclarationNameInfo &DNI,
3351 SourceLocation Loc)
3352 ->bool {
3353 if (K == OMPD_critical &&
3354 DNI.getName() == CurrentName.getName()) {
3355 PreviousCriticalLoc = Loc;
3356 return true;
3357 } else
3358 return false;
3359 },
3360 false /* skip top directive */);
3361 if (DeadLock) {
3362 SemaRef.Diag(StartLoc,
3363 diag::err_omp_prohibited_region_critical_same_name)
3364 << CurrentName.getName();
3365 if (PreviousCriticalLoc.isValid())
3366 SemaRef.Diag(PreviousCriticalLoc,
3367 diag::note_omp_previous_critical_region);
3368 return true;
3369 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003370 } else if (CurrentRegion == OMPD_barrier) {
3371 // OpenMP [2.16, Nesting of Regions]
3372 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003373 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003374 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3375 isOpenMPTaskingDirective(ParentRegion) ||
3376 ParentRegion == OMPD_master ||
3377 ParentRegion == OMPD_critical ||
3378 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00003379 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00003380 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003381 // OpenMP [2.16, Nesting of Regions]
3382 // A worksharing region may not be closely nested inside a worksharing,
3383 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003384 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3385 isOpenMPTaskingDirective(ParentRegion) ||
3386 ParentRegion == OMPD_master ||
3387 ParentRegion == OMPD_critical ||
3388 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003389 Recommend = ShouldBeInParallelRegion;
3390 } else if (CurrentRegion == OMPD_ordered) {
3391 // OpenMP [2.16, Nesting of Regions]
3392 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00003393 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003394 // An ordered region must be closely nested inside a loop region (or
3395 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003396 // OpenMP [2.8.1,simd Construct, Restrictions]
3397 // An ordered construct with the simd clause is the only OpenMP construct
3398 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003399 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003400 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003401 !(isOpenMPSimdDirective(ParentRegion) ||
3402 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003403 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003404 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
3405 // OpenMP [2.16, Nesting of Regions]
3406 // If specified, a teams construct must be contained within a target
3407 // construct.
3408 NestingProhibited = ParentRegion != OMPD_target;
Kelvin Li2b51f722016-07-26 04:32:50 +00003409 OrphanSeen = ParentRegion == OMPD_unknown;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003410 Recommend = ShouldBeInTargetRegion;
3411 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
3412 }
Kelvin Li02532872016-08-05 14:37:37 +00003413 if (!NestingProhibited && ParentRegion == OMPD_teams) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00003414 // OpenMP [2.16, Nesting of Regions]
3415 // distribute, parallel, parallel sections, parallel workshare, and the
3416 // parallel loop and parallel loop SIMD constructs are the only OpenMP
3417 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003418 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
3419 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00003420 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003421 }
Kelvin Li02532872016-08-05 14:37:37 +00003422 if (!NestingProhibited &&
3423 isOpenMPNestingDistributeDirective(CurrentRegion)) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003424 // OpenMP 4.5 [2.17 Nesting of Regions]
3425 // The region associated with the distribute construct must be strictly
3426 // nested inside a teams region
Kelvin Li02532872016-08-05 14:37:37 +00003427 NestingProhibited = ParentRegion != OMPD_teams;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003428 Recommend = ShouldBeInTeamsRegion;
3429 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003430 if (!NestingProhibited &&
3431 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
3432 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
3433 // OpenMP 4.5 [2.17 Nesting of Regions]
3434 // If a target, target update, target data, target enter data, or
3435 // target exit data construct is encountered during execution of a
3436 // target region, the behavior is unspecified.
3437 NestingProhibited = Stack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +00003438 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
3439 SourceLocation) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003440 if (isOpenMPTargetExecutionDirective(K)) {
3441 OffendingRegion = K;
3442 return true;
3443 } else
3444 return false;
3445 },
3446 false /* don't skip top directive */);
3447 CloseNesting = false;
3448 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003449 if (NestingProhibited) {
Kelvin Li2b51f722016-07-26 04:32:50 +00003450 if (OrphanSeen) {
3451 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
3452 << getOpenMPDirectiveName(CurrentRegion) << Recommend;
3453 } else {
3454 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
3455 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
3456 << Recommend << getOpenMPDirectiveName(CurrentRegion);
3457 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003458 return true;
3459 }
3460 }
3461 return false;
3462}
3463
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003464static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
3465 ArrayRef<OMPClause *> Clauses,
3466 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
3467 bool ErrorFound = false;
3468 unsigned NamedModifiersNumber = 0;
3469 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
3470 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00003471 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003472 for (const auto *C : Clauses) {
3473 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
3474 // At most one if clause without a directive-name-modifier can appear on
3475 // the directive.
3476 OpenMPDirectiveKind CurNM = IC->getNameModifier();
3477 if (FoundNameModifiers[CurNM]) {
3478 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
3479 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
3480 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
3481 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003482 } else if (CurNM != OMPD_unknown) {
3483 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003484 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003485 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003486 FoundNameModifiers[CurNM] = IC;
3487 if (CurNM == OMPD_unknown)
3488 continue;
3489 // Check if the specified name modifier is allowed for the current
3490 // directive.
3491 // At most one if clause with the particular directive-name-modifier can
3492 // appear on the directive.
3493 bool MatchFound = false;
3494 for (auto NM : AllowedNameModifiers) {
3495 if (CurNM == NM) {
3496 MatchFound = true;
3497 break;
3498 }
3499 }
3500 if (!MatchFound) {
3501 S.Diag(IC->getNameModifierLoc(),
3502 diag::err_omp_wrong_if_directive_name_modifier)
3503 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
3504 ErrorFound = true;
3505 }
3506 }
3507 }
3508 // If any if clause on the directive includes a directive-name-modifier then
3509 // all if clauses on the directive must include a directive-name-modifier.
3510 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3511 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
3512 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
3513 diag::err_omp_no_more_if_clause);
3514 } else {
3515 std::string Values;
3516 std::string Sep(", ");
3517 unsigned AllowedCnt = 0;
3518 unsigned TotalAllowedNum =
3519 AllowedNameModifiers.size() - NamedModifiersNumber;
3520 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3521 ++Cnt) {
3522 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3523 if (!FoundNameModifiers[NM]) {
3524 Values += "'";
3525 Values += getOpenMPDirectiveName(NM);
3526 Values += "'";
3527 if (AllowedCnt + 2 == TotalAllowedNum)
3528 Values += " or ";
3529 else if (AllowedCnt + 1 != TotalAllowedNum)
3530 Values += Sep;
3531 ++AllowedCnt;
3532 }
3533 }
3534 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
3535 diag::err_omp_unnamed_if_clause)
3536 << (TotalAllowedNum > 1) << Values;
3537 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00003538 for (auto Loc : NameModifierLoc) {
3539 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3540 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003541 ErrorFound = true;
3542 }
3543 return ErrorFound;
3544}
3545
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003546StmtResult Sema::ActOnOpenMPExecutableDirective(
3547 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3548 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3549 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003550 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003551 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
3552 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00003553 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003554
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003555 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003556 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003557 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00003558 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00003559 if (AStmt) {
3560 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3561
3562 // Check default data sharing attributes for referenced variables.
3563 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
3564 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
3565 if (DSAChecker.isErrorFound())
3566 return StmtError();
3567 // Generate list of implicitly defined firstprivate variables.
3568 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00003569
3570 if (!DSAChecker.getImplicitFirstprivate().empty()) {
3571 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
3572 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
3573 SourceLocation(), SourceLocation())) {
3574 ClausesWithImplicit.push_back(Implicit);
3575 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
3576 DSAChecker.getImplicitFirstprivate().size();
3577 } else
3578 ErrorFound = true;
3579 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003580 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003581
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003582 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003583 switch (Kind) {
3584 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00003585 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3586 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003587 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003588 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003589 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003590 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3591 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003592 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003593 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003594 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3595 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003596 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003597 case OMPD_for_simd:
3598 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3599 EndLoc, VarsWithInheritedDSA);
3600 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003601 case OMPD_sections:
3602 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3603 EndLoc);
3604 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003605 case OMPD_section:
3606 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003607 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003608 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3609 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003610 case OMPD_single:
3611 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3612 EndLoc);
3613 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003614 case OMPD_master:
3615 assert(ClausesWithImplicit.empty() &&
3616 "No clauses are allowed for 'omp master' directive");
3617 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3618 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003619 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003620 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3621 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003622 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003623 case OMPD_parallel_for:
3624 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3625 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003626 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003627 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003628 case OMPD_parallel_for_simd:
3629 Res = ActOnOpenMPParallelForSimdDirective(
3630 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003631 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003632 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003633 case OMPD_parallel_sections:
3634 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3635 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003636 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003637 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003638 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003639 Res =
3640 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003641 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003642 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003643 case OMPD_taskyield:
3644 assert(ClausesWithImplicit.empty() &&
3645 "No clauses are allowed for 'omp taskyield' directive");
3646 assert(AStmt == nullptr &&
3647 "No associated statement allowed for 'omp taskyield' directive");
3648 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3649 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003650 case OMPD_barrier:
3651 assert(ClausesWithImplicit.empty() &&
3652 "No clauses are allowed for 'omp barrier' directive");
3653 assert(AStmt == nullptr &&
3654 "No associated statement allowed for 'omp barrier' directive");
3655 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3656 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003657 case OMPD_taskwait:
3658 assert(ClausesWithImplicit.empty() &&
3659 "No clauses are allowed for 'omp taskwait' directive");
3660 assert(AStmt == nullptr &&
3661 "No associated statement allowed for 'omp taskwait' directive");
3662 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3663 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003664 case OMPD_taskgroup:
3665 assert(ClausesWithImplicit.empty() &&
3666 "No clauses are allowed for 'omp taskgroup' directive");
3667 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3668 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003669 case OMPD_flush:
3670 assert(AStmt == nullptr &&
3671 "No associated statement allowed for 'omp flush' directive");
3672 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3673 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003674 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003675 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3676 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003677 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003678 case OMPD_atomic:
3679 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3680 EndLoc);
3681 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003682 case OMPD_teams:
3683 Res =
3684 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3685 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003686 case OMPD_target:
3687 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3688 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003689 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003690 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003691 case OMPD_target_parallel:
3692 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3693 StartLoc, EndLoc);
3694 AllowedNameModifiers.push_back(OMPD_target);
3695 AllowedNameModifiers.push_back(OMPD_parallel);
3696 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003697 case OMPD_target_parallel_for:
3698 Res = ActOnOpenMPTargetParallelForDirective(
3699 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3700 AllowedNameModifiers.push_back(OMPD_target);
3701 AllowedNameModifiers.push_back(OMPD_parallel);
3702 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003703 case OMPD_cancellation_point:
3704 assert(ClausesWithImplicit.empty() &&
3705 "No clauses are allowed for 'omp cancellation point' directive");
3706 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3707 "cancellation point' directive");
3708 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3709 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003710 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003711 assert(AStmt == nullptr &&
3712 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003713 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3714 CancelRegion);
3715 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003716 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003717 case OMPD_target_data:
3718 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3719 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003720 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003721 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003722 case OMPD_target_enter_data:
3723 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3724 EndLoc);
3725 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3726 break;
Samuel Antao72590762016-01-19 20:04:50 +00003727 case OMPD_target_exit_data:
3728 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3729 EndLoc);
3730 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3731 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003732 case OMPD_taskloop:
3733 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3734 EndLoc, VarsWithInheritedDSA);
3735 AllowedNameModifiers.push_back(OMPD_taskloop);
3736 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003737 case OMPD_taskloop_simd:
3738 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3739 EndLoc, VarsWithInheritedDSA);
3740 AllowedNameModifiers.push_back(OMPD_taskloop);
3741 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003742 case OMPD_distribute:
3743 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3744 EndLoc, VarsWithInheritedDSA);
3745 break;
Samuel Antao686c70c2016-05-26 17:30:50 +00003746 case OMPD_target_update:
3747 assert(!AStmt && "Statement is not allowed for target update");
3748 Res =
3749 ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, EndLoc);
3750 AllowedNameModifiers.push_back(OMPD_target_update);
3751 break;
Carlo Bertolli9925f152016-06-27 14:55:37 +00003752 case OMPD_distribute_parallel_for:
3753 Res = ActOnOpenMPDistributeParallelForDirective(
3754 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3755 AllowedNameModifiers.push_back(OMPD_parallel);
3756 break;
Kelvin Li4a39add2016-07-05 05:00:15 +00003757 case OMPD_distribute_parallel_for_simd:
3758 Res = ActOnOpenMPDistributeParallelForSimdDirective(
3759 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3760 AllowedNameModifiers.push_back(OMPD_parallel);
3761 break;
Kelvin Li787f3fc2016-07-06 04:45:38 +00003762 case OMPD_distribute_simd:
3763 Res = ActOnOpenMPDistributeSimdDirective(
3764 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3765 break;
Kelvin Lia579b912016-07-14 02:54:56 +00003766 case OMPD_target_parallel_for_simd:
3767 Res = ActOnOpenMPTargetParallelForSimdDirective(
3768 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3769 AllowedNameModifiers.push_back(OMPD_target);
3770 AllowedNameModifiers.push_back(OMPD_parallel);
3771 break;
Kelvin Li986330c2016-07-20 22:57:10 +00003772 case OMPD_target_simd:
3773 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3774 EndLoc, VarsWithInheritedDSA);
3775 AllowedNameModifiers.push_back(OMPD_target);
3776 break;
Kelvin Li02532872016-08-05 14:37:37 +00003777 case OMPD_teams_distribute:
3778 Res = ActOnOpenMPTeamsDistributeDirective(ClausesWithImplicit, AStmt,
3779 StartLoc, EndLoc,
3780 VarsWithInheritedDSA);
3781 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003782 case OMPD_declare_target:
3783 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003784 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003785 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003786 case OMPD_declare_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003787 llvm_unreachable("OpenMP Directive is not allowed");
3788 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003789 llvm_unreachable("Unknown OpenMP directive");
3790 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003791
Alexey Bataev4acb8592014-07-07 13:01:15 +00003792 for (auto P : VarsWithInheritedDSA) {
3793 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3794 << P.first << P.second->getSourceRange();
3795 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003796 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3797
3798 if (!AllowedNameModifiers.empty())
3799 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3800 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003801
Alexey Bataeved09d242014-05-28 05:53:51 +00003802 if (ErrorFound)
3803 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003804 return Res;
3805}
3806
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003807Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3808 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00003809 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00003810 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3811 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003812 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00003813 assert(Linears.size() == LinModifiers.size());
3814 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00003815 if (!DG || DG.get().isNull())
3816 return DeclGroupPtrTy();
3817
3818 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00003819 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003820 return DG;
3821 }
3822 auto *ADecl = DG.get().getSingleDecl();
3823 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3824 ADecl = FTD->getTemplatedDecl();
3825
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003826 auto *FD = dyn_cast<FunctionDecl>(ADecl);
3827 if (!FD) {
3828 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003829 return DeclGroupPtrTy();
3830 }
3831
Alexey Bataev2af33e32016-04-07 12:45:37 +00003832 // OpenMP [2.8.2, declare simd construct, Description]
3833 // The parameter of the simdlen clause must be a constant positive integer
3834 // expression.
3835 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003836 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00003837 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003838 // OpenMP [2.8.2, declare simd construct, Description]
3839 // The special this pointer can be used as if was one of the arguments to the
3840 // function in any of the linear, aligned, or uniform clauses.
3841 // The uniform clause declares one or more arguments to have an invariant
3842 // value for all concurrent invocations of the function in the execution of a
3843 // single SIMD loop.
Alexey Bataevecba70f2016-04-12 11:02:11 +00003844 llvm::DenseMap<Decl *, Expr *> UniformedArgs;
3845 Expr *UniformedLinearThis = nullptr;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003846 for (auto *E : Uniforms) {
3847 E = E->IgnoreParenImpCasts();
3848 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3849 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
3850 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3851 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00003852 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
3853 UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E));
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003854 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003855 }
3856 if (isa<CXXThisExpr>(E)) {
3857 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003858 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003859 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003860 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3861 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00003862 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00003863 // OpenMP [2.8.2, declare simd construct, Description]
3864 // The aligned clause declares that the object to which each list item points
3865 // is aligned to the number of bytes expressed in the optional parameter of
3866 // the aligned clause.
3867 // The special this pointer can be used as if was one of the arguments to the
3868 // function in any of the linear, aligned, or uniform clauses.
3869 // The type of list items appearing in the aligned clause must be array,
3870 // pointer, reference to array, or reference to pointer.
3871 llvm::DenseMap<Decl *, Expr *> AlignedArgs;
3872 Expr *AlignedThis = nullptr;
3873 for (auto *E : Aligneds) {
3874 E = E->IgnoreParenImpCasts();
3875 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3876 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3877 auto *CanonPVD = PVD->getCanonicalDecl();
3878 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3879 FD->getParamDecl(PVD->getFunctionScopeIndex())
3880 ->getCanonicalDecl() == CanonPVD) {
3881 // OpenMP [2.8.1, simd construct, Restrictions]
3882 // A list-item cannot appear in more than one aligned clause.
3883 if (AlignedArgs.count(CanonPVD) > 0) {
3884 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3885 << 1 << E->getSourceRange();
3886 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3887 diag::note_omp_explicit_dsa)
3888 << getOpenMPClauseName(OMPC_aligned);
3889 continue;
3890 }
3891 AlignedArgs[CanonPVD] = E;
3892 QualType QTy = PVD->getType()
3893 .getNonReferenceType()
3894 .getUnqualifiedType()
3895 .getCanonicalType();
3896 const Type *Ty = QTy.getTypePtrOrNull();
3897 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3898 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3899 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3900 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3901 }
3902 continue;
3903 }
3904 }
3905 if (isa<CXXThisExpr>(E)) {
3906 if (AlignedThis) {
3907 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3908 << 2 << E->getSourceRange();
3909 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3910 << getOpenMPClauseName(OMPC_aligned);
3911 }
3912 AlignedThis = E;
3913 continue;
3914 }
3915 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3916 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3917 }
3918 // The optional parameter of the aligned clause, alignment, must be a constant
3919 // positive integer expression. If no optional parameter is specified,
3920 // implementation-defined default alignments for SIMD instructions on the
3921 // target platforms are assumed.
3922 SmallVector<Expr *, 4> NewAligns;
3923 for (auto *E : Alignments) {
3924 ExprResult Align;
3925 if (E)
3926 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3927 NewAligns.push_back(Align.get());
3928 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00003929 // OpenMP [2.8.2, declare simd construct, Description]
3930 // The linear clause declares one or more list items to be private to a SIMD
3931 // lane and to have a linear relationship with respect to the iteration space
3932 // of a loop.
3933 // The special this pointer can be used as if was one of the arguments to the
3934 // function in any of the linear, aligned, or uniform clauses.
3935 // When a linear-step expression is specified in a linear clause it must be
3936 // either a constant integer expression or an integer-typed parameter that is
3937 // specified in a uniform clause on the directive.
3938 llvm::DenseMap<Decl *, Expr *> LinearArgs;
3939 const bool IsUniformedThis = UniformedLinearThis != nullptr;
3940 auto MI = LinModifiers.begin();
3941 for (auto *E : Linears) {
3942 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3943 ++MI;
3944 E = E->IgnoreParenImpCasts();
3945 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3946 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3947 auto *CanonPVD = PVD->getCanonicalDecl();
3948 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3949 FD->getParamDecl(PVD->getFunctionScopeIndex())
3950 ->getCanonicalDecl() == CanonPVD) {
3951 // OpenMP [2.15.3.7, linear Clause, Restrictions]
3952 // A list-item cannot appear in more than one linear clause.
3953 if (LinearArgs.count(CanonPVD) > 0) {
3954 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3955 << getOpenMPClauseName(OMPC_linear)
3956 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3957 Diag(LinearArgs[CanonPVD]->getExprLoc(),
3958 diag::note_omp_explicit_dsa)
3959 << getOpenMPClauseName(OMPC_linear);
3960 continue;
3961 }
3962 // Each argument can appear in at most one uniform or linear clause.
3963 if (UniformedArgs.count(CanonPVD) > 0) {
3964 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3965 << getOpenMPClauseName(OMPC_linear)
3966 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3967 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3968 diag::note_omp_explicit_dsa)
3969 << getOpenMPClauseName(OMPC_uniform);
3970 continue;
3971 }
3972 LinearArgs[CanonPVD] = E;
3973 if (E->isValueDependent() || E->isTypeDependent() ||
3974 E->isInstantiationDependent() ||
3975 E->containsUnexpandedParameterPack())
3976 continue;
3977 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3978 PVD->getOriginalType());
3979 continue;
3980 }
3981 }
3982 if (isa<CXXThisExpr>(E)) {
3983 if (UniformedLinearThis) {
3984 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3985 << getOpenMPClauseName(OMPC_linear)
3986 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3987 << E->getSourceRange();
3988 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3989 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3990 : OMPC_linear);
3991 continue;
3992 }
3993 UniformedLinearThis = E;
3994 if (E->isValueDependent() || E->isTypeDependent() ||
3995 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3996 continue;
3997 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3998 E->getType());
3999 continue;
4000 }
4001 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
4002 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
4003 }
4004 Expr *Step = nullptr;
4005 Expr *NewStep = nullptr;
4006 SmallVector<Expr *, 4> NewSteps;
4007 for (auto *E : Steps) {
4008 // Skip the same step expression, it was checked already.
4009 if (Step == E || !E) {
4010 NewSteps.push_back(E ? NewStep : nullptr);
4011 continue;
4012 }
4013 Step = E;
4014 if (auto *DRE = dyn_cast<DeclRefExpr>(Step))
4015 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
4016 auto *CanonPVD = PVD->getCanonicalDecl();
4017 if (UniformedArgs.count(CanonPVD) == 0) {
4018 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
4019 << Step->getSourceRange();
4020 } else if (E->isValueDependent() || E->isTypeDependent() ||
4021 E->isInstantiationDependent() ||
4022 E->containsUnexpandedParameterPack() ||
4023 CanonPVD->getType()->hasIntegerRepresentation())
4024 NewSteps.push_back(Step);
4025 else {
4026 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
4027 << Step->getSourceRange();
4028 }
4029 continue;
4030 }
4031 NewStep = Step;
4032 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
4033 !Step->isInstantiationDependent() &&
4034 !Step->containsUnexpandedParameterPack()) {
4035 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
4036 .get();
4037 if (NewStep)
4038 NewStep = VerifyIntegerConstantExpression(NewStep).get();
4039 }
4040 NewSteps.push_back(NewStep);
4041 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00004042 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
4043 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00004044 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00004045 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
4046 const_cast<Expr **>(Linears.data()), Linears.size(),
4047 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
4048 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00004049 ADecl->addAttr(NewAttr);
4050 return ConvertDeclToDeclGroup(ADecl);
4051}
4052
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004053StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
4054 Stmt *AStmt,
4055 SourceLocation StartLoc,
4056 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004057 if (!AStmt)
4058 return StmtError();
4059
Alexey Bataev9959db52014-05-06 10:08:46 +00004060 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4061 // 1.2.2 OpenMP Language Terminology
4062 // Structured block - An executable statement with a single entry at the
4063 // top and a single exit at the bottom.
4064 // The point of exit cannot be a branch out of the structured block.
4065 // longjmp() and throw() must not violate the entry/exit criteria.
4066 CS->getCapturedDecl()->setNothrow();
4067
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004068 getCurFunction()->setHasBranchProtectedScope();
4069
Alexey Bataev25e5b442015-09-15 12:52:43 +00004070 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4071 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004072}
4073
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004074namespace {
4075/// \brief Helper class for checking canonical form of the OpenMP loops and
4076/// extracting iteration space of each loop in the loop nest, that will be used
4077/// for IR generation.
4078class OpenMPIterationSpaceChecker {
4079 /// \brief Reference to Sema.
4080 Sema &SemaRef;
4081 /// \brief A location for diagnostics (when there is no some better location).
4082 SourceLocation DefaultLoc;
4083 /// \brief A location for diagnostics (when increment is not compatible).
4084 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004085 /// \brief A source location for referring to loop init later.
4086 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004087 /// \brief A source location for referring to condition later.
4088 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004089 /// \brief A source location for referring to increment later.
4090 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004091 /// \brief Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004092 ValueDecl *LCDecl = nullptr;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00004093 /// \brief Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004094 Expr *LCRef = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004095 /// \brief Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004096 Expr *LB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004097 /// \brief Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004098 Expr *UB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004099 /// \brief Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004100 Expr *Step = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004101 /// \brief This flag is true when condition is one of:
4102 /// Var < UB
4103 /// Var <= UB
4104 /// UB > Var
4105 /// UB >= Var
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004106 bool TestIsLessOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004107 /// \brief This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004108 bool TestIsStrictOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004109 /// \brief This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004110 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004111
4112public:
4113 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004114 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004115 /// \brief Check init-expr for canonical loop form and save loop counter
4116 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00004117 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004118 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
4119 /// for less/greater and for strict/non-strict comparison.
4120 bool CheckCond(Expr *S);
4121 /// \brief Check incr-expr for canonical loop form and return true if it
4122 /// does not conform, otherwise save loop step (#Step).
4123 bool CheckInc(Expr *S);
4124 /// \brief Return the loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004125 ValueDecl *GetLoopDecl() const { return LCDecl; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00004126 /// \brief Return the reference expression to loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004127 Expr *GetLoopDeclRefExpr() const { return LCRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004128 /// \brief Source range of the loop init.
4129 SourceRange GetInitSrcRange() const { return InitSrcRange; }
4130 /// \brief Source range of the loop condition.
4131 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
4132 /// \brief Source range of the loop increment.
4133 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
4134 /// \brief True if the step should be subtracted.
4135 bool ShouldSubtractStep() const { return SubtractStep; }
4136 /// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004137 Expr *
4138 BuildNumIterations(Scope *S, const bool LimitedType,
4139 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00004140 /// \brief Build the precondition expression for the loops.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004141 Expr *BuildPreCond(Scope *S, Expr *Cond,
4142 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004143 /// \brief Build reference expression to the counter be used for codegen.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004144 DeclRefExpr *BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures,
4145 DSAStackTy &DSA) const;
Alexey Bataeva8899172015-08-06 12:30:57 +00004146 /// \brief Build reference expression to the private counter be used for
4147 /// codegen.
4148 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004149 /// \brief Build initization of the counter be used for codegen.
4150 Expr *BuildCounterInit() const;
4151 /// \brief Build step of the counter be used for codegen.
4152 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004153 /// \brief Return true if any expression is dependent.
4154 bool Dependent() const;
4155
4156private:
4157 /// \brief Check the right-hand side of an assignment in the increment
4158 /// expression.
4159 bool CheckIncRHS(Expr *RHS);
4160 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004161 bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004162 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00004163 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00004164 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004165 /// \brief Helper to set loop increment.
4166 bool SetStep(Expr *NewStep, bool Subtract);
4167};
4168
4169bool OpenMPIterationSpaceChecker::Dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004170 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004171 assert(!LB && !UB && !Step);
4172 return false;
4173 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004174 return LCDecl->getType()->isDependentType() ||
4175 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
4176 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004177}
4178
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004179static Expr *getExprAsWritten(Expr *E) {
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004180 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
4181 E = ExprTemp->getSubExpr();
4182
4183 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
4184 E = MTE->GetTemporaryExpr();
4185
4186 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
4187 E = Binder->getSubExpr();
4188
4189 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
4190 E = ICE->getSubExprAsWritten();
4191 return E->IgnoreParens();
4192}
4193
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004194bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl,
4195 Expr *NewLCRefExpr,
4196 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004197 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004198 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00004199 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004200 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004201 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004202 LCDecl = getCanonicalDecl(NewLCDecl);
4203 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004204 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
4205 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004206 if ((Ctor->isCopyOrMoveConstructor() ||
4207 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4208 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004209 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004210 LB = NewLB;
4211 return false;
4212}
4213
4214bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00004215 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004216 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004217 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
4218 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004219 if (!NewUB)
4220 return true;
4221 UB = NewUB;
4222 TestIsLessOp = LessOp;
4223 TestIsStrictOp = StrictOp;
4224 ConditionSrcRange = SR;
4225 ConditionLoc = SL;
4226 return false;
4227}
4228
4229bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
4230 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004231 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004232 if (!NewStep)
4233 return true;
4234 if (!NewStep->isValueDependent()) {
4235 // Check that the step is integer expression.
4236 SourceLocation StepLoc = NewStep->getLocStart();
4237 ExprResult Val =
4238 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
4239 if (Val.isInvalid())
4240 return true;
4241 NewStep = Val.get();
4242
4243 // OpenMP [2.6, Canonical Loop Form, Restrictions]
4244 // If test-expr is of form var relational-op b and relational-op is < or
4245 // <= then incr-expr must cause var to increase on each iteration of the
4246 // loop. If test-expr is of form var relational-op b and relational-op is
4247 // > or >= then incr-expr must cause var to decrease on each iteration of
4248 // the loop.
4249 // If test-expr is of form b relational-op var and relational-op is < or
4250 // <= then incr-expr must cause var to decrease on each iteration of the
4251 // loop. If test-expr is of form b relational-op var and relational-op is
4252 // > or >= then incr-expr must cause var to increase on each iteration of
4253 // the loop.
4254 llvm::APSInt Result;
4255 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
4256 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
4257 bool IsConstNeg =
4258 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004259 bool IsConstPos =
4260 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004261 bool IsConstZero = IsConstant && !Result.getBoolValue();
4262 if (UB && (IsConstZero ||
4263 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00004264 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004265 SemaRef.Diag(NewStep->getExprLoc(),
4266 diag::err_omp_loop_incr_not_compatible)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004267 << LCDecl << TestIsLessOp << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004268 SemaRef.Diag(ConditionLoc,
4269 diag::note_omp_loop_cond_requres_compatible_incr)
4270 << TestIsLessOp << ConditionSrcRange;
4271 return true;
4272 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004273 if (TestIsLessOp == Subtract) {
4274 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
4275 NewStep).get();
4276 Subtract = !Subtract;
4277 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004278 }
4279
4280 Step = NewStep;
4281 SubtractStep = Subtract;
4282 return false;
4283}
4284
Alexey Bataev9c821032015-04-30 04:23:23 +00004285bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004286 // Check init-expr for canonical loop form and save loop counter
4287 // variable - #Var and its initialization value - #LB.
4288 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
4289 // var = lb
4290 // integer-type var = lb
4291 // random-access-iterator-type var = lb
4292 // pointer-type var = lb
4293 //
4294 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00004295 if (EmitDiags) {
4296 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
4297 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004298 return true;
4299 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004300 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4301 if (!ExprTemp->cleanupsHaveSideEffects())
4302 S = ExprTemp->getSubExpr();
4303
Alexander Musmana5f070a2014-10-01 06:03:56 +00004304 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004305 if (Expr *E = dyn_cast<Expr>(S))
4306 S = E->IgnoreParens();
4307 if (auto BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004308 if (BO->getOpcode() == BO_Assign) {
4309 auto *LHS = BO->getLHS()->IgnoreParens();
4310 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
4311 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4312 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4313 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4314 return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
4315 }
4316 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4317 if (ME->isArrow() &&
4318 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4319 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4320 }
4321 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004322 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
4323 if (DS->isSingleDecl()) {
4324 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00004325 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004326 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00004327 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004328 SemaRef.Diag(S->getLocStart(),
4329 diag::ext_omp_loop_not_canonical_init)
4330 << S->getSourceRange();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004331 return SetLCDeclAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004332 }
4333 }
4334 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004335 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4336 if (CE->getOperator() == OO_Equal) {
4337 auto *LHS = CE->getArg(0);
4338 if (auto DRE = dyn_cast<DeclRefExpr>(LHS)) {
4339 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4340 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4341 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4342 return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
4343 }
4344 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4345 if (ME->isArrow() &&
4346 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4347 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4348 }
4349 }
4350 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004351
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004352 if (Dependent() || SemaRef.CurContext->isDependentContext())
4353 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00004354 if (EmitDiags) {
4355 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
4356 << S->getSourceRange();
4357 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004358 return true;
4359}
4360
Alexey Bataev23b69422014-06-18 07:08:49 +00004361/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004362/// variable (which may be the loop variable) if possible.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004363static const ValueDecl *GetInitLCDecl(Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004364 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00004365 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004366 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004367 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
4368 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004369 if ((Ctor->isCopyOrMoveConstructor() ||
4370 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4371 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004372 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004373 if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
4374 if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
4375 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
4376 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4377 return getCanonicalDecl(ME->getMemberDecl());
4378 return getCanonicalDecl(VD);
4379 }
4380 }
4381 if (auto *ME = dyn_cast_or_null<MemberExpr>(E))
4382 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4383 return getCanonicalDecl(ME->getMemberDecl());
4384 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004385}
4386
4387bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
4388 // Check test-expr for canonical form, save upper-bound UB, flags for
4389 // less/greater and for strict/non-strict comparison.
4390 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4391 // var relational-op b
4392 // b relational-op var
4393 //
4394 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004395 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004396 return true;
4397 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004398 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004399 SourceLocation CondLoc = S->getLocStart();
4400 if (auto BO = dyn_cast<BinaryOperator>(S)) {
4401 if (BO->isRelationalOp()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004402 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004403 return SetUB(BO->getRHS(),
4404 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
4405 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4406 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004407 if (GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004408 return SetUB(BO->getLHS(),
4409 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
4410 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4411 BO->getSourceRange(), BO->getOperatorLoc());
4412 }
4413 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4414 if (CE->getNumArgs() == 2) {
4415 auto Op = CE->getOperator();
4416 switch (Op) {
4417 case OO_Greater:
4418 case OO_GreaterEqual:
4419 case OO_Less:
4420 case OO_LessEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004421 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004422 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
4423 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4424 CE->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004425 if (GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004426 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
4427 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4428 CE->getOperatorLoc());
4429 break;
4430 default:
4431 break;
4432 }
4433 }
4434 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004435 if (Dependent() || SemaRef.CurContext->isDependentContext())
4436 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004437 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004438 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004439 return true;
4440}
4441
4442bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
4443 // RHS of canonical loop form increment can be:
4444 // var + incr
4445 // incr + var
4446 // var - incr
4447 //
4448 RHS = RHS->IgnoreParenImpCasts();
4449 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
4450 if (BO->isAdditiveOp()) {
4451 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004452 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004453 return SetStep(BO->getRHS(), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004454 if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004455 return SetStep(BO->getLHS(), false);
4456 }
4457 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
4458 bool IsAdd = CE->getOperator() == OO_Plus;
4459 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004460 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004461 return SetStep(CE->getArg(1), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004462 if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004463 return SetStep(CE->getArg(0), false);
4464 }
4465 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004466 if (Dependent() || SemaRef.CurContext->isDependentContext())
4467 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004468 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004469 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004470 return true;
4471}
4472
4473bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
4474 // Check incr-expr for canonical loop form and return true if it
4475 // does not conform.
4476 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4477 // ++var
4478 // var++
4479 // --var
4480 // var--
4481 // var += incr
4482 // var -= incr
4483 // var = var + incr
4484 // var = incr + var
4485 // var = var - incr
4486 //
4487 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004488 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004489 return true;
4490 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004491 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4492 if (!ExprTemp->cleanupsHaveSideEffects())
4493 S = ExprTemp->getSubExpr();
4494
Alexander Musmana5f070a2014-10-01 06:03:56 +00004495 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004496 S = S->IgnoreParens();
4497 if (auto UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004498 if (UO->isIncrementDecrementOp() &&
4499 GetInitLCDecl(UO->getSubExpr()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004500 return SetStep(
4501 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
4502 (UO->isDecrementOp() ? -1 : 1)).get(),
4503 false);
4504 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
4505 switch (BO->getOpcode()) {
4506 case BO_AddAssign:
4507 case BO_SubAssign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004508 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004509 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
4510 break;
4511 case BO_Assign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004512 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004513 return CheckIncRHS(BO->getRHS());
4514 break;
4515 default:
4516 break;
4517 }
4518 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4519 switch (CE->getOperator()) {
4520 case OO_PlusPlus:
4521 case OO_MinusMinus:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004522 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004523 return SetStep(
4524 SemaRef.ActOnIntegerConstant(
4525 CE->getLocStart(),
4526 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
4527 false);
4528 break;
4529 case OO_PlusEqual:
4530 case OO_MinusEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004531 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004532 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
4533 break;
4534 case OO_Equal:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004535 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004536 return CheckIncRHS(CE->getArg(1));
4537 break;
4538 default:
4539 break;
4540 }
4541 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004542 if (Dependent() || SemaRef.CurContext->isDependentContext())
4543 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004544 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004545 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004546 return true;
4547}
Alexander Musmana5f070a2014-10-01 06:03:56 +00004548
Alexey Bataev5a3af132016-03-29 08:58:54 +00004549static ExprResult
4550tryBuildCapture(Sema &SemaRef, Expr *Capture,
4551 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00004552 if (SemaRef.CurContext->isDependentContext())
4553 return ExprResult(Capture);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004554 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
4555 return SemaRef.PerformImplicitConversion(
4556 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
4557 /*AllowExplicit=*/true);
4558 auto I = Captures.find(Capture);
4559 if (I != Captures.end())
4560 return buildCapture(SemaRef, Capture, I->second);
4561 DeclRefExpr *Ref = nullptr;
4562 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4563 Captures[Capture] = Ref;
4564 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004565}
4566
Alexander Musmana5f070a2014-10-01 06:03:56 +00004567/// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004568Expr *OpenMPIterationSpaceChecker::BuildNumIterations(
4569 Scope *S, const bool LimitedType,
4570 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004571 ExprResult Diff;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004572 auto VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004573 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004574 SemaRef.getLangOpts().CPlusPlus) {
4575 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004576 auto *UBExpr = TestIsLessOp ? UB : LB;
4577 auto *LBExpr = TestIsLessOp ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00004578 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
4579 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004580 if (!Upper || !Lower)
4581 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004582
4583 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4584
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004585 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004586 // BuildBinOp already emitted error, this one is to point user to upper
4587 // and lower bound, and to tell what is passed to 'operator-'.
4588 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
4589 << Upper->getSourceRange() << Lower->getSourceRange();
4590 return nullptr;
4591 }
4592 }
4593
4594 if (!Diff.isUsable())
4595 return nullptr;
4596
4597 // Upper - Lower [- 1]
4598 if (TestIsStrictOp)
4599 Diff = SemaRef.BuildBinOp(
4600 S, DefaultLoc, BO_Sub, Diff.get(),
4601 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4602 if (!Diff.isUsable())
4603 return nullptr;
4604
4605 // Upper - Lower [- 1] + Step
Alexey Bataev5a3af132016-03-29 08:58:54 +00004606 auto NewStep = tryBuildCapture(SemaRef, Step, Captures);
4607 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004608 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004609 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004610 if (!Diff.isUsable())
4611 return nullptr;
4612
4613 // Parentheses (for dumping/debugging purposes only).
4614 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4615 if (!Diff.isUsable())
4616 return nullptr;
4617
4618 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004619 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004620 if (!Diff.isUsable())
4621 return nullptr;
4622
Alexander Musman174b3ca2014-10-06 11:16:29 +00004623 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004624 QualType Type = Diff.get()->getType();
4625 auto &C = SemaRef.Context;
4626 bool UseVarType = VarType->hasIntegerRepresentation() &&
4627 C.getTypeSize(Type) > C.getTypeSize(VarType);
4628 if (!Type->isIntegerType() || UseVarType) {
4629 unsigned NewSize =
4630 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4631 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4632 : Type->hasSignedIntegerRepresentation();
4633 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00004634 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4635 Diff = SemaRef.PerformImplicitConversion(
4636 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4637 if (!Diff.isUsable())
4638 return nullptr;
4639 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004640 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004641 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00004642 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4643 if (NewSize != C.getTypeSize(Type)) {
4644 if (NewSize < C.getTypeSize(Type)) {
4645 assert(NewSize == 64 && "incorrect loop var size");
4646 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4647 << InitSrcRange << ConditionSrcRange;
4648 }
4649 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004650 NewSize, Type->hasSignedIntegerRepresentation() ||
4651 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00004652 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4653 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4654 Sema::AA_Converting, true);
4655 if (!Diff.isUsable())
4656 return nullptr;
4657 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004658 }
4659 }
4660
Alexander Musmana5f070a2014-10-01 06:03:56 +00004661 return Diff.get();
4662}
4663
Alexey Bataev5a3af132016-03-29 08:58:54 +00004664Expr *OpenMPIterationSpaceChecker::BuildPreCond(
4665 Scope *S, Expr *Cond,
4666 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004667 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4668 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4669 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004670
Alexey Bataev5a3af132016-03-29 08:58:54 +00004671 auto NewLB = tryBuildCapture(SemaRef, LB, Captures);
4672 auto NewUB = tryBuildCapture(SemaRef, UB, Captures);
4673 if (!NewLB.isUsable() || !NewUB.isUsable())
4674 return nullptr;
4675
Alexey Bataev62dbb972015-04-22 11:59:37 +00004676 auto CondExpr = SemaRef.BuildBinOp(
4677 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
4678 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004679 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004680 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004681 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4682 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00004683 CondExpr = SemaRef.PerformImplicitConversion(
4684 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4685 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004686 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00004687 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4688 // Otherwise use original loop conditon and evaluate it in runtime.
4689 return CondExpr.isUsable() ? CondExpr.get() : Cond;
4690}
4691
Alexander Musmana5f070a2014-10-01 06:03:56 +00004692/// \brief Build reference expression to the counter be used for codegen.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004693DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar(
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004694 llvm::MapVector<Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004695 auto *VD = dyn_cast<VarDecl>(LCDecl);
4696 if (!VD) {
4697 VD = SemaRef.IsOpenMPCapturedDecl(LCDecl);
4698 auto *Ref = buildDeclRefExpr(
4699 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004700 DSAStackTy::DSAVarData Data = DSA.getTopDSA(LCDecl, /*FromParent=*/false);
4701 // If the loop control decl is explicitly marked as private, do not mark it
4702 // as captured again.
4703 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4704 Captures.insert(std::make_pair(LCRef, Ref));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004705 return Ref;
4706 }
4707 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00004708 DefaultLoc);
4709}
4710
4711Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004712 if (LCDecl && !LCDecl->isInvalidDecl()) {
4713 auto Type = LCDecl->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00004714 auto *PrivateVar =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004715 buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(),
4716 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00004717 if (PrivateVar->isInvalidDecl())
4718 return nullptr;
4719 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4720 }
4721 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004722}
4723
4724/// \brief Build initization of the counter be used for codegen.
4725Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
4726
4727/// \brief Build step of the counter be used for codegen.
4728Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
4729
4730/// \brief Iteration space of a single for loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004731struct LoopIterationSpace final {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004732 /// \brief Condition of the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004733 Expr *PreCond = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004734 /// \brief This expression calculates the number of iterations in the loop.
4735 /// It is always possible to calculate it before starting the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004736 Expr *NumIterations = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004737 /// \brief The loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004738 Expr *CounterVar = nullptr;
Alexey Bataeva8899172015-08-06 12:30:57 +00004739 /// \brief Private loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004740 Expr *PrivateCounterVar = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004741 /// \brief This is initializer for the initial value of #CounterVar.
Alexey Bataev8b427062016-05-25 12:36:08 +00004742 Expr *CounterInit = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004743 /// \brief This is step for the #CounterVar used to generate its update:
4744 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
Alexey Bataev8b427062016-05-25 12:36:08 +00004745 Expr *CounterStep = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004746 /// \brief Should step be subtracted?
Alexey Bataev8b427062016-05-25 12:36:08 +00004747 bool Subtract = false;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004748 /// \brief Source range of the loop init.
4749 SourceRange InitSrcRange;
4750 /// \brief Source range of the loop condition.
4751 SourceRange CondSrcRange;
4752 /// \brief Source range of the loop increment.
4753 SourceRange IncSrcRange;
4754};
4755
Alexey Bataev23b69422014-06-18 07:08:49 +00004756} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004757
Alexey Bataev9c821032015-04-30 04:23:23 +00004758void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4759 assert(getLangOpts().OpenMP && "OpenMP is not active.");
4760 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004761 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4762 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00004763 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4764 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004765 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
4766 if (auto *D = ISC.GetLoopDecl()) {
4767 auto *VD = dyn_cast<VarDecl>(D);
4768 if (!VD) {
4769 if (auto *Private = IsOpenMPCapturedDecl(D))
4770 VD = Private;
4771 else {
4772 auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(),
4773 /*WithInit=*/false);
4774 VD = cast<VarDecl>(Ref->getDecl());
4775 }
4776 }
4777 DSAStack->addLoopControlVariable(D, VD);
4778 }
4779 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004780 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00004781 }
4782}
4783
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004784/// \brief Called on a for stmt to check and extract its iteration space
4785/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00004786static bool CheckOpenMPIterationSpace(
4787 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4788 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004789 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004790 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004791 LoopIterationSpace &ResultIterSpace,
4792 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004793 // OpenMP [2.6, Canonical Loop Form]
4794 // for (init-expr; test-expr; incr-expr) structured-block
4795 auto For = dyn_cast_or_null<ForStmt>(S);
4796 if (!For) {
4797 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004798 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
4799 << getOpenMPDirectiveName(DKind) << NestedLoopCount
4800 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
4801 if (NestedLoopCount > 1) {
4802 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4803 SemaRef.Diag(DSA.getConstructLoc(),
4804 diag::note_omp_collapse_ordered_expr)
4805 << 2 << CollapseLoopCountExpr->getSourceRange()
4806 << OrderedLoopCountExpr->getSourceRange();
4807 else if (CollapseLoopCountExpr)
4808 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4809 diag::note_omp_collapse_ordered_expr)
4810 << 0 << CollapseLoopCountExpr->getSourceRange();
4811 else
4812 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4813 diag::note_omp_collapse_ordered_expr)
4814 << 1 << OrderedLoopCountExpr->getSourceRange();
4815 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004816 return true;
4817 }
4818 assert(For->getBody());
4819
4820 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4821
4822 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00004823 auto Init = For->getInit();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004824 if (ISC.CheckInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004825 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004826
4827 bool HasErrors = false;
4828
4829 // Check loop variable's type.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004830 if (auto *LCDecl = ISC.GetLoopDecl()) {
4831 auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004832
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004833 // OpenMP [2.6, Canonical Loop Form]
4834 // Var is one of the following:
4835 // A variable of signed or unsigned integer type.
4836 // For C++, a variable of a random access iterator type.
4837 // For C, a variable of a pointer type.
4838 auto VarType = LCDecl->getType().getNonReferenceType();
4839 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4840 !VarType->isPointerType() &&
4841 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
4842 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
4843 << SemaRef.getLangOpts().CPlusPlus;
4844 HasErrors = true;
4845 }
4846
4847 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4848 // a Construct
4849 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4850 // parallel for construct is (are) private.
4851 // The loop iteration variable in the associated for-loop of a simd
4852 // construct with just one associated for-loop is linear with a
4853 // constant-linear-step that is the increment of the associated for-loop.
4854 // Exclude loop var from the list of variables with implicitly defined data
4855 // sharing attributes.
4856 VarsWithImplicitDSA.erase(LCDecl);
4857
4858 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4859 // in a Construct, C/C++].
4860 // The loop iteration variable in the associated for-loop of a simd
4861 // construct with just one associated for-loop may be listed in a linear
4862 // clause with a constant-linear-step that is the increment of the
4863 // associated for-loop.
4864 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4865 // parallel for construct may be listed in a private or lastprivate clause.
4866 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4867 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4868 // declared in the loop and it is predetermined as a private.
4869 auto PredeterminedCKind =
4870 isOpenMPSimdDirective(DKind)
4871 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4872 : OMPC_private;
4873 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4874 DVar.CKind != PredeterminedCKind) ||
4875 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4876 isOpenMPDistributeDirective(DKind)) &&
4877 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4878 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4879 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4880 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
4881 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4882 << getOpenMPClauseName(PredeterminedCKind);
4883 if (DVar.RefExpr == nullptr)
4884 DVar.CKind = PredeterminedCKind;
4885 ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
4886 HasErrors = true;
4887 } else if (LoopDeclRefExpr != nullptr) {
4888 // Make the loop iteration variable private (for worksharing constructs),
4889 // linear (for simd directives with the only one associated loop) or
4890 // lastprivate (for simd directives with several collapsed or ordered
4891 // loops).
4892 if (DVar.CKind == OMPC_unknown)
Alexey Bataev7ace49d2016-05-17 08:55:33 +00004893 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
4894 [](OpenMPDirectiveKind) -> bool { return true; },
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004895 /*FromParent=*/false);
4896 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4897 }
4898
4899 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4900
4901 // Check test-expr.
4902 HasErrors |= ISC.CheckCond(For->getCond());
4903
4904 // Check incr-expr.
4905 HasErrors |= ISC.CheckInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004906 }
4907
Alexander Musmana5f070a2014-10-01 06:03:56 +00004908 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004909 return HasErrors;
4910
Alexander Musmana5f070a2014-10-01 06:03:56 +00004911 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004912 ResultIterSpace.PreCond =
4913 ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures);
Alexander Musman174b3ca2014-10-06 11:16:29 +00004914 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004915 DSA.getCurScope(),
4916 (isOpenMPWorksharingDirective(DKind) ||
4917 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4918 Captures);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004919 ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures, DSA);
Alexey Bataeva8899172015-08-06 12:30:57 +00004920 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004921 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
4922 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
4923 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
4924 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4925 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4926 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4927
Alexey Bataev62dbb972015-04-22 11:59:37 +00004928 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4929 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004930 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004931 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004932 ResultIterSpace.CounterInit == nullptr ||
4933 ResultIterSpace.CounterStep == nullptr);
4934
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004935 return HasErrors;
4936}
4937
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004938/// \brief Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004939static ExprResult
4940BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4941 ExprResult Start,
4942 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004943 // Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004944 auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
4945 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004946 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004947 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00004948 VarRef.get()->getType())) {
4949 NewStart = SemaRef.PerformImplicitConversion(
4950 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4951 /*AllowExplicit=*/true);
4952 if (!NewStart.isUsable())
4953 return ExprError();
4954 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004955
4956 auto Init =
4957 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4958 return Init;
4959}
4960
Alexander Musmana5f070a2014-10-01 06:03:56 +00004961/// \brief Build 'VarRef = Start + Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004962static ExprResult
4963BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc,
4964 ExprResult VarRef, ExprResult Start, ExprResult Iter,
4965 ExprResult Step, bool Subtract,
4966 llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004967 // Add parentheses (for debugging purposes only).
4968 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4969 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4970 !Step.isUsable())
4971 return ExprError();
4972
Alexey Bataev5a3af132016-03-29 08:58:54 +00004973 ExprResult NewStep = Step;
4974 if (Captures)
4975 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004976 if (NewStep.isInvalid())
4977 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004978 ExprResult Update =
4979 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004980 if (!Update.isUsable())
4981 return ExprError();
4982
Alexey Bataevc0214e02016-02-16 12:13:49 +00004983 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4984 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004985 ExprResult NewStart = Start;
4986 if (Captures)
4987 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004988 if (NewStart.isInvalid())
4989 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004990
Alexey Bataevc0214e02016-02-16 12:13:49 +00004991 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4992 ExprResult SavedUpdate = Update;
4993 ExprResult UpdateVal;
4994 if (VarRef.get()->getType()->isOverloadableType() ||
4995 NewStart.get()->getType()->isOverloadableType() ||
4996 Update.get()->getType()->isOverloadableType()) {
4997 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4998 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4999 Update =
5000 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
5001 if (Update.isUsable()) {
5002 UpdateVal =
5003 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
5004 VarRef.get(), SavedUpdate.get());
5005 if (UpdateVal.isUsable()) {
5006 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
5007 UpdateVal.get());
5008 }
5009 }
5010 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
5011 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005012
Alexey Bataevc0214e02016-02-16 12:13:49 +00005013 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
5014 if (!Update.isUsable() || !UpdateVal.isUsable()) {
5015 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
5016 NewStart.get(), SavedUpdate.get());
5017 if (!Update.isUsable())
5018 return ExprError();
5019
Alexey Bataev11481f52016-02-17 10:29:05 +00005020 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
5021 VarRef.get()->getType())) {
5022 Update = SemaRef.PerformImplicitConversion(
5023 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
5024 if (!Update.isUsable())
5025 return ExprError();
5026 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00005027
5028 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
5029 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005030 return Update;
5031}
5032
5033/// \brief Convert integer expression \a E to make it have at least \a Bits
5034/// bits.
5035static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
5036 Sema &SemaRef) {
5037 if (E == nullptr)
5038 return ExprError();
5039 auto &C = SemaRef.Context;
5040 QualType OldType = E->getType();
5041 unsigned HasBits = C.getTypeSize(OldType);
5042 if (HasBits >= Bits)
5043 return ExprResult(E);
5044 // OK to convert to signed, because new type has more bits than old.
5045 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
5046 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
5047 true);
5048}
5049
5050/// \brief Check if the given expression \a E is a constant integer that fits
5051/// into \a Bits bits.
5052static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
5053 if (E == nullptr)
5054 return false;
5055 llvm::APSInt Result;
5056 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
5057 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
5058 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005059}
5060
Alexey Bataev5a3af132016-03-29 08:58:54 +00005061/// Build preinits statement for the given declarations.
5062static Stmt *buildPreInits(ASTContext &Context,
5063 SmallVectorImpl<Decl *> &PreInits) {
5064 if (!PreInits.empty()) {
5065 return new (Context) DeclStmt(
5066 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
5067 SourceLocation(), SourceLocation());
5068 }
5069 return nullptr;
5070}
5071
5072/// Build preinits statement for the given declarations.
5073static Stmt *buildPreInits(ASTContext &Context,
5074 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
5075 if (!Captures.empty()) {
5076 SmallVector<Decl *, 16> PreInits;
5077 for (auto &Pair : Captures)
5078 PreInits.push_back(Pair.second->getDecl());
5079 return buildPreInits(Context, PreInits);
5080 }
5081 return nullptr;
5082}
5083
5084/// Build postupdate expression for the given list of postupdates expressions.
5085static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
5086 Expr *PostUpdate = nullptr;
5087 if (!PostUpdates.empty()) {
5088 for (auto *E : PostUpdates) {
5089 Expr *ConvE = S.BuildCStyleCastExpr(
5090 E->getExprLoc(),
5091 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
5092 E->getExprLoc(), E)
5093 .get();
5094 PostUpdate = PostUpdate
5095 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
5096 PostUpdate, ConvE)
5097 .get()
5098 : ConvE;
5099 }
5100 }
5101 return PostUpdate;
5102}
5103
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005104/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00005105/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
5106/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005107static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00005108CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
5109 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
5110 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005111 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00005112 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005113 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005114 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005115 // Found 'collapse' clause - calculate collapse number.
5116 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005117 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005118 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005119 }
5120 if (OrderedLoopCountExpr) {
5121 // Found 'ordered' clause - calculate collapse number.
5122 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005123 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
5124 if (Result.getLimitedValue() < NestedLoopCount) {
5125 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
5126 diag::err_omp_wrong_ordered_loop_count)
5127 << OrderedLoopCountExpr->getSourceRange();
5128 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
5129 diag::note_collapse_loop_count)
5130 << CollapseLoopCountExpr->getSourceRange();
5131 }
5132 NestedLoopCount = Result.getLimitedValue();
5133 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005134 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005135 // This is helper routine for loop directives (e.g., 'for', 'simd',
5136 // 'for simd', etc.).
Alexey Bataev5a3af132016-03-29 08:58:54 +00005137 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005138 SmallVector<LoopIterationSpace, 4> IterSpaces;
5139 IterSpaces.resize(NestedLoopCount);
5140 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005141 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005142 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00005143 NestedLoopCount, CollapseLoopCountExpr,
5144 OrderedLoopCountExpr, VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005145 IterSpaces[Cnt], Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00005146 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005147 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005148 // OpenMP [2.8.1, simd construct, Restrictions]
5149 // All loops associated with the construct must be perfectly nested; that
5150 // is, there must be no intervening code nor any OpenMP directive between
5151 // any two loops.
5152 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005153 }
5154
Alexander Musmana5f070a2014-10-01 06:03:56 +00005155 Built.clear(/* size */ NestedLoopCount);
5156
5157 if (SemaRef.CurContext->isDependentContext())
5158 return NestedLoopCount;
5159
5160 // An example of what is generated for the following code:
5161 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00005162 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00005163 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00005164 // for (k = 0; k < NK; ++k)
5165 // for (j = J0; j < NJ; j+=2) {
5166 // <loop body>
5167 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005168 //
5169 // We generate the code below.
5170 // Note: the loop body may be outlined in CodeGen.
5171 // Note: some counters may be C++ classes, operator- is used to find number of
5172 // iterations and operator+= to calculate counter value.
5173 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
5174 // or i64 is currently supported).
5175 //
5176 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
5177 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
5178 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
5179 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
5180 // // similar updates for vars in clauses (e.g. 'linear')
5181 // <loop body (using local i and j)>
5182 // }
5183 // i = NI; // assign final values of counters
5184 // j = NJ;
5185 //
5186
5187 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
5188 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00005189 // Precondition tests if there is at least one iteration (all conditions are
5190 // true).
5191 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005192 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005193 ExprResult LastIteration32 = WidenIterationCount(
5194 32 /* Bits */, SemaRef.PerformImplicitConversion(
5195 N0->IgnoreImpCasts(), N0->getType(),
5196 Sema::AA_Converting, /*AllowExplicit=*/true)
5197 .get(),
5198 SemaRef);
5199 ExprResult LastIteration64 = WidenIterationCount(
5200 64 /* Bits */, SemaRef.PerformImplicitConversion(
5201 N0->IgnoreImpCasts(), N0->getType(),
5202 Sema::AA_Converting, /*AllowExplicit=*/true)
5203 .get(),
5204 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005205
5206 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
5207 return NestedLoopCount;
5208
5209 auto &C = SemaRef.Context;
5210 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
5211
5212 Scope *CurScope = DSA.getCurScope();
5213 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00005214 if (PreCond.isUsable()) {
5215 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
5216 PreCond.get(), IterSpaces[Cnt].PreCond);
5217 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005218 auto N = IterSpaces[Cnt].NumIterations;
5219 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
5220 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005221 LastIteration32 = SemaRef.BuildBinOp(
5222 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
5223 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5224 Sema::AA_Converting,
5225 /*AllowExplicit=*/true)
5226 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005227 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005228 LastIteration64 = SemaRef.BuildBinOp(
5229 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
5230 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5231 Sema::AA_Converting,
5232 /*AllowExplicit=*/true)
5233 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005234 }
5235
5236 // Choose either the 32-bit or 64-bit version.
5237 ExprResult LastIteration = LastIteration64;
5238 if (LastIteration32.isUsable() &&
5239 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
5240 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
5241 FitsInto(
5242 32 /* Bits */,
5243 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
5244 LastIteration64.get(), SemaRef)))
5245 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00005246 QualType VType = LastIteration.get()->getType();
5247 QualType RealVType = VType;
5248 QualType StrideVType = VType;
5249 if (isOpenMPTaskLoopDirective(DKind)) {
5250 VType =
5251 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
5252 StrideVType =
5253 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
5254 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005255
5256 if (!LastIteration.isUsable())
5257 return 0;
5258
5259 // Save the number of iterations.
5260 ExprResult NumIterations = LastIteration;
5261 {
5262 LastIteration = SemaRef.BuildBinOp(
5263 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
5264 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5265 if (!LastIteration.isUsable())
5266 return 0;
5267 }
5268
5269 // Calculate the last iteration number beforehand instead of doing this on
5270 // each iteration. Do not do this if the number of iterations may be kfold-ed.
5271 llvm::APSInt Result;
5272 bool IsConstant =
5273 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
5274 ExprResult CalcLastIteration;
5275 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005276 ExprResult SaveRef =
5277 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005278 LastIteration = SaveRef;
5279
5280 // Prepare SaveRef + 1.
5281 NumIterations = SemaRef.BuildBinOp(
Alexey Bataev5a3af132016-03-29 08:58:54 +00005282 CurScope, SourceLocation(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00005283 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5284 if (!NumIterations.isUsable())
5285 return 0;
5286 }
5287
5288 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
5289
Alexander Musmanc6388682014-12-15 07:07:06 +00005290 // Build variables passed into runtime, nesessary for worksharing directives.
Carlo Bertolli9925f152016-06-27 14:55:37 +00005291 ExprResult LB, UB, IL, ST, EUB, PrevLB, PrevUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005292 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5293 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005294 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005295 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
5296 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005297 SemaRef.AddInitializerToDecl(
5298 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5299 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5300
5301 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005302 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
5303 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005304 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
5305 /*DirectInit*/ false,
5306 /*TypeMayContainAuto*/ false);
5307
5308 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
5309 // This will be used to implement clause 'lastprivate'.
5310 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005311 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
5312 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005313 SemaRef.AddInitializerToDecl(
5314 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5315 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5316
5317 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00005318 VarDecl *STDecl =
5319 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
5320 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005321 SemaRef.AddInitializerToDecl(
5322 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
5323 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5324
5325 // Build expression: UB = min(UB, LastIteration)
5326 // It is nesessary for CodeGen of directives with static scheduling.
5327 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
5328 UB.get(), LastIteration.get());
5329 ExprResult CondOp = SemaRef.ActOnConditionalOp(
5330 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
5331 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
5332 CondOp.get());
5333 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
Carlo Bertolli9925f152016-06-27 14:55:37 +00005334
5335 // If we have a combined directive that combines 'distribute', 'for' or
5336 // 'simd' we need to be able to access the bounds of the schedule of the
5337 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
5338 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
5339 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5340 auto *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
5341
5342 // We expect to have at least 2 more parameters than the 'parallel'
5343 // directive does - the lower and upper bounds of the previous schedule.
5344 assert(CD->getNumParams() >= 4 &&
5345 "Unexpected number of parameters in loop combined directive");
5346
5347 // Set the proper type for the bounds given what we learned from the
5348 // enclosed loops.
5349 auto *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
5350 auto *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
5351
5352 // Previous lower and upper bounds are obtained from the region
5353 // parameters.
5354 PrevLB =
5355 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
5356 PrevUB =
5357 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
5358 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005359 }
5360
5361 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005362 ExprResult IV;
5363 ExprResult Init;
5364 {
Alexey Bataev7292c292016-04-25 12:22:29 +00005365 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
5366 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005367 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005368 isOpenMPTaskLoopDirective(DKind) ||
5369 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00005370 ? LB.get()
5371 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
5372 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
5373 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005374 }
5375
Alexander Musmanc6388682014-12-15 07:07:06 +00005376 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005377 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00005378 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005379 (isOpenMPWorksharingDirective(DKind) ||
5380 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00005381 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
5382 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
5383 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005384
5385 // Loop increment (IV = IV + 1)
5386 SourceLocation IncLoc;
5387 ExprResult Inc =
5388 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
5389 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
5390 if (!Inc.isUsable())
5391 return 0;
5392 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00005393 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
5394 if (!Inc.isUsable())
5395 return 0;
5396
5397 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
5398 // Used for directives with static scheduling.
5399 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005400 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5401 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005402 // LB + ST
5403 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
5404 if (!NextLB.isUsable())
5405 return 0;
5406 // LB = LB + ST
5407 NextLB =
5408 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
5409 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
5410 if (!NextLB.isUsable())
5411 return 0;
5412 // UB + ST
5413 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
5414 if (!NextUB.isUsable())
5415 return 0;
5416 // UB = UB + ST
5417 NextUB =
5418 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
5419 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
5420 if (!NextUB.isUsable())
5421 return 0;
5422 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005423
5424 // Build updates and final values of the loop counters.
5425 bool HasErrors = false;
5426 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005427 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005428 Built.Updates.resize(NestedLoopCount);
5429 Built.Finals.resize(NestedLoopCount);
Alexey Bataev8b427062016-05-25 12:36:08 +00005430 SmallVector<Expr *, 4> LoopMultipliers;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005431 {
5432 ExprResult Div;
5433 // Go from inner nested loop to outer.
5434 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5435 LoopIterationSpace &IS = IterSpaces[Cnt];
5436 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
5437 // Build: Iter = (IV / Div) % IS.NumIters
5438 // where Div is product of previous iterations' IS.NumIters.
5439 ExprResult Iter;
5440 if (Div.isUsable()) {
5441 Iter =
5442 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
5443 } else {
5444 Iter = IV;
5445 assert((Cnt == (int)NestedLoopCount - 1) &&
5446 "unusable div expected on first iteration only");
5447 }
5448
5449 if (Cnt != 0 && Iter.isUsable())
5450 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
5451 IS.NumIterations);
5452 if (!Iter.isUsable()) {
5453 HasErrors = true;
5454 break;
5455 }
5456
Alexey Bataev39f915b82015-05-08 10:41:21 +00005457 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005458 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
5459 auto *CounterVar = buildDeclRefExpr(SemaRef, VD, IS.CounterVar->getType(),
5460 IS.CounterVar->getExprLoc(),
5461 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005462 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005463 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005464 if (!Init.isUsable()) {
5465 HasErrors = true;
5466 break;
5467 }
Alexey Bataev5a3af132016-03-29 08:58:54 +00005468 ExprResult Update = BuildCounterUpdate(
5469 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
5470 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005471 if (!Update.isUsable()) {
5472 HasErrors = true;
5473 break;
5474 }
5475
5476 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
5477 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00005478 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005479 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005480 if (!Final.isUsable()) {
5481 HasErrors = true;
5482 break;
5483 }
5484
5485 // Build Div for the next iteration: Div <- Div * IS.NumIters
5486 if (Cnt != 0) {
5487 if (Div.isUnset())
5488 Div = IS.NumIterations;
5489 else
5490 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
5491 IS.NumIterations);
5492
5493 // Add parentheses (for debugging purposes only).
5494 if (Div.isUsable())
Alexey Bataev8b427062016-05-25 12:36:08 +00005495 Div = tryBuildCapture(SemaRef, Div.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005496 if (!Div.isUsable()) {
5497 HasErrors = true;
5498 break;
5499 }
Alexey Bataev8b427062016-05-25 12:36:08 +00005500 LoopMultipliers.push_back(Div.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005501 }
5502 if (!Update.isUsable() || !Final.isUsable()) {
5503 HasErrors = true;
5504 break;
5505 }
5506 // Save results
5507 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00005508 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005509 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005510 Built.Updates[Cnt] = Update.get();
5511 Built.Finals[Cnt] = Final.get();
5512 }
5513 }
5514
5515 if (HasErrors)
5516 return 0;
5517
5518 // Save results
5519 Built.IterationVarRef = IV.get();
5520 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00005521 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00005522 Built.CalcLastIteration =
5523 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005524 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00005525 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005526 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005527 Built.Init = Init.get();
5528 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005529 Built.LB = LB.get();
5530 Built.UB = UB.get();
5531 Built.IL = IL.get();
5532 Built.ST = ST.get();
5533 Built.EUB = EUB.get();
5534 Built.NLB = NextLB.get();
5535 Built.NUB = NextUB.get();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005536 Built.PrevLB = PrevLB.get();
5537 Built.PrevUB = PrevUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005538
Alexey Bataev8b427062016-05-25 12:36:08 +00005539 Expr *CounterVal = SemaRef.DefaultLvalueConversion(IV.get()).get();
5540 // Fill data for doacross depend clauses.
5541 for (auto Pair : DSA.getDoacrossDependClauses()) {
5542 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
5543 Pair.first->setCounterValue(CounterVal);
5544 else {
5545 if (NestedLoopCount != Pair.second.size() ||
5546 NestedLoopCount != LoopMultipliers.size() + 1) {
5547 // Erroneous case - clause has some problems.
5548 Pair.first->setCounterValue(CounterVal);
5549 continue;
5550 }
5551 assert(Pair.first->getDependencyKind() == OMPC_DEPEND_sink);
5552 auto I = Pair.second.rbegin();
5553 auto IS = IterSpaces.rbegin();
5554 auto ILM = LoopMultipliers.rbegin();
5555 Expr *UpCounterVal = CounterVal;
5556 Expr *Multiplier = nullptr;
5557 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5558 if (I->first) {
5559 assert(IS->CounterStep);
5560 Expr *NormalizedOffset =
5561 SemaRef
5562 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Div,
5563 I->first, IS->CounterStep)
5564 .get();
5565 if (Multiplier) {
5566 NormalizedOffset =
5567 SemaRef
5568 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Mul,
5569 NormalizedOffset, Multiplier)
5570 .get();
5571 }
5572 assert(I->second == OO_Plus || I->second == OO_Minus);
5573 BinaryOperatorKind BOK = (I->second == OO_Plus) ? BO_Add : BO_Sub;
5574 UpCounterVal =
5575 SemaRef.BuildBinOp(CurScope, I->first->getExprLoc(), BOK,
5576 UpCounterVal, NormalizedOffset).get();
5577 }
5578 Multiplier = *ILM;
5579 ++I;
5580 ++IS;
5581 ++ILM;
5582 }
5583 Pair.first->setCounterValue(UpCounterVal);
5584 }
5585 }
5586
Alexey Bataevabfc0692014-06-25 06:52:00 +00005587 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005588}
5589
Alexey Bataev10e775f2015-07-30 11:36:16 +00005590static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005591 auto CollapseClauses =
5592 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
5593 if (CollapseClauses.begin() != CollapseClauses.end())
5594 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005595 return nullptr;
5596}
5597
Alexey Bataev10e775f2015-07-30 11:36:16 +00005598static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005599 auto OrderedClauses =
5600 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
5601 if (OrderedClauses.begin() != OrderedClauses.end())
5602 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005603 return nullptr;
5604}
5605
Kelvin Lic5609492016-07-15 04:39:07 +00005606static bool checkSimdlenSafelenSpecified(Sema &S,
5607 const ArrayRef<OMPClause *> Clauses) {
5608 OMPSafelenClause *Safelen = nullptr;
5609 OMPSimdlenClause *Simdlen = nullptr;
5610
5611 for (auto *Clause : Clauses) {
5612 if (Clause->getClauseKind() == OMPC_safelen)
5613 Safelen = cast<OMPSafelenClause>(Clause);
5614 else if (Clause->getClauseKind() == OMPC_simdlen)
5615 Simdlen = cast<OMPSimdlenClause>(Clause);
5616 if (Safelen && Simdlen)
5617 break;
5618 }
5619
5620 if (Simdlen && Safelen) {
5621 llvm::APSInt SimdlenRes, SafelenRes;
5622 auto SimdlenLength = Simdlen->getSimdlen();
5623 auto SafelenLength = Safelen->getSafelen();
5624 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
5625 SimdlenLength->isInstantiationDependent() ||
5626 SimdlenLength->containsUnexpandedParameterPack())
5627 return false;
5628 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
5629 SafelenLength->isInstantiationDependent() ||
5630 SafelenLength->containsUnexpandedParameterPack())
5631 return false;
5632 SimdlenLength->EvaluateAsInt(SimdlenRes, S.Context);
5633 SafelenLength->EvaluateAsInt(SafelenRes, S.Context);
5634 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
5635 // If both simdlen and safelen clauses are specified, the value of the
5636 // simdlen parameter must be less than or equal to the value of the safelen
5637 // parameter.
5638 if (SimdlenRes > SafelenRes) {
5639 S.Diag(SimdlenLength->getExprLoc(),
5640 diag::err_omp_wrong_simdlen_safelen_values)
5641 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
5642 return true;
5643 }
Alexey Bataev66b15b52015-08-21 11:14:16 +00005644 }
5645 return false;
5646}
5647
Alexey Bataev4acb8592014-07-07 13:01:15 +00005648StmtResult Sema::ActOnOpenMPSimdDirective(
5649 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5650 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005651 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005652 if (!AStmt)
5653 return StmtError();
5654
5655 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005656 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005657 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5658 // define the nested loops number.
5659 unsigned NestedLoopCount = CheckOpenMPLoop(
5660 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5661 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005662 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005663 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005664
Alexander Musmana5f070a2014-10-01 06:03:56 +00005665 assert((CurContext->isDependentContext() || B.builtAll()) &&
5666 "omp simd loop exprs were not built");
5667
Alexander Musman3276a272015-03-21 10:12:56 +00005668 if (!CurContext->isDependentContext()) {
5669 // Finalize the clauses that need pre-built expressions for CodeGen.
5670 for (auto C : Clauses) {
5671 if (auto LC = dyn_cast<OMPLinearClause>(C))
5672 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005673 B.NumIterations, *this, CurScope,
5674 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00005675 return StmtError();
5676 }
5677 }
5678
Kelvin Lic5609492016-07-15 04:39:07 +00005679 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005680 return StmtError();
5681
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005682 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005683 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5684 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005685}
5686
Alexey Bataev4acb8592014-07-07 13:01:15 +00005687StmtResult Sema::ActOnOpenMPForDirective(
5688 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5689 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005690 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005691 if (!AStmt)
5692 return StmtError();
5693
5694 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005695 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005696 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5697 // define the nested loops number.
5698 unsigned NestedLoopCount = CheckOpenMPLoop(
5699 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5700 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005701 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005702 return StmtError();
5703
Alexander Musmana5f070a2014-10-01 06:03:56 +00005704 assert((CurContext->isDependentContext() || B.builtAll()) &&
5705 "omp for loop exprs were not built");
5706
Alexey Bataev54acd402015-08-04 11:18:19 +00005707 if (!CurContext->isDependentContext()) {
5708 // Finalize the clauses that need pre-built expressions for CodeGen.
5709 for (auto C : Clauses) {
5710 if (auto LC = dyn_cast<OMPLinearClause>(C))
5711 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005712 B.NumIterations, *this, CurScope,
5713 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005714 return StmtError();
5715 }
5716 }
5717
Alexey Bataevf29276e2014-06-18 04:14:57 +00005718 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005719 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005720 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005721}
5722
Alexander Musmanf82886e2014-09-18 05:12:34 +00005723StmtResult Sema::ActOnOpenMPForSimdDirective(
5724 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5725 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005726 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005727 if (!AStmt)
5728 return StmtError();
5729
5730 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005731 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005732 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5733 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00005734 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005735 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
5736 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5737 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005738 if (NestedLoopCount == 0)
5739 return StmtError();
5740
Alexander Musmanc6388682014-12-15 07:07:06 +00005741 assert((CurContext->isDependentContext() || B.builtAll()) &&
5742 "omp for simd loop exprs were not built");
5743
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005744 if (!CurContext->isDependentContext()) {
5745 // Finalize the clauses that need pre-built expressions for CodeGen.
5746 for (auto C : Clauses) {
5747 if (auto LC = dyn_cast<OMPLinearClause>(C))
5748 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005749 B.NumIterations, *this, CurScope,
5750 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005751 return StmtError();
5752 }
5753 }
5754
Kelvin Lic5609492016-07-15 04:39:07 +00005755 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005756 return StmtError();
5757
Alexander Musmanf82886e2014-09-18 05:12:34 +00005758 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005759 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5760 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005761}
5762
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005763StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5764 Stmt *AStmt,
5765 SourceLocation StartLoc,
5766 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005767 if (!AStmt)
5768 return StmtError();
5769
5770 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005771 auto BaseStmt = AStmt;
5772 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5773 BaseStmt = CS->getCapturedStmt();
5774 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5775 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005776 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005777 return StmtError();
5778 // All associated statements must be '#pragma omp section' except for
5779 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005780 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005781 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5782 if (SectionStmt)
5783 Diag(SectionStmt->getLocStart(),
5784 diag::err_omp_sections_substmt_not_section);
5785 return StmtError();
5786 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005787 cast<OMPSectionDirective>(SectionStmt)
5788 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005789 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005790 } else {
5791 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
5792 return StmtError();
5793 }
5794
5795 getCurFunction()->setHasBranchProtectedScope();
5796
Alexey Bataev25e5b442015-09-15 12:52:43 +00005797 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5798 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005799}
5800
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005801StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5802 SourceLocation StartLoc,
5803 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005804 if (!AStmt)
5805 return StmtError();
5806
5807 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005808
5809 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00005810 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005811
Alexey Bataev25e5b442015-09-15 12:52:43 +00005812 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5813 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005814}
5815
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005816StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5817 Stmt *AStmt,
5818 SourceLocation StartLoc,
5819 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005820 if (!AStmt)
5821 return StmtError();
5822
5823 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00005824
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005825 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00005826
Alexey Bataev3255bf32015-01-19 05:20:46 +00005827 // OpenMP [2.7.3, single Construct, Restrictions]
5828 // The copyprivate clause must not be used with the nowait clause.
5829 OMPClause *Nowait = nullptr;
5830 OMPClause *Copyprivate = nullptr;
5831 for (auto *Clause : Clauses) {
5832 if (Clause->getClauseKind() == OMPC_nowait)
5833 Nowait = Clause;
5834 else if (Clause->getClauseKind() == OMPC_copyprivate)
5835 Copyprivate = Clause;
5836 if (Copyprivate && Nowait) {
5837 Diag(Copyprivate->getLocStart(),
5838 diag::err_omp_single_copyprivate_with_nowait);
5839 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
5840 return StmtError();
5841 }
5842 }
5843
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005844 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5845}
5846
Alexander Musman80c22892014-07-17 08:54:58 +00005847StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5848 SourceLocation StartLoc,
5849 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005850 if (!AStmt)
5851 return StmtError();
5852
5853 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00005854
5855 getCurFunction()->setHasBranchProtectedScope();
5856
5857 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5858}
5859
Alexey Bataev28c75412015-12-15 08:19:24 +00005860StmtResult Sema::ActOnOpenMPCriticalDirective(
5861 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5862 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005863 if (!AStmt)
5864 return StmtError();
5865
5866 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005867
Alexey Bataev28c75412015-12-15 08:19:24 +00005868 bool ErrorFound = false;
5869 llvm::APSInt Hint;
5870 SourceLocation HintLoc;
5871 bool DependentHint = false;
5872 for (auto *C : Clauses) {
5873 if (C->getClauseKind() == OMPC_hint) {
5874 if (!DirName.getName()) {
5875 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
5876 ErrorFound = true;
5877 }
5878 Expr *E = cast<OMPHintClause>(C)->getHint();
5879 if (E->isTypeDependent() || E->isValueDependent() ||
5880 E->isInstantiationDependent())
5881 DependentHint = true;
5882 else {
5883 Hint = E->EvaluateKnownConstInt(Context);
5884 HintLoc = C->getLocStart();
5885 }
5886 }
5887 }
5888 if (ErrorFound)
5889 return StmtError();
5890 auto Pair = DSAStack->getCriticalWithHint(DirName);
5891 if (Pair.first && DirName.getName() && !DependentHint) {
5892 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5893 Diag(StartLoc, diag::err_omp_critical_with_hint);
5894 if (HintLoc.isValid()) {
5895 Diag(HintLoc, diag::note_omp_critical_hint_here)
5896 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
5897 } else
5898 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
5899 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
5900 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
5901 << 1
5902 << C->getHint()->EvaluateKnownConstInt(Context).toString(
5903 /*Radix=*/10, /*Signed=*/false);
5904 } else
5905 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
5906 }
5907 }
5908
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005909 getCurFunction()->setHasBranchProtectedScope();
5910
Alexey Bataev28c75412015-12-15 08:19:24 +00005911 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5912 Clauses, AStmt);
5913 if (!Pair.first && DirName.getName() && !DependentHint)
5914 DSAStack->addCriticalWithHint(Dir, Hint);
5915 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005916}
5917
Alexey Bataev4acb8592014-07-07 13:01:15 +00005918StmtResult Sema::ActOnOpenMPParallelForDirective(
5919 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5920 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005921 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005922 if (!AStmt)
5923 return StmtError();
5924
Alexey Bataev4acb8592014-07-07 13:01:15 +00005925 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5926 // 1.2.2 OpenMP Language Terminology
5927 // Structured block - An executable statement with a single entry at the
5928 // top and a single exit at the bottom.
5929 // The point of exit cannot be a branch out of the structured block.
5930 // longjmp() and throw() must not violate the entry/exit criteria.
5931 CS->getCapturedDecl()->setNothrow();
5932
Alexander Musmanc6388682014-12-15 07:07:06 +00005933 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005934 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5935 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005936 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005937 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
5938 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5939 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005940 if (NestedLoopCount == 0)
5941 return StmtError();
5942
Alexander Musmana5f070a2014-10-01 06:03:56 +00005943 assert((CurContext->isDependentContext() || B.builtAll()) &&
5944 "omp parallel for loop exprs were not built");
5945
Alexey Bataev54acd402015-08-04 11:18:19 +00005946 if (!CurContext->isDependentContext()) {
5947 // Finalize the clauses that need pre-built expressions for CodeGen.
5948 for (auto C : Clauses) {
5949 if (auto LC = dyn_cast<OMPLinearClause>(C))
5950 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005951 B.NumIterations, *this, CurScope,
5952 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005953 return StmtError();
5954 }
5955 }
5956
Alexey Bataev4acb8592014-07-07 13:01:15 +00005957 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005958 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005959 NestedLoopCount, Clauses, AStmt, B,
5960 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00005961}
5962
Alexander Musmane4e893b2014-09-23 09:33:00 +00005963StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5964 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5965 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005966 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005967 if (!AStmt)
5968 return StmtError();
5969
Alexander Musmane4e893b2014-09-23 09:33:00 +00005970 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5971 // 1.2.2 OpenMP Language Terminology
5972 // Structured block - An executable statement with a single entry at the
5973 // top and a single exit at the bottom.
5974 // The point of exit cannot be a branch out of the structured block.
5975 // longjmp() and throw() must not violate the entry/exit criteria.
5976 CS->getCapturedDecl()->setNothrow();
5977
Alexander Musmanc6388682014-12-15 07:07:06 +00005978 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005979 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5980 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00005981 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005982 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
5983 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5984 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005985 if (NestedLoopCount == 0)
5986 return StmtError();
5987
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005988 if (!CurContext->isDependentContext()) {
5989 // Finalize the clauses that need pre-built expressions for CodeGen.
5990 for (auto C : Clauses) {
5991 if (auto LC = dyn_cast<OMPLinearClause>(C))
5992 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005993 B.NumIterations, *this, CurScope,
5994 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005995 return StmtError();
5996 }
5997 }
5998
Kelvin Lic5609492016-07-15 04:39:07 +00005999 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00006000 return StmtError();
6001
Alexander Musmane4e893b2014-09-23 09:33:00 +00006002 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00006003 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00006004 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00006005}
6006
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006007StmtResult
6008Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
6009 Stmt *AStmt, SourceLocation StartLoc,
6010 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006011 if (!AStmt)
6012 return StmtError();
6013
6014 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006015 auto BaseStmt = AStmt;
6016 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
6017 BaseStmt = CS->getCapturedStmt();
6018 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
6019 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00006020 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006021 return StmtError();
6022 // All associated statements must be '#pragma omp section' except for
6023 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00006024 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006025 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
6026 if (SectionStmt)
6027 Diag(SectionStmt->getLocStart(),
6028 diag::err_omp_parallel_sections_substmt_not_section);
6029 return StmtError();
6030 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006031 cast<OMPSectionDirective>(SectionStmt)
6032 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006033 }
6034 } else {
6035 Diag(AStmt->getLocStart(),
6036 diag::err_omp_parallel_sections_not_compound_stmt);
6037 return StmtError();
6038 }
6039
6040 getCurFunction()->setHasBranchProtectedScope();
6041
Alexey Bataev25e5b442015-09-15 12:52:43 +00006042 return OMPParallelSectionsDirective::Create(
6043 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006044}
6045
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006046StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
6047 Stmt *AStmt, SourceLocation StartLoc,
6048 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006049 if (!AStmt)
6050 return StmtError();
6051
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006052 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6053 // 1.2.2 OpenMP Language Terminology
6054 // Structured block - An executable statement with a single entry at the
6055 // top and a single exit at the bottom.
6056 // The point of exit cannot be a branch out of the structured block.
6057 // longjmp() and throw() must not violate the entry/exit criteria.
6058 CS->getCapturedDecl()->setNothrow();
6059
6060 getCurFunction()->setHasBranchProtectedScope();
6061
Alexey Bataev25e5b442015-09-15 12:52:43 +00006062 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
6063 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006064}
6065
Alexey Bataev68446b72014-07-18 07:47:19 +00006066StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
6067 SourceLocation EndLoc) {
6068 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
6069}
6070
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00006071StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
6072 SourceLocation EndLoc) {
6073 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
6074}
6075
Alexey Bataev2df347a2014-07-18 10:17:07 +00006076StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
6077 SourceLocation EndLoc) {
6078 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
6079}
6080
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006081StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
6082 SourceLocation StartLoc,
6083 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006084 if (!AStmt)
6085 return StmtError();
6086
6087 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006088
6089 getCurFunction()->setHasBranchProtectedScope();
6090
6091 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
6092}
6093
Alexey Bataev6125da92014-07-21 11:26:11 +00006094StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
6095 SourceLocation StartLoc,
6096 SourceLocation EndLoc) {
6097 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
6098 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
6099}
6100
Alexey Bataev346265e2015-09-25 10:37:12 +00006101StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
6102 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006103 SourceLocation StartLoc,
6104 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00006105 OMPClause *DependFound = nullptr;
6106 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006107 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00006108 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00006109 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006110 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00006111 for (auto *C : Clauses) {
6112 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
6113 DependFound = C;
6114 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
6115 if (DependSourceClause) {
6116 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
6117 << getOpenMPDirectiveName(OMPD_ordered)
6118 << getOpenMPClauseName(OMPC_depend) << 2;
6119 ErrorFound = true;
6120 } else
6121 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006122 if (DependSinkClause) {
6123 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
6124 << 0;
6125 ErrorFound = true;
6126 }
6127 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
6128 if (DependSourceClause) {
6129 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
6130 << 1;
6131 ErrorFound = true;
6132 }
6133 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00006134 }
6135 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00006136 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006137 else if (C->getClauseKind() == OMPC_simd)
6138 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00006139 }
Alexey Bataeveb482352015-12-18 05:05:56 +00006140 if (!ErrorFound && !SC &&
6141 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006142 // OpenMP [2.8.1,simd Construct, Restrictions]
6143 // An ordered construct with the simd clause is the only OpenMP construct
6144 // that can appear in the simd region.
6145 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00006146 ErrorFound = true;
6147 } else if (DependFound && (TC || SC)) {
6148 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
6149 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
6150 ErrorFound = true;
6151 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
6152 Diag(DependFound->getLocStart(),
6153 diag::err_omp_ordered_directive_without_param);
6154 ErrorFound = true;
6155 } else if (TC || Clauses.empty()) {
6156 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
6157 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
6158 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
6159 << (TC != nullptr);
6160 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
6161 ErrorFound = true;
6162 }
6163 }
6164 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006165 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00006166
6167 if (AStmt) {
6168 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6169
6170 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006171 }
Alexey Bataev346265e2015-09-25 10:37:12 +00006172
6173 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006174}
6175
Alexey Bataev1d160b12015-03-13 12:27:31 +00006176namespace {
6177/// \brief Helper class for checking expression in 'omp atomic [update]'
6178/// construct.
6179class OpenMPAtomicUpdateChecker {
6180 /// \brief Error results for atomic update expressions.
6181 enum ExprAnalysisErrorCode {
6182 /// \brief A statement is not an expression statement.
6183 NotAnExpression,
6184 /// \brief Expression is not builtin binary or unary operation.
6185 NotABinaryOrUnaryExpression,
6186 /// \brief Unary operation is not post-/pre- increment/decrement operation.
6187 NotAnUnaryIncDecExpression,
6188 /// \brief An expression is not of scalar type.
6189 NotAScalarType,
6190 /// \brief A binary operation is not an assignment operation.
6191 NotAnAssignmentOp,
6192 /// \brief RHS part of the binary operation is not a binary expression.
6193 NotABinaryExpression,
6194 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
6195 /// expression.
6196 NotABinaryOperator,
6197 /// \brief RHS binary operation does not have reference to the updated LHS
6198 /// part.
6199 NotAnUpdateExpression,
6200 /// \brief No errors is found.
6201 NoError
6202 };
6203 /// \brief Reference to Sema.
6204 Sema &SemaRef;
6205 /// \brief A location for note diagnostics (when error is found).
6206 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006207 /// \brief 'x' lvalue part of the source atomic expression.
6208 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006209 /// \brief 'expr' rvalue part of the source atomic expression.
6210 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006211 /// \brief Helper expression of the form
6212 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6213 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6214 Expr *UpdateExpr;
6215 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
6216 /// important for non-associative operations.
6217 bool IsXLHSInRHSPart;
6218 BinaryOperatorKind Op;
6219 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006220 /// \brief true if the source expression is a postfix unary operation, false
6221 /// if it is a prefix unary operation.
6222 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006223
6224public:
6225 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00006226 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00006227 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00006228 /// \brief Check specified statement that it is suitable for 'atomic update'
6229 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00006230 /// expression. If DiagId and NoteId == 0, then only check is performed
6231 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006232 /// \param DiagId Diagnostic which should be emitted if error is found.
6233 /// \param NoteId Diagnostic note for the main error message.
6234 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00006235 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00006236 /// \brief Return the 'x' lvalue part of the source atomic expression.
6237 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00006238 /// \brief Return the 'expr' rvalue part of the source atomic expression.
6239 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00006240 /// \brief Return the update expression used in calculation of the updated
6241 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6242 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6243 Expr *getUpdateExpr() const { return UpdateExpr; }
6244 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
6245 /// false otherwise.
6246 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
6247
Alexey Bataevb78ca832015-04-01 03:33:17 +00006248 /// \brief true if the source expression is a postfix unary operation, false
6249 /// if it is a prefix unary operation.
6250 bool isPostfixUpdate() const { return IsPostfixUpdate; }
6251
Alexey Bataev1d160b12015-03-13 12:27:31 +00006252private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00006253 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
6254 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00006255};
6256} // namespace
6257
6258bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
6259 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
6260 ExprAnalysisErrorCode ErrorFound = NoError;
6261 SourceLocation ErrorLoc, NoteLoc;
6262 SourceRange ErrorRange, NoteRange;
6263 // Allowed constructs are:
6264 // x = x binop expr;
6265 // x = expr binop x;
6266 if (AtomicBinOp->getOpcode() == BO_Assign) {
6267 X = AtomicBinOp->getLHS();
6268 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
6269 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
6270 if (AtomicInnerBinOp->isMultiplicativeOp() ||
6271 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
6272 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006273 Op = AtomicInnerBinOp->getOpcode();
6274 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006275 auto *LHS = AtomicInnerBinOp->getLHS();
6276 auto *RHS = AtomicInnerBinOp->getRHS();
6277 llvm::FoldingSetNodeID XId, LHSId, RHSId;
6278 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
6279 /*Canonical=*/true);
6280 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
6281 /*Canonical=*/true);
6282 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
6283 /*Canonical=*/true);
6284 if (XId == LHSId) {
6285 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006286 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006287 } else if (XId == RHSId) {
6288 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006289 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006290 } else {
6291 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6292 ErrorRange = AtomicInnerBinOp->getSourceRange();
6293 NoteLoc = X->getExprLoc();
6294 NoteRange = X->getSourceRange();
6295 ErrorFound = NotAnUpdateExpression;
6296 }
6297 } else {
6298 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6299 ErrorRange = AtomicInnerBinOp->getSourceRange();
6300 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
6301 NoteRange = SourceRange(NoteLoc, NoteLoc);
6302 ErrorFound = NotABinaryOperator;
6303 }
6304 } else {
6305 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
6306 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
6307 ErrorFound = NotABinaryExpression;
6308 }
6309 } else {
6310 ErrorLoc = AtomicBinOp->getExprLoc();
6311 ErrorRange = AtomicBinOp->getSourceRange();
6312 NoteLoc = AtomicBinOp->getOperatorLoc();
6313 NoteRange = SourceRange(NoteLoc, NoteLoc);
6314 ErrorFound = NotAnAssignmentOp;
6315 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006316 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006317 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6318 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6319 return true;
6320 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006321 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006322 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006323}
6324
6325bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
6326 unsigned NoteId) {
6327 ExprAnalysisErrorCode ErrorFound = NoError;
6328 SourceLocation ErrorLoc, NoteLoc;
6329 SourceRange ErrorRange, NoteRange;
6330 // Allowed constructs are:
6331 // x++;
6332 // x--;
6333 // ++x;
6334 // --x;
6335 // x binop= expr;
6336 // x = x binop expr;
6337 // x = expr binop x;
6338 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
6339 AtomicBody = AtomicBody->IgnoreParenImpCasts();
6340 if (AtomicBody->getType()->isScalarType() ||
6341 AtomicBody->isInstantiationDependent()) {
6342 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
6343 AtomicBody->IgnoreParenImpCasts())) {
6344 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00006345 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006346 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00006347 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006348 E = AtomicCompAssignOp->getRHS();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006349 X = AtomicCompAssignOp->getLHS()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006350 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006351 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
6352 AtomicBody->IgnoreParenImpCasts())) {
6353 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00006354 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
6355 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006356 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00006357 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
6358 // Check for Unary Operation
6359 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006360 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006361 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
6362 OpLoc = AtomicUnaryOp->getOperatorLoc();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006363 X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006364 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
6365 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006366 } else {
6367 ErrorFound = NotAnUnaryIncDecExpression;
6368 ErrorLoc = AtomicUnaryOp->getExprLoc();
6369 ErrorRange = AtomicUnaryOp->getSourceRange();
6370 NoteLoc = AtomicUnaryOp->getOperatorLoc();
6371 NoteRange = SourceRange(NoteLoc, NoteLoc);
6372 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006373 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006374 ErrorFound = NotABinaryOrUnaryExpression;
6375 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
6376 NoteRange = ErrorRange = AtomicBody->getSourceRange();
6377 }
6378 } else {
6379 ErrorFound = NotAScalarType;
6380 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
6381 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6382 }
6383 } else {
6384 ErrorFound = NotAnExpression;
6385 NoteLoc = ErrorLoc = S->getLocStart();
6386 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6387 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006388 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006389 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6390 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6391 return true;
6392 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006393 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006394 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006395 // Build an update expression of form 'OpaqueValueExpr(x) binop
6396 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
6397 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
6398 auto *OVEX = new (SemaRef.getASTContext())
6399 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
6400 auto *OVEExpr = new (SemaRef.getASTContext())
6401 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
6402 auto Update =
6403 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
6404 IsXLHSInRHSPart ? OVEExpr : OVEX);
6405 if (Update.isInvalid())
6406 return true;
6407 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
6408 Sema::AA_Casting);
6409 if (Update.isInvalid())
6410 return true;
6411 UpdateExpr = Update.get();
6412 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00006413 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006414}
6415
Alexey Bataev0162e452014-07-22 10:10:35 +00006416StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
6417 Stmt *AStmt,
6418 SourceLocation StartLoc,
6419 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006420 if (!AStmt)
6421 return StmtError();
6422
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006423 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00006424 // 1.2.2 OpenMP Language Terminology
6425 // Structured block - An executable statement with a single entry at the
6426 // top and a single exit at the bottom.
6427 // The point of exit cannot be a branch out of the structured block.
6428 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00006429 OpenMPClauseKind AtomicKind = OMPC_unknown;
6430 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006431 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00006432 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00006433 C->getClauseKind() == OMPC_update ||
6434 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00006435 if (AtomicKind != OMPC_unknown) {
6436 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
6437 << SourceRange(C->getLocStart(), C->getLocEnd());
6438 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
6439 << getOpenMPClauseName(AtomicKind);
6440 } else {
6441 AtomicKind = C->getClauseKind();
6442 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006443 }
6444 }
6445 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006446
Alexey Bataev459dec02014-07-24 06:46:57 +00006447 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00006448 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
6449 Body = EWC->getSubExpr();
6450
Alexey Bataev62cec442014-11-18 10:14:22 +00006451 Expr *X = nullptr;
6452 Expr *V = nullptr;
6453 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006454 Expr *UE = nullptr;
6455 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006456 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00006457 // OpenMP [2.12.6, atomic Construct]
6458 // In the next expressions:
6459 // * x and v (as applicable) are both l-value expressions with scalar type.
6460 // * During the execution of an atomic region, multiple syntactic
6461 // occurrences of x must designate the same storage location.
6462 // * Neither of v and expr (as applicable) may access the storage location
6463 // designated by x.
6464 // * Neither of x and expr (as applicable) may access the storage location
6465 // designated by v.
6466 // * expr is an expression with scalar type.
6467 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
6468 // * binop, binop=, ++, and -- are not overloaded operators.
6469 // * The expression x binop expr must be numerically equivalent to x binop
6470 // (expr). This requirement is satisfied if the operators in expr have
6471 // precedence greater than binop, or by using parentheses around expr or
6472 // subexpressions of expr.
6473 // * The expression expr binop x must be numerically equivalent to (expr)
6474 // binop x. This requirement is satisfied if the operators in expr have
6475 // precedence equal to or greater than binop, or by using parentheses around
6476 // expr or subexpressions of expr.
6477 // * For forms that allow multiple occurrences of x, the number of times
6478 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00006479 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006480 enum {
6481 NotAnExpression,
6482 NotAnAssignmentOp,
6483 NotAScalarType,
6484 NotAnLValue,
6485 NoError
6486 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00006487 SourceLocation ErrorLoc, NoteLoc;
6488 SourceRange ErrorRange, NoteRange;
6489 // If clause is read:
6490 // v = x;
6491 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
6492 auto AtomicBinOp =
6493 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6494 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6495 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6496 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
6497 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6498 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
6499 if (!X->isLValue() || !V->isLValue()) {
6500 auto NotLValueExpr = X->isLValue() ? V : X;
6501 ErrorFound = NotAnLValue;
6502 ErrorLoc = AtomicBinOp->getExprLoc();
6503 ErrorRange = AtomicBinOp->getSourceRange();
6504 NoteLoc = NotLValueExpr->getExprLoc();
6505 NoteRange = NotLValueExpr->getSourceRange();
6506 }
6507 } else if (!X->isInstantiationDependent() ||
6508 !V->isInstantiationDependent()) {
6509 auto NotScalarExpr =
6510 (X->isInstantiationDependent() || X->getType()->isScalarType())
6511 ? V
6512 : X;
6513 ErrorFound = NotAScalarType;
6514 ErrorLoc = AtomicBinOp->getExprLoc();
6515 ErrorRange = AtomicBinOp->getSourceRange();
6516 NoteLoc = NotScalarExpr->getExprLoc();
6517 NoteRange = NotScalarExpr->getSourceRange();
6518 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006519 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00006520 ErrorFound = NotAnAssignmentOp;
6521 ErrorLoc = AtomicBody->getExprLoc();
6522 ErrorRange = AtomicBody->getSourceRange();
6523 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6524 : AtomicBody->getExprLoc();
6525 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6526 : AtomicBody->getSourceRange();
6527 }
6528 } else {
6529 ErrorFound = NotAnExpression;
6530 NoteLoc = ErrorLoc = Body->getLocStart();
6531 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006532 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006533 if (ErrorFound != NoError) {
6534 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
6535 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006536 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6537 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00006538 return StmtError();
6539 } else if (CurContext->isDependentContext())
6540 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00006541 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006542 enum {
6543 NotAnExpression,
6544 NotAnAssignmentOp,
6545 NotAScalarType,
6546 NotAnLValue,
6547 NoError
6548 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006549 SourceLocation ErrorLoc, NoteLoc;
6550 SourceRange ErrorRange, NoteRange;
6551 // If clause is write:
6552 // x = expr;
6553 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
6554 auto AtomicBinOp =
6555 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6556 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00006557 X = AtomicBinOp->getLHS();
6558 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006559 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6560 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
6561 if (!X->isLValue()) {
6562 ErrorFound = NotAnLValue;
6563 ErrorLoc = AtomicBinOp->getExprLoc();
6564 ErrorRange = AtomicBinOp->getSourceRange();
6565 NoteLoc = X->getExprLoc();
6566 NoteRange = X->getSourceRange();
6567 }
6568 } else if (!X->isInstantiationDependent() ||
6569 !E->isInstantiationDependent()) {
6570 auto NotScalarExpr =
6571 (X->isInstantiationDependent() || X->getType()->isScalarType())
6572 ? E
6573 : X;
6574 ErrorFound = NotAScalarType;
6575 ErrorLoc = AtomicBinOp->getExprLoc();
6576 ErrorRange = AtomicBinOp->getSourceRange();
6577 NoteLoc = NotScalarExpr->getExprLoc();
6578 NoteRange = NotScalarExpr->getSourceRange();
6579 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006580 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00006581 ErrorFound = NotAnAssignmentOp;
6582 ErrorLoc = AtomicBody->getExprLoc();
6583 ErrorRange = AtomicBody->getSourceRange();
6584 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6585 : AtomicBody->getExprLoc();
6586 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6587 : AtomicBody->getSourceRange();
6588 }
6589 } else {
6590 ErrorFound = NotAnExpression;
6591 NoteLoc = ErrorLoc = Body->getLocStart();
6592 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006593 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00006594 if (ErrorFound != NoError) {
6595 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
6596 << ErrorRange;
6597 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6598 << NoteRange;
6599 return StmtError();
6600 } else if (CurContext->isDependentContext())
6601 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006602 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006603 // If clause is update:
6604 // x++;
6605 // x--;
6606 // ++x;
6607 // --x;
6608 // x binop= expr;
6609 // x = x binop expr;
6610 // x = expr binop x;
6611 OpenMPAtomicUpdateChecker Checker(*this);
6612 if (Checker.checkStatement(
6613 Body, (AtomicKind == OMPC_update)
6614 ? diag::err_omp_atomic_update_not_expression_statement
6615 : diag::err_omp_atomic_not_expression_statement,
6616 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00006617 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006618 if (!CurContext->isDependentContext()) {
6619 E = Checker.getExpr();
6620 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006621 UE = Checker.getUpdateExpr();
6622 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00006623 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006624 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006625 enum {
6626 NotAnAssignmentOp,
6627 NotACompoundStatement,
6628 NotTwoSubstatements,
6629 NotASpecificExpression,
6630 NoError
6631 } ErrorFound = NoError;
6632 SourceLocation ErrorLoc, NoteLoc;
6633 SourceRange ErrorRange, NoteRange;
6634 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6635 // If clause is a capture:
6636 // v = x++;
6637 // v = x--;
6638 // v = ++x;
6639 // v = --x;
6640 // v = x binop= expr;
6641 // v = x = x binop expr;
6642 // v = x = expr binop x;
6643 auto *AtomicBinOp =
6644 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6645 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6646 V = AtomicBinOp->getLHS();
6647 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6648 OpenMPAtomicUpdateChecker Checker(*this);
6649 if (Checker.checkStatement(
6650 Body, diag::err_omp_atomic_capture_not_expression_statement,
6651 diag::note_omp_atomic_update))
6652 return StmtError();
6653 E = Checker.getExpr();
6654 X = Checker.getX();
6655 UE = Checker.getUpdateExpr();
6656 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6657 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00006658 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006659 ErrorLoc = AtomicBody->getExprLoc();
6660 ErrorRange = AtomicBody->getSourceRange();
6661 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6662 : AtomicBody->getExprLoc();
6663 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6664 : AtomicBody->getSourceRange();
6665 ErrorFound = NotAnAssignmentOp;
6666 }
6667 if (ErrorFound != NoError) {
6668 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6669 << ErrorRange;
6670 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6671 return StmtError();
6672 } else if (CurContext->isDependentContext()) {
6673 UE = V = E = X = nullptr;
6674 }
6675 } else {
6676 // If clause is a capture:
6677 // { v = x; x = expr; }
6678 // { v = x; x++; }
6679 // { v = x; x--; }
6680 // { v = x; ++x; }
6681 // { v = x; --x; }
6682 // { v = x; x binop= expr; }
6683 // { v = x; x = x binop expr; }
6684 // { v = x; x = expr binop x; }
6685 // { x++; v = x; }
6686 // { x--; v = x; }
6687 // { ++x; v = x; }
6688 // { --x; v = x; }
6689 // { x binop= expr; v = x; }
6690 // { x = x binop expr; v = x; }
6691 // { x = expr binop x; v = x; }
6692 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6693 // Check that this is { expr1; expr2; }
6694 if (CS->size() == 2) {
6695 auto *First = CS->body_front();
6696 auto *Second = CS->body_back();
6697 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6698 First = EWC->getSubExpr()->IgnoreParenImpCasts();
6699 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6700 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6701 // Need to find what subexpression is 'v' and what is 'x'.
6702 OpenMPAtomicUpdateChecker Checker(*this);
6703 bool IsUpdateExprFound = !Checker.checkStatement(Second);
6704 BinaryOperator *BinOp = nullptr;
6705 if (IsUpdateExprFound) {
6706 BinOp = dyn_cast<BinaryOperator>(First);
6707 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6708 }
6709 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6710 // { v = x; x++; }
6711 // { v = x; x--; }
6712 // { v = x; ++x; }
6713 // { v = x; --x; }
6714 // { v = x; x binop= expr; }
6715 // { v = x; x = x binop expr; }
6716 // { v = x; x = expr binop x; }
6717 // Check that the first expression has form v = x.
6718 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6719 llvm::FoldingSetNodeID XId, PossibleXId;
6720 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6721 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6722 IsUpdateExprFound = XId == PossibleXId;
6723 if (IsUpdateExprFound) {
6724 V = BinOp->getLHS();
6725 X = Checker.getX();
6726 E = Checker.getExpr();
6727 UE = Checker.getUpdateExpr();
6728 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006729 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006730 }
6731 }
6732 if (!IsUpdateExprFound) {
6733 IsUpdateExprFound = !Checker.checkStatement(First);
6734 BinOp = nullptr;
6735 if (IsUpdateExprFound) {
6736 BinOp = dyn_cast<BinaryOperator>(Second);
6737 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6738 }
6739 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6740 // { x++; v = x; }
6741 // { x--; v = x; }
6742 // { ++x; v = x; }
6743 // { --x; v = x; }
6744 // { x binop= expr; v = x; }
6745 // { x = x binop expr; v = x; }
6746 // { x = expr binop x; v = x; }
6747 // Check that the second expression has form v = x.
6748 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6749 llvm::FoldingSetNodeID XId, PossibleXId;
6750 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6751 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6752 IsUpdateExprFound = XId == PossibleXId;
6753 if (IsUpdateExprFound) {
6754 V = BinOp->getLHS();
6755 X = Checker.getX();
6756 E = Checker.getExpr();
6757 UE = Checker.getUpdateExpr();
6758 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006759 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006760 }
6761 }
6762 }
6763 if (!IsUpdateExprFound) {
6764 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00006765 auto *FirstExpr = dyn_cast<Expr>(First);
6766 auto *SecondExpr = dyn_cast<Expr>(Second);
6767 if (!FirstExpr || !SecondExpr ||
6768 !(FirstExpr->isInstantiationDependent() ||
6769 SecondExpr->isInstantiationDependent())) {
6770 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6771 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006772 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00006773 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
6774 : First->getLocStart();
6775 NoteRange = ErrorRange = FirstBinOp
6776 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00006777 : SourceRange(ErrorLoc, ErrorLoc);
6778 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006779 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6780 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6781 ErrorFound = NotAnAssignmentOp;
6782 NoteLoc = ErrorLoc = SecondBinOp
6783 ? SecondBinOp->getOperatorLoc()
6784 : Second->getLocStart();
6785 NoteRange = ErrorRange =
6786 SecondBinOp ? SecondBinOp->getSourceRange()
6787 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00006788 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006789 auto *PossibleXRHSInFirst =
6790 FirstBinOp->getRHS()->IgnoreParenImpCasts();
6791 auto *PossibleXLHSInSecond =
6792 SecondBinOp->getLHS()->IgnoreParenImpCasts();
6793 llvm::FoldingSetNodeID X1Id, X2Id;
6794 PossibleXRHSInFirst->Profile(X1Id, Context,
6795 /*Canonical=*/true);
6796 PossibleXLHSInSecond->Profile(X2Id, Context,
6797 /*Canonical=*/true);
6798 IsUpdateExprFound = X1Id == X2Id;
6799 if (IsUpdateExprFound) {
6800 V = FirstBinOp->getLHS();
6801 X = SecondBinOp->getLHS();
6802 E = SecondBinOp->getRHS();
6803 UE = nullptr;
6804 IsXLHSInRHSPart = false;
6805 IsPostfixUpdate = true;
6806 } else {
6807 ErrorFound = NotASpecificExpression;
6808 ErrorLoc = FirstBinOp->getExprLoc();
6809 ErrorRange = FirstBinOp->getSourceRange();
6810 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6811 NoteRange = SecondBinOp->getRHS()->getSourceRange();
6812 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006813 }
6814 }
6815 }
6816 }
6817 } else {
6818 NoteLoc = ErrorLoc = Body->getLocStart();
6819 NoteRange = ErrorRange =
6820 SourceRange(Body->getLocStart(), Body->getLocStart());
6821 ErrorFound = NotTwoSubstatements;
6822 }
6823 } else {
6824 NoteLoc = ErrorLoc = Body->getLocStart();
6825 NoteRange = ErrorRange =
6826 SourceRange(Body->getLocStart(), Body->getLocStart());
6827 ErrorFound = NotACompoundStatement;
6828 }
6829 if (ErrorFound != NoError) {
6830 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6831 << ErrorRange;
6832 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6833 return StmtError();
6834 } else if (CurContext->isDependentContext()) {
6835 UE = V = E = X = nullptr;
6836 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006837 }
Alexey Bataevdea47612014-07-23 07:46:59 +00006838 }
Alexey Bataev0162e452014-07-22 10:10:35 +00006839
6840 getCurFunction()->setHasBranchProtectedScope();
6841
Alexey Bataev62cec442014-11-18 10:14:22 +00006842 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00006843 X, V, E, UE, IsXLHSInRHSPart,
6844 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00006845}
6846
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006847StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6848 Stmt *AStmt,
6849 SourceLocation StartLoc,
6850 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006851 if (!AStmt)
6852 return StmtError();
6853
Samuel Antao4af1b7b2015-12-02 17:44:43 +00006854 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6855 // 1.2.2 OpenMP Language Terminology
6856 // Structured block - An executable statement with a single entry at the
6857 // top and a single exit at the bottom.
6858 // The point of exit cannot be a branch out of the structured block.
6859 // longjmp() and throw() must not violate the entry/exit criteria.
6860 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006861
Alexey Bataev13314bf2014-10-09 04:18:56 +00006862 // OpenMP [2.16, Nesting of Regions]
6863 // If specified, a teams construct must be contained within a target
6864 // construct. That target construct must contain no statements or directives
6865 // outside of the teams construct.
6866 if (DSAStack->hasInnerTeamsRegion()) {
6867 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
6868 bool OMPTeamsFound = true;
6869 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
6870 auto I = CS->body_begin();
6871 while (I != CS->body_end()) {
6872 auto OED = dyn_cast<OMPExecutableDirective>(*I);
6873 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6874 OMPTeamsFound = false;
6875 break;
6876 }
6877 ++I;
6878 }
6879 assert(I != CS->body_end() && "Not found statement");
6880 S = *I;
Kelvin Li3834dce2016-06-27 19:15:43 +00006881 } else {
6882 auto *OED = dyn_cast<OMPExecutableDirective>(S);
6883 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
Alexey Bataev13314bf2014-10-09 04:18:56 +00006884 }
6885 if (!OMPTeamsFound) {
6886 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6887 Diag(DSAStack->getInnerTeamsRegionLoc(),
6888 diag::note_omp_nested_teams_construct_here);
6889 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
6890 << isa<OMPExecutableDirective>(S);
6891 return StmtError();
6892 }
6893 }
6894
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006895 getCurFunction()->setHasBranchProtectedScope();
6896
6897 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6898}
6899
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006900StmtResult
6901Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6902 Stmt *AStmt, SourceLocation StartLoc,
6903 SourceLocation EndLoc) {
6904 if (!AStmt)
6905 return StmtError();
6906
6907 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6908 // 1.2.2 OpenMP Language Terminology
6909 // Structured block - An executable statement with a single entry at the
6910 // top and a single exit at the bottom.
6911 // The point of exit cannot be a branch out of the structured block.
6912 // longjmp() and throw() must not violate the entry/exit criteria.
6913 CS->getCapturedDecl()->setNothrow();
6914
6915 getCurFunction()->setHasBranchProtectedScope();
6916
6917 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6918 AStmt);
6919}
6920
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006921StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6922 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6923 SourceLocation EndLoc,
6924 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6925 if (!AStmt)
6926 return StmtError();
6927
6928 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6929 // 1.2.2 OpenMP Language Terminology
6930 // Structured block - An executable statement with a single entry at the
6931 // top and a single exit at the bottom.
6932 // The point of exit cannot be a branch out of the structured block.
6933 // longjmp() and throw() must not violate the entry/exit criteria.
6934 CS->getCapturedDecl()->setNothrow();
6935
6936 OMPLoopDirective::HelperExprs B;
6937 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6938 // define the nested loops number.
6939 unsigned NestedLoopCount =
6940 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
6941 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6942 VarsWithImplicitDSA, B);
6943 if (NestedLoopCount == 0)
6944 return StmtError();
6945
6946 assert((CurContext->isDependentContext() || B.builtAll()) &&
6947 "omp target parallel for loop exprs were not built");
6948
6949 if (!CurContext->isDependentContext()) {
6950 // Finalize the clauses that need pre-built expressions for CodeGen.
6951 for (auto C : Clauses) {
6952 if (auto LC = dyn_cast<OMPLinearClause>(C))
6953 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006954 B.NumIterations, *this, CurScope,
6955 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006956 return StmtError();
6957 }
6958 }
6959
6960 getCurFunction()->setHasBranchProtectedScope();
6961 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
6962 NestedLoopCount, Clauses, AStmt,
6963 B, DSAStack->isCancelRegion());
6964}
6965
Samuel Antaodf67fc42016-01-19 19:15:56 +00006966/// \brief Check for existence of a map clause in the list of clauses.
6967static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
6968 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
6969 I != E; ++I) {
6970 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
6971 return true;
6972 }
6973 }
6974
6975 return false;
6976}
6977
Michael Wong65f367f2015-07-21 13:44:28 +00006978StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6979 Stmt *AStmt,
6980 SourceLocation StartLoc,
6981 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006982 if (!AStmt)
6983 return StmtError();
6984
6985 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6986
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006987 // OpenMP [2.10.1, Restrictions, p. 97]
6988 // At least one map clause must appear on the directive.
6989 if (!HasMapClause(Clauses)) {
6990 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
6991 getOpenMPDirectiveName(OMPD_target_data);
6992 return StmtError();
6993 }
6994
Michael Wong65f367f2015-07-21 13:44:28 +00006995 getCurFunction()->setHasBranchProtectedScope();
6996
6997 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6998 AStmt);
6999}
7000
Samuel Antaodf67fc42016-01-19 19:15:56 +00007001StmtResult
7002Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
7003 SourceLocation StartLoc,
7004 SourceLocation EndLoc) {
7005 // OpenMP [2.10.2, Restrictions, p. 99]
7006 // At least one map clause must appear on the directive.
7007 if (!HasMapClause(Clauses)) {
7008 Diag(StartLoc, diag::err_omp_no_map_for_directive)
7009 << getOpenMPDirectiveName(OMPD_target_enter_data);
7010 return StmtError();
7011 }
7012
7013 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
7014 Clauses);
7015}
7016
Samuel Antao72590762016-01-19 20:04:50 +00007017StmtResult
7018Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
7019 SourceLocation StartLoc,
7020 SourceLocation EndLoc) {
7021 // OpenMP [2.10.3, Restrictions, p. 102]
7022 // At least one map clause must appear on the directive.
7023 if (!HasMapClause(Clauses)) {
7024 Diag(StartLoc, diag::err_omp_no_map_for_directive)
7025 << getOpenMPDirectiveName(OMPD_target_exit_data);
7026 return StmtError();
7027 }
7028
7029 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
7030}
7031
Samuel Antao686c70c2016-05-26 17:30:50 +00007032StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
7033 SourceLocation StartLoc,
7034 SourceLocation EndLoc) {
Samuel Antao686c70c2016-05-26 17:30:50 +00007035 bool seenMotionClause = false;
Samuel Antao661c0902016-05-26 17:39:58 +00007036 for (auto *C : Clauses) {
Samuel Antaoec172c62016-05-26 17:49:04 +00007037 if (C->getClauseKind() == OMPC_to || C->getClauseKind() == OMPC_from)
Samuel Antao661c0902016-05-26 17:39:58 +00007038 seenMotionClause = true;
7039 }
Samuel Antao686c70c2016-05-26 17:30:50 +00007040 if (!seenMotionClause) {
7041 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
7042 return StmtError();
7043 }
7044 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses);
7045}
7046
Alexey Bataev13314bf2014-10-09 04:18:56 +00007047StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
7048 Stmt *AStmt, SourceLocation StartLoc,
7049 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007050 if (!AStmt)
7051 return StmtError();
7052
Alexey Bataev13314bf2014-10-09 04:18:56 +00007053 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7054 // 1.2.2 OpenMP Language Terminology
7055 // Structured block - An executable statement with a single entry at the
7056 // top and a single exit at the bottom.
7057 // The point of exit cannot be a branch out of the structured block.
7058 // longjmp() and throw() must not violate the entry/exit criteria.
7059 CS->getCapturedDecl()->setNothrow();
7060
7061 getCurFunction()->setHasBranchProtectedScope();
7062
7063 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
7064}
7065
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007066StmtResult
7067Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
7068 SourceLocation EndLoc,
7069 OpenMPDirectiveKind CancelRegion) {
7070 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
7071 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
7072 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
7073 << getOpenMPDirectiveName(CancelRegion);
7074 return StmtError();
7075 }
7076 if (DSAStack->isParentNowaitRegion()) {
7077 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
7078 return StmtError();
7079 }
7080 if (DSAStack->isParentOrderedRegion()) {
7081 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
7082 return StmtError();
7083 }
7084 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
7085 CancelRegion);
7086}
7087
Alexey Bataev87933c72015-09-18 08:07:34 +00007088StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
7089 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00007090 SourceLocation EndLoc,
7091 OpenMPDirectiveKind CancelRegion) {
7092 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
7093 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
7094 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
7095 << getOpenMPDirectiveName(CancelRegion);
7096 return StmtError();
7097 }
7098 if (DSAStack->isParentNowaitRegion()) {
7099 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
7100 return StmtError();
7101 }
7102 if (DSAStack->isParentOrderedRegion()) {
7103 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
7104 return StmtError();
7105 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00007106 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00007107 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
7108 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00007109}
7110
Alexey Bataev382967a2015-12-08 12:06:20 +00007111static bool checkGrainsizeNumTasksClauses(Sema &S,
7112 ArrayRef<OMPClause *> Clauses) {
7113 OMPClause *PrevClause = nullptr;
7114 bool ErrorFound = false;
7115 for (auto *C : Clauses) {
7116 if (C->getClauseKind() == OMPC_grainsize ||
7117 C->getClauseKind() == OMPC_num_tasks) {
7118 if (!PrevClause)
7119 PrevClause = C;
7120 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
7121 S.Diag(C->getLocStart(),
7122 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
7123 << getOpenMPClauseName(C->getClauseKind())
7124 << getOpenMPClauseName(PrevClause->getClauseKind());
7125 S.Diag(PrevClause->getLocStart(),
7126 diag::note_omp_previous_grainsize_num_tasks)
7127 << getOpenMPClauseName(PrevClause->getClauseKind());
7128 ErrorFound = true;
7129 }
7130 }
7131 }
7132 return ErrorFound;
7133}
7134
Alexey Bataev49f6e782015-12-01 04:18:41 +00007135StmtResult Sema::ActOnOpenMPTaskLoopDirective(
7136 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7137 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007138 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00007139 if (!AStmt)
7140 return StmtError();
7141
7142 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7143 OMPLoopDirective::HelperExprs B;
7144 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7145 // define the nested loops number.
7146 unsigned NestedLoopCount =
7147 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007148 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00007149 VarsWithImplicitDSA, B);
7150 if (NestedLoopCount == 0)
7151 return StmtError();
7152
7153 assert((CurContext->isDependentContext() || B.builtAll()) &&
7154 "omp for loop exprs were not built");
7155
Alexey Bataev382967a2015-12-08 12:06:20 +00007156 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7157 // The grainsize clause and num_tasks clause are mutually exclusive and may
7158 // not appear on the same taskloop directive.
7159 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7160 return StmtError();
7161
Alexey Bataev49f6e782015-12-01 04:18:41 +00007162 getCurFunction()->setHasBranchProtectedScope();
7163 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
7164 NestedLoopCount, Clauses, AStmt, B);
7165}
7166
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007167StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
7168 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7169 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007170 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007171 if (!AStmt)
7172 return StmtError();
7173
7174 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7175 OMPLoopDirective::HelperExprs B;
7176 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7177 // define the nested loops number.
7178 unsigned NestedLoopCount =
7179 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
7180 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
7181 VarsWithImplicitDSA, B);
7182 if (NestedLoopCount == 0)
7183 return StmtError();
7184
7185 assert((CurContext->isDependentContext() || B.builtAll()) &&
7186 "omp for loop exprs were not built");
7187
Alexey Bataev5a3af132016-03-29 08:58:54 +00007188 if (!CurContext->isDependentContext()) {
7189 // Finalize the clauses that need pre-built expressions for CodeGen.
7190 for (auto C : Clauses) {
7191 if (auto LC = dyn_cast<OMPLinearClause>(C))
7192 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007193 B.NumIterations, *this, CurScope,
7194 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007195 return StmtError();
7196 }
7197 }
7198
Alexey Bataev382967a2015-12-08 12:06:20 +00007199 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7200 // The grainsize clause and num_tasks clause are mutually exclusive and may
7201 // not appear on the same taskloop directive.
7202 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7203 return StmtError();
7204
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007205 getCurFunction()->setHasBranchProtectedScope();
7206 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
7207 NestedLoopCount, Clauses, AStmt, B);
7208}
7209
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007210StmtResult Sema::ActOnOpenMPDistributeDirective(
7211 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7212 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007213 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007214 if (!AStmt)
7215 return StmtError();
7216
7217 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7218 OMPLoopDirective::HelperExprs B;
7219 // In presence of clause 'collapse' with number of loops, it will
7220 // define the nested loops number.
7221 unsigned NestedLoopCount =
7222 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
7223 nullptr /*ordered not a clause on distribute*/, AStmt,
7224 *this, *DSAStack, VarsWithImplicitDSA, B);
7225 if (NestedLoopCount == 0)
7226 return StmtError();
7227
7228 assert((CurContext->isDependentContext() || B.builtAll()) &&
7229 "omp for loop exprs were not built");
7230
7231 getCurFunction()->setHasBranchProtectedScope();
7232 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
7233 NestedLoopCount, Clauses, AStmt, B);
7234}
7235
Carlo Bertolli9925f152016-06-27 14:55:37 +00007236StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
7237 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7238 SourceLocation EndLoc,
7239 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7240 if (!AStmt)
7241 return StmtError();
7242
7243 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7244 // 1.2.2 OpenMP Language Terminology
7245 // Structured block - An executable statement with a single entry at the
7246 // top and a single exit at the bottom.
7247 // The point of exit cannot be a branch out of the structured block.
7248 // longjmp() and throw() must not violate the entry/exit criteria.
7249 CS->getCapturedDecl()->setNothrow();
7250
7251 OMPLoopDirective::HelperExprs B;
7252 // In presence of clause 'collapse' with number of loops, it will
7253 // define the nested loops number.
7254 unsigned NestedLoopCount = CheckOpenMPLoop(
7255 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
7256 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
7257 VarsWithImplicitDSA, B);
7258 if (NestedLoopCount == 0)
7259 return StmtError();
7260
7261 assert((CurContext->isDependentContext() || B.builtAll()) &&
7262 "omp for loop exprs were not built");
7263
7264 getCurFunction()->setHasBranchProtectedScope();
7265 return OMPDistributeParallelForDirective::Create(
7266 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7267}
7268
Kelvin Li4a39add2016-07-05 05:00:15 +00007269StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
7270 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7271 SourceLocation EndLoc,
7272 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7273 if (!AStmt)
7274 return StmtError();
7275
7276 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7277 // 1.2.2 OpenMP Language Terminology
7278 // Structured block - An executable statement with a single entry at the
7279 // top and a single exit at the bottom.
7280 // The point of exit cannot be a branch out of the structured block.
7281 // longjmp() and throw() must not violate the entry/exit criteria.
7282 CS->getCapturedDecl()->setNothrow();
7283
7284 OMPLoopDirective::HelperExprs B;
7285 // In presence of clause 'collapse' with number of loops, it will
7286 // define the nested loops number.
7287 unsigned NestedLoopCount = CheckOpenMPLoop(
7288 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
7289 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
7290 VarsWithImplicitDSA, B);
7291 if (NestedLoopCount == 0)
7292 return StmtError();
7293
7294 assert((CurContext->isDependentContext() || B.builtAll()) &&
7295 "omp for loop exprs were not built");
7296
Kelvin Lic5609492016-07-15 04:39:07 +00007297 if (checkSimdlenSafelenSpecified(*this, Clauses))
7298 return StmtError();
7299
Kelvin Li4a39add2016-07-05 05:00:15 +00007300 getCurFunction()->setHasBranchProtectedScope();
7301 return OMPDistributeParallelForSimdDirective::Create(
7302 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7303}
7304
Kelvin Li787f3fc2016-07-06 04:45:38 +00007305StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
7306 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7307 SourceLocation EndLoc,
7308 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7309 if (!AStmt)
7310 return StmtError();
7311
7312 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7313 // 1.2.2 OpenMP Language Terminology
7314 // Structured block - An executable statement with a single entry at the
7315 // top and a single exit at the bottom.
7316 // The point of exit cannot be a branch out of the structured block.
7317 // longjmp() and throw() must not violate the entry/exit criteria.
7318 CS->getCapturedDecl()->setNothrow();
7319
7320 OMPLoopDirective::HelperExprs B;
7321 // In presence of clause 'collapse' with number of loops, it will
7322 // define the nested loops number.
7323 unsigned NestedLoopCount =
7324 CheckOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
7325 nullptr /*ordered not a clause on distribute*/, AStmt,
7326 *this, *DSAStack, VarsWithImplicitDSA, B);
7327 if (NestedLoopCount == 0)
7328 return StmtError();
7329
7330 assert((CurContext->isDependentContext() || B.builtAll()) &&
7331 "omp for loop exprs were not built");
7332
Kelvin Lic5609492016-07-15 04:39:07 +00007333 if (checkSimdlenSafelenSpecified(*this, Clauses))
7334 return StmtError();
7335
Kelvin Li787f3fc2016-07-06 04:45:38 +00007336 getCurFunction()->setHasBranchProtectedScope();
7337 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
7338 NestedLoopCount, Clauses, AStmt, B);
7339}
7340
Kelvin Lia579b912016-07-14 02:54:56 +00007341StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
7342 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7343 SourceLocation EndLoc,
7344 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7345 if (!AStmt)
7346 return StmtError();
7347
7348 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7349 // 1.2.2 OpenMP Language Terminology
7350 // Structured block - An executable statement with a single entry at the
7351 // top and a single exit at the bottom.
7352 // The point of exit cannot be a branch out of the structured block.
7353 // longjmp() and throw() must not violate the entry/exit criteria.
7354 CS->getCapturedDecl()->setNothrow();
7355
7356 OMPLoopDirective::HelperExprs B;
7357 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7358 // define the nested loops number.
7359 unsigned NestedLoopCount = CheckOpenMPLoop(
7360 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
7361 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
7362 VarsWithImplicitDSA, B);
7363 if (NestedLoopCount == 0)
7364 return StmtError();
7365
7366 assert((CurContext->isDependentContext() || B.builtAll()) &&
7367 "omp target parallel for simd loop exprs were not built");
7368
7369 if (!CurContext->isDependentContext()) {
7370 // Finalize the clauses that need pre-built expressions for CodeGen.
7371 for (auto C : Clauses) {
7372 if (auto LC = dyn_cast<OMPLinearClause>(C))
7373 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7374 B.NumIterations, *this, CurScope,
7375 DSAStack))
7376 return StmtError();
7377 }
7378 }
Kelvin Lic5609492016-07-15 04:39:07 +00007379 if (checkSimdlenSafelenSpecified(*this, Clauses))
Kelvin Lia579b912016-07-14 02:54:56 +00007380 return StmtError();
7381
7382 getCurFunction()->setHasBranchProtectedScope();
7383 return OMPTargetParallelForSimdDirective::Create(
7384 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7385}
7386
Kelvin Li986330c2016-07-20 22:57:10 +00007387StmtResult Sema::ActOnOpenMPTargetSimdDirective(
7388 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7389 SourceLocation EndLoc,
7390 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7391 if (!AStmt)
7392 return StmtError();
7393
7394 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7395 // 1.2.2 OpenMP Language Terminology
7396 // Structured block - An executable statement with a single entry at the
7397 // top and a single exit at the bottom.
7398 // The point of exit cannot be a branch out of the structured block.
7399 // longjmp() and throw() must not violate the entry/exit criteria.
7400 CS->getCapturedDecl()->setNothrow();
7401
7402 OMPLoopDirective::HelperExprs B;
7403 // In presence of clause 'collapse' with number of loops, it will define the
7404 // nested loops number.
7405 unsigned NestedLoopCount =
7406 CheckOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
7407 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
7408 VarsWithImplicitDSA, B);
7409 if (NestedLoopCount == 0)
7410 return StmtError();
7411
7412 assert((CurContext->isDependentContext() || B.builtAll()) &&
7413 "omp target simd loop exprs were not built");
7414
7415 if (!CurContext->isDependentContext()) {
7416 // Finalize the clauses that need pre-built expressions for CodeGen.
7417 for (auto C : Clauses) {
7418 if (auto LC = dyn_cast<OMPLinearClause>(C))
7419 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7420 B.NumIterations, *this, CurScope,
7421 DSAStack))
7422 return StmtError();
7423 }
7424 }
7425
7426 if (checkSimdlenSafelenSpecified(*this, Clauses))
7427 return StmtError();
7428
7429 getCurFunction()->setHasBranchProtectedScope();
7430 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
7431 NestedLoopCount, Clauses, AStmt, B);
7432}
7433
Kelvin Li02532872016-08-05 14:37:37 +00007434StmtResult Sema::ActOnOpenMPTeamsDistributeDirective(
7435 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7436 SourceLocation EndLoc,
7437 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7438 if (!AStmt)
7439 return StmtError();
7440
7441 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7442 // 1.2.2 OpenMP Language Terminology
7443 // Structured block - An executable statement with a single entry at the
7444 // top and a single exit at the bottom.
7445 // The point of exit cannot be a branch out of the structured block.
7446 // longjmp() and throw() must not violate the entry/exit criteria.
7447 CS->getCapturedDecl()->setNothrow();
7448
7449 OMPLoopDirective::HelperExprs B;
7450 // In presence of clause 'collapse' with number of loops, it will
7451 // define the nested loops number.
7452 unsigned NestedLoopCount =
7453 CheckOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses),
7454 nullptr /*ordered not a clause on distribute*/, AStmt,
7455 *this, *DSAStack, VarsWithImplicitDSA, B);
7456 if (NestedLoopCount == 0)
7457 return StmtError();
7458
7459 assert((CurContext->isDependentContext() || B.builtAll()) &&
7460 "omp teams distribute loop exprs were not built");
7461
7462 getCurFunction()->setHasBranchProtectedScope();
7463 return OMPTeamsDistributeDirective::Create(Context, StartLoc, EndLoc,
7464 NestedLoopCount, Clauses, AStmt,
7465 B);
7466}
7467
Alexey Bataeved09d242014-05-28 05:53:51 +00007468OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007469 SourceLocation StartLoc,
7470 SourceLocation LParenLoc,
7471 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007472 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007473 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00007474 case OMPC_final:
7475 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
7476 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00007477 case OMPC_num_threads:
7478 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
7479 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007480 case OMPC_safelen:
7481 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
7482 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00007483 case OMPC_simdlen:
7484 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
7485 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00007486 case OMPC_collapse:
7487 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
7488 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00007489 case OMPC_ordered:
7490 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
7491 break;
Michael Wonge710d542015-08-07 16:16:36 +00007492 case OMPC_device:
7493 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
7494 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00007495 case OMPC_num_teams:
7496 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
7497 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007498 case OMPC_thread_limit:
7499 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
7500 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00007501 case OMPC_priority:
7502 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
7503 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007504 case OMPC_grainsize:
7505 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
7506 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00007507 case OMPC_num_tasks:
7508 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
7509 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00007510 case OMPC_hint:
7511 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
7512 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007513 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007514 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007515 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007516 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007517 case OMPC_private:
7518 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00007519 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007520 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007521 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00007522 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007523 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007524 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007525 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00007526 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007527 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007528 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007529 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007530 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007531 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007532 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007533 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007534 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007535 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007536 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00007537 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007538 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007539 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00007540 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007541 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007542 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007543 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007544 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007545 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007546 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007547 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007548 case OMPC_is_device_ptr:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007549 llvm_unreachable("Clause is not allowed.");
7550 }
7551 return Res;
7552}
7553
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007554OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
7555 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007556 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007557 SourceLocation NameModifierLoc,
7558 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007559 SourceLocation EndLoc) {
7560 Expr *ValExpr = Condition;
7561 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
7562 !Condition->isInstantiationDependent() &&
7563 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00007564 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007565 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007566 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007567
Richard Smith03a4aa32016-06-23 19:02:52 +00007568 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007569 }
7570
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007571 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
7572 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007573}
7574
Alexey Bataev3778b602014-07-17 07:32:53 +00007575OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
7576 SourceLocation StartLoc,
7577 SourceLocation LParenLoc,
7578 SourceLocation EndLoc) {
7579 Expr *ValExpr = Condition;
7580 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
7581 !Condition->isInstantiationDependent() &&
7582 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00007583 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataev3778b602014-07-17 07:32:53 +00007584 if (Val.isInvalid())
7585 return nullptr;
7586
Richard Smith03a4aa32016-06-23 19:02:52 +00007587 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataev3778b602014-07-17 07:32:53 +00007588 }
7589
7590 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
7591}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00007592ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
7593 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00007594 if (!Op)
7595 return ExprError();
7596
7597 class IntConvertDiagnoser : public ICEConvertDiagnoser {
7598 public:
7599 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00007600 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00007601 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
7602 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007603 return S.Diag(Loc, diag::err_omp_not_integral) << T;
7604 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007605 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
7606 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007607 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
7608 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007609 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
7610 QualType T,
7611 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007612 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
7613 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007614 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
7615 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007616 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00007617 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00007618 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007619 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
7620 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007621 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
7622 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007623 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
7624 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007625 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00007626 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00007627 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007628 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
7629 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007630 llvm_unreachable("conversion functions are permitted");
7631 }
7632 } ConvertDiagnoser;
7633 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
7634}
7635
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007636static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00007637 OpenMPClauseKind CKind,
7638 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007639 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
7640 !ValExpr->isInstantiationDependent()) {
7641 SourceLocation Loc = ValExpr->getExprLoc();
7642 ExprResult Value =
7643 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
7644 if (Value.isInvalid())
7645 return false;
7646
7647 ValExpr = Value.get();
7648 // The expression must evaluate to a non-negative integer value.
7649 llvm::APSInt Result;
7650 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00007651 Result.isSigned() &&
7652 !((!StrictlyPositive && Result.isNonNegative()) ||
7653 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007654 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00007655 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
7656 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007657 return false;
7658 }
7659 }
7660 return true;
7661}
7662
Alexey Bataev568a8332014-03-06 06:15:19 +00007663OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
7664 SourceLocation StartLoc,
7665 SourceLocation LParenLoc,
7666 SourceLocation EndLoc) {
7667 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00007668
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007669 // OpenMP [2.5, Restrictions]
7670 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00007671 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
7672 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007673 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00007674
Alexey Bataeved09d242014-05-28 05:53:51 +00007675 return new (Context)
7676 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00007677}
7678
Alexey Bataev62c87d22014-03-21 04:51:18 +00007679ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007680 OpenMPClauseKind CKind,
7681 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00007682 if (!E)
7683 return ExprError();
7684 if (E->isValueDependent() || E->isTypeDependent() ||
7685 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00007686 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007687 llvm::APSInt Result;
7688 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
7689 if (ICE.isInvalid())
7690 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007691 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
7692 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00007693 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007694 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
7695 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00007696 return ExprError();
7697 }
Alexander Musman09184fe2014-09-30 05:29:28 +00007698 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
7699 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
7700 << E->getSourceRange();
7701 return ExprError();
7702 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007703 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
7704 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00007705 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007706 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00007707 return ICE;
7708}
7709
7710OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
7711 SourceLocation LParenLoc,
7712 SourceLocation EndLoc) {
7713 // OpenMP [2.8.1, simd construct, Description]
7714 // The parameter of the safelen clause must be a constant
7715 // positive integer expression.
7716 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
7717 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007718 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007719 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00007720 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00007721}
7722
Alexey Bataev66b15b52015-08-21 11:14:16 +00007723OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
7724 SourceLocation LParenLoc,
7725 SourceLocation EndLoc) {
7726 // OpenMP [2.8.1, simd construct, Description]
7727 // The parameter of the simdlen clause must be a constant
7728 // positive integer expression.
7729 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
7730 if (Simdlen.isInvalid())
7731 return nullptr;
7732 return new (Context)
7733 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
7734}
7735
Alexander Musman64d33f12014-06-04 07:53:32 +00007736OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
7737 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00007738 SourceLocation LParenLoc,
7739 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00007740 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00007741 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00007742 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00007743 // The parameter of the collapse clause must be a constant
7744 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00007745 ExprResult NumForLoopsResult =
7746 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
7747 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00007748 return nullptr;
7749 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00007750 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00007751}
7752
Alexey Bataev10e775f2015-07-30 11:36:16 +00007753OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
7754 SourceLocation EndLoc,
7755 SourceLocation LParenLoc,
7756 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00007757 // OpenMP [2.7.1, loop construct, Description]
7758 // OpenMP [2.8.1, simd construct, Description]
7759 // OpenMP [2.9.6, distribute construct, Description]
7760 // The parameter of the ordered clause must be a constant
7761 // positive integer expression if any.
7762 if (NumForLoops && LParenLoc.isValid()) {
7763 ExprResult NumForLoopsResult =
7764 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
7765 if (NumForLoopsResult.isInvalid())
7766 return nullptr;
7767 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00007768 } else
7769 NumForLoops = nullptr;
7770 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00007771 return new (Context)
7772 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
7773}
7774
Alexey Bataeved09d242014-05-28 05:53:51 +00007775OMPClause *Sema::ActOnOpenMPSimpleClause(
7776 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
7777 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007778 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007779 switch (Kind) {
7780 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00007781 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00007782 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
7783 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007784 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007785 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00007786 Res = ActOnOpenMPProcBindClause(
7787 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
7788 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007789 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007790 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007791 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00007792 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00007793 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007794 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00007795 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007796 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007797 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007798 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00007799 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00007800 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007801 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00007802 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007803 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007804 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007805 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007806 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007807 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007808 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007809 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007810 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007811 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007812 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007813 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007814 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007815 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007816 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007817 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007818 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007819 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007820 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007821 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007822 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007823 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007824 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007825 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007826 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007827 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007828 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007829 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007830 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007831 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007832 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007833 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007834 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007835 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007836 case OMPC_is_device_ptr:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007837 llvm_unreachable("Clause is not allowed.");
7838 }
7839 return Res;
7840}
7841
Alexey Bataev6402bca2015-12-28 07:25:51 +00007842static std::string
7843getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
7844 ArrayRef<unsigned> Exclude = llvm::None) {
7845 std::string Values;
7846 unsigned Bound = Last >= 2 ? Last - 2 : 0;
7847 unsigned Skipped = Exclude.size();
7848 auto S = Exclude.begin(), E = Exclude.end();
7849 for (unsigned i = First; i < Last; ++i) {
7850 if (std::find(S, E, i) != E) {
7851 --Skipped;
7852 continue;
7853 }
7854 Values += "'";
7855 Values += getOpenMPSimpleClauseTypeName(K, i);
7856 Values += "'";
7857 if (i == Bound - Skipped)
7858 Values += " or ";
7859 else if (i != Bound + 1 - Skipped)
7860 Values += ", ";
7861 }
7862 return Values;
7863}
7864
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007865OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
7866 SourceLocation KindKwLoc,
7867 SourceLocation StartLoc,
7868 SourceLocation LParenLoc,
7869 SourceLocation EndLoc) {
7870 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00007871 static_assert(OMPC_DEFAULT_unknown > 0,
7872 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007873 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00007874 << getListOfPossibleValues(OMPC_default, /*First=*/0,
7875 /*Last=*/OMPC_DEFAULT_unknown)
7876 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007877 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007878 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00007879 switch (Kind) {
7880 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007881 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007882 break;
7883 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007884 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007885 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007886 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007887 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00007888 break;
7889 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007890 return new (Context)
7891 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007892}
7893
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007894OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
7895 SourceLocation KindKwLoc,
7896 SourceLocation StartLoc,
7897 SourceLocation LParenLoc,
7898 SourceLocation EndLoc) {
7899 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007900 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00007901 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
7902 /*Last=*/OMPC_PROC_BIND_unknown)
7903 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007904 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007905 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007906 return new (Context)
7907 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007908}
7909
Alexey Bataev56dafe82014-06-20 07:16:17 +00007910OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007911 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007912 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007913 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007914 SourceLocation EndLoc) {
7915 OMPClause *Res = nullptr;
7916 switch (Kind) {
7917 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00007918 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
7919 assert(Argument.size() == NumberOfElements &&
7920 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007921 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007922 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
7923 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
7924 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
7925 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
7926 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007927 break;
7928 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00007929 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
7930 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
7931 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
7932 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007933 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00007934 case OMPC_dist_schedule:
7935 Res = ActOnOpenMPDistScheduleClause(
7936 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
7937 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
7938 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007939 case OMPC_defaultmap:
7940 enum { Modifier, DefaultmapKind };
7941 Res = ActOnOpenMPDefaultmapClause(
7942 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
7943 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
7944 StartLoc, LParenLoc, ArgumentLoc[Modifier],
7945 ArgumentLoc[DefaultmapKind], EndLoc);
7946 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00007947 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007948 case OMPC_num_threads:
7949 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007950 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007951 case OMPC_collapse:
7952 case OMPC_default:
7953 case OMPC_proc_bind:
7954 case OMPC_private:
7955 case OMPC_firstprivate:
7956 case OMPC_lastprivate:
7957 case OMPC_shared:
7958 case OMPC_reduction:
7959 case OMPC_linear:
7960 case OMPC_aligned:
7961 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007962 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007963 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007964 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007965 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007966 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007967 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007968 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007969 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007970 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007971 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007972 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007973 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007974 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007975 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007976 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007977 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007978 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007979 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007980 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007981 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007982 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007983 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007984 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007985 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007986 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007987 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007988 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007989 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007990 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007991 case OMPC_is_device_ptr:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007992 llvm_unreachable("Clause is not allowed.");
7993 }
7994 return Res;
7995}
7996
Alexey Bataev6402bca2015-12-28 07:25:51 +00007997static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
7998 OpenMPScheduleClauseModifier M2,
7999 SourceLocation M1Loc, SourceLocation M2Loc) {
8000 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
8001 SmallVector<unsigned, 2> Excluded;
8002 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
8003 Excluded.push_back(M2);
8004 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
8005 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
8006 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
8007 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
8008 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
8009 << getListOfPossibleValues(OMPC_schedule,
8010 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
8011 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
8012 Excluded)
8013 << getOpenMPClauseName(OMPC_schedule);
8014 return true;
8015 }
8016 return false;
8017}
8018
Alexey Bataev56dafe82014-06-20 07:16:17 +00008019OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00008020 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00008021 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00008022 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
8023 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
8024 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
8025 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
8026 return nullptr;
8027 // OpenMP, 2.7.1, Loop Construct, Restrictions
8028 // Either the monotonic modifier or the nonmonotonic modifier can be specified
8029 // but not both.
8030 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
8031 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
8032 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
8033 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
8034 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
8035 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
8036 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
8037 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
8038 return nullptr;
8039 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00008040 if (Kind == OMPC_SCHEDULE_unknown) {
8041 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00008042 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
8043 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
8044 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
8045 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
8046 Exclude);
8047 } else {
8048 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
8049 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00008050 }
8051 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
8052 << Values << getOpenMPClauseName(OMPC_schedule);
8053 return nullptr;
8054 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00008055 // OpenMP, 2.7.1, Loop Construct, Restrictions
8056 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
8057 // schedule(guided).
8058 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
8059 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
8060 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
8061 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
8062 diag::err_omp_schedule_nonmonotonic_static);
8063 return nullptr;
8064 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00008065 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00008066 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00008067 if (ChunkSize) {
8068 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
8069 !ChunkSize->isInstantiationDependent() &&
8070 !ChunkSize->containsUnexpandedParameterPack()) {
8071 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
8072 ExprResult Val =
8073 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
8074 if (Val.isInvalid())
8075 return nullptr;
8076
8077 ValExpr = Val.get();
8078
8079 // OpenMP [2.7.1, Restrictions]
8080 // chunk_size must be a loop invariant integer expression with a positive
8081 // value.
8082 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00008083 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
8084 if (Result.isSigned() && !Result.isStrictlyPositive()) {
8085 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00008086 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00008087 return nullptr;
8088 }
Alexey Bataevb46cdea2016-06-15 11:20:48 +00008089 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
8090 !CurContext->isDependentContext()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00008091 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
8092 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8093 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00008094 }
8095 }
8096 }
8097
Alexey Bataev6402bca2015-12-28 07:25:51 +00008098 return new (Context)
8099 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00008100 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00008101}
8102
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008103OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
8104 SourceLocation StartLoc,
8105 SourceLocation EndLoc) {
8106 OMPClause *Res = nullptr;
8107 switch (Kind) {
8108 case OMPC_ordered:
8109 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
8110 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00008111 case OMPC_nowait:
8112 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
8113 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008114 case OMPC_untied:
8115 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
8116 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008117 case OMPC_mergeable:
8118 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
8119 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008120 case OMPC_read:
8121 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
8122 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00008123 case OMPC_write:
8124 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
8125 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00008126 case OMPC_update:
8127 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
8128 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00008129 case OMPC_capture:
8130 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
8131 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008132 case OMPC_seq_cst:
8133 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
8134 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00008135 case OMPC_threads:
8136 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
8137 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008138 case OMPC_simd:
8139 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
8140 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00008141 case OMPC_nogroup:
8142 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
8143 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008144 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00008145 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008146 case OMPC_num_threads:
8147 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008148 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008149 case OMPC_collapse:
8150 case OMPC_schedule:
8151 case OMPC_private:
8152 case OMPC_firstprivate:
8153 case OMPC_lastprivate:
8154 case OMPC_shared:
8155 case OMPC_reduction:
8156 case OMPC_linear:
8157 case OMPC_aligned:
8158 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008159 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008160 case OMPC_default:
8161 case OMPC_proc_bind:
8162 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00008163 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008164 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00008165 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008166 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00008167 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008168 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00008169 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008170 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00008171 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00008172 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008173 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008174 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008175 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008176 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00008177 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00008178 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00008179 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00008180 case OMPC_is_device_ptr:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008181 llvm_unreachable("Clause is not allowed.");
8182 }
8183 return Res;
8184}
8185
Alexey Bataev236070f2014-06-20 11:19:47 +00008186OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
8187 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00008188 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00008189 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
8190}
8191
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008192OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
8193 SourceLocation EndLoc) {
8194 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
8195}
8196
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008197OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
8198 SourceLocation EndLoc) {
8199 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
8200}
8201
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008202OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
8203 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008204 return new (Context) OMPReadClause(StartLoc, EndLoc);
8205}
8206
Alexey Bataevdea47612014-07-23 07:46:59 +00008207OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
8208 SourceLocation EndLoc) {
8209 return new (Context) OMPWriteClause(StartLoc, EndLoc);
8210}
8211
Alexey Bataev67a4f222014-07-23 10:25:33 +00008212OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
8213 SourceLocation EndLoc) {
8214 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
8215}
8216
Alexey Bataev459dec02014-07-24 06:46:57 +00008217OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
8218 SourceLocation EndLoc) {
8219 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
8220}
8221
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008222OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
8223 SourceLocation EndLoc) {
8224 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
8225}
8226
Alexey Bataev346265e2015-09-25 10:37:12 +00008227OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
8228 SourceLocation EndLoc) {
8229 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
8230}
8231
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008232OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
8233 SourceLocation EndLoc) {
8234 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
8235}
8236
Alexey Bataevb825de12015-12-07 10:51:44 +00008237OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
8238 SourceLocation EndLoc) {
8239 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
8240}
8241
Alexey Bataevc5e02582014-06-16 07:08:35 +00008242OMPClause *Sema::ActOnOpenMPVarListClause(
8243 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
8244 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
8245 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008246 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00008247 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
8248 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
8249 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008250 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008251 switch (Kind) {
8252 case OMPC_private:
8253 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8254 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008255 case OMPC_firstprivate:
8256 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8257 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008258 case OMPC_lastprivate:
8259 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8260 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008261 case OMPC_shared:
8262 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
8263 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008264 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00008265 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
8266 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008267 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00008268 case OMPC_linear:
8269 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00008270 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00008271 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008272 case OMPC_aligned:
8273 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
8274 ColonLoc, EndLoc);
8275 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008276 case OMPC_copyin:
8277 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
8278 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008279 case OMPC_copyprivate:
8280 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8281 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00008282 case OMPC_flush:
8283 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
8284 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008285 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008286 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
8287 StartLoc, LParenLoc, EndLoc);
8288 break;
8289 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00008290 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
8291 DepLinMapLoc, ColonLoc, VarList, StartLoc,
8292 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008293 break;
Samuel Antao661c0902016-05-26 17:39:58 +00008294 case OMPC_to:
8295 Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc);
8296 break;
Samuel Antaoec172c62016-05-26 17:49:04 +00008297 case OMPC_from:
8298 Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc);
8299 break;
Carlo Bertolli2404b172016-07-13 15:37:16 +00008300 case OMPC_use_device_ptr:
8301 Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
8302 break;
Carlo Bertolli70594e92016-07-13 17:16:49 +00008303 case OMPC_is_device_ptr:
8304 Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
8305 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008306 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00008307 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00008308 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00008309 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008310 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00008311 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008312 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008313 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008314 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008315 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00008316 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008317 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008318 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008319 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008320 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008321 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008322 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008323 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008324 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00008325 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00008326 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008327 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00008328 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008329 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00008330 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008331 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00008332 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00008333 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00008334 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008335 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008336 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008337 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008338 case OMPC_uniform:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008339 llvm_unreachable("Clause is not allowed.");
8340 }
8341 return Res;
8342}
8343
Alexey Bataev90c228f2016-02-08 09:29:13 +00008344ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00008345 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00008346 ExprResult Res = BuildDeclRefExpr(
8347 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
8348 if (!Res.isUsable())
8349 return ExprError();
8350 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
8351 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
8352 if (!Res.isUsable())
8353 return ExprError();
8354 }
8355 if (VK != VK_LValue && Res.get()->isGLValue()) {
8356 Res = DefaultLvalueConversion(Res.get());
8357 if (!Res.isUsable())
8358 return ExprError();
8359 }
8360 return Res;
8361}
8362
Alexey Bataev60da77e2016-02-29 05:54:20 +00008363static std::pair<ValueDecl *, bool>
8364getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
8365 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008366 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
8367 RefExpr->containsUnexpandedParameterPack())
8368 return std::make_pair(nullptr, true);
8369
Alexey Bataevd985eda2016-02-10 11:29:16 +00008370 // OpenMP [3.1, C/C++]
8371 // A list item is a variable name.
8372 // OpenMP [2.9.3.3, Restrictions, p.1]
8373 // A variable that is part of another variable (as an array or
8374 // structure element) cannot appear in a private clause.
8375 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008376 enum {
8377 NoArrayExpr = -1,
8378 ArraySubscript = 0,
8379 OMPArraySection = 1
8380 } IsArrayExpr = NoArrayExpr;
8381 if (AllowArraySection) {
8382 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
8383 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
8384 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
8385 Base = TempASE->getBase()->IgnoreParenImpCasts();
8386 RefExpr = Base;
8387 IsArrayExpr = ArraySubscript;
8388 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
8389 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
8390 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
8391 Base = TempOASE->getBase()->IgnoreParenImpCasts();
8392 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
8393 Base = TempASE->getBase()->IgnoreParenImpCasts();
8394 RefExpr = Base;
8395 IsArrayExpr = OMPArraySection;
8396 }
8397 }
8398 ELoc = RefExpr->getExprLoc();
8399 ERange = RefExpr->getSourceRange();
8400 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008401 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
8402 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
8403 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
8404 (S.getCurrentThisType().isNull() || !ME ||
8405 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
8406 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008407 if (IsArrayExpr != NoArrayExpr)
8408 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
8409 << ERange;
8410 else {
8411 S.Diag(ELoc,
8412 AllowArraySection
8413 ? diag::err_omp_expected_var_name_member_expr_or_array_item
8414 : diag::err_omp_expected_var_name_member_expr)
8415 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
8416 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008417 return std::make_pair(nullptr, false);
8418 }
8419 return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
8420}
8421
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008422OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
8423 SourceLocation StartLoc,
8424 SourceLocation LParenLoc,
8425 SourceLocation EndLoc) {
8426 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00008427 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00008428 for (auto &RefExpr : VarList) {
8429 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008430 SourceLocation ELoc;
8431 SourceRange ERange;
8432 Expr *SimpleRefExpr = RefExpr;
8433 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008434 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008435 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008436 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008437 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008438 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008439 ValueDecl *D = Res.first;
8440 if (!D)
8441 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008442
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008443 QualType Type = D->getType();
8444 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008445
8446 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8447 // A variable that appears in a private clause must not have an incomplete
8448 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008449 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008450 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008451 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008452
Alexey Bataev758e55e2013-09-06 18:03:48 +00008453 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8454 // in a Construct]
8455 // Variables with the predetermined data-sharing attributes may not be
8456 // listed in data-sharing attributes clauses, except for the cases
8457 // listed below. For these exceptions only, listing a predetermined
8458 // variable in a data-sharing attribute clause is allowed and overrides
8459 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008460 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008461 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00008462 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8463 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008464 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008465 continue;
8466 }
8467
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008468 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008469 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00008470 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008471 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
8472 << getOpenMPClauseName(OMPC_private) << Type
8473 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8474 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008475 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008476 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008477 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008478 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008479 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008480 continue;
8481 }
8482
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008483 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
8484 // A list item cannot appear in both a map clause and a data-sharing
8485 // attribute clause on the same construct
8486 if (DSAStack->getCurrentDirective() == OMPD_target) {
Samuel Antao6890b092016-07-28 14:25:09 +00008487 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +00008488 if (DSAStack->checkMappableExprComponentListsForDecl(
8489 VD, /* CurrentRegionOnly = */ true,
Samuel Antao6890b092016-07-28 14:25:09 +00008490 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
8491 OpenMPClauseKind WhereFoundClauseKind) -> bool {
8492 ConflictKind = WhereFoundClauseKind;
8493 return true;
8494 })) {
8495 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008496 << getOpenMPClauseName(OMPC_private)
Samuel Antao6890b092016-07-28 14:25:09 +00008497 << getOpenMPClauseName(ConflictKind)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008498 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8499 ReportOriginalDSA(*this, DSAStack, D, DVar);
8500 continue;
8501 }
8502 }
8503
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008504 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
8505 // A variable of class type (or array thereof) that appears in a private
8506 // clause requires an accessible, unambiguous default constructor for the
8507 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00008508 // Generate helper private variable and initialize it with the default
8509 // value. The address of the original variable is replaced by the address of
8510 // the new private variable in CodeGen. This new variable is not added to
8511 // IdResolver, so the code in the OpenMP region uses original variable for
8512 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008513 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008514 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
8515 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008516 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008517 if (VDPrivate->isInvalidDecl())
8518 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008519 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008520 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008521
Alexey Bataev90c228f2016-02-08 09:29:13 +00008522 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008523 if (!VD && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00008524 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00008525 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008526 Vars.push_back((VD || CurContext->isDependentContext())
8527 ? RefExpr->IgnoreParens()
8528 : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008529 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008530 }
8531
Alexey Bataeved09d242014-05-28 05:53:51 +00008532 if (Vars.empty())
8533 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008534
Alexey Bataev03b340a2014-10-21 03:16:40 +00008535 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8536 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008537}
8538
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008539namespace {
8540class DiagsUninitializedSeveretyRAII {
8541private:
8542 DiagnosticsEngine &Diags;
8543 SourceLocation SavedLoc;
8544 bool IsIgnored;
8545
8546public:
8547 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
8548 bool IsIgnored)
8549 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
8550 if (!IsIgnored) {
8551 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
8552 /*Map*/ diag::Severity::Ignored, Loc);
8553 }
8554 }
8555 ~DiagsUninitializedSeveretyRAII() {
8556 if (!IsIgnored)
8557 Diags.popMappings(SavedLoc);
8558 }
8559};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00008560}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008561
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008562OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
8563 SourceLocation StartLoc,
8564 SourceLocation LParenLoc,
8565 SourceLocation EndLoc) {
8566 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008567 SmallVector<Expr *, 8> PrivateCopies;
8568 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00008569 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008570 bool IsImplicitClause =
8571 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
8572 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
8573
Alexey Bataeved09d242014-05-28 05:53:51 +00008574 for (auto &RefExpr : VarList) {
8575 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008576 SourceLocation ELoc;
8577 SourceRange ERange;
8578 Expr *SimpleRefExpr = RefExpr;
8579 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008580 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008581 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008582 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008583 PrivateCopies.push_back(nullptr);
8584 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008585 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008586 ValueDecl *D = Res.first;
8587 if (!D)
8588 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008589
Alexey Bataev60da77e2016-02-29 05:54:20 +00008590 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00008591 QualType Type = D->getType();
8592 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008593
8594 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8595 // A variable that appears in a private clause must not have an incomplete
8596 // type or a reference type.
8597 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00008598 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008599 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008600 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008601
8602 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
8603 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00008604 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008605 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008606 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008607
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008608 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00008609 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008610 if (!IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008611 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev005248a2016-02-25 05:25:57 +00008612 TopDVar = DVar;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008613 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008614 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
8615 // A list item that specifies a given variable may not appear in more
8616 // than one clause on the same directive, except that a variable may be
8617 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008618 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00008619 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008620 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00008621 << getOpenMPClauseName(DVar.CKind)
8622 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008623 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008624 continue;
8625 }
8626
8627 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8628 // in a Construct]
8629 // Variables with the predetermined data-sharing attributes may not be
8630 // listed in data-sharing attributes clauses, except for the cases
8631 // listed below. For these exceptions only, listing a predetermined
8632 // variable in a data-sharing attribute clause is allowed and overrides
8633 // the variable's predetermined data-sharing attributes.
8634 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8635 // in a Construct, C/C++, p.2]
8636 // Variables with const-qualified type having no mutable member may be
8637 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00008638 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008639 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
8640 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00008641 << getOpenMPClauseName(DVar.CKind)
8642 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008643 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008644 continue;
8645 }
8646
Alexey Bataevf29276e2014-06-18 04:14:57 +00008647 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008648 // OpenMP [2.9.3.4, Restrictions, p.2]
8649 // A list item that is private within a parallel region must not appear
8650 // in a firstprivate clause on a worksharing construct if any of the
8651 // worksharing regions arising from the worksharing construct ever bind
8652 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00008653 if (isOpenMPWorksharingDirective(CurrDir) &&
8654 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008655 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008656 if (DVar.CKind != OMPC_shared &&
8657 (isOpenMPParallelDirective(DVar.DKind) ||
8658 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00008659 Diag(ELoc, diag::err_omp_required_access)
8660 << getOpenMPClauseName(OMPC_firstprivate)
8661 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008662 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008663 continue;
8664 }
8665 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008666 // OpenMP [2.9.3.4, Restrictions, p.3]
8667 // A list item that appears in a reduction clause of a parallel construct
8668 // must not appear in a firstprivate clause on a worksharing or task
8669 // construct if any of the worksharing or task regions arising from the
8670 // worksharing or task construct ever bind to any of the parallel regions
8671 // arising from the parallel construct.
8672 // OpenMP [2.9.3.4, Restrictions, p.4]
8673 // A list item that appears in a reduction clause in worksharing
8674 // construct must not appear in a firstprivate clause in a task construct
8675 // encountered during execution of any of the worksharing regions arising
8676 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +00008677 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008678 DVar = DSAStack->hasInnermostDSA(
8679 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
8680 [](OpenMPDirectiveKind K) -> bool {
8681 return isOpenMPParallelDirective(K) ||
8682 isOpenMPWorksharingDirective(K);
8683 },
8684 false);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008685 if (DVar.CKind == OMPC_reduction &&
8686 (isOpenMPParallelDirective(DVar.DKind) ||
8687 isOpenMPWorksharingDirective(DVar.DKind))) {
8688 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
8689 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008690 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008691 continue;
8692 }
8693 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008694
8695 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
8696 // A list item that is private within a teams region must not appear in a
8697 // firstprivate clause on a distribute construct if any of the distribute
8698 // regions arising from the distribute construct ever bind to any of the
8699 // teams regions arising from the teams construct.
8700 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
8701 // A list item that appears in a reduction clause of a teams construct
8702 // must not appear in a firstprivate clause on a distribute construct if
8703 // any of the distribute regions arising from the distribute construct
8704 // ever bind to any of the teams regions arising from the teams construct.
8705 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8706 // A list item may appear in a firstprivate or lastprivate clause but not
8707 // both.
8708 if (CurrDir == OMPD_distribute) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008709 DVar = DSAStack->hasInnermostDSA(
8710 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_private; },
8711 [](OpenMPDirectiveKind K) -> bool {
8712 return isOpenMPTeamsDirective(K);
8713 },
8714 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008715 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
8716 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008717 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008718 continue;
8719 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008720 DVar = DSAStack->hasInnermostDSA(
8721 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
8722 [](OpenMPDirectiveKind K) -> bool {
8723 return isOpenMPTeamsDirective(K);
8724 },
8725 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008726 if (DVar.CKind == OMPC_reduction &&
8727 isOpenMPTeamsDirective(DVar.DKind)) {
8728 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008729 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008730 continue;
8731 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008732 DVar = DSAStack->getTopDSA(D, false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008733 if (DVar.CKind == OMPC_lastprivate) {
8734 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008735 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008736 continue;
8737 }
8738 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008739 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
8740 // A list item cannot appear in both a map clause and a data-sharing
8741 // attribute clause on the same construct
8742 if (CurrDir == OMPD_target) {
Samuel Antao6890b092016-07-28 14:25:09 +00008743 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +00008744 if (DSAStack->checkMappableExprComponentListsForDecl(
8745 VD, /* CurrentRegionOnly = */ true,
Samuel Antao6890b092016-07-28 14:25:09 +00008746 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
8747 OpenMPClauseKind WhereFoundClauseKind) -> bool {
8748 ConflictKind = WhereFoundClauseKind;
8749 return true;
8750 })) {
8751 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008752 << getOpenMPClauseName(OMPC_firstprivate)
Samuel Antao6890b092016-07-28 14:25:09 +00008753 << getOpenMPClauseName(ConflictKind)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008754 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8755 ReportOriginalDSA(*this, DSAStack, D, DVar);
8756 continue;
8757 }
8758 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008759 }
8760
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008761 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008762 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00008763 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008764 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
8765 << getOpenMPClauseName(OMPC_firstprivate) << Type
8766 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8767 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00008768 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008769 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00008770 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008771 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00008772 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008773 continue;
8774 }
8775
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008776 Type = Type.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008777 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
8778 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008779 // Generate helper private variable and initialize it with the value of the
8780 // original variable. The address of the original variable is replaced by
8781 // the address of the new private variable in the CodeGen. This new variable
8782 // is not added to IdResolver, so the code in the OpenMP region uses
8783 // original variable for proper diagnostics and variable capturing.
8784 Expr *VDInitRefExpr = nullptr;
8785 // For arrays generate initializer for single element and replace it by the
8786 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008787 if (Type->isArrayType()) {
8788 auto VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00008789 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008790 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008791 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008792 ElemType = ElemType.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008793 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008794 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00008795 InitializedEntity Entity =
8796 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008797 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
8798
8799 InitializationSequence InitSeq(*this, Entity, Kind, Init);
8800 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
8801 if (Result.isInvalid())
8802 VDPrivate->setInvalidDecl();
8803 else
8804 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008805 // Remove temp variable declaration.
8806 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008807 } else {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008808 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
8809 ".firstprivate.temp");
8810 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
8811 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00008812 AddInitializerToDecl(VDPrivate,
8813 DefaultLvalueConversion(VDInitRefExpr).get(),
8814 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008815 }
8816 if (VDPrivate->isInvalidDecl()) {
8817 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008818 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008819 diag::note_omp_task_predetermined_firstprivate_here);
8820 }
8821 continue;
8822 }
8823 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008824 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00008825 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
8826 RefExpr->getExprLoc());
8827 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008828 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008829 if (TopDVar.CKind == OMPC_lastprivate)
8830 Ref = TopDVar.PrivateCopy;
8831 else {
Alexey Bataev61205072016-03-02 04:57:40 +00008832 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataev005248a2016-02-25 05:25:57 +00008833 if (!IsOpenMPCapturedDecl(D))
8834 ExprCaptures.push_back(Ref->getDecl());
8835 }
Alexey Bataev417089f2016-02-17 13:19:37 +00008836 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008837 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008838 Vars.push_back((VD || CurContext->isDependentContext())
8839 ? RefExpr->IgnoreParens()
8840 : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008841 PrivateCopies.push_back(VDPrivateRefExpr);
8842 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008843 }
8844
Alexey Bataeved09d242014-05-28 05:53:51 +00008845 if (Vars.empty())
8846 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008847
8848 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008849 Vars, PrivateCopies, Inits,
8850 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008851}
8852
Alexander Musman1bb328c2014-06-04 13:06:39 +00008853OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
8854 SourceLocation StartLoc,
8855 SourceLocation LParenLoc,
8856 SourceLocation EndLoc) {
8857 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00008858 SmallVector<Expr *, 8> SrcExprs;
8859 SmallVector<Expr *, 8> DstExprs;
8860 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +00008861 SmallVector<Decl *, 4> ExprCaptures;
8862 SmallVector<Expr *, 4> ExprPostUpdates;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008863 for (auto &RefExpr : VarList) {
8864 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008865 SourceLocation ELoc;
8866 SourceRange ERange;
8867 Expr *SimpleRefExpr = RefExpr;
8868 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008869 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00008870 // It will be analyzed later.
8871 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00008872 SrcExprs.push_back(nullptr);
8873 DstExprs.push_back(nullptr);
8874 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008875 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00008876 ValueDecl *D = Res.first;
8877 if (!D)
8878 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008879
Alexey Bataev74caaf22016-02-20 04:09:36 +00008880 QualType Type = D->getType();
8881 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008882
8883 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
8884 // A variable that appears in a lastprivate clause must not have an
8885 // incomplete type or a reference type.
8886 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +00008887 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +00008888 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008889 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00008890
8891 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
8892 // in a Construct]
8893 // Variables with the predetermined data-sharing attributes may not be
8894 // listed in data-sharing attributes clauses, except for the cases
8895 // listed below.
Alexey Bataev74caaf22016-02-20 04:09:36 +00008896 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008897 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
8898 DVar.CKind != OMPC_firstprivate &&
8899 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
8900 Diag(ELoc, diag::err_omp_wrong_dsa)
8901 << getOpenMPClauseName(DVar.CKind)
8902 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008903 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008904 continue;
8905 }
8906
Alexey Bataevf29276e2014-06-18 04:14:57 +00008907 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8908 // OpenMP [2.14.3.5, Restrictions, p.2]
8909 // A list item that is private within a parallel region, or that appears in
8910 // the reduction clause of a parallel construct, must not appear in a
8911 // lastprivate clause on a worksharing construct if any of the corresponding
8912 // worksharing regions ever binds to any of the corresponding parallel
8913 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00008914 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00008915 if (isOpenMPWorksharingDirective(CurrDir) &&
8916 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +00008917 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008918 if (DVar.CKind != OMPC_shared) {
8919 Diag(ELoc, diag::err_omp_required_access)
8920 << getOpenMPClauseName(OMPC_lastprivate)
8921 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008922 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008923 continue;
8924 }
8925 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00008926
8927 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8928 // A list item may appear in a firstprivate or lastprivate clause but not
8929 // both.
8930 if (CurrDir == OMPD_distribute) {
8931 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8932 if (DVar.CKind == OMPC_firstprivate) {
8933 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
8934 ReportOriginalDSA(*this, DSAStack, D, DVar);
8935 continue;
8936 }
8937 }
8938
Alexander Musman1bb328c2014-06-04 13:06:39 +00008939 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00008940 // A variable of class type (or array thereof) that appears in a
8941 // lastprivate clause requires an accessible, unambiguous default
8942 // constructor for the class type, unless the list item is also specified
8943 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00008944 // A variable of class type (or array thereof) that appears in a
8945 // lastprivate clause requires an accessible, unambiguous copy assignment
8946 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00008947 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008948 auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008949 Type.getUnqualifiedType(), ".lastprivate.src",
Alexey Bataev74caaf22016-02-20 04:09:36 +00008950 D->hasAttrs() ? &D->getAttrs() : nullptr);
8951 auto *PseudoSrcExpr =
8952 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00008953 auto *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008954 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +00008955 D->hasAttrs() ? &D->getAttrs() : nullptr);
8956 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00008957 // For arrays generate assignment operation for single element and replace
8958 // it by the original array element in CodeGen.
Alexey Bataev74caaf22016-02-20 04:09:36 +00008959 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
Alexey Bataev38e89532015-04-16 04:54:05 +00008960 PseudoDstExpr, PseudoSrcExpr);
8961 if (AssignmentOp.isInvalid())
8962 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +00008963 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00008964 /*DiscardedValue=*/true);
8965 if (AssignmentOp.isInvalid())
8966 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008967
Alexey Bataev74caaf22016-02-20 04:09:36 +00008968 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008969 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008970 if (TopDVar.CKind == OMPC_firstprivate)
8971 Ref = TopDVar.PrivateCopy;
8972 else {
Alexey Bataev61205072016-03-02 04:57:40 +00008973 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00008974 if (!IsOpenMPCapturedDecl(D))
8975 ExprCaptures.push_back(Ref->getDecl());
8976 }
8977 if (TopDVar.CKind == OMPC_firstprivate ||
8978 (!IsOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008979 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008980 ExprResult RefRes = DefaultLvalueConversion(Ref);
8981 if (!RefRes.isUsable())
8982 continue;
8983 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008984 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
8985 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008986 if (!PostUpdateRes.isUsable())
8987 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008988 ExprPostUpdates.push_back(
8989 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008990 }
8991 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008992 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008993 Vars.push_back((VD || CurContext->isDependentContext())
8994 ? RefExpr->IgnoreParens()
8995 : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +00008996 SrcExprs.push_back(PseudoSrcExpr);
8997 DstExprs.push_back(PseudoDstExpr);
8998 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00008999 }
9000
9001 if (Vars.empty())
9002 return nullptr;
9003
9004 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +00009005 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009006 buildPreInits(Context, ExprCaptures),
9007 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +00009008}
9009
Alexey Bataev758e55e2013-09-06 18:03:48 +00009010OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
9011 SourceLocation StartLoc,
9012 SourceLocation LParenLoc,
9013 SourceLocation EndLoc) {
9014 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00009015 for (auto &RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009016 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009017 SourceLocation ELoc;
9018 SourceRange ERange;
9019 Expr *SimpleRefExpr = RefExpr;
9020 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009021 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00009022 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009023 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009024 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009025 ValueDecl *D = Res.first;
9026 if (!D)
9027 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +00009028
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009029 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009030 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9031 // in a Construct]
9032 // Variables with the predetermined data-sharing attributes may not be
9033 // listed in data-sharing attributes clauses, except for the cases
9034 // listed below. For these exceptions only, listing a predetermined
9035 // variable in a data-sharing attribute clause is allowed and overrides
9036 // the variable's predetermined data-sharing attributes.
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009037 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00009038 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
9039 DVar.RefExpr) {
9040 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9041 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009042 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009043 continue;
9044 }
9045
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009046 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009047 if (!VD && IsOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00009048 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009049 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009050 Vars.push_back((VD || !Ref || CurContext->isDependentContext())
9051 ? RefExpr->IgnoreParens()
9052 : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009053 }
9054
Alexey Bataeved09d242014-05-28 05:53:51 +00009055 if (Vars.empty())
9056 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00009057
9058 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
9059}
9060
Alexey Bataevc5e02582014-06-16 07:08:35 +00009061namespace {
9062class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
9063 DSAStackTy *Stack;
9064
9065public:
9066 bool VisitDeclRefExpr(DeclRefExpr *E) {
9067 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009068 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00009069 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
9070 return false;
9071 if (DVar.CKind != OMPC_unknown)
9072 return true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +00009073 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
9074 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
9075 false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00009076 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00009077 return true;
9078 return false;
9079 }
9080 return false;
9081 }
9082 bool VisitStmt(Stmt *S) {
9083 for (auto Child : S->children()) {
9084 if (Child && Visit(Child))
9085 return true;
9086 }
9087 return false;
9088 }
Alexey Bataev23b69422014-06-18 07:08:49 +00009089 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00009090};
Alexey Bataev23b69422014-06-18 07:08:49 +00009091} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00009092
Alexey Bataev60da77e2016-02-29 05:54:20 +00009093namespace {
9094// Transform MemberExpression for specified FieldDecl of current class to
9095// DeclRefExpr to specified OMPCapturedExprDecl.
9096class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
9097 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
9098 ValueDecl *Field;
9099 DeclRefExpr *CapturedExpr;
9100
9101public:
9102 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
9103 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
9104
9105 ExprResult TransformMemberExpr(MemberExpr *E) {
9106 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
9107 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +00009108 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009109 return CapturedExpr;
9110 }
9111 return BaseTransform::TransformMemberExpr(E);
9112 }
9113 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
9114};
9115} // namespace
9116
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009117template <typename T>
9118static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups,
9119 const llvm::function_ref<T(ValueDecl *)> &Gen) {
9120 for (auto &Set : Lookups) {
9121 for (auto *D : Set) {
9122 if (auto Res = Gen(cast<ValueDecl>(D)))
9123 return Res;
9124 }
9125 }
9126 return T();
9127}
9128
9129static ExprResult
9130buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
9131 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
9132 const DeclarationNameInfo &ReductionId, QualType Ty,
9133 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
9134 if (ReductionIdScopeSpec.isInvalid())
9135 return ExprError();
9136 SmallVector<UnresolvedSet<8>, 4> Lookups;
9137 if (S) {
9138 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
9139 Lookup.suppressDiagnostics();
9140 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
9141 auto *D = Lookup.getRepresentativeDecl();
9142 do {
9143 S = S->getParent();
9144 } while (S && !S->isDeclScope(D));
9145 if (S)
9146 S = S->getParent();
9147 Lookups.push_back(UnresolvedSet<8>());
9148 Lookups.back().append(Lookup.begin(), Lookup.end());
9149 Lookup.clear();
9150 }
9151 } else if (auto *ULE =
9152 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
9153 Lookups.push_back(UnresolvedSet<8>());
9154 Decl *PrevD = nullptr;
9155 for(auto *D : ULE->decls()) {
9156 if (D == PrevD)
9157 Lookups.push_back(UnresolvedSet<8>());
9158 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
9159 Lookups.back().addDecl(DRD);
9160 PrevD = D;
9161 }
9162 }
9163 if (Ty->isDependentType() || Ty->isInstantiationDependentType() ||
9164 Ty->containsUnexpandedParameterPack() ||
9165 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool {
9166 return !D->isInvalidDecl() &&
9167 (D->getType()->isDependentType() ||
9168 D->getType()->isInstantiationDependentType() ||
9169 D->getType()->containsUnexpandedParameterPack());
9170 })) {
9171 UnresolvedSet<8> ResSet;
9172 for (auto &Set : Lookups) {
9173 ResSet.append(Set.begin(), Set.end());
9174 // The last item marks the end of all declarations at the specified scope.
9175 ResSet.addDecl(Set[Set.size() - 1]);
9176 }
9177 return UnresolvedLookupExpr::Create(
9178 SemaRef.Context, /*NamingClass=*/nullptr,
9179 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
9180 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
9181 }
9182 if (auto *VD = filterLookupForUDR<ValueDecl *>(
9183 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
9184 if (!D->isInvalidDecl() &&
9185 SemaRef.Context.hasSameType(D->getType(), Ty))
9186 return D;
9187 return nullptr;
9188 }))
9189 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
9190 if (auto *VD = filterLookupForUDR<ValueDecl *>(
9191 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
9192 if (!D->isInvalidDecl() &&
9193 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
9194 !Ty.isMoreQualifiedThan(D->getType()))
9195 return D;
9196 return nullptr;
9197 })) {
9198 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
9199 /*DetectVirtual=*/false);
9200 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
9201 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
9202 VD->getType().getUnqualifiedType()))) {
9203 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
9204 /*DiagID=*/0) !=
9205 Sema::AR_inaccessible) {
9206 SemaRef.BuildBasePathArray(Paths, BasePath);
9207 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
9208 }
9209 }
9210 }
9211 }
9212 if (ReductionIdScopeSpec.isSet()) {
9213 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
9214 return ExprError();
9215 }
9216 return ExprEmpty();
9217}
9218
Alexey Bataevc5e02582014-06-16 07:08:35 +00009219OMPClause *Sema::ActOnOpenMPReductionClause(
9220 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
9221 SourceLocation ColonLoc, SourceLocation EndLoc,
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009222 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
9223 ArrayRef<Expr *> UnresolvedReductions) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00009224 auto DN = ReductionId.getName();
9225 auto OOK = DN.getCXXOverloadedOperator();
9226 BinaryOperatorKind BOK = BO_Comma;
9227
9228 // OpenMP [2.14.3.6, reduction clause]
9229 // C
9230 // reduction-identifier is either an identifier or one of the following
9231 // operators: +, -, *, &, |, ^, && and ||
9232 // C++
9233 // reduction-identifier is either an id-expression or one of the following
9234 // operators: +, -, *, &, |, ^, && and ||
9235 // FIXME: Only 'min' and 'max' identifiers are supported for now.
9236 switch (OOK) {
9237 case OO_Plus:
9238 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009239 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009240 break;
9241 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009242 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009243 break;
9244 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009245 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009246 break;
9247 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009248 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009249 break;
9250 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009251 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009252 break;
9253 case OO_AmpAmp:
9254 BOK = BO_LAnd;
9255 break;
9256 case OO_PipePipe:
9257 BOK = BO_LOr;
9258 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009259 case OO_New:
9260 case OO_Delete:
9261 case OO_Array_New:
9262 case OO_Array_Delete:
9263 case OO_Slash:
9264 case OO_Percent:
9265 case OO_Tilde:
9266 case OO_Exclaim:
9267 case OO_Equal:
9268 case OO_Less:
9269 case OO_Greater:
9270 case OO_LessEqual:
9271 case OO_GreaterEqual:
9272 case OO_PlusEqual:
9273 case OO_MinusEqual:
9274 case OO_StarEqual:
9275 case OO_SlashEqual:
9276 case OO_PercentEqual:
9277 case OO_CaretEqual:
9278 case OO_AmpEqual:
9279 case OO_PipeEqual:
9280 case OO_LessLess:
9281 case OO_GreaterGreater:
9282 case OO_LessLessEqual:
9283 case OO_GreaterGreaterEqual:
9284 case OO_EqualEqual:
9285 case OO_ExclaimEqual:
9286 case OO_PlusPlus:
9287 case OO_MinusMinus:
9288 case OO_Comma:
9289 case OO_ArrowStar:
9290 case OO_Arrow:
9291 case OO_Call:
9292 case OO_Subscript:
9293 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00009294 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009295 case NUM_OVERLOADED_OPERATORS:
9296 llvm_unreachable("Unexpected reduction identifier");
9297 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00009298 if (auto II = DN.getAsIdentifierInfo()) {
9299 if (II->isStr("max"))
9300 BOK = BO_GT;
9301 else if (II->isStr("min"))
9302 BOK = BO_LT;
9303 }
9304 break;
9305 }
9306 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009307 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +00009308 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009309 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009310
9311 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009312 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009313 SmallVector<Expr *, 8> LHSs;
9314 SmallVector<Expr *, 8> RHSs;
9315 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev61205072016-03-02 04:57:40 +00009316 SmallVector<Decl *, 4> ExprCaptures;
9317 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009318 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
9319 bool FirstIter = true;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009320 for (auto RefExpr : VarList) {
9321 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +00009322 // OpenMP [2.1, C/C++]
9323 // A list item is a variable or array section, subject to the restrictions
9324 // specified in Section 2.4 on page 42 and in each of the sections
9325 // describing clauses and directives for which a list appears.
9326 // OpenMP [2.14.3.3, Restrictions, p.1]
9327 // A variable that is part of another variable (as an array or
9328 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009329 if (!FirstIter && IR != ER)
9330 ++IR;
9331 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009332 SourceLocation ELoc;
9333 SourceRange ERange;
9334 Expr *SimpleRefExpr = RefExpr;
9335 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9336 /*AllowArraySection=*/true);
9337 if (Res.second) {
9338 // It will be analyzed later.
9339 Vars.push_back(RefExpr);
9340 Privates.push_back(nullptr);
9341 LHSs.push_back(nullptr);
9342 RHSs.push_back(nullptr);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009343 // Try to find 'declare reduction' corresponding construct before using
9344 // builtin/overloaded operators.
9345 QualType Type = Context.DependentTy;
9346 CXXCastPath BasePath;
9347 ExprResult DeclareReductionRef = buildDeclareReductionRef(
9348 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
9349 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
9350 if (CurContext->isDependentContext() &&
9351 (DeclareReductionRef.isUnset() ||
9352 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
9353 ReductionOps.push_back(DeclareReductionRef.get());
9354 else
9355 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00009356 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00009357 ValueDecl *D = Res.first;
9358 if (!D)
9359 continue;
9360
Alexey Bataeva1764212015-09-30 09:22:36 +00009361 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009362 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
9363 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
9364 if (ASE)
Alexey Bataev31300ed2016-02-04 11:27:03 +00009365 Type = ASE->getType().getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009366 else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009367 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
9368 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
9369 Type = ATy->getElementType();
9370 else
9371 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +00009372 Type = Type.getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009373 } else
9374 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
9375 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +00009376
Alexey Bataevc5e02582014-06-16 07:08:35 +00009377 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9378 // A variable that appears in a private clause must not have an incomplete
9379 // type or a reference type.
9380 if (RequireCompleteType(ELoc, Type,
9381 diag::err_omp_reduction_incomplete_type))
9382 continue;
9383 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00009384 // A list item that appears in a reduction clause must not be
9385 // const-qualified.
9386 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009387 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00009388 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009389 if (!ASE && !OASE) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009390 bool IsDecl = !VD ||
9391 VD->isThisDeclarationADefinition(Context) ==
9392 VarDecl::DeclarationOnly;
9393 Diag(D->getLocation(),
Alexey Bataeva1764212015-09-30 09:22:36 +00009394 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +00009395 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +00009396 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00009397 continue;
9398 }
9399 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
9400 // If a list-item is a reference type then it must bind to the same object
9401 // for all threads of the team.
Alexey Bataev60da77e2016-02-29 05:54:20 +00009402 if (!ASE && !OASE && VD) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009403 VarDecl *VDDef = VD->getDefinition();
Alexey Bataev31300ed2016-02-04 11:27:03 +00009404 if (VD->getType()->isReferenceType() && VDDef) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009405 DSARefChecker Check(DSAStack);
9406 if (Check.Visit(VDDef->getInit())) {
9407 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
9408 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
9409 continue;
9410 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00009411 }
9412 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009413
Alexey Bataevc5e02582014-06-16 07:08:35 +00009414 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
9415 // in a Construct]
9416 // Variables with the predetermined data-sharing attributes may not be
9417 // listed in data-sharing attributes clauses, except for the cases
9418 // listed below. For these exceptions only, listing a predetermined
9419 // variable in a data-sharing attribute clause is allowed and overrides
9420 // the variable's predetermined data-sharing attributes.
9421 // OpenMP [2.14.3.6, Restrictions, p.3]
9422 // Any number of reduction clauses can be specified on the directive,
9423 // but a list item can appear only once in the reduction clauses for that
9424 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00009425 DSAStackTy::DSAVarData DVar;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009426 DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009427 if (DVar.CKind == OMPC_reduction) {
9428 Diag(ELoc, diag::err_omp_once_referenced)
9429 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009430 if (DVar.RefExpr)
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009431 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009432 } else if (DVar.CKind != OMPC_unknown) {
9433 Diag(ELoc, diag::err_omp_wrong_dsa)
9434 << getOpenMPClauseName(DVar.CKind)
9435 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009436 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009437 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009438 }
9439
9440 // OpenMP [2.14.3.6, Restrictions, p.1]
9441 // A list item that appears in a reduction clause of a worksharing
9442 // construct must be shared in the parallel regions to which any of the
9443 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009444 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
9445 if (isOpenMPWorksharingDirective(CurrDir) &&
9446 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009447 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009448 if (DVar.CKind != OMPC_shared) {
9449 Diag(ELoc, diag::err_omp_required_access)
9450 << getOpenMPClauseName(OMPC_reduction)
9451 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009452 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009453 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00009454 }
9455 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009456
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009457 // Try to find 'declare reduction' corresponding construct before using
9458 // builtin/overloaded operators.
9459 CXXCastPath BasePath;
9460 ExprResult DeclareReductionRef = buildDeclareReductionRef(
9461 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
9462 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
9463 if (DeclareReductionRef.isInvalid())
9464 continue;
9465 if (CurContext->isDependentContext() &&
9466 (DeclareReductionRef.isUnset() ||
9467 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
9468 Vars.push_back(RefExpr);
9469 Privates.push_back(nullptr);
9470 LHSs.push_back(nullptr);
9471 RHSs.push_back(nullptr);
9472 ReductionOps.push_back(DeclareReductionRef.get());
9473 continue;
9474 }
9475 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
9476 // Not allowed reduction identifier is found.
9477 Diag(ReductionId.getLocStart(),
9478 diag::err_omp_unknown_reduction_identifier)
9479 << Type << ReductionIdRange;
9480 continue;
9481 }
9482
9483 // OpenMP [2.14.3.6, reduction clause, Restrictions]
9484 // The type of a list item that appears in a reduction clause must be valid
9485 // for the reduction-identifier. For a max or min reduction in C, the type
9486 // of the list item must be an allowed arithmetic data type: char, int,
9487 // float, double, or _Bool, possibly modified with long, short, signed, or
9488 // unsigned. For a max or min reduction in C++, the type of the list item
9489 // must be an allowed arithmetic data type: char, wchar_t, int, float,
9490 // double, or bool, possibly modified with long, short, signed, or unsigned.
9491 if (DeclareReductionRef.isUnset()) {
9492 if ((BOK == BO_GT || BOK == BO_LT) &&
9493 !(Type->isScalarType() ||
9494 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
9495 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
9496 << getLangOpts().CPlusPlus;
9497 if (!ASE && !OASE) {
9498 bool IsDecl = !VD ||
9499 VD->isThisDeclarationADefinition(Context) ==
9500 VarDecl::DeclarationOnly;
9501 Diag(D->getLocation(),
9502 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9503 << D;
9504 }
9505 continue;
9506 }
9507 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
9508 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
9509 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
9510 if (!ASE && !OASE) {
9511 bool IsDecl = !VD ||
9512 VD->isThisDeclarationADefinition(Context) ==
9513 VarDecl::DeclarationOnly;
9514 Diag(D->getLocation(),
9515 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9516 << D;
9517 }
9518 continue;
9519 }
9520 }
9521
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009522 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009523 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
Alexey Bataev60da77e2016-02-29 05:54:20 +00009524 D->hasAttrs() ? &D->getAttrs() : nullptr);
9525 auto *RHSVD = buildVarDecl(*this, ELoc, Type, D->getName(),
9526 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009527 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00009528 if (OASE ||
Alexey Bataev60da77e2016-02-29 05:54:20 +00009529 (!ASE &&
9530 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Alexey Bataev1189bd02016-01-26 12:20:39 +00009531 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009532 // Create pseudo array type for private copy. The size for this array will
9533 // be generated during codegen.
9534 // For array subscripts or single variables Private Ty is the same as Type
9535 // (type of the variable or single array element).
9536 PrivateTy = Context.getVariableArrayType(
9537 Type, new (Context) OpaqueValueExpr(SourceLocation(),
9538 Context.getSizeType(), VK_RValue),
9539 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +00009540 } else if (!ASE && !OASE &&
9541 Context.getAsArrayType(D->getType().getNonReferenceType()))
9542 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009543 // Private copy.
Alexey Bataev60da77e2016-02-29 05:54:20 +00009544 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, D->getName(),
9545 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009546 // Add initializer for private variable.
9547 Expr *Init = nullptr;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009548 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
9549 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
9550 if (DeclareReductionRef.isUsable()) {
9551 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
9552 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
9553 if (DRD->getInitializer()) {
9554 Init = DRDRef;
9555 RHSVD->setInit(DRDRef);
9556 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009557 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009558 } else {
9559 switch (BOK) {
9560 case BO_Add:
9561 case BO_Xor:
9562 case BO_Or:
9563 case BO_LOr:
9564 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
9565 if (Type->isScalarType() || Type->isAnyComplexType())
9566 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
9567 break;
9568 case BO_Mul:
9569 case BO_LAnd:
9570 if (Type->isScalarType() || Type->isAnyComplexType()) {
9571 // '*' and '&&' reduction ops - initializer is '1'.
9572 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00009573 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009574 break;
9575 case BO_And: {
9576 // '&' reduction op - initializer is '~0'.
9577 QualType OrigType = Type;
9578 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
9579 Type = ComplexTy->getElementType();
9580 if (Type->isRealFloatingType()) {
9581 llvm::APFloat InitValue =
9582 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
9583 /*isIEEE=*/true);
9584 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
9585 Type, ELoc);
9586 } else if (Type->isScalarType()) {
9587 auto Size = Context.getTypeSize(Type);
9588 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
9589 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
9590 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
9591 }
9592 if (Init && OrigType->isAnyComplexType()) {
9593 // Init = 0xFFFF + 0xFFFFi;
9594 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
9595 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
9596 }
9597 Type = OrigType;
9598 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009599 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009600 case BO_LT:
9601 case BO_GT: {
9602 // 'min' reduction op - initializer is 'Largest representable number in
9603 // the reduction list item type'.
9604 // 'max' reduction op - initializer is 'Least representable number in
9605 // the reduction list item type'.
9606 if (Type->isIntegerType() || Type->isPointerType()) {
9607 bool IsSigned = Type->hasSignedIntegerRepresentation();
9608 auto Size = Context.getTypeSize(Type);
9609 QualType IntTy =
9610 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
9611 llvm::APInt InitValue =
9612 (BOK != BO_LT)
9613 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
9614 : llvm::APInt::getMinValue(Size)
9615 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
9616 : llvm::APInt::getMaxValue(Size);
9617 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
9618 if (Type->isPointerType()) {
9619 // Cast to pointer type.
9620 auto CastExpr = BuildCStyleCastExpr(
9621 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
9622 SourceLocation(), Init);
9623 if (CastExpr.isInvalid())
9624 continue;
9625 Init = CastExpr.get();
9626 }
9627 } else if (Type->isRealFloatingType()) {
9628 llvm::APFloat InitValue = llvm::APFloat::getLargest(
9629 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
9630 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
9631 Type, ELoc);
9632 }
9633 break;
9634 }
9635 case BO_PtrMemD:
9636 case BO_PtrMemI:
9637 case BO_MulAssign:
9638 case BO_Div:
9639 case BO_Rem:
9640 case BO_Sub:
9641 case BO_Shl:
9642 case BO_Shr:
9643 case BO_LE:
9644 case BO_GE:
9645 case BO_EQ:
9646 case BO_NE:
9647 case BO_AndAssign:
9648 case BO_XorAssign:
9649 case BO_OrAssign:
9650 case BO_Assign:
9651 case BO_AddAssign:
9652 case BO_SubAssign:
9653 case BO_DivAssign:
9654 case BO_RemAssign:
9655 case BO_ShlAssign:
9656 case BO_ShrAssign:
9657 case BO_Comma:
9658 llvm_unreachable("Unexpected reduction operation");
9659 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009660 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009661 if (Init && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009662 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
9663 /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009664 } else if (!Init)
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009665 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009666 if (RHSVD->isInvalidDecl())
9667 continue;
9668 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009669 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
9670 << ReductionIdRange;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009671 bool IsDecl =
9672 !VD ||
9673 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9674 Diag(D->getLocation(),
9675 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9676 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009677 continue;
9678 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009679 // Store initializer for single element in private copy. Will be used during
9680 // codegen.
9681 PrivateVD->setInit(RHSVD->getInit());
9682 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009683 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009684 ExprResult ReductionOp;
9685 if (DeclareReductionRef.isUsable()) {
9686 QualType RedTy = DeclareReductionRef.get()->getType();
9687 QualType PtrRedTy = Context.getPointerType(RedTy);
9688 ExprResult LHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
9689 ExprResult RHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
9690 if (!BasePath.empty()) {
9691 LHS = DefaultLvalueConversion(LHS.get());
9692 RHS = DefaultLvalueConversion(RHS.get());
9693 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
9694 CK_UncheckedDerivedToBase, LHS.get(),
9695 &BasePath, LHS.get()->getValueKind());
9696 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
9697 CK_UncheckedDerivedToBase, RHS.get(),
9698 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009699 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009700 FunctionProtoType::ExtProtoInfo EPI;
9701 QualType Params[] = {PtrRedTy, PtrRedTy};
9702 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
9703 auto *OVE = new (Context) OpaqueValueExpr(
9704 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
9705 DefaultLvalueConversion(DeclareReductionRef.get()).get());
9706 Expr *Args[] = {LHS.get(), RHS.get()};
9707 ReductionOp = new (Context)
9708 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
9709 } else {
9710 ReductionOp = BuildBinOp(DSAStack->getCurScope(),
9711 ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE);
9712 if (ReductionOp.isUsable()) {
9713 if (BOK != BO_LT && BOK != BO_GT) {
9714 ReductionOp =
9715 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
9716 BO_Assign, LHSDRE, ReductionOp.get());
9717 } else {
9718 auto *ConditionalOp = new (Context) ConditionalOperator(
9719 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
9720 RHSDRE, Type, VK_LValue, OK_Ordinary);
9721 ReductionOp =
9722 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
9723 BO_Assign, LHSDRE, ConditionalOp);
9724 }
9725 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
9726 }
9727 if (ReductionOp.isInvalid())
9728 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009729 }
9730
Alexey Bataev60da77e2016-02-29 05:54:20 +00009731 DeclRefExpr *Ref = nullptr;
9732 Expr *VarsExpr = RefExpr->IgnoreParens();
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009733 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009734 if (ASE || OASE) {
9735 TransformExprToCaptures RebuildToCapture(*this, D);
9736 VarsExpr =
9737 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
9738 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +00009739 } else {
9740 VarsExpr = Ref =
9741 buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +00009742 }
9743 if (!IsOpenMPCapturedDecl(D)) {
9744 ExprCaptures.push_back(Ref->getDecl());
9745 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
9746 ExprResult RefRes = DefaultLvalueConversion(Ref);
9747 if (!RefRes.isUsable())
9748 continue;
9749 ExprResult PostUpdateRes =
9750 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
9751 SimpleRefExpr, RefRes.get());
9752 if (!PostUpdateRes.isUsable())
9753 continue;
9754 ExprPostUpdates.push_back(
9755 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +00009756 }
9757 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00009758 }
9759 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
9760 Vars.push_back(VarsExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009761 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009762 LHSs.push_back(LHSDRE);
9763 RHSs.push_back(RHSDRE);
9764 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009765 }
9766
9767 if (Vars.empty())
9768 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +00009769
Alexey Bataevc5e02582014-06-16 07:08:35 +00009770 return OMPReductionClause::Create(
9771 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009772 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009773 LHSs, RHSs, ReductionOps, buildPreInits(Context, ExprCaptures),
9774 buildPostUpdate(*this, ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +00009775}
9776
Alexey Bataevecba70f2016-04-12 11:02:11 +00009777bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
9778 SourceLocation LinLoc) {
9779 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
9780 LinKind == OMPC_LINEAR_unknown) {
9781 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
9782 return true;
9783 }
9784 return false;
9785}
9786
9787bool Sema::CheckOpenMPLinearDecl(ValueDecl *D, SourceLocation ELoc,
9788 OpenMPLinearClauseKind LinKind,
9789 QualType Type) {
9790 auto *VD = dyn_cast_or_null<VarDecl>(D);
9791 // A variable must not have an incomplete type or a reference type.
9792 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
9793 return true;
9794 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
9795 !Type->isReferenceType()) {
9796 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
9797 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
9798 return true;
9799 }
9800 Type = Type.getNonReferenceType();
9801
9802 // A list item must not be const-qualified.
9803 if (Type.isConstant(Context)) {
9804 Diag(ELoc, diag::err_omp_const_variable)
9805 << getOpenMPClauseName(OMPC_linear);
9806 if (D) {
9807 bool IsDecl =
9808 !VD ||
9809 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9810 Diag(D->getLocation(),
9811 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9812 << D;
9813 }
9814 return true;
9815 }
9816
9817 // A list item must be of integral or pointer type.
9818 Type = Type.getUnqualifiedType().getCanonicalType();
9819 const auto *Ty = Type.getTypePtrOrNull();
9820 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
9821 !Ty->isPointerType())) {
9822 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
9823 if (D) {
9824 bool IsDecl =
9825 !VD ||
9826 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9827 Diag(D->getLocation(),
9828 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9829 << D;
9830 }
9831 return true;
9832 }
9833 return false;
9834}
9835
Alexey Bataev182227b2015-08-20 10:54:39 +00009836OMPClause *Sema::ActOnOpenMPLinearClause(
9837 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
9838 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
9839 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00009840 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009841 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00009842 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +00009843 SmallVector<Decl *, 4> ExprCaptures;
9844 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +00009845 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +00009846 LinKind = OMPC_LINEAR_val;
Alexey Bataeved09d242014-05-28 05:53:51 +00009847 for (auto &RefExpr : VarList) {
9848 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009849 SourceLocation ELoc;
9850 SourceRange ERange;
9851 Expr *SimpleRefExpr = RefExpr;
9852 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9853 /*AllowArraySection=*/false);
9854 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +00009855 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009856 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009857 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00009858 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00009859 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009860 ValueDecl *D = Res.first;
9861 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +00009862 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +00009863
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009864 QualType Type = D->getType();
9865 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +00009866
9867 // OpenMP [2.14.3.7, linear clause]
9868 // A list-item cannot appear in more than one linear clause.
9869 // A list-item that appears in a linear clause cannot appear in any
9870 // other data-sharing attribute clause.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009871 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00009872 if (DVar.RefExpr) {
9873 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9874 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009875 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00009876 continue;
9877 }
9878
Alexey Bataevecba70f2016-04-12 11:02:11 +00009879 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +00009880 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00009881 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +00009882
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009883 // Build private copy of original var.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009884 auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(),
9885 D->hasAttrs() ? &D->getAttrs() : nullptr);
9886 auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +00009887 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009888 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009889 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009890 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009891 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev78849fb2016-03-09 09:49:00 +00009892 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
9893 if (!IsOpenMPCapturedDecl(D)) {
9894 ExprCaptures.push_back(Ref->getDecl());
9895 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
9896 ExprResult RefRes = DefaultLvalueConversion(Ref);
9897 if (!RefRes.isUsable())
9898 continue;
9899 ExprResult PostUpdateRes =
9900 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
9901 SimpleRefExpr, RefRes.get());
9902 if (!PostUpdateRes.isUsable())
9903 continue;
9904 ExprPostUpdates.push_back(
9905 IgnoredValueConversions(PostUpdateRes.get()).get());
9906 }
9907 }
9908 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009909 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009910 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009911 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009912 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009913 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009914 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
9915 auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
9916
9917 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009918 Vars.push_back((VD || CurContext->isDependentContext())
9919 ? RefExpr->IgnoreParens()
9920 : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009921 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00009922 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00009923 }
9924
9925 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009926 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00009927
9928 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00009929 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00009930 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
9931 !Step->isInstantiationDependent() &&
9932 !Step->containsUnexpandedParameterPack()) {
9933 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00009934 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00009935 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009936 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00009937 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00009938
Alexander Musman3276a272015-03-21 10:12:56 +00009939 // Build var to save the step value.
9940 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00009941 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00009942 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00009943 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00009944 ExprResult CalcStep =
9945 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009946 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00009947
Alexander Musman8dba6642014-04-22 13:09:42 +00009948 // Warn about zero linear step (it would be probably better specified as
9949 // making corresponding variables 'const').
9950 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00009951 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
9952 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00009953 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
9954 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00009955 if (!IsConstant && CalcStep.isUsable()) {
9956 // Calculate the step beforehand instead of doing this on each iteration.
9957 // (This is not used if the number of iterations may be kfold-ed).
9958 CalcStepExpr = CalcStep.get();
9959 }
Alexander Musman8dba6642014-04-22 13:09:42 +00009960 }
9961
Alexey Bataev182227b2015-08-20 10:54:39 +00009962 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
9963 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009964 StepExpr, CalcStepExpr,
9965 buildPreInits(Context, ExprCaptures),
9966 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +00009967}
9968
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009969static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
9970 Expr *NumIterations, Sema &SemaRef,
9971 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +00009972 // Walk the vars and build update/final expressions for the CodeGen.
9973 SmallVector<Expr *, 8> Updates;
9974 SmallVector<Expr *, 8> Finals;
9975 Expr *Step = Clause.getStep();
9976 Expr *CalcStep = Clause.getCalcStep();
9977 // OpenMP [2.14.3.7, linear clause]
9978 // If linear-step is not specified it is assumed to be 1.
9979 if (Step == nullptr)
9980 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009981 else if (CalcStep) {
Alexander Musman3276a272015-03-21 10:12:56 +00009982 Step = cast<BinaryOperator>(CalcStep)->getLHS();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009983 }
Alexander Musman3276a272015-03-21 10:12:56 +00009984 bool HasErrors = false;
9985 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009986 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009987 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00009988 for (auto &RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009989 SourceLocation ELoc;
9990 SourceRange ERange;
9991 Expr *SimpleRefExpr = RefExpr;
9992 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange,
9993 /*AllowArraySection=*/false);
9994 ValueDecl *D = Res.first;
9995 if (Res.second || !D) {
9996 Updates.push_back(nullptr);
9997 Finals.push_back(nullptr);
9998 HasErrors = true;
9999 continue;
10000 }
10001 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) {
10002 D = cast<MemberExpr>(CED->getInit()->IgnoreParenImpCasts())
10003 ->getMemberDecl();
10004 }
10005 auto &&Info = Stack->isLoopControlVariable(D);
Alexander Musman3276a272015-03-21 10:12:56 +000010006 Expr *InitExpr = *CurInit;
10007
10008 // Build privatized reference to the current linear var.
Alexey Bataev5dff95c2016-04-22 03:56:56 +000010009 auto DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000010010 Expr *CapturedRef;
10011 if (LinKind == OMPC_LINEAR_uval)
10012 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
10013 else
10014 CapturedRef =
10015 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
10016 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
10017 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +000010018
10019 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000010020 ExprResult Update;
10021 if (!Info.first) {
10022 Update =
10023 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
10024 InitExpr, IV, Step, /* Subtract */ false);
10025 } else
10026 Update = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +000010027 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
10028 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +000010029
10030 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000010031 ExprResult Final;
10032 if (!Info.first) {
10033 Final = BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
10034 InitExpr, NumIterations, Step,
10035 /* Subtract */ false);
10036 } else
10037 Final = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +000010038 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
10039 /*DiscardedValue=*/true);
Alexey Bataev5dff95c2016-04-22 03:56:56 +000010040
Alexander Musman3276a272015-03-21 10:12:56 +000010041 if (!Update.isUsable() || !Final.isUsable()) {
10042 Updates.push_back(nullptr);
10043 Finals.push_back(nullptr);
10044 HasErrors = true;
10045 } else {
10046 Updates.push_back(Update.get());
10047 Finals.push_back(Final.get());
10048 }
Richard Trieucc3949d2016-02-18 22:34:54 +000010049 ++CurInit;
10050 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +000010051 }
10052 Clause.setUpdates(Updates);
10053 Clause.setFinals(Finals);
10054 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +000010055}
10056
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010057OMPClause *Sema::ActOnOpenMPAlignedClause(
10058 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
10059 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
10060
10061 SmallVector<Expr *, 8> Vars;
10062 for (auto &RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +000010063 assert(RefExpr && "NULL expr in OpenMP linear clause.");
10064 SourceLocation ELoc;
10065 SourceRange ERange;
10066 Expr *SimpleRefExpr = RefExpr;
10067 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
10068 /*AllowArraySection=*/false);
10069 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010070 // It will be analyzed later.
10071 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010072 }
Alexey Bataev1efd1662016-03-29 10:59:56 +000010073 ValueDecl *D = Res.first;
10074 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010075 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010076
Alexey Bataev1efd1662016-03-29 10:59:56 +000010077 QualType QType = D->getType();
10078 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010079
10080 // OpenMP [2.8.1, simd construct, Restrictions]
10081 // The type of list items appearing in the aligned clause must be
10082 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010083 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010084 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +000010085 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010086 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +000010087 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010088 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +000010089 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010090 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +000010091 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010092 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +000010093 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010094 continue;
10095 }
10096
10097 // OpenMP [2.8.1, simd construct, Restrictions]
10098 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev1efd1662016-03-29 10:59:56 +000010099 if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +000010100 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010101 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
10102 << getOpenMPClauseName(OMPC_aligned);
10103 continue;
10104 }
10105
Alexey Bataev1efd1662016-03-29 10:59:56 +000010106 DeclRefExpr *Ref = nullptr;
10107 if (!VD && IsOpenMPCapturedDecl(D))
10108 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
10109 Vars.push_back(DefaultFunctionArrayConversion(
10110 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
10111 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010112 }
10113
10114 // OpenMP [2.8.1, simd construct, Description]
10115 // The parameter of the aligned clause, alignment, must be a constant
10116 // positive integer expression.
10117 // If no optional parameter is specified, implementation-defined default
10118 // alignments for SIMD instructions on the target platforms are assumed.
10119 if (Alignment != nullptr) {
10120 ExprResult AlignResult =
10121 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
10122 if (AlignResult.isInvalid())
10123 return nullptr;
10124 Alignment = AlignResult.get();
10125 }
10126 if (Vars.empty())
10127 return nullptr;
10128
10129 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
10130 EndLoc, Vars, Alignment);
10131}
10132
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010133OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
10134 SourceLocation StartLoc,
10135 SourceLocation LParenLoc,
10136 SourceLocation EndLoc) {
10137 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010138 SmallVector<Expr *, 8> SrcExprs;
10139 SmallVector<Expr *, 8> DstExprs;
10140 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +000010141 for (auto &RefExpr : VarList) {
10142 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
10143 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010144 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000010145 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010146 SrcExprs.push_back(nullptr);
10147 DstExprs.push_back(nullptr);
10148 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010149 continue;
10150 }
10151
Alexey Bataeved09d242014-05-28 05:53:51 +000010152 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010153 // OpenMP [2.1, C/C++]
10154 // A list item is a variable name.
10155 // OpenMP [2.14.4.1, Restrictions, p.1]
10156 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +000010157 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010158 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000010159 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
10160 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010161 continue;
10162 }
10163
10164 Decl *D = DE->getDecl();
10165 VarDecl *VD = cast<VarDecl>(D);
10166
10167 QualType Type = VD->getType();
10168 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
10169 // It will be analyzed later.
10170 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010171 SrcExprs.push_back(nullptr);
10172 DstExprs.push_back(nullptr);
10173 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010174 continue;
10175 }
10176
10177 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
10178 // A list item that appears in a copyin clause must be threadprivate.
10179 if (!DSAStack->isThreadPrivate(VD)) {
10180 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +000010181 << getOpenMPClauseName(OMPC_copyin)
10182 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010183 continue;
10184 }
10185
10186 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
10187 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +000010188 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010189 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010190 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000010191 auto *SrcVD =
10192 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
10193 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +000010194 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010195 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
10196 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000010197 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
10198 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010199 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010200 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010201 // For arrays generate assignment operation for single element and replace
10202 // it by the original array element in CodeGen.
10203 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
10204 PseudoDstExpr, PseudoSrcExpr);
10205 if (AssignmentOp.isInvalid())
10206 continue;
10207 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
10208 /*DiscardedValue=*/true);
10209 if (AssignmentOp.isInvalid())
10210 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010211
10212 DSAStack->addDSA(VD, DE, OMPC_copyin);
10213 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010214 SrcExprs.push_back(PseudoSrcExpr);
10215 DstExprs.push_back(PseudoDstExpr);
10216 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010217 }
10218
Alexey Bataeved09d242014-05-28 05:53:51 +000010219 if (Vars.empty())
10220 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010221
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010222 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
10223 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010224}
10225
Alexey Bataevbae9a792014-06-27 10:37:06 +000010226OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
10227 SourceLocation StartLoc,
10228 SourceLocation LParenLoc,
10229 SourceLocation EndLoc) {
10230 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +000010231 SmallVector<Expr *, 8> SrcExprs;
10232 SmallVector<Expr *, 8> DstExprs;
10233 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +000010234 for (auto &RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +000010235 assert(RefExpr && "NULL expr in OpenMP linear clause.");
10236 SourceLocation ELoc;
10237 SourceRange ERange;
10238 Expr *SimpleRefExpr = RefExpr;
10239 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
10240 /*AllowArraySection=*/false);
10241 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000010242 // It will be analyzed later.
10243 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000010244 SrcExprs.push_back(nullptr);
10245 DstExprs.push_back(nullptr);
10246 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010247 }
Alexey Bataeve122da12016-03-17 10:50:17 +000010248 ValueDecl *D = Res.first;
10249 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +000010250 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000010251
Alexey Bataeve122da12016-03-17 10:50:17 +000010252 QualType Type = D->getType();
10253 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010254
10255 // OpenMP [2.14.4.2, Restrictions, p.2]
10256 // A list item that appears in a copyprivate clause may not appear in a
10257 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +000010258 if (!VD || !DSAStack->isThreadPrivate(VD)) {
10259 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +000010260 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
10261 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000010262 Diag(ELoc, diag::err_omp_wrong_dsa)
10263 << getOpenMPClauseName(DVar.CKind)
10264 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve122da12016-03-17 10:50:17 +000010265 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010266 continue;
10267 }
10268
10269 // OpenMP [2.11.4.2, Restrictions, p.1]
10270 // All list items that appear in a copyprivate clause must be either
10271 // threadprivate or private in the enclosing context.
10272 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +000010273 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010274 if (DVar.CKind == OMPC_shared) {
10275 Diag(ELoc, diag::err_omp_required_access)
10276 << getOpenMPClauseName(OMPC_copyprivate)
10277 << "threadprivate or private in the enclosing context";
Alexey Bataeve122da12016-03-17 10:50:17 +000010278 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010279 continue;
10280 }
10281 }
10282 }
10283
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010284 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000010285 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010286 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010287 << getOpenMPClauseName(OMPC_copyprivate) << Type
10288 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010289 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +000010290 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010291 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +000010292 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010293 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +000010294 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010295 continue;
10296 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010297
Alexey Bataevbae9a792014-06-27 10:37:06 +000010298 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
10299 // A variable of class type (or array thereof) that appears in a
10300 // copyin clause requires an accessible, unambiguous copy assignment
10301 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010302 Type = Context.getBaseElementType(Type.getNonReferenceType())
10303 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +000010304 auto *SrcVD =
Alexey Bataeve122da12016-03-17 10:50:17 +000010305 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src",
10306 D->hasAttrs() ? &D->getAttrs() : nullptr);
10307 auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
Alexey Bataev420d45b2015-04-14 05:11:24 +000010308 auto *DstVD =
Alexey Bataeve122da12016-03-17 10:50:17 +000010309 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst",
10310 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +000010311 auto *PseudoDstExpr =
Alexey Bataeve122da12016-03-17 10:50:17 +000010312 buildDeclRefExpr(*this, DstVD, Type, ELoc);
10313 auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
Alexey Bataeva63048e2015-03-23 06:18:07 +000010314 PseudoDstExpr, PseudoSrcExpr);
10315 if (AssignmentOp.isInvalid())
10316 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +000010317 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +000010318 /*DiscardedValue=*/true);
10319 if (AssignmentOp.isInvalid())
10320 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000010321
10322 // No need to mark vars as copyprivate, they are already threadprivate or
10323 // implicitly private.
Alexey Bataeve122da12016-03-17 10:50:17 +000010324 assert(VD || IsOpenMPCapturedDecl(D));
10325 Vars.push_back(
10326 VD ? RefExpr->IgnoreParens()
10327 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +000010328 SrcExprs.push_back(PseudoSrcExpr);
10329 DstExprs.push_back(PseudoDstExpr);
10330 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +000010331 }
10332
10333 if (Vars.empty())
10334 return nullptr;
10335
Alexey Bataeva63048e2015-03-23 06:18:07 +000010336 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10337 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010338}
10339
Alexey Bataev6125da92014-07-21 11:26:11 +000010340OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
10341 SourceLocation StartLoc,
10342 SourceLocation LParenLoc,
10343 SourceLocation EndLoc) {
10344 if (VarList.empty())
10345 return nullptr;
10346
10347 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
10348}
Alexey Bataevdea47612014-07-23 07:46:59 +000010349
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010350OMPClause *
10351Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
10352 SourceLocation DepLoc, SourceLocation ColonLoc,
10353 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
10354 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +000010355 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010356 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +000010357 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000010358 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +000010359 return nullptr;
10360 }
10361 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010362 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
10363 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +000010364 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010365 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000010366 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
10367 /*Last=*/OMPC_DEPEND_unknown, Except)
10368 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010369 return nullptr;
10370 }
10371 SmallVector<Expr *, 8> Vars;
Alexey Bataev8b427062016-05-25 12:36:08 +000010372 DSAStackTy::OperatorOffsetTy OpsOffs;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010373 llvm::APSInt DepCounter(/*BitWidth=*/32);
10374 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
10375 if (DepKind == OMPC_DEPEND_sink) {
10376 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
10377 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
10378 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010379 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010380 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010381 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
10382 DSAStack->getParentOrderedRegionParam()) {
10383 for (auto &RefExpr : VarList) {
10384 assert(RefExpr && "NULL expr in OpenMP shared clause.");
Alexey Bataev8b427062016-05-25 12:36:08 +000010385 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010386 // It will be analyzed later.
10387 Vars.push_back(RefExpr);
10388 continue;
10389 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010390
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010391 SourceLocation ELoc = RefExpr->getExprLoc();
10392 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
10393 if (DepKind == OMPC_DEPEND_sink) {
10394 if (DepCounter >= TotalDepCount) {
10395 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
10396 continue;
10397 }
10398 ++DepCounter;
10399 // OpenMP [2.13.9, Summary]
10400 // depend(dependence-type : vec), where dependence-type is:
10401 // 'sink' and where vec is the iteration vector, which has the form:
10402 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
10403 // where n is the value specified by the ordered clause in the loop
10404 // directive, xi denotes the loop iteration variable of the i-th nested
10405 // loop associated with the loop directive, and di is a constant
10406 // non-negative integer.
Alexey Bataev8b427062016-05-25 12:36:08 +000010407 if (CurContext->isDependentContext()) {
10408 // It will be analyzed later.
10409 Vars.push_back(RefExpr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010410 continue;
10411 }
Alexey Bataev8b427062016-05-25 12:36:08 +000010412 SimpleExpr = SimpleExpr->IgnoreImplicit();
10413 OverloadedOperatorKind OOK = OO_None;
10414 SourceLocation OOLoc;
10415 Expr *LHS = SimpleExpr;
10416 Expr *RHS = nullptr;
10417 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
10418 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
10419 OOLoc = BO->getOperatorLoc();
10420 LHS = BO->getLHS()->IgnoreParenImpCasts();
10421 RHS = BO->getRHS()->IgnoreParenImpCasts();
10422 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
10423 OOK = OCE->getOperator();
10424 OOLoc = OCE->getOperatorLoc();
10425 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
10426 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
10427 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
10428 OOK = MCE->getMethodDecl()
10429 ->getNameInfo()
10430 .getName()
10431 .getCXXOverloadedOperator();
10432 OOLoc = MCE->getCallee()->getExprLoc();
10433 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
10434 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
10435 }
10436 SourceLocation ELoc;
10437 SourceRange ERange;
10438 auto Res = getPrivateItem(*this, LHS, ELoc, ERange,
10439 /*AllowArraySection=*/false);
10440 if (Res.second) {
10441 // It will be analyzed later.
10442 Vars.push_back(RefExpr);
10443 }
10444 ValueDecl *D = Res.first;
10445 if (!D)
10446 continue;
10447
10448 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
10449 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
10450 continue;
10451 }
10452 if (RHS) {
10453 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
10454 RHS, OMPC_depend, /*StrictlyPositive=*/false);
10455 if (RHSRes.isInvalid())
10456 continue;
10457 }
10458 if (!CurContext->isDependentContext() &&
10459 DSAStack->getParentOrderedRegionParam() &&
10460 DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
10461 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
10462 << DSAStack->getParentLoopControlVariable(
10463 DepCounter.getZExtValue());
10464 continue;
10465 }
10466 OpsOffs.push_back({RHS, OOK});
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010467 } else {
10468 // OpenMP [2.11.1.1, Restrictions, p.3]
10469 // A variable that is part of another variable (such as a field of a
10470 // structure) but is not an array element or an array section cannot
10471 // appear in a depend clause.
10472 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
10473 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
10474 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
10475 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
10476 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev31300ed2016-02-04 11:27:03 +000010477 (ASE &&
10478 !ASE->getBase()
10479 ->getType()
10480 .getNonReferenceType()
10481 ->isPointerType() &&
10482 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000010483 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
10484 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010485 continue;
10486 }
10487 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010488 Vars.push_back(RefExpr->IgnoreParenImpCasts());
10489 }
10490
10491 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
10492 TotalDepCount > VarList.size() &&
10493 DSAStack->getParentOrderedRegionParam()) {
10494 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
10495 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
10496 }
10497 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
10498 Vars.empty())
10499 return nullptr;
10500 }
Alexey Bataev8b427062016-05-25 12:36:08 +000010501 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10502 DepKind, DepLoc, ColonLoc, Vars);
10503 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source)
10504 DSAStack->addDoacrossDependClause(C, OpsOffs);
10505 return C;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010506}
Michael Wonge710d542015-08-07 16:16:36 +000010507
10508OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
10509 SourceLocation LParenLoc,
10510 SourceLocation EndLoc) {
10511 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +000010512
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010513 // OpenMP [2.9.1, Restrictions]
10514 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010515 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
10516 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010517 return nullptr;
10518
Michael Wonge710d542015-08-07 16:16:36 +000010519 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10520}
Kelvin Li0bff7af2015-11-23 05:32:03 +000010521
10522static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
10523 DSAStackTy *Stack, CXXRecordDecl *RD) {
10524 if (!RD || RD->isInvalidDecl())
10525 return true;
10526
Alexey Bataevc9bd03d2015-12-17 06:55:08 +000010527 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
10528 if (auto *CTD = CTSD->getSpecializedTemplate())
10529 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010530 auto QTy = SemaRef.Context.getRecordType(RD);
10531 if (RD->isDynamicClass()) {
10532 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10533 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
10534 return false;
10535 }
10536 auto *DC = RD;
10537 bool IsCorrect = true;
10538 for (auto *I : DC->decls()) {
10539 if (I) {
10540 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
10541 if (MD->isStatic()) {
10542 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10543 SemaRef.Diag(MD->getLocation(),
10544 diag::note_omp_static_member_in_target);
10545 IsCorrect = false;
10546 }
10547 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
10548 if (VD->isStaticDataMember()) {
10549 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10550 SemaRef.Diag(VD->getLocation(),
10551 diag::note_omp_static_member_in_target);
10552 IsCorrect = false;
10553 }
10554 }
10555 }
10556 }
10557
10558 for (auto &I : RD->bases()) {
10559 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
10560 I.getType()->getAsCXXRecordDecl()))
10561 IsCorrect = false;
10562 }
10563 return IsCorrect;
10564}
10565
10566static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
10567 DSAStackTy *Stack, QualType QTy) {
10568 NamedDecl *ND;
10569 if (QTy->isIncompleteType(&ND)) {
10570 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
10571 return false;
10572 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
10573 if (!RD->isInvalidDecl() &&
10574 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
10575 return false;
10576 }
10577 return true;
10578}
10579
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010580/// \brief Return true if it can be proven that the provided array expression
10581/// (array section or array subscript) does NOT specify the whole size of the
10582/// array whose base type is \a BaseQTy.
10583static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
10584 const Expr *E,
10585 QualType BaseQTy) {
10586 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
10587
10588 // If this is an array subscript, it refers to the whole size if the size of
10589 // the dimension is constant and equals 1. Also, an array section assumes the
10590 // format of an array subscript if no colon is used.
10591 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
10592 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
10593 return ATy->getSize().getSExtValue() != 1;
10594 // Size can't be evaluated statically.
10595 return false;
10596 }
10597
10598 assert(OASE && "Expecting array section if not an array subscript.");
10599 auto *LowerBound = OASE->getLowerBound();
10600 auto *Length = OASE->getLength();
10601
10602 // If there is a lower bound that does not evaluates to zero, we are not
10603 // convering the whole dimension.
10604 if (LowerBound) {
10605 llvm::APSInt ConstLowerBound;
10606 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
10607 return false; // Can't get the integer value as a constant.
10608 if (ConstLowerBound.getSExtValue())
10609 return true;
10610 }
10611
10612 // If we don't have a length we covering the whole dimension.
10613 if (!Length)
10614 return false;
10615
10616 // If the base is a pointer, we don't have a way to get the size of the
10617 // pointee.
10618 if (BaseQTy->isPointerType())
10619 return false;
10620
10621 // We can only check if the length is the same as the size of the dimension
10622 // if we have a constant array.
10623 auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
10624 if (!CATy)
10625 return false;
10626
10627 llvm::APSInt ConstLength;
10628 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
10629 return false; // Can't get the integer value as a constant.
10630
10631 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
10632}
10633
10634// Return true if it can be proven that the provided array expression (array
10635// section or array subscript) does NOT specify a single element of the array
10636// whose base type is \a BaseQTy.
10637static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
10638 const Expr *E,
10639 QualType BaseQTy) {
10640 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
10641
10642 // An array subscript always refer to a single element. Also, an array section
10643 // assumes the format of an array subscript if no colon is used.
10644 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
10645 return false;
10646
10647 assert(OASE && "Expecting array section if not an array subscript.");
10648 auto *Length = OASE->getLength();
10649
10650 // If we don't have a length we have to check if the array has unitary size
10651 // for this dimension. Also, we should always expect a length if the base type
10652 // is pointer.
10653 if (!Length) {
10654 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
10655 return ATy->getSize().getSExtValue() != 1;
10656 // We cannot assume anything.
10657 return false;
10658 }
10659
10660 // Check if the length evaluates to 1.
10661 llvm::APSInt ConstLength;
10662 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
10663 return false; // Can't get the integer value as a constant.
10664
10665 return ConstLength.getSExtValue() != 1;
10666}
10667
Samuel Antao661c0902016-05-26 17:39:58 +000010668// Return the expression of the base of the mappable expression or null if it
10669// cannot be determined and do all the necessary checks to see if the expression
10670// is valid as a standalone mappable expression. In the process, record all the
Samuel Antao90927002016-04-26 14:54:23 +000010671// components of the expression.
10672static Expr *CheckMapClauseExpressionBase(
10673 Sema &SemaRef, Expr *E,
Samuel Antao661c0902016-05-26 17:39:58 +000010674 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
10675 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010676 SourceLocation ELoc = E->getExprLoc();
10677 SourceRange ERange = E->getSourceRange();
10678
10679 // The base of elements of list in a map clause have to be either:
10680 // - a reference to variable or field.
10681 // - a member expression.
10682 // - an array expression.
10683 //
10684 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
10685 // reference to 'r'.
10686 //
10687 // If we have:
10688 //
10689 // struct SS {
10690 // Bla S;
10691 // foo() {
10692 // #pragma omp target map (S.Arr[:12]);
10693 // }
10694 // }
10695 //
10696 // We want to retrieve the member expression 'this->S';
10697
10698 Expr *RelevantExpr = nullptr;
10699
Samuel Antao5de996e2016-01-22 20:21:36 +000010700 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
10701 // If a list item is an array section, it must specify contiguous storage.
10702 //
10703 // For this restriction it is sufficient that we make sure only references
10704 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010705 // exist except in the rightmost expression (unless they cover the whole
10706 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +000010707 //
10708 // r.ArrS[3:5].Arr[6:7]
10709 //
10710 // r.ArrS[3:5].x
10711 //
10712 // but these would be valid:
10713 // r.ArrS[3].Arr[6:7]
10714 //
10715 // r.ArrS[3].x
10716
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010717 bool AllowUnitySizeArraySection = true;
10718 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +000010719
Dmitry Polukhin644a9252016-03-11 07:58:34 +000010720 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010721 E = E->IgnoreParenImpCasts();
10722
10723 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
10724 if (!isa<VarDecl>(CurE->getDecl()))
10725 break;
10726
10727 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010728
10729 // If we got a reference to a declaration, we should not expect any array
10730 // section before that.
10731 AllowUnitySizeArraySection = false;
10732 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010733
10734 // Record the component.
10735 CurComponents.push_back(OMPClauseMappableExprCommon::MappableComponent(
10736 CurE, CurE->getDecl()));
Samuel Antao5de996e2016-01-22 20:21:36 +000010737 continue;
10738 }
10739
10740 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
10741 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
10742
10743 if (isa<CXXThisExpr>(BaseE))
10744 // We found a base expression: this->Val.
10745 RelevantExpr = CurE;
10746 else
10747 E = BaseE;
10748
10749 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
10750 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
10751 << CurE->getSourceRange();
10752 break;
10753 }
10754
10755 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
10756
10757 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
10758 // A bit-field cannot appear in a map clause.
10759 //
10760 if (FD->isBitField()) {
Samuel Antao661c0902016-05-26 17:39:58 +000010761 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
10762 << CurE->getSourceRange() << getOpenMPClauseName(CKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010763 break;
10764 }
10765
10766 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10767 // If the type of a list item is a reference to a type T then the type
10768 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010769 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010770
10771 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
10772 // A list item cannot be a variable that is a member of a structure with
10773 // a union type.
10774 //
10775 if (auto *RT = CurType->getAs<RecordType>())
10776 if (RT->isUnionType()) {
10777 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
10778 << CurE->getSourceRange();
10779 break;
10780 }
10781
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010782 // If we got a member expression, we should not expect any array section
10783 // before that:
10784 //
10785 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
10786 // If a list item is an element of a structure, only the rightmost symbol
10787 // of the variable reference can be an array section.
10788 //
10789 AllowUnitySizeArraySection = false;
10790 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010791
10792 // Record the component.
10793 CurComponents.push_back(
10794 OMPClauseMappableExprCommon::MappableComponent(CurE, FD));
Samuel Antao5de996e2016-01-22 20:21:36 +000010795 continue;
10796 }
10797
10798 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
10799 E = CurE->getBase()->IgnoreParenImpCasts();
10800
10801 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
10802 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
10803 << 0 << CurE->getSourceRange();
10804 break;
10805 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010806
10807 // If we got an array subscript that express the whole dimension we
10808 // can have any array expressions before. If it only expressing part of
10809 // the dimension, we can only have unitary-size array expressions.
10810 if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
10811 E->getType()))
10812 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010813
10814 // Record the component - we don't have any declaration associated.
10815 CurComponents.push_back(
10816 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +000010817 continue;
10818 }
10819
10820 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010821 E = CurE->getBase()->IgnoreParenImpCasts();
10822
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010823 auto CurType =
10824 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
10825
Samuel Antao5de996e2016-01-22 20:21:36 +000010826 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10827 // If the type of a list item is a reference to a type T then the type
10828 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +000010829 if (CurType->isReferenceType())
10830 CurType = CurType->getPointeeType();
10831
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010832 bool IsPointer = CurType->isAnyPointerType();
10833
10834 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010835 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
10836 << 0 << CurE->getSourceRange();
10837 break;
10838 }
10839
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010840 bool NotWhole =
10841 CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
10842 bool NotUnity =
10843 CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
10844
Samuel Antaodab51bb2016-07-18 23:22:11 +000010845 if (AllowWholeSizeArraySection) {
10846 // Any array section is currently allowed. Allowing a whole size array
10847 // section implies allowing a unity array section as well.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010848 //
10849 // If this array section refers to the whole dimension we can still
10850 // accept other array sections before this one, except if the base is a
10851 // pointer. Otherwise, only unitary sections are accepted.
10852 if (NotWhole || IsPointer)
10853 AllowWholeSizeArraySection = false;
Samuel Antaodab51bb2016-07-18 23:22:11 +000010854 } else if (AllowUnitySizeArraySection && NotUnity) {
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010855 // A unity or whole array section is not allowed and that is not
10856 // compatible with the properties of the current array section.
10857 SemaRef.Diag(
10858 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
10859 << CurE->getSourceRange();
10860 break;
10861 }
Samuel Antao90927002016-04-26 14:54:23 +000010862
10863 // Record the component - we don't have any declaration associated.
10864 CurComponents.push_back(
10865 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +000010866 continue;
10867 }
10868
10869 // If nothing else worked, this is not a valid map clause expression.
10870 SemaRef.Diag(ELoc,
10871 diag::err_omp_expected_named_var_member_or_array_expression)
10872 << ERange;
10873 break;
10874 }
10875
10876 return RelevantExpr;
10877}
10878
10879// Return true if expression E associated with value VD has conflicts with other
10880// map information.
Samuel Antao90927002016-04-26 14:54:23 +000010881static bool CheckMapConflicts(
10882 Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD, Expr *E,
10883 bool CurrentRegionOnly,
Samuel Antao661c0902016-05-26 17:39:58 +000010884 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
10885 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010886 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +000010887 SourceLocation ELoc = E->getExprLoc();
10888 SourceRange ERange = E->getSourceRange();
10889
10890 // In order to easily check the conflicts we need to match each component of
10891 // the expression under test with the components of the expressions that are
10892 // already in the stack.
10893
Samuel Antao5de996e2016-01-22 20:21:36 +000010894 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000010895 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000010896 "Map clause expression with unexpected base!");
10897
10898 // Variables to help detecting enclosing problems in data environment nests.
10899 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +000010900 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000010901
Samuel Antao90927002016-04-26 14:54:23 +000010902 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
10903 VD, CurrentRegionOnly,
10904 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
Samuel Antao6890b092016-07-28 14:25:09 +000010905 StackComponents,
10906 OpenMPClauseKind) -> bool {
Samuel Antao90927002016-04-26 14:54:23 +000010907
Samuel Antao5de996e2016-01-22 20:21:36 +000010908 assert(!StackComponents.empty() &&
10909 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000010910 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000010911 "Map clause expression with unexpected base!");
10912
Samuel Antao90927002016-04-26 14:54:23 +000010913 // The whole expression in the stack.
10914 auto *RE = StackComponents.front().getAssociatedExpression();
10915
Samuel Antao5de996e2016-01-22 20:21:36 +000010916 // Expressions must start from the same base. Here we detect at which
10917 // point both expressions diverge from each other and see if we can
10918 // detect if the memory referred to both expressions is contiguous and
10919 // do not overlap.
10920 auto CI = CurComponents.rbegin();
10921 auto CE = CurComponents.rend();
10922 auto SI = StackComponents.rbegin();
10923 auto SE = StackComponents.rend();
10924 for (; CI != CE && SI != SE; ++CI, ++SI) {
10925
10926 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
10927 // At most one list item can be an array item derived from a given
10928 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +000010929 if (CurrentRegionOnly &&
10930 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
10931 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
10932 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
10933 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
10934 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +000010935 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +000010936 << CI->getAssociatedExpression()->getSourceRange();
10937 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
10938 diag::note_used_here)
10939 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +000010940 return true;
10941 }
10942
10943 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +000010944 if (CI->getAssociatedExpression()->getStmtClass() !=
10945 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +000010946 break;
10947
10948 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +000010949 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +000010950 break;
10951 }
Kelvin Li9f645ae2016-07-18 22:49:16 +000010952 // Check if the extra components of the expressions in the enclosing
10953 // data environment are redundant for the current base declaration.
10954 // If they are, the maps completely overlap, which is legal.
10955 for (; SI != SE; ++SI) {
10956 QualType Type;
10957 if (auto *ASE =
10958 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
10959 Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
10960 } else if (auto *OASE =
10961 dyn_cast<OMPArraySectionExpr>(SI->getAssociatedExpression())) {
10962 auto *E = OASE->getBase()->IgnoreParenImpCasts();
10963 Type =
10964 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
10965 }
10966 if (Type.isNull() || Type->isAnyPointerType() ||
10967 CheckArrayExpressionDoesNotReferToWholeSize(
10968 SemaRef, SI->getAssociatedExpression(), Type))
10969 break;
10970 }
Samuel Antao5de996e2016-01-22 20:21:36 +000010971
10972 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10973 // List items of map clauses in the same construct must not share
10974 // original storage.
10975 //
10976 // If the expressions are exactly the same or one is a subset of the
10977 // other, it means they are sharing storage.
10978 if (CI == CE && SI == SE) {
10979 if (CurrentRegionOnly) {
Samuel Antao661c0902016-05-26 17:39:58 +000010980 if (CKind == OMPC_map)
10981 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10982 else {
Samuel Antaoec172c62016-05-26 17:49:04 +000010983 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000010984 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
10985 << ERange;
10986 }
Samuel Antao5de996e2016-01-22 20:21:36 +000010987 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10988 << RE->getSourceRange();
10989 return true;
10990 } else {
10991 // If we find the same expression in the enclosing data environment,
10992 // that is legal.
10993 IsEnclosedByDataEnvironmentExpr = true;
10994 return false;
10995 }
10996 }
10997
Samuel Antao90927002016-04-26 14:54:23 +000010998 QualType DerivedType =
10999 std::prev(CI)->getAssociatedDeclaration()->getType();
11000 SourceLocation DerivedLoc =
11001 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000011002
11003 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
11004 // If the type of a list item is a reference to a type T then the type
11005 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000011006 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000011007
11008 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
11009 // A variable for which the type is pointer and an array section
11010 // derived from that variable must not appear as list items of map
11011 // clauses of the same construct.
11012 //
11013 // Also, cover one of the cases in:
11014 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
11015 // If any part of the original storage of a list item has corresponding
11016 // storage in the device data environment, all of the original storage
11017 // must have corresponding storage in the device data environment.
11018 //
11019 if (DerivedType->isAnyPointerType()) {
11020 if (CI == CE || SI == SE) {
11021 SemaRef.Diag(
11022 DerivedLoc,
11023 diag::err_omp_pointer_mapped_along_with_derived_section)
11024 << DerivedLoc;
11025 } else {
11026 assert(CI != CE && SI != SE);
11027 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
11028 << DerivedLoc;
11029 }
11030 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
11031 << RE->getSourceRange();
11032 return true;
11033 }
11034
11035 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
11036 // List items of map clauses in the same construct must not share
11037 // original storage.
11038 //
11039 // An expression is a subset of the other.
11040 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
Samuel Antao661c0902016-05-26 17:39:58 +000011041 if (CKind == OMPC_map)
11042 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
11043 else {
Samuel Antaoec172c62016-05-26 17:49:04 +000011044 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000011045 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
11046 << ERange;
11047 }
Samuel Antao5de996e2016-01-22 20:21:36 +000011048 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
11049 << RE->getSourceRange();
11050 return true;
11051 }
11052
11053 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000011054 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000011055 if (!CurrentRegionOnly && SI != SE)
11056 EnclosingExpr = RE;
11057
11058 // The current expression is a subset of the expression in the data
11059 // environment.
11060 IsEnclosedByDataEnvironmentExpr |=
11061 (!CurrentRegionOnly && CI != CE && SI == SE);
11062
11063 return false;
11064 });
11065
11066 if (CurrentRegionOnly)
11067 return FoundError;
11068
11069 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
11070 // If any part of the original storage of a list item has corresponding
11071 // storage in the device data environment, all of the original storage must
11072 // have corresponding storage in the device data environment.
11073 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
11074 // If a list item is an element of a structure, and a different element of
11075 // the structure has a corresponding list item in the device data environment
11076 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000011077 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000011078 // data environment prior to the task encountering the construct.
11079 //
11080 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
11081 SemaRef.Diag(ELoc,
11082 diag::err_omp_original_storage_is_shared_and_does_not_contain)
11083 << ERange;
11084 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
11085 << EnclosingExpr->getSourceRange();
11086 return true;
11087 }
11088
11089 return FoundError;
11090}
11091
Samuel Antao661c0902016-05-26 17:39:58 +000011092namespace {
11093// Utility struct that gathers all the related lists associated with a mappable
11094// expression.
11095struct MappableVarListInfo final {
11096 // The list of expressions.
11097 ArrayRef<Expr *> VarList;
11098 // The list of processed expressions.
11099 SmallVector<Expr *, 16> ProcessedVarList;
11100 // The mappble components for each expression.
11101 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
11102 // The base declaration of the variable.
11103 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
11104
11105 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
11106 // We have a list of components and base declarations for each entry in the
11107 // variable list.
11108 VarComponents.reserve(VarList.size());
11109 VarBaseDeclarations.reserve(VarList.size());
11110 }
11111};
11112}
11113
11114// Check the validity of the provided variable list for the provided clause kind
11115// \a CKind. In the check process the valid expressions, and mappable expression
11116// components and variables are extracted and used to fill \a Vars,
11117// \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and
11118// \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'.
11119static void
11120checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS,
11121 OpenMPClauseKind CKind, MappableVarListInfo &MVLI,
11122 SourceLocation StartLoc,
11123 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
11124 bool IsMapTypeImplicit = false) {
Samuel Antaoec172c62016-05-26 17:49:04 +000011125 // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
11126 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
Samuel Antao661c0902016-05-26 17:39:58 +000011127 "Unexpected clause kind with mappable expressions!");
Kelvin Li0bff7af2015-11-23 05:32:03 +000011128
Samuel Antao90927002016-04-26 14:54:23 +000011129 // Keep track of the mappable components and base declarations in this clause.
11130 // Each entry in the list is going to have a list of components associated. We
11131 // record each set of the components so that we can build the clause later on.
11132 // In the end we should have the same amount of declarations and component
11133 // lists.
Samuel Antao90927002016-04-26 14:54:23 +000011134
Samuel Antao661c0902016-05-26 17:39:58 +000011135 for (auto &RE : MVLI.VarList) {
Samuel Antaoec172c62016-05-26 17:49:04 +000011136 assert(RE && "Null expr in omp to/from/map clause");
Kelvin Li0bff7af2015-11-23 05:32:03 +000011137 SourceLocation ELoc = RE->getExprLoc();
11138
Kelvin Li0bff7af2015-11-23 05:32:03 +000011139 auto *VE = RE->IgnoreParenLValueCasts();
11140
11141 if (VE->isValueDependent() || VE->isTypeDependent() ||
11142 VE->isInstantiationDependent() ||
11143 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000011144 // We can only analyze this information once the missing information is
11145 // resolved.
Samuel Antao661c0902016-05-26 17:39:58 +000011146 MVLI.ProcessedVarList.push_back(RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011147 continue;
11148 }
11149
11150 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000011151
Samuel Antao5de996e2016-01-22 20:21:36 +000011152 if (!RE->IgnoreParenImpCasts()->isLValue()) {
Samuel Antao661c0902016-05-26 17:39:58 +000011153 SemaRef.Diag(ELoc,
11154 diag::err_omp_expected_named_var_member_or_array_expression)
Samuel Antao5de996e2016-01-22 20:21:36 +000011155 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000011156 continue;
11157 }
11158
Samuel Antao90927002016-04-26 14:54:23 +000011159 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
11160 ValueDecl *CurDeclaration = nullptr;
11161
11162 // Obtain the array or member expression bases if required. Also, fill the
11163 // components array with all the components identified in the process.
Samuel Antao661c0902016-05-26 17:39:58 +000011164 auto *BE =
11165 CheckMapClauseExpressionBase(SemaRef, SimpleExpr, CurComponents, CKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000011166 if (!BE)
11167 continue;
11168
Samuel Antao90927002016-04-26 14:54:23 +000011169 assert(!CurComponents.empty() &&
11170 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000011171
Samuel Antao90927002016-04-26 14:54:23 +000011172 // For the following checks, we rely on the base declaration which is
11173 // expected to be associated with the last component. The declaration is
11174 // expected to be a variable or a field (if 'this' is being mapped).
11175 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
11176 assert(CurDeclaration && "Null decl on map clause.");
11177 assert(
11178 CurDeclaration->isCanonicalDecl() &&
11179 "Expecting components to have associated only canonical declarations.");
11180
11181 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
11182 auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000011183
11184 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000011185 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000011186
11187 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
Samuel Antao661c0902016-05-26 17:39:58 +000011188 // threadprivate variables cannot appear in a map clause.
11189 // OpenMP 4.5 [2.10.5, target update Construct]
11190 // threadprivate variables cannot appear in a from clause.
11191 if (VD && DSAS->isThreadPrivate(VD)) {
11192 auto DVar = DSAS->getTopDSA(VD, false);
11193 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
11194 << getOpenMPClauseName(CKind);
11195 ReportOriginalDSA(SemaRef, DSAS, VD, DVar);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011196 continue;
11197 }
11198
Samuel Antao5de996e2016-01-22 20:21:36 +000011199 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
11200 // A list item cannot appear in both a map clause and a data-sharing
11201 // attribute clause on the same construct.
Kelvin Li0bff7af2015-11-23 05:32:03 +000011202
Samuel Antao5de996e2016-01-22 20:21:36 +000011203 // Check conflicts with other map clause expressions. We check the conflicts
11204 // with the current construct separately from the enclosing data
Samuel Antao661c0902016-05-26 17:39:58 +000011205 // environment, because the restrictions are different. We only have to
11206 // check conflicts across regions for the map clauses.
11207 if (CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
11208 /*CurrentRegionOnly=*/true, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000011209 break;
Samuel Antao661c0902016-05-26 17:39:58 +000011210 if (CKind == OMPC_map &&
11211 CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
11212 /*CurrentRegionOnly=*/false, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000011213 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000011214
Samuel Antao661c0902016-05-26 17:39:58 +000011215 // OpenMP 4.5 [2.10.5, target update Construct]
Samuel Antao5de996e2016-01-22 20:21:36 +000011216 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
11217 // If the type of a list item is a reference to a type T then the type will
11218 // be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000011219 QualType Type = CurDeclaration->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000011220
Samuel Antao661c0902016-05-26 17:39:58 +000011221 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
11222 // A list item in a to or from clause must have a mappable type.
Samuel Antao5de996e2016-01-22 20:21:36 +000011223 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000011224 // A list item must have a mappable type.
Samuel Antao661c0902016-05-26 17:39:58 +000011225 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
11226 DSAS, Type))
Kelvin Li0bff7af2015-11-23 05:32:03 +000011227 continue;
11228
Samuel Antao661c0902016-05-26 17:39:58 +000011229 if (CKind == OMPC_map) {
11230 // target enter data
11231 // OpenMP [2.10.2, Restrictions, p. 99]
11232 // A map-type must be specified in all map clauses and must be either
11233 // to or alloc.
11234 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
11235 if (DKind == OMPD_target_enter_data &&
11236 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
11237 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
11238 << (IsMapTypeImplicit ? 1 : 0)
11239 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
11240 << getOpenMPDirectiveName(DKind);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000011241 continue;
11242 }
Samuel Antao661c0902016-05-26 17:39:58 +000011243
11244 // target exit_data
11245 // OpenMP [2.10.3, Restrictions, p. 102]
11246 // A map-type must be specified in all map clauses and must be either
11247 // from, release, or delete.
11248 if (DKind == OMPD_target_exit_data &&
11249 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
11250 MapType == OMPC_MAP_delete)) {
11251 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
11252 << (IsMapTypeImplicit ? 1 : 0)
11253 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
11254 << getOpenMPDirectiveName(DKind);
11255 continue;
11256 }
11257
11258 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
11259 // A list item cannot appear in both a map clause and a data-sharing
11260 // attribute clause on the same construct
11261 if (DKind == OMPD_target && VD) {
11262 auto DVar = DSAS->getTopDSA(VD, false);
11263 if (isOpenMPPrivate(DVar.CKind)) {
Samuel Antao6890b092016-07-28 14:25:09 +000011264 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Samuel Antao661c0902016-05-26 17:39:58 +000011265 << getOpenMPClauseName(DVar.CKind)
Samuel Antao6890b092016-07-28 14:25:09 +000011266 << getOpenMPClauseName(OMPC_map)
Samuel Antao661c0902016-05-26 17:39:58 +000011267 << getOpenMPDirectiveName(DSAS->getCurrentDirective());
11268 ReportOriginalDSA(SemaRef, DSAS, CurDeclaration, DVar);
11269 continue;
11270 }
11271 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +000011272 }
11273
Samuel Antao90927002016-04-26 14:54:23 +000011274 // Save the current expression.
Samuel Antao661c0902016-05-26 17:39:58 +000011275 MVLI.ProcessedVarList.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000011276
11277 // Store the components in the stack so that they can be used to check
11278 // against other clauses later on.
Samuel Antao6890b092016-07-28 14:25:09 +000011279 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
11280 /*WhereFoundClauseKind=*/OMPC_map);
Samuel Antao90927002016-04-26 14:54:23 +000011281
11282 // Save the components and declaration to create the clause. For purposes of
11283 // the clause creation, any component list that has has base 'this' uses
Samuel Antao686c70c2016-05-26 17:30:50 +000011284 // null as base declaration.
Samuel Antao661c0902016-05-26 17:39:58 +000011285 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
11286 MVLI.VarComponents.back().append(CurComponents.begin(),
11287 CurComponents.end());
11288 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
11289 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011290 }
Samuel Antao661c0902016-05-26 17:39:58 +000011291}
11292
11293OMPClause *
11294Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
11295 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
11296 SourceLocation MapLoc, SourceLocation ColonLoc,
11297 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
11298 SourceLocation LParenLoc, SourceLocation EndLoc) {
11299 MappableVarListInfo MVLI(VarList);
11300 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc,
11301 MapType, IsMapTypeImplicit);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011302
Samuel Antao5de996e2016-01-22 20:21:36 +000011303 // We need to produce a map clause even if we don't have variables so that
11304 // other diagnostics related with non-existing map clauses are accurate.
Samuel Antao661c0902016-05-26 17:39:58 +000011305 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11306 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11307 MVLI.VarComponents, MapTypeModifier, MapType,
11308 IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011309}
Kelvin Li099bb8c2015-11-24 20:50:12 +000011310
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011311QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
11312 TypeResult ParsedType) {
11313 assert(ParsedType.isUsable());
11314
11315 QualType ReductionType = GetTypeFromParser(ParsedType.get());
11316 if (ReductionType.isNull())
11317 return QualType();
11318
11319 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
11320 // A type name in a declare reduction directive cannot be a function type, an
11321 // array type, a reference type, or a type qualified with const, volatile or
11322 // restrict.
11323 if (ReductionType.hasQualifiers()) {
11324 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
11325 return QualType();
11326 }
11327
11328 if (ReductionType->isFunctionType()) {
11329 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
11330 return QualType();
11331 }
11332 if (ReductionType->isReferenceType()) {
11333 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
11334 return QualType();
11335 }
11336 if (ReductionType->isArrayType()) {
11337 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
11338 return QualType();
11339 }
11340 return ReductionType;
11341}
11342
11343Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
11344 Scope *S, DeclContext *DC, DeclarationName Name,
11345 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
11346 AccessSpecifier AS, Decl *PrevDeclInScope) {
11347 SmallVector<Decl *, 8> Decls;
11348 Decls.reserve(ReductionTypes.size());
11349
11350 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
11351 ForRedeclaration);
11352 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
11353 // A reduction-identifier may not be re-declared in the current scope for the
11354 // same type or for a type that is compatible according to the base language
11355 // rules.
11356 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
11357 OMPDeclareReductionDecl *PrevDRD = nullptr;
11358 bool InCompoundScope = true;
11359 if (S != nullptr) {
11360 // Find previous declaration with the same name not referenced in other
11361 // declarations.
11362 FunctionScopeInfo *ParentFn = getEnclosingFunction();
11363 InCompoundScope =
11364 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
11365 LookupName(Lookup, S);
11366 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
11367 /*AllowInlineNamespace=*/false);
11368 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
11369 auto Filter = Lookup.makeFilter();
11370 while (Filter.hasNext()) {
11371 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
11372 if (InCompoundScope) {
11373 auto I = UsedAsPrevious.find(PrevDecl);
11374 if (I == UsedAsPrevious.end())
11375 UsedAsPrevious[PrevDecl] = false;
11376 if (auto *D = PrevDecl->getPrevDeclInScope())
11377 UsedAsPrevious[D] = true;
11378 }
11379 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
11380 PrevDecl->getLocation();
11381 }
11382 Filter.done();
11383 if (InCompoundScope) {
11384 for (auto &PrevData : UsedAsPrevious) {
11385 if (!PrevData.second) {
11386 PrevDRD = PrevData.first;
11387 break;
11388 }
11389 }
11390 }
11391 } else if (PrevDeclInScope != nullptr) {
11392 auto *PrevDRDInScope = PrevDRD =
11393 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
11394 do {
11395 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
11396 PrevDRDInScope->getLocation();
11397 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
11398 } while (PrevDRDInScope != nullptr);
11399 }
11400 for (auto &TyData : ReductionTypes) {
11401 auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
11402 bool Invalid = false;
11403 if (I != PreviousRedeclTypes.end()) {
11404 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
11405 << TyData.first;
11406 Diag(I->second, diag::note_previous_definition);
11407 Invalid = true;
11408 }
11409 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
11410 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
11411 Name, TyData.first, PrevDRD);
11412 DC->addDecl(DRD);
11413 DRD->setAccess(AS);
11414 Decls.push_back(DRD);
11415 if (Invalid)
11416 DRD->setInvalidDecl();
11417 else
11418 PrevDRD = DRD;
11419 }
11420
11421 return DeclGroupPtrTy::make(
11422 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
11423}
11424
11425void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
11426 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11427
11428 // Enter new function scope.
11429 PushFunctionScope();
11430 getCurFunction()->setHasBranchProtectedScope();
11431 getCurFunction()->setHasOMPDeclareReductionCombiner();
11432
11433 if (S != nullptr)
11434 PushDeclContext(S, DRD);
11435 else
11436 CurContext = DRD;
11437
11438 PushExpressionEvaluationContext(PotentiallyEvaluated);
11439
11440 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011441 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
11442 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
11443 // uses semantics of argument handles by value, but it should be passed by
11444 // reference. C lang does not support references, so pass all parameters as
11445 // pointers.
11446 // Create 'T omp_in;' variable.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011447 auto *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011448 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011449 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
11450 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
11451 // uses semantics of argument handles by value, but it should be passed by
11452 // reference. C lang does not support references, so pass all parameters as
11453 // pointers.
11454 // Create 'T omp_out;' variable.
11455 auto *OmpOutParm =
11456 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
11457 if (S != nullptr) {
11458 PushOnScopeChains(OmpInParm, S);
11459 PushOnScopeChains(OmpOutParm, S);
11460 } else {
11461 DRD->addDecl(OmpInParm);
11462 DRD->addDecl(OmpOutParm);
11463 }
11464}
11465
11466void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
11467 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11468 DiscardCleanupsInEvaluationContext();
11469 PopExpressionEvaluationContext();
11470
11471 PopDeclContext();
11472 PopFunctionScopeInfo();
11473
11474 if (Combiner != nullptr)
11475 DRD->setCombiner(Combiner);
11476 else
11477 DRD->setInvalidDecl();
11478}
11479
11480void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
11481 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11482
11483 // Enter new function scope.
11484 PushFunctionScope();
11485 getCurFunction()->setHasBranchProtectedScope();
11486
11487 if (S != nullptr)
11488 PushDeclContext(S, DRD);
11489 else
11490 CurContext = DRD;
11491
11492 PushExpressionEvaluationContext(PotentiallyEvaluated);
11493
11494 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011495 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
11496 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
11497 // uses semantics of argument handles by value, but it should be passed by
11498 // reference. C lang does not support references, so pass all parameters as
11499 // pointers.
11500 // Create 'T omp_priv;' variable.
11501 auto *OmpPrivParm =
11502 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011503 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
11504 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
11505 // uses semantics of argument handles by value, but it should be passed by
11506 // reference. C lang does not support references, so pass all parameters as
11507 // pointers.
11508 // Create 'T omp_orig;' variable.
11509 auto *OmpOrigParm =
11510 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011511 if (S != nullptr) {
11512 PushOnScopeChains(OmpPrivParm, S);
11513 PushOnScopeChains(OmpOrigParm, S);
11514 } else {
11515 DRD->addDecl(OmpPrivParm);
11516 DRD->addDecl(OmpOrigParm);
11517 }
11518}
11519
11520void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D,
11521 Expr *Initializer) {
11522 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11523 DiscardCleanupsInEvaluationContext();
11524 PopExpressionEvaluationContext();
11525
11526 PopDeclContext();
11527 PopFunctionScopeInfo();
11528
11529 if (Initializer != nullptr)
11530 DRD->setInitializer(Initializer);
11531 else
11532 DRD->setInvalidDecl();
11533}
11534
11535Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
11536 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
11537 for (auto *D : DeclReductions.get()) {
11538 if (IsValid) {
11539 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11540 if (S != nullptr)
11541 PushOnScopeChains(DRD, S, /*AddToContext=*/false);
11542 } else
11543 D->setInvalidDecl();
11544 }
11545 return DeclReductions;
11546}
11547
Kelvin Li099bb8c2015-11-24 20:50:12 +000011548OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
11549 SourceLocation StartLoc,
11550 SourceLocation LParenLoc,
11551 SourceLocation EndLoc) {
11552 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +000011553
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011554 // OpenMP [teams Constrcut, Restrictions]
11555 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000011556 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
11557 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011558 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000011559
11560 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11561}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011562
11563OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
11564 SourceLocation StartLoc,
11565 SourceLocation LParenLoc,
11566 SourceLocation EndLoc) {
11567 Expr *ValExpr = ThreadLimit;
11568
11569 // OpenMP [teams Constrcut, Restrictions]
11570 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000011571 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
11572 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011573 return nullptr;
11574
11575 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
11576 EndLoc);
11577}
Alexey Bataeva0569352015-12-01 10:17:31 +000011578
11579OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
11580 SourceLocation StartLoc,
11581 SourceLocation LParenLoc,
11582 SourceLocation EndLoc) {
11583 Expr *ValExpr = Priority;
11584
11585 // OpenMP [2.9.1, task Constrcut]
11586 // The priority-value is a non-negative numerical scalar expression.
11587 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
11588 /*StrictlyPositive=*/false))
11589 return nullptr;
11590
11591 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11592}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000011593
11594OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
11595 SourceLocation StartLoc,
11596 SourceLocation LParenLoc,
11597 SourceLocation EndLoc) {
11598 Expr *ValExpr = Grainsize;
11599
11600 // OpenMP [2.9.2, taskloop Constrcut]
11601 // The parameter of the grainsize clause must be a positive integer
11602 // expression.
11603 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
11604 /*StrictlyPositive=*/true))
11605 return nullptr;
11606
11607 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11608}
Alexey Bataev382967a2015-12-08 12:06:20 +000011609
11610OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
11611 SourceLocation StartLoc,
11612 SourceLocation LParenLoc,
11613 SourceLocation EndLoc) {
11614 Expr *ValExpr = NumTasks;
11615
11616 // OpenMP [2.9.2, taskloop Constrcut]
11617 // The parameter of the num_tasks clause must be a positive integer
11618 // expression.
11619 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
11620 /*StrictlyPositive=*/true))
11621 return nullptr;
11622
11623 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11624}
11625
Alexey Bataev28c75412015-12-15 08:19:24 +000011626OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
11627 SourceLocation LParenLoc,
11628 SourceLocation EndLoc) {
11629 // OpenMP [2.13.2, critical construct, Description]
11630 // ... where hint-expression is an integer constant expression that evaluates
11631 // to a valid lock hint.
11632 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
11633 if (HintExpr.isInvalid())
11634 return nullptr;
11635 return new (Context)
11636 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
11637}
11638
Carlo Bertollib4adf552016-01-15 18:50:31 +000011639OMPClause *Sema::ActOnOpenMPDistScheduleClause(
11640 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
11641 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
11642 SourceLocation EndLoc) {
11643 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
11644 std::string Values;
11645 Values += "'";
11646 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
11647 Values += "'";
11648 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
11649 << Values << getOpenMPClauseName(OMPC_dist_schedule);
11650 return nullptr;
11651 }
11652 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000011653 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000011654 if (ChunkSize) {
11655 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
11656 !ChunkSize->isInstantiationDependent() &&
11657 !ChunkSize->containsUnexpandedParameterPack()) {
11658 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
11659 ExprResult Val =
11660 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
11661 if (Val.isInvalid())
11662 return nullptr;
11663
11664 ValExpr = Val.get();
11665
11666 // OpenMP [2.7.1, Restrictions]
11667 // chunk_size must be a loop invariant integer expression with a positive
11668 // value.
11669 llvm::APSInt Result;
11670 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
11671 if (Result.isSigned() && !Result.isStrictlyPositive()) {
11672 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
11673 << "dist_schedule" << ChunkSize->getSourceRange();
11674 return nullptr;
11675 }
Alexey Bataevb46cdea2016-06-15 11:20:48 +000011676 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
11677 !CurContext->isDependentContext()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +000011678 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
11679 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
11680 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000011681 }
11682 }
11683 }
11684
11685 return new (Context)
11686 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000011687 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000011688}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000011689
11690OMPClause *Sema::ActOnOpenMPDefaultmapClause(
11691 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
11692 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
11693 SourceLocation KindLoc, SourceLocation EndLoc) {
11694 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
11695 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
11696 Kind != OMPC_DEFAULTMAP_scalar) {
11697 std::string Value;
11698 SourceLocation Loc;
11699 Value += "'";
11700 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
11701 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
11702 OMPC_DEFAULTMAP_MODIFIER_tofrom);
11703 Loc = MLoc;
11704 } else {
11705 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
11706 OMPC_DEFAULTMAP_scalar);
11707 Loc = KindLoc;
11708 }
11709 Value += "'";
11710 Diag(Loc, diag::err_omp_unexpected_clause_value)
11711 << Value << getOpenMPClauseName(OMPC_defaultmap);
11712 return nullptr;
11713 }
11714
11715 return new (Context)
11716 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
11717}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011718
11719bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
11720 DeclContext *CurLexicalContext = getCurLexicalContext();
11721 if (!CurLexicalContext->isFileContext() &&
11722 !CurLexicalContext->isExternCContext() &&
11723 !CurLexicalContext->isExternCXXContext()) {
11724 Diag(Loc, diag::err_omp_region_not_file_context);
11725 return false;
11726 }
11727 if (IsInOpenMPDeclareTargetContext) {
11728 Diag(Loc, diag::err_omp_enclosed_declare_target);
11729 return false;
11730 }
11731
11732 IsInOpenMPDeclareTargetContext = true;
11733 return true;
11734}
11735
11736void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
11737 assert(IsInOpenMPDeclareTargetContext &&
11738 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
11739
11740 IsInOpenMPDeclareTargetContext = false;
11741}
11742
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011743void
11744Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope, CXXScopeSpec &ScopeSpec,
11745 const DeclarationNameInfo &Id,
11746 OMPDeclareTargetDeclAttr::MapTypeTy MT,
11747 NamedDeclSetType &SameDirectiveDecls) {
11748 LookupResult Lookup(*this, Id, LookupOrdinaryName);
11749 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
11750
11751 if (Lookup.isAmbiguous())
11752 return;
11753 Lookup.suppressDiagnostics();
11754
11755 if (!Lookup.isSingleResult()) {
11756 if (TypoCorrection Corrected =
11757 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
11758 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
11759 CTK_ErrorRecovery)) {
11760 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
11761 << Id.getName());
11762 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
11763 return;
11764 }
11765
11766 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
11767 return;
11768 }
11769
11770 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
11771 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
11772 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
11773 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
11774
11775 if (!ND->hasAttr<OMPDeclareTargetDeclAttr>()) {
11776 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
11777 ND->addAttr(A);
11778 if (ASTMutationListener *ML = Context.getASTMutationListener())
11779 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
11780 checkDeclIsAllowedInOpenMPTarget(nullptr, ND);
11781 } else if (ND->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() != MT) {
11782 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
11783 << Id.getName();
11784 }
11785 } else
11786 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
11787}
11788
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011789static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
11790 Sema &SemaRef, Decl *D) {
11791 if (!D)
11792 return;
11793 Decl *LD = nullptr;
11794 if (isa<TagDecl>(D)) {
11795 LD = cast<TagDecl>(D)->getDefinition();
11796 } else if (isa<VarDecl>(D)) {
11797 LD = cast<VarDecl>(D)->getDefinition();
11798
11799 // If this is an implicit variable that is legal and we do not need to do
11800 // anything.
11801 if (cast<VarDecl>(D)->isImplicit()) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011802 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11803 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11804 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011805 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011806 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011807 return;
11808 }
11809
11810 } else if (isa<FunctionDecl>(D)) {
11811 const FunctionDecl *FD = nullptr;
11812 if (cast<FunctionDecl>(D)->hasBody(FD))
11813 LD = const_cast<FunctionDecl *>(FD);
11814
11815 // If the definition is associated with the current declaration in the
11816 // target region (it can be e.g. a lambda) that is legal and we do not need
11817 // to do anything else.
11818 if (LD == D) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011819 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11820 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11821 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011822 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011823 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011824 return;
11825 }
11826 }
11827 if (!LD)
11828 LD = D;
11829 if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() &&
11830 (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) {
11831 // Outlined declaration is not declared target.
11832 if (LD->isOutOfLine()) {
11833 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
11834 SemaRef.Diag(SL, diag::note_used_here) << SR;
11835 } else {
11836 DeclContext *DC = LD->getDeclContext();
11837 while (DC) {
11838 if (isa<FunctionDecl>(DC) &&
11839 cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>())
11840 break;
11841 DC = DC->getParent();
11842 }
11843 if (DC)
11844 return;
11845
11846 // Is not declared in target context.
11847 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
11848 SemaRef.Diag(SL, diag::note_used_here) << SR;
11849 }
11850 // Mark decl as declared target to prevent further diagnostic.
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011851 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11852 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11853 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011854 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011855 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011856 }
11857}
11858
11859static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
11860 Sema &SemaRef, DSAStackTy *Stack,
11861 ValueDecl *VD) {
11862 if (VD->hasAttr<OMPDeclareTargetDeclAttr>())
11863 return true;
11864 if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType()))
11865 return false;
11866 return true;
11867}
11868
11869void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D) {
11870 if (!D || D->isInvalidDecl())
11871 return;
11872 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
11873 SourceLocation SL = E ? E->getLocStart() : D->getLocation();
11874 // 2.10.6: threadprivate variable cannot appear in a declare target directive.
11875 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
11876 if (DSAStack->isThreadPrivate(VD)) {
11877 Diag(SL, diag::err_omp_threadprivate_in_target);
11878 ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
11879 return;
11880 }
11881 }
11882 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
11883 // Problem if any with var declared with incomplete type will be reported
11884 // as normal, so no need to check it here.
11885 if ((E || !VD->getType()->isIncompleteType()) &&
11886 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) {
11887 // Mark decl as declared target to prevent further diagnostic.
11888 if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011889 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11890 Context, OMPDeclareTargetDeclAttr::MT_To);
11891 VD->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011892 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011893 ML->DeclarationMarkedOpenMPDeclareTarget(VD, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011894 }
11895 return;
11896 }
11897 }
11898 if (!E) {
11899 // Checking declaration inside declare target region.
11900 if (!D->hasAttr<OMPDeclareTargetDeclAttr>() &&
11901 (isa<VarDecl>(D) || isa<FunctionDecl>(D))) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011902 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11903 Context, OMPDeclareTargetDeclAttr::MT_To);
11904 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011905 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011906 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011907 }
11908 return;
11909 }
11910 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
11911}
Samuel Antao661c0902016-05-26 17:39:58 +000011912
11913OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
11914 SourceLocation StartLoc,
11915 SourceLocation LParenLoc,
11916 SourceLocation EndLoc) {
11917 MappableVarListInfo MVLI(VarList);
11918 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc);
11919 if (MVLI.ProcessedVarList.empty())
11920 return nullptr;
11921
11922 return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11923 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11924 MVLI.VarComponents);
11925}
Samuel Antaoec172c62016-05-26 17:49:04 +000011926
11927OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
11928 SourceLocation StartLoc,
11929 SourceLocation LParenLoc,
11930 SourceLocation EndLoc) {
11931 MappableVarListInfo MVLI(VarList);
11932 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc);
11933 if (MVLI.ProcessedVarList.empty())
11934 return nullptr;
11935
11936 return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11937 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11938 MVLI.VarComponents);
11939}
Carlo Bertolli2404b172016-07-13 15:37:16 +000011940
11941OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
11942 SourceLocation StartLoc,
11943 SourceLocation LParenLoc,
11944 SourceLocation EndLoc) {
Samuel Antaocc10b852016-07-28 14:23:26 +000011945 MappableVarListInfo MVLI(VarList);
11946 SmallVector<Expr *, 8> PrivateCopies;
11947 SmallVector<Expr *, 8> Inits;
11948
Carlo Bertolli2404b172016-07-13 15:37:16 +000011949 for (auto &RefExpr : VarList) {
11950 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
11951 SourceLocation ELoc;
11952 SourceRange ERange;
11953 Expr *SimpleRefExpr = RefExpr;
11954 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
11955 if (Res.second) {
11956 // It will be analyzed later.
Samuel Antaocc10b852016-07-28 14:23:26 +000011957 MVLI.ProcessedVarList.push_back(RefExpr);
11958 PrivateCopies.push_back(nullptr);
11959 Inits.push_back(nullptr);
Carlo Bertolli2404b172016-07-13 15:37:16 +000011960 }
11961 ValueDecl *D = Res.first;
11962 if (!D)
11963 continue;
11964
11965 QualType Type = D->getType();
Samuel Antaocc10b852016-07-28 14:23:26 +000011966 Type = Type.getNonReferenceType().getUnqualifiedType();
11967
11968 auto *VD = dyn_cast<VarDecl>(D);
11969
11970 // Item should be a pointer or reference to pointer.
11971 if (!Type->isPointerType()) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000011972 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
11973 << 0 << RefExpr->getSourceRange();
11974 continue;
11975 }
Samuel Antaocc10b852016-07-28 14:23:26 +000011976
11977 // Build the private variable and the expression that refers to it.
11978 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
11979 D->hasAttrs() ? &D->getAttrs() : nullptr);
11980 if (VDPrivate->isInvalidDecl())
11981 continue;
11982
11983 CurContext->addDecl(VDPrivate);
11984 auto VDPrivateRefExpr = buildDeclRefExpr(
11985 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
11986
11987 // Add temporary variable to initialize the private copy of the pointer.
11988 auto *VDInit =
11989 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
11990 auto *VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
11991 RefExpr->getExprLoc());
11992 AddInitializerToDecl(VDPrivate,
11993 DefaultLvalueConversion(VDInitRefExpr).get(),
11994 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
11995
11996 // If required, build a capture to implement the privatization initialized
11997 // with the current list item value.
11998 DeclRefExpr *Ref = nullptr;
11999 if (!VD)
12000 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
12001 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
12002 PrivateCopies.push_back(VDPrivateRefExpr);
12003 Inits.push_back(VDInitRefExpr);
12004
12005 // We need to add a data sharing attribute for this variable to make sure it
12006 // is correctly captured. A variable that shows up in a use_device_ptr has
12007 // similar properties of a first private variable.
12008 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
12009
12010 // Create a mappable component for the list item. List items in this clause
12011 // only need a component.
12012 MVLI.VarBaseDeclarations.push_back(D);
12013 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
12014 MVLI.VarComponents.back().push_back(
12015 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D));
Carlo Bertolli2404b172016-07-13 15:37:16 +000012016 }
12017
Samuel Antaocc10b852016-07-28 14:23:26 +000012018 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli2404b172016-07-13 15:37:16 +000012019 return nullptr;
12020
Samuel Antaocc10b852016-07-28 14:23:26 +000012021 return OMPUseDevicePtrClause::Create(
12022 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
12023 PrivateCopies, Inits, MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli2404b172016-07-13 15:37:16 +000012024}
Carlo Bertolli70594e92016-07-13 17:16:49 +000012025
12026OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
12027 SourceLocation StartLoc,
12028 SourceLocation LParenLoc,
12029 SourceLocation EndLoc) {
Samuel Antao6890b092016-07-28 14:25:09 +000012030 MappableVarListInfo MVLI(VarList);
Carlo Bertolli70594e92016-07-13 17:16:49 +000012031 for (auto &RefExpr : VarList) {
12032 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
12033 SourceLocation ELoc;
12034 SourceRange ERange;
12035 Expr *SimpleRefExpr = RefExpr;
12036 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
12037 if (Res.second) {
12038 // It will be analyzed later.
Samuel Antao6890b092016-07-28 14:25:09 +000012039 MVLI.ProcessedVarList.push_back(RefExpr);
Carlo Bertolli70594e92016-07-13 17:16:49 +000012040 }
12041 ValueDecl *D = Res.first;
12042 if (!D)
12043 continue;
12044
12045 QualType Type = D->getType();
12046 // item should be a pointer or array or reference to pointer or array
12047 if (!Type.getNonReferenceType()->isPointerType() &&
12048 !Type.getNonReferenceType()->isArrayType()) {
12049 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
12050 << 0 << RefExpr->getSourceRange();
12051 continue;
12052 }
Samuel Antao6890b092016-07-28 14:25:09 +000012053
12054 // Check if the declaration in the clause does not show up in any data
12055 // sharing attribute.
12056 auto DVar = DSAStack->getTopDSA(D, false);
12057 if (isOpenMPPrivate(DVar.CKind)) {
12058 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
12059 << getOpenMPClauseName(DVar.CKind)
12060 << getOpenMPClauseName(OMPC_is_device_ptr)
12061 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
12062 ReportOriginalDSA(*this, DSAStack, D, DVar);
12063 continue;
12064 }
12065
12066 Expr *ConflictExpr;
12067 if (DSAStack->checkMappableExprComponentListsForDecl(
12068 D, /* CurrentRegionOnly = */ true,
12069 [&ConflictExpr](
12070 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
12071 OpenMPClauseKind) -> bool {
12072 ConflictExpr = R.front().getAssociatedExpression();
12073 return true;
12074 })) {
12075 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
12076 Diag(ConflictExpr->getExprLoc(), diag::note_used_here)
12077 << ConflictExpr->getSourceRange();
12078 continue;
12079 }
12080
12081 // Store the components in the stack so that they can be used to check
12082 // against other clauses later on.
12083 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D);
12084 DSAStack->addMappableExpressionComponents(
12085 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
12086
12087 // Record the expression we've just processed.
12088 MVLI.ProcessedVarList.push_back(SimpleRefExpr);
12089
12090 // Create a mappable component for the list item. List items in this clause
12091 // only need a component. We use a null declaration to signal fields in
12092 // 'this'.
12093 assert((isa<DeclRefExpr>(SimpleRefExpr) ||
12094 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
12095 "Unexpected device pointer expression!");
12096 MVLI.VarBaseDeclarations.push_back(
12097 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr);
12098 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
12099 MVLI.VarComponents.back().push_back(MC);
Carlo Bertolli70594e92016-07-13 17:16:49 +000012100 }
12101
Samuel Antao6890b092016-07-28 14:25:09 +000012102 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli70594e92016-07-13 17:16:49 +000012103 return nullptr;
12104
Samuel Antao6890b092016-07-28 14:25:09 +000012105 return OMPIsDevicePtrClause::Create(
12106 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
12107 MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli70594e92016-07-13 17:16:49 +000012108}