blob: 25096e615fe4f7f1e124538b685fe0410419b337 [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
David Majnemer9d168222016-08-05 17:44:54 +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.
David Majnemer9d168222016-08-05 17:44:54 +0000301 bool isCancelRegion() const { return Stack.back().CancelRegion; }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000302
Alexey Bataev9c821032015-04-30 04:23:23 +0000303 /// \brief Set collapse value for the region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000304 void setAssociatedLoops(unsigned Val) { Stack.back().AssociatedLoops = Val; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000305 /// \brief Return collapse value for region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000306 unsigned getAssociatedLoops() const { return Stack.back().AssociatedLoops; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000307
Alexey Bataev13314bf2014-10-09 04:18:56 +0000308 /// \brief Marks current target region as one with closely nested teams
309 /// region.
310 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
311 if (Stack.size() > 2)
312 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
313 }
314 /// \brief Returns true, if current region has closely nested teams region.
315 bool hasInnerTeamsRegion() const {
316 return getInnerTeamsRegionLoc().isValid();
317 }
318 /// \brief Returns location of the nested teams region (if any).
319 SourceLocation getInnerTeamsRegionLoc() const {
320 if (Stack.size() > 1)
321 return Stack.back().InnerTeamsRegionLoc;
322 return SourceLocation();
323 }
324
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000325 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000326 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000327 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000328
Samuel Antao90927002016-04-26 14:54:23 +0000329 // Do the check specified in \a Check to all component lists and return true
330 // if any issue is found.
331 bool checkMappableExprComponentListsForDecl(
332 ValueDecl *VD, bool CurrentRegionOnly,
Samuel Antao6890b092016-07-28 14:25:09 +0000333 const llvm::function_ref<
334 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
335 OpenMPClauseKind)> &Check) {
Samuel Antao5de996e2016-01-22 20:21:36 +0000336 auto SI = Stack.rbegin();
337 auto SE = Stack.rend();
338
339 if (SI == SE)
340 return false;
341
342 if (CurrentRegionOnly) {
343 SE = std::next(SI);
344 } else {
345 ++SI;
346 }
347
348 for (; SI != SE; ++SI) {
Samuel Antao90927002016-04-26 14:54:23 +0000349 auto MI = SI->MappedExprComponents.find(VD);
350 if (MI != SI->MappedExprComponents.end())
Samuel Antao6890b092016-07-28 14:25:09 +0000351 for (auto &L : MI->second.Components)
352 if (Check(L, MI->second.Kind))
Samuel Antao5de996e2016-01-22 20:21:36 +0000353 return true;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000354 }
Samuel Antao5de996e2016-01-22 20:21:36 +0000355 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000356 }
357
Samuel Antao90927002016-04-26 14:54:23 +0000358 // Create a new mappable expression component list associated with a given
359 // declaration and initialize it with the provided list of components.
360 void addMappableExpressionComponents(
361 ValueDecl *VD,
Samuel Antao6890b092016-07-28 14:25:09 +0000362 OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
363 OpenMPClauseKind WhereFoundClauseKind) {
Samuel Antao90927002016-04-26 14:54:23 +0000364 assert(Stack.size() > 1 &&
365 "Not expecting to retrieve components from a empty stack!");
366 auto &MEC = Stack.back().MappedExprComponents[VD];
367 // Create new entry and append the new components there.
Samuel Antao6890b092016-07-28 14:25:09 +0000368 MEC.Components.resize(MEC.Components.size() + 1);
369 MEC.Components.back().append(Components.begin(), Components.end());
370 MEC.Kind = WhereFoundClauseKind;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000371 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000372
373 unsigned getNestingLevel() const {
374 assert(Stack.size() > 1);
375 return Stack.size() - 2;
376 }
Alexey Bataev8b427062016-05-25 12:36:08 +0000377 void addDoacrossDependClause(OMPDependClause *C, OperatorOffsetTy &OpsOffs) {
378 assert(Stack.size() > 2);
379 assert(isOpenMPWorksharingDirective(Stack[Stack.size() - 2].Directive));
380 Stack[Stack.size() - 2].DoacrossDepends.insert({C, OpsOffs});
381 }
382 llvm::iterator_range<DoacrossDependMapTy::const_iterator>
383 getDoacrossDependClauses() const {
384 assert(Stack.size() > 1);
385 if (isOpenMPWorksharingDirective(Stack[Stack.size() - 1].Directive)) {
386 auto &Ref = Stack[Stack.size() - 1].DoacrossDepends;
387 return llvm::make_range(Ref.begin(), Ref.end());
388 }
389 return llvm::make_range(Stack[0].DoacrossDepends.end(),
390 Stack[0].DoacrossDepends.end());
391 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000392};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000393bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
Alexey Bataev35aaee62016-04-13 13:36:48 +0000394 return isOpenMPParallelDirective(DKind) || isOpenMPTaskingDirective(DKind) ||
395 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000396}
Alexey Bataeved09d242014-05-28 05:53:51 +0000397} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000398
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000399static ValueDecl *getCanonicalDecl(ValueDecl *D) {
400 auto *VD = dyn_cast<VarDecl>(D);
401 auto *FD = dyn_cast<FieldDecl>(D);
David Majnemer9d168222016-08-05 17:44:54 +0000402 if (VD != nullptr) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000403 VD = VD->getCanonicalDecl();
404 D = VD;
405 } else {
406 assert(FD);
407 FD = FD->getCanonicalDecl();
408 D = FD;
409 }
410 return D;
411}
412
David Majnemer9d168222016-08-05 17:44:54 +0000413DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator &Iter,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000414 ValueDecl *D) {
415 D = getCanonicalDecl(D);
416 auto *VD = dyn_cast<VarDecl>(D);
417 auto *FD = dyn_cast<FieldDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000418 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000419 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000420 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
421 // in a region but not in construct]
422 // File-scope or namespace-scope variables referenced in called routines
423 // in the region are shared unless they appear in a threadprivate
424 // directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000425 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000426 DVar.CKind = OMPC_shared;
427
428 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
429 // in a region but not in construct]
430 // Variables with static storage duration that are declared in called
431 // routines in the region are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000432 if (VD && VD->hasGlobalStorage())
433 DVar.CKind = OMPC_shared;
434
435 // Non-static data members are shared by default.
436 if (FD)
Alexey Bataev750a58b2014-03-18 12:19:12 +0000437 DVar.CKind = OMPC_shared;
438
Alexey Bataev758e55e2013-09-06 18:03:48 +0000439 return DVar;
440 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000441
Alexey Bataev758e55e2013-09-06 18:03:48 +0000442 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000443 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
444 // in a Construct, C/C++, predetermined, p.1]
445 // Variables with automatic storage duration that are declared in a scope
446 // inside the construct are private.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000447 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
448 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000449 DVar.CKind = OMPC_private;
450 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000451 }
452
Alexey Bataev758e55e2013-09-06 18:03:48 +0000453 // Explicitly specified attributes and local variables with predetermined
454 // attributes.
455 if (Iter->SharingMap.count(D)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000456 DVar.RefExpr = Iter->SharingMap[D].RefExpr.getPointer();
Alexey Bataev90c228f2016-02-08 09:29:13 +0000457 DVar.PrivateCopy = Iter->SharingMap[D].PrivateCopy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000458 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000459 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000460 return DVar;
461 }
462
463 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
464 // in a Construct, C/C++, implicitly determined, p.1]
465 // In a parallel or task construct, the data-sharing attributes of these
466 // variables are determined by the default clause, if present.
467 switch (Iter->DefaultAttr) {
468 case DSA_shared:
469 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000470 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000471 return DVar;
472 case DSA_none:
473 return DVar;
474 case DSA_unspecified:
475 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
476 // in a Construct, implicitly determined, p.2]
477 // In a parallel construct, if no default clause is present, these
478 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000479 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000480 if (isOpenMPParallelDirective(DVar.DKind) ||
481 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000482 DVar.CKind = OMPC_shared;
483 return DVar;
484 }
485
486 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
487 // in a Construct, implicitly determined, p.4]
488 // In a task construct, if no default clause is present, a variable that in
489 // the enclosing context is determined to be shared by all implicit tasks
490 // bound to the current team is shared.
Alexey Bataev35aaee62016-04-13 13:36:48 +0000491 if (isOpenMPTaskingDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000492 DSAVarData DVarTemp;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000493 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000494 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000495 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
Alexey Bataev35aaee62016-04-13 13:36:48 +0000496 // Referenced in a Construct, implicitly determined, p.6]
Alexey Bataev758e55e2013-09-06 18:03:48 +0000497 // In a task construct, if no default clause is present, a variable
498 // whose data-sharing attribute is not determined by the rules above is
499 // firstprivate.
500 DVarTemp = getDSA(I, D);
501 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000502 DVar.RefExpr = nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000503 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000504 return DVar;
505 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000506 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000507 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000508 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000509 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000510 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000511 return DVar;
512 }
513 }
514 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
515 // in a Construct, implicitly determined, p.3]
516 // For constructs other than task, if no default clause is present, these
517 // variables inherit their data-sharing attributes from the enclosing
518 // context.
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000519 return getDSA(++Iter, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000520}
521
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000522Expr *DSAStackTy::addUniqueAligned(ValueDecl *D, Expr *NewDE) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000523 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000524 D = getCanonicalDecl(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000525 auto It = Stack.back().AlignedMap.find(D);
526 if (It == Stack.back().AlignedMap.end()) {
527 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
528 Stack.back().AlignedMap[D] = NewDE;
529 return nullptr;
530 } else {
531 assert(It->second && "Unexpected nullptr expr in the aligned map");
532 return It->second;
533 }
534 return nullptr;
535}
536
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000537void DSAStackTy::addLoopControlVariable(ValueDecl *D, VarDecl *Capture) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000538 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000539 D = getCanonicalDecl(D);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000540 Stack.back().LCVMap.insert(
541 std::make_pair(D, LCDeclInfo(Stack.back().LCVMap.size() + 1, Capture)));
Alexey Bataev9c821032015-04-30 04:23:23 +0000542}
543
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000544DSAStackTy::LCDeclInfo DSAStackTy::isLoopControlVariable(ValueDecl *D) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000545 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000546 D = getCanonicalDecl(D);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000547 return Stack.back().LCVMap.count(D) > 0 ? Stack.back().LCVMap[D]
548 : LCDeclInfo(0, nullptr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000549}
550
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000551DSAStackTy::LCDeclInfo DSAStackTy::isParentLoopControlVariable(ValueDecl *D) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000552 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000553 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000554 return Stack[Stack.size() - 2].LCVMap.count(D) > 0
555 ? Stack[Stack.size() - 2].LCVMap[D]
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000556 : LCDeclInfo(0, nullptr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000557}
558
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000559ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000560 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
561 if (Stack[Stack.size() - 2].LCVMap.size() < I)
562 return nullptr;
563 for (auto &Pair : Stack[Stack.size() - 2].LCVMap) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000564 if (Pair.second.first == I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000565 return Pair.first;
566 }
567 return nullptr;
Alexey Bataev9c821032015-04-30 04:23:23 +0000568}
569
Alexey Bataev90c228f2016-02-08 09:29:13 +0000570void DSAStackTy::addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
571 DeclRefExpr *PrivateCopy) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000572 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000573 if (A == OMPC_threadprivate) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000574 auto &Data = Stack[0].SharingMap[D];
575 Data.Attributes = A;
576 Data.RefExpr.setPointer(E);
577 Data.PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000578 } else {
579 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000580 auto &Data = Stack.back().SharingMap[D];
581 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
582 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
583 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
584 (isLoopControlVariable(D).first && A == OMPC_private));
585 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
586 Data.RefExpr.setInt(/*IntVal=*/true);
587 return;
588 }
589 const bool IsLastprivate =
590 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
591 Data.Attributes = A;
592 Data.RefExpr.setPointerAndInt(E, IsLastprivate);
593 Data.PrivateCopy = PrivateCopy;
594 if (PrivateCopy) {
595 auto &Data = Stack.back().SharingMap[PrivateCopy->getDecl()];
596 Data.Attributes = A;
597 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
598 Data.PrivateCopy = nullptr;
599 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000600 }
601}
602
Alexey Bataeved09d242014-05-28 05:53:51 +0000603bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000604 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000605 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000606 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000607 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000608 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000609 ++I;
610 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000611 if (I == E)
612 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000613 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000614 Scope *CurScope = getCurScope();
615 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000616 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000617 }
618 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000619 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000620 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000621}
622
Alexey Bataev39f915b82015-05-08 10:41:21 +0000623/// \brief Build a variable declaration for OpenMP loop iteration variable.
624static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000625 StringRef Name, const AttrVec *Attrs = nullptr) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000626 DeclContext *DC = SemaRef.CurContext;
627 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
628 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
629 VarDecl *Decl =
630 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000631 if (Attrs) {
632 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
633 I != E; ++I)
634 Decl->addAttr(*I);
635 }
Alexey Bataev39f915b82015-05-08 10:41:21 +0000636 Decl->setImplicit();
637 return Decl;
638}
639
640static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
641 SourceLocation Loc,
642 bool RefersToCapture = false) {
643 D->setReferenced();
644 D->markUsed(S.Context);
645 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
646 SourceLocation(), D, RefersToCapture, Loc, Ty,
647 VK_LValue);
648}
649
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000650DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, bool FromParent) {
651 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000652 DSAVarData DVar;
653
654 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
655 // in a Construct, C/C++, predetermined, p.1]
656 // Variables appearing in threadprivate directives are threadprivate.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000657 auto *VD = dyn_cast<VarDecl>(D);
658 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
659 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
Samuel Antaof8b50122015-07-13 22:54:53 +0000660 SemaRef.getLangOpts().OpenMPUseTLS &&
661 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000662 (VD && VD->getStorageClass() == SC_Register &&
663 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
664 addDSA(D, buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
Alexey Bataev39f915b82015-05-08 10:41:21 +0000665 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000666 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000667 }
668 if (Stack[0].SharingMap.count(D)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000669 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr.getPointer();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000670 DVar.CKind = OMPC_threadprivate;
671 return DVar;
672 }
673
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000674 if (Stack.size() == 1) {
675 // Not in OpenMP execution region and top scope was already checked.
676 return DVar;
677 }
678
Alexey Bataev758e55e2013-09-06 18:03:48 +0000679 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000680 // in a Construct, C/C++, predetermined, p.4]
681 // Static data members are shared.
682 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
683 // in a Construct, C/C++, predetermined, p.7]
684 // Variables with static storage duration that are declared in a scope
685 // inside the construct are shared.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000686 auto &&MatchesAlways = [](OpenMPDirectiveKind) -> bool { return true; };
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000687 if (VD && VD->isStaticDataMember()) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000688 DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent);
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000689 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +0000690 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000691
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000692 DVar.CKind = OMPC_shared;
693 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000694 }
695
696 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000697 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
698 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000699 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
700 // in a Construct, C/C++, predetermined, p.6]
701 // Variables with const qualified type having no mutable member are
702 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000703 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000704 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataevc9bd03d2015-12-17 06:55:08 +0000705 if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
706 if (auto *CTD = CTSD->getSpecializedTemplate())
707 RD = CTD->getTemplatedDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000708 if (IsConstant &&
Alexey Bataev4bcad7f2016-02-10 10:50:12 +0000709 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
710 RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000711 // Variables with const-qualified type having no mutable member may be
712 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000713 DSAVarData DVarTemp = hasDSA(
714 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_firstprivate; },
715 MatchesAlways, FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000716 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
717 return DVar;
718
Alexey Bataev758e55e2013-09-06 18:03:48 +0000719 DVar.CKind = OMPC_shared;
720 return DVar;
721 }
722
Alexey Bataev758e55e2013-09-06 18:03:48 +0000723 // Explicitly specified attributes and local variables with predetermined
724 // attributes.
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000725 auto StartI = std::next(Stack.rbegin());
726 auto EndI = std::prev(Stack.rend());
727 if (FromParent && StartI != EndI) {
728 StartI = std::next(StartI);
729 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000730 auto I = std::prev(StartI);
731 if (I->SharingMap.count(D)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000732 DVar.RefExpr = I->SharingMap[D].RefExpr.getPointer();
Alexey Bataev90c228f2016-02-08 09:29:13 +0000733 DVar.PrivateCopy = I->SharingMap[D].PrivateCopy;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000734 DVar.CKind = I->SharingMap[D].Attributes;
735 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000736 }
737
738 return DVar;
739}
740
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000741DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
742 bool FromParent) {
743 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000744 auto StartI = Stack.rbegin();
745 auto EndI = std::prev(Stack.rend());
746 if (FromParent && StartI != EndI) {
747 StartI = std::next(StartI);
748 }
749 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000750}
751
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000752DSAStackTy::DSAVarData
753DSAStackTy::hasDSA(ValueDecl *D,
754 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
755 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
756 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000757 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000758 auto StartI = std::next(Stack.rbegin());
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000759 auto EndI = Stack.rend();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000760 if (FromParent && StartI != EndI) {
761 StartI = std::next(StartI);
762 }
763 for (auto I = StartI, EE = EndI; I != EE; ++I) {
764 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000765 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000766 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000767 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000768 return DVar;
769 }
770 return DSAVarData();
771}
772
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000773DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
774 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
775 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
776 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000777 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000778 auto StartI = std::next(Stack.rbegin());
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000779 auto EndI = Stack.rend();
Alexey Bataeve3978122016-07-19 05:06:39 +0000780 if (FromParent && StartI != EndI)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000781 StartI = std::next(StartI);
Alexey Bataeve3978122016-07-19 05:06:39 +0000782 if (StartI == EndI || !DPred(StartI->Directive))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000783 return DSAVarData();
Alexey Bataeve3978122016-07-19 05:06:39 +0000784 DSAVarData DVar = getDSA(StartI, D);
785 return CPred(DVar.CKind) ? DVar : DSAVarData();
Alexey Bataevc5e02582014-06-16 07:08:35 +0000786}
787
Alexey Bataevaac108a2015-06-23 04:51:00 +0000788bool DSAStackTy::hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000789 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000790 unsigned Level, bool NotLastprivate) {
Alexey Bataevaac108a2015-06-23 04:51:00 +0000791 if (CPred(ClauseKindMode))
792 return true;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000793 D = getCanonicalDecl(D);
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000794 auto StartI = std::next(Stack.begin());
795 auto EndI = Stack.end();
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000796 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000797 return false;
798 std::advance(StartI, Level);
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000799 return (StartI->SharingMap.count(D) > 0) &&
800 StartI->SharingMap[D].RefExpr.getPointer() &&
801 CPred(StartI->SharingMap[D].Attributes) &&
802 (!NotLastprivate || !StartI->SharingMap[D].RefExpr.getInt());
Alexey Bataevaac108a2015-06-23 04:51:00 +0000803}
804
Samuel Antao4be30e92015-10-02 17:14:03 +0000805bool DSAStackTy::hasExplicitDirective(
806 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
807 unsigned Level) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000808 auto StartI = std::next(Stack.begin());
809 auto EndI = Stack.end();
Samuel Antao4be30e92015-10-02 17:14:03 +0000810 if (std::distance(StartI, EndI) <= (int)Level)
811 return false;
812 std::advance(StartI, Level);
813 return DPred(StartI->Directive);
814}
815
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000816bool DSAStackTy::hasDirective(
817 const llvm::function_ref<bool(OpenMPDirectiveKind,
818 const DeclarationNameInfo &, SourceLocation)>
819 &DPred,
820 bool FromParent) {
Samuel Antaof0d79752016-05-27 15:21:27 +0000821 // We look only in the enclosing region.
822 if (Stack.size() < 2)
823 return false;
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000824 auto StartI = std::next(Stack.rbegin());
825 auto EndI = std::prev(Stack.rend());
826 if (FromParent && StartI != EndI) {
827 StartI = std::next(StartI);
828 }
829 for (auto I = StartI, EE = EndI; I != EE; ++I) {
830 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
831 return true;
832 }
833 return false;
834}
835
Alexey Bataev758e55e2013-09-06 18:03:48 +0000836void Sema::InitDataSharingAttributesStack() {
837 VarDataSharingAttributesStack = new DSAStackTy(*this);
838}
839
840#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
841
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000842bool Sema::IsOpenMPCapturedByRef(ValueDecl *D, unsigned Level) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000843 assert(LangOpts.OpenMP && "OpenMP is not allowed");
844
845 auto &Ctx = getASTContext();
846 bool IsByRef = true;
847
848 // Find the directive that is associated with the provided scope.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000849 auto Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000850
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000851 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000852 // This table summarizes how a given variable should be passed to the device
853 // given its type and the clauses where it appears. This table is based on
854 // the description in OpenMP 4.5 [2.10.4, target Construct] and
855 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
856 //
857 // =========================================================================
858 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
859 // | |(tofrom:scalar)| | pvt | | | |
860 // =========================================================================
861 // | scl | | | | - | | bycopy|
862 // | scl | | - | x | - | - | bycopy|
863 // | scl | | x | - | - | - | null |
864 // | scl | x | | | - | | byref |
865 // | scl | x | - | x | - | - | bycopy|
866 // | scl | x | x | - | - | - | null |
867 // | scl | | - | - | - | x | byref |
868 // | scl | x | - | - | - | x | byref |
869 //
870 // | agg | n.a. | | | - | | byref |
871 // | agg | n.a. | - | x | - | - | byref |
872 // | agg | n.a. | x | - | - | - | null |
873 // | agg | n.a. | - | - | - | x | byref |
874 // | agg | n.a. | - | - | - | x[] | byref |
875 //
876 // | ptr | n.a. | | | - | | bycopy|
877 // | ptr | n.a. | - | x | - | - | bycopy|
878 // | ptr | n.a. | x | - | - | - | null |
879 // | ptr | n.a. | - | - | - | x | byref |
880 // | ptr | n.a. | - | - | - | x[] | bycopy|
881 // | ptr | n.a. | - | - | x | | bycopy|
882 // | ptr | n.a. | - | - | x | x | bycopy|
883 // | ptr | n.a. | - | - | x | x[] | bycopy|
884 // =========================================================================
885 // Legend:
886 // scl - scalar
887 // ptr - pointer
888 // agg - aggregate
889 // x - applies
890 // - - invalid in this combination
891 // [] - mapped with an array section
892 // byref - should be mapped by reference
893 // byval - should be mapped by value
894 // null - initialize a local variable to null on the device
895 //
896 // Observations:
897 // - All scalar declarations that show up in a map clause have to be passed
898 // by reference, because they may have been mapped in the enclosing data
899 // environment.
900 // - If the scalar value does not fit the size of uintptr, it has to be
901 // passed by reference, regardless the result in the table above.
902 // - For pointers mapped by value that have either an implicit map or an
903 // array section, the runtime library may pass the NULL value to the
904 // device instead of the value passed to it by the compiler.
905
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000906 if (Ty->isReferenceType())
907 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
Samuel Antao86ace552016-04-27 22:40:57 +0000908
909 // Locate map clauses and see if the variable being captured is referred to
910 // in any of those clauses. Here we only care about variables, not fields,
911 // because fields are part of aggregates.
912 bool IsVariableUsedInMapClause = false;
913 bool IsVariableAssociatedWithSection = false;
914
915 DSAStack->checkMappableExprComponentListsForDecl(
916 D, /*CurrentRegionOnly=*/true,
917 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
Samuel Antao6890b092016-07-28 14:25:09 +0000918 MapExprComponents,
919 OpenMPClauseKind WhereFoundClauseKind) {
920 // Only the map clause information influences how a variable is
921 // captured. E.g. is_device_ptr does not require changing the default
922 // behaviour.
923 if (WhereFoundClauseKind != OMPC_map)
924 return false;
Samuel Antao86ace552016-04-27 22:40:57 +0000925
926 auto EI = MapExprComponents.rbegin();
927 auto EE = MapExprComponents.rend();
928
929 assert(EI != EE && "Invalid map expression!");
930
931 if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
932 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
933
934 ++EI;
935 if (EI == EE)
936 return false;
937
938 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
939 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
940 isa<MemberExpr>(EI->getAssociatedExpression())) {
941 IsVariableAssociatedWithSection = true;
942 // There is nothing more we need to know about this variable.
943 return true;
944 }
945
946 // Keep looking for more map info.
947 return false;
948 });
949
950 if (IsVariableUsedInMapClause) {
951 // If variable is identified in a map clause it is always captured by
952 // reference except if it is a pointer that is dereferenced somehow.
953 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
954 } else {
955 // By default, all the data that has a scalar type is mapped by copy.
956 IsByRef = !Ty->isScalarType();
957 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000958 }
959
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000960 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
961 IsByRef = !DSAStack->hasExplicitDSA(
962 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
963 Level, /*NotLastprivate=*/true);
964 }
965
Samuel Antao86ace552016-04-27 22:40:57 +0000966 // When passing data by copy, we need to make sure it fits the uintptr size
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000967 // and alignment, because the runtime library only deals with uintptr types.
968 // If it does not fit the uintptr size, we need to pass the data by reference
969 // instead.
970 if (!IsByRef &&
971 (Ctx.getTypeSizeInChars(Ty) >
972 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000973 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000974 IsByRef = true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000975 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000976
977 return IsByRef;
978}
979
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000980unsigned Sema::getOpenMPNestingLevel() const {
981 assert(getLangOpts().OpenMP);
982 return DSAStack->getNestingLevel();
983}
984
Alexey Bataev90c228f2016-02-08 09:29:13 +0000985VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +0000986 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000987 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +0000988
989 // If we are attempting to capture a global variable in a directive with
990 // 'target' we return true so that this global is also mapped to the device.
991 //
992 // FIXME: If the declaration is enclosed in a 'declare target' directive,
993 // then it should not be captured. Therefore, an extra check has to be
994 // inserted here once support for 'declare target' is added.
995 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000996 auto *VD = dyn_cast<VarDecl>(D);
997 if (VD && !VD->hasLocalStorage()) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000998 if (DSAStack->getCurrentDirective() == OMPD_target &&
Alexey Bataev90c228f2016-02-08 09:29:13 +0000999 !DSAStack->isClauseParsingMode())
1000 return VD;
Samuel Antaof0d79752016-05-27 15:21:27 +00001001 if (DSAStack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001002 [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
1003 SourceLocation) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00001004 return isOpenMPTargetExecutionDirective(K);
Samuel Antao4be30e92015-10-02 17:14:03 +00001005 },
Alexey Bataev90c228f2016-02-08 09:29:13 +00001006 false))
1007 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +00001008 }
1009
Alexey Bataev48977c32015-08-04 08:10:48 +00001010 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
1011 (!DSAStack->isClauseParsingMode() ||
1012 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001013 auto &&Info = DSAStack->isLoopControlVariable(D);
1014 if (Info.first ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001015 (VD && VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001016 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001017 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001018 return VD ? VD : Info.second;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001019 auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001020 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +00001021 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001022 DVarPrivate = DSAStack->hasDSA(
1023 D, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
1024 DSAStack->isClauseParsingMode());
Alexey Bataev90c228f2016-02-08 09:29:13 +00001025 if (DVarPrivate.CKind != OMPC_unknown)
1026 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001027 }
Alexey Bataev90c228f2016-02-08 09:29:13 +00001028 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +00001029}
1030
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001031bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) {
Alexey Bataevaac108a2015-06-23 04:51:00 +00001032 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1033 return DSAStack->hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001034 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
Alexey Bataevaac108a2015-06-23 04:51:00 +00001035}
1036
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001037bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) {
Samuel Antao4be30e92015-10-02 17:14:03 +00001038 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1039 // Return true if the current level is no longer enclosed in a target region.
1040
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001041 auto *VD = dyn_cast<VarDecl>(D);
1042 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00001043 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1044 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +00001045}
1046
Alexey Bataeved09d242014-05-28 05:53:51 +00001047void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001048
1049void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
1050 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001051 Scope *CurScope, SourceLocation Loc) {
1052 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001053 PushExpressionEvaluationContext(PotentiallyEvaluated);
1054}
1055
Alexey Bataevaac108a2015-06-23 04:51:00 +00001056void Sema::StartOpenMPClause(OpenMPClauseKind K) {
1057 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001058}
1059
Alexey Bataevaac108a2015-06-23 04:51:00 +00001060void Sema::EndOpenMPClause() {
1061 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001062}
1063
Alexey Bataev758e55e2013-09-06 18:03:48 +00001064void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001065 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
1066 // A variable of class type (or array thereof) that appears in a lastprivate
1067 // clause requires an accessible, unambiguous default constructor for the
1068 // class type, unless the list item is also specified in a firstprivate
1069 // clause.
David Majnemer9d168222016-08-05 17:44:54 +00001070 if (auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001071 for (auto *C : D->clauses()) {
1072 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
1073 SmallVector<Expr *, 8> PrivateCopies;
1074 for (auto *DE : Clause->varlists()) {
1075 if (DE->isValueDependent() || DE->isTypeDependent()) {
1076 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001077 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +00001078 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00001079 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
Alexey Bataev005248a2016-02-25 05:25:57 +00001080 VarDecl *VD = cast<VarDecl>(DRE->getDecl());
1081 QualType Type = VD->getType().getNonReferenceType();
1082 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001083 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001084 // Generate helper private variable and initialize it with the
1085 // default value. The address of the original variable is replaced
1086 // by the address of the new private variable in CodeGen. This new
1087 // variable is not added to IdResolver, so the code in the OpenMP
1088 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001089 auto *VDPrivate = buildVarDecl(
1090 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev005248a2016-02-25 05:25:57 +00001091 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00001092 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
1093 if (VDPrivate->isInvalidDecl())
1094 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001095 PrivateCopies.push_back(buildDeclRefExpr(
1096 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001097 } else {
1098 // The variable is also a firstprivate, so initialization sequence
1099 // for private copy is generated already.
1100 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001101 }
1102 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001103 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001104 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001105 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001106 }
1107 }
1108 }
1109
Alexey Bataev758e55e2013-09-06 18:03:48 +00001110 DSAStack->pop();
1111 DiscardCleanupsInEvaluationContext();
1112 PopExpressionEvaluationContext();
1113}
1114
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001115static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1116 Expr *NumIterations, Sema &SemaRef,
1117 Scope *S, DSAStackTy *Stack);
Alexander Musman3276a272015-03-21 10:12:56 +00001118
Alexey Bataeva769e072013-03-22 06:34:35 +00001119namespace {
1120
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001121class VarDeclFilterCCC : public CorrectionCandidateCallback {
1122private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001123 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001124
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001125public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001126 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001127 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001128 NamedDecl *ND = Candidate.getCorrectionDecl();
David Majnemer9d168222016-08-05 17:44:54 +00001129 if (auto *VD = dyn_cast_or_null<VarDecl>(ND)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001130 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001131 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1132 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001133 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001134 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001135 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001136};
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001137
1138class VarOrFuncDeclFilterCCC : public CorrectionCandidateCallback {
1139private:
1140 Sema &SemaRef;
1141
1142public:
1143 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
1144 bool ValidateCandidate(const TypoCorrection &Candidate) override {
1145 NamedDecl *ND = Candidate.getCorrectionDecl();
1146 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
1147 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1148 SemaRef.getCurScope());
1149 }
1150 return false;
1151 }
1152};
1153
Alexey Bataeved09d242014-05-28 05:53:51 +00001154} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001155
1156ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1157 CXXScopeSpec &ScopeSpec,
1158 const DeclarationNameInfo &Id) {
1159 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1160 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1161
1162 if (Lookup.isAmbiguous())
1163 return ExprError();
1164
1165 VarDecl *VD;
1166 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001167 if (TypoCorrection Corrected = CorrectTypo(
1168 Id, LookupOrdinaryName, CurScope, nullptr,
1169 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001170 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001171 PDiag(Lookup.empty()
1172 ? diag::err_undeclared_var_use_suggest
1173 : diag::err_omp_expected_var_arg_suggest)
1174 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001175 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001176 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001177 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1178 : diag::err_omp_expected_var_arg)
1179 << Id.getName();
1180 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001181 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001182 } else {
1183 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001184 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001185 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1186 return ExprError();
1187 }
1188 }
1189 Lookup.suppressDiagnostics();
1190
1191 // OpenMP [2.9.2, Syntax, C/C++]
1192 // Variables must be file-scope, namespace-scope, or static block-scope.
1193 if (!VD->hasGlobalStorage()) {
1194 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001195 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1196 bool IsDecl =
1197 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001198 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001199 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1200 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001201 return ExprError();
1202 }
1203
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001204 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1205 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001206 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1207 // A threadprivate directive for file-scope variables must appear outside
1208 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001209 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1210 !getCurLexicalContext()->isTranslationUnit()) {
1211 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001212 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1213 bool IsDecl =
1214 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1215 Diag(VD->getLocation(),
1216 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1217 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001218 return ExprError();
1219 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001220 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1221 // A threadprivate directive for static class member variables must appear
1222 // in the class definition, in the same scope in which the member
1223 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001224 if (CanonicalVD->isStaticDataMember() &&
1225 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1226 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001227 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1228 bool IsDecl =
1229 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1230 Diag(VD->getLocation(),
1231 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1232 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001233 return ExprError();
1234 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001235 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1236 // A threadprivate directive for namespace-scope variables must appear
1237 // outside any definition or declaration other than the namespace
1238 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001239 if (CanonicalVD->getDeclContext()->isNamespace() &&
1240 (!getCurLexicalContext()->isFileContext() ||
1241 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1242 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001243 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1244 bool IsDecl =
1245 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1246 Diag(VD->getLocation(),
1247 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1248 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001249 return ExprError();
1250 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001251 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1252 // A threadprivate directive for static block-scope variables must appear
1253 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001254 if (CanonicalVD->isStaticLocal() && CurScope &&
1255 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001256 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001257 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1258 bool IsDecl =
1259 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1260 Diag(VD->getLocation(),
1261 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1262 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001263 return ExprError();
1264 }
1265
1266 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1267 // A threadprivate directive must lexically precede all references to any
1268 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001269 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001270 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001271 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001272 return ExprError();
1273 }
1274
1275 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00001276 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1277 SourceLocation(), VD,
1278 /*RefersToEnclosingVariableOrCapture=*/false,
1279 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001280}
1281
Alexey Bataeved09d242014-05-28 05:53:51 +00001282Sema::DeclGroupPtrTy
1283Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1284 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001285 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001286 CurContext->addDecl(D);
1287 return DeclGroupPtrTy::make(DeclGroupRef(D));
1288 }
David Blaikie0403cb12016-01-15 23:43:25 +00001289 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001290}
1291
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001292namespace {
1293class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1294 Sema &SemaRef;
1295
1296public:
1297 bool VisitDeclRefExpr(const DeclRefExpr *E) {
David Majnemer9d168222016-08-05 17:44:54 +00001298 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001299 if (VD->hasLocalStorage()) {
1300 SemaRef.Diag(E->getLocStart(),
1301 diag::err_omp_local_var_in_threadprivate_init)
1302 << E->getSourceRange();
1303 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1304 << VD << VD->getSourceRange();
1305 return true;
1306 }
1307 }
1308 return false;
1309 }
1310 bool VisitStmt(const Stmt *S) {
1311 for (auto Child : S->children()) {
1312 if (Child && Visit(Child))
1313 return true;
1314 }
1315 return false;
1316 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001317 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001318};
1319} // namespace
1320
Alexey Bataeved09d242014-05-28 05:53:51 +00001321OMPThreadPrivateDecl *
1322Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001323 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001324 for (auto &RefExpr : VarList) {
1325 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001326 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1327 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001328
Alexey Bataev376b4a42016-02-09 09:41:09 +00001329 // Mark variable as used.
1330 VD->setReferenced();
1331 VD->markUsed(Context);
1332
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001333 QualType QType = VD->getType();
1334 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1335 // It will be analyzed later.
1336 Vars.push_back(DE);
1337 continue;
1338 }
1339
Alexey Bataeva769e072013-03-22 06:34:35 +00001340 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1341 // A threadprivate variable must not have an incomplete type.
1342 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001343 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001344 continue;
1345 }
1346
1347 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1348 // A threadprivate variable must not have a reference type.
1349 if (VD->getType()->isReferenceType()) {
1350 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001351 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1352 bool IsDecl =
1353 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1354 Diag(VD->getLocation(),
1355 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1356 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001357 continue;
1358 }
1359
Samuel Antaof8b50122015-07-13 22:54:53 +00001360 // Check if this is a TLS variable. If TLS is not being supported, produce
1361 // the corresponding diagnostic.
1362 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1363 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1364 getLangOpts().OpenMPUseTLS &&
1365 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001366 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1367 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001368 Diag(ILoc, diag::err_omp_var_thread_local)
1369 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001370 bool IsDecl =
1371 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1372 Diag(VD->getLocation(),
1373 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1374 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001375 continue;
1376 }
1377
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001378 // Check if initial value of threadprivate variable reference variable with
1379 // local storage (it is not supported by runtime).
1380 if (auto Init = VD->getAnyInitializer()) {
1381 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001382 if (Checker.Visit(Init))
1383 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001384 }
1385
Alexey Bataeved09d242014-05-28 05:53:51 +00001386 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001387 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001388 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1389 Context, SourceRange(Loc, Loc)));
1390 if (auto *ML = Context.getASTMutationListener())
1391 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001392 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001393 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001394 if (!Vars.empty()) {
1395 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1396 Vars);
1397 D->setAccess(AS_public);
1398 }
1399 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001400}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001401
Alexey Bataev7ff55242014-06-19 09:13:45 +00001402static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001403 const ValueDecl *D, DSAStackTy::DSAVarData DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00001404 bool IsLoopIterVar = false) {
1405 if (DVar.RefExpr) {
1406 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1407 << getOpenMPClauseName(DVar.CKind);
1408 return;
1409 }
1410 enum {
1411 PDSA_StaticMemberShared,
1412 PDSA_StaticLocalVarShared,
1413 PDSA_LoopIterVarPrivate,
1414 PDSA_LoopIterVarLinear,
1415 PDSA_LoopIterVarLastprivate,
1416 PDSA_ConstVarShared,
1417 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001418 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001419 PDSA_LocalVarPrivate,
1420 PDSA_Implicit
1421 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001422 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001423 auto ReportLoc = D->getLocation();
1424 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001425 if (IsLoopIterVar) {
1426 if (DVar.CKind == OMPC_private)
1427 Reason = PDSA_LoopIterVarPrivate;
1428 else if (DVar.CKind == OMPC_lastprivate)
1429 Reason = PDSA_LoopIterVarLastprivate;
1430 else
1431 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev35aaee62016-04-13 13:36:48 +00001432 } else if (isOpenMPTaskingDirective(DVar.DKind) &&
1433 DVar.CKind == OMPC_firstprivate) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001434 Reason = PDSA_TaskVarFirstprivate;
1435 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001436 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001437 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001438 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001439 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001440 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001441 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001442 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00001443 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001444 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001445 ReportHint = true;
1446 Reason = PDSA_LocalVarPrivate;
1447 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001448 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001449 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001450 << Reason << ReportHint
1451 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1452 } else if (DVar.ImplicitDSALoc.isValid()) {
1453 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1454 << getOpenMPClauseName(DVar.CKind);
1455 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001456}
1457
Alexey Bataev758e55e2013-09-06 18:03:48 +00001458namespace {
1459class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1460 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001461 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001462 bool ErrorFound;
1463 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001464 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001465 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001466
Alexey Bataev758e55e2013-09-06 18:03:48 +00001467public:
1468 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00001469 if (E->isTypeDependent() || E->isValueDependent() ||
1470 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1471 return;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001472 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001473 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001474 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1475 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001476
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001477 auto DVar = Stack->getTopDSA(VD, false);
1478 // Check if the variable has explicit DSA set and stop analysis if it so.
David Majnemer9d168222016-08-05 17:44:54 +00001479 if (DVar.RefExpr)
1480 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001481
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001482 auto ELoc = E->getExprLoc();
1483 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001484 // The default(none) clause requires that each variable that is referenced
1485 // in the construct, and does not have a predetermined data-sharing
1486 // attribute, must have its data-sharing attribute explicitly determined
1487 // by being listed in a data-sharing attribute clause.
1488 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001489 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001490 VarsWithInheritedDSA.count(VD) == 0) {
1491 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001492 return;
1493 }
1494
1495 // OpenMP [2.9.3.6, Restrictions, p.2]
1496 // A list item that appears in a reduction clause of the innermost
1497 // enclosing worksharing or parallel construct may not be accessed in an
1498 // explicit task.
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001499 DVar = Stack->hasInnermostDSA(
1500 VD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
1501 [](OpenMPDirectiveKind K) -> bool {
1502 return isOpenMPParallelDirective(K) ||
1503 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
1504 },
1505 false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001506 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00001507 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001508 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1509 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001510 return;
1511 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001512
1513 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001514 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001515 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1516 !Stack->isLoopControlVariable(VD).first)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001517 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001518 }
1519 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001520 void VisitMemberExpr(MemberExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00001521 if (E->isTypeDependent() || E->isValueDependent() ||
1522 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1523 return;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001524 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1525 if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1526 auto DVar = Stack->getTopDSA(FD, false);
1527 // Check if the variable has explicit DSA set and stop analysis if it
1528 // so.
1529 if (DVar.RefExpr)
1530 return;
1531
1532 auto ELoc = E->getExprLoc();
1533 auto DKind = Stack->getCurrentDirective();
1534 // OpenMP [2.9.3.6, Restrictions, p.2]
1535 // A list item that appears in a reduction clause of the innermost
1536 // enclosing worksharing or parallel construct may not be accessed in
Alexey Bataevd985eda2016-02-10 11:29:16 +00001537 // an explicit task.
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001538 DVar = Stack->hasInnermostDSA(
1539 FD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
1540 [](OpenMPDirectiveKind K) -> bool {
1541 return isOpenMPParallelDirective(K) ||
1542 isOpenMPWorksharingDirective(K) ||
1543 isOpenMPTeamsDirective(K);
1544 },
1545 false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001546 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001547 ErrorFound = true;
1548 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1549 ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1550 return;
1551 }
1552
1553 // Define implicit data-sharing attributes for task.
1554 DVar = Stack->getImplicitDSA(FD, false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001555 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1556 !Stack->isLoopControlVariable(FD).first)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001557 ImplicitFirstprivate.push_back(E);
1558 }
1559 }
1560 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001561 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001562 for (auto *C : S->clauses()) {
1563 // Skip analysis of arguments of implicitly defined firstprivate clause
1564 // for task directives.
1565 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1566 for (auto *CC : C->children()) {
1567 if (CC)
1568 Visit(CC);
1569 }
1570 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001571 }
1572 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001573 for (auto *C : S->children()) {
1574 if (C && !isa<OMPExecutableDirective>(C))
1575 Visit(C);
1576 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001577 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001578
1579 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001580 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001581 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001582 return VarsWithInheritedDSA;
1583 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001584
Alexey Bataev7ff55242014-06-19 09:13:45 +00001585 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1586 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001587};
Alexey Bataeved09d242014-05-28 05:53:51 +00001588} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001589
Alexey Bataevbae9a792014-06-27 10:37:06 +00001590void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001591 switch (DKind) {
Kelvin Li70a12c52016-07-13 21:51:49 +00001592 case OMPD_parallel:
1593 case OMPD_parallel_for:
1594 case OMPD_parallel_for_simd:
1595 case OMPD_parallel_sections:
1596 case OMPD_teams: {
Alexey Bataev9959db52014-05-06 10:08:46 +00001597 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001598 QualType KmpInt32PtrTy =
1599 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001600 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001601 std::make_pair(".global_tid.", KmpInt32PtrTy),
1602 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1603 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001604 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001605 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1606 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001607 break;
1608 }
Kelvin Li70a12c52016-07-13 21:51:49 +00001609 case OMPD_simd:
1610 case OMPD_for:
1611 case OMPD_for_simd:
1612 case OMPD_sections:
1613 case OMPD_section:
1614 case OMPD_single:
1615 case OMPD_master:
1616 case OMPD_critical:
Kelvin Lia579b912016-07-14 02:54:56 +00001617 case OMPD_taskgroup:
1618 case OMPD_distribute:
Kelvin Li70a12c52016-07-13 21:51:49 +00001619 case OMPD_ordered:
1620 case OMPD_atomic:
1621 case OMPD_target_data:
1622 case OMPD_target:
1623 case OMPD_target_parallel:
1624 case OMPD_target_parallel_for:
Kelvin Li986330c2016-07-20 22:57:10 +00001625 case OMPD_target_parallel_for_simd:
1626 case OMPD_target_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001627 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001628 std::make_pair(StringRef(), QualType()) // __context with shared vars
1629 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001630 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1631 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001632 break;
1633 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001634 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001635 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001636 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1637 FunctionProtoType::ExtProtoInfo EPI;
1638 EPI.Variadic = true;
1639 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001640 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001641 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataev48591dd2016-04-20 04:01:36 +00001642 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1643 std::make_pair(".privates.", Context.VoidPtrTy.withConst()),
1644 std::make_pair(".copy_fn.",
1645 Context.getPointerType(CopyFnType).withConst()),
1646 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001647 std::make_pair(StringRef(), QualType()) // __context with shared vars
1648 };
1649 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1650 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001651 // Mark this captured region as inlined, because we don't use outlined
1652 // function directly.
1653 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1654 AlwaysInlineAttr::CreateImplicit(
1655 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001656 break;
1657 }
Alexey Bataev1e73ef32016-04-28 12:14:51 +00001658 case OMPD_taskloop:
1659 case OMPD_taskloop_simd: {
Alexey Bataev7292c292016-04-25 12:22:29 +00001660 QualType KmpInt32Ty =
1661 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
1662 QualType KmpUInt64Ty =
1663 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
1664 QualType KmpInt64Ty =
1665 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
1666 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1667 FunctionProtoType::ExtProtoInfo EPI;
1668 EPI.Variadic = true;
1669 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev49f6e782015-12-01 04:18:41 +00001670 Sema::CapturedParamNameType Params[] = {
Alexey Bataev7292c292016-04-25 12:22:29 +00001671 std::make_pair(".global_tid.", KmpInt32Ty),
1672 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1673 std::make_pair(".privates.",
1674 Context.VoidPtrTy.withConst().withRestrict()),
1675 std::make_pair(
1676 ".copy_fn.",
1677 Context.getPointerType(CopyFnType).withConst().withRestrict()),
1678 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
1679 std::make_pair(".lb.", KmpUInt64Ty),
1680 std::make_pair(".ub.", KmpUInt64Ty), std::make_pair(".st.", KmpInt64Ty),
1681 std::make_pair(".liter.", KmpInt32Ty),
Alexey Bataev49f6e782015-12-01 04:18:41 +00001682 std::make_pair(StringRef(), QualType()) // __context with shared vars
1683 };
1684 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1685 Params);
Alexey Bataev7292c292016-04-25 12:22:29 +00001686 // Mark this captured region as inlined, because we don't use outlined
1687 // function directly.
1688 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1689 AlwaysInlineAttr::CreateImplicit(
1690 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev49f6e782015-12-01 04:18:41 +00001691 break;
1692 }
Kelvin Li4a39add2016-07-05 05:00:15 +00001693 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001694 case OMPD_distribute_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001695 case OMPD_distribute_parallel_for:
Kelvin Li0e3bde82016-08-17 23:13:03 +00001696 case OMPD_teams_distribute:
1697 case OMPD_teams_distribute_simd: {
Carlo Bertolli9925f152016-06-27 14:55:37 +00001698 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1699 QualType KmpInt32PtrTy =
1700 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
1701 Sema::CapturedParamNameType Params[] = {
1702 std::make_pair(".global_tid.", KmpInt32PtrTy),
1703 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1704 std::make_pair(".previous.lb.", Context.getSizeType()),
1705 std::make_pair(".previous.ub.", Context.getSizeType()),
1706 std::make_pair(StringRef(), QualType()) // __context with shared vars
1707 };
1708 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1709 Params);
1710 break;
1711 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001712 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001713 case OMPD_taskyield:
1714 case OMPD_barrier:
1715 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001716 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001717 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001718 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001719 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001720 case OMPD_target_exit_data:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001721 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001722 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001723 case OMPD_declare_target:
1724 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +00001725 case OMPD_target_update:
Alexey Bataev9959db52014-05-06 10:08:46 +00001726 llvm_unreachable("OpenMP Directive is not allowed");
1727 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001728 llvm_unreachable("Unknown OpenMP directive");
1729 }
1730}
1731
Alexey Bataev3392d762016-02-16 11:18:12 +00001732static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00001733 Expr *CaptureExpr, bool WithInit,
1734 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001735 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00001736 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00001737 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00001738 QualType Ty = Init->getType();
1739 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1740 if (S.getLangOpts().CPlusPlus)
1741 Ty = C.getLValueReferenceType(Ty);
1742 else {
1743 Ty = C.getPointerType(Ty);
1744 ExprResult Res =
1745 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1746 if (!Res.isUsable())
1747 return nullptr;
1748 Init = Res.get();
1749 }
Alexey Bataev61205072016-03-02 04:57:40 +00001750 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00001751 }
1752 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001753 if (!WithInit)
1754 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
Alexey Bataev4244be22016-02-11 05:35:55 +00001755 S.CurContext->addHiddenDecl(CED);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001756 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false,
1757 /*TypeMayContainAuto=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00001758 return CED;
1759}
1760
Alexey Bataev61205072016-03-02 04:57:40 +00001761static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
1762 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00001763 OMPCapturedExprDecl *CD;
1764 if (auto *VD = S.IsOpenMPCapturedDecl(D))
1765 CD = cast<OMPCapturedExprDecl>(VD);
1766 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00001767 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
1768 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001769 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00001770 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00001771}
1772
Alexey Bataev5a3af132016-03-29 08:58:54 +00001773static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
1774 if (!Ref) {
1775 auto *CD =
1776 buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."),
1777 CaptureExpr, /*WithInit=*/true, /*AsExpression=*/true);
1778 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1779 CaptureExpr->getExprLoc());
1780 }
1781 ExprResult Res = Ref;
1782 if (!S.getLangOpts().CPlusPlus &&
1783 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
1784 Ref->getType()->isPointerType())
1785 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
1786 if (!Res.isUsable())
1787 return ExprError();
1788 return CaptureExpr->isGLValue() ? Res : S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00001789}
1790
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001791StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1792 ArrayRef<OMPClause *> Clauses) {
1793 if (!S.isUsable()) {
1794 ActOnCapturedRegionError();
1795 return StmtError();
1796 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001797
1798 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001799 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001800 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001801 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001802 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001803 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001804 Clause->getClauseKind() == OMPC_copyprivate ||
1805 (getLangOpts().OpenMPUseTLS &&
1806 getASTContext().getTargetInfo().isTLSSupported() &&
1807 Clause->getClauseKind() == OMPC_copyin)) {
1808 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001809 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001810 for (auto *VarRef : Clause->children()) {
1811 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001812 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001813 }
1814 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001815 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001816 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev040d5402015-05-12 08:35:28 +00001817 // Mark all variables in private list clauses as used in inner region.
1818 // Required for proper codegen of combined directives.
1819 // TODO: add processing for other clauses.
Alexey Bataev3392d762016-02-16 11:18:12 +00001820 if (auto *C = OMPClauseWithPreInit::get(Clause)) {
Alexey Bataev005248a2016-02-25 05:25:57 +00001821 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
1822 for (auto *D : DS->decls())
Alexey Bataev3392d762016-02-16 11:18:12 +00001823 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
1824 }
Alexey Bataev4244be22016-02-11 05:35:55 +00001825 }
Alexey Bataev005248a2016-02-25 05:25:57 +00001826 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
1827 if (auto *E = C->getPostUpdateExpr())
1828 MarkDeclarationsReferencedInExpr(E);
1829 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001830 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001831 if (Clause->getClauseKind() == OMPC_schedule)
1832 SC = cast<OMPScheduleClause>(Clause);
1833 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001834 OC = cast<OMPOrderedClause>(Clause);
1835 else if (Clause->getClauseKind() == OMPC_linear)
1836 LCs.push_back(cast<OMPLinearClause>(Clause));
1837 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001838 bool ErrorFound = false;
1839 // OpenMP, 2.7.1 Loop Construct, Restrictions
1840 // The nonmonotonic modifier cannot be specified if an ordered clause is
1841 // specified.
1842 if (SC &&
1843 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1844 SC->getSecondScheduleModifier() ==
1845 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1846 OC) {
1847 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1848 ? SC->getFirstScheduleModifierLoc()
1849 : SC->getSecondScheduleModifierLoc(),
1850 diag::err_omp_schedule_nonmonotonic_ordered)
1851 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1852 ErrorFound = true;
1853 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001854 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1855 for (auto *C : LCs) {
1856 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1857 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1858 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001859 ErrorFound = true;
1860 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001861 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1862 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1863 OC->getNumForLoops()) {
1864 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1865 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1866 ErrorFound = true;
1867 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001868 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001869 ActOnCapturedRegionError();
1870 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001871 }
1872 return ActOnCapturedRegionEnd(S.get());
1873}
1874
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001875static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1876 OpenMPDirectiveKind CurrentRegion,
1877 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001878 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001879 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001880 // Allowed nesting of constructs
1881 // +------------------+-----------------+------------------------------------+
1882 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1883 // +------------------+-----------------+------------------------------------+
1884 // | parallel | parallel | * |
1885 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001886 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001887 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001888 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001889 // | parallel | simd | * |
1890 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001891 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001892 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001893 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001894 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001895 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001896 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001897 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001898 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001899 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001900 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001901 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001902 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001903 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001904 // | parallel | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001905 // | parallel | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001906 // | parallel | target parallel | * |
1907 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001908 // | parallel | target enter | * |
1909 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001910 // | parallel | target exit | * |
1911 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001912 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001913 // | parallel | cancellation | |
1914 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001915 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001916 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001917 // | parallel | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00001918 // | parallel | distribute | + |
1919 // | parallel | distribute | + |
1920 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00001921 // | parallel | distribute | + |
1922 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00001923 // | parallel | distribute simd | + |
Kelvin Li986330c2016-07-20 22:57:10 +00001924 // | parallel | target simd | * |
Kelvin Li02532872016-08-05 14:37:37 +00001925 // | parallel | teams distribute| + |
Kelvin Li0e3bde82016-08-17 23:13:03 +00001926 // | parallel | teams distribute| |
1927 // | | simd | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001928 // +------------------+-----------------+------------------------------------+
1929 // | for | parallel | * |
1930 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001931 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001932 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001933 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001934 // | for | simd | * |
1935 // | for | sections | + |
1936 // | for | section | + |
1937 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001938 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001939 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001940 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001941 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001942 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001943 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001944 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001945 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001946 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001947 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001948 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001949 // | for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001950 // | for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001951 // | for | target parallel | * |
1952 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001953 // | for | target enter | * |
1954 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001955 // | for | target exit | * |
1956 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001957 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001958 // | for | cancellation | |
1959 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001960 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001961 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001962 // | for | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00001963 // | for | distribute | + |
1964 // | for | distribute | + |
1965 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00001966 // | for | distribute | + |
1967 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00001968 // | for | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00001969 // | for | target parallel | + |
1970 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00001971 // | for | target simd | * |
Kelvin Li02532872016-08-05 14:37:37 +00001972 // | for | teams distribute| + |
Kelvin Li0e3bde82016-08-17 23:13:03 +00001973 // | for | teams distribute| |
1974 // | | simd | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001975 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001976 // | master | parallel | * |
1977 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001978 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001979 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001980 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001981 // | master | simd | * |
1982 // | master | sections | + |
1983 // | master | section | + |
1984 // | master | single | + |
1985 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001986 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001987 // | master |parallel sections| * |
1988 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001989 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001990 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001991 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001992 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001993 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001994 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001995 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001996 // | master | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001997 // | master | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001998 // | master | target parallel | * |
1999 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002000 // | master | target enter | * |
2001 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002002 // | master | target exit | * |
2003 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002004 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002005 // | master | cancellation | |
2006 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002007 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002008 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002009 // | master | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002010 // | master | distribute | + |
2011 // | master | distribute | + |
2012 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002013 // | master | distribute | + |
2014 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002015 // | master | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002016 // | master | target parallel | + |
2017 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002018 // | master | target simd | * |
Kelvin Li02532872016-08-05 14:37:37 +00002019 // | master | teams distribute| + |
Kelvin Li0e3bde82016-08-17 23:13:03 +00002020 // | master | teams distribute| |
2021 // | | simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002022 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002023 // | critical | parallel | * |
2024 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002025 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002026 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002027 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002028 // | critical | simd | * |
2029 // | critical | sections | + |
2030 // | critical | section | + |
2031 // | critical | single | + |
2032 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002033 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002034 // | critical |parallel sections| * |
2035 // | critical | task | * |
2036 // | critical | taskyield | * |
2037 // | critical | barrier | + |
2038 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002039 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002040 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002041 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002042 // | critical | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002043 // | critical | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002044 // | critical | target parallel | * |
2045 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002046 // | critical | target enter | * |
2047 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002048 // | critical | target exit | * |
2049 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002050 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002051 // | critical | cancellation | |
2052 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002053 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002054 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002055 // | critical | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002056 // | critical | distribute | + |
2057 // | critical | distribute | + |
2058 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002059 // | critical | distribute | + |
2060 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002061 // | critical | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002062 // | critical | target parallel | + |
2063 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002064 // | critical | target simd | * |
Kelvin Li02532872016-08-05 14:37:37 +00002065 // | critical | teams distribute| + |
Kelvin Li0e3bde82016-08-17 23:13:03 +00002066 // | critical | teams distribute| |
2067 // | | simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002068 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002069 // | simd | parallel | |
2070 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002071 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00002072 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002073 // | simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002074 // | simd | simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002075 // | simd | sections | |
2076 // | simd | section | |
2077 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002078 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002079 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002080 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002081 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00002082 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002083 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002084 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002085 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00002086 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002087 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002088 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002089 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002090 // | simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002091 // | simd | target parallel | |
2092 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002093 // | simd | target enter | |
2094 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002095 // | simd | target exit | |
2096 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002097 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002098 // | simd | cancellation | |
2099 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002100 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002101 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002102 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002103 // | simd | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002104 // | simd | distribute | |
2105 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002106 // | simd | distribute | |
2107 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002108 // | simd | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002109 // | simd | target parallel | |
2110 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002111 // | simd | target simd | |
Kelvin Li02532872016-08-05 14:37:37 +00002112 // | simd | teams distribute| |
Kelvin Li0e3bde82016-08-17 23:13:03 +00002113 // | simd | teams distribute| |
2114 // | | simd | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002115 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00002116 // | for simd | parallel | |
2117 // | for simd | for | |
2118 // | for simd | for simd | |
2119 // | for simd | master | |
2120 // | for simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002121 // | for simd | simd | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002122 // | for simd | sections | |
2123 // | for simd | section | |
2124 // | for simd | single | |
2125 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002126 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002127 // | for simd |parallel sections| |
2128 // | for simd | task | |
2129 // | for simd | taskyield | |
2130 // | for simd | barrier | |
2131 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002132 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002133 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002134 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002135 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002136 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002137 // | for simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002138 // | for simd | target parallel | |
2139 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002140 // | for simd | target enter | |
2141 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002142 // | for simd | target exit | |
2143 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002144 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002145 // | for simd | cancellation | |
2146 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002147 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002148 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002149 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002150 // | for simd | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002151 // | for simd | distribute | |
2152 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002153 // | for simd | distribute | |
2154 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002155 // | for simd | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002156 // | for simd | target parallel | |
2157 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002158 // | for simd | target simd | |
Kelvin Li02532872016-08-05 14:37:37 +00002159 // | for simd | teams distribute| |
Kelvin Li0e3bde82016-08-17 23:13:03 +00002160 // | for simd | teams distribute| |
2161 // | | simd | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002162 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00002163 // | parallel for simd| parallel | |
2164 // | parallel for simd| for | |
2165 // | parallel for simd| for simd | |
2166 // | parallel for simd| master | |
2167 // | parallel for simd| critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002168 // | parallel for simd| simd | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002169 // | parallel for simd| sections | |
2170 // | parallel for simd| section | |
2171 // | parallel for simd| single | |
2172 // | parallel for simd| parallel for | |
2173 // | parallel for simd|parallel for simd| |
2174 // | parallel for simd|parallel sections| |
2175 // | parallel for simd| task | |
2176 // | parallel for simd| taskyield | |
2177 // | parallel for simd| barrier | |
2178 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002179 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002180 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002181 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002182 // | parallel for simd| atomic | |
2183 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002184 // | parallel for simd| target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002185 // | parallel for simd| target parallel | |
2186 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002187 // | parallel for simd| target enter | |
2188 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002189 // | parallel for simd| target exit | |
2190 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002191 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002192 // | parallel for simd| cancellation | |
2193 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002194 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002195 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002196 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002197 // | parallel for simd| distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002198 // | parallel for simd| distribute | |
2199 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002200 // | parallel for simd| distribute | |
2201 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002202 // | parallel for simd| distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002203 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002204 // | parallel for simd| target simd | |
Kelvin Li02532872016-08-05 14:37:37 +00002205 // | parallel for simd| teams distribute| |
Kelvin Li0e3bde82016-08-17 23:13:03 +00002206 // | parallel for simd| teams distribute| |
2207 // | | simd | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002208 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002209 // | sections | parallel | * |
2210 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002211 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002212 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002213 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002214 // | sections | simd | * |
2215 // | sections | sections | + |
2216 // | sections | section | * |
2217 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002218 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002219 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002220 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002221 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002222 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002223 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002224 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002225 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002226 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002227 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002228 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002229 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002230 // | sections | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002231 // | sections | target parallel | * |
2232 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002233 // | sections | target enter | * |
2234 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002235 // | sections | target exit | * |
2236 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002237 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002238 // | sections | cancellation | |
2239 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002240 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002241 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002242 // | sections | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002243 // | sections | distribute | + |
2244 // | sections | distribute | + |
2245 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002246 // | sections | distribute | + |
2247 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002248 // | sections | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002249 // | sections | target parallel | + |
2250 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002251 // | sections | target simd | * |
Kelvin Li0e3bde82016-08-17 23:13:03 +00002252 // | sections | teams distribute| |
2253 // | | simd | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002254 // +------------------+-----------------+------------------------------------+
2255 // | section | parallel | * |
2256 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002257 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002258 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002259 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002260 // | section | simd | * |
2261 // | section | sections | + |
2262 // | section | section | + |
2263 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002264 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002265 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002266 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002267 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002268 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002269 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002270 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002271 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002272 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002273 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002274 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002275 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002276 // | section | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002277 // | section | target parallel | * |
2278 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002279 // | section | target enter | * |
2280 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002281 // | section | target exit | * |
2282 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002283 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002284 // | section | cancellation | |
2285 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002286 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002287 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002288 // | section | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002289 // | section | distribute | + |
2290 // | section | distribute | + |
2291 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002292 // | section | distribute | + |
2293 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002294 // | section | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002295 // | section | target parallel | + |
2296 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002297 // | section | target simd | * |
Kelvin Li02532872016-08-05 14:37:37 +00002298 // | section | teams distrubte | + |
Kelvin Li0e3bde82016-08-17 23:13:03 +00002299 // | section | teams distribute| |
2300 // | | simd | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002301 // +------------------+-----------------+------------------------------------+
2302 // | single | parallel | * |
2303 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002304 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002305 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002306 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002307 // | single | simd | * |
2308 // | single | sections | + |
2309 // | single | section | + |
2310 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002311 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002312 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002313 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002314 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002315 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002316 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002317 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002318 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002319 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002320 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002321 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002322 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002323 // | single | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002324 // | single | target parallel | * |
2325 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002326 // | single | target enter | * |
2327 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002328 // | single | target exit | * |
2329 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002330 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002331 // | single | cancellation | |
2332 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002333 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002334 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002335 // | single | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002336 // | single | distribute | + |
2337 // | single | distribute | + |
2338 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002339 // | single | distribute | + |
2340 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002341 // | single | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002342 // | single | target parallel | + |
2343 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002344 // | single | target simd | * |
Kelvin Li02532872016-08-05 14:37:37 +00002345 // | single | teams distrubte | + |
Kelvin Li0e3bde82016-08-17 23:13:03 +00002346 // | single | teams distribute| |
2347 // | | simd | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002348 // +------------------+-----------------+------------------------------------+
2349 // | parallel for | parallel | * |
2350 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002351 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002352 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002353 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002354 // | parallel for | simd | * |
2355 // | parallel for | sections | + |
2356 // | parallel for | section | + |
2357 // | parallel for | single | + |
2358 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002359 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002360 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002361 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002362 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002363 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002364 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002365 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002366 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002367 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002368 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002369 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002370 // | parallel for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002371 // | parallel for | target parallel | * |
2372 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002373 // | parallel for | target enter | * |
2374 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002375 // | parallel for | target exit | * |
2376 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002377 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002378 // | parallel for | cancellation | |
2379 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002380 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002381 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002382 // | parallel for | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002383 // | parallel for | distribute | + |
2384 // | parallel for | distribute | + |
2385 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002386 // | parallel for | distribute | + |
2387 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002388 // | parallel for | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002389 // | parallel for | target parallel | + |
2390 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002391 // | parallel for | target simd | * |
Kelvin Li02532872016-08-05 14:37:37 +00002392 // | parallel for | teams distribute| + |
Kelvin Li0e3bde82016-08-17 23:13:03 +00002393 // | parallel for | teams distribute| |
2394 // | | simd | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002395 // +------------------+-----------------+------------------------------------+
2396 // | parallel sections| parallel | * |
2397 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002398 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002399 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002400 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002401 // | parallel sections| simd | * |
2402 // | parallel sections| sections | + |
2403 // | parallel sections| section | * |
2404 // | parallel sections| single | + |
2405 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002406 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002407 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002408 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002409 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002410 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002411 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002412 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002413 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002414 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002415 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002416 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002417 // | parallel sections| target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002418 // | parallel sections| target parallel | * |
2419 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002420 // | parallel sections| target enter | * |
2421 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002422 // | parallel sections| target exit | * |
2423 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002424 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002425 // | parallel sections| cancellation | |
2426 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002427 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002428 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002429 // | parallel sections| taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002430 // | parallel sections| distribute | + |
2431 // | parallel sections| distribute | + |
2432 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002433 // | parallel sections| distribute | + |
2434 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002435 // | parallel sections| distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002436 // | parallel sections| target parallel | + |
2437 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002438 // | parallel sections| target simd | * |
Kelvin Li02532872016-08-05 14:37:37 +00002439 // | parallel sections| teams distribute| + |
Kelvin Li0e3bde82016-08-17 23:13:03 +00002440 // | parallel sections| teams distribute| |
2441 // | | simd | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002442 // +------------------+-----------------+------------------------------------+
2443 // | task | parallel | * |
2444 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002445 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002446 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002447 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002448 // | task | simd | * |
2449 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002450 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002451 // | task | single | + |
2452 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002453 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002454 // | task |parallel sections| * |
2455 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002456 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002457 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002458 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002459 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002460 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002461 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002462 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002463 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002464 // | task | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002465 // | task | target parallel | * |
2466 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002467 // | task | target enter | * |
2468 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002469 // | task | target exit | * |
2470 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002471 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002472 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002473 // | | point | ! |
2474 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002475 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002476 // | task | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002477 // | task | distribute | + |
2478 // | task | distribute | + |
2479 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002480 // | task | distribute | + |
2481 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002482 // | task | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002483 // | task | target parallel | + |
2484 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002485 // | task | target simd | * |
Kelvin Li02532872016-08-05 14:37:37 +00002486 // | task | teams distribute| + |
Kelvin Li0e3bde82016-08-17 23:13:03 +00002487 // | task | teams distribute| |
2488 // | | simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002489 // +------------------+-----------------+------------------------------------+
2490 // | ordered | parallel | * |
2491 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002492 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002493 // | ordered | master | * |
2494 // | ordered | critical | * |
2495 // | ordered | simd | * |
2496 // | ordered | sections | + |
2497 // | ordered | section | + |
2498 // | ordered | single | + |
2499 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002500 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002501 // | ordered |parallel sections| * |
2502 // | ordered | task | * |
2503 // | ordered | taskyield | * |
2504 // | ordered | barrier | + |
2505 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002506 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002507 // | ordered | flush | * |
2508 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002509 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002510 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002511 // | ordered | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002512 // | ordered | target parallel | * |
2513 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002514 // | ordered | target enter | * |
2515 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002516 // | ordered | target exit | * |
2517 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002518 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002519 // | ordered | cancellation | |
2520 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002521 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002522 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002523 // | ordered | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002524 // | ordered | distribute | + |
2525 // | ordered | distribute | + |
2526 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002527 // | ordered | distribute | + |
2528 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002529 // | ordered | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002530 // | ordered | target parallel | + |
2531 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002532 // | ordered | target simd | * |
Kelvin Li02532872016-08-05 14:37:37 +00002533 // | ordered | teams distribute| + |
Kelvin Li0e3bde82016-08-17 23:13:03 +00002534 // | ordered | teams distribute| |
2535 // | | simd | + |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002536 // +------------------+-----------------+------------------------------------+
2537 // | atomic | parallel | |
2538 // | atomic | for | |
2539 // | atomic | for simd | |
2540 // | atomic | master | |
2541 // | atomic | critical | |
2542 // | atomic | simd | |
2543 // | atomic | sections | |
2544 // | atomic | section | |
2545 // | atomic | single | |
2546 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002547 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002548 // | atomic |parallel sections| |
2549 // | atomic | task | |
2550 // | atomic | taskyield | |
2551 // | atomic | barrier | |
2552 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002553 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002554 // | atomic | flush | |
2555 // | atomic | ordered | |
2556 // | atomic | atomic | |
2557 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002558 // | atomic | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002559 // | atomic | target parallel | |
2560 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002561 // | atomic | target enter | |
2562 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002563 // | atomic | target exit | |
2564 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002565 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002566 // | atomic | cancellation | |
2567 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002568 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002569 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002570 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002571 // | atomic | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002572 // | atomic | distribute | |
2573 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002574 // | atomic | distribute | |
2575 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002576 // | atomic | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002577 // | atomic | target parallel | |
2578 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002579 // | atomic | target simd | |
Kelvin Li02532872016-08-05 14:37:37 +00002580 // | atomic | teams distribute| |
Kelvin Li0e3bde82016-08-17 23:13:03 +00002581 // | atomic | teams distribute| |
2582 // | | simd | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002583 // +------------------+-----------------+------------------------------------+
2584 // | target | parallel | * |
2585 // | target | for | * |
2586 // | target | for simd | * |
2587 // | target | master | * |
2588 // | target | critical | * |
2589 // | target | simd | * |
2590 // | target | sections | * |
2591 // | target | section | * |
2592 // | target | single | * |
2593 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002594 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002595 // | target |parallel sections| * |
2596 // | target | task | * |
2597 // | target | taskyield | * |
2598 // | target | barrier | * |
2599 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002600 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002601 // | target | flush | * |
2602 // | target | ordered | * |
2603 // | target | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002604 // | target | target | |
2605 // | target | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002606 // | target | target parallel | |
2607 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002608 // | target | target enter | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002609 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002610 // | target | target exit | |
Samuel Antao72590762016-01-19 20:04:50 +00002611 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002612 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002613 // | target | cancellation | |
2614 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002615 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002616 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002617 // | target | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002618 // | target | distribute | + |
2619 // | target | distribute | + |
2620 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002621 // | target | distribute | + |
2622 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002623 // | target | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002624 // | target | target parallel | |
2625 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002626 // | target | target simd | |
Kelvin Li02532872016-08-05 14:37:37 +00002627 // | target | teams distribute| |
Kelvin Li0e3bde82016-08-17 23:13:03 +00002628 // | target | teams distribute| |
2629 // | | simd | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002630 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002631 // | target parallel | parallel | * |
2632 // | target parallel | for | * |
2633 // | target parallel | for simd | * |
2634 // | target parallel | master | * |
2635 // | target parallel | critical | * |
2636 // | target parallel | simd | * |
2637 // | target parallel | sections | * |
2638 // | target parallel | section | * |
2639 // | target parallel | single | * |
2640 // | target parallel | parallel for | * |
2641 // | target parallel |parallel for simd| * |
2642 // | target parallel |parallel sections| * |
2643 // | target parallel | task | * |
2644 // | target parallel | taskyield | * |
2645 // | target parallel | barrier | * |
2646 // | target parallel | taskwait | * |
2647 // | target parallel | taskgroup | * |
2648 // | target parallel | flush | * |
2649 // | target parallel | ordered | * |
2650 // | target parallel | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002651 // | target parallel | target | |
2652 // | target parallel | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002653 // | target parallel | target parallel | |
2654 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002655 // | target parallel | target enter | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002656 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002657 // | target parallel | target exit | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002658 // | | data | |
2659 // | target parallel | teams | |
2660 // | target parallel | cancellation | |
2661 // | | point | ! |
2662 // | target parallel | cancel | ! |
2663 // | target parallel | taskloop | * |
2664 // | target parallel | taskloop simd | * |
2665 // | target parallel | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002666 // | target parallel | distribute | |
2667 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002668 // | target parallel | distribute | |
2669 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002670 // | target parallel | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002671 // | target parallel | target parallel | |
2672 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002673 // | target parallel | target simd | |
Kelvin Li02532872016-08-05 14:37:37 +00002674 // | target parallel | teams distribute| + |
Kelvin Li0e3bde82016-08-17 23:13:03 +00002675 // | target parallel | teams distribute| + |
2676 // | | simd | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002677 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002678 // | target parallel | parallel | * |
2679 // | for | | |
2680 // | target parallel | for | * |
2681 // | for | | |
2682 // | target parallel | for simd | * |
2683 // | for | | |
2684 // | target parallel | master | * |
2685 // | for | | |
2686 // | target parallel | critical | * |
2687 // | for | | |
2688 // | target parallel | simd | * |
2689 // | for | | |
2690 // | target parallel | sections | * |
2691 // | for | | |
2692 // | target parallel | section | * |
2693 // | for | | |
2694 // | target parallel | single | * |
2695 // | for | | |
2696 // | target parallel | parallel for | * |
2697 // | for | | |
2698 // | target parallel |parallel for simd| * |
2699 // | for | | |
2700 // | target parallel |parallel sections| * |
2701 // | for | | |
2702 // | target parallel | task | * |
2703 // | for | | |
2704 // | target parallel | taskyield | * |
2705 // | for | | |
2706 // | target parallel | barrier | * |
2707 // | for | | |
2708 // | target parallel | taskwait | * |
2709 // | for | | |
2710 // | target parallel | taskgroup | * |
2711 // | for | | |
2712 // | target parallel | flush | * |
2713 // | for | | |
2714 // | target parallel | ordered | * |
2715 // | for | | |
2716 // | target parallel | atomic | * |
2717 // | for | | |
2718 // | target parallel | target | |
2719 // | for | | |
2720 // | target parallel | target parallel | |
2721 // | for | | |
2722 // | target parallel | target parallel | |
2723 // | for | for | |
2724 // | target parallel | target enter | |
2725 // | for | data | |
2726 // | target parallel | target exit | |
2727 // | for | data | |
2728 // | target parallel | teams | |
2729 // | for | | |
2730 // | target parallel | cancellation | |
2731 // | for | point | ! |
2732 // | target parallel | cancel | ! |
2733 // | for | | |
2734 // | target parallel | taskloop | * |
2735 // | for | | |
2736 // | target parallel | taskloop simd | * |
2737 // | for | | |
2738 // | target parallel | distribute | |
2739 // | for | | |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002740 // | target parallel | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002741 // | for | parallel for | |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002742 // | target parallel | distribute | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002743 // | for |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002744 // | target parallel | distribute simd | |
2745 // | for | | |
Kelvin Lia579b912016-07-14 02:54:56 +00002746 // | target parallel | target parallel | |
2747 // | for | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002748 // | target parallel | target simd | |
2749 // | for | | |
Kelvin Li02532872016-08-05 14:37:37 +00002750 // | target parallel | teams distribute| |
2751 // | for | | |
Kelvin Li0e3bde82016-08-17 23:13:03 +00002752 // | target parallel | teams distribute| |
2753 // | for | simd | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002754 // +------------------+-----------------+------------------------------------+
Alexey Bataev13314bf2014-10-09 04:18:56 +00002755 // | teams | parallel | * |
2756 // | teams | for | + |
2757 // | teams | for simd | + |
2758 // | teams | master | + |
2759 // | teams | critical | + |
2760 // | teams | simd | + |
2761 // | teams | sections | + |
2762 // | teams | section | + |
2763 // | teams | single | + |
2764 // | teams | parallel for | * |
2765 // | teams |parallel for simd| * |
2766 // | teams |parallel sections| * |
2767 // | teams | task | + |
2768 // | teams | taskyield | + |
2769 // | teams | barrier | + |
2770 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002771 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002772 // | teams | flush | + |
2773 // | teams | ordered | + |
2774 // | teams | atomic | + |
2775 // | teams | target | + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002776 // | teams | target parallel | + |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002777 // | teams | target parallel | + |
2778 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002779 // | teams | target enter | + |
2780 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002781 // | teams | target exit | + |
2782 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002783 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002784 // | teams | cancellation | |
2785 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002786 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002787 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002788 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002789 // | teams | distribute | ! |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002790 // | teams | distribute | ! |
2791 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002792 // | teams | distribute | ! |
2793 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002794 // | teams | distribute simd | ! |
Kelvin Lia579b912016-07-14 02:54:56 +00002795 // | teams | target parallel | + |
2796 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002797 // | teams | target simd | + |
Kelvin Li02532872016-08-05 14:37:37 +00002798 // | teams | teams distribute| + |
Kelvin Li0e3bde82016-08-17 23:13:03 +00002799 // | teams | teams distribute| + |
2800 // | | simd | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002801 // +------------------+-----------------+------------------------------------+
2802 // | taskloop | parallel | * |
2803 // | taskloop | for | + |
2804 // | taskloop | for simd | + |
2805 // | taskloop | master | + |
2806 // | taskloop | critical | * |
2807 // | taskloop | simd | * |
2808 // | taskloop | sections | + |
2809 // | taskloop | section | + |
2810 // | taskloop | single | + |
2811 // | taskloop | parallel for | * |
2812 // | taskloop |parallel for simd| * |
2813 // | taskloop |parallel sections| * |
2814 // | taskloop | task | * |
2815 // | taskloop | taskyield | * |
2816 // | taskloop | barrier | + |
2817 // | taskloop | taskwait | * |
2818 // | taskloop | taskgroup | * |
2819 // | taskloop | flush | * |
2820 // | taskloop | ordered | + |
2821 // | taskloop | atomic | * |
2822 // | taskloop | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002823 // | taskloop | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002824 // | taskloop | target parallel | * |
2825 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002826 // | taskloop | target enter | * |
2827 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002828 // | taskloop | target exit | * |
2829 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002830 // | taskloop | teams | + |
2831 // | taskloop | cancellation | |
2832 // | | point | |
2833 // | taskloop | cancel | |
2834 // | taskloop | taskloop | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002835 // | taskloop | distribute | + |
2836 // | taskloop | distribute | + |
2837 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002838 // | taskloop | distribute | + |
2839 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002840 // | taskloop | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002841 // | taskloop | target parallel | * |
2842 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002843 // | taskloop | target simd | * |
Kelvin Li02532872016-08-05 14:37:37 +00002844 // | taskloop | teams distribute| + |
Kelvin Li0e3bde82016-08-17 23:13:03 +00002845 // | taskloop | teams distribute| + |
2846 // | | simd | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002847 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002848 // | taskloop simd | parallel | |
2849 // | taskloop simd | for | |
2850 // | taskloop simd | for simd | |
2851 // | taskloop simd | master | |
2852 // | taskloop simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002853 // | taskloop simd | simd | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002854 // | taskloop simd | sections | |
2855 // | taskloop simd | section | |
2856 // | taskloop simd | single | |
2857 // | taskloop simd | parallel for | |
2858 // | taskloop simd |parallel for simd| |
2859 // | taskloop simd |parallel sections| |
2860 // | taskloop simd | task | |
2861 // | taskloop simd | taskyield | |
2862 // | taskloop simd | barrier | |
2863 // | taskloop simd | taskwait | |
2864 // | taskloop simd | taskgroup | |
2865 // | taskloop simd | flush | |
2866 // | taskloop simd | ordered | + (with simd clause) |
2867 // | taskloop simd | atomic | |
2868 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002869 // | taskloop simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002870 // | taskloop simd | target parallel | |
2871 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002872 // | taskloop simd | target enter | |
2873 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002874 // | taskloop simd | target exit | |
2875 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002876 // | taskloop simd | teams | |
2877 // | taskloop simd | cancellation | |
2878 // | | point | |
2879 // | taskloop simd | cancel | |
2880 // | taskloop simd | taskloop | |
2881 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002882 // | taskloop simd | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002883 // | taskloop simd | distribute | |
2884 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002885 // | taskloop simd | distribute | |
2886 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002887 // | taskloop simd | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002888 // | taskloop simd | target parallel | |
2889 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002890 // | taskloop simd | target simd | |
Kelvin Li02532872016-08-05 14:37:37 +00002891 // | taskloop simd | teams distribute| |
Kelvin Li0e3bde82016-08-17 23:13:03 +00002892 // | taskloop simd | teams distribute| |
2893 // | | simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002894 // +------------------+-----------------+------------------------------------+
2895 // | distribute | parallel | * |
2896 // | distribute | for | * |
2897 // | distribute | for simd | * |
2898 // | distribute | master | * |
2899 // | distribute | critical | * |
2900 // | distribute | simd | * |
2901 // | distribute | sections | * |
2902 // | distribute | section | * |
2903 // | distribute | single | * |
2904 // | distribute | parallel for | * |
2905 // | distribute |parallel for simd| * |
2906 // | distribute |parallel sections| * |
2907 // | distribute | task | * |
2908 // | distribute | taskyield | * |
2909 // | distribute | barrier | * |
2910 // | distribute | taskwait | * |
2911 // | distribute | taskgroup | * |
2912 // | distribute | flush | * |
2913 // | distribute | ordered | + |
2914 // | distribute | atomic | * |
2915 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002916 // | distribute | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002917 // | distribute | target parallel | |
2918 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002919 // | distribute | target enter | |
2920 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002921 // | distribute | target exit | |
2922 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002923 // | distribute | teams | |
2924 // | distribute | cancellation | + |
2925 // | | point | |
2926 // | distribute | cancel | + |
2927 // | distribute | taskloop | * |
2928 // | distribute | taskloop simd | * |
2929 // | distribute | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002930 // | distribute | distribute | |
2931 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002932 // | distribute | distribute | |
2933 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002934 // | distribute | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002935 // | distribute | target parallel | |
2936 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002937 // | distribute | target simd | |
Kelvin Li02532872016-08-05 14:37:37 +00002938 // | distribute | teams distribute| |
Kelvin Li0e3bde82016-08-17 23:13:03 +00002939 // | distribute | teams distribute| |
2940 // | | simd | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002941 // +------------------+-----------------+------------------------------------+
2942 // | distribute | parallel | * |
2943 // | parallel for | | |
2944 // | distribute | for | * |
2945 // | parallel for | | |
2946 // | distribute | for simd | * |
2947 // | parallel for | | |
2948 // | distribute | master | * |
2949 // | parallel for | | |
2950 // | distribute | critical | * |
2951 // | parallel for | | |
2952 // | distribute | simd | * |
2953 // | parallel for | | |
2954 // | distribute | sections | * |
2955 // | parallel for | | |
2956 // | distribute | section | * |
2957 // | parallel for | | |
2958 // | distribute | single | * |
2959 // | parallel for | | |
2960 // | distribute | parallel for | * |
2961 // | parallel for | | |
2962 // | distribute |parallel for simd| * |
2963 // | parallel for | | |
2964 // | distribute |parallel sections| * |
2965 // | parallel for | | |
2966 // | distribute | task | * |
2967 // | parallel for | | |
2968 // | parallel for | | |
2969 // | distribute | taskyield | * |
2970 // | parallel for | | |
2971 // | distribute | barrier | * |
2972 // | parallel for | | |
2973 // | distribute | taskwait | * |
2974 // | parallel for | | |
2975 // | distribute | taskgroup | * |
2976 // | parallel for | | |
2977 // | distribute | flush | * |
2978 // | parallel for | | |
2979 // | distribute | ordered | + |
2980 // | parallel for | | |
2981 // | distribute | atomic | * |
2982 // | parallel for | | |
2983 // | distribute | target | |
2984 // | parallel for | | |
2985 // | distribute | target parallel | |
2986 // | parallel for | | |
2987 // | distribute | target parallel | |
2988 // | parallel for | for | |
2989 // | distribute | target enter | |
2990 // | parallel for | data | |
2991 // | distribute | target exit | |
2992 // | parallel for | data | |
2993 // | distribute | teams | |
2994 // | parallel for | | |
2995 // | distribute | cancellation | + |
2996 // | parallel for | point | |
2997 // | distribute | cancel | + |
2998 // | parallel for | | |
2999 // | distribute | taskloop | * |
3000 // | parallel for | | |
3001 // | distribute | taskloop simd | * |
3002 // | parallel for | | |
3003 // | distribute | distribute | |
3004 // | parallel for | | |
3005 // | distribute | distribute | |
3006 // | parallel for | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00003007 // | distribute | distribute | |
3008 // | parallel for |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00003009 // | distribute | distribute simd | |
3010 // | parallel for | | |
Kelvin Lia579b912016-07-14 02:54:56 +00003011 // | distribute | target parallel | |
3012 // | parallel for | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00003013 // | distribute | target simd | |
3014 // | parallel for | | |
Kelvin Li02532872016-08-05 14:37:37 +00003015 // | distribute | teams distribute| |
3016 // | parallel for | | |
Kelvin Li0e3bde82016-08-17 23:13:03 +00003017 // | distribute | teams distribute| |
3018 // | parallel for | simd | |
Kelvin Li4a39add2016-07-05 05:00:15 +00003019 // +------------------+-----------------+------------------------------------+
3020 // | distribute | parallel | * |
3021 // | parallel for simd| | |
3022 // | distribute | for | * |
3023 // | parallel for simd| | |
3024 // | distribute | for simd | * |
3025 // | parallel for simd| | |
3026 // | distribute | master | * |
3027 // | parallel for simd| | |
3028 // | distribute | critical | * |
3029 // | parallel for simd| | |
3030 // | distribute | simd | * |
3031 // | parallel for simd| | |
3032 // | distribute | sections | * |
3033 // | parallel for simd| | |
3034 // | distribute | section | * |
3035 // | parallel for simd| | |
3036 // | distribute | single | * |
3037 // | parallel for simd| | |
3038 // | distribute | parallel for | * |
3039 // | parallel for simd| | |
3040 // | distribute |parallel for simd| * |
3041 // | parallel for simd| | |
3042 // | distribute |parallel sections| * |
3043 // | parallel for simd| | |
3044 // | distribute | task | * |
3045 // | parallel for simd| | |
3046 // | distribute | taskyield | * |
3047 // | parallel for simd| | |
3048 // | distribute | barrier | * |
3049 // | parallel for simd| | |
3050 // | distribute | taskwait | * |
3051 // | parallel for simd| | |
3052 // | distribute | taskgroup | * |
3053 // | parallel for simd| | |
3054 // | distribute | flush | * |
3055 // | parallel for simd| | |
3056 // | distribute | ordered | + |
3057 // | parallel for simd| | |
3058 // | distribute | atomic | * |
3059 // | parallel for simd| | |
3060 // | distribute | target | |
3061 // | parallel for simd| | |
3062 // | distribute | target parallel | |
3063 // | parallel for simd| | |
3064 // | distribute | target parallel | |
3065 // | parallel for simd| for | |
3066 // | distribute | target enter | |
3067 // | parallel for simd| data | |
3068 // | distribute | target exit | |
3069 // | parallel for simd| data | |
3070 // | distribute | teams | |
3071 // | parallel for simd| | |
3072 // | distribute | cancellation | + |
3073 // | parallel for simd| point | |
3074 // | distribute | cancel | + |
3075 // | parallel for simd| | |
3076 // | distribute | taskloop | * |
3077 // | parallel for simd| | |
3078 // | distribute | taskloop simd | * |
3079 // | parallel for simd| | |
3080 // | distribute | distribute | |
3081 // | parallel for simd| | |
3082 // | distribute | distribute | * |
3083 // | parallel for simd| parallel for | |
3084 // | distribute | distribute | * |
3085 // | parallel for simd|parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00003086 // | distribute | distribute simd | * |
3087 // | parallel for simd| | |
Kelvin Lia579b912016-07-14 02:54:56 +00003088 // | distribute | target parallel | |
3089 // | parallel for simd| for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00003090 // | distribute | target simd | |
3091 // | parallel for simd| | |
Kelvin Li02532872016-08-05 14:37:37 +00003092 // | distribute | teams distribute| |
3093 // | parallel for simd| | |
Kelvin Li0e3bde82016-08-17 23:13:03 +00003094 // | distribute | teams distribute| |
3095 // | parallel for simd| simd | |
Kelvin Li787f3fc2016-07-06 04:45:38 +00003096 // +------------------+-----------------+------------------------------------+
3097 // | distribute simd | parallel | * |
3098 // | distribute simd | for | * |
3099 // | distribute simd | for simd | * |
3100 // | distribute simd | master | * |
3101 // | distribute simd | critical | * |
3102 // | distribute simd | simd | * |
3103 // | distribute simd | sections | * |
3104 // | distribute simd | section | * |
3105 // | distribute simd | single | * |
3106 // | distribute simd | parallel for | * |
3107 // | distribute simd |parallel for simd| * |
3108 // | distribute simd |parallel sections| * |
3109 // | distribute simd | task | * |
3110 // | distribute simd | taskyield | * |
3111 // | distribute simd | barrier | * |
3112 // | distribute simd | taskwait | * |
3113 // | distribute simd | taskgroup | * |
3114 // | distribute simd | flush | * |
3115 // | distribute simd | ordered | + |
3116 // | distribute simd | atomic | * |
3117 // | distribute simd | target | * |
3118 // | distribute simd | target parallel | * |
3119 // | distribute simd | target parallel | * |
3120 // | | for | |
3121 // | distribute simd | target enter | * |
3122 // | | data | |
3123 // | distribute simd | target exit | * |
3124 // | | data | |
3125 // | distribute simd | teams | * |
3126 // | distribute simd | cancellation | + |
3127 // | | point | |
3128 // | distribute simd | cancel | + |
3129 // | distribute simd | taskloop | * |
3130 // | distribute simd | taskloop simd | * |
3131 // | distribute simd | distribute | |
3132 // | distribute simd | distribute | * |
3133 // | | parallel for | |
3134 // | distribute simd | distribute | * |
3135 // | |parallel for simd| |
3136 // | distribute simd | distribute simd | * |
Kelvin Lia579b912016-07-14 02:54:56 +00003137 // | distribute simd | target parallel | * |
3138 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00003139 // | distribute simd | target simd | * |
Kelvin Li02532872016-08-05 14:37:37 +00003140 // | distribute simd | teams distribute| * |
Kelvin Li0e3bde82016-08-17 23:13:03 +00003141 // | distribute simd | teams distribute| |
3142 // | | simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00003143 // +------------------+-----------------+------------------------------------+
3144 // | target parallel | parallel | * |
3145 // | for simd | | |
3146 // | target parallel | for | * |
3147 // | for simd | | |
3148 // | target parallel | for simd | * |
3149 // | for simd | | |
3150 // | target parallel | master | * |
3151 // | for simd | | |
3152 // | target parallel | critical | * |
3153 // | for simd | | |
3154 // | target parallel | simd | ! |
3155 // | for simd | | |
3156 // | target parallel | sections | * |
3157 // | for simd | | |
3158 // | target parallel | section | * |
3159 // | for simd | | |
3160 // | target parallel | single | * |
3161 // | for simd | | |
3162 // | target parallel | parallel for | * |
3163 // | for simd | | |
3164 // | target parallel |parallel for simd| * |
3165 // | for simd | | |
3166 // | target parallel |parallel sections| * |
3167 // | for simd | | |
3168 // | target parallel | task | * |
3169 // | for simd | | |
3170 // | target parallel | taskyield | * |
3171 // | for simd | | |
3172 // | target parallel | barrier | * |
3173 // | for simd | | |
3174 // | target parallel | taskwait | * |
3175 // | for simd | | |
3176 // | target parallel | taskgroup | * |
3177 // | for simd | | |
3178 // | target parallel | flush | * |
3179 // | for simd | | |
3180 // | target parallel | ordered | + (with simd clause) |
3181 // | for simd | | |
3182 // | target parallel | atomic | * |
3183 // | for simd | | |
3184 // | target parallel | target | * |
3185 // | for simd | | |
3186 // | target parallel | target parallel | * |
3187 // | for simd | | |
3188 // | target parallel | target parallel | * |
3189 // | for simd | for | |
3190 // | target parallel | target enter | * |
3191 // | for simd | data | |
3192 // | target parallel | target exit | * |
3193 // | for simd | data | |
3194 // | target parallel | teams | * |
3195 // | for simd | | |
3196 // | target parallel | cancellation | * |
3197 // | for simd | point | |
3198 // | target parallel | cancel | * |
3199 // | for simd | | |
3200 // | target parallel | taskloop | * |
3201 // | for simd | | |
3202 // | target parallel | taskloop simd | * |
3203 // | for simd | | |
3204 // | target parallel | distribute | * |
3205 // | for simd | | |
3206 // | target parallel | distribute | * |
3207 // | for simd | parallel for | |
3208 // | target parallel | distribute | * |
3209 // | for simd |parallel for simd| |
3210 // | target parallel | distribute simd | * |
3211 // | for simd | | |
3212 // | target parallel | target parallel | * |
3213 // | for simd | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00003214 // | target parallel | target simd | * |
3215 // | for simd | | |
Kelvin Li02532872016-08-05 14:37:37 +00003216 // | target parallel | teams distribute| * |
3217 // | for simd | | |
Kelvin Li0e3bde82016-08-17 23:13:03 +00003218 // | target parallel | teams distribute| |
3219 // | for simd | simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00003220 // +------------------+-----------------+------------------------------------+
3221 // | target simd | parallel | |
3222 // | target simd | for | |
3223 // | target simd | for simd | |
3224 // | target simd | master | |
3225 // | target simd | critical | |
3226 // | target simd | simd | |
3227 // | target simd | sections | |
3228 // | target simd | section | |
3229 // | target simd | single | |
3230 // | target simd | parallel for | |
3231 // | target simd |parallel for simd| |
3232 // | target simd |parallel sections| |
3233 // | target simd | task | |
3234 // | target simd | taskyield | |
3235 // | target simd | barrier | |
3236 // | target simd | taskwait | |
3237 // | target simd | taskgroup | |
3238 // | target simd | flush | |
3239 // | target simd | ordered | + (with simd clause) |
3240 // | target simd | atomic | |
3241 // | target simd | target | |
3242 // | target simd | target parallel | |
3243 // | target simd | target parallel | |
3244 // | | for | |
3245 // | target simd | target enter | |
3246 // | | data | |
3247 // | target simd | target exit | |
3248 // | | data | |
3249 // | target simd | teams | |
3250 // | target simd | cancellation | |
3251 // | | point | |
3252 // | target simd | cancel | |
3253 // | target simd | taskloop | |
3254 // | target simd | taskloop simd | |
3255 // | target simd | distribute | |
3256 // | target simd | distribute | |
3257 // | | parallel for | |
3258 // | target simd | distribute | |
3259 // | |parallel for simd| |
3260 // | target simd | distribute simd | |
3261 // | target simd | target parallel | |
3262 // | | for simd | |
3263 // | target simd | target simd | |
Kelvin Li02532872016-08-05 14:37:37 +00003264 // | target simd | teams distribute| |
Kelvin Li0e3bde82016-08-17 23:13:03 +00003265 // | target simd | teams distribute| |
3266 // | | simd | |
Kelvin Li02532872016-08-05 14:37:37 +00003267 // +------------------+-----------------+------------------------------------+
3268 // | teams distribute | parallel | |
3269 // | teams distribute | for | |
3270 // | teams distribute | for simd | |
3271 // | teams distribute | master | |
3272 // | teams distribute | critical | |
3273 // | teams distribute | simd | |
3274 // | teams distribute | sections | |
3275 // | teams distribute | section | |
3276 // | teams distribute | single | |
3277 // | teams distribute | parallel for | |
3278 // | teams distribute |parallel for simd| |
3279 // | teams distribute |parallel sections| |
3280 // | teams distribute | task | |
3281 // | teams distribute | taskyield | |
3282 // | teams distribute | barrier | |
3283 // | teams distribute | taskwait | |
3284 // | teams distribute | taskgroup | |
3285 // | teams distribute | flush | |
3286 // | teams distribute | ordered | + (with simd clause) |
3287 // | teams distribute | atomic | |
3288 // | teams distribute | target | |
3289 // | teams distribute | target parallel | |
3290 // | teams distribute | target parallel | |
3291 // | | for | |
3292 // | teams distribute | target enter | |
3293 // | | data | |
3294 // | teams distribute | target exit | |
3295 // | | data | |
3296 // | teams distribute | teams | |
3297 // | teams distribute | cancellation | |
3298 // | | point | |
3299 // | teams distribute | cancel | |
3300 // | teams distribute | taskloop | |
3301 // | teams distribute | taskloop simd | |
3302 // | teams distribute | distribute | |
3303 // | teams distribute | distribute | |
3304 // | | parallel for | |
3305 // | teams distribute | distribute | |
3306 // | |parallel for simd| |
3307 // | teams distribute | distribute simd | |
3308 // | teams distribute | target parallel | |
3309 // | | for simd | |
3310 // | teams distribute | teams distribute| |
Kelvin Li0e3bde82016-08-17 23:13:03 +00003311 // | teams distribute | teams distribute| |
3312 // | | simd | |
3313 // +------------------+-----------------+------------------------------------+
3314 // | teams distribute | parallel | |
3315 // | simd | | |
3316 // | teams distribute | for | |
3317 // | simd | | |
3318 // | teams distribute | for simd | |
3319 // | simd | | |
3320 // | teams distribute | master | |
3321 // | simd | | |
3322 // | teams distribute | critical | |
3323 // | simd | | |
3324 // | teams distribute | simd | |
3325 // | simd | | |
3326 // | teams distribute | sections | |
3327 // | simd | | |
3328 // | teams distribute | section | |
3329 // | simd | | |
3330 // | teams distribute | single | |
3331 // | simd | | |
3332 // | teams distribute | parallel for | |
3333 // | simd | | |
3334 // | teams distribute |parallel for simd| |
3335 // | simd | | |
3336 // | teams distribute |parallel sections| |
3337 // | simd | | |
3338 // | teams distribute | task | |
3339 // | simd | | |
3340 // | teams distribute | taskyield | |
3341 // | simd | | |
3342 // | teams distribute | barrier | |
3343 // | simd | | |
3344 // | teams distribute | taskwait | |
3345 // | simd | | |
3346 // | teams distribute | taskgroup | |
3347 // | simd | | |
3348 // | teams distribute | flush | |
3349 // | simd | | |
3350 // | teams distribute | ordered | + (with simd clause) |
3351 // | simd | | |
3352 // | teams distribute | atomic | |
3353 // | simd | | |
3354 // | teams distribute | target | |
3355 // | simd | | |
3356 // | teams distribute | target parallel | |
3357 // | simd | | |
3358 // | teams distribute | target parallel | |
3359 // | simd | for | |
3360 // | teams distribute | target enter | |
3361 // | simd | data | |
3362 // | teams distribute | target exit | |
3363 // | simd | data | |
3364 // | teams distribute | teams | |
3365 // | simd | | |
3366 // | teams distribute | cancellation | |
3367 // | simd | point | |
3368 // | teams distribute | cancel | |
3369 // | simd | | |
3370 // | teams distribute | taskloop | |
3371 // | simd | | |
3372 // | teams distribute | taskloop simd | |
3373 // | simd | | |
3374 // | teams distribute | distribute | |
3375 // | simd | | |
3376 // | teams distribute | distribute | |
3377 // | simd | parallel for | |
3378 // | teams distribute | distribute | |
3379 // | simd |parallel for simd| |
3380 // | teams distribute | distribute simd | |
3381 // | simd | | |
3382 // | teams distribute | target parallel | |
3383 // | simd | for simd | |
3384 // | teams distribute | teams distribute| |
3385 // | simd | | |
3386 // | teams distribute | teams distribute| |
3387 // | simd | simd | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003388 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00003389 if (Stack->getCurScope()) {
3390 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003391 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003392 bool NestingProhibited = false;
3393 bool CloseNesting = true;
David Majnemer9d168222016-08-05 17:44:54 +00003394 bool OrphanSeen = false;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003395 enum {
3396 NoRecommend,
3397 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00003398 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003399 ShouldBeInTargetRegion,
3400 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003401 } Recommend = NoRecommend;
Kelvin Lifd8b5742016-07-01 14:30:25 +00003402 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003403 // OpenMP [2.16, Nesting of Regions]
3404 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003405 // OpenMP [2.8.1,simd Construct, Restrictions]
Kelvin Lifd8b5742016-07-01 14:30:25 +00003406 // An ordered construct with the simd clause is the only OpenMP
3407 // construct that can appear in the simd region.
David Majnemer9d168222016-08-05 17:44:54 +00003408 // Allowing a SIMD construct nested in another SIMD construct is an
Kelvin Lifd8b5742016-07-01 14:30:25 +00003409 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
3410 // message.
3411 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
3412 ? diag::err_omp_prohibited_region_simd
3413 : diag::warn_omp_nesting_simd);
3414 return CurrentRegion != OMPD_simd;
Alexey Bataev549210e2014-06-24 04:39:47 +00003415 }
Alexey Bataev0162e452014-07-22 10:10:35 +00003416 if (ParentRegion == OMPD_atomic) {
3417 // OpenMP [2.16, Nesting of Regions]
3418 // OpenMP constructs may not be nested inside an atomic region.
3419 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
3420 return true;
3421 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003422 if (CurrentRegion == OMPD_section) {
3423 // OpenMP [2.7.2, sections Construct, Restrictions]
3424 // Orphaned section directives are prohibited. That is, the section
3425 // directives must appear within the sections construct and must not be
3426 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003427 if (ParentRegion != OMPD_sections &&
3428 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003429 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
3430 << (ParentRegion != OMPD_unknown)
3431 << getOpenMPDirectiveName(ParentRegion);
3432 return true;
3433 }
3434 return false;
3435 }
Kelvin Li2b51f722016-07-26 04:32:50 +00003436 // Allow some constructs (except teams) to be orphaned (they could be
David Majnemer9d168222016-08-05 17:44:54 +00003437 // used in functions, called from OpenMP regions with the required
Kelvin Li2b51f722016-07-26 04:32:50 +00003438 // preconditions).
3439 if (ParentRegion == OMPD_unknown && !isOpenMPTeamsDirective(CurrentRegion))
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003440 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00003441 if (CurrentRegion == OMPD_cancellation_point ||
3442 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003443 // OpenMP [2.16, Nesting of Regions]
3444 // A cancellation point construct for which construct-type-clause is
3445 // taskgroup must be nested inside a task construct. A cancellation
3446 // point construct for which construct-type-clause is not taskgroup must
3447 // be closely nested inside an OpenMP construct that matches the type
3448 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00003449 // A cancel construct for which construct-type-clause is taskgroup must be
3450 // nested inside a task construct. A cancel construct for which
3451 // construct-type-clause is not taskgroup must be closely nested inside an
3452 // OpenMP construct that matches the type specified in
3453 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003454 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003455 !((CancelRegion == OMPD_parallel &&
3456 (ParentRegion == OMPD_parallel ||
3457 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00003458 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003459 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
3460 ParentRegion == OMPD_target_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003461 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
3462 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00003463 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
3464 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003465 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00003466 // OpenMP [2.16, Nesting of Regions]
3467 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003468 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00003469 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003470 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003471 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
3472 // OpenMP [2.16, Nesting of Regions]
3473 // A critical region may not be nested (closely or otherwise) inside a
3474 // critical region with the same name. Note that this restriction is not
3475 // sufficient to prevent deadlock.
3476 SourceLocation PreviousCriticalLoc;
David Majnemer9d168222016-08-05 17:44:54 +00003477 bool DeadLock = Stack->hasDirective(
3478 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
3479 const DeclarationNameInfo &DNI,
3480 SourceLocation Loc) -> bool {
3481 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
3482 PreviousCriticalLoc = Loc;
3483 return true;
3484 } else
3485 return false;
3486 },
3487 false /* skip top directive */);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003488 if (DeadLock) {
3489 SemaRef.Diag(StartLoc,
3490 diag::err_omp_prohibited_region_critical_same_name)
3491 << CurrentName.getName();
3492 if (PreviousCriticalLoc.isValid())
3493 SemaRef.Diag(PreviousCriticalLoc,
3494 diag::note_omp_previous_critical_region);
3495 return true;
3496 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003497 } else if (CurrentRegion == OMPD_barrier) {
3498 // OpenMP [2.16, Nesting of Regions]
3499 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003500 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003501 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3502 isOpenMPTaskingDirective(ParentRegion) ||
3503 ParentRegion == OMPD_master ||
3504 ParentRegion == OMPD_critical ||
3505 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00003506 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00003507 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003508 // OpenMP [2.16, Nesting of Regions]
3509 // A worksharing region may not be closely nested inside a worksharing,
3510 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003511 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3512 isOpenMPTaskingDirective(ParentRegion) ||
3513 ParentRegion == OMPD_master ||
3514 ParentRegion == OMPD_critical ||
3515 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003516 Recommend = ShouldBeInParallelRegion;
3517 } else if (CurrentRegion == OMPD_ordered) {
3518 // OpenMP [2.16, Nesting of Regions]
3519 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00003520 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003521 // An ordered region must be closely nested inside a loop region (or
3522 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003523 // OpenMP [2.8.1,simd Construct, Restrictions]
3524 // An ordered construct with the simd clause is the only OpenMP construct
3525 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003526 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003527 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003528 !(isOpenMPSimdDirective(ParentRegion) ||
3529 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003530 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003531 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
3532 // OpenMP [2.16, Nesting of Regions]
3533 // If specified, a teams construct must be contained within a target
3534 // construct.
3535 NestingProhibited = ParentRegion != OMPD_target;
Kelvin Li2b51f722016-07-26 04:32:50 +00003536 OrphanSeen = ParentRegion == OMPD_unknown;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003537 Recommend = ShouldBeInTargetRegion;
3538 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
3539 }
Kelvin Li02532872016-08-05 14:37:37 +00003540 if (!NestingProhibited && ParentRegion == OMPD_teams) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00003541 // OpenMP [2.16, Nesting of Regions]
3542 // distribute, parallel, parallel sections, parallel workshare, and the
3543 // parallel loop and parallel loop SIMD constructs are the only OpenMP
3544 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003545 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
3546 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00003547 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003548 }
David Majnemer9d168222016-08-05 17:44:54 +00003549 if (!NestingProhibited &&
Kelvin Li02532872016-08-05 14:37:37 +00003550 isOpenMPNestingDistributeDirective(CurrentRegion)) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003551 // OpenMP 4.5 [2.17 Nesting of Regions]
3552 // The region associated with the distribute construct must be strictly
3553 // nested inside a teams region
Kelvin Li02532872016-08-05 14:37:37 +00003554 NestingProhibited = ParentRegion != OMPD_teams;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003555 Recommend = ShouldBeInTeamsRegion;
3556 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003557 if (!NestingProhibited &&
3558 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
3559 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
3560 // OpenMP 4.5 [2.17 Nesting of Regions]
3561 // If a target, target update, target data, target enter data, or
3562 // target exit data construct is encountered during execution of a
3563 // target region, the behavior is unspecified.
3564 NestingProhibited = Stack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +00003565 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
3566 SourceLocation) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003567 if (isOpenMPTargetExecutionDirective(K)) {
3568 OffendingRegion = K;
3569 return true;
3570 } else
3571 return false;
3572 },
3573 false /* don't skip top directive */);
3574 CloseNesting = false;
3575 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003576 if (NestingProhibited) {
Kelvin Li2b51f722016-07-26 04:32:50 +00003577 if (OrphanSeen) {
3578 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
3579 << getOpenMPDirectiveName(CurrentRegion) << Recommend;
3580 } else {
3581 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
3582 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
3583 << Recommend << getOpenMPDirectiveName(CurrentRegion);
3584 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003585 return true;
3586 }
3587 }
3588 return false;
3589}
3590
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003591static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
3592 ArrayRef<OMPClause *> Clauses,
3593 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
3594 bool ErrorFound = false;
3595 unsigned NamedModifiersNumber = 0;
3596 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
3597 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00003598 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003599 for (const auto *C : Clauses) {
3600 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
3601 // At most one if clause without a directive-name-modifier can appear on
3602 // the directive.
3603 OpenMPDirectiveKind CurNM = IC->getNameModifier();
3604 if (FoundNameModifiers[CurNM]) {
3605 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
3606 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
3607 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
3608 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003609 } else if (CurNM != OMPD_unknown) {
3610 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003611 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003612 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003613 FoundNameModifiers[CurNM] = IC;
3614 if (CurNM == OMPD_unknown)
3615 continue;
3616 // Check if the specified name modifier is allowed for the current
3617 // directive.
3618 // At most one if clause with the particular directive-name-modifier can
3619 // appear on the directive.
3620 bool MatchFound = false;
3621 for (auto NM : AllowedNameModifiers) {
3622 if (CurNM == NM) {
3623 MatchFound = true;
3624 break;
3625 }
3626 }
3627 if (!MatchFound) {
3628 S.Diag(IC->getNameModifierLoc(),
3629 diag::err_omp_wrong_if_directive_name_modifier)
3630 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
3631 ErrorFound = true;
3632 }
3633 }
3634 }
3635 // If any if clause on the directive includes a directive-name-modifier then
3636 // all if clauses on the directive must include a directive-name-modifier.
3637 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3638 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
3639 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
3640 diag::err_omp_no_more_if_clause);
3641 } else {
3642 std::string Values;
3643 std::string Sep(", ");
3644 unsigned AllowedCnt = 0;
3645 unsigned TotalAllowedNum =
3646 AllowedNameModifiers.size() - NamedModifiersNumber;
3647 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3648 ++Cnt) {
3649 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3650 if (!FoundNameModifiers[NM]) {
3651 Values += "'";
3652 Values += getOpenMPDirectiveName(NM);
3653 Values += "'";
3654 if (AllowedCnt + 2 == TotalAllowedNum)
3655 Values += " or ";
3656 else if (AllowedCnt + 1 != TotalAllowedNum)
3657 Values += Sep;
3658 ++AllowedCnt;
3659 }
3660 }
3661 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
3662 diag::err_omp_unnamed_if_clause)
3663 << (TotalAllowedNum > 1) << Values;
3664 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00003665 for (auto Loc : NameModifierLoc) {
3666 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3667 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003668 ErrorFound = true;
3669 }
3670 return ErrorFound;
3671}
3672
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003673StmtResult Sema::ActOnOpenMPExecutableDirective(
3674 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3675 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3676 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003677 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003678 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
3679 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00003680 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003681
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003682 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003683 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003684 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00003685 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00003686 if (AStmt) {
3687 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3688
3689 // Check default data sharing attributes for referenced variables.
3690 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
3691 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
3692 if (DSAChecker.isErrorFound())
3693 return StmtError();
3694 // Generate list of implicitly defined firstprivate variables.
3695 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00003696
3697 if (!DSAChecker.getImplicitFirstprivate().empty()) {
3698 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
3699 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
3700 SourceLocation(), SourceLocation())) {
3701 ClausesWithImplicit.push_back(Implicit);
3702 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
3703 DSAChecker.getImplicitFirstprivate().size();
3704 } else
3705 ErrorFound = true;
3706 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003707 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003708
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003709 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003710 switch (Kind) {
3711 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00003712 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3713 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003714 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003715 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003716 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003717 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3718 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003719 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003720 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003721 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3722 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003723 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003724 case OMPD_for_simd:
3725 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3726 EndLoc, VarsWithInheritedDSA);
3727 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003728 case OMPD_sections:
3729 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3730 EndLoc);
3731 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003732 case OMPD_section:
3733 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003734 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003735 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3736 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003737 case OMPD_single:
3738 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3739 EndLoc);
3740 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003741 case OMPD_master:
3742 assert(ClausesWithImplicit.empty() &&
3743 "No clauses are allowed for 'omp master' directive");
3744 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3745 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003746 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003747 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3748 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003749 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003750 case OMPD_parallel_for:
3751 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3752 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003753 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003754 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003755 case OMPD_parallel_for_simd:
3756 Res = ActOnOpenMPParallelForSimdDirective(
3757 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003758 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003759 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003760 case OMPD_parallel_sections:
3761 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3762 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003763 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003764 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003765 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003766 Res =
3767 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003768 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003769 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003770 case OMPD_taskyield:
3771 assert(ClausesWithImplicit.empty() &&
3772 "No clauses are allowed for 'omp taskyield' directive");
3773 assert(AStmt == nullptr &&
3774 "No associated statement allowed for 'omp taskyield' directive");
3775 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3776 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003777 case OMPD_barrier:
3778 assert(ClausesWithImplicit.empty() &&
3779 "No clauses are allowed for 'omp barrier' directive");
3780 assert(AStmt == nullptr &&
3781 "No associated statement allowed for 'omp barrier' directive");
3782 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3783 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003784 case OMPD_taskwait:
3785 assert(ClausesWithImplicit.empty() &&
3786 "No clauses are allowed for 'omp taskwait' directive");
3787 assert(AStmt == nullptr &&
3788 "No associated statement allowed for 'omp taskwait' directive");
3789 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3790 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003791 case OMPD_taskgroup:
3792 assert(ClausesWithImplicit.empty() &&
3793 "No clauses are allowed for 'omp taskgroup' directive");
3794 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3795 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003796 case OMPD_flush:
3797 assert(AStmt == nullptr &&
3798 "No associated statement allowed for 'omp flush' directive");
3799 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3800 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003801 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003802 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3803 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003804 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003805 case OMPD_atomic:
3806 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3807 EndLoc);
3808 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003809 case OMPD_teams:
3810 Res =
3811 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3812 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003813 case OMPD_target:
3814 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3815 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003816 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003817 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003818 case OMPD_target_parallel:
3819 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3820 StartLoc, EndLoc);
3821 AllowedNameModifiers.push_back(OMPD_target);
3822 AllowedNameModifiers.push_back(OMPD_parallel);
3823 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003824 case OMPD_target_parallel_for:
3825 Res = ActOnOpenMPTargetParallelForDirective(
3826 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3827 AllowedNameModifiers.push_back(OMPD_target);
3828 AllowedNameModifiers.push_back(OMPD_parallel);
3829 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003830 case OMPD_cancellation_point:
3831 assert(ClausesWithImplicit.empty() &&
3832 "No clauses are allowed for 'omp cancellation point' directive");
3833 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3834 "cancellation point' directive");
3835 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3836 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003837 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003838 assert(AStmt == nullptr &&
3839 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003840 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3841 CancelRegion);
3842 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003843 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003844 case OMPD_target_data:
3845 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3846 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003847 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003848 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003849 case OMPD_target_enter_data:
3850 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3851 EndLoc);
3852 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3853 break;
Samuel Antao72590762016-01-19 20:04:50 +00003854 case OMPD_target_exit_data:
3855 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3856 EndLoc);
3857 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3858 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003859 case OMPD_taskloop:
3860 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3861 EndLoc, VarsWithInheritedDSA);
3862 AllowedNameModifiers.push_back(OMPD_taskloop);
3863 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003864 case OMPD_taskloop_simd:
3865 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3866 EndLoc, VarsWithInheritedDSA);
3867 AllowedNameModifiers.push_back(OMPD_taskloop);
3868 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003869 case OMPD_distribute:
3870 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3871 EndLoc, VarsWithInheritedDSA);
3872 break;
Samuel Antao686c70c2016-05-26 17:30:50 +00003873 case OMPD_target_update:
3874 assert(!AStmt && "Statement is not allowed for target update");
3875 Res =
3876 ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, EndLoc);
3877 AllowedNameModifiers.push_back(OMPD_target_update);
3878 break;
Carlo Bertolli9925f152016-06-27 14:55:37 +00003879 case OMPD_distribute_parallel_for:
3880 Res = ActOnOpenMPDistributeParallelForDirective(
3881 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3882 AllowedNameModifiers.push_back(OMPD_parallel);
3883 break;
Kelvin Li4a39add2016-07-05 05:00:15 +00003884 case OMPD_distribute_parallel_for_simd:
3885 Res = ActOnOpenMPDistributeParallelForSimdDirective(
3886 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3887 AllowedNameModifiers.push_back(OMPD_parallel);
3888 break;
Kelvin Li787f3fc2016-07-06 04:45:38 +00003889 case OMPD_distribute_simd:
3890 Res = ActOnOpenMPDistributeSimdDirective(
3891 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3892 break;
Kelvin Lia579b912016-07-14 02:54:56 +00003893 case OMPD_target_parallel_for_simd:
3894 Res = ActOnOpenMPTargetParallelForSimdDirective(
3895 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3896 AllowedNameModifiers.push_back(OMPD_target);
3897 AllowedNameModifiers.push_back(OMPD_parallel);
3898 break;
Kelvin Li986330c2016-07-20 22:57:10 +00003899 case OMPD_target_simd:
3900 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3901 EndLoc, VarsWithInheritedDSA);
3902 AllowedNameModifiers.push_back(OMPD_target);
3903 break;
Kelvin Li02532872016-08-05 14:37:37 +00003904 case OMPD_teams_distribute:
David Majnemer9d168222016-08-05 17:44:54 +00003905 Res = ActOnOpenMPTeamsDistributeDirective(
3906 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Kelvin Li02532872016-08-05 14:37:37 +00003907 break;
Kelvin Li0e3bde82016-08-17 23:13:03 +00003908 case OMPD_teams_distribute_simd:
3909 Res = ActOnOpenMPTeamsDistributeSimdDirective(
3910 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3911 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003912 case OMPD_declare_target:
3913 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003914 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003915 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003916 case OMPD_declare_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003917 llvm_unreachable("OpenMP Directive is not allowed");
3918 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003919 llvm_unreachable("Unknown OpenMP directive");
3920 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003921
Alexey Bataev4acb8592014-07-07 13:01:15 +00003922 for (auto P : VarsWithInheritedDSA) {
3923 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3924 << P.first << P.second->getSourceRange();
3925 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003926 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3927
3928 if (!AllowedNameModifiers.empty())
3929 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3930 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003931
Alexey Bataeved09d242014-05-28 05:53:51 +00003932 if (ErrorFound)
3933 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003934 return Res;
3935}
3936
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003937Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3938 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00003939 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00003940 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3941 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003942 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00003943 assert(Linears.size() == LinModifiers.size());
3944 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00003945 if (!DG || DG.get().isNull())
3946 return DeclGroupPtrTy();
3947
3948 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00003949 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003950 return DG;
3951 }
3952 auto *ADecl = DG.get().getSingleDecl();
3953 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3954 ADecl = FTD->getTemplatedDecl();
3955
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003956 auto *FD = dyn_cast<FunctionDecl>(ADecl);
3957 if (!FD) {
3958 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003959 return DeclGroupPtrTy();
3960 }
3961
Alexey Bataev2af33e32016-04-07 12:45:37 +00003962 // OpenMP [2.8.2, declare simd construct, Description]
3963 // The parameter of the simdlen clause must be a constant positive integer
3964 // expression.
3965 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003966 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00003967 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003968 // OpenMP [2.8.2, declare simd construct, Description]
3969 // The special this pointer can be used as if was one of the arguments to the
3970 // function in any of the linear, aligned, or uniform clauses.
3971 // The uniform clause declares one or more arguments to have an invariant
3972 // value for all concurrent invocations of the function in the execution of a
3973 // single SIMD loop.
Alexey Bataevecba70f2016-04-12 11:02:11 +00003974 llvm::DenseMap<Decl *, Expr *> UniformedArgs;
3975 Expr *UniformedLinearThis = nullptr;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003976 for (auto *E : Uniforms) {
3977 E = E->IgnoreParenImpCasts();
3978 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3979 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
3980 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3981 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00003982 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
3983 UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E));
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003984 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003985 }
3986 if (isa<CXXThisExpr>(E)) {
3987 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003988 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003989 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003990 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3991 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00003992 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00003993 // OpenMP [2.8.2, declare simd construct, Description]
3994 // The aligned clause declares that the object to which each list item points
3995 // is aligned to the number of bytes expressed in the optional parameter of
3996 // the aligned clause.
3997 // The special this pointer can be used as if was one of the arguments to the
3998 // function in any of the linear, aligned, or uniform clauses.
3999 // The type of list items appearing in the aligned clause must be array,
4000 // pointer, reference to array, or reference to pointer.
4001 llvm::DenseMap<Decl *, Expr *> AlignedArgs;
4002 Expr *AlignedThis = nullptr;
4003 for (auto *E : Aligneds) {
4004 E = E->IgnoreParenImpCasts();
4005 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
4006 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
4007 auto *CanonPVD = PVD->getCanonicalDecl();
4008 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
4009 FD->getParamDecl(PVD->getFunctionScopeIndex())
4010 ->getCanonicalDecl() == CanonPVD) {
4011 // OpenMP [2.8.1, simd construct, Restrictions]
4012 // A list-item cannot appear in more than one aligned clause.
4013 if (AlignedArgs.count(CanonPVD) > 0) {
4014 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
4015 << 1 << E->getSourceRange();
4016 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
4017 diag::note_omp_explicit_dsa)
4018 << getOpenMPClauseName(OMPC_aligned);
4019 continue;
4020 }
4021 AlignedArgs[CanonPVD] = E;
4022 QualType QTy = PVD->getType()
4023 .getNonReferenceType()
4024 .getUnqualifiedType()
4025 .getCanonicalType();
4026 const Type *Ty = QTy.getTypePtrOrNull();
4027 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
4028 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
4029 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
4030 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
4031 }
4032 continue;
4033 }
4034 }
4035 if (isa<CXXThisExpr>(E)) {
4036 if (AlignedThis) {
4037 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
4038 << 2 << E->getSourceRange();
4039 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
4040 << getOpenMPClauseName(OMPC_aligned);
4041 }
4042 AlignedThis = E;
4043 continue;
4044 }
4045 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
4046 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
4047 }
4048 // The optional parameter of the aligned clause, alignment, must be a constant
4049 // positive integer expression. If no optional parameter is specified,
4050 // implementation-defined default alignments for SIMD instructions on the
4051 // target platforms are assumed.
4052 SmallVector<Expr *, 4> NewAligns;
4053 for (auto *E : Alignments) {
4054 ExprResult Align;
4055 if (E)
4056 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
4057 NewAligns.push_back(Align.get());
4058 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00004059 // OpenMP [2.8.2, declare simd construct, Description]
4060 // The linear clause declares one or more list items to be private to a SIMD
4061 // lane and to have a linear relationship with respect to the iteration space
4062 // of a loop.
4063 // The special this pointer can be used as if was one of the arguments to the
4064 // function in any of the linear, aligned, or uniform clauses.
4065 // When a linear-step expression is specified in a linear clause it must be
4066 // either a constant integer expression or an integer-typed parameter that is
4067 // specified in a uniform clause on the directive.
4068 llvm::DenseMap<Decl *, Expr *> LinearArgs;
4069 const bool IsUniformedThis = UniformedLinearThis != nullptr;
4070 auto MI = LinModifiers.begin();
4071 for (auto *E : Linears) {
4072 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
4073 ++MI;
4074 E = E->IgnoreParenImpCasts();
4075 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
4076 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
4077 auto *CanonPVD = PVD->getCanonicalDecl();
4078 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
4079 FD->getParamDecl(PVD->getFunctionScopeIndex())
4080 ->getCanonicalDecl() == CanonPVD) {
4081 // OpenMP [2.15.3.7, linear Clause, Restrictions]
4082 // A list-item cannot appear in more than one linear clause.
4083 if (LinearArgs.count(CanonPVD) > 0) {
4084 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
4085 << getOpenMPClauseName(OMPC_linear)
4086 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
4087 Diag(LinearArgs[CanonPVD]->getExprLoc(),
4088 diag::note_omp_explicit_dsa)
4089 << getOpenMPClauseName(OMPC_linear);
4090 continue;
4091 }
4092 // Each argument can appear in at most one uniform or linear clause.
4093 if (UniformedArgs.count(CanonPVD) > 0) {
4094 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
4095 << getOpenMPClauseName(OMPC_linear)
4096 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
4097 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
4098 diag::note_omp_explicit_dsa)
4099 << getOpenMPClauseName(OMPC_uniform);
4100 continue;
4101 }
4102 LinearArgs[CanonPVD] = E;
4103 if (E->isValueDependent() || E->isTypeDependent() ||
4104 E->isInstantiationDependent() ||
4105 E->containsUnexpandedParameterPack())
4106 continue;
4107 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
4108 PVD->getOriginalType());
4109 continue;
4110 }
4111 }
4112 if (isa<CXXThisExpr>(E)) {
4113 if (UniformedLinearThis) {
4114 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
4115 << getOpenMPClauseName(OMPC_linear)
4116 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
4117 << E->getSourceRange();
4118 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
4119 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
4120 : OMPC_linear);
4121 continue;
4122 }
4123 UniformedLinearThis = E;
4124 if (E->isValueDependent() || E->isTypeDependent() ||
4125 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
4126 continue;
4127 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
4128 E->getType());
4129 continue;
4130 }
4131 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
4132 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
4133 }
4134 Expr *Step = nullptr;
4135 Expr *NewStep = nullptr;
4136 SmallVector<Expr *, 4> NewSteps;
4137 for (auto *E : Steps) {
4138 // Skip the same step expression, it was checked already.
4139 if (Step == E || !E) {
4140 NewSteps.push_back(E ? NewStep : nullptr);
4141 continue;
4142 }
4143 Step = E;
4144 if (auto *DRE = dyn_cast<DeclRefExpr>(Step))
4145 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
4146 auto *CanonPVD = PVD->getCanonicalDecl();
4147 if (UniformedArgs.count(CanonPVD) == 0) {
4148 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
4149 << Step->getSourceRange();
4150 } else if (E->isValueDependent() || E->isTypeDependent() ||
4151 E->isInstantiationDependent() ||
4152 E->containsUnexpandedParameterPack() ||
4153 CanonPVD->getType()->hasIntegerRepresentation())
4154 NewSteps.push_back(Step);
4155 else {
4156 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
4157 << Step->getSourceRange();
4158 }
4159 continue;
4160 }
4161 NewStep = Step;
4162 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
4163 !Step->isInstantiationDependent() &&
4164 !Step->containsUnexpandedParameterPack()) {
4165 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
4166 .get();
4167 if (NewStep)
4168 NewStep = VerifyIntegerConstantExpression(NewStep).get();
4169 }
4170 NewSteps.push_back(NewStep);
4171 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00004172 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
4173 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00004174 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00004175 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
4176 const_cast<Expr **>(Linears.data()), Linears.size(),
4177 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
4178 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00004179 ADecl->addAttr(NewAttr);
4180 return ConvertDeclToDeclGroup(ADecl);
4181}
4182
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004183StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
4184 Stmt *AStmt,
4185 SourceLocation StartLoc,
4186 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004187 if (!AStmt)
4188 return StmtError();
4189
Alexey Bataev9959db52014-05-06 10:08:46 +00004190 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4191 // 1.2.2 OpenMP Language Terminology
4192 // Structured block - An executable statement with a single entry at the
4193 // top and a single exit at the bottom.
4194 // The point of exit cannot be a branch out of the structured block.
4195 // longjmp() and throw() must not violate the entry/exit criteria.
4196 CS->getCapturedDecl()->setNothrow();
4197
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004198 getCurFunction()->setHasBranchProtectedScope();
4199
Alexey Bataev25e5b442015-09-15 12:52:43 +00004200 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4201 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004202}
4203
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004204namespace {
4205/// \brief Helper class for checking canonical form of the OpenMP loops and
4206/// extracting iteration space of each loop in the loop nest, that will be used
4207/// for IR generation.
4208class OpenMPIterationSpaceChecker {
4209 /// \brief Reference to Sema.
4210 Sema &SemaRef;
4211 /// \brief A location for diagnostics (when there is no some better location).
4212 SourceLocation DefaultLoc;
4213 /// \brief A location for diagnostics (when increment is not compatible).
4214 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004215 /// \brief A source location for referring to loop init later.
4216 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004217 /// \brief A source location for referring to condition later.
4218 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004219 /// \brief A source location for referring to increment later.
4220 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004221 /// \brief Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004222 ValueDecl *LCDecl = nullptr;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00004223 /// \brief Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004224 Expr *LCRef = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004225 /// \brief Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004226 Expr *LB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004227 /// \brief Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004228 Expr *UB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004229 /// \brief Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004230 Expr *Step = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004231 /// \brief This flag is true when condition is one of:
4232 /// Var < UB
4233 /// Var <= UB
4234 /// UB > Var
4235 /// UB >= Var
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004236 bool TestIsLessOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004237 /// \brief This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004238 bool TestIsStrictOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004239 /// \brief This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004240 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004241
4242public:
4243 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004244 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004245 /// \brief Check init-expr for canonical loop form and save loop counter
4246 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00004247 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004248 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
4249 /// for less/greater and for strict/non-strict comparison.
4250 bool CheckCond(Expr *S);
4251 /// \brief Check incr-expr for canonical loop form and return true if it
4252 /// does not conform, otherwise save loop step (#Step).
4253 bool CheckInc(Expr *S);
4254 /// \brief Return the loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004255 ValueDecl *GetLoopDecl() const { return LCDecl; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00004256 /// \brief Return the reference expression to loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004257 Expr *GetLoopDeclRefExpr() const { return LCRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004258 /// \brief Source range of the loop init.
4259 SourceRange GetInitSrcRange() const { return InitSrcRange; }
4260 /// \brief Source range of the loop condition.
4261 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
4262 /// \brief Source range of the loop increment.
4263 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
4264 /// \brief True if the step should be subtracted.
4265 bool ShouldSubtractStep() const { return SubtractStep; }
4266 /// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004267 Expr *
4268 BuildNumIterations(Scope *S, const bool LimitedType,
4269 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00004270 /// \brief Build the precondition expression for the loops.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004271 Expr *BuildPreCond(Scope *S, Expr *Cond,
4272 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004273 /// \brief Build reference expression to the counter be used for codegen.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004274 DeclRefExpr *BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures,
4275 DSAStackTy &DSA) const;
Alexey Bataeva8899172015-08-06 12:30:57 +00004276 /// \brief Build reference expression to the private counter be used for
4277 /// codegen.
4278 Expr *BuildPrivateCounterVar() const;
David Majnemer9d168222016-08-05 17:44:54 +00004279 /// \brief Build initialization of the counter be used for codegen.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004280 Expr *BuildCounterInit() const;
4281 /// \brief Build step of the counter be used for codegen.
4282 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004283 /// \brief Return true if any expression is dependent.
4284 bool Dependent() const;
4285
4286private:
4287 /// \brief Check the right-hand side of an assignment in the increment
4288 /// expression.
4289 bool CheckIncRHS(Expr *RHS);
4290 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004291 bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004292 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00004293 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00004294 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004295 /// \brief Helper to set loop increment.
4296 bool SetStep(Expr *NewStep, bool Subtract);
4297};
4298
4299bool OpenMPIterationSpaceChecker::Dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004300 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004301 assert(!LB && !UB && !Step);
4302 return false;
4303 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004304 return LCDecl->getType()->isDependentType() ||
4305 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
4306 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004307}
4308
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004309static Expr *getExprAsWritten(Expr *E) {
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004310 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
4311 E = ExprTemp->getSubExpr();
4312
4313 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
4314 E = MTE->GetTemporaryExpr();
4315
4316 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
4317 E = Binder->getSubExpr();
4318
4319 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
4320 E = ICE->getSubExprAsWritten();
4321 return E->IgnoreParens();
4322}
4323
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004324bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl,
4325 Expr *NewLCRefExpr,
4326 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004327 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004328 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00004329 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004330 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004331 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004332 LCDecl = getCanonicalDecl(NewLCDecl);
4333 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004334 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
4335 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004336 if ((Ctor->isCopyOrMoveConstructor() ||
4337 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4338 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004339 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004340 LB = NewLB;
4341 return false;
4342}
4343
4344bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00004345 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004346 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004347 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
4348 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004349 if (!NewUB)
4350 return true;
4351 UB = NewUB;
4352 TestIsLessOp = LessOp;
4353 TestIsStrictOp = StrictOp;
4354 ConditionSrcRange = SR;
4355 ConditionLoc = SL;
4356 return false;
4357}
4358
4359bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
4360 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004361 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004362 if (!NewStep)
4363 return true;
4364 if (!NewStep->isValueDependent()) {
4365 // Check that the step is integer expression.
4366 SourceLocation StepLoc = NewStep->getLocStart();
4367 ExprResult Val =
4368 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
4369 if (Val.isInvalid())
4370 return true;
4371 NewStep = Val.get();
4372
4373 // OpenMP [2.6, Canonical Loop Form, Restrictions]
4374 // If test-expr is of form var relational-op b and relational-op is < or
4375 // <= then incr-expr must cause var to increase on each iteration of the
4376 // loop. If test-expr is of form var relational-op b and relational-op is
4377 // > or >= then incr-expr must cause var to decrease on each iteration of
4378 // the loop.
4379 // If test-expr is of form b relational-op var and relational-op is < or
4380 // <= then incr-expr must cause var to decrease on each iteration of the
4381 // loop. If test-expr is of form b relational-op var and relational-op is
4382 // > or >= then incr-expr must cause var to increase on each iteration of
4383 // the loop.
4384 llvm::APSInt Result;
4385 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
4386 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
4387 bool IsConstNeg =
4388 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004389 bool IsConstPos =
4390 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004391 bool IsConstZero = IsConstant && !Result.getBoolValue();
4392 if (UB && (IsConstZero ||
4393 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00004394 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004395 SemaRef.Diag(NewStep->getExprLoc(),
4396 diag::err_omp_loop_incr_not_compatible)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004397 << LCDecl << TestIsLessOp << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004398 SemaRef.Diag(ConditionLoc,
4399 diag::note_omp_loop_cond_requres_compatible_incr)
4400 << TestIsLessOp << ConditionSrcRange;
4401 return true;
4402 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004403 if (TestIsLessOp == Subtract) {
David Majnemer9d168222016-08-05 17:44:54 +00004404 NewStep =
4405 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep)
4406 .get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004407 Subtract = !Subtract;
4408 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004409 }
4410
4411 Step = NewStep;
4412 SubtractStep = Subtract;
4413 return false;
4414}
4415
Alexey Bataev9c821032015-04-30 04:23:23 +00004416bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004417 // Check init-expr for canonical loop form and save loop counter
4418 // variable - #Var and its initialization value - #LB.
4419 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
4420 // var = lb
4421 // integer-type var = lb
4422 // random-access-iterator-type var = lb
4423 // pointer-type var = lb
4424 //
4425 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00004426 if (EmitDiags) {
4427 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
4428 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004429 return true;
4430 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004431 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4432 if (!ExprTemp->cleanupsHaveSideEffects())
4433 S = ExprTemp->getSubExpr();
4434
Alexander Musmana5f070a2014-10-01 06:03:56 +00004435 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004436 if (Expr *E = dyn_cast<Expr>(S))
4437 S = E->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00004438 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004439 if (BO->getOpcode() == BO_Assign) {
4440 auto *LHS = BO->getLHS()->IgnoreParens();
4441 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
4442 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4443 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4444 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4445 return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
4446 }
4447 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4448 if (ME->isArrow() &&
4449 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4450 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4451 }
4452 }
David Majnemer9d168222016-08-05 17:44:54 +00004453 } else if (auto *DS = dyn_cast<DeclStmt>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004454 if (DS->isSingleDecl()) {
David Majnemer9d168222016-08-05 17:44:54 +00004455 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00004456 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004457 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00004458 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004459 SemaRef.Diag(S->getLocStart(),
4460 diag::ext_omp_loop_not_canonical_init)
4461 << S->getSourceRange();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004462 return SetLCDeclAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004463 }
4464 }
4465 }
David Majnemer9d168222016-08-05 17:44:54 +00004466 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004467 if (CE->getOperator() == OO_Equal) {
4468 auto *LHS = CE->getArg(0);
David Majnemer9d168222016-08-05 17:44:54 +00004469 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004470 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4471 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4472 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4473 return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
4474 }
4475 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4476 if (ME->isArrow() &&
4477 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4478 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4479 }
4480 }
4481 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004482
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004483 if (Dependent() || SemaRef.CurContext->isDependentContext())
4484 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00004485 if (EmitDiags) {
4486 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
4487 << S->getSourceRange();
4488 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004489 return true;
4490}
4491
Alexey Bataev23b69422014-06-18 07:08:49 +00004492/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004493/// variable (which may be the loop variable) if possible.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004494static const ValueDecl *GetInitLCDecl(Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004495 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00004496 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004497 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004498 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
4499 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004500 if ((Ctor->isCopyOrMoveConstructor() ||
4501 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4502 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004503 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004504 if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
4505 if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
4506 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
4507 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4508 return getCanonicalDecl(ME->getMemberDecl());
4509 return getCanonicalDecl(VD);
4510 }
4511 }
4512 if (auto *ME = dyn_cast_or_null<MemberExpr>(E))
4513 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4514 return getCanonicalDecl(ME->getMemberDecl());
4515 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004516}
4517
4518bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
4519 // Check test-expr for canonical form, save upper-bound UB, flags for
4520 // less/greater and for strict/non-strict comparison.
4521 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4522 // var relational-op b
4523 // b relational-op var
4524 //
4525 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004526 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004527 return true;
4528 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004529 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004530 SourceLocation CondLoc = S->getLocStart();
David Majnemer9d168222016-08-05 17:44:54 +00004531 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004532 if (BO->isRelationalOp()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004533 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004534 return SetUB(BO->getRHS(),
4535 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
4536 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4537 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004538 if (GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004539 return SetUB(BO->getLHS(),
4540 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
4541 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4542 BO->getSourceRange(), BO->getOperatorLoc());
4543 }
David Majnemer9d168222016-08-05 17:44:54 +00004544 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004545 if (CE->getNumArgs() == 2) {
4546 auto Op = CE->getOperator();
4547 switch (Op) {
4548 case OO_Greater:
4549 case OO_GreaterEqual:
4550 case OO_Less:
4551 case OO_LessEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004552 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004553 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
4554 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4555 CE->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004556 if (GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004557 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
4558 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4559 CE->getOperatorLoc());
4560 break;
4561 default:
4562 break;
4563 }
4564 }
4565 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004566 if (Dependent() || SemaRef.CurContext->isDependentContext())
4567 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004568 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004569 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004570 return true;
4571}
4572
4573bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
4574 // RHS of canonical loop form increment can be:
4575 // var + incr
4576 // incr + var
4577 // var - incr
4578 //
4579 RHS = RHS->IgnoreParenImpCasts();
David Majnemer9d168222016-08-05 17:44:54 +00004580 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004581 if (BO->isAdditiveOp()) {
4582 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004583 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004584 return SetStep(BO->getRHS(), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004585 if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004586 return SetStep(BO->getLHS(), false);
4587 }
David Majnemer9d168222016-08-05 17:44:54 +00004588 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004589 bool IsAdd = CE->getOperator() == OO_Plus;
4590 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004591 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004592 return SetStep(CE->getArg(1), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004593 if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004594 return SetStep(CE->getArg(0), false);
4595 }
4596 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004597 if (Dependent() || SemaRef.CurContext->isDependentContext())
4598 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004599 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004600 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004601 return true;
4602}
4603
4604bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
4605 // Check incr-expr for canonical loop form and return true if it
4606 // does not conform.
4607 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4608 // ++var
4609 // var++
4610 // --var
4611 // var--
4612 // var += incr
4613 // var -= incr
4614 // var = var + incr
4615 // var = incr + var
4616 // var = var - incr
4617 //
4618 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004619 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004620 return true;
4621 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004622 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4623 if (!ExprTemp->cleanupsHaveSideEffects())
4624 S = ExprTemp->getSubExpr();
4625
Alexander Musmana5f070a2014-10-01 06:03:56 +00004626 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004627 S = S->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00004628 if (auto *UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004629 if (UO->isIncrementDecrementOp() &&
4630 GetInitLCDecl(UO->getSubExpr()) == LCDecl)
David Majnemer9d168222016-08-05 17:44:54 +00004631 return SetStep(SemaRef
4632 .ActOnIntegerConstant(UO->getLocStart(),
4633 (UO->isDecrementOp() ? -1 : 1))
4634 .get(),
4635 false);
4636 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004637 switch (BO->getOpcode()) {
4638 case BO_AddAssign:
4639 case BO_SubAssign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004640 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004641 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
4642 break;
4643 case BO_Assign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004644 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004645 return CheckIncRHS(BO->getRHS());
4646 break;
4647 default:
4648 break;
4649 }
David Majnemer9d168222016-08-05 17:44:54 +00004650 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004651 switch (CE->getOperator()) {
4652 case OO_PlusPlus:
4653 case OO_MinusMinus:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004654 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
David Majnemer9d168222016-08-05 17:44:54 +00004655 return SetStep(SemaRef
4656 .ActOnIntegerConstant(
4657 CE->getLocStart(),
4658 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
4659 .get(),
4660 false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004661 break;
4662 case OO_PlusEqual:
4663 case OO_MinusEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004664 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004665 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
4666 break;
4667 case OO_Equal:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004668 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004669 return CheckIncRHS(CE->getArg(1));
4670 break;
4671 default:
4672 break;
4673 }
4674 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004675 if (Dependent() || SemaRef.CurContext->isDependentContext())
4676 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004677 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004678 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004679 return true;
4680}
Alexander Musmana5f070a2014-10-01 06:03:56 +00004681
Alexey Bataev5a3af132016-03-29 08:58:54 +00004682static ExprResult
4683tryBuildCapture(Sema &SemaRef, Expr *Capture,
4684 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00004685 if (SemaRef.CurContext->isDependentContext())
4686 return ExprResult(Capture);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004687 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
4688 return SemaRef.PerformImplicitConversion(
4689 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
4690 /*AllowExplicit=*/true);
4691 auto I = Captures.find(Capture);
4692 if (I != Captures.end())
4693 return buildCapture(SemaRef, Capture, I->second);
4694 DeclRefExpr *Ref = nullptr;
4695 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4696 Captures[Capture] = Ref;
4697 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004698}
4699
Alexander Musmana5f070a2014-10-01 06:03:56 +00004700/// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004701Expr *OpenMPIterationSpaceChecker::BuildNumIterations(
4702 Scope *S, const bool LimitedType,
4703 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004704 ExprResult Diff;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004705 auto VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004706 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004707 SemaRef.getLangOpts().CPlusPlus) {
4708 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004709 auto *UBExpr = TestIsLessOp ? UB : LB;
4710 auto *LBExpr = TestIsLessOp ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00004711 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
4712 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004713 if (!Upper || !Lower)
4714 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004715
4716 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4717
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004718 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004719 // BuildBinOp already emitted error, this one is to point user to upper
4720 // and lower bound, and to tell what is passed to 'operator-'.
4721 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
4722 << Upper->getSourceRange() << Lower->getSourceRange();
4723 return nullptr;
4724 }
4725 }
4726
4727 if (!Diff.isUsable())
4728 return nullptr;
4729
4730 // Upper - Lower [- 1]
4731 if (TestIsStrictOp)
4732 Diff = SemaRef.BuildBinOp(
4733 S, DefaultLoc, BO_Sub, Diff.get(),
4734 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4735 if (!Diff.isUsable())
4736 return nullptr;
4737
4738 // Upper - Lower [- 1] + Step
Alexey Bataev5a3af132016-03-29 08:58:54 +00004739 auto NewStep = tryBuildCapture(SemaRef, Step, Captures);
4740 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004741 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004742 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004743 if (!Diff.isUsable())
4744 return nullptr;
4745
4746 // Parentheses (for dumping/debugging purposes only).
4747 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4748 if (!Diff.isUsable())
4749 return nullptr;
4750
4751 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004752 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004753 if (!Diff.isUsable())
4754 return nullptr;
4755
Alexander Musman174b3ca2014-10-06 11:16:29 +00004756 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004757 QualType Type = Diff.get()->getType();
4758 auto &C = SemaRef.Context;
4759 bool UseVarType = VarType->hasIntegerRepresentation() &&
4760 C.getTypeSize(Type) > C.getTypeSize(VarType);
4761 if (!Type->isIntegerType() || UseVarType) {
4762 unsigned NewSize =
4763 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4764 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4765 : Type->hasSignedIntegerRepresentation();
4766 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00004767 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4768 Diff = SemaRef.PerformImplicitConversion(
4769 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4770 if (!Diff.isUsable())
4771 return nullptr;
4772 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004773 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004774 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00004775 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4776 if (NewSize != C.getTypeSize(Type)) {
4777 if (NewSize < C.getTypeSize(Type)) {
4778 assert(NewSize == 64 && "incorrect loop var size");
4779 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4780 << InitSrcRange << ConditionSrcRange;
4781 }
4782 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004783 NewSize, Type->hasSignedIntegerRepresentation() ||
4784 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00004785 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4786 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4787 Sema::AA_Converting, true);
4788 if (!Diff.isUsable())
4789 return nullptr;
4790 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004791 }
4792 }
4793
Alexander Musmana5f070a2014-10-01 06:03:56 +00004794 return Diff.get();
4795}
4796
Alexey Bataev5a3af132016-03-29 08:58:54 +00004797Expr *OpenMPIterationSpaceChecker::BuildPreCond(
4798 Scope *S, Expr *Cond,
4799 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004800 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4801 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4802 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004803
Alexey Bataev5a3af132016-03-29 08:58:54 +00004804 auto NewLB = tryBuildCapture(SemaRef, LB, Captures);
4805 auto NewUB = tryBuildCapture(SemaRef, UB, Captures);
4806 if (!NewLB.isUsable() || !NewUB.isUsable())
4807 return nullptr;
4808
Alexey Bataev62dbb972015-04-22 11:59:37 +00004809 auto CondExpr = SemaRef.BuildBinOp(
4810 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
4811 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004812 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004813 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004814 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4815 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00004816 CondExpr = SemaRef.PerformImplicitConversion(
4817 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4818 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004819 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00004820 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4821 // Otherwise use original loop conditon and evaluate it in runtime.
4822 return CondExpr.isUsable() ? CondExpr.get() : Cond;
4823}
4824
Alexander Musmana5f070a2014-10-01 06:03:56 +00004825/// \brief Build reference expression to the counter be used for codegen.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004826DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar(
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004827 llvm::MapVector<Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004828 auto *VD = dyn_cast<VarDecl>(LCDecl);
4829 if (!VD) {
4830 VD = SemaRef.IsOpenMPCapturedDecl(LCDecl);
4831 auto *Ref = buildDeclRefExpr(
4832 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004833 DSAStackTy::DSAVarData Data = DSA.getTopDSA(LCDecl, /*FromParent=*/false);
4834 // If the loop control decl is explicitly marked as private, do not mark it
4835 // as captured again.
4836 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4837 Captures.insert(std::make_pair(LCRef, Ref));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004838 return Ref;
4839 }
4840 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00004841 DefaultLoc);
4842}
4843
4844Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004845 if (LCDecl && !LCDecl->isInvalidDecl()) {
4846 auto Type = LCDecl->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00004847 auto *PrivateVar =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004848 buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(),
4849 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00004850 if (PrivateVar->isInvalidDecl())
4851 return nullptr;
4852 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4853 }
4854 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004855}
4856
David Majnemer9d168222016-08-05 17:44:54 +00004857/// \brief Build instillation of the counter be used for codegen.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004858Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
4859
4860/// \brief Build step of the counter be used for codegen.
4861Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
4862
4863/// \brief Iteration space of a single for loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004864struct LoopIterationSpace final {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004865 /// \brief Condition of the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004866 Expr *PreCond = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004867 /// \brief This expression calculates the number of iterations in the loop.
4868 /// It is always possible to calculate it before starting the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004869 Expr *NumIterations = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004870 /// \brief The loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004871 Expr *CounterVar = nullptr;
Alexey Bataeva8899172015-08-06 12:30:57 +00004872 /// \brief Private loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004873 Expr *PrivateCounterVar = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004874 /// \brief This is initializer for the initial value of #CounterVar.
Alexey Bataev8b427062016-05-25 12:36:08 +00004875 Expr *CounterInit = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004876 /// \brief This is step for the #CounterVar used to generate its update:
4877 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
Alexey Bataev8b427062016-05-25 12:36:08 +00004878 Expr *CounterStep = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004879 /// \brief Should step be subtracted?
Alexey Bataev8b427062016-05-25 12:36:08 +00004880 bool Subtract = false;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004881 /// \brief Source range of the loop init.
4882 SourceRange InitSrcRange;
4883 /// \brief Source range of the loop condition.
4884 SourceRange CondSrcRange;
4885 /// \brief Source range of the loop increment.
4886 SourceRange IncSrcRange;
4887};
4888
Alexey Bataev23b69422014-06-18 07:08:49 +00004889} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004890
Alexey Bataev9c821032015-04-30 04:23:23 +00004891void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4892 assert(getLangOpts().OpenMP && "OpenMP is not active.");
4893 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004894 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4895 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00004896 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4897 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004898 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
4899 if (auto *D = ISC.GetLoopDecl()) {
4900 auto *VD = dyn_cast<VarDecl>(D);
4901 if (!VD) {
4902 if (auto *Private = IsOpenMPCapturedDecl(D))
4903 VD = Private;
4904 else {
4905 auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(),
4906 /*WithInit=*/false);
4907 VD = cast<VarDecl>(Ref->getDecl());
4908 }
4909 }
4910 DSAStack->addLoopControlVariable(D, VD);
4911 }
4912 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004913 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00004914 }
4915}
4916
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004917/// \brief Called on a for stmt to check and extract its iteration space
4918/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00004919static bool CheckOpenMPIterationSpace(
4920 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4921 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004922 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004923 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004924 LoopIterationSpace &ResultIterSpace,
4925 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004926 // OpenMP [2.6, Canonical Loop Form]
4927 // for (init-expr; test-expr; incr-expr) structured-block
David Majnemer9d168222016-08-05 17:44:54 +00004928 auto *For = dyn_cast_or_null<ForStmt>(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004929 if (!For) {
4930 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004931 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
4932 << getOpenMPDirectiveName(DKind) << NestedLoopCount
4933 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
4934 if (NestedLoopCount > 1) {
4935 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4936 SemaRef.Diag(DSA.getConstructLoc(),
4937 diag::note_omp_collapse_ordered_expr)
4938 << 2 << CollapseLoopCountExpr->getSourceRange()
4939 << OrderedLoopCountExpr->getSourceRange();
4940 else if (CollapseLoopCountExpr)
4941 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4942 diag::note_omp_collapse_ordered_expr)
4943 << 0 << CollapseLoopCountExpr->getSourceRange();
4944 else
4945 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4946 diag::note_omp_collapse_ordered_expr)
4947 << 1 << OrderedLoopCountExpr->getSourceRange();
4948 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004949 return true;
4950 }
4951 assert(For->getBody());
4952
4953 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4954
4955 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00004956 auto Init = For->getInit();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004957 if (ISC.CheckInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004958 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004959
4960 bool HasErrors = false;
4961
4962 // Check loop variable's type.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004963 if (auto *LCDecl = ISC.GetLoopDecl()) {
4964 auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004965
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004966 // OpenMP [2.6, Canonical Loop Form]
4967 // Var is one of the following:
4968 // A variable of signed or unsigned integer type.
4969 // For C++, a variable of a random access iterator type.
4970 // For C, a variable of a pointer type.
4971 auto VarType = LCDecl->getType().getNonReferenceType();
4972 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4973 !VarType->isPointerType() &&
4974 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
4975 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
4976 << SemaRef.getLangOpts().CPlusPlus;
4977 HasErrors = true;
4978 }
4979
4980 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4981 // a Construct
4982 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4983 // parallel for construct is (are) private.
4984 // The loop iteration variable in the associated for-loop of a simd
4985 // construct with just one associated for-loop is linear with a
4986 // constant-linear-step that is the increment of the associated for-loop.
4987 // Exclude loop var from the list of variables with implicitly defined data
4988 // sharing attributes.
4989 VarsWithImplicitDSA.erase(LCDecl);
4990
4991 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4992 // in a Construct, C/C++].
4993 // The loop iteration variable in the associated for-loop of a simd
4994 // construct with just one associated for-loop may be listed in a linear
4995 // clause with a constant-linear-step that is the increment of the
4996 // associated for-loop.
4997 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4998 // parallel for construct may be listed in a private or lastprivate clause.
4999 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
5000 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
5001 // declared in the loop and it is predetermined as a private.
5002 auto PredeterminedCKind =
5003 isOpenMPSimdDirective(DKind)
5004 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
5005 : OMPC_private;
5006 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
5007 DVar.CKind != PredeterminedCKind) ||
5008 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
5009 isOpenMPDistributeDirective(DKind)) &&
5010 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
5011 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
5012 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
5013 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
5014 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
5015 << getOpenMPClauseName(PredeterminedCKind);
5016 if (DVar.RefExpr == nullptr)
5017 DVar.CKind = PredeterminedCKind;
5018 ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
5019 HasErrors = true;
5020 } else if (LoopDeclRefExpr != nullptr) {
5021 // Make the loop iteration variable private (for worksharing constructs),
5022 // linear (for simd directives with the only one associated loop) or
5023 // lastprivate (for simd directives with several collapsed or ordered
5024 // loops).
5025 if (DVar.CKind == OMPC_unknown)
Alexey Bataev7ace49d2016-05-17 08:55:33 +00005026 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
5027 [](OpenMPDirectiveKind) -> bool { return true; },
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005028 /*FromParent=*/false);
5029 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
5030 }
5031
5032 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
5033
5034 // Check test-expr.
5035 HasErrors |= ISC.CheckCond(For->getCond());
5036
5037 // Check incr-expr.
5038 HasErrors |= ISC.CheckInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005039 }
5040
Alexander Musmana5f070a2014-10-01 06:03:56 +00005041 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005042 return HasErrors;
5043
Alexander Musmana5f070a2014-10-01 06:03:56 +00005044 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00005045 ResultIterSpace.PreCond =
5046 ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures);
Alexander Musman174b3ca2014-10-06 11:16:29 +00005047 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00005048 DSA.getCurScope(),
5049 (isOpenMPWorksharingDirective(DKind) ||
5050 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
5051 Captures);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005052 ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures, DSA);
Alexey Bataeva8899172015-08-06 12:30:57 +00005053 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005054 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
5055 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
5056 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
5057 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
5058 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
5059 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
5060
Alexey Bataev62dbb972015-04-22 11:59:37 +00005061 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
5062 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00005063 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00005064 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00005065 ResultIterSpace.CounterInit == nullptr ||
5066 ResultIterSpace.CounterStep == nullptr);
5067
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005068 return HasErrors;
5069}
5070
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005071/// \brief Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00005072static ExprResult
5073BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
5074 ExprResult Start,
5075 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005076 // Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00005077 auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
5078 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005079 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00005080 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00005081 VarRef.get()->getType())) {
5082 NewStart = SemaRef.PerformImplicitConversion(
5083 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
5084 /*AllowExplicit=*/true);
5085 if (!NewStart.isUsable())
5086 return ExprError();
5087 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005088
5089 auto Init =
5090 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
5091 return Init;
5092}
5093
Alexander Musmana5f070a2014-10-01 06:03:56 +00005094/// \brief Build 'VarRef = Start + Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00005095static ExprResult
5096BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc,
5097 ExprResult VarRef, ExprResult Start, ExprResult Iter,
5098 ExprResult Step, bool Subtract,
5099 llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00005100 // Add parentheses (for debugging purposes only).
5101 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
5102 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
5103 !Step.isUsable())
5104 return ExprError();
5105
Alexey Bataev5a3af132016-03-29 08:58:54 +00005106 ExprResult NewStep = Step;
5107 if (Captures)
5108 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005109 if (NewStep.isInvalid())
5110 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005111 ExprResult Update =
5112 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005113 if (!Update.isUsable())
5114 return ExprError();
5115
Alexey Bataevc0214e02016-02-16 12:13:49 +00005116 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
5117 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00005118 ExprResult NewStart = Start;
5119 if (Captures)
5120 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005121 if (NewStart.isInvalid())
5122 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005123
Alexey Bataevc0214e02016-02-16 12:13:49 +00005124 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
5125 ExprResult SavedUpdate = Update;
5126 ExprResult UpdateVal;
5127 if (VarRef.get()->getType()->isOverloadableType() ||
5128 NewStart.get()->getType()->isOverloadableType() ||
5129 Update.get()->getType()->isOverloadableType()) {
5130 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
5131 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
5132 Update =
5133 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
5134 if (Update.isUsable()) {
5135 UpdateVal =
5136 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
5137 VarRef.get(), SavedUpdate.get());
5138 if (UpdateVal.isUsable()) {
5139 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
5140 UpdateVal.get());
5141 }
5142 }
5143 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
5144 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005145
Alexey Bataevc0214e02016-02-16 12:13:49 +00005146 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
5147 if (!Update.isUsable() || !UpdateVal.isUsable()) {
5148 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
5149 NewStart.get(), SavedUpdate.get());
5150 if (!Update.isUsable())
5151 return ExprError();
5152
Alexey Bataev11481f52016-02-17 10:29:05 +00005153 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
5154 VarRef.get()->getType())) {
5155 Update = SemaRef.PerformImplicitConversion(
5156 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
5157 if (!Update.isUsable())
5158 return ExprError();
5159 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00005160
5161 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
5162 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005163 return Update;
5164}
5165
5166/// \brief Convert integer expression \a E to make it have at least \a Bits
5167/// bits.
David Majnemer9d168222016-08-05 17:44:54 +00005168static ExprResult WidenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00005169 if (E == nullptr)
5170 return ExprError();
5171 auto &C = SemaRef.Context;
5172 QualType OldType = E->getType();
5173 unsigned HasBits = C.getTypeSize(OldType);
5174 if (HasBits >= Bits)
5175 return ExprResult(E);
5176 // OK to convert to signed, because new type has more bits than old.
5177 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
5178 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
5179 true);
5180}
5181
5182/// \brief Check if the given expression \a E is a constant integer that fits
5183/// into \a Bits bits.
5184static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
5185 if (E == nullptr)
5186 return false;
5187 llvm::APSInt Result;
5188 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
5189 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
5190 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005191}
5192
Alexey Bataev5a3af132016-03-29 08:58:54 +00005193/// Build preinits statement for the given declarations.
5194static Stmt *buildPreInits(ASTContext &Context,
5195 SmallVectorImpl<Decl *> &PreInits) {
5196 if (!PreInits.empty()) {
5197 return new (Context) DeclStmt(
5198 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
5199 SourceLocation(), SourceLocation());
5200 }
5201 return nullptr;
5202}
5203
5204/// Build preinits statement for the given declarations.
5205static Stmt *buildPreInits(ASTContext &Context,
5206 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
5207 if (!Captures.empty()) {
5208 SmallVector<Decl *, 16> PreInits;
5209 for (auto &Pair : Captures)
5210 PreInits.push_back(Pair.second->getDecl());
5211 return buildPreInits(Context, PreInits);
5212 }
5213 return nullptr;
5214}
5215
5216/// Build postupdate expression for the given list of postupdates expressions.
5217static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
5218 Expr *PostUpdate = nullptr;
5219 if (!PostUpdates.empty()) {
5220 for (auto *E : PostUpdates) {
5221 Expr *ConvE = S.BuildCStyleCastExpr(
5222 E->getExprLoc(),
5223 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
5224 E->getExprLoc(), E)
5225 .get();
5226 PostUpdate = PostUpdate
5227 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
5228 PostUpdate, ConvE)
5229 .get()
5230 : ConvE;
5231 }
5232 }
5233 return PostUpdate;
5234}
5235
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005236/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00005237/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
5238/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005239static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00005240CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
5241 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
5242 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005243 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00005244 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005245 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005246 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005247 // Found 'collapse' clause - calculate collapse number.
5248 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005249 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005250 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005251 }
5252 if (OrderedLoopCountExpr) {
5253 // Found 'ordered' clause - calculate collapse number.
5254 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005255 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
5256 if (Result.getLimitedValue() < NestedLoopCount) {
5257 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
5258 diag::err_omp_wrong_ordered_loop_count)
5259 << OrderedLoopCountExpr->getSourceRange();
5260 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
5261 diag::note_collapse_loop_count)
5262 << CollapseLoopCountExpr->getSourceRange();
5263 }
5264 NestedLoopCount = Result.getLimitedValue();
5265 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005266 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005267 // This is helper routine for loop directives (e.g., 'for', 'simd',
5268 // 'for simd', etc.).
Alexey Bataev5a3af132016-03-29 08:58:54 +00005269 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005270 SmallVector<LoopIterationSpace, 4> IterSpaces;
5271 IterSpaces.resize(NestedLoopCount);
5272 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005273 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005274 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00005275 NestedLoopCount, CollapseLoopCountExpr,
5276 OrderedLoopCountExpr, VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005277 IterSpaces[Cnt], Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00005278 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005279 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005280 // OpenMP [2.8.1, simd construct, Restrictions]
5281 // All loops associated with the construct must be perfectly nested; that
5282 // is, there must be no intervening code nor any OpenMP directive between
5283 // any two loops.
5284 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005285 }
5286
Alexander Musmana5f070a2014-10-01 06:03:56 +00005287 Built.clear(/* size */ NestedLoopCount);
5288
5289 if (SemaRef.CurContext->isDependentContext())
5290 return NestedLoopCount;
5291
5292 // An example of what is generated for the following code:
5293 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00005294 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00005295 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00005296 // for (k = 0; k < NK; ++k)
5297 // for (j = J0; j < NJ; j+=2) {
5298 // <loop body>
5299 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005300 //
5301 // We generate the code below.
5302 // Note: the loop body may be outlined in CodeGen.
5303 // Note: some counters may be C++ classes, operator- is used to find number of
5304 // iterations and operator+= to calculate counter value.
5305 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
5306 // or i64 is currently supported).
5307 //
5308 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
5309 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
5310 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
5311 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
5312 // // similar updates for vars in clauses (e.g. 'linear')
5313 // <loop body (using local i and j)>
5314 // }
5315 // i = NI; // assign final values of counters
5316 // j = NJ;
5317 //
5318
5319 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
5320 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00005321 // Precondition tests if there is at least one iteration (all conditions are
5322 // true).
5323 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005324 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005325 ExprResult LastIteration32 = WidenIterationCount(
David Majnemer9d168222016-08-05 17:44:54 +00005326 32 /* Bits */, SemaRef
5327 .PerformImplicitConversion(
5328 N0->IgnoreImpCasts(), N0->getType(),
5329 Sema::AA_Converting, /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005330 .get(),
5331 SemaRef);
5332 ExprResult LastIteration64 = WidenIterationCount(
David Majnemer9d168222016-08-05 17:44:54 +00005333 64 /* Bits */, SemaRef
5334 .PerformImplicitConversion(
5335 N0->IgnoreImpCasts(), N0->getType(),
5336 Sema::AA_Converting, /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005337 .get(),
5338 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005339
5340 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
5341 return NestedLoopCount;
5342
5343 auto &C = SemaRef.Context;
5344 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
5345
5346 Scope *CurScope = DSA.getCurScope();
5347 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00005348 if (PreCond.isUsable()) {
5349 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
5350 PreCond.get(), IterSpaces[Cnt].PreCond);
5351 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005352 auto N = IterSpaces[Cnt].NumIterations;
5353 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
5354 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005355 LastIteration32 = SemaRef.BuildBinOp(
5356 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
David Majnemer9d168222016-08-05 17:44:54 +00005357 SemaRef
5358 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5359 Sema::AA_Converting,
5360 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005361 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005362 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005363 LastIteration64 = SemaRef.BuildBinOp(
5364 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
David Majnemer9d168222016-08-05 17:44:54 +00005365 SemaRef
5366 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5367 Sema::AA_Converting,
5368 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005369 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005370 }
5371
5372 // Choose either the 32-bit or 64-bit version.
5373 ExprResult LastIteration = LastIteration64;
5374 if (LastIteration32.isUsable() &&
5375 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
5376 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
5377 FitsInto(
5378 32 /* Bits */,
5379 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
5380 LastIteration64.get(), SemaRef)))
5381 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00005382 QualType VType = LastIteration.get()->getType();
5383 QualType RealVType = VType;
5384 QualType StrideVType = VType;
5385 if (isOpenMPTaskLoopDirective(DKind)) {
5386 VType =
5387 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
5388 StrideVType =
5389 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
5390 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005391
5392 if (!LastIteration.isUsable())
5393 return 0;
5394
5395 // Save the number of iterations.
5396 ExprResult NumIterations = LastIteration;
5397 {
5398 LastIteration = SemaRef.BuildBinOp(
5399 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
5400 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5401 if (!LastIteration.isUsable())
5402 return 0;
5403 }
5404
5405 // Calculate the last iteration number beforehand instead of doing this on
5406 // each iteration. Do not do this if the number of iterations may be kfold-ed.
5407 llvm::APSInt Result;
5408 bool IsConstant =
5409 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
5410 ExprResult CalcLastIteration;
5411 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005412 ExprResult SaveRef =
5413 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005414 LastIteration = SaveRef;
5415
5416 // Prepare SaveRef + 1.
5417 NumIterations = SemaRef.BuildBinOp(
Alexey Bataev5a3af132016-03-29 08:58:54 +00005418 CurScope, SourceLocation(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00005419 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5420 if (!NumIterations.isUsable())
5421 return 0;
5422 }
5423
5424 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
5425
David Majnemer9d168222016-08-05 17:44:54 +00005426 // Build variables passed into runtime, necessary for worksharing directives.
Carlo Bertolli9925f152016-06-27 14:55:37 +00005427 ExprResult LB, UB, IL, ST, EUB, PrevLB, PrevUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005428 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5429 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005430 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005431 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
5432 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005433 SemaRef.AddInitializerToDecl(
5434 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5435 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5436
5437 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005438 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
5439 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005440 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
5441 /*DirectInit*/ false,
5442 /*TypeMayContainAuto*/ false);
5443
5444 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
5445 // This will be used to implement clause 'lastprivate'.
5446 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005447 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
5448 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005449 SemaRef.AddInitializerToDecl(
5450 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5451 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5452
5453 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00005454 VarDecl *STDecl =
5455 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
5456 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005457 SemaRef.AddInitializerToDecl(
5458 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
5459 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5460
5461 // Build expression: UB = min(UB, LastIteration)
David Majnemer9d168222016-08-05 17:44:54 +00005462 // It is necessary for CodeGen of directives with static scheduling.
Alexander Musmanc6388682014-12-15 07:07:06 +00005463 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
5464 UB.get(), LastIteration.get());
5465 ExprResult CondOp = SemaRef.ActOnConditionalOp(
5466 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
5467 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
5468 CondOp.get());
5469 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
Carlo Bertolli9925f152016-06-27 14:55:37 +00005470
5471 // If we have a combined directive that combines 'distribute', 'for' or
5472 // 'simd' we need to be able to access the bounds of the schedule of the
5473 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
5474 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
5475 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5476 auto *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
5477
5478 // We expect to have at least 2 more parameters than the 'parallel'
5479 // directive does - the lower and upper bounds of the previous schedule.
5480 assert(CD->getNumParams() >= 4 &&
5481 "Unexpected number of parameters in loop combined directive");
5482
5483 // Set the proper type for the bounds given what we learned from the
5484 // enclosed loops.
5485 auto *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
5486 auto *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
5487
5488 // Previous lower and upper bounds are obtained from the region
5489 // parameters.
5490 PrevLB =
5491 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
5492 PrevUB =
5493 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
5494 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005495 }
5496
5497 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005498 ExprResult IV;
5499 ExprResult Init;
5500 {
Alexey Bataev7292c292016-04-25 12:22:29 +00005501 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
5502 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
David Majnemer9d168222016-08-05 17:44:54 +00005503 Expr *RHS =
5504 (isOpenMPWorksharingDirective(DKind) ||
5505 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
5506 ? LB.get()
5507 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005508 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
5509 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005510 }
5511
Alexander Musmanc6388682014-12-15 07:07:06 +00005512 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005513 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00005514 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005515 (isOpenMPWorksharingDirective(DKind) ||
5516 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00005517 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
5518 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
5519 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005520
5521 // Loop increment (IV = IV + 1)
5522 SourceLocation IncLoc;
5523 ExprResult Inc =
5524 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
5525 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
5526 if (!Inc.isUsable())
5527 return 0;
5528 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00005529 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
5530 if (!Inc.isUsable())
5531 return 0;
5532
5533 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
5534 // Used for directives with static scheduling.
5535 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005536 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5537 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005538 // LB + ST
5539 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
5540 if (!NextLB.isUsable())
5541 return 0;
5542 // LB = LB + ST
5543 NextLB =
5544 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
5545 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
5546 if (!NextLB.isUsable())
5547 return 0;
5548 // UB + ST
5549 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
5550 if (!NextUB.isUsable())
5551 return 0;
5552 // UB = UB + ST
5553 NextUB =
5554 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
5555 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
5556 if (!NextUB.isUsable())
5557 return 0;
5558 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005559
5560 // Build updates and final values of the loop counters.
5561 bool HasErrors = false;
5562 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005563 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005564 Built.Updates.resize(NestedLoopCount);
5565 Built.Finals.resize(NestedLoopCount);
Alexey Bataev8b427062016-05-25 12:36:08 +00005566 SmallVector<Expr *, 4> LoopMultipliers;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005567 {
5568 ExprResult Div;
5569 // Go from inner nested loop to outer.
5570 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5571 LoopIterationSpace &IS = IterSpaces[Cnt];
5572 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
5573 // Build: Iter = (IV / Div) % IS.NumIters
5574 // where Div is product of previous iterations' IS.NumIters.
5575 ExprResult Iter;
5576 if (Div.isUsable()) {
5577 Iter =
5578 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
5579 } else {
5580 Iter = IV;
5581 assert((Cnt == (int)NestedLoopCount - 1) &&
5582 "unusable div expected on first iteration only");
5583 }
5584
5585 if (Cnt != 0 && Iter.isUsable())
5586 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
5587 IS.NumIterations);
5588 if (!Iter.isUsable()) {
5589 HasErrors = true;
5590 break;
5591 }
5592
Alexey Bataev39f915b82015-05-08 10:41:21 +00005593 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005594 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
5595 auto *CounterVar = buildDeclRefExpr(SemaRef, VD, IS.CounterVar->getType(),
5596 IS.CounterVar->getExprLoc(),
5597 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005598 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005599 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005600 if (!Init.isUsable()) {
5601 HasErrors = true;
5602 break;
5603 }
Alexey Bataev5a3af132016-03-29 08:58:54 +00005604 ExprResult Update = BuildCounterUpdate(
5605 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
5606 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005607 if (!Update.isUsable()) {
5608 HasErrors = true;
5609 break;
5610 }
5611
5612 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
5613 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00005614 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005615 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005616 if (!Final.isUsable()) {
5617 HasErrors = true;
5618 break;
5619 }
5620
5621 // Build Div for the next iteration: Div <- Div * IS.NumIters
5622 if (Cnt != 0) {
5623 if (Div.isUnset())
5624 Div = IS.NumIterations;
5625 else
5626 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
5627 IS.NumIterations);
5628
5629 // Add parentheses (for debugging purposes only).
5630 if (Div.isUsable())
Alexey Bataev8b427062016-05-25 12:36:08 +00005631 Div = tryBuildCapture(SemaRef, Div.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005632 if (!Div.isUsable()) {
5633 HasErrors = true;
5634 break;
5635 }
Alexey Bataev8b427062016-05-25 12:36:08 +00005636 LoopMultipliers.push_back(Div.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005637 }
5638 if (!Update.isUsable() || !Final.isUsable()) {
5639 HasErrors = true;
5640 break;
5641 }
5642 // Save results
5643 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00005644 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005645 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005646 Built.Updates[Cnt] = Update.get();
5647 Built.Finals[Cnt] = Final.get();
5648 }
5649 }
5650
5651 if (HasErrors)
5652 return 0;
5653
5654 // Save results
5655 Built.IterationVarRef = IV.get();
5656 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00005657 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00005658 Built.CalcLastIteration =
5659 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005660 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00005661 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005662 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005663 Built.Init = Init.get();
5664 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005665 Built.LB = LB.get();
5666 Built.UB = UB.get();
5667 Built.IL = IL.get();
5668 Built.ST = ST.get();
5669 Built.EUB = EUB.get();
5670 Built.NLB = NextLB.get();
5671 Built.NUB = NextUB.get();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005672 Built.PrevLB = PrevLB.get();
5673 Built.PrevUB = PrevUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005674
Alexey Bataev8b427062016-05-25 12:36:08 +00005675 Expr *CounterVal = SemaRef.DefaultLvalueConversion(IV.get()).get();
5676 // Fill data for doacross depend clauses.
5677 for (auto Pair : DSA.getDoacrossDependClauses()) {
5678 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
5679 Pair.first->setCounterValue(CounterVal);
5680 else {
5681 if (NestedLoopCount != Pair.second.size() ||
5682 NestedLoopCount != LoopMultipliers.size() + 1) {
5683 // Erroneous case - clause has some problems.
5684 Pair.first->setCounterValue(CounterVal);
5685 continue;
5686 }
5687 assert(Pair.first->getDependencyKind() == OMPC_DEPEND_sink);
5688 auto I = Pair.second.rbegin();
5689 auto IS = IterSpaces.rbegin();
5690 auto ILM = LoopMultipliers.rbegin();
5691 Expr *UpCounterVal = CounterVal;
5692 Expr *Multiplier = nullptr;
5693 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5694 if (I->first) {
5695 assert(IS->CounterStep);
5696 Expr *NormalizedOffset =
5697 SemaRef
5698 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Div,
5699 I->first, IS->CounterStep)
5700 .get();
5701 if (Multiplier) {
5702 NormalizedOffset =
5703 SemaRef
5704 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Mul,
5705 NormalizedOffset, Multiplier)
5706 .get();
5707 }
5708 assert(I->second == OO_Plus || I->second == OO_Minus);
5709 BinaryOperatorKind BOK = (I->second == OO_Plus) ? BO_Add : BO_Sub;
David Majnemer9d168222016-08-05 17:44:54 +00005710 UpCounterVal = SemaRef
5711 .BuildBinOp(CurScope, I->first->getExprLoc(), BOK,
5712 UpCounterVal, NormalizedOffset)
5713 .get();
Alexey Bataev8b427062016-05-25 12:36:08 +00005714 }
5715 Multiplier = *ILM;
5716 ++I;
5717 ++IS;
5718 ++ILM;
5719 }
5720 Pair.first->setCounterValue(UpCounterVal);
5721 }
5722 }
5723
Alexey Bataevabfc0692014-06-25 06:52:00 +00005724 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005725}
5726
Alexey Bataev10e775f2015-07-30 11:36:16 +00005727static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005728 auto CollapseClauses =
5729 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
5730 if (CollapseClauses.begin() != CollapseClauses.end())
5731 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005732 return nullptr;
5733}
5734
Alexey Bataev10e775f2015-07-30 11:36:16 +00005735static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005736 auto OrderedClauses =
5737 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
5738 if (OrderedClauses.begin() != OrderedClauses.end())
5739 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005740 return nullptr;
5741}
5742
Kelvin Lic5609492016-07-15 04:39:07 +00005743static bool checkSimdlenSafelenSpecified(Sema &S,
5744 const ArrayRef<OMPClause *> Clauses) {
5745 OMPSafelenClause *Safelen = nullptr;
5746 OMPSimdlenClause *Simdlen = nullptr;
5747
5748 for (auto *Clause : Clauses) {
5749 if (Clause->getClauseKind() == OMPC_safelen)
5750 Safelen = cast<OMPSafelenClause>(Clause);
5751 else if (Clause->getClauseKind() == OMPC_simdlen)
5752 Simdlen = cast<OMPSimdlenClause>(Clause);
5753 if (Safelen && Simdlen)
5754 break;
5755 }
5756
5757 if (Simdlen && Safelen) {
5758 llvm::APSInt SimdlenRes, SafelenRes;
5759 auto SimdlenLength = Simdlen->getSimdlen();
5760 auto SafelenLength = Safelen->getSafelen();
5761 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
5762 SimdlenLength->isInstantiationDependent() ||
5763 SimdlenLength->containsUnexpandedParameterPack())
5764 return false;
5765 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
5766 SafelenLength->isInstantiationDependent() ||
5767 SafelenLength->containsUnexpandedParameterPack())
5768 return false;
5769 SimdlenLength->EvaluateAsInt(SimdlenRes, S.Context);
5770 SafelenLength->EvaluateAsInt(SafelenRes, S.Context);
5771 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
5772 // If both simdlen and safelen clauses are specified, the value of the
5773 // simdlen parameter must be less than or equal to the value of the safelen
5774 // parameter.
5775 if (SimdlenRes > SafelenRes) {
5776 S.Diag(SimdlenLength->getExprLoc(),
5777 diag::err_omp_wrong_simdlen_safelen_values)
5778 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
5779 return true;
5780 }
Alexey Bataev66b15b52015-08-21 11:14:16 +00005781 }
5782 return false;
5783}
5784
Alexey Bataev4acb8592014-07-07 13:01:15 +00005785StmtResult Sema::ActOnOpenMPSimdDirective(
5786 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5787 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005788 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005789 if (!AStmt)
5790 return StmtError();
5791
5792 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005793 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005794 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5795 // define the nested loops number.
5796 unsigned NestedLoopCount = CheckOpenMPLoop(
5797 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5798 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005799 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005800 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005801
Alexander Musmana5f070a2014-10-01 06:03:56 +00005802 assert((CurContext->isDependentContext() || B.builtAll()) &&
5803 "omp simd loop exprs were not built");
5804
Alexander Musman3276a272015-03-21 10:12:56 +00005805 if (!CurContext->isDependentContext()) {
5806 // Finalize the clauses that need pre-built expressions for CodeGen.
5807 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005808 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexander Musman3276a272015-03-21 10:12:56 +00005809 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005810 B.NumIterations, *this, CurScope,
5811 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00005812 return StmtError();
5813 }
5814 }
5815
Kelvin Lic5609492016-07-15 04:39:07 +00005816 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005817 return StmtError();
5818
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005819 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005820 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5821 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005822}
5823
Alexey Bataev4acb8592014-07-07 13:01:15 +00005824StmtResult Sema::ActOnOpenMPForDirective(
5825 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5826 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005827 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005828 if (!AStmt)
5829 return StmtError();
5830
5831 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005832 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005833 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5834 // define the nested loops number.
5835 unsigned NestedLoopCount = CheckOpenMPLoop(
5836 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5837 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005838 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005839 return StmtError();
5840
Alexander Musmana5f070a2014-10-01 06:03:56 +00005841 assert((CurContext->isDependentContext() || B.builtAll()) &&
5842 "omp for loop exprs were not built");
5843
Alexey Bataev54acd402015-08-04 11:18:19 +00005844 if (!CurContext->isDependentContext()) {
5845 // Finalize the clauses that need pre-built expressions for CodeGen.
5846 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005847 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00005848 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005849 B.NumIterations, *this, CurScope,
5850 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005851 return StmtError();
5852 }
5853 }
5854
Alexey Bataevf29276e2014-06-18 04:14:57 +00005855 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005856 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005857 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005858}
5859
Alexander Musmanf82886e2014-09-18 05:12:34 +00005860StmtResult Sema::ActOnOpenMPForSimdDirective(
5861 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5862 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005863 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005864 if (!AStmt)
5865 return StmtError();
5866
5867 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005868 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005869 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5870 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00005871 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005872 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
5873 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5874 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005875 if (NestedLoopCount == 0)
5876 return StmtError();
5877
Alexander Musmanc6388682014-12-15 07:07:06 +00005878 assert((CurContext->isDependentContext() || B.builtAll()) &&
5879 "omp for simd loop exprs were not built");
5880
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005881 if (!CurContext->isDependentContext()) {
5882 // Finalize the clauses that need pre-built expressions for CodeGen.
5883 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005884 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005885 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005886 B.NumIterations, *this, CurScope,
5887 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005888 return StmtError();
5889 }
5890 }
5891
Kelvin Lic5609492016-07-15 04:39:07 +00005892 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005893 return StmtError();
5894
Alexander Musmanf82886e2014-09-18 05:12:34 +00005895 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005896 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5897 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005898}
5899
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005900StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5901 Stmt *AStmt,
5902 SourceLocation StartLoc,
5903 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005904 if (!AStmt)
5905 return StmtError();
5906
5907 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005908 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00005909 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005910 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00005911 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005912 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005913 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005914 return StmtError();
5915 // All associated statements must be '#pragma omp section' except for
5916 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005917 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005918 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5919 if (SectionStmt)
5920 Diag(SectionStmt->getLocStart(),
5921 diag::err_omp_sections_substmt_not_section);
5922 return StmtError();
5923 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005924 cast<OMPSectionDirective>(SectionStmt)
5925 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005926 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005927 } else {
5928 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
5929 return StmtError();
5930 }
5931
5932 getCurFunction()->setHasBranchProtectedScope();
5933
Alexey Bataev25e5b442015-09-15 12:52:43 +00005934 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5935 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005936}
5937
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005938StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5939 SourceLocation StartLoc,
5940 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005941 if (!AStmt)
5942 return StmtError();
5943
5944 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005945
5946 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00005947 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005948
Alexey Bataev25e5b442015-09-15 12:52:43 +00005949 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5950 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005951}
5952
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005953StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5954 Stmt *AStmt,
5955 SourceLocation StartLoc,
5956 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005957 if (!AStmt)
5958 return StmtError();
5959
5960 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00005961
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005962 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00005963
Alexey Bataev3255bf32015-01-19 05:20:46 +00005964 // OpenMP [2.7.3, single Construct, Restrictions]
5965 // The copyprivate clause must not be used with the nowait clause.
5966 OMPClause *Nowait = nullptr;
5967 OMPClause *Copyprivate = nullptr;
5968 for (auto *Clause : Clauses) {
5969 if (Clause->getClauseKind() == OMPC_nowait)
5970 Nowait = Clause;
5971 else if (Clause->getClauseKind() == OMPC_copyprivate)
5972 Copyprivate = Clause;
5973 if (Copyprivate && Nowait) {
5974 Diag(Copyprivate->getLocStart(),
5975 diag::err_omp_single_copyprivate_with_nowait);
5976 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
5977 return StmtError();
5978 }
5979 }
5980
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005981 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5982}
5983
Alexander Musman80c22892014-07-17 08:54:58 +00005984StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5985 SourceLocation StartLoc,
5986 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005987 if (!AStmt)
5988 return StmtError();
5989
5990 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00005991
5992 getCurFunction()->setHasBranchProtectedScope();
5993
5994 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5995}
5996
Alexey Bataev28c75412015-12-15 08:19:24 +00005997StmtResult Sema::ActOnOpenMPCriticalDirective(
5998 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5999 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006000 if (!AStmt)
6001 return StmtError();
6002
6003 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00006004
Alexey Bataev28c75412015-12-15 08:19:24 +00006005 bool ErrorFound = false;
6006 llvm::APSInt Hint;
6007 SourceLocation HintLoc;
6008 bool DependentHint = false;
6009 for (auto *C : Clauses) {
6010 if (C->getClauseKind() == OMPC_hint) {
6011 if (!DirName.getName()) {
6012 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
6013 ErrorFound = true;
6014 }
6015 Expr *E = cast<OMPHintClause>(C)->getHint();
6016 if (E->isTypeDependent() || E->isValueDependent() ||
6017 E->isInstantiationDependent())
6018 DependentHint = true;
6019 else {
6020 Hint = E->EvaluateKnownConstInt(Context);
6021 HintLoc = C->getLocStart();
6022 }
6023 }
6024 }
6025 if (ErrorFound)
6026 return StmtError();
6027 auto Pair = DSAStack->getCriticalWithHint(DirName);
6028 if (Pair.first && DirName.getName() && !DependentHint) {
6029 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
6030 Diag(StartLoc, diag::err_omp_critical_with_hint);
6031 if (HintLoc.isValid()) {
6032 Diag(HintLoc, diag::note_omp_critical_hint_here)
6033 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
6034 } else
6035 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
6036 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
6037 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
6038 << 1
6039 << C->getHint()->EvaluateKnownConstInt(Context).toString(
6040 /*Radix=*/10, /*Signed=*/false);
6041 } else
6042 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
6043 }
6044 }
6045
Alexander Musmand9ed09f2014-07-21 09:42:05 +00006046 getCurFunction()->setHasBranchProtectedScope();
6047
Alexey Bataev28c75412015-12-15 08:19:24 +00006048 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
6049 Clauses, AStmt);
6050 if (!Pair.first && DirName.getName() && !DependentHint)
6051 DSAStack->addCriticalWithHint(Dir, Hint);
6052 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00006053}
6054
Alexey Bataev4acb8592014-07-07 13:01:15 +00006055StmtResult Sema::ActOnOpenMPParallelForDirective(
6056 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6057 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006058 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006059 if (!AStmt)
6060 return StmtError();
6061
Alexey Bataev4acb8592014-07-07 13:01:15 +00006062 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6063 // 1.2.2 OpenMP Language Terminology
6064 // Structured block - An executable statement with a single entry at the
6065 // top and a single exit at the bottom.
6066 // The point of exit cannot be a branch out of the structured block.
6067 // longjmp() and throw() must not violate the entry/exit criteria.
6068 CS->getCapturedDecl()->setNothrow();
6069
Alexander Musmanc6388682014-12-15 07:07:06 +00006070 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006071 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6072 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00006073 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00006074 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
6075 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6076 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00006077 if (NestedLoopCount == 0)
6078 return StmtError();
6079
Alexander Musmana5f070a2014-10-01 06:03:56 +00006080 assert((CurContext->isDependentContext() || B.builtAll()) &&
6081 "omp parallel for loop exprs were not built");
6082
Alexey Bataev54acd402015-08-04 11:18:19 +00006083 if (!CurContext->isDependentContext()) {
6084 // Finalize the clauses that need pre-built expressions for CodeGen.
6085 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00006086 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00006087 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006088 B.NumIterations, *this, CurScope,
6089 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00006090 return StmtError();
6091 }
6092 }
6093
Alexey Bataev4acb8592014-07-07 13:01:15 +00006094 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00006095 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00006096 NestedLoopCount, Clauses, AStmt, B,
6097 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00006098}
6099
Alexander Musmane4e893b2014-09-23 09:33:00 +00006100StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
6101 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6102 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006103 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006104 if (!AStmt)
6105 return StmtError();
6106
Alexander Musmane4e893b2014-09-23 09:33:00 +00006107 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6108 // 1.2.2 OpenMP Language Terminology
6109 // Structured block - An executable statement with a single entry at the
6110 // top and a single exit at the bottom.
6111 // The point of exit cannot be a branch out of the structured block.
6112 // longjmp() and throw() must not violate the entry/exit criteria.
6113 CS->getCapturedDecl()->setNothrow();
6114
Alexander Musmanc6388682014-12-15 07:07:06 +00006115 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006116 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6117 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00006118 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00006119 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
6120 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6121 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00006122 if (NestedLoopCount == 0)
6123 return StmtError();
6124
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00006125 if (!CurContext->isDependentContext()) {
6126 // Finalize the clauses that need pre-built expressions for CodeGen.
6127 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00006128 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00006129 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006130 B.NumIterations, *this, CurScope,
6131 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00006132 return StmtError();
6133 }
6134 }
6135
Kelvin Lic5609492016-07-15 04:39:07 +00006136 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00006137 return StmtError();
6138
Alexander Musmane4e893b2014-09-23 09:33:00 +00006139 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00006140 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00006141 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00006142}
6143
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006144StmtResult
6145Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
6146 Stmt *AStmt, SourceLocation StartLoc,
6147 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006148 if (!AStmt)
6149 return StmtError();
6150
6151 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006152 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00006153 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006154 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00006155 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006156 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00006157 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006158 return StmtError();
6159 // All associated statements must be '#pragma omp section' except for
6160 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00006161 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006162 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
6163 if (SectionStmt)
6164 Diag(SectionStmt->getLocStart(),
6165 diag::err_omp_parallel_sections_substmt_not_section);
6166 return StmtError();
6167 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006168 cast<OMPSectionDirective>(SectionStmt)
6169 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006170 }
6171 } else {
6172 Diag(AStmt->getLocStart(),
6173 diag::err_omp_parallel_sections_not_compound_stmt);
6174 return StmtError();
6175 }
6176
6177 getCurFunction()->setHasBranchProtectedScope();
6178
Alexey Bataev25e5b442015-09-15 12:52:43 +00006179 return OMPParallelSectionsDirective::Create(
6180 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006181}
6182
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006183StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
6184 Stmt *AStmt, SourceLocation StartLoc,
6185 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006186 if (!AStmt)
6187 return StmtError();
6188
David Majnemer9d168222016-08-05 17:44:54 +00006189 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006190 // 1.2.2 OpenMP Language Terminology
6191 // Structured block - An executable statement with a single entry at the
6192 // top and a single exit at the bottom.
6193 // The point of exit cannot be a branch out of the structured block.
6194 // longjmp() and throw() must not violate the entry/exit criteria.
6195 CS->getCapturedDecl()->setNothrow();
6196
6197 getCurFunction()->setHasBranchProtectedScope();
6198
Alexey Bataev25e5b442015-09-15 12:52:43 +00006199 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
6200 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006201}
6202
Alexey Bataev68446b72014-07-18 07:47:19 +00006203StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
6204 SourceLocation EndLoc) {
6205 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
6206}
6207
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00006208StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
6209 SourceLocation EndLoc) {
6210 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
6211}
6212
Alexey Bataev2df347a2014-07-18 10:17:07 +00006213StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
6214 SourceLocation EndLoc) {
6215 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
6216}
6217
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006218StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
6219 SourceLocation StartLoc,
6220 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006221 if (!AStmt)
6222 return StmtError();
6223
6224 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006225
6226 getCurFunction()->setHasBranchProtectedScope();
6227
6228 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
6229}
6230
Alexey Bataev6125da92014-07-21 11:26:11 +00006231StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
6232 SourceLocation StartLoc,
6233 SourceLocation EndLoc) {
6234 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
6235 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
6236}
6237
Alexey Bataev346265e2015-09-25 10:37:12 +00006238StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
6239 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006240 SourceLocation StartLoc,
6241 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00006242 OMPClause *DependFound = nullptr;
6243 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006244 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00006245 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00006246 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006247 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00006248 for (auto *C : Clauses) {
6249 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
6250 DependFound = C;
6251 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
6252 if (DependSourceClause) {
6253 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
6254 << getOpenMPDirectiveName(OMPD_ordered)
6255 << getOpenMPClauseName(OMPC_depend) << 2;
6256 ErrorFound = true;
6257 } else
6258 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006259 if (DependSinkClause) {
6260 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
6261 << 0;
6262 ErrorFound = true;
6263 }
6264 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
6265 if (DependSourceClause) {
6266 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
6267 << 1;
6268 ErrorFound = true;
6269 }
6270 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00006271 }
6272 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00006273 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006274 else if (C->getClauseKind() == OMPC_simd)
6275 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00006276 }
Alexey Bataeveb482352015-12-18 05:05:56 +00006277 if (!ErrorFound && !SC &&
6278 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006279 // OpenMP [2.8.1,simd Construct, Restrictions]
6280 // An ordered construct with the simd clause is the only OpenMP construct
6281 // that can appear in the simd region.
6282 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00006283 ErrorFound = true;
6284 } else if (DependFound && (TC || SC)) {
6285 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
6286 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
6287 ErrorFound = true;
6288 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
6289 Diag(DependFound->getLocStart(),
6290 diag::err_omp_ordered_directive_without_param);
6291 ErrorFound = true;
6292 } else if (TC || Clauses.empty()) {
6293 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
6294 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
6295 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
6296 << (TC != nullptr);
6297 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
6298 ErrorFound = true;
6299 }
6300 }
6301 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006302 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00006303
6304 if (AStmt) {
6305 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6306
6307 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006308 }
Alexey Bataev346265e2015-09-25 10:37:12 +00006309
6310 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006311}
6312
Alexey Bataev1d160b12015-03-13 12:27:31 +00006313namespace {
6314/// \brief Helper class for checking expression in 'omp atomic [update]'
6315/// construct.
6316class OpenMPAtomicUpdateChecker {
6317 /// \brief Error results for atomic update expressions.
6318 enum ExprAnalysisErrorCode {
6319 /// \brief A statement is not an expression statement.
6320 NotAnExpression,
6321 /// \brief Expression is not builtin binary or unary operation.
6322 NotABinaryOrUnaryExpression,
6323 /// \brief Unary operation is not post-/pre- increment/decrement operation.
6324 NotAnUnaryIncDecExpression,
6325 /// \brief An expression is not of scalar type.
6326 NotAScalarType,
6327 /// \brief A binary operation is not an assignment operation.
6328 NotAnAssignmentOp,
6329 /// \brief RHS part of the binary operation is not a binary expression.
6330 NotABinaryExpression,
6331 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
6332 /// expression.
6333 NotABinaryOperator,
6334 /// \brief RHS binary operation does not have reference to the updated LHS
6335 /// part.
6336 NotAnUpdateExpression,
6337 /// \brief No errors is found.
6338 NoError
6339 };
6340 /// \brief Reference to Sema.
6341 Sema &SemaRef;
6342 /// \brief A location for note diagnostics (when error is found).
6343 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006344 /// \brief 'x' lvalue part of the source atomic expression.
6345 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006346 /// \brief 'expr' rvalue part of the source atomic expression.
6347 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006348 /// \brief Helper expression of the form
6349 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6350 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6351 Expr *UpdateExpr;
6352 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
6353 /// important for non-associative operations.
6354 bool IsXLHSInRHSPart;
6355 BinaryOperatorKind Op;
6356 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006357 /// \brief true if the source expression is a postfix unary operation, false
6358 /// if it is a prefix unary operation.
6359 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006360
6361public:
6362 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00006363 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00006364 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00006365 /// \brief Check specified statement that it is suitable for 'atomic update'
6366 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00006367 /// expression. If DiagId and NoteId == 0, then only check is performed
6368 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006369 /// \param DiagId Diagnostic which should be emitted if error is found.
6370 /// \param NoteId Diagnostic note for the main error message.
6371 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00006372 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00006373 /// \brief Return the 'x' lvalue part of the source atomic expression.
6374 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00006375 /// \brief Return the 'expr' rvalue part of the source atomic expression.
6376 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00006377 /// \brief Return the update expression used in calculation of the updated
6378 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6379 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6380 Expr *getUpdateExpr() const { return UpdateExpr; }
6381 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
6382 /// false otherwise.
6383 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
6384
Alexey Bataevb78ca832015-04-01 03:33:17 +00006385 /// \brief true if the source expression is a postfix unary operation, false
6386 /// if it is a prefix unary operation.
6387 bool isPostfixUpdate() const { return IsPostfixUpdate; }
6388
Alexey Bataev1d160b12015-03-13 12:27:31 +00006389private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00006390 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
6391 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00006392};
6393} // namespace
6394
6395bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
6396 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
6397 ExprAnalysisErrorCode ErrorFound = NoError;
6398 SourceLocation ErrorLoc, NoteLoc;
6399 SourceRange ErrorRange, NoteRange;
6400 // Allowed constructs are:
6401 // x = x binop expr;
6402 // x = expr binop x;
6403 if (AtomicBinOp->getOpcode() == BO_Assign) {
6404 X = AtomicBinOp->getLHS();
6405 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
6406 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
6407 if (AtomicInnerBinOp->isMultiplicativeOp() ||
6408 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
6409 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006410 Op = AtomicInnerBinOp->getOpcode();
6411 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006412 auto *LHS = AtomicInnerBinOp->getLHS();
6413 auto *RHS = AtomicInnerBinOp->getRHS();
6414 llvm::FoldingSetNodeID XId, LHSId, RHSId;
6415 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
6416 /*Canonical=*/true);
6417 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
6418 /*Canonical=*/true);
6419 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
6420 /*Canonical=*/true);
6421 if (XId == LHSId) {
6422 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006423 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006424 } else if (XId == RHSId) {
6425 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006426 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006427 } else {
6428 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6429 ErrorRange = AtomicInnerBinOp->getSourceRange();
6430 NoteLoc = X->getExprLoc();
6431 NoteRange = X->getSourceRange();
6432 ErrorFound = NotAnUpdateExpression;
6433 }
6434 } else {
6435 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6436 ErrorRange = AtomicInnerBinOp->getSourceRange();
6437 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
6438 NoteRange = SourceRange(NoteLoc, NoteLoc);
6439 ErrorFound = NotABinaryOperator;
6440 }
6441 } else {
6442 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
6443 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
6444 ErrorFound = NotABinaryExpression;
6445 }
6446 } else {
6447 ErrorLoc = AtomicBinOp->getExprLoc();
6448 ErrorRange = AtomicBinOp->getSourceRange();
6449 NoteLoc = AtomicBinOp->getOperatorLoc();
6450 NoteRange = SourceRange(NoteLoc, NoteLoc);
6451 ErrorFound = NotAnAssignmentOp;
6452 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006453 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006454 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6455 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6456 return true;
6457 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006458 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006459 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006460}
6461
6462bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
6463 unsigned NoteId) {
6464 ExprAnalysisErrorCode ErrorFound = NoError;
6465 SourceLocation ErrorLoc, NoteLoc;
6466 SourceRange ErrorRange, NoteRange;
6467 // Allowed constructs are:
6468 // x++;
6469 // x--;
6470 // ++x;
6471 // --x;
6472 // x binop= expr;
6473 // x = x binop expr;
6474 // x = expr binop x;
6475 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
6476 AtomicBody = AtomicBody->IgnoreParenImpCasts();
6477 if (AtomicBody->getType()->isScalarType() ||
6478 AtomicBody->isInstantiationDependent()) {
6479 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
6480 AtomicBody->IgnoreParenImpCasts())) {
6481 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00006482 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006483 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00006484 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006485 E = AtomicCompAssignOp->getRHS();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006486 X = AtomicCompAssignOp->getLHS()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006487 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006488 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
6489 AtomicBody->IgnoreParenImpCasts())) {
6490 // Check for Binary Operation
David Majnemer9d168222016-08-05 17:44:54 +00006491 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
Alexey Bataevb4505a72015-03-30 05:20:59 +00006492 return true;
David Majnemer9d168222016-08-05 17:44:54 +00006493 } else if (auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
6494 AtomicBody->IgnoreParenImpCasts())) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006495 // Check for Unary Operation
6496 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006497 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006498 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
6499 OpLoc = AtomicUnaryOp->getOperatorLoc();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006500 X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006501 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
6502 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006503 } else {
6504 ErrorFound = NotAnUnaryIncDecExpression;
6505 ErrorLoc = AtomicUnaryOp->getExprLoc();
6506 ErrorRange = AtomicUnaryOp->getSourceRange();
6507 NoteLoc = AtomicUnaryOp->getOperatorLoc();
6508 NoteRange = SourceRange(NoteLoc, NoteLoc);
6509 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006510 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006511 ErrorFound = NotABinaryOrUnaryExpression;
6512 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
6513 NoteRange = ErrorRange = AtomicBody->getSourceRange();
6514 }
6515 } else {
6516 ErrorFound = NotAScalarType;
6517 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
6518 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6519 }
6520 } else {
6521 ErrorFound = NotAnExpression;
6522 NoteLoc = ErrorLoc = S->getLocStart();
6523 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6524 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006525 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006526 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6527 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6528 return true;
6529 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006530 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006531 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006532 // Build an update expression of form 'OpaqueValueExpr(x) binop
6533 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
6534 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
6535 auto *OVEX = new (SemaRef.getASTContext())
6536 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
6537 auto *OVEExpr = new (SemaRef.getASTContext())
6538 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
6539 auto Update =
6540 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
6541 IsXLHSInRHSPart ? OVEExpr : OVEX);
6542 if (Update.isInvalid())
6543 return true;
6544 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
6545 Sema::AA_Casting);
6546 if (Update.isInvalid())
6547 return true;
6548 UpdateExpr = Update.get();
6549 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00006550 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006551}
6552
Alexey Bataev0162e452014-07-22 10:10:35 +00006553StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
6554 Stmt *AStmt,
6555 SourceLocation StartLoc,
6556 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006557 if (!AStmt)
6558 return StmtError();
6559
David Majnemer9d168222016-08-05 17:44:54 +00006560 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00006561 // 1.2.2 OpenMP Language Terminology
6562 // Structured block - An executable statement with a single entry at the
6563 // top and a single exit at the bottom.
6564 // The point of exit cannot be a branch out of the structured block.
6565 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00006566 OpenMPClauseKind AtomicKind = OMPC_unknown;
6567 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006568 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00006569 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00006570 C->getClauseKind() == OMPC_update ||
6571 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00006572 if (AtomicKind != OMPC_unknown) {
6573 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
6574 << SourceRange(C->getLocStart(), C->getLocEnd());
6575 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
6576 << getOpenMPClauseName(AtomicKind);
6577 } else {
6578 AtomicKind = C->getClauseKind();
6579 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006580 }
6581 }
6582 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006583
Alexey Bataev459dec02014-07-24 06:46:57 +00006584 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00006585 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
6586 Body = EWC->getSubExpr();
6587
Alexey Bataev62cec442014-11-18 10:14:22 +00006588 Expr *X = nullptr;
6589 Expr *V = nullptr;
6590 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006591 Expr *UE = nullptr;
6592 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006593 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00006594 // OpenMP [2.12.6, atomic Construct]
6595 // In the next expressions:
6596 // * x and v (as applicable) are both l-value expressions with scalar type.
6597 // * During the execution of an atomic region, multiple syntactic
6598 // occurrences of x must designate the same storage location.
6599 // * Neither of v and expr (as applicable) may access the storage location
6600 // designated by x.
6601 // * Neither of x and expr (as applicable) may access the storage location
6602 // designated by v.
6603 // * expr is an expression with scalar type.
6604 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
6605 // * binop, binop=, ++, and -- are not overloaded operators.
6606 // * The expression x binop expr must be numerically equivalent to x binop
6607 // (expr). This requirement is satisfied if the operators in expr have
6608 // precedence greater than binop, or by using parentheses around expr or
6609 // subexpressions of expr.
6610 // * The expression expr binop x must be numerically equivalent to (expr)
6611 // binop x. This requirement is satisfied if the operators in expr have
6612 // precedence equal to or greater than binop, or by using parentheses around
6613 // expr or subexpressions of expr.
6614 // * For forms that allow multiple occurrences of x, the number of times
6615 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00006616 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006617 enum {
6618 NotAnExpression,
6619 NotAnAssignmentOp,
6620 NotAScalarType,
6621 NotAnLValue,
6622 NoError
6623 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00006624 SourceLocation ErrorLoc, NoteLoc;
6625 SourceRange ErrorRange, NoteRange;
6626 // If clause is read:
6627 // v = x;
David Majnemer9d168222016-08-05 17:44:54 +00006628 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6629 auto *AtomicBinOp =
Alexey Bataev62cec442014-11-18 10:14:22 +00006630 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6631 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6632 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6633 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
6634 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6635 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
6636 if (!X->isLValue() || !V->isLValue()) {
6637 auto NotLValueExpr = X->isLValue() ? V : X;
6638 ErrorFound = NotAnLValue;
6639 ErrorLoc = AtomicBinOp->getExprLoc();
6640 ErrorRange = AtomicBinOp->getSourceRange();
6641 NoteLoc = NotLValueExpr->getExprLoc();
6642 NoteRange = NotLValueExpr->getSourceRange();
6643 }
6644 } else if (!X->isInstantiationDependent() ||
6645 !V->isInstantiationDependent()) {
6646 auto NotScalarExpr =
6647 (X->isInstantiationDependent() || X->getType()->isScalarType())
6648 ? V
6649 : X;
6650 ErrorFound = NotAScalarType;
6651 ErrorLoc = AtomicBinOp->getExprLoc();
6652 ErrorRange = AtomicBinOp->getSourceRange();
6653 NoteLoc = NotScalarExpr->getExprLoc();
6654 NoteRange = NotScalarExpr->getSourceRange();
6655 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006656 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00006657 ErrorFound = NotAnAssignmentOp;
6658 ErrorLoc = AtomicBody->getExprLoc();
6659 ErrorRange = AtomicBody->getSourceRange();
6660 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6661 : AtomicBody->getExprLoc();
6662 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6663 : AtomicBody->getSourceRange();
6664 }
6665 } else {
6666 ErrorFound = NotAnExpression;
6667 NoteLoc = ErrorLoc = Body->getLocStart();
6668 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006669 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006670 if (ErrorFound != NoError) {
6671 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
6672 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006673 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6674 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00006675 return StmtError();
6676 } else if (CurContext->isDependentContext())
6677 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00006678 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006679 enum {
6680 NotAnExpression,
6681 NotAnAssignmentOp,
6682 NotAScalarType,
6683 NotAnLValue,
6684 NoError
6685 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006686 SourceLocation ErrorLoc, NoteLoc;
6687 SourceRange ErrorRange, NoteRange;
6688 // If clause is write:
6689 // x = expr;
David Majnemer9d168222016-08-05 17:44:54 +00006690 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6691 auto *AtomicBinOp =
Alexey Bataevf33eba62014-11-28 07:21:40 +00006692 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6693 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00006694 X = AtomicBinOp->getLHS();
6695 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006696 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6697 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
6698 if (!X->isLValue()) {
6699 ErrorFound = NotAnLValue;
6700 ErrorLoc = AtomicBinOp->getExprLoc();
6701 ErrorRange = AtomicBinOp->getSourceRange();
6702 NoteLoc = X->getExprLoc();
6703 NoteRange = X->getSourceRange();
6704 }
6705 } else if (!X->isInstantiationDependent() ||
6706 !E->isInstantiationDependent()) {
6707 auto NotScalarExpr =
6708 (X->isInstantiationDependent() || X->getType()->isScalarType())
6709 ? E
6710 : X;
6711 ErrorFound = NotAScalarType;
6712 ErrorLoc = AtomicBinOp->getExprLoc();
6713 ErrorRange = AtomicBinOp->getSourceRange();
6714 NoteLoc = NotScalarExpr->getExprLoc();
6715 NoteRange = NotScalarExpr->getSourceRange();
6716 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006717 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00006718 ErrorFound = NotAnAssignmentOp;
6719 ErrorLoc = AtomicBody->getExprLoc();
6720 ErrorRange = AtomicBody->getSourceRange();
6721 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6722 : AtomicBody->getExprLoc();
6723 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6724 : AtomicBody->getSourceRange();
6725 }
6726 } else {
6727 ErrorFound = NotAnExpression;
6728 NoteLoc = ErrorLoc = Body->getLocStart();
6729 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006730 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00006731 if (ErrorFound != NoError) {
6732 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
6733 << ErrorRange;
6734 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6735 << NoteRange;
6736 return StmtError();
6737 } else if (CurContext->isDependentContext())
6738 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006739 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006740 // If clause is update:
6741 // x++;
6742 // x--;
6743 // ++x;
6744 // --x;
6745 // x binop= expr;
6746 // x = x binop expr;
6747 // x = expr binop x;
6748 OpenMPAtomicUpdateChecker Checker(*this);
6749 if (Checker.checkStatement(
6750 Body, (AtomicKind == OMPC_update)
6751 ? diag::err_omp_atomic_update_not_expression_statement
6752 : diag::err_omp_atomic_not_expression_statement,
6753 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00006754 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006755 if (!CurContext->isDependentContext()) {
6756 E = Checker.getExpr();
6757 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006758 UE = Checker.getUpdateExpr();
6759 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00006760 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006761 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006762 enum {
6763 NotAnAssignmentOp,
6764 NotACompoundStatement,
6765 NotTwoSubstatements,
6766 NotASpecificExpression,
6767 NoError
6768 } ErrorFound = NoError;
6769 SourceLocation ErrorLoc, NoteLoc;
6770 SourceRange ErrorRange, NoteRange;
6771 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6772 // If clause is a capture:
6773 // v = x++;
6774 // v = x--;
6775 // v = ++x;
6776 // v = --x;
6777 // v = x binop= expr;
6778 // v = x = x binop expr;
6779 // v = x = expr binop x;
6780 auto *AtomicBinOp =
6781 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6782 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6783 V = AtomicBinOp->getLHS();
6784 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6785 OpenMPAtomicUpdateChecker Checker(*this);
6786 if (Checker.checkStatement(
6787 Body, diag::err_omp_atomic_capture_not_expression_statement,
6788 diag::note_omp_atomic_update))
6789 return StmtError();
6790 E = Checker.getExpr();
6791 X = Checker.getX();
6792 UE = Checker.getUpdateExpr();
6793 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6794 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00006795 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006796 ErrorLoc = AtomicBody->getExprLoc();
6797 ErrorRange = AtomicBody->getSourceRange();
6798 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6799 : AtomicBody->getExprLoc();
6800 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6801 : AtomicBody->getSourceRange();
6802 ErrorFound = NotAnAssignmentOp;
6803 }
6804 if (ErrorFound != NoError) {
6805 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6806 << ErrorRange;
6807 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6808 return StmtError();
6809 } else if (CurContext->isDependentContext()) {
6810 UE = V = E = X = nullptr;
6811 }
6812 } else {
6813 // If clause is a capture:
6814 // { v = x; x = expr; }
6815 // { v = x; x++; }
6816 // { v = x; x--; }
6817 // { v = x; ++x; }
6818 // { v = x; --x; }
6819 // { v = x; x binop= expr; }
6820 // { v = x; x = x binop expr; }
6821 // { v = x; x = expr binop x; }
6822 // { x++; v = x; }
6823 // { x--; v = x; }
6824 // { ++x; v = x; }
6825 // { --x; v = x; }
6826 // { x binop= expr; v = x; }
6827 // { x = x binop expr; v = x; }
6828 // { x = expr binop x; v = x; }
6829 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6830 // Check that this is { expr1; expr2; }
6831 if (CS->size() == 2) {
6832 auto *First = CS->body_front();
6833 auto *Second = CS->body_back();
6834 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6835 First = EWC->getSubExpr()->IgnoreParenImpCasts();
6836 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6837 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6838 // Need to find what subexpression is 'v' and what is 'x'.
6839 OpenMPAtomicUpdateChecker Checker(*this);
6840 bool IsUpdateExprFound = !Checker.checkStatement(Second);
6841 BinaryOperator *BinOp = nullptr;
6842 if (IsUpdateExprFound) {
6843 BinOp = dyn_cast<BinaryOperator>(First);
6844 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6845 }
6846 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6847 // { v = x; x++; }
6848 // { v = x; x--; }
6849 // { v = x; ++x; }
6850 // { v = x; --x; }
6851 // { v = x; x binop= expr; }
6852 // { v = x; x = x binop expr; }
6853 // { v = x; x = expr binop x; }
6854 // Check that the first expression has form v = x.
6855 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6856 llvm::FoldingSetNodeID XId, PossibleXId;
6857 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6858 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6859 IsUpdateExprFound = XId == PossibleXId;
6860 if (IsUpdateExprFound) {
6861 V = BinOp->getLHS();
6862 X = Checker.getX();
6863 E = Checker.getExpr();
6864 UE = Checker.getUpdateExpr();
6865 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006866 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006867 }
6868 }
6869 if (!IsUpdateExprFound) {
6870 IsUpdateExprFound = !Checker.checkStatement(First);
6871 BinOp = nullptr;
6872 if (IsUpdateExprFound) {
6873 BinOp = dyn_cast<BinaryOperator>(Second);
6874 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6875 }
6876 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6877 // { x++; v = x; }
6878 // { x--; v = x; }
6879 // { ++x; v = x; }
6880 // { --x; v = x; }
6881 // { x binop= expr; v = x; }
6882 // { x = x binop expr; v = x; }
6883 // { x = expr binop x; v = x; }
6884 // Check that the second expression has form v = x.
6885 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6886 llvm::FoldingSetNodeID XId, PossibleXId;
6887 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6888 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6889 IsUpdateExprFound = XId == PossibleXId;
6890 if (IsUpdateExprFound) {
6891 V = BinOp->getLHS();
6892 X = Checker.getX();
6893 E = Checker.getExpr();
6894 UE = Checker.getUpdateExpr();
6895 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006896 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006897 }
6898 }
6899 }
6900 if (!IsUpdateExprFound) {
6901 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00006902 auto *FirstExpr = dyn_cast<Expr>(First);
6903 auto *SecondExpr = dyn_cast<Expr>(Second);
6904 if (!FirstExpr || !SecondExpr ||
6905 !(FirstExpr->isInstantiationDependent() ||
6906 SecondExpr->isInstantiationDependent())) {
6907 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6908 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006909 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00006910 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
6911 : First->getLocStart();
6912 NoteRange = ErrorRange = FirstBinOp
6913 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00006914 : SourceRange(ErrorLoc, ErrorLoc);
6915 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006916 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6917 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6918 ErrorFound = NotAnAssignmentOp;
6919 NoteLoc = ErrorLoc = SecondBinOp
6920 ? SecondBinOp->getOperatorLoc()
6921 : Second->getLocStart();
6922 NoteRange = ErrorRange =
6923 SecondBinOp ? SecondBinOp->getSourceRange()
6924 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00006925 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006926 auto *PossibleXRHSInFirst =
6927 FirstBinOp->getRHS()->IgnoreParenImpCasts();
6928 auto *PossibleXLHSInSecond =
6929 SecondBinOp->getLHS()->IgnoreParenImpCasts();
6930 llvm::FoldingSetNodeID X1Id, X2Id;
6931 PossibleXRHSInFirst->Profile(X1Id, Context,
6932 /*Canonical=*/true);
6933 PossibleXLHSInSecond->Profile(X2Id, Context,
6934 /*Canonical=*/true);
6935 IsUpdateExprFound = X1Id == X2Id;
6936 if (IsUpdateExprFound) {
6937 V = FirstBinOp->getLHS();
6938 X = SecondBinOp->getLHS();
6939 E = SecondBinOp->getRHS();
6940 UE = nullptr;
6941 IsXLHSInRHSPart = false;
6942 IsPostfixUpdate = true;
6943 } else {
6944 ErrorFound = NotASpecificExpression;
6945 ErrorLoc = FirstBinOp->getExprLoc();
6946 ErrorRange = FirstBinOp->getSourceRange();
6947 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6948 NoteRange = SecondBinOp->getRHS()->getSourceRange();
6949 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006950 }
6951 }
6952 }
6953 }
6954 } else {
6955 NoteLoc = ErrorLoc = Body->getLocStart();
6956 NoteRange = ErrorRange =
6957 SourceRange(Body->getLocStart(), Body->getLocStart());
6958 ErrorFound = NotTwoSubstatements;
6959 }
6960 } else {
6961 NoteLoc = ErrorLoc = Body->getLocStart();
6962 NoteRange = ErrorRange =
6963 SourceRange(Body->getLocStart(), Body->getLocStart());
6964 ErrorFound = NotACompoundStatement;
6965 }
6966 if (ErrorFound != NoError) {
6967 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6968 << ErrorRange;
6969 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6970 return StmtError();
6971 } else if (CurContext->isDependentContext()) {
6972 UE = V = E = X = nullptr;
6973 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006974 }
Alexey Bataevdea47612014-07-23 07:46:59 +00006975 }
Alexey Bataev0162e452014-07-22 10:10:35 +00006976
6977 getCurFunction()->setHasBranchProtectedScope();
6978
Alexey Bataev62cec442014-11-18 10:14:22 +00006979 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00006980 X, V, E, UE, IsXLHSInRHSPart,
6981 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00006982}
6983
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006984StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6985 Stmt *AStmt,
6986 SourceLocation StartLoc,
6987 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006988 if (!AStmt)
6989 return StmtError();
6990
Samuel Antao4af1b7b2015-12-02 17:44:43 +00006991 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6992 // 1.2.2 OpenMP Language Terminology
6993 // Structured block - An executable statement with a single entry at the
6994 // top and a single exit at the bottom.
6995 // The point of exit cannot be a branch out of the structured block.
6996 // longjmp() and throw() must not violate the entry/exit criteria.
6997 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006998
Alexey Bataev13314bf2014-10-09 04:18:56 +00006999 // OpenMP [2.16, Nesting of Regions]
7000 // If specified, a teams construct must be contained within a target
7001 // construct. That target construct must contain no statements or directives
7002 // outside of the teams construct.
7003 if (DSAStack->hasInnerTeamsRegion()) {
7004 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
7005 bool OMPTeamsFound = true;
7006 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
7007 auto I = CS->body_begin();
7008 while (I != CS->body_end()) {
David Majnemer9d168222016-08-05 17:44:54 +00007009 auto *OED = dyn_cast<OMPExecutableDirective>(*I);
Alexey Bataev13314bf2014-10-09 04:18:56 +00007010 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
7011 OMPTeamsFound = false;
7012 break;
7013 }
7014 ++I;
7015 }
7016 assert(I != CS->body_end() && "Not found statement");
7017 S = *I;
Kelvin Li3834dce2016-06-27 19:15:43 +00007018 } else {
7019 auto *OED = dyn_cast<OMPExecutableDirective>(S);
7020 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
Alexey Bataev13314bf2014-10-09 04:18:56 +00007021 }
7022 if (!OMPTeamsFound) {
7023 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
7024 Diag(DSAStack->getInnerTeamsRegionLoc(),
7025 diag::note_omp_nested_teams_construct_here);
7026 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
7027 << isa<OMPExecutableDirective>(S);
7028 return StmtError();
7029 }
7030 }
7031
Alexey Bataev0bd520b2014-09-19 08:19:49 +00007032 getCurFunction()->setHasBranchProtectedScope();
7033
7034 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
7035}
7036
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00007037StmtResult
7038Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
7039 Stmt *AStmt, SourceLocation StartLoc,
7040 SourceLocation EndLoc) {
7041 if (!AStmt)
7042 return StmtError();
7043
7044 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7045 // 1.2.2 OpenMP Language Terminology
7046 // Structured block - An executable statement with a single entry at the
7047 // top and a single exit at the bottom.
7048 // The point of exit cannot be a branch out of the structured block.
7049 // longjmp() and throw() must not violate the entry/exit criteria.
7050 CS->getCapturedDecl()->setNothrow();
7051
7052 getCurFunction()->setHasBranchProtectedScope();
7053
7054 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
7055 AStmt);
7056}
7057
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007058StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
7059 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7060 SourceLocation EndLoc,
7061 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7062 if (!AStmt)
7063 return StmtError();
7064
7065 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7066 // 1.2.2 OpenMP Language Terminology
7067 // Structured block - An executable statement with a single entry at the
7068 // top and a single exit at the bottom.
7069 // The point of exit cannot be a branch out of the structured block.
7070 // longjmp() and throw() must not violate the entry/exit criteria.
7071 CS->getCapturedDecl()->setNothrow();
7072
7073 OMPLoopDirective::HelperExprs B;
7074 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7075 // define the nested loops number.
7076 unsigned NestedLoopCount =
7077 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
7078 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
7079 VarsWithImplicitDSA, B);
7080 if (NestedLoopCount == 0)
7081 return StmtError();
7082
7083 assert((CurContext->isDependentContext() || B.builtAll()) &&
7084 "omp target parallel for loop exprs were not built");
7085
7086 if (!CurContext->isDependentContext()) {
7087 // Finalize the clauses that need pre-built expressions for CodeGen.
7088 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007089 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007090 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007091 B.NumIterations, *this, CurScope,
7092 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007093 return StmtError();
7094 }
7095 }
7096
7097 getCurFunction()->setHasBranchProtectedScope();
7098 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
7099 NestedLoopCount, Clauses, AStmt,
7100 B, DSAStack->isCancelRegion());
7101}
7102
Samuel Antaodf67fc42016-01-19 19:15:56 +00007103/// \brief Check for existence of a map clause in the list of clauses.
7104static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
7105 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
7106 I != E; ++I) {
7107 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
7108 return true;
7109 }
7110 }
7111
7112 return false;
7113}
7114
Michael Wong65f367f2015-07-21 13:44:28 +00007115StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
7116 Stmt *AStmt,
7117 SourceLocation StartLoc,
7118 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007119 if (!AStmt)
7120 return StmtError();
7121
7122 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7123
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00007124 // OpenMP [2.10.1, Restrictions, p. 97]
7125 // At least one map clause must appear on the directive.
7126 if (!HasMapClause(Clauses)) {
David Majnemer9d168222016-08-05 17:44:54 +00007127 Diag(StartLoc, diag::err_omp_no_map_for_directive)
7128 << getOpenMPDirectiveName(OMPD_target_data);
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00007129 return StmtError();
7130 }
7131
Michael Wong65f367f2015-07-21 13:44:28 +00007132 getCurFunction()->setHasBranchProtectedScope();
7133
7134 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
7135 AStmt);
7136}
7137
Samuel Antaodf67fc42016-01-19 19:15:56 +00007138StmtResult
7139Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
7140 SourceLocation StartLoc,
7141 SourceLocation EndLoc) {
7142 // OpenMP [2.10.2, Restrictions, p. 99]
7143 // At least one map clause must appear on the directive.
7144 if (!HasMapClause(Clauses)) {
7145 Diag(StartLoc, diag::err_omp_no_map_for_directive)
7146 << getOpenMPDirectiveName(OMPD_target_enter_data);
7147 return StmtError();
7148 }
7149
7150 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
7151 Clauses);
7152}
7153
Samuel Antao72590762016-01-19 20:04:50 +00007154StmtResult
7155Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
7156 SourceLocation StartLoc,
7157 SourceLocation EndLoc) {
7158 // OpenMP [2.10.3, Restrictions, p. 102]
7159 // At least one map clause must appear on the directive.
7160 if (!HasMapClause(Clauses)) {
7161 Diag(StartLoc, diag::err_omp_no_map_for_directive)
7162 << getOpenMPDirectiveName(OMPD_target_exit_data);
7163 return StmtError();
7164 }
7165
7166 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
7167}
7168
Samuel Antao686c70c2016-05-26 17:30:50 +00007169StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
7170 SourceLocation StartLoc,
7171 SourceLocation EndLoc) {
Samuel Antao686c70c2016-05-26 17:30:50 +00007172 bool seenMotionClause = false;
Samuel Antao661c0902016-05-26 17:39:58 +00007173 for (auto *C : Clauses) {
Samuel Antaoec172c62016-05-26 17:49:04 +00007174 if (C->getClauseKind() == OMPC_to || C->getClauseKind() == OMPC_from)
Samuel Antao661c0902016-05-26 17:39:58 +00007175 seenMotionClause = true;
7176 }
Samuel Antao686c70c2016-05-26 17:30:50 +00007177 if (!seenMotionClause) {
7178 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
7179 return StmtError();
7180 }
7181 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses);
7182}
7183
Alexey Bataev13314bf2014-10-09 04:18:56 +00007184StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
7185 Stmt *AStmt, SourceLocation StartLoc,
7186 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007187 if (!AStmt)
7188 return StmtError();
7189
Alexey Bataev13314bf2014-10-09 04:18:56 +00007190 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7191 // 1.2.2 OpenMP Language Terminology
7192 // Structured block - An executable statement with a single entry at the
7193 // top and a single exit at the bottom.
7194 // The point of exit cannot be a branch out of the structured block.
7195 // longjmp() and throw() must not violate the entry/exit criteria.
7196 CS->getCapturedDecl()->setNothrow();
7197
7198 getCurFunction()->setHasBranchProtectedScope();
7199
7200 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
7201}
7202
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007203StmtResult
7204Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
7205 SourceLocation EndLoc,
7206 OpenMPDirectiveKind CancelRegion) {
7207 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
7208 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
7209 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
7210 << getOpenMPDirectiveName(CancelRegion);
7211 return StmtError();
7212 }
7213 if (DSAStack->isParentNowaitRegion()) {
7214 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
7215 return StmtError();
7216 }
7217 if (DSAStack->isParentOrderedRegion()) {
7218 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
7219 return StmtError();
7220 }
7221 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
7222 CancelRegion);
7223}
7224
Alexey Bataev87933c72015-09-18 08:07:34 +00007225StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
7226 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00007227 SourceLocation EndLoc,
7228 OpenMPDirectiveKind CancelRegion) {
7229 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
7230 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
7231 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
7232 << getOpenMPDirectiveName(CancelRegion);
7233 return StmtError();
7234 }
7235 if (DSAStack->isParentNowaitRegion()) {
7236 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
7237 return StmtError();
7238 }
7239 if (DSAStack->isParentOrderedRegion()) {
7240 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
7241 return StmtError();
7242 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00007243 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00007244 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
7245 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00007246}
7247
Alexey Bataev382967a2015-12-08 12:06:20 +00007248static bool checkGrainsizeNumTasksClauses(Sema &S,
7249 ArrayRef<OMPClause *> Clauses) {
7250 OMPClause *PrevClause = nullptr;
7251 bool ErrorFound = false;
7252 for (auto *C : Clauses) {
7253 if (C->getClauseKind() == OMPC_grainsize ||
7254 C->getClauseKind() == OMPC_num_tasks) {
7255 if (!PrevClause)
7256 PrevClause = C;
7257 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
7258 S.Diag(C->getLocStart(),
7259 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
7260 << getOpenMPClauseName(C->getClauseKind())
7261 << getOpenMPClauseName(PrevClause->getClauseKind());
7262 S.Diag(PrevClause->getLocStart(),
7263 diag::note_omp_previous_grainsize_num_tasks)
7264 << getOpenMPClauseName(PrevClause->getClauseKind());
7265 ErrorFound = true;
7266 }
7267 }
7268 }
7269 return ErrorFound;
7270}
7271
Alexey Bataev49f6e782015-12-01 04:18:41 +00007272StmtResult Sema::ActOnOpenMPTaskLoopDirective(
7273 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7274 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007275 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00007276 if (!AStmt)
7277 return StmtError();
7278
7279 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7280 OMPLoopDirective::HelperExprs B;
7281 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7282 // define the nested loops number.
7283 unsigned NestedLoopCount =
7284 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007285 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00007286 VarsWithImplicitDSA, B);
7287 if (NestedLoopCount == 0)
7288 return StmtError();
7289
7290 assert((CurContext->isDependentContext() || B.builtAll()) &&
7291 "omp for loop exprs were not built");
7292
Alexey Bataev382967a2015-12-08 12:06:20 +00007293 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7294 // The grainsize clause and num_tasks clause are mutually exclusive and may
7295 // not appear on the same taskloop directive.
7296 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7297 return StmtError();
7298
Alexey Bataev49f6e782015-12-01 04:18:41 +00007299 getCurFunction()->setHasBranchProtectedScope();
7300 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
7301 NestedLoopCount, Clauses, AStmt, B);
7302}
7303
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007304StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
7305 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7306 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007307 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007308 if (!AStmt)
7309 return StmtError();
7310
7311 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7312 OMPLoopDirective::HelperExprs B;
7313 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7314 // define the nested loops number.
7315 unsigned NestedLoopCount =
7316 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
7317 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
7318 VarsWithImplicitDSA, B);
7319 if (NestedLoopCount == 0)
7320 return StmtError();
7321
7322 assert((CurContext->isDependentContext() || B.builtAll()) &&
7323 "omp for loop exprs were not built");
7324
Alexey Bataev5a3af132016-03-29 08:58:54 +00007325 if (!CurContext->isDependentContext()) {
7326 // Finalize the clauses that need pre-built expressions for CodeGen.
7327 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007328 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007329 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007330 B.NumIterations, *this, CurScope,
7331 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007332 return StmtError();
7333 }
7334 }
7335
Alexey Bataev382967a2015-12-08 12:06:20 +00007336 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7337 // The grainsize clause and num_tasks clause are mutually exclusive and may
7338 // not appear on the same taskloop directive.
7339 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7340 return StmtError();
7341
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007342 getCurFunction()->setHasBranchProtectedScope();
7343 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
7344 NestedLoopCount, Clauses, AStmt, B);
7345}
7346
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007347StmtResult Sema::ActOnOpenMPDistributeDirective(
7348 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7349 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007350 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007351 if (!AStmt)
7352 return StmtError();
7353
7354 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7355 OMPLoopDirective::HelperExprs B;
7356 // In presence of clause 'collapse' with number of loops, it will
7357 // define the nested loops number.
7358 unsigned NestedLoopCount =
7359 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
7360 nullptr /*ordered not a clause on distribute*/, AStmt,
7361 *this, *DSAStack, VarsWithImplicitDSA, B);
7362 if (NestedLoopCount == 0)
7363 return StmtError();
7364
7365 assert((CurContext->isDependentContext() || B.builtAll()) &&
7366 "omp for loop exprs were not built");
7367
7368 getCurFunction()->setHasBranchProtectedScope();
7369 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
7370 NestedLoopCount, Clauses, AStmt, B);
7371}
7372
Carlo Bertolli9925f152016-06-27 14:55:37 +00007373StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
7374 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7375 SourceLocation EndLoc,
7376 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7377 if (!AStmt)
7378 return StmtError();
7379
7380 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7381 // 1.2.2 OpenMP Language Terminology
7382 // Structured block - An executable statement with a single entry at the
7383 // top and a single exit at the bottom.
7384 // The point of exit cannot be a branch out of the structured block.
7385 // longjmp() and throw() must not violate the entry/exit criteria.
7386 CS->getCapturedDecl()->setNothrow();
7387
7388 OMPLoopDirective::HelperExprs B;
7389 // In presence of clause 'collapse' with number of loops, it will
7390 // define the nested loops number.
7391 unsigned NestedLoopCount = CheckOpenMPLoop(
7392 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
7393 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
7394 VarsWithImplicitDSA, B);
7395 if (NestedLoopCount == 0)
7396 return StmtError();
7397
7398 assert((CurContext->isDependentContext() || B.builtAll()) &&
7399 "omp for loop exprs were not built");
7400
7401 getCurFunction()->setHasBranchProtectedScope();
7402 return OMPDistributeParallelForDirective::Create(
7403 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7404}
7405
Kelvin Li4a39add2016-07-05 05:00:15 +00007406StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
7407 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7408 SourceLocation EndLoc,
7409 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7410 if (!AStmt)
7411 return StmtError();
7412
7413 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7414 // 1.2.2 OpenMP Language Terminology
7415 // Structured block - An executable statement with a single entry at the
7416 // top and a single exit at the bottom.
7417 // The point of exit cannot be a branch out of the structured block.
7418 // longjmp() and throw() must not violate the entry/exit criteria.
7419 CS->getCapturedDecl()->setNothrow();
7420
7421 OMPLoopDirective::HelperExprs B;
7422 // In presence of clause 'collapse' with number of loops, it will
7423 // define the nested loops number.
7424 unsigned NestedLoopCount = CheckOpenMPLoop(
7425 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
7426 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
7427 VarsWithImplicitDSA, B);
7428 if (NestedLoopCount == 0)
7429 return StmtError();
7430
7431 assert((CurContext->isDependentContext() || B.builtAll()) &&
7432 "omp for loop exprs were not built");
7433
Kelvin Lic5609492016-07-15 04:39:07 +00007434 if (checkSimdlenSafelenSpecified(*this, Clauses))
7435 return StmtError();
7436
Kelvin Li4a39add2016-07-05 05:00:15 +00007437 getCurFunction()->setHasBranchProtectedScope();
7438 return OMPDistributeParallelForSimdDirective::Create(
7439 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7440}
7441
Kelvin Li787f3fc2016-07-06 04:45:38 +00007442StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
7443 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7444 SourceLocation EndLoc,
7445 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7446 if (!AStmt)
7447 return StmtError();
7448
7449 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7450 // 1.2.2 OpenMP Language Terminology
7451 // Structured block - An executable statement with a single entry at the
7452 // top and a single exit at the bottom.
7453 // The point of exit cannot be a branch out of the structured block.
7454 // longjmp() and throw() must not violate the entry/exit criteria.
7455 CS->getCapturedDecl()->setNothrow();
7456
7457 OMPLoopDirective::HelperExprs B;
7458 // In presence of clause 'collapse' with number of loops, it will
7459 // define the nested loops number.
7460 unsigned NestedLoopCount =
7461 CheckOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
7462 nullptr /*ordered not a clause on distribute*/, AStmt,
7463 *this, *DSAStack, VarsWithImplicitDSA, B);
7464 if (NestedLoopCount == 0)
7465 return StmtError();
7466
7467 assert((CurContext->isDependentContext() || B.builtAll()) &&
7468 "omp for loop exprs were not built");
7469
Kelvin Lic5609492016-07-15 04:39:07 +00007470 if (checkSimdlenSafelenSpecified(*this, Clauses))
7471 return StmtError();
7472
Kelvin Li787f3fc2016-07-06 04:45:38 +00007473 getCurFunction()->setHasBranchProtectedScope();
7474 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
7475 NestedLoopCount, Clauses, AStmt, B);
7476}
7477
Kelvin Lia579b912016-07-14 02:54:56 +00007478StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
7479 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7480 SourceLocation EndLoc,
7481 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7482 if (!AStmt)
7483 return StmtError();
7484
7485 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7486 // 1.2.2 OpenMP Language Terminology
7487 // Structured block - An executable statement with a single entry at the
7488 // top and a single exit at the bottom.
7489 // The point of exit cannot be a branch out of the structured block.
7490 // longjmp() and throw() must not violate the entry/exit criteria.
7491 CS->getCapturedDecl()->setNothrow();
7492
7493 OMPLoopDirective::HelperExprs B;
7494 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7495 // define the nested loops number.
7496 unsigned NestedLoopCount = CheckOpenMPLoop(
7497 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
7498 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
7499 VarsWithImplicitDSA, B);
7500 if (NestedLoopCount == 0)
7501 return StmtError();
7502
7503 assert((CurContext->isDependentContext() || B.builtAll()) &&
7504 "omp target parallel for simd loop exprs were not built");
7505
7506 if (!CurContext->isDependentContext()) {
7507 // Finalize the clauses that need pre-built expressions for CodeGen.
7508 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007509 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Lia579b912016-07-14 02:54:56 +00007510 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7511 B.NumIterations, *this, CurScope,
7512 DSAStack))
7513 return StmtError();
7514 }
7515 }
Kelvin Lic5609492016-07-15 04:39:07 +00007516 if (checkSimdlenSafelenSpecified(*this, Clauses))
Kelvin Lia579b912016-07-14 02:54:56 +00007517 return StmtError();
7518
7519 getCurFunction()->setHasBranchProtectedScope();
7520 return OMPTargetParallelForSimdDirective::Create(
7521 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7522}
7523
Kelvin Li986330c2016-07-20 22:57:10 +00007524StmtResult Sema::ActOnOpenMPTargetSimdDirective(
7525 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7526 SourceLocation EndLoc,
7527 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7528 if (!AStmt)
7529 return StmtError();
7530
7531 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7532 // 1.2.2 OpenMP Language Terminology
7533 // Structured block - An executable statement with a single entry at the
7534 // top and a single exit at the bottom.
7535 // The point of exit cannot be a branch out of the structured block.
7536 // longjmp() and throw() must not violate the entry/exit criteria.
7537 CS->getCapturedDecl()->setNothrow();
7538
7539 OMPLoopDirective::HelperExprs B;
7540 // In presence of clause 'collapse' with number of loops, it will define the
7541 // nested loops number.
David Majnemer9d168222016-08-05 17:44:54 +00007542 unsigned NestedLoopCount =
Kelvin Li986330c2016-07-20 22:57:10 +00007543 CheckOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
7544 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
7545 VarsWithImplicitDSA, B);
7546 if (NestedLoopCount == 0)
7547 return StmtError();
7548
7549 assert((CurContext->isDependentContext() || B.builtAll()) &&
7550 "omp target simd loop exprs were not built");
7551
7552 if (!CurContext->isDependentContext()) {
7553 // Finalize the clauses that need pre-built expressions for CodeGen.
7554 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007555 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Li986330c2016-07-20 22:57:10 +00007556 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7557 B.NumIterations, *this, CurScope,
7558 DSAStack))
7559 return StmtError();
7560 }
7561 }
7562
7563 if (checkSimdlenSafelenSpecified(*this, Clauses))
7564 return StmtError();
7565
7566 getCurFunction()->setHasBranchProtectedScope();
7567 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
7568 NestedLoopCount, Clauses, AStmt, B);
7569}
7570
Kelvin Li02532872016-08-05 14:37:37 +00007571StmtResult Sema::ActOnOpenMPTeamsDistributeDirective(
7572 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7573 SourceLocation EndLoc,
7574 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7575 if (!AStmt)
7576 return StmtError();
7577
7578 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7579 // 1.2.2 OpenMP Language Terminology
7580 // Structured block - An executable statement with a single entry at the
7581 // top and a single exit at the bottom.
7582 // The point of exit cannot be a branch out of the structured block.
7583 // longjmp() and throw() must not violate the entry/exit criteria.
7584 CS->getCapturedDecl()->setNothrow();
7585
7586 OMPLoopDirective::HelperExprs B;
7587 // In presence of clause 'collapse' with number of loops, it will
7588 // define the nested loops number.
7589 unsigned NestedLoopCount =
7590 CheckOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses),
7591 nullptr /*ordered not a clause on distribute*/, AStmt,
7592 *this, *DSAStack, VarsWithImplicitDSA, B);
7593 if (NestedLoopCount == 0)
7594 return StmtError();
7595
7596 assert((CurContext->isDependentContext() || B.builtAll()) &&
7597 "omp teams distribute loop exprs were not built");
7598
7599 getCurFunction()->setHasBranchProtectedScope();
David Majnemer9d168222016-08-05 17:44:54 +00007600 return OMPTeamsDistributeDirective::Create(
7601 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Kelvin Li02532872016-08-05 14:37:37 +00007602}
7603
Kelvin Li0e3bde82016-08-17 23:13:03 +00007604StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective(
7605 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7606 SourceLocation EndLoc,
7607 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7608 if (!AStmt)
7609 return StmtError();
7610
7611 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7612 // 1.2.2 OpenMP Language Terminology
7613 // Structured block - An executable statement with a single entry at the
7614 // top and a single exit at the bottom.
7615 // The point of exit cannot be a branch out of the structured block.
7616 // longjmp() and throw() must not violate the entry/exit criteria.
7617 CS->getCapturedDecl()->setNothrow();
7618
7619 OMPLoopDirective::HelperExprs B;
7620 // In presence of clause 'collapse' with number of loops, it will
7621 // define the nested loops number.
7622 unsigned NestedLoopCount =
7623 CheckOpenMPLoop(OMPD_teams_distribute_simd,
7624 getCollapseNumberExpr(Clauses),
7625 nullptr /*ordered not a clause on distribute*/, AStmt,
7626 *this, *DSAStack, VarsWithImplicitDSA, B);
7627
7628 if (NestedLoopCount == 0)
7629 return StmtError();
7630
7631 assert((CurContext->isDependentContext() || B.builtAll()) &&
7632 "omp teams distribute simd loop exprs were not built");
7633
7634 if (!CurContext->isDependentContext()) {
7635 // Finalize the clauses that need pre-built expressions for CodeGen.
7636 for (auto C : Clauses) {
7637 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7638 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7639 B.NumIterations, *this, CurScope,
7640 DSAStack))
7641 return StmtError();
7642 }
7643 }
7644
7645 if (checkSimdlenSafelenSpecified(*this, Clauses))
7646 return StmtError();
7647
7648 getCurFunction()->setHasBranchProtectedScope();
7649 return OMPTeamsDistributeSimdDirective::Create(
7650 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7651}
7652
Alexey Bataeved09d242014-05-28 05:53:51 +00007653OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007654 SourceLocation StartLoc,
7655 SourceLocation LParenLoc,
7656 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007657 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007658 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00007659 case OMPC_final:
7660 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
7661 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00007662 case OMPC_num_threads:
7663 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
7664 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007665 case OMPC_safelen:
7666 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
7667 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00007668 case OMPC_simdlen:
7669 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
7670 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00007671 case OMPC_collapse:
7672 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
7673 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00007674 case OMPC_ordered:
7675 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
7676 break;
Michael Wonge710d542015-08-07 16:16:36 +00007677 case OMPC_device:
7678 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
7679 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00007680 case OMPC_num_teams:
7681 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
7682 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007683 case OMPC_thread_limit:
7684 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
7685 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00007686 case OMPC_priority:
7687 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
7688 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007689 case OMPC_grainsize:
7690 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
7691 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00007692 case OMPC_num_tasks:
7693 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
7694 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00007695 case OMPC_hint:
7696 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
7697 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007698 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007699 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007700 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007701 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007702 case OMPC_private:
7703 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00007704 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007705 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007706 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00007707 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007708 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007709 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007710 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00007711 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007712 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007713 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007714 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007715 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007716 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007717 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007718 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007719 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007720 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007721 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00007722 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007723 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007724 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00007725 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007726 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007727 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007728 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007729 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007730 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007731 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007732 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007733 case OMPC_is_device_ptr:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007734 llvm_unreachable("Clause is not allowed.");
7735 }
7736 return Res;
7737}
7738
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007739OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
7740 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007741 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007742 SourceLocation NameModifierLoc,
7743 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007744 SourceLocation EndLoc) {
7745 Expr *ValExpr = Condition;
7746 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
7747 !Condition->isInstantiationDependent() &&
7748 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00007749 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007750 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007751 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007752
Richard Smith03a4aa32016-06-23 19:02:52 +00007753 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007754 }
7755
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007756 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
7757 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007758}
7759
Alexey Bataev3778b602014-07-17 07:32:53 +00007760OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
7761 SourceLocation StartLoc,
7762 SourceLocation LParenLoc,
7763 SourceLocation EndLoc) {
7764 Expr *ValExpr = Condition;
7765 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
7766 !Condition->isInstantiationDependent() &&
7767 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00007768 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataev3778b602014-07-17 07:32:53 +00007769 if (Val.isInvalid())
7770 return nullptr;
7771
Richard Smith03a4aa32016-06-23 19:02:52 +00007772 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataev3778b602014-07-17 07:32:53 +00007773 }
7774
7775 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
7776}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00007777ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
7778 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00007779 if (!Op)
7780 return ExprError();
7781
7782 class IntConvertDiagnoser : public ICEConvertDiagnoser {
7783 public:
7784 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00007785 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00007786 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
7787 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007788 return S.Diag(Loc, diag::err_omp_not_integral) << T;
7789 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007790 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
7791 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007792 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
7793 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007794 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
7795 QualType T,
7796 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007797 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
7798 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007799 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
7800 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007801 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00007802 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00007803 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007804 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
7805 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007806 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
7807 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007808 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
7809 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007810 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00007811 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00007812 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007813 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
7814 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007815 llvm_unreachable("conversion functions are permitted");
7816 }
7817 } ConvertDiagnoser;
7818 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
7819}
7820
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007821static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00007822 OpenMPClauseKind CKind,
7823 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007824 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
7825 !ValExpr->isInstantiationDependent()) {
7826 SourceLocation Loc = ValExpr->getExprLoc();
7827 ExprResult Value =
7828 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
7829 if (Value.isInvalid())
7830 return false;
7831
7832 ValExpr = Value.get();
7833 // The expression must evaluate to a non-negative integer value.
7834 llvm::APSInt Result;
7835 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00007836 Result.isSigned() &&
7837 !((!StrictlyPositive && Result.isNonNegative()) ||
7838 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007839 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00007840 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
7841 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007842 return false;
7843 }
7844 }
7845 return true;
7846}
7847
Alexey Bataev568a8332014-03-06 06:15:19 +00007848OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
7849 SourceLocation StartLoc,
7850 SourceLocation LParenLoc,
7851 SourceLocation EndLoc) {
7852 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00007853
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007854 // OpenMP [2.5, Restrictions]
7855 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00007856 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
7857 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007858 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00007859
Alexey Bataeved09d242014-05-28 05:53:51 +00007860 return new (Context)
7861 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00007862}
7863
Alexey Bataev62c87d22014-03-21 04:51:18 +00007864ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007865 OpenMPClauseKind CKind,
7866 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00007867 if (!E)
7868 return ExprError();
7869 if (E->isValueDependent() || E->isTypeDependent() ||
7870 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00007871 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007872 llvm::APSInt Result;
7873 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
7874 if (ICE.isInvalid())
7875 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007876 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
7877 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00007878 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007879 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
7880 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00007881 return ExprError();
7882 }
Alexander Musman09184fe2014-09-30 05:29:28 +00007883 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
7884 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
7885 << E->getSourceRange();
7886 return ExprError();
7887 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007888 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
7889 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00007890 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007891 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00007892 return ICE;
7893}
7894
7895OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
7896 SourceLocation LParenLoc,
7897 SourceLocation EndLoc) {
7898 // OpenMP [2.8.1, simd construct, Description]
7899 // The parameter of the safelen clause must be a constant
7900 // positive integer expression.
7901 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
7902 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007903 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007904 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00007905 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00007906}
7907
Alexey Bataev66b15b52015-08-21 11:14:16 +00007908OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
7909 SourceLocation LParenLoc,
7910 SourceLocation EndLoc) {
7911 // OpenMP [2.8.1, simd construct, Description]
7912 // The parameter of the simdlen clause must be a constant
7913 // positive integer expression.
7914 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
7915 if (Simdlen.isInvalid())
7916 return nullptr;
7917 return new (Context)
7918 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
7919}
7920
Alexander Musman64d33f12014-06-04 07:53:32 +00007921OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
7922 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00007923 SourceLocation LParenLoc,
7924 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00007925 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00007926 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00007927 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00007928 // The parameter of the collapse clause must be a constant
7929 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00007930 ExprResult NumForLoopsResult =
7931 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
7932 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00007933 return nullptr;
7934 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00007935 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00007936}
7937
Alexey Bataev10e775f2015-07-30 11:36:16 +00007938OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
7939 SourceLocation EndLoc,
7940 SourceLocation LParenLoc,
7941 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00007942 // OpenMP [2.7.1, loop construct, Description]
7943 // OpenMP [2.8.1, simd construct, Description]
7944 // OpenMP [2.9.6, distribute construct, Description]
7945 // The parameter of the ordered clause must be a constant
7946 // positive integer expression if any.
7947 if (NumForLoops && LParenLoc.isValid()) {
7948 ExprResult NumForLoopsResult =
7949 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
7950 if (NumForLoopsResult.isInvalid())
7951 return nullptr;
7952 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00007953 } else
7954 NumForLoops = nullptr;
7955 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00007956 return new (Context)
7957 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
7958}
7959
Alexey Bataeved09d242014-05-28 05:53:51 +00007960OMPClause *Sema::ActOnOpenMPSimpleClause(
7961 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
7962 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007963 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007964 switch (Kind) {
7965 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00007966 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00007967 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
7968 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007969 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007970 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00007971 Res = ActOnOpenMPProcBindClause(
7972 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
7973 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007974 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007975 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007976 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00007977 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00007978 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007979 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00007980 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007981 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007982 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007983 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00007984 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00007985 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007986 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00007987 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007988 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007989 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007990 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007991 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007992 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007993 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007994 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007995 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007996 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007997 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007998 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007999 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008000 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008001 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008002 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00008003 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00008004 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008005 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008006 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00008007 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008008 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00008009 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008010 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00008011 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00008012 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00008013 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008014 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008015 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008016 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008017 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00008018 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00008019 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00008020 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00008021 case OMPC_is_device_ptr:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008022 llvm_unreachable("Clause is not allowed.");
8023 }
8024 return Res;
8025}
8026
Alexey Bataev6402bca2015-12-28 07:25:51 +00008027static std::string
8028getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
8029 ArrayRef<unsigned> Exclude = llvm::None) {
8030 std::string Values;
8031 unsigned Bound = Last >= 2 ? Last - 2 : 0;
8032 unsigned Skipped = Exclude.size();
8033 auto S = Exclude.begin(), E = Exclude.end();
8034 for (unsigned i = First; i < Last; ++i) {
8035 if (std::find(S, E, i) != E) {
8036 --Skipped;
8037 continue;
8038 }
8039 Values += "'";
8040 Values += getOpenMPSimpleClauseTypeName(K, i);
8041 Values += "'";
8042 if (i == Bound - Skipped)
8043 Values += " or ";
8044 else if (i != Bound + 1 - Skipped)
8045 Values += ", ";
8046 }
8047 return Values;
8048}
8049
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008050OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
8051 SourceLocation KindKwLoc,
8052 SourceLocation StartLoc,
8053 SourceLocation LParenLoc,
8054 SourceLocation EndLoc) {
8055 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00008056 static_assert(OMPC_DEFAULT_unknown > 0,
8057 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008058 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008059 << getListOfPossibleValues(OMPC_default, /*First=*/0,
8060 /*Last=*/OMPC_DEFAULT_unknown)
8061 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008062 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008063 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00008064 switch (Kind) {
8065 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008066 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008067 break;
8068 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008069 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008070 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008071 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008072 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00008073 break;
8074 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008075 return new (Context)
8076 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008077}
8078
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008079OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
8080 SourceLocation KindKwLoc,
8081 SourceLocation StartLoc,
8082 SourceLocation LParenLoc,
8083 SourceLocation EndLoc) {
8084 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008085 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008086 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
8087 /*Last=*/OMPC_PROC_BIND_unknown)
8088 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008089 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008090 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008091 return new (Context)
8092 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008093}
8094
Alexey Bataev56dafe82014-06-20 07:16:17 +00008095OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00008096 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00008097 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00008098 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00008099 SourceLocation EndLoc) {
8100 OMPClause *Res = nullptr;
8101 switch (Kind) {
8102 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00008103 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
8104 assert(Argument.size() == NumberOfElements &&
8105 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00008106 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00008107 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
8108 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
8109 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
8110 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
8111 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00008112 break;
8113 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00008114 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
8115 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
8116 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
8117 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008118 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00008119 case OMPC_dist_schedule:
8120 Res = ActOnOpenMPDistScheduleClause(
8121 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
8122 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
8123 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008124 case OMPC_defaultmap:
8125 enum { Modifier, DefaultmapKind };
8126 Res = ActOnOpenMPDefaultmapClause(
8127 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
8128 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
David Majnemer9d168222016-08-05 17:44:54 +00008129 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind],
8130 EndLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008131 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00008132 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008133 case OMPC_num_threads:
8134 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008135 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008136 case OMPC_collapse:
8137 case OMPC_default:
8138 case OMPC_proc_bind:
8139 case OMPC_private:
8140 case OMPC_firstprivate:
8141 case OMPC_lastprivate:
8142 case OMPC_shared:
8143 case OMPC_reduction:
8144 case OMPC_linear:
8145 case OMPC_aligned:
8146 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008147 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008148 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00008149 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008150 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008151 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008152 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00008153 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008154 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008155 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008156 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008157 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008158 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008159 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00008160 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00008161 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008162 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008163 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00008164 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008165 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00008166 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008167 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00008168 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00008169 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00008170 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008171 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008172 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00008173 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00008174 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00008175 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00008176 case OMPC_is_device_ptr:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008177 llvm_unreachable("Clause is not allowed.");
8178 }
8179 return Res;
8180}
8181
Alexey Bataev6402bca2015-12-28 07:25:51 +00008182static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
8183 OpenMPScheduleClauseModifier M2,
8184 SourceLocation M1Loc, SourceLocation M2Loc) {
8185 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
8186 SmallVector<unsigned, 2> Excluded;
8187 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
8188 Excluded.push_back(M2);
8189 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
8190 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
8191 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
8192 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
8193 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
8194 << getListOfPossibleValues(OMPC_schedule,
8195 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
8196 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
8197 Excluded)
8198 << getOpenMPClauseName(OMPC_schedule);
8199 return true;
8200 }
8201 return false;
8202}
8203
Alexey Bataev56dafe82014-06-20 07:16:17 +00008204OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00008205 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00008206 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00008207 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
8208 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
8209 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
8210 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
8211 return nullptr;
8212 // OpenMP, 2.7.1, Loop Construct, Restrictions
8213 // Either the monotonic modifier or the nonmonotonic modifier can be specified
8214 // but not both.
8215 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
8216 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
8217 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
8218 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
8219 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
8220 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
8221 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
8222 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
8223 return nullptr;
8224 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00008225 if (Kind == OMPC_SCHEDULE_unknown) {
8226 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00008227 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
8228 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
8229 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
8230 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
8231 Exclude);
8232 } else {
8233 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
8234 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00008235 }
8236 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
8237 << Values << getOpenMPClauseName(OMPC_schedule);
8238 return nullptr;
8239 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00008240 // OpenMP, 2.7.1, Loop Construct, Restrictions
8241 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
8242 // schedule(guided).
8243 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
8244 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
8245 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
8246 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
8247 diag::err_omp_schedule_nonmonotonic_static);
8248 return nullptr;
8249 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00008250 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00008251 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00008252 if (ChunkSize) {
8253 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
8254 !ChunkSize->isInstantiationDependent() &&
8255 !ChunkSize->containsUnexpandedParameterPack()) {
8256 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
8257 ExprResult Val =
8258 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
8259 if (Val.isInvalid())
8260 return nullptr;
8261
8262 ValExpr = Val.get();
8263
8264 // OpenMP [2.7.1, Restrictions]
8265 // chunk_size must be a loop invariant integer expression with a positive
8266 // value.
8267 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00008268 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
8269 if (Result.isSigned() && !Result.isStrictlyPositive()) {
8270 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00008271 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00008272 return nullptr;
8273 }
Alexey Bataevb46cdea2016-06-15 11:20:48 +00008274 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
8275 !CurContext->isDependentContext()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00008276 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
8277 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8278 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00008279 }
8280 }
8281 }
8282
Alexey Bataev6402bca2015-12-28 07:25:51 +00008283 return new (Context)
8284 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00008285 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00008286}
8287
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008288OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
8289 SourceLocation StartLoc,
8290 SourceLocation EndLoc) {
8291 OMPClause *Res = nullptr;
8292 switch (Kind) {
8293 case OMPC_ordered:
8294 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
8295 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00008296 case OMPC_nowait:
8297 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
8298 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008299 case OMPC_untied:
8300 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
8301 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008302 case OMPC_mergeable:
8303 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
8304 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008305 case OMPC_read:
8306 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
8307 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00008308 case OMPC_write:
8309 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
8310 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00008311 case OMPC_update:
8312 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
8313 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00008314 case OMPC_capture:
8315 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
8316 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008317 case OMPC_seq_cst:
8318 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
8319 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00008320 case OMPC_threads:
8321 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
8322 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008323 case OMPC_simd:
8324 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
8325 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00008326 case OMPC_nogroup:
8327 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
8328 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008329 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00008330 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008331 case OMPC_num_threads:
8332 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008333 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008334 case OMPC_collapse:
8335 case OMPC_schedule:
8336 case OMPC_private:
8337 case OMPC_firstprivate:
8338 case OMPC_lastprivate:
8339 case OMPC_shared:
8340 case OMPC_reduction:
8341 case OMPC_linear:
8342 case OMPC_aligned:
8343 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008344 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008345 case OMPC_default:
8346 case OMPC_proc_bind:
8347 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00008348 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008349 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00008350 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008351 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00008352 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008353 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00008354 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008355 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00008356 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00008357 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008358 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008359 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008360 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008361 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00008362 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00008363 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00008364 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00008365 case OMPC_is_device_ptr:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008366 llvm_unreachable("Clause is not allowed.");
8367 }
8368 return Res;
8369}
8370
Alexey Bataev236070f2014-06-20 11:19:47 +00008371OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
8372 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00008373 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00008374 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
8375}
8376
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008377OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
8378 SourceLocation EndLoc) {
8379 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
8380}
8381
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008382OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
8383 SourceLocation EndLoc) {
8384 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
8385}
8386
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008387OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
8388 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008389 return new (Context) OMPReadClause(StartLoc, EndLoc);
8390}
8391
Alexey Bataevdea47612014-07-23 07:46:59 +00008392OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
8393 SourceLocation EndLoc) {
8394 return new (Context) OMPWriteClause(StartLoc, EndLoc);
8395}
8396
Alexey Bataev67a4f222014-07-23 10:25:33 +00008397OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
8398 SourceLocation EndLoc) {
8399 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
8400}
8401
Alexey Bataev459dec02014-07-24 06:46:57 +00008402OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
8403 SourceLocation EndLoc) {
8404 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
8405}
8406
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008407OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
8408 SourceLocation EndLoc) {
8409 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
8410}
8411
Alexey Bataev346265e2015-09-25 10:37:12 +00008412OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
8413 SourceLocation EndLoc) {
8414 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
8415}
8416
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008417OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
8418 SourceLocation EndLoc) {
8419 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
8420}
8421
Alexey Bataevb825de12015-12-07 10:51:44 +00008422OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
8423 SourceLocation EndLoc) {
8424 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
8425}
8426
Alexey Bataevc5e02582014-06-16 07:08:35 +00008427OMPClause *Sema::ActOnOpenMPVarListClause(
8428 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
8429 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
8430 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008431 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00008432 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
8433 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
8434 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008435 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008436 switch (Kind) {
8437 case OMPC_private:
8438 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8439 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008440 case OMPC_firstprivate:
8441 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8442 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008443 case OMPC_lastprivate:
8444 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8445 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008446 case OMPC_shared:
8447 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
8448 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008449 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00008450 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
8451 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008452 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00008453 case OMPC_linear:
8454 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00008455 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00008456 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008457 case OMPC_aligned:
8458 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
8459 ColonLoc, EndLoc);
8460 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008461 case OMPC_copyin:
8462 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
8463 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008464 case OMPC_copyprivate:
8465 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8466 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00008467 case OMPC_flush:
8468 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
8469 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008470 case OMPC_depend:
David Majnemer9d168222016-08-05 17:44:54 +00008471 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
Kelvin Li0bff7af2015-11-23 05:32:03 +00008472 StartLoc, LParenLoc, EndLoc);
8473 break;
8474 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00008475 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
8476 DepLinMapLoc, ColonLoc, VarList, StartLoc,
8477 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008478 break;
Samuel Antao661c0902016-05-26 17:39:58 +00008479 case OMPC_to:
8480 Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc);
8481 break;
Samuel Antaoec172c62016-05-26 17:49:04 +00008482 case OMPC_from:
8483 Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc);
8484 break;
Carlo Bertolli2404b172016-07-13 15:37:16 +00008485 case OMPC_use_device_ptr:
8486 Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
8487 break;
Carlo Bertolli70594e92016-07-13 17:16:49 +00008488 case OMPC_is_device_ptr:
8489 Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
8490 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008491 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00008492 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00008493 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00008494 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008495 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00008496 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008497 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008498 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008499 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008500 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00008501 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008502 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008503 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008504 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008505 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008506 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008507 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008508 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008509 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00008510 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00008511 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008512 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00008513 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008514 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00008515 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008516 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00008517 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00008518 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00008519 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008520 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008521 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008522 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008523 case OMPC_uniform:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008524 llvm_unreachable("Clause is not allowed.");
8525 }
8526 return Res;
8527}
8528
Alexey Bataev90c228f2016-02-08 09:29:13 +00008529ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00008530 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00008531 ExprResult Res = BuildDeclRefExpr(
8532 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
8533 if (!Res.isUsable())
8534 return ExprError();
8535 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
8536 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
8537 if (!Res.isUsable())
8538 return ExprError();
8539 }
8540 if (VK != VK_LValue && Res.get()->isGLValue()) {
8541 Res = DefaultLvalueConversion(Res.get());
8542 if (!Res.isUsable())
8543 return ExprError();
8544 }
8545 return Res;
8546}
8547
Alexey Bataev60da77e2016-02-29 05:54:20 +00008548static std::pair<ValueDecl *, bool>
8549getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
8550 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008551 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
8552 RefExpr->containsUnexpandedParameterPack())
8553 return std::make_pair(nullptr, true);
8554
Alexey Bataevd985eda2016-02-10 11:29:16 +00008555 // OpenMP [3.1, C/C++]
8556 // A list item is a variable name.
8557 // OpenMP [2.9.3.3, Restrictions, p.1]
8558 // A variable that is part of another variable (as an array or
8559 // structure element) cannot appear in a private clause.
8560 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008561 enum {
8562 NoArrayExpr = -1,
8563 ArraySubscript = 0,
8564 OMPArraySection = 1
8565 } IsArrayExpr = NoArrayExpr;
8566 if (AllowArraySection) {
8567 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
8568 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
8569 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
8570 Base = TempASE->getBase()->IgnoreParenImpCasts();
8571 RefExpr = Base;
8572 IsArrayExpr = ArraySubscript;
8573 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
8574 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
8575 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
8576 Base = TempOASE->getBase()->IgnoreParenImpCasts();
8577 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
8578 Base = TempASE->getBase()->IgnoreParenImpCasts();
8579 RefExpr = Base;
8580 IsArrayExpr = OMPArraySection;
8581 }
8582 }
8583 ELoc = RefExpr->getExprLoc();
8584 ERange = RefExpr->getSourceRange();
8585 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008586 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
8587 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
8588 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
8589 (S.getCurrentThisType().isNull() || !ME ||
8590 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
8591 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008592 if (IsArrayExpr != NoArrayExpr)
8593 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
8594 << ERange;
8595 else {
8596 S.Diag(ELoc,
8597 AllowArraySection
8598 ? diag::err_omp_expected_var_name_member_expr_or_array_item
8599 : diag::err_omp_expected_var_name_member_expr)
8600 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
8601 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008602 return std::make_pair(nullptr, false);
8603 }
8604 return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
8605}
8606
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008607OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
8608 SourceLocation StartLoc,
8609 SourceLocation LParenLoc,
8610 SourceLocation EndLoc) {
8611 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00008612 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00008613 for (auto &RefExpr : VarList) {
8614 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008615 SourceLocation ELoc;
8616 SourceRange ERange;
8617 Expr *SimpleRefExpr = RefExpr;
8618 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008619 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008620 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008621 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008622 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008623 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008624 ValueDecl *D = Res.first;
8625 if (!D)
8626 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008627
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008628 QualType Type = D->getType();
8629 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008630
8631 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8632 // A variable that appears in a private clause must not have an incomplete
8633 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008634 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008635 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008636 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008637
Alexey Bataev758e55e2013-09-06 18:03:48 +00008638 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8639 // in a Construct]
8640 // Variables with the predetermined data-sharing attributes may not be
8641 // listed in data-sharing attributes clauses, except for the cases
8642 // listed below. For these exceptions only, listing a predetermined
8643 // variable in a data-sharing attribute clause is allowed and overrides
8644 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008645 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008646 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00008647 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8648 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008649 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008650 continue;
8651 }
8652
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008653 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008654 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00008655 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008656 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
8657 << getOpenMPClauseName(OMPC_private) << Type
8658 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8659 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008660 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008661 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008662 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008663 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008664 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008665 continue;
8666 }
8667
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008668 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
8669 // A list item cannot appear in both a map clause and a data-sharing
8670 // attribute clause on the same construct
8671 if (DSAStack->getCurrentDirective() == OMPD_target) {
Samuel Antao6890b092016-07-28 14:25:09 +00008672 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +00008673 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +00008674 VD, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +00008675 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
8676 OpenMPClauseKind WhereFoundClauseKind) -> bool {
8677 ConflictKind = WhereFoundClauseKind;
8678 return true;
8679 })) {
8680 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008681 << getOpenMPClauseName(OMPC_private)
Samuel Antao6890b092016-07-28 14:25:09 +00008682 << getOpenMPClauseName(ConflictKind)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008683 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8684 ReportOriginalDSA(*this, DSAStack, D, DVar);
8685 continue;
8686 }
8687 }
8688
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008689 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
8690 // A variable of class type (or array thereof) that appears in a private
8691 // clause requires an accessible, unambiguous default constructor for the
8692 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00008693 // Generate helper private variable and initialize it with the default
8694 // value. The address of the original variable is replaced by the address of
8695 // the new private variable in CodeGen. This new variable is not added to
8696 // IdResolver, so the code in the OpenMP region uses original variable for
8697 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008698 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008699 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
8700 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008701 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008702 if (VDPrivate->isInvalidDecl())
8703 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008704 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008705 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008706
Alexey Bataev90c228f2016-02-08 09:29:13 +00008707 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008708 if (!VD && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00008709 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00008710 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008711 Vars.push_back((VD || CurContext->isDependentContext())
8712 ? RefExpr->IgnoreParens()
8713 : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008714 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008715 }
8716
Alexey Bataeved09d242014-05-28 05:53:51 +00008717 if (Vars.empty())
8718 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008719
Alexey Bataev03b340a2014-10-21 03:16:40 +00008720 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8721 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008722}
8723
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008724namespace {
8725class DiagsUninitializedSeveretyRAII {
8726private:
8727 DiagnosticsEngine &Diags;
8728 SourceLocation SavedLoc;
8729 bool IsIgnored;
8730
8731public:
8732 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
8733 bool IsIgnored)
8734 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
8735 if (!IsIgnored) {
8736 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
8737 /*Map*/ diag::Severity::Ignored, Loc);
8738 }
8739 }
8740 ~DiagsUninitializedSeveretyRAII() {
8741 if (!IsIgnored)
8742 Diags.popMappings(SavedLoc);
8743 }
8744};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00008745}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008746
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008747OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
8748 SourceLocation StartLoc,
8749 SourceLocation LParenLoc,
8750 SourceLocation EndLoc) {
8751 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008752 SmallVector<Expr *, 8> PrivateCopies;
8753 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00008754 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008755 bool IsImplicitClause =
8756 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
8757 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
8758
Alexey Bataeved09d242014-05-28 05:53:51 +00008759 for (auto &RefExpr : VarList) {
8760 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008761 SourceLocation ELoc;
8762 SourceRange ERange;
8763 Expr *SimpleRefExpr = RefExpr;
8764 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008765 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008766 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008767 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008768 PrivateCopies.push_back(nullptr);
8769 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008770 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008771 ValueDecl *D = Res.first;
8772 if (!D)
8773 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008774
Alexey Bataev60da77e2016-02-29 05:54:20 +00008775 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00008776 QualType Type = D->getType();
8777 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008778
8779 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8780 // A variable that appears in a private clause must not have an incomplete
8781 // type or a reference type.
8782 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00008783 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008784 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008785 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008786
8787 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
8788 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00008789 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008790 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008791 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008792
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008793 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00008794 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008795 if (!IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008796 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev005248a2016-02-25 05:25:57 +00008797 TopDVar = DVar;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008798 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008799 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
8800 // A list item that specifies a given variable may not appear in more
8801 // than one clause on the same directive, except that a variable may be
8802 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008803 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00008804 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008805 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00008806 << getOpenMPClauseName(DVar.CKind)
8807 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008808 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008809 continue;
8810 }
8811
8812 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8813 // in a Construct]
8814 // Variables with the predetermined data-sharing attributes may not be
8815 // listed in data-sharing attributes clauses, except for the cases
8816 // listed below. For these exceptions only, listing a predetermined
8817 // variable in a data-sharing attribute clause is allowed and overrides
8818 // the variable's predetermined data-sharing attributes.
8819 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8820 // in a Construct, C/C++, p.2]
8821 // Variables with const-qualified type having no mutable member may be
8822 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00008823 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008824 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
8825 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00008826 << getOpenMPClauseName(DVar.CKind)
8827 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008828 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008829 continue;
8830 }
8831
Alexey Bataevf29276e2014-06-18 04:14:57 +00008832 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008833 // OpenMP [2.9.3.4, Restrictions, p.2]
8834 // A list item that is private within a parallel region must not appear
8835 // in a firstprivate clause on a worksharing construct if any of the
8836 // worksharing regions arising from the worksharing construct ever bind
8837 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00008838 if (isOpenMPWorksharingDirective(CurrDir) &&
8839 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008840 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008841 if (DVar.CKind != OMPC_shared &&
8842 (isOpenMPParallelDirective(DVar.DKind) ||
8843 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00008844 Diag(ELoc, diag::err_omp_required_access)
8845 << getOpenMPClauseName(OMPC_firstprivate)
8846 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008847 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008848 continue;
8849 }
8850 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008851 // OpenMP [2.9.3.4, Restrictions, p.3]
8852 // A list item that appears in a reduction clause of a parallel construct
8853 // must not appear in a firstprivate clause on a worksharing or task
8854 // construct if any of the worksharing or task regions arising from the
8855 // worksharing or task construct ever bind to any of the parallel regions
8856 // arising from the parallel construct.
8857 // OpenMP [2.9.3.4, Restrictions, p.4]
8858 // A list item that appears in a reduction clause in worksharing
8859 // construct must not appear in a firstprivate clause in a task construct
8860 // encountered during execution of any of the worksharing regions arising
8861 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +00008862 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008863 DVar = DSAStack->hasInnermostDSA(
8864 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
8865 [](OpenMPDirectiveKind K) -> bool {
8866 return isOpenMPParallelDirective(K) ||
8867 isOpenMPWorksharingDirective(K);
8868 },
8869 false);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008870 if (DVar.CKind == OMPC_reduction &&
8871 (isOpenMPParallelDirective(DVar.DKind) ||
8872 isOpenMPWorksharingDirective(DVar.DKind))) {
8873 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
8874 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008875 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008876 continue;
8877 }
8878 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008879
8880 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
8881 // A list item that is private within a teams region must not appear in a
8882 // firstprivate clause on a distribute construct if any of the distribute
8883 // regions arising from the distribute construct ever bind to any of the
8884 // teams regions arising from the teams construct.
8885 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
8886 // A list item that appears in a reduction clause of a teams construct
8887 // must not appear in a firstprivate clause on a distribute construct if
8888 // any of the distribute regions arising from the distribute construct
8889 // ever bind to any of the teams regions arising from the teams construct.
8890 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8891 // A list item may appear in a firstprivate or lastprivate clause but not
8892 // both.
8893 if (CurrDir == OMPD_distribute) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008894 DVar = DSAStack->hasInnermostDSA(
8895 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_private; },
8896 [](OpenMPDirectiveKind K) -> bool {
8897 return isOpenMPTeamsDirective(K);
8898 },
8899 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008900 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
8901 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008902 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008903 continue;
8904 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008905 DVar = DSAStack->hasInnermostDSA(
8906 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
8907 [](OpenMPDirectiveKind K) -> bool {
8908 return isOpenMPTeamsDirective(K);
8909 },
8910 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008911 if (DVar.CKind == OMPC_reduction &&
8912 isOpenMPTeamsDirective(DVar.DKind)) {
8913 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008914 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008915 continue;
8916 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008917 DVar = DSAStack->getTopDSA(D, false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008918 if (DVar.CKind == OMPC_lastprivate) {
8919 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008920 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008921 continue;
8922 }
8923 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008924 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
8925 // A list item cannot appear in both a map clause and a data-sharing
8926 // attribute clause on the same construct
8927 if (CurrDir == OMPD_target) {
Samuel Antao6890b092016-07-28 14:25:09 +00008928 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +00008929 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +00008930 VD, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +00008931 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
8932 OpenMPClauseKind WhereFoundClauseKind) -> bool {
8933 ConflictKind = WhereFoundClauseKind;
8934 return true;
8935 })) {
8936 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008937 << getOpenMPClauseName(OMPC_firstprivate)
Samuel Antao6890b092016-07-28 14:25:09 +00008938 << getOpenMPClauseName(ConflictKind)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008939 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8940 ReportOriginalDSA(*this, DSAStack, D, DVar);
8941 continue;
8942 }
8943 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008944 }
8945
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008946 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008947 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00008948 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008949 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
8950 << getOpenMPClauseName(OMPC_firstprivate) << Type
8951 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8952 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00008953 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008954 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00008955 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008956 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00008957 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008958 continue;
8959 }
8960
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008961 Type = Type.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008962 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
8963 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008964 // Generate helper private variable and initialize it with the value of the
8965 // original variable. The address of the original variable is replaced by
8966 // the address of the new private variable in the CodeGen. This new variable
8967 // is not added to IdResolver, so the code in the OpenMP region uses
8968 // original variable for proper diagnostics and variable capturing.
8969 Expr *VDInitRefExpr = nullptr;
8970 // For arrays generate initializer for single element and replace it by the
8971 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008972 if (Type->isArrayType()) {
8973 auto VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00008974 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008975 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008976 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008977 ElemType = ElemType.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008978 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008979 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00008980 InitializedEntity Entity =
8981 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008982 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
8983
8984 InitializationSequence InitSeq(*this, Entity, Kind, Init);
8985 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
8986 if (Result.isInvalid())
8987 VDPrivate->setInvalidDecl();
8988 else
8989 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008990 // Remove temp variable declaration.
8991 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008992 } else {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008993 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
8994 ".firstprivate.temp");
8995 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
8996 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00008997 AddInitializerToDecl(VDPrivate,
8998 DefaultLvalueConversion(VDInitRefExpr).get(),
8999 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009000 }
9001 if (VDPrivate->isInvalidDecl()) {
9002 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00009003 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009004 diag::note_omp_task_predetermined_firstprivate_here);
9005 }
9006 continue;
9007 }
9008 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00009009 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00009010 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
9011 RefExpr->getExprLoc());
9012 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009013 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev005248a2016-02-25 05:25:57 +00009014 if (TopDVar.CKind == OMPC_lastprivate)
9015 Ref = TopDVar.PrivateCopy;
9016 else {
Alexey Bataev61205072016-03-02 04:57:40 +00009017 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataev005248a2016-02-25 05:25:57 +00009018 if (!IsOpenMPCapturedDecl(D))
9019 ExprCaptures.push_back(Ref->getDecl());
9020 }
Alexey Bataev417089f2016-02-17 13:19:37 +00009021 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009022 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009023 Vars.push_back((VD || CurContext->isDependentContext())
9024 ? RefExpr->IgnoreParens()
9025 : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009026 PrivateCopies.push_back(VDPrivateRefExpr);
9027 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009028 }
9029
Alexey Bataeved09d242014-05-28 05:53:51 +00009030 if (Vars.empty())
9031 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009032
9033 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009034 Vars, PrivateCopies, Inits,
9035 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009036}
9037
Alexander Musman1bb328c2014-06-04 13:06:39 +00009038OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
9039 SourceLocation StartLoc,
9040 SourceLocation LParenLoc,
9041 SourceLocation EndLoc) {
9042 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00009043 SmallVector<Expr *, 8> SrcExprs;
9044 SmallVector<Expr *, 8> DstExprs;
9045 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +00009046 SmallVector<Decl *, 4> ExprCaptures;
9047 SmallVector<Expr *, 4> ExprPostUpdates;
Alexander Musman1bb328c2014-06-04 13:06:39 +00009048 for (auto &RefExpr : VarList) {
9049 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009050 SourceLocation ELoc;
9051 SourceRange ERange;
9052 Expr *SimpleRefExpr = RefExpr;
9053 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +00009054 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00009055 // It will be analyzed later.
9056 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00009057 SrcExprs.push_back(nullptr);
9058 DstExprs.push_back(nullptr);
9059 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00009060 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00009061 ValueDecl *D = Res.first;
9062 if (!D)
9063 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00009064
Alexey Bataev74caaf22016-02-20 04:09:36 +00009065 QualType Type = D->getType();
9066 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +00009067
9068 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
9069 // A variable that appears in a lastprivate clause must not have an
9070 // incomplete type or a reference type.
9071 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +00009072 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +00009073 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009074 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00009075
9076 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
9077 // in a Construct]
9078 // Variables with the predetermined data-sharing attributes may not be
9079 // listed in data-sharing attributes clauses, except for the cases
9080 // listed below.
Alexey Bataev74caaf22016-02-20 04:09:36 +00009081 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00009082 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
9083 DVar.CKind != OMPC_firstprivate &&
9084 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
9085 Diag(ELoc, diag::err_omp_wrong_dsa)
9086 << getOpenMPClauseName(DVar.CKind)
9087 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev74caaf22016-02-20 04:09:36 +00009088 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00009089 continue;
9090 }
9091
Alexey Bataevf29276e2014-06-18 04:14:57 +00009092 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
9093 // OpenMP [2.14.3.5, Restrictions, p.2]
9094 // A list item that is private within a parallel region, or that appears in
9095 // the reduction clause of a parallel construct, must not appear in a
9096 // lastprivate clause on a worksharing construct if any of the corresponding
9097 // worksharing regions ever binds to any of the corresponding parallel
9098 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00009099 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00009100 if (isOpenMPWorksharingDirective(CurrDir) &&
9101 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +00009102 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00009103 if (DVar.CKind != OMPC_shared) {
9104 Diag(ELoc, diag::err_omp_required_access)
9105 << getOpenMPClauseName(OMPC_lastprivate)
9106 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev74caaf22016-02-20 04:09:36 +00009107 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00009108 continue;
9109 }
9110 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00009111
9112 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
9113 // A list item may appear in a firstprivate or lastprivate clause but not
9114 // both.
9115 if (CurrDir == OMPD_distribute) {
9116 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
9117 if (DVar.CKind == OMPC_firstprivate) {
9118 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
9119 ReportOriginalDSA(*this, DSAStack, D, DVar);
9120 continue;
9121 }
9122 }
9123
Alexander Musman1bb328c2014-06-04 13:06:39 +00009124 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00009125 // A variable of class type (or array thereof) that appears in a
9126 // lastprivate clause requires an accessible, unambiguous default
9127 // constructor for the class type, unless the list item is also specified
9128 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00009129 // A variable of class type (or array thereof) that appears in a
9130 // lastprivate clause requires an accessible, unambiguous copy assignment
9131 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00009132 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009133 auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009134 Type.getUnqualifiedType(), ".lastprivate.src",
Alexey Bataev74caaf22016-02-20 04:09:36 +00009135 D->hasAttrs() ? &D->getAttrs() : nullptr);
9136 auto *PseudoSrcExpr =
9137 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00009138 auto *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +00009139 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +00009140 D->hasAttrs() ? &D->getAttrs() : nullptr);
9141 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00009142 // For arrays generate assignment operation for single element and replace
9143 // it by the original array element in CodeGen.
Alexey Bataev74caaf22016-02-20 04:09:36 +00009144 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
Alexey Bataev38e89532015-04-16 04:54:05 +00009145 PseudoDstExpr, PseudoSrcExpr);
9146 if (AssignmentOp.isInvalid())
9147 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +00009148 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00009149 /*DiscardedValue=*/true);
9150 if (AssignmentOp.isInvalid())
9151 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00009152
Alexey Bataev74caaf22016-02-20 04:09:36 +00009153 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009154 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev005248a2016-02-25 05:25:57 +00009155 if (TopDVar.CKind == OMPC_firstprivate)
9156 Ref = TopDVar.PrivateCopy;
9157 else {
Alexey Bataev61205072016-03-02 04:57:40 +00009158 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00009159 if (!IsOpenMPCapturedDecl(D))
9160 ExprCaptures.push_back(Ref->getDecl());
9161 }
9162 if (TopDVar.CKind == OMPC_firstprivate ||
9163 (!IsOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009164 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +00009165 ExprResult RefRes = DefaultLvalueConversion(Ref);
9166 if (!RefRes.isUsable())
9167 continue;
9168 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +00009169 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
9170 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +00009171 if (!PostUpdateRes.isUsable())
9172 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +00009173 ExprPostUpdates.push_back(
9174 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +00009175 }
9176 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00009177 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009178 Vars.push_back((VD || CurContext->isDependentContext())
9179 ? RefExpr->IgnoreParens()
9180 : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +00009181 SrcExprs.push_back(PseudoSrcExpr);
9182 DstExprs.push_back(PseudoDstExpr);
9183 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00009184 }
9185
9186 if (Vars.empty())
9187 return nullptr;
9188
9189 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +00009190 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009191 buildPreInits(Context, ExprCaptures),
9192 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +00009193}
9194
Alexey Bataev758e55e2013-09-06 18:03:48 +00009195OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
9196 SourceLocation StartLoc,
9197 SourceLocation LParenLoc,
9198 SourceLocation EndLoc) {
9199 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00009200 for (auto &RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009201 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009202 SourceLocation ELoc;
9203 SourceRange ERange;
9204 Expr *SimpleRefExpr = RefExpr;
9205 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009206 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00009207 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009208 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009209 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009210 ValueDecl *D = Res.first;
9211 if (!D)
9212 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +00009213
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009214 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009215 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9216 // in a Construct]
9217 // Variables with the predetermined data-sharing attributes may not be
9218 // listed in data-sharing attributes clauses, except for the cases
9219 // listed below. For these exceptions only, listing a predetermined
9220 // variable in a data-sharing attribute clause is allowed and overrides
9221 // the variable's predetermined data-sharing attributes.
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009222 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00009223 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
9224 DVar.RefExpr) {
9225 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9226 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009227 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009228 continue;
9229 }
9230
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009231 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009232 if (!VD && IsOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00009233 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009234 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009235 Vars.push_back((VD || !Ref || CurContext->isDependentContext())
9236 ? RefExpr->IgnoreParens()
9237 : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009238 }
9239
Alexey Bataeved09d242014-05-28 05:53:51 +00009240 if (Vars.empty())
9241 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00009242
9243 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
9244}
9245
Alexey Bataevc5e02582014-06-16 07:08:35 +00009246namespace {
9247class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
9248 DSAStackTy *Stack;
9249
9250public:
9251 bool VisitDeclRefExpr(DeclRefExpr *E) {
9252 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009253 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00009254 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
9255 return false;
9256 if (DVar.CKind != OMPC_unknown)
9257 return true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +00009258 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
9259 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
9260 false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00009261 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00009262 return true;
9263 return false;
9264 }
9265 return false;
9266 }
9267 bool VisitStmt(Stmt *S) {
9268 for (auto Child : S->children()) {
9269 if (Child && Visit(Child))
9270 return true;
9271 }
9272 return false;
9273 }
Alexey Bataev23b69422014-06-18 07:08:49 +00009274 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00009275};
Alexey Bataev23b69422014-06-18 07:08:49 +00009276} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00009277
Alexey Bataev60da77e2016-02-29 05:54:20 +00009278namespace {
9279// Transform MemberExpression for specified FieldDecl of current class to
9280// DeclRefExpr to specified OMPCapturedExprDecl.
9281class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
9282 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
9283 ValueDecl *Field;
9284 DeclRefExpr *CapturedExpr;
9285
9286public:
9287 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
9288 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
9289
9290 ExprResult TransformMemberExpr(MemberExpr *E) {
9291 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
9292 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +00009293 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009294 return CapturedExpr;
9295 }
9296 return BaseTransform::TransformMemberExpr(E);
9297 }
9298 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
9299};
9300} // namespace
9301
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009302template <typename T>
9303static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups,
9304 const llvm::function_ref<T(ValueDecl *)> &Gen) {
9305 for (auto &Set : Lookups) {
9306 for (auto *D : Set) {
9307 if (auto Res = Gen(cast<ValueDecl>(D)))
9308 return Res;
9309 }
9310 }
9311 return T();
9312}
9313
9314static ExprResult
9315buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
9316 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
9317 const DeclarationNameInfo &ReductionId, QualType Ty,
9318 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
9319 if (ReductionIdScopeSpec.isInvalid())
9320 return ExprError();
9321 SmallVector<UnresolvedSet<8>, 4> Lookups;
9322 if (S) {
9323 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
9324 Lookup.suppressDiagnostics();
9325 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
9326 auto *D = Lookup.getRepresentativeDecl();
9327 do {
9328 S = S->getParent();
9329 } while (S && !S->isDeclScope(D));
9330 if (S)
9331 S = S->getParent();
9332 Lookups.push_back(UnresolvedSet<8>());
9333 Lookups.back().append(Lookup.begin(), Lookup.end());
9334 Lookup.clear();
9335 }
9336 } else if (auto *ULE =
9337 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
9338 Lookups.push_back(UnresolvedSet<8>());
9339 Decl *PrevD = nullptr;
David Majnemer9d168222016-08-05 17:44:54 +00009340 for (auto *D : ULE->decls()) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009341 if (D == PrevD)
9342 Lookups.push_back(UnresolvedSet<8>());
9343 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
9344 Lookups.back().addDecl(DRD);
9345 PrevD = D;
9346 }
9347 }
9348 if (Ty->isDependentType() || Ty->isInstantiationDependentType() ||
9349 Ty->containsUnexpandedParameterPack() ||
9350 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool {
9351 return !D->isInvalidDecl() &&
9352 (D->getType()->isDependentType() ||
9353 D->getType()->isInstantiationDependentType() ||
9354 D->getType()->containsUnexpandedParameterPack());
9355 })) {
9356 UnresolvedSet<8> ResSet;
9357 for (auto &Set : Lookups) {
9358 ResSet.append(Set.begin(), Set.end());
9359 // The last item marks the end of all declarations at the specified scope.
9360 ResSet.addDecl(Set[Set.size() - 1]);
9361 }
9362 return UnresolvedLookupExpr::Create(
9363 SemaRef.Context, /*NamingClass=*/nullptr,
9364 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
9365 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
9366 }
9367 if (auto *VD = filterLookupForUDR<ValueDecl *>(
9368 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
9369 if (!D->isInvalidDecl() &&
9370 SemaRef.Context.hasSameType(D->getType(), Ty))
9371 return D;
9372 return nullptr;
9373 }))
9374 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
9375 if (auto *VD = filterLookupForUDR<ValueDecl *>(
9376 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
9377 if (!D->isInvalidDecl() &&
9378 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
9379 !Ty.isMoreQualifiedThan(D->getType()))
9380 return D;
9381 return nullptr;
9382 })) {
9383 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
9384 /*DetectVirtual=*/false);
9385 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
9386 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
9387 VD->getType().getUnqualifiedType()))) {
9388 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
9389 /*DiagID=*/0) !=
9390 Sema::AR_inaccessible) {
9391 SemaRef.BuildBasePathArray(Paths, BasePath);
9392 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
9393 }
9394 }
9395 }
9396 }
9397 if (ReductionIdScopeSpec.isSet()) {
9398 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
9399 return ExprError();
9400 }
9401 return ExprEmpty();
9402}
9403
Alexey Bataevc5e02582014-06-16 07:08:35 +00009404OMPClause *Sema::ActOnOpenMPReductionClause(
9405 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
9406 SourceLocation ColonLoc, SourceLocation EndLoc,
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009407 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
9408 ArrayRef<Expr *> UnresolvedReductions) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00009409 auto DN = ReductionId.getName();
9410 auto OOK = DN.getCXXOverloadedOperator();
9411 BinaryOperatorKind BOK = BO_Comma;
9412
9413 // OpenMP [2.14.3.6, reduction clause]
9414 // C
9415 // reduction-identifier is either an identifier or one of the following
9416 // operators: +, -, *, &, |, ^, && and ||
9417 // C++
9418 // reduction-identifier is either an id-expression or one of the following
9419 // operators: +, -, *, &, |, ^, && and ||
9420 // FIXME: Only 'min' and 'max' identifiers are supported for now.
9421 switch (OOK) {
9422 case OO_Plus:
9423 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009424 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009425 break;
9426 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009427 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009428 break;
9429 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009430 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009431 break;
9432 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009433 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009434 break;
9435 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009436 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009437 break;
9438 case OO_AmpAmp:
9439 BOK = BO_LAnd;
9440 break;
9441 case OO_PipePipe:
9442 BOK = BO_LOr;
9443 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009444 case OO_New:
9445 case OO_Delete:
9446 case OO_Array_New:
9447 case OO_Array_Delete:
9448 case OO_Slash:
9449 case OO_Percent:
9450 case OO_Tilde:
9451 case OO_Exclaim:
9452 case OO_Equal:
9453 case OO_Less:
9454 case OO_Greater:
9455 case OO_LessEqual:
9456 case OO_GreaterEqual:
9457 case OO_PlusEqual:
9458 case OO_MinusEqual:
9459 case OO_StarEqual:
9460 case OO_SlashEqual:
9461 case OO_PercentEqual:
9462 case OO_CaretEqual:
9463 case OO_AmpEqual:
9464 case OO_PipeEqual:
9465 case OO_LessLess:
9466 case OO_GreaterGreater:
9467 case OO_LessLessEqual:
9468 case OO_GreaterGreaterEqual:
9469 case OO_EqualEqual:
9470 case OO_ExclaimEqual:
9471 case OO_PlusPlus:
9472 case OO_MinusMinus:
9473 case OO_Comma:
9474 case OO_ArrowStar:
9475 case OO_Arrow:
9476 case OO_Call:
9477 case OO_Subscript:
9478 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00009479 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009480 case NUM_OVERLOADED_OPERATORS:
9481 llvm_unreachable("Unexpected reduction identifier");
9482 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00009483 if (auto II = DN.getAsIdentifierInfo()) {
9484 if (II->isStr("max"))
9485 BOK = BO_GT;
9486 else if (II->isStr("min"))
9487 BOK = BO_LT;
9488 }
9489 break;
9490 }
9491 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009492 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +00009493 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009494 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009495
9496 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009497 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009498 SmallVector<Expr *, 8> LHSs;
9499 SmallVector<Expr *, 8> RHSs;
9500 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev61205072016-03-02 04:57:40 +00009501 SmallVector<Decl *, 4> ExprCaptures;
9502 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009503 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
9504 bool FirstIter = true;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009505 for (auto RefExpr : VarList) {
9506 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +00009507 // OpenMP [2.1, C/C++]
9508 // A list item is a variable or array section, subject to the restrictions
9509 // specified in Section 2.4 on page 42 and in each of the sections
9510 // describing clauses and directives for which a list appears.
9511 // OpenMP [2.14.3.3, Restrictions, p.1]
9512 // A variable that is part of another variable (as an array or
9513 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009514 if (!FirstIter && IR != ER)
9515 ++IR;
9516 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009517 SourceLocation ELoc;
9518 SourceRange ERange;
9519 Expr *SimpleRefExpr = RefExpr;
9520 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9521 /*AllowArraySection=*/true);
9522 if (Res.second) {
9523 // It will be analyzed later.
9524 Vars.push_back(RefExpr);
9525 Privates.push_back(nullptr);
9526 LHSs.push_back(nullptr);
9527 RHSs.push_back(nullptr);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009528 // Try to find 'declare reduction' corresponding construct before using
9529 // builtin/overloaded operators.
9530 QualType Type = Context.DependentTy;
9531 CXXCastPath BasePath;
9532 ExprResult DeclareReductionRef = buildDeclareReductionRef(
9533 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
9534 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
9535 if (CurContext->isDependentContext() &&
9536 (DeclareReductionRef.isUnset() ||
9537 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
9538 ReductionOps.push_back(DeclareReductionRef.get());
9539 else
9540 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00009541 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00009542 ValueDecl *D = Res.first;
9543 if (!D)
9544 continue;
9545
Alexey Bataeva1764212015-09-30 09:22:36 +00009546 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009547 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
9548 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
9549 if (ASE)
Alexey Bataev31300ed2016-02-04 11:27:03 +00009550 Type = ASE->getType().getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009551 else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009552 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
9553 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
9554 Type = ATy->getElementType();
9555 else
9556 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +00009557 Type = Type.getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009558 } else
9559 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
9560 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +00009561
Alexey Bataevc5e02582014-06-16 07:08:35 +00009562 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9563 // A variable that appears in a private clause must not have an incomplete
9564 // type or a reference type.
9565 if (RequireCompleteType(ELoc, Type,
9566 diag::err_omp_reduction_incomplete_type))
9567 continue;
9568 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00009569 // A list item that appears in a reduction clause must not be
9570 // const-qualified.
9571 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009572 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00009573 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009574 if (!ASE && !OASE) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009575 bool IsDecl = !VD ||
9576 VD->isThisDeclarationADefinition(Context) ==
9577 VarDecl::DeclarationOnly;
9578 Diag(D->getLocation(),
Alexey Bataeva1764212015-09-30 09:22:36 +00009579 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +00009580 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +00009581 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00009582 continue;
9583 }
9584 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
9585 // If a list-item is a reference type then it must bind to the same object
9586 // for all threads of the team.
Alexey Bataev60da77e2016-02-29 05:54:20 +00009587 if (!ASE && !OASE && VD) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009588 VarDecl *VDDef = VD->getDefinition();
Alexey Bataev31300ed2016-02-04 11:27:03 +00009589 if (VD->getType()->isReferenceType() && VDDef) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009590 DSARefChecker Check(DSAStack);
9591 if (Check.Visit(VDDef->getInit())) {
9592 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
9593 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
9594 continue;
9595 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00009596 }
9597 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009598
Alexey Bataevc5e02582014-06-16 07:08:35 +00009599 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
9600 // in a Construct]
9601 // Variables with the predetermined data-sharing attributes may not be
9602 // listed in data-sharing attributes clauses, except for the cases
9603 // listed below. For these exceptions only, listing a predetermined
9604 // variable in a data-sharing attribute clause is allowed and overrides
9605 // the variable's predetermined data-sharing attributes.
9606 // OpenMP [2.14.3.6, Restrictions, p.3]
9607 // Any number of reduction clauses can be specified on the directive,
9608 // but a list item can appear only once in the reduction clauses for that
9609 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00009610 DSAStackTy::DSAVarData DVar;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009611 DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009612 if (DVar.CKind == OMPC_reduction) {
9613 Diag(ELoc, diag::err_omp_once_referenced)
9614 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009615 if (DVar.RefExpr)
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009616 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009617 } else if (DVar.CKind != OMPC_unknown) {
9618 Diag(ELoc, diag::err_omp_wrong_dsa)
9619 << getOpenMPClauseName(DVar.CKind)
9620 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009621 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009622 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009623 }
9624
9625 // OpenMP [2.14.3.6, Restrictions, p.1]
9626 // A list item that appears in a reduction clause of a worksharing
9627 // construct must be shared in the parallel regions to which any of the
9628 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009629 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
9630 if (isOpenMPWorksharingDirective(CurrDir) &&
9631 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009632 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009633 if (DVar.CKind != OMPC_shared) {
9634 Diag(ELoc, diag::err_omp_required_access)
9635 << getOpenMPClauseName(OMPC_reduction)
9636 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009637 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009638 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00009639 }
9640 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009641
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009642 // Try to find 'declare reduction' corresponding construct before using
9643 // builtin/overloaded operators.
9644 CXXCastPath BasePath;
9645 ExprResult DeclareReductionRef = buildDeclareReductionRef(
9646 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
9647 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
9648 if (DeclareReductionRef.isInvalid())
9649 continue;
9650 if (CurContext->isDependentContext() &&
9651 (DeclareReductionRef.isUnset() ||
9652 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
9653 Vars.push_back(RefExpr);
9654 Privates.push_back(nullptr);
9655 LHSs.push_back(nullptr);
9656 RHSs.push_back(nullptr);
9657 ReductionOps.push_back(DeclareReductionRef.get());
9658 continue;
9659 }
9660 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
9661 // Not allowed reduction identifier is found.
9662 Diag(ReductionId.getLocStart(),
9663 diag::err_omp_unknown_reduction_identifier)
9664 << Type << ReductionIdRange;
9665 continue;
9666 }
9667
9668 // OpenMP [2.14.3.6, reduction clause, Restrictions]
9669 // The type of a list item that appears in a reduction clause must be valid
9670 // for the reduction-identifier. For a max or min reduction in C, the type
9671 // of the list item must be an allowed arithmetic data type: char, int,
9672 // float, double, or _Bool, possibly modified with long, short, signed, or
9673 // unsigned. For a max or min reduction in C++, the type of the list item
9674 // must be an allowed arithmetic data type: char, wchar_t, int, float,
9675 // double, or bool, possibly modified with long, short, signed, or unsigned.
9676 if (DeclareReductionRef.isUnset()) {
9677 if ((BOK == BO_GT || BOK == BO_LT) &&
9678 !(Type->isScalarType() ||
9679 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
9680 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
9681 << getLangOpts().CPlusPlus;
9682 if (!ASE && !OASE) {
9683 bool IsDecl = !VD ||
9684 VD->isThisDeclarationADefinition(Context) ==
9685 VarDecl::DeclarationOnly;
9686 Diag(D->getLocation(),
9687 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9688 << D;
9689 }
9690 continue;
9691 }
9692 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
9693 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
9694 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
9695 if (!ASE && !OASE) {
9696 bool IsDecl = !VD ||
9697 VD->isThisDeclarationADefinition(Context) ==
9698 VarDecl::DeclarationOnly;
9699 Diag(D->getLocation(),
9700 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9701 << D;
9702 }
9703 continue;
9704 }
9705 }
9706
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009707 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009708 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
Alexey Bataev60da77e2016-02-29 05:54:20 +00009709 D->hasAttrs() ? &D->getAttrs() : nullptr);
9710 auto *RHSVD = buildVarDecl(*this, ELoc, Type, D->getName(),
9711 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009712 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00009713 if (OASE ||
Alexey Bataev60da77e2016-02-29 05:54:20 +00009714 (!ASE &&
9715 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
David Majnemer9d168222016-08-05 17:44:54 +00009716 // For arrays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009717 // Create pseudo array type for private copy. The size for this array will
9718 // be generated during codegen.
9719 // For array subscripts or single variables Private Ty is the same as Type
9720 // (type of the variable or single array element).
9721 PrivateTy = Context.getVariableArrayType(
9722 Type, new (Context) OpaqueValueExpr(SourceLocation(),
9723 Context.getSizeType(), VK_RValue),
9724 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +00009725 } else if (!ASE && !OASE &&
9726 Context.getAsArrayType(D->getType().getNonReferenceType()))
9727 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009728 // Private copy.
Alexey Bataev60da77e2016-02-29 05:54:20 +00009729 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, D->getName(),
9730 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009731 // Add initializer for private variable.
9732 Expr *Init = nullptr;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009733 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
9734 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
9735 if (DeclareReductionRef.isUsable()) {
9736 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
9737 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
9738 if (DRD->getInitializer()) {
9739 Init = DRDRef;
9740 RHSVD->setInit(DRDRef);
9741 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009742 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009743 } else {
9744 switch (BOK) {
9745 case BO_Add:
9746 case BO_Xor:
9747 case BO_Or:
9748 case BO_LOr:
9749 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
9750 if (Type->isScalarType() || Type->isAnyComplexType())
9751 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
9752 break;
9753 case BO_Mul:
9754 case BO_LAnd:
9755 if (Type->isScalarType() || Type->isAnyComplexType()) {
9756 // '*' and '&&' reduction ops - initializer is '1'.
9757 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00009758 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009759 break;
9760 case BO_And: {
9761 // '&' reduction op - initializer is '~0'.
9762 QualType OrigType = Type;
9763 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
9764 Type = ComplexTy->getElementType();
9765 if (Type->isRealFloatingType()) {
9766 llvm::APFloat InitValue =
9767 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
9768 /*isIEEE=*/true);
9769 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
9770 Type, ELoc);
9771 } else if (Type->isScalarType()) {
9772 auto Size = Context.getTypeSize(Type);
9773 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
9774 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
9775 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
9776 }
9777 if (Init && OrigType->isAnyComplexType()) {
9778 // Init = 0xFFFF + 0xFFFFi;
9779 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
9780 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
9781 }
9782 Type = OrigType;
9783 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009784 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009785 case BO_LT:
9786 case BO_GT: {
9787 // 'min' reduction op - initializer is 'Largest representable number in
9788 // the reduction list item type'.
9789 // 'max' reduction op - initializer is 'Least representable number in
9790 // the reduction list item type'.
9791 if (Type->isIntegerType() || Type->isPointerType()) {
9792 bool IsSigned = Type->hasSignedIntegerRepresentation();
9793 auto Size = Context.getTypeSize(Type);
9794 QualType IntTy =
9795 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
9796 llvm::APInt InitValue =
9797 (BOK != BO_LT)
9798 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
9799 : llvm::APInt::getMinValue(Size)
9800 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
9801 : llvm::APInt::getMaxValue(Size);
9802 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
9803 if (Type->isPointerType()) {
9804 // Cast to pointer type.
9805 auto CastExpr = BuildCStyleCastExpr(
9806 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
9807 SourceLocation(), Init);
9808 if (CastExpr.isInvalid())
9809 continue;
9810 Init = CastExpr.get();
9811 }
9812 } else if (Type->isRealFloatingType()) {
9813 llvm::APFloat InitValue = llvm::APFloat::getLargest(
9814 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
9815 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
9816 Type, ELoc);
9817 }
9818 break;
9819 }
9820 case BO_PtrMemD:
9821 case BO_PtrMemI:
9822 case BO_MulAssign:
9823 case BO_Div:
9824 case BO_Rem:
9825 case BO_Sub:
9826 case BO_Shl:
9827 case BO_Shr:
9828 case BO_LE:
9829 case BO_GE:
9830 case BO_EQ:
9831 case BO_NE:
9832 case BO_AndAssign:
9833 case BO_XorAssign:
9834 case BO_OrAssign:
9835 case BO_Assign:
9836 case BO_AddAssign:
9837 case BO_SubAssign:
9838 case BO_DivAssign:
9839 case BO_RemAssign:
9840 case BO_ShlAssign:
9841 case BO_ShrAssign:
9842 case BO_Comma:
9843 llvm_unreachable("Unexpected reduction operation");
9844 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009845 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009846 if (Init && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009847 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
9848 /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009849 } else if (!Init)
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009850 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009851 if (RHSVD->isInvalidDecl())
9852 continue;
9853 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009854 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
9855 << ReductionIdRange;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009856 bool IsDecl =
9857 !VD ||
9858 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9859 Diag(D->getLocation(),
9860 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9861 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009862 continue;
9863 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009864 // Store initializer for single element in private copy. Will be used during
9865 // codegen.
9866 PrivateVD->setInit(RHSVD->getInit());
9867 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009868 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009869 ExprResult ReductionOp;
9870 if (DeclareReductionRef.isUsable()) {
9871 QualType RedTy = DeclareReductionRef.get()->getType();
9872 QualType PtrRedTy = Context.getPointerType(RedTy);
9873 ExprResult LHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
9874 ExprResult RHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
9875 if (!BasePath.empty()) {
9876 LHS = DefaultLvalueConversion(LHS.get());
9877 RHS = DefaultLvalueConversion(RHS.get());
9878 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
9879 CK_UncheckedDerivedToBase, LHS.get(),
9880 &BasePath, LHS.get()->getValueKind());
9881 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
9882 CK_UncheckedDerivedToBase, RHS.get(),
9883 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009884 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009885 FunctionProtoType::ExtProtoInfo EPI;
9886 QualType Params[] = {PtrRedTy, PtrRedTy};
9887 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
9888 auto *OVE = new (Context) OpaqueValueExpr(
9889 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
9890 DefaultLvalueConversion(DeclareReductionRef.get()).get());
9891 Expr *Args[] = {LHS.get(), RHS.get()};
9892 ReductionOp = new (Context)
9893 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
9894 } else {
9895 ReductionOp = BuildBinOp(DSAStack->getCurScope(),
9896 ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE);
9897 if (ReductionOp.isUsable()) {
9898 if (BOK != BO_LT && BOK != BO_GT) {
9899 ReductionOp =
9900 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
9901 BO_Assign, LHSDRE, ReductionOp.get());
9902 } else {
9903 auto *ConditionalOp = new (Context) ConditionalOperator(
9904 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
9905 RHSDRE, Type, VK_LValue, OK_Ordinary);
9906 ReductionOp =
9907 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
9908 BO_Assign, LHSDRE, ConditionalOp);
9909 }
9910 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
9911 }
9912 if (ReductionOp.isInvalid())
9913 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009914 }
9915
Alexey Bataev60da77e2016-02-29 05:54:20 +00009916 DeclRefExpr *Ref = nullptr;
9917 Expr *VarsExpr = RefExpr->IgnoreParens();
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009918 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009919 if (ASE || OASE) {
9920 TransformExprToCaptures RebuildToCapture(*this, D);
9921 VarsExpr =
9922 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
9923 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +00009924 } else {
9925 VarsExpr = Ref =
9926 buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +00009927 }
9928 if (!IsOpenMPCapturedDecl(D)) {
9929 ExprCaptures.push_back(Ref->getDecl());
9930 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
9931 ExprResult RefRes = DefaultLvalueConversion(Ref);
9932 if (!RefRes.isUsable())
9933 continue;
9934 ExprResult PostUpdateRes =
9935 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
9936 SimpleRefExpr, RefRes.get());
9937 if (!PostUpdateRes.isUsable())
9938 continue;
9939 ExprPostUpdates.push_back(
9940 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +00009941 }
9942 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00009943 }
9944 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
9945 Vars.push_back(VarsExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009946 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009947 LHSs.push_back(LHSDRE);
9948 RHSs.push_back(RHSDRE);
9949 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009950 }
9951
9952 if (Vars.empty())
9953 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +00009954
Alexey Bataevc5e02582014-06-16 07:08:35 +00009955 return OMPReductionClause::Create(
9956 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009957 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009958 LHSs, RHSs, ReductionOps, buildPreInits(Context, ExprCaptures),
9959 buildPostUpdate(*this, ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +00009960}
9961
Alexey Bataevecba70f2016-04-12 11:02:11 +00009962bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
9963 SourceLocation LinLoc) {
9964 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
9965 LinKind == OMPC_LINEAR_unknown) {
9966 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
9967 return true;
9968 }
9969 return false;
9970}
9971
9972bool Sema::CheckOpenMPLinearDecl(ValueDecl *D, SourceLocation ELoc,
9973 OpenMPLinearClauseKind LinKind,
9974 QualType Type) {
9975 auto *VD = dyn_cast_or_null<VarDecl>(D);
9976 // A variable must not have an incomplete type or a reference type.
9977 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
9978 return true;
9979 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
9980 !Type->isReferenceType()) {
9981 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
9982 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
9983 return true;
9984 }
9985 Type = Type.getNonReferenceType();
9986
9987 // A list item must not be const-qualified.
9988 if (Type.isConstant(Context)) {
9989 Diag(ELoc, diag::err_omp_const_variable)
9990 << getOpenMPClauseName(OMPC_linear);
9991 if (D) {
9992 bool IsDecl =
9993 !VD ||
9994 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9995 Diag(D->getLocation(),
9996 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9997 << D;
9998 }
9999 return true;
10000 }
10001
10002 // A list item must be of integral or pointer type.
10003 Type = Type.getUnqualifiedType().getCanonicalType();
10004 const auto *Ty = Type.getTypePtrOrNull();
10005 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
10006 !Ty->isPointerType())) {
10007 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
10008 if (D) {
10009 bool IsDecl =
10010 !VD ||
10011 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
10012 Diag(D->getLocation(),
10013 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
10014 << D;
10015 }
10016 return true;
10017 }
10018 return false;
10019}
10020
Alexey Bataev182227b2015-08-20 10:54:39 +000010021OMPClause *Sema::ActOnOpenMPLinearClause(
10022 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
10023 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
10024 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +000010025 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010026 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +000010027 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +000010028 SmallVector<Decl *, 4> ExprCaptures;
10029 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +000010030 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +000010031 LinKind = OMPC_LINEAR_val;
Alexey Bataeved09d242014-05-28 05:53:51 +000010032 for (auto &RefExpr : VarList) {
10033 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010034 SourceLocation ELoc;
10035 SourceRange ERange;
10036 Expr *SimpleRefExpr = RefExpr;
10037 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
10038 /*AllowArraySection=*/false);
10039 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +000010040 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000010041 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010042 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +000010043 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +000010044 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010045 ValueDecl *D = Res.first;
10046 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +000010047 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +000010048
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010049 QualType Type = D->getType();
10050 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +000010051
10052 // OpenMP [2.14.3.7, linear clause]
10053 // A list-item cannot appear in more than one linear clause.
10054 // A list-item that appears in a linear clause cannot appear in any
10055 // other data-sharing attribute clause.
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010056 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman8dba6642014-04-22 13:09:42 +000010057 if (DVar.RefExpr) {
10058 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
10059 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010060 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +000010061 continue;
10062 }
10063
Alexey Bataevecba70f2016-04-12 11:02:11 +000010064 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +000010065 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +000010066 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +000010067
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010068 // Build private copy of original var.
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010069 auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(),
10070 D->hasAttrs() ? &D->getAttrs() : nullptr);
10071 auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +000010072 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010073 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000010074 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010075 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010076 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev78849fb2016-03-09 09:49:00 +000010077 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
10078 if (!IsOpenMPCapturedDecl(D)) {
10079 ExprCaptures.push_back(Ref->getDecl());
10080 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
10081 ExprResult RefRes = DefaultLvalueConversion(Ref);
10082 if (!RefRes.isUsable())
10083 continue;
10084 ExprResult PostUpdateRes =
10085 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
10086 SimpleRefExpr, RefRes.get());
10087 if (!PostUpdateRes.isUsable())
10088 continue;
10089 ExprPostUpdates.push_back(
10090 IgnoredValueConversions(PostUpdateRes.get()).get());
10091 }
10092 }
10093 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000010094 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010095 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000010096 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010097 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000010098 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010099 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
10100 auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
10101
10102 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010103 Vars.push_back((VD || CurContext->isDependentContext())
10104 ? RefExpr->IgnoreParens()
10105 : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010106 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +000010107 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +000010108 }
10109
10110 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000010111 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000010112
10113 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +000010114 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000010115 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
10116 !Step->isInstantiationDependent() &&
10117 !Step->containsUnexpandedParameterPack()) {
10118 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +000010119 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +000010120 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000010121 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +000010122 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +000010123
Alexander Musman3276a272015-03-21 10:12:56 +000010124 // Build var to save the step value.
10125 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +000010126 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +000010127 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +000010128 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +000010129 ExprResult CalcStep =
10130 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +000010131 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +000010132
Alexander Musman8dba6642014-04-22 13:09:42 +000010133 // Warn about zero linear step (it would be probably better specified as
10134 // making corresponding variables 'const').
10135 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +000010136 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
10137 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +000010138 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
10139 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +000010140 if (!IsConstant && CalcStep.isUsable()) {
10141 // Calculate the step beforehand instead of doing this on each iteration.
10142 // (This is not used if the number of iterations may be kfold-ed).
10143 CalcStepExpr = CalcStep.get();
10144 }
Alexander Musman8dba6642014-04-22 13:09:42 +000010145 }
10146
Alexey Bataev182227b2015-08-20 10:54:39 +000010147 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
10148 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +000010149 StepExpr, CalcStepExpr,
10150 buildPreInits(Context, ExprCaptures),
10151 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +000010152}
10153
Alexey Bataev5dff95c2016-04-22 03:56:56 +000010154static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
10155 Expr *NumIterations, Sema &SemaRef,
10156 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +000010157 // Walk the vars and build update/final expressions for the CodeGen.
10158 SmallVector<Expr *, 8> Updates;
10159 SmallVector<Expr *, 8> Finals;
10160 Expr *Step = Clause.getStep();
10161 Expr *CalcStep = Clause.getCalcStep();
10162 // OpenMP [2.14.3.7, linear clause]
10163 // If linear-step is not specified it is assumed to be 1.
10164 if (Step == nullptr)
10165 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +000010166 else if (CalcStep) {
Alexander Musman3276a272015-03-21 10:12:56 +000010167 Step = cast<BinaryOperator>(CalcStep)->getLHS();
Alexey Bataev5a3af132016-03-29 08:58:54 +000010168 }
Alexander Musman3276a272015-03-21 10:12:56 +000010169 bool HasErrors = false;
10170 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010171 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000010172 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +000010173 for (auto &RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +000010174 SourceLocation ELoc;
10175 SourceRange ERange;
10176 Expr *SimpleRefExpr = RefExpr;
10177 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange,
10178 /*AllowArraySection=*/false);
10179 ValueDecl *D = Res.first;
10180 if (Res.second || !D) {
10181 Updates.push_back(nullptr);
10182 Finals.push_back(nullptr);
10183 HasErrors = true;
10184 continue;
10185 }
10186 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) {
10187 D = cast<MemberExpr>(CED->getInit()->IgnoreParenImpCasts())
10188 ->getMemberDecl();
10189 }
10190 auto &&Info = Stack->isLoopControlVariable(D);
Alexander Musman3276a272015-03-21 10:12:56 +000010191 Expr *InitExpr = *CurInit;
10192
10193 // Build privatized reference to the current linear var.
David Majnemer9d168222016-08-05 17:44:54 +000010194 auto *DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000010195 Expr *CapturedRef;
10196 if (LinKind == OMPC_LINEAR_uval)
10197 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
10198 else
10199 CapturedRef =
10200 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
10201 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
10202 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +000010203
10204 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000010205 ExprResult Update;
10206 if (!Info.first) {
10207 Update =
10208 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
10209 InitExpr, IV, Step, /* Subtract */ false);
10210 } else
10211 Update = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +000010212 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
10213 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +000010214
10215 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000010216 ExprResult Final;
10217 if (!Info.first) {
10218 Final = BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
10219 InitExpr, NumIterations, Step,
10220 /* Subtract */ false);
10221 } else
10222 Final = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +000010223 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
10224 /*DiscardedValue=*/true);
Alexey Bataev5dff95c2016-04-22 03:56:56 +000010225
Alexander Musman3276a272015-03-21 10:12:56 +000010226 if (!Update.isUsable() || !Final.isUsable()) {
10227 Updates.push_back(nullptr);
10228 Finals.push_back(nullptr);
10229 HasErrors = true;
10230 } else {
10231 Updates.push_back(Update.get());
10232 Finals.push_back(Final.get());
10233 }
Richard Trieucc3949d2016-02-18 22:34:54 +000010234 ++CurInit;
10235 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +000010236 }
10237 Clause.setUpdates(Updates);
10238 Clause.setFinals(Finals);
10239 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +000010240}
10241
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010242OMPClause *Sema::ActOnOpenMPAlignedClause(
10243 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
10244 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
10245
10246 SmallVector<Expr *, 8> Vars;
10247 for (auto &RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +000010248 assert(RefExpr && "NULL expr in OpenMP linear clause.");
10249 SourceLocation ELoc;
10250 SourceRange ERange;
10251 Expr *SimpleRefExpr = RefExpr;
10252 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
10253 /*AllowArraySection=*/false);
10254 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010255 // It will be analyzed later.
10256 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010257 }
Alexey Bataev1efd1662016-03-29 10:59:56 +000010258 ValueDecl *D = Res.first;
10259 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010260 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010261
Alexey Bataev1efd1662016-03-29 10:59:56 +000010262 QualType QType = D->getType();
10263 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010264
10265 // OpenMP [2.8.1, simd construct, Restrictions]
10266 // The type of list items appearing in the aligned clause must be
10267 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010268 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010269 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +000010270 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010271 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +000010272 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010273 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +000010274 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010275 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +000010276 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010277 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +000010278 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010279 continue;
10280 }
10281
10282 // OpenMP [2.8.1, simd construct, Restrictions]
10283 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev1efd1662016-03-29 10:59:56 +000010284 if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +000010285 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010286 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
10287 << getOpenMPClauseName(OMPC_aligned);
10288 continue;
10289 }
10290
Alexey Bataev1efd1662016-03-29 10:59:56 +000010291 DeclRefExpr *Ref = nullptr;
10292 if (!VD && IsOpenMPCapturedDecl(D))
10293 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
10294 Vars.push_back(DefaultFunctionArrayConversion(
10295 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
10296 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010297 }
10298
10299 // OpenMP [2.8.1, simd construct, Description]
10300 // The parameter of the aligned clause, alignment, must be a constant
10301 // positive integer expression.
10302 // If no optional parameter is specified, implementation-defined default
10303 // alignments for SIMD instructions on the target platforms are assumed.
10304 if (Alignment != nullptr) {
10305 ExprResult AlignResult =
10306 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
10307 if (AlignResult.isInvalid())
10308 return nullptr;
10309 Alignment = AlignResult.get();
10310 }
10311 if (Vars.empty())
10312 return nullptr;
10313
10314 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
10315 EndLoc, Vars, Alignment);
10316}
10317
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010318OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
10319 SourceLocation StartLoc,
10320 SourceLocation LParenLoc,
10321 SourceLocation EndLoc) {
10322 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010323 SmallVector<Expr *, 8> SrcExprs;
10324 SmallVector<Expr *, 8> DstExprs;
10325 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +000010326 for (auto &RefExpr : VarList) {
10327 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
10328 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010329 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000010330 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010331 SrcExprs.push_back(nullptr);
10332 DstExprs.push_back(nullptr);
10333 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010334 continue;
10335 }
10336
Alexey Bataeved09d242014-05-28 05:53:51 +000010337 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010338 // OpenMP [2.1, C/C++]
10339 // A list item is a variable name.
10340 // OpenMP [2.14.4.1, Restrictions, p.1]
10341 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +000010342 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010343 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000010344 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
10345 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010346 continue;
10347 }
10348
10349 Decl *D = DE->getDecl();
10350 VarDecl *VD = cast<VarDecl>(D);
10351
10352 QualType Type = VD->getType();
10353 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
10354 // It will be analyzed later.
10355 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010356 SrcExprs.push_back(nullptr);
10357 DstExprs.push_back(nullptr);
10358 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010359 continue;
10360 }
10361
10362 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
10363 // A list item that appears in a copyin clause must be threadprivate.
10364 if (!DSAStack->isThreadPrivate(VD)) {
10365 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +000010366 << getOpenMPClauseName(OMPC_copyin)
10367 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010368 continue;
10369 }
10370
10371 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
10372 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +000010373 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010374 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010375 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000010376 auto *SrcVD =
10377 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
10378 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +000010379 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010380 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
10381 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000010382 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
10383 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010384 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010385 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010386 // For arrays generate assignment operation for single element and replace
10387 // it by the original array element in CodeGen.
10388 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
10389 PseudoDstExpr, PseudoSrcExpr);
10390 if (AssignmentOp.isInvalid())
10391 continue;
10392 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
10393 /*DiscardedValue=*/true);
10394 if (AssignmentOp.isInvalid())
10395 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010396
10397 DSAStack->addDSA(VD, DE, OMPC_copyin);
10398 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010399 SrcExprs.push_back(PseudoSrcExpr);
10400 DstExprs.push_back(PseudoDstExpr);
10401 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010402 }
10403
Alexey Bataeved09d242014-05-28 05:53:51 +000010404 if (Vars.empty())
10405 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010406
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010407 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
10408 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010409}
10410
Alexey Bataevbae9a792014-06-27 10:37:06 +000010411OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
10412 SourceLocation StartLoc,
10413 SourceLocation LParenLoc,
10414 SourceLocation EndLoc) {
10415 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +000010416 SmallVector<Expr *, 8> SrcExprs;
10417 SmallVector<Expr *, 8> DstExprs;
10418 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +000010419 for (auto &RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +000010420 assert(RefExpr && "NULL expr in OpenMP linear clause.");
10421 SourceLocation ELoc;
10422 SourceRange ERange;
10423 Expr *SimpleRefExpr = RefExpr;
10424 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
10425 /*AllowArraySection=*/false);
10426 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000010427 // It will be analyzed later.
10428 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000010429 SrcExprs.push_back(nullptr);
10430 DstExprs.push_back(nullptr);
10431 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010432 }
Alexey Bataeve122da12016-03-17 10:50:17 +000010433 ValueDecl *D = Res.first;
10434 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +000010435 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000010436
Alexey Bataeve122da12016-03-17 10:50:17 +000010437 QualType Type = D->getType();
10438 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010439
10440 // OpenMP [2.14.4.2, Restrictions, p.2]
10441 // A list item that appears in a copyprivate clause may not appear in a
10442 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +000010443 if (!VD || !DSAStack->isThreadPrivate(VD)) {
10444 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +000010445 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
10446 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000010447 Diag(ELoc, diag::err_omp_wrong_dsa)
10448 << getOpenMPClauseName(DVar.CKind)
10449 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve122da12016-03-17 10:50:17 +000010450 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010451 continue;
10452 }
10453
10454 // OpenMP [2.11.4.2, Restrictions, p.1]
10455 // All list items that appear in a copyprivate clause must be either
10456 // threadprivate or private in the enclosing context.
10457 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +000010458 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010459 if (DVar.CKind == OMPC_shared) {
10460 Diag(ELoc, diag::err_omp_required_access)
10461 << getOpenMPClauseName(OMPC_copyprivate)
10462 << "threadprivate or private in the enclosing context";
Alexey Bataeve122da12016-03-17 10:50:17 +000010463 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010464 continue;
10465 }
10466 }
10467 }
10468
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010469 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000010470 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010471 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010472 << getOpenMPClauseName(OMPC_copyprivate) << Type
10473 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010474 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +000010475 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010476 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +000010477 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010478 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +000010479 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010480 continue;
10481 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010482
Alexey Bataevbae9a792014-06-27 10:37:06 +000010483 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
10484 // A variable of class type (or array thereof) that appears in a
10485 // copyin clause requires an accessible, unambiguous copy assignment
10486 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010487 Type = Context.getBaseElementType(Type.getNonReferenceType())
10488 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +000010489 auto *SrcVD =
Alexey Bataeve122da12016-03-17 10:50:17 +000010490 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src",
10491 D->hasAttrs() ? &D->getAttrs() : nullptr);
10492 auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
Alexey Bataev420d45b2015-04-14 05:11:24 +000010493 auto *DstVD =
Alexey Bataeve122da12016-03-17 10:50:17 +000010494 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst",
10495 D->hasAttrs() ? &D->getAttrs() : nullptr);
David Majnemer9d168222016-08-05 17:44:54 +000010496 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataeve122da12016-03-17 10:50:17 +000010497 auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
Alexey Bataeva63048e2015-03-23 06:18:07 +000010498 PseudoDstExpr, PseudoSrcExpr);
10499 if (AssignmentOp.isInvalid())
10500 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +000010501 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +000010502 /*DiscardedValue=*/true);
10503 if (AssignmentOp.isInvalid())
10504 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000010505
10506 // No need to mark vars as copyprivate, they are already threadprivate or
10507 // implicitly private.
Alexey Bataeve122da12016-03-17 10:50:17 +000010508 assert(VD || IsOpenMPCapturedDecl(D));
10509 Vars.push_back(
10510 VD ? RefExpr->IgnoreParens()
10511 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +000010512 SrcExprs.push_back(PseudoSrcExpr);
10513 DstExprs.push_back(PseudoDstExpr);
10514 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +000010515 }
10516
10517 if (Vars.empty())
10518 return nullptr;
10519
Alexey Bataeva63048e2015-03-23 06:18:07 +000010520 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10521 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010522}
10523
Alexey Bataev6125da92014-07-21 11:26:11 +000010524OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
10525 SourceLocation StartLoc,
10526 SourceLocation LParenLoc,
10527 SourceLocation EndLoc) {
10528 if (VarList.empty())
10529 return nullptr;
10530
10531 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
10532}
Alexey Bataevdea47612014-07-23 07:46:59 +000010533
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010534OMPClause *
10535Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
10536 SourceLocation DepLoc, SourceLocation ColonLoc,
10537 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
10538 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +000010539 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010540 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +000010541 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000010542 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +000010543 return nullptr;
10544 }
10545 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010546 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
10547 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +000010548 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010549 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000010550 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
10551 /*Last=*/OMPC_DEPEND_unknown, Except)
10552 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010553 return nullptr;
10554 }
10555 SmallVector<Expr *, 8> Vars;
Alexey Bataev8b427062016-05-25 12:36:08 +000010556 DSAStackTy::OperatorOffsetTy OpsOffs;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010557 llvm::APSInt DepCounter(/*BitWidth=*/32);
10558 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
10559 if (DepKind == OMPC_DEPEND_sink) {
10560 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
10561 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
10562 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010563 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010564 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010565 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
10566 DSAStack->getParentOrderedRegionParam()) {
10567 for (auto &RefExpr : VarList) {
10568 assert(RefExpr && "NULL expr in OpenMP shared clause.");
Alexey Bataev8b427062016-05-25 12:36:08 +000010569 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010570 // It will be analyzed later.
10571 Vars.push_back(RefExpr);
10572 continue;
10573 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010574
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010575 SourceLocation ELoc = RefExpr->getExprLoc();
10576 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
10577 if (DepKind == OMPC_DEPEND_sink) {
10578 if (DepCounter >= TotalDepCount) {
10579 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
10580 continue;
10581 }
10582 ++DepCounter;
10583 // OpenMP [2.13.9, Summary]
10584 // depend(dependence-type : vec), where dependence-type is:
10585 // 'sink' and where vec is the iteration vector, which has the form:
10586 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
10587 // where n is the value specified by the ordered clause in the loop
10588 // directive, xi denotes the loop iteration variable of the i-th nested
10589 // loop associated with the loop directive, and di is a constant
10590 // non-negative integer.
Alexey Bataev8b427062016-05-25 12:36:08 +000010591 if (CurContext->isDependentContext()) {
10592 // It will be analyzed later.
10593 Vars.push_back(RefExpr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010594 continue;
10595 }
Alexey Bataev8b427062016-05-25 12:36:08 +000010596 SimpleExpr = SimpleExpr->IgnoreImplicit();
10597 OverloadedOperatorKind OOK = OO_None;
10598 SourceLocation OOLoc;
10599 Expr *LHS = SimpleExpr;
10600 Expr *RHS = nullptr;
10601 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
10602 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
10603 OOLoc = BO->getOperatorLoc();
10604 LHS = BO->getLHS()->IgnoreParenImpCasts();
10605 RHS = BO->getRHS()->IgnoreParenImpCasts();
10606 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
10607 OOK = OCE->getOperator();
10608 OOLoc = OCE->getOperatorLoc();
10609 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
10610 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
10611 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
10612 OOK = MCE->getMethodDecl()
10613 ->getNameInfo()
10614 .getName()
10615 .getCXXOverloadedOperator();
10616 OOLoc = MCE->getCallee()->getExprLoc();
10617 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
10618 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
10619 }
10620 SourceLocation ELoc;
10621 SourceRange ERange;
10622 auto Res = getPrivateItem(*this, LHS, ELoc, ERange,
10623 /*AllowArraySection=*/false);
10624 if (Res.second) {
10625 // It will be analyzed later.
10626 Vars.push_back(RefExpr);
10627 }
10628 ValueDecl *D = Res.first;
10629 if (!D)
10630 continue;
10631
10632 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
10633 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
10634 continue;
10635 }
10636 if (RHS) {
10637 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
10638 RHS, OMPC_depend, /*StrictlyPositive=*/false);
10639 if (RHSRes.isInvalid())
10640 continue;
10641 }
10642 if (!CurContext->isDependentContext() &&
10643 DSAStack->getParentOrderedRegionParam() &&
10644 DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
10645 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
10646 << DSAStack->getParentLoopControlVariable(
10647 DepCounter.getZExtValue());
10648 continue;
10649 }
10650 OpsOffs.push_back({RHS, OOK});
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010651 } else {
10652 // OpenMP [2.11.1.1, Restrictions, p.3]
10653 // A variable that is part of another variable (such as a field of a
10654 // structure) but is not an array element or an array section cannot
10655 // appear in a depend clause.
10656 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
10657 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
10658 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
10659 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
10660 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev31300ed2016-02-04 11:27:03 +000010661 (ASE &&
10662 !ASE->getBase()
10663 ->getType()
10664 .getNonReferenceType()
10665 ->isPointerType() &&
10666 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000010667 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
10668 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010669 continue;
10670 }
10671 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010672 Vars.push_back(RefExpr->IgnoreParenImpCasts());
10673 }
10674
10675 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
10676 TotalDepCount > VarList.size() &&
10677 DSAStack->getParentOrderedRegionParam()) {
10678 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
10679 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
10680 }
10681 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
10682 Vars.empty())
10683 return nullptr;
10684 }
Alexey Bataev8b427062016-05-25 12:36:08 +000010685 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10686 DepKind, DepLoc, ColonLoc, Vars);
10687 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source)
10688 DSAStack->addDoacrossDependClause(C, OpsOffs);
10689 return C;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010690}
Michael Wonge710d542015-08-07 16:16:36 +000010691
10692OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
10693 SourceLocation LParenLoc,
10694 SourceLocation EndLoc) {
10695 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +000010696
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010697 // OpenMP [2.9.1, Restrictions]
10698 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010699 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
10700 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010701 return nullptr;
10702
Michael Wonge710d542015-08-07 16:16:36 +000010703 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10704}
Kelvin Li0bff7af2015-11-23 05:32:03 +000010705
10706static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
10707 DSAStackTy *Stack, CXXRecordDecl *RD) {
10708 if (!RD || RD->isInvalidDecl())
10709 return true;
10710
Alexey Bataevc9bd03d2015-12-17 06:55:08 +000010711 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
10712 if (auto *CTD = CTSD->getSpecializedTemplate())
10713 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010714 auto QTy = SemaRef.Context.getRecordType(RD);
10715 if (RD->isDynamicClass()) {
10716 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10717 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
10718 return false;
10719 }
10720 auto *DC = RD;
10721 bool IsCorrect = true;
10722 for (auto *I : DC->decls()) {
10723 if (I) {
10724 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
10725 if (MD->isStatic()) {
10726 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10727 SemaRef.Diag(MD->getLocation(),
10728 diag::note_omp_static_member_in_target);
10729 IsCorrect = false;
10730 }
10731 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
10732 if (VD->isStaticDataMember()) {
10733 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10734 SemaRef.Diag(VD->getLocation(),
10735 diag::note_omp_static_member_in_target);
10736 IsCorrect = false;
10737 }
10738 }
10739 }
10740 }
10741
10742 for (auto &I : RD->bases()) {
10743 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
10744 I.getType()->getAsCXXRecordDecl()))
10745 IsCorrect = false;
10746 }
10747 return IsCorrect;
10748}
10749
10750static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
10751 DSAStackTy *Stack, QualType QTy) {
10752 NamedDecl *ND;
10753 if (QTy->isIncompleteType(&ND)) {
10754 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
10755 return false;
10756 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
David Majnemer9d168222016-08-05 17:44:54 +000010757 if (!RD->isInvalidDecl() && !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
Kelvin Li0bff7af2015-11-23 05:32:03 +000010758 return false;
10759 }
10760 return true;
10761}
10762
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010763/// \brief Return true if it can be proven that the provided array expression
10764/// (array section or array subscript) does NOT specify the whole size of the
10765/// array whose base type is \a BaseQTy.
10766static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
10767 const Expr *E,
10768 QualType BaseQTy) {
10769 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
10770
10771 // If this is an array subscript, it refers to the whole size if the size of
10772 // the dimension is constant and equals 1. Also, an array section assumes the
10773 // format of an array subscript if no colon is used.
10774 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
10775 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
10776 return ATy->getSize().getSExtValue() != 1;
10777 // Size can't be evaluated statically.
10778 return false;
10779 }
10780
10781 assert(OASE && "Expecting array section if not an array subscript.");
10782 auto *LowerBound = OASE->getLowerBound();
10783 auto *Length = OASE->getLength();
10784
10785 // If there is a lower bound that does not evaluates to zero, we are not
David Majnemer9d168222016-08-05 17:44:54 +000010786 // covering the whole dimension.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010787 if (LowerBound) {
10788 llvm::APSInt ConstLowerBound;
10789 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
10790 return false; // Can't get the integer value as a constant.
10791 if (ConstLowerBound.getSExtValue())
10792 return true;
10793 }
10794
10795 // If we don't have a length we covering the whole dimension.
10796 if (!Length)
10797 return false;
10798
10799 // If the base is a pointer, we don't have a way to get the size of the
10800 // pointee.
10801 if (BaseQTy->isPointerType())
10802 return false;
10803
10804 // We can only check if the length is the same as the size of the dimension
10805 // if we have a constant array.
10806 auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
10807 if (!CATy)
10808 return false;
10809
10810 llvm::APSInt ConstLength;
10811 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
10812 return false; // Can't get the integer value as a constant.
10813
10814 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
10815}
10816
10817// Return true if it can be proven that the provided array expression (array
10818// section or array subscript) does NOT specify a single element of the array
10819// whose base type is \a BaseQTy.
10820static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
David Majnemer9d168222016-08-05 17:44:54 +000010821 const Expr *E,
10822 QualType BaseQTy) {
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010823 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
10824
10825 // An array subscript always refer to a single element. Also, an array section
10826 // assumes the format of an array subscript if no colon is used.
10827 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
10828 return false;
10829
10830 assert(OASE && "Expecting array section if not an array subscript.");
10831 auto *Length = OASE->getLength();
10832
10833 // If we don't have a length we have to check if the array has unitary size
10834 // for this dimension. Also, we should always expect a length if the base type
10835 // is pointer.
10836 if (!Length) {
10837 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
10838 return ATy->getSize().getSExtValue() != 1;
10839 // We cannot assume anything.
10840 return false;
10841 }
10842
10843 // Check if the length evaluates to 1.
10844 llvm::APSInt ConstLength;
10845 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
10846 return false; // Can't get the integer value as a constant.
10847
10848 return ConstLength.getSExtValue() != 1;
10849}
10850
Samuel Antao661c0902016-05-26 17:39:58 +000010851// Return the expression of the base of the mappable expression or null if it
10852// cannot be determined and do all the necessary checks to see if the expression
10853// is valid as a standalone mappable expression. In the process, record all the
Samuel Antao90927002016-04-26 14:54:23 +000010854// components of the expression.
10855static Expr *CheckMapClauseExpressionBase(
10856 Sema &SemaRef, Expr *E,
Samuel Antao661c0902016-05-26 17:39:58 +000010857 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
10858 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010859 SourceLocation ELoc = E->getExprLoc();
10860 SourceRange ERange = E->getSourceRange();
10861
10862 // The base of elements of list in a map clause have to be either:
10863 // - a reference to variable or field.
10864 // - a member expression.
10865 // - an array expression.
10866 //
10867 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
10868 // reference to 'r'.
10869 //
10870 // If we have:
10871 //
10872 // struct SS {
10873 // Bla S;
10874 // foo() {
10875 // #pragma omp target map (S.Arr[:12]);
10876 // }
10877 // }
10878 //
10879 // We want to retrieve the member expression 'this->S';
10880
10881 Expr *RelevantExpr = nullptr;
10882
Samuel Antao5de996e2016-01-22 20:21:36 +000010883 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
10884 // If a list item is an array section, it must specify contiguous storage.
10885 //
10886 // For this restriction it is sufficient that we make sure only references
10887 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010888 // exist except in the rightmost expression (unless they cover the whole
10889 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +000010890 //
10891 // r.ArrS[3:5].Arr[6:7]
10892 //
10893 // r.ArrS[3:5].x
10894 //
10895 // but these would be valid:
10896 // r.ArrS[3].Arr[6:7]
10897 //
10898 // r.ArrS[3].x
10899
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010900 bool AllowUnitySizeArraySection = true;
10901 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +000010902
Dmitry Polukhin644a9252016-03-11 07:58:34 +000010903 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010904 E = E->IgnoreParenImpCasts();
10905
10906 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
10907 if (!isa<VarDecl>(CurE->getDecl()))
10908 break;
10909
10910 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010911
10912 // If we got a reference to a declaration, we should not expect any array
10913 // section before that.
10914 AllowUnitySizeArraySection = false;
10915 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010916
10917 // Record the component.
10918 CurComponents.push_back(OMPClauseMappableExprCommon::MappableComponent(
10919 CurE, CurE->getDecl()));
Samuel Antao5de996e2016-01-22 20:21:36 +000010920 continue;
10921 }
10922
10923 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
10924 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
10925
10926 if (isa<CXXThisExpr>(BaseE))
10927 // We found a base expression: this->Val.
10928 RelevantExpr = CurE;
10929 else
10930 E = BaseE;
10931
10932 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
10933 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
10934 << CurE->getSourceRange();
10935 break;
10936 }
10937
10938 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
10939
10940 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
10941 // A bit-field cannot appear in a map clause.
10942 //
10943 if (FD->isBitField()) {
Samuel Antao661c0902016-05-26 17:39:58 +000010944 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
10945 << CurE->getSourceRange() << getOpenMPClauseName(CKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010946 break;
10947 }
10948
10949 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10950 // If the type of a list item is a reference to a type T then the type
10951 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010952 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010953
10954 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
10955 // A list item cannot be a variable that is a member of a structure with
10956 // a union type.
10957 //
10958 if (auto *RT = CurType->getAs<RecordType>())
10959 if (RT->isUnionType()) {
10960 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
10961 << CurE->getSourceRange();
10962 break;
10963 }
10964
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010965 // If we got a member expression, we should not expect any array section
10966 // before that:
10967 //
10968 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
10969 // If a list item is an element of a structure, only the rightmost symbol
10970 // of the variable reference can be an array section.
10971 //
10972 AllowUnitySizeArraySection = false;
10973 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010974
10975 // Record the component.
10976 CurComponents.push_back(
10977 OMPClauseMappableExprCommon::MappableComponent(CurE, FD));
Samuel Antao5de996e2016-01-22 20:21:36 +000010978 continue;
10979 }
10980
10981 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
10982 E = CurE->getBase()->IgnoreParenImpCasts();
10983
10984 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
10985 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
10986 << 0 << CurE->getSourceRange();
10987 break;
10988 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010989
10990 // If we got an array subscript that express the whole dimension we
10991 // can have any array expressions before. If it only expressing part of
10992 // the dimension, we can only have unitary-size array expressions.
10993 if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
10994 E->getType()))
10995 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010996
10997 // Record the component - we don't have any declaration associated.
10998 CurComponents.push_back(
10999 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +000011000 continue;
11001 }
11002
11003 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +000011004 E = CurE->getBase()->IgnoreParenImpCasts();
11005
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011006 auto CurType =
11007 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
11008
Samuel Antao5de996e2016-01-22 20:21:36 +000011009 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
11010 // If the type of a list item is a reference to a type T then the type
11011 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +000011012 if (CurType->isReferenceType())
11013 CurType = CurType->getPointeeType();
11014
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011015 bool IsPointer = CurType->isAnyPointerType();
11016
11017 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000011018 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
11019 << 0 << CurE->getSourceRange();
11020 break;
11021 }
11022
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011023 bool NotWhole =
11024 CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
11025 bool NotUnity =
11026 CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
11027
Samuel Antaodab51bb2016-07-18 23:22:11 +000011028 if (AllowWholeSizeArraySection) {
11029 // Any array section is currently allowed. Allowing a whole size array
11030 // section implies allowing a unity array section as well.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011031 //
11032 // If this array section refers to the whole dimension we can still
11033 // accept other array sections before this one, except if the base is a
11034 // pointer. Otherwise, only unitary sections are accepted.
11035 if (NotWhole || IsPointer)
11036 AllowWholeSizeArraySection = false;
Samuel Antaodab51bb2016-07-18 23:22:11 +000011037 } else if (AllowUnitySizeArraySection && NotUnity) {
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011038 // A unity or whole array section is not allowed and that is not
11039 // compatible with the properties of the current array section.
11040 SemaRef.Diag(
11041 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
11042 << CurE->getSourceRange();
11043 break;
11044 }
Samuel Antao90927002016-04-26 14:54:23 +000011045
11046 // Record the component - we don't have any declaration associated.
11047 CurComponents.push_back(
11048 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +000011049 continue;
11050 }
11051
11052 // If nothing else worked, this is not a valid map clause expression.
11053 SemaRef.Diag(ELoc,
11054 diag::err_omp_expected_named_var_member_or_array_expression)
11055 << ERange;
11056 break;
11057 }
11058
11059 return RelevantExpr;
11060}
11061
11062// Return true if expression E associated with value VD has conflicts with other
11063// map information.
Samuel Antao90927002016-04-26 14:54:23 +000011064static bool CheckMapConflicts(
11065 Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD, Expr *E,
11066 bool CurrentRegionOnly,
Samuel Antao661c0902016-05-26 17:39:58 +000011067 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
11068 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000011069 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +000011070 SourceLocation ELoc = E->getExprLoc();
11071 SourceRange ERange = E->getSourceRange();
11072
11073 // In order to easily check the conflicts we need to match each component of
11074 // the expression under test with the components of the expressions that are
11075 // already in the stack.
11076
Samuel Antao5de996e2016-01-22 20:21:36 +000011077 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000011078 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000011079 "Map clause expression with unexpected base!");
11080
11081 // Variables to help detecting enclosing problems in data environment nests.
11082 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +000011083 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000011084
Samuel Antao90927002016-04-26 14:54:23 +000011085 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
11086 VD, CurrentRegionOnly,
11087 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
Samuel Antao6890b092016-07-28 14:25:09 +000011088 StackComponents,
11089 OpenMPClauseKind) -> bool {
Samuel Antao90927002016-04-26 14:54:23 +000011090
Samuel Antao5de996e2016-01-22 20:21:36 +000011091 assert(!StackComponents.empty() &&
11092 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000011093 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000011094 "Map clause expression with unexpected base!");
11095
Samuel Antao90927002016-04-26 14:54:23 +000011096 // The whole expression in the stack.
11097 auto *RE = StackComponents.front().getAssociatedExpression();
11098
Samuel Antao5de996e2016-01-22 20:21:36 +000011099 // Expressions must start from the same base. Here we detect at which
11100 // point both expressions diverge from each other and see if we can
11101 // detect if the memory referred to both expressions is contiguous and
11102 // do not overlap.
11103 auto CI = CurComponents.rbegin();
11104 auto CE = CurComponents.rend();
11105 auto SI = StackComponents.rbegin();
11106 auto SE = StackComponents.rend();
11107 for (; CI != CE && SI != SE; ++CI, ++SI) {
11108
11109 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
11110 // At most one list item can be an array item derived from a given
11111 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +000011112 if (CurrentRegionOnly &&
11113 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
11114 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
11115 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
11116 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
11117 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +000011118 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +000011119 << CI->getAssociatedExpression()->getSourceRange();
11120 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
11121 diag::note_used_here)
11122 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +000011123 return true;
11124 }
11125
11126 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +000011127 if (CI->getAssociatedExpression()->getStmtClass() !=
11128 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +000011129 break;
11130
11131 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +000011132 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +000011133 break;
11134 }
Kelvin Li9f645ae2016-07-18 22:49:16 +000011135 // Check if the extra components of the expressions in the enclosing
11136 // data environment are redundant for the current base declaration.
11137 // If they are, the maps completely overlap, which is legal.
11138 for (; SI != SE; ++SI) {
11139 QualType Type;
11140 if (auto *ASE =
David Majnemer9d168222016-08-05 17:44:54 +000011141 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
Kelvin Li9f645ae2016-07-18 22:49:16 +000011142 Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
David Majnemer9d168222016-08-05 17:44:54 +000011143 } else if (auto *OASE = dyn_cast<OMPArraySectionExpr>(
11144 SI->getAssociatedExpression())) {
Kelvin Li9f645ae2016-07-18 22:49:16 +000011145 auto *E = OASE->getBase()->IgnoreParenImpCasts();
11146 Type =
11147 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
11148 }
11149 if (Type.isNull() || Type->isAnyPointerType() ||
11150 CheckArrayExpressionDoesNotReferToWholeSize(
11151 SemaRef, SI->getAssociatedExpression(), Type))
11152 break;
11153 }
Samuel Antao5de996e2016-01-22 20:21:36 +000011154
11155 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
11156 // List items of map clauses in the same construct must not share
11157 // original storage.
11158 //
11159 // If the expressions are exactly the same or one is a subset of the
11160 // other, it means they are sharing storage.
11161 if (CI == CE && SI == SE) {
11162 if (CurrentRegionOnly) {
Samuel Antao661c0902016-05-26 17:39:58 +000011163 if (CKind == OMPC_map)
11164 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
11165 else {
Samuel Antaoec172c62016-05-26 17:49:04 +000011166 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000011167 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
11168 << ERange;
11169 }
Samuel Antao5de996e2016-01-22 20:21:36 +000011170 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
11171 << RE->getSourceRange();
11172 return true;
11173 } else {
11174 // If we find the same expression in the enclosing data environment,
11175 // that is legal.
11176 IsEnclosedByDataEnvironmentExpr = true;
11177 return false;
11178 }
11179 }
11180
Samuel Antao90927002016-04-26 14:54:23 +000011181 QualType DerivedType =
11182 std::prev(CI)->getAssociatedDeclaration()->getType();
11183 SourceLocation DerivedLoc =
11184 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000011185
11186 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
11187 // If the type of a list item is a reference to a type T then the type
11188 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000011189 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000011190
11191 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
11192 // A variable for which the type is pointer and an array section
11193 // derived from that variable must not appear as list items of map
11194 // clauses of the same construct.
11195 //
11196 // Also, cover one of the cases in:
11197 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
11198 // If any part of the original storage of a list item has corresponding
11199 // storage in the device data environment, all of the original storage
11200 // must have corresponding storage in the device data environment.
11201 //
11202 if (DerivedType->isAnyPointerType()) {
11203 if (CI == CE || SI == SE) {
11204 SemaRef.Diag(
11205 DerivedLoc,
11206 diag::err_omp_pointer_mapped_along_with_derived_section)
11207 << DerivedLoc;
11208 } else {
11209 assert(CI != CE && SI != SE);
11210 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
11211 << DerivedLoc;
11212 }
11213 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
11214 << RE->getSourceRange();
11215 return true;
11216 }
11217
11218 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
11219 // List items of map clauses in the same construct must not share
11220 // original storage.
11221 //
11222 // An expression is a subset of the other.
11223 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
Samuel Antao661c0902016-05-26 17:39:58 +000011224 if (CKind == OMPC_map)
11225 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
11226 else {
Samuel Antaoec172c62016-05-26 17:49:04 +000011227 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000011228 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
11229 << ERange;
11230 }
Samuel Antao5de996e2016-01-22 20:21:36 +000011231 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
11232 << RE->getSourceRange();
11233 return true;
11234 }
11235
11236 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000011237 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000011238 if (!CurrentRegionOnly && SI != SE)
11239 EnclosingExpr = RE;
11240
11241 // The current expression is a subset of the expression in the data
11242 // environment.
11243 IsEnclosedByDataEnvironmentExpr |=
11244 (!CurrentRegionOnly && CI != CE && SI == SE);
11245
11246 return false;
11247 });
11248
11249 if (CurrentRegionOnly)
11250 return FoundError;
11251
11252 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
11253 // If any part of the original storage of a list item has corresponding
11254 // storage in the device data environment, all of the original storage must
11255 // have corresponding storage in the device data environment.
11256 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
11257 // If a list item is an element of a structure, and a different element of
11258 // the structure has a corresponding list item in the device data environment
11259 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000011260 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000011261 // data environment prior to the task encountering the construct.
11262 //
11263 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
11264 SemaRef.Diag(ELoc,
11265 diag::err_omp_original_storage_is_shared_and_does_not_contain)
11266 << ERange;
11267 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
11268 << EnclosingExpr->getSourceRange();
11269 return true;
11270 }
11271
11272 return FoundError;
11273}
11274
Samuel Antao661c0902016-05-26 17:39:58 +000011275namespace {
11276// Utility struct that gathers all the related lists associated with a mappable
11277// expression.
11278struct MappableVarListInfo final {
11279 // The list of expressions.
11280 ArrayRef<Expr *> VarList;
11281 // The list of processed expressions.
11282 SmallVector<Expr *, 16> ProcessedVarList;
11283 // The mappble components for each expression.
11284 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
11285 // The base declaration of the variable.
11286 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
11287
11288 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
11289 // We have a list of components and base declarations for each entry in the
11290 // variable list.
11291 VarComponents.reserve(VarList.size());
11292 VarBaseDeclarations.reserve(VarList.size());
11293 }
11294};
11295}
11296
11297// Check the validity of the provided variable list for the provided clause kind
11298// \a CKind. In the check process the valid expressions, and mappable expression
11299// components and variables are extracted and used to fill \a Vars,
11300// \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and
11301// \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'.
11302static void
11303checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS,
11304 OpenMPClauseKind CKind, MappableVarListInfo &MVLI,
11305 SourceLocation StartLoc,
11306 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
11307 bool IsMapTypeImplicit = false) {
Samuel Antaoec172c62016-05-26 17:49:04 +000011308 // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
11309 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
Samuel Antao661c0902016-05-26 17:39:58 +000011310 "Unexpected clause kind with mappable expressions!");
Kelvin Li0bff7af2015-11-23 05:32:03 +000011311
Samuel Antao90927002016-04-26 14:54:23 +000011312 // Keep track of the mappable components and base declarations in this clause.
11313 // Each entry in the list is going to have a list of components associated. We
11314 // record each set of the components so that we can build the clause later on.
11315 // In the end we should have the same amount of declarations and component
11316 // lists.
Samuel Antao90927002016-04-26 14:54:23 +000011317
Samuel Antao661c0902016-05-26 17:39:58 +000011318 for (auto &RE : MVLI.VarList) {
Samuel Antaoec172c62016-05-26 17:49:04 +000011319 assert(RE && "Null expr in omp to/from/map clause");
Kelvin Li0bff7af2015-11-23 05:32:03 +000011320 SourceLocation ELoc = RE->getExprLoc();
11321
Kelvin Li0bff7af2015-11-23 05:32:03 +000011322 auto *VE = RE->IgnoreParenLValueCasts();
11323
11324 if (VE->isValueDependent() || VE->isTypeDependent() ||
11325 VE->isInstantiationDependent() ||
11326 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000011327 // We can only analyze this information once the missing information is
11328 // resolved.
Samuel Antao661c0902016-05-26 17:39:58 +000011329 MVLI.ProcessedVarList.push_back(RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011330 continue;
11331 }
11332
11333 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000011334
Samuel Antao5de996e2016-01-22 20:21:36 +000011335 if (!RE->IgnoreParenImpCasts()->isLValue()) {
Samuel Antao661c0902016-05-26 17:39:58 +000011336 SemaRef.Diag(ELoc,
11337 diag::err_omp_expected_named_var_member_or_array_expression)
Samuel Antao5de996e2016-01-22 20:21:36 +000011338 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000011339 continue;
11340 }
11341
Samuel Antao90927002016-04-26 14:54:23 +000011342 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
11343 ValueDecl *CurDeclaration = nullptr;
11344
11345 // Obtain the array or member expression bases if required. Also, fill the
11346 // components array with all the components identified in the process.
Samuel Antao661c0902016-05-26 17:39:58 +000011347 auto *BE =
11348 CheckMapClauseExpressionBase(SemaRef, SimpleExpr, CurComponents, CKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000011349 if (!BE)
11350 continue;
11351
Samuel Antao90927002016-04-26 14:54:23 +000011352 assert(!CurComponents.empty() &&
11353 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000011354
Samuel Antao90927002016-04-26 14:54:23 +000011355 // For the following checks, we rely on the base declaration which is
11356 // expected to be associated with the last component. The declaration is
11357 // expected to be a variable or a field (if 'this' is being mapped).
11358 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
11359 assert(CurDeclaration && "Null decl on map clause.");
11360 assert(
11361 CurDeclaration->isCanonicalDecl() &&
11362 "Expecting components to have associated only canonical declarations.");
11363
11364 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
11365 auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000011366
11367 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000011368 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000011369
11370 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
Samuel Antao661c0902016-05-26 17:39:58 +000011371 // threadprivate variables cannot appear in a map clause.
11372 // OpenMP 4.5 [2.10.5, target update Construct]
11373 // threadprivate variables cannot appear in a from clause.
11374 if (VD && DSAS->isThreadPrivate(VD)) {
11375 auto DVar = DSAS->getTopDSA(VD, false);
11376 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
11377 << getOpenMPClauseName(CKind);
11378 ReportOriginalDSA(SemaRef, DSAS, VD, DVar);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011379 continue;
11380 }
11381
Samuel Antao5de996e2016-01-22 20:21:36 +000011382 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
11383 // A list item cannot appear in both a map clause and a data-sharing
11384 // attribute clause on the same construct.
Kelvin Li0bff7af2015-11-23 05:32:03 +000011385
Samuel Antao5de996e2016-01-22 20:21:36 +000011386 // Check conflicts with other map clause expressions. We check the conflicts
11387 // with the current construct separately from the enclosing data
Samuel Antao661c0902016-05-26 17:39:58 +000011388 // environment, because the restrictions are different. We only have to
11389 // check conflicts across regions for the map clauses.
11390 if (CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
11391 /*CurrentRegionOnly=*/true, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000011392 break;
Samuel Antao661c0902016-05-26 17:39:58 +000011393 if (CKind == OMPC_map &&
11394 CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
11395 /*CurrentRegionOnly=*/false, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000011396 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000011397
Samuel Antao661c0902016-05-26 17:39:58 +000011398 // OpenMP 4.5 [2.10.5, target update Construct]
Samuel Antao5de996e2016-01-22 20:21:36 +000011399 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
11400 // If the type of a list item is a reference to a type T then the type will
11401 // be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000011402 QualType Type = CurDeclaration->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000011403
Samuel Antao661c0902016-05-26 17:39:58 +000011404 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
11405 // A list item in a to or from clause must have a mappable type.
Samuel Antao5de996e2016-01-22 20:21:36 +000011406 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000011407 // A list item must have a mappable type.
Samuel Antao661c0902016-05-26 17:39:58 +000011408 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
11409 DSAS, Type))
Kelvin Li0bff7af2015-11-23 05:32:03 +000011410 continue;
11411
Samuel Antao661c0902016-05-26 17:39:58 +000011412 if (CKind == OMPC_map) {
11413 // target enter data
11414 // OpenMP [2.10.2, Restrictions, p. 99]
11415 // A map-type must be specified in all map clauses and must be either
11416 // to or alloc.
11417 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
11418 if (DKind == OMPD_target_enter_data &&
11419 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
11420 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
11421 << (IsMapTypeImplicit ? 1 : 0)
11422 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
11423 << getOpenMPDirectiveName(DKind);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000011424 continue;
11425 }
Samuel Antao661c0902016-05-26 17:39:58 +000011426
11427 // target exit_data
11428 // OpenMP [2.10.3, Restrictions, p. 102]
11429 // A map-type must be specified in all map clauses and must be either
11430 // from, release, or delete.
11431 if (DKind == OMPD_target_exit_data &&
11432 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
11433 MapType == OMPC_MAP_delete)) {
11434 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
11435 << (IsMapTypeImplicit ? 1 : 0)
11436 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
11437 << getOpenMPDirectiveName(DKind);
11438 continue;
11439 }
11440
11441 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
11442 // A list item cannot appear in both a map clause and a data-sharing
11443 // attribute clause on the same construct
11444 if (DKind == OMPD_target && VD) {
11445 auto DVar = DSAS->getTopDSA(VD, false);
11446 if (isOpenMPPrivate(DVar.CKind)) {
Samuel Antao6890b092016-07-28 14:25:09 +000011447 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Samuel Antao661c0902016-05-26 17:39:58 +000011448 << getOpenMPClauseName(DVar.CKind)
Samuel Antao6890b092016-07-28 14:25:09 +000011449 << getOpenMPClauseName(OMPC_map)
Samuel Antao661c0902016-05-26 17:39:58 +000011450 << getOpenMPDirectiveName(DSAS->getCurrentDirective());
11451 ReportOriginalDSA(SemaRef, DSAS, CurDeclaration, DVar);
11452 continue;
11453 }
11454 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +000011455 }
11456
Samuel Antao90927002016-04-26 14:54:23 +000011457 // Save the current expression.
Samuel Antao661c0902016-05-26 17:39:58 +000011458 MVLI.ProcessedVarList.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000011459
11460 // Store the components in the stack so that they can be used to check
11461 // against other clauses later on.
Samuel Antao6890b092016-07-28 14:25:09 +000011462 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
11463 /*WhereFoundClauseKind=*/OMPC_map);
Samuel Antao90927002016-04-26 14:54:23 +000011464
11465 // Save the components and declaration to create the clause. For purposes of
11466 // the clause creation, any component list that has has base 'this' uses
Samuel Antao686c70c2016-05-26 17:30:50 +000011467 // null as base declaration.
Samuel Antao661c0902016-05-26 17:39:58 +000011468 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
11469 MVLI.VarComponents.back().append(CurComponents.begin(),
11470 CurComponents.end());
11471 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
11472 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011473 }
Samuel Antao661c0902016-05-26 17:39:58 +000011474}
11475
11476OMPClause *
11477Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
11478 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
11479 SourceLocation MapLoc, SourceLocation ColonLoc,
11480 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
11481 SourceLocation LParenLoc, SourceLocation EndLoc) {
11482 MappableVarListInfo MVLI(VarList);
11483 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc,
11484 MapType, IsMapTypeImplicit);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011485
Samuel Antao5de996e2016-01-22 20:21:36 +000011486 // We need to produce a map clause even if we don't have variables so that
11487 // other diagnostics related with non-existing map clauses are accurate.
Samuel Antao661c0902016-05-26 17:39:58 +000011488 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11489 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11490 MVLI.VarComponents, MapTypeModifier, MapType,
11491 IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011492}
Kelvin Li099bb8c2015-11-24 20:50:12 +000011493
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011494QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
11495 TypeResult ParsedType) {
11496 assert(ParsedType.isUsable());
11497
11498 QualType ReductionType = GetTypeFromParser(ParsedType.get());
11499 if (ReductionType.isNull())
11500 return QualType();
11501
11502 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
11503 // A type name in a declare reduction directive cannot be a function type, an
11504 // array type, a reference type, or a type qualified with const, volatile or
11505 // restrict.
11506 if (ReductionType.hasQualifiers()) {
11507 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
11508 return QualType();
11509 }
11510
11511 if (ReductionType->isFunctionType()) {
11512 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
11513 return QualType();
11514 }
11515 if (ReductionType->isReferenceType()) {
11516 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
11517 return QualType();
11518 }
11519 if (ReductionType->isArrayType()) {
11520 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
11521 return QualType();
11522 }
11523 return ReductionType;
11524}
11525
11526Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
11527 Scope *S, DeclContext *DC, DeclarationName Name,
11528 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
11529 AccessSpecifier AS, Decl *PrevDeclInScope) {
11530 SmallVector<Decl *, 8> Decls;
11531 Decls.reserve(ReductionTypes.size());
11532
11533 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
11534 ForRedeclaration);
11535 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
11536 // A reduction-identifier may not be re-declared in the current scope for the
11537 // same type or for a type that is compatible according to the base language
11538 // rules.
11539 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
11540 OMPDeclareReductionDecl *PrevDRD = nullptr;
11541 bool InCompoundScope = true;
11542 if (S != nullptr) {
11543 // Find previous declaration with the same name not referenced in other
11544 // declarations.
11545 FunctionScopeInfo *ParentFn = getEnclosingFunction();
11546 InCompoundScope =
11547 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
11548 LookupName(Lookup, S);
11549 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
11550 /*AllowInlineNamespace=*/false);
11551 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
11552 auto Filter = Lookup.makeFilter();
11553 while (Filter.hasNext()) {
11554 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
11555 if (InCompoundScope) {
11556 auto I = UsedAsPrevious.find(PrevDecl);
11557 if (I == UsedAsPrevious.end())
11558 UsedAsPrevious[PrevDecl] = false;
11559 if (auto *D = PrevDecl->getPrevDeclInScope())
11560 UsedAsPrevious[D] = true;
11561 }
11562 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
11563 PrevDecl->getLocation();
11564 }
11565 Filter.done();
11566 if (InCompoundScope) {
11567 for (auto &PrevData : UsedAsPrevious) {
11568 if (!PrevData.second) {
11569 PrevDRD = PrevData.first;
11570 break;
11571 }
11572 }
11573 }
11574 } else if (PrevDeclInScope != nullptr) {
11575 auto *PrevDRDInScope = PrevDRD =
11576 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
11577 do {
11578 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
11579 PrevDRDInScope->getLocation();
11580 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
11581 } while (PrevDRDInScope != nullptr);
11582 }
11583 for (auto &TyData : ReductionTypes) {
11584 auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
11585 bool Invalid = false;
11586 if (I != PreviousRedeclTypes.end()) {
11587 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
11588 << TyData.first;
11589 Diag(I->second, diag::note_previous_definition);
11590 Invalid = true;
11591 }
11592 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
11593 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
11594 Name, TyData.first, PrevDRD);
11595 DC->addDecl(DRD);
11596 DRD->setAccess(AS);
11597 Decls.push_back(DRD);
11598 if (Invalid)
11599 DRD->setInvalidDecl();
11600 else
11601 PrevDRD = DRD;
11602 }
11603
11604 return DeclGroupPtrTy::make(
11605 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
11606}
11607
11608void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
11609 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11610
11611 // Enter new function scope.
11612 PushFunctionScope();
11613 getCurFunction()->setHasBranchProtectedScope();
11614 getCurFunction()->setHasOMPDeclareReductionCombiner();
11615
11616 if (S != nullptr)
11617 PushDeclContext(S, DRD);
11618 else
11619 CurContext = DRD;
11620
11621 PushExpressionEvaluationContext(PotentiallyEvaluated);
11622
11623 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011624 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
11625 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
11626 // uses semantics of argument handles by value, but it should be passed by
11627 // reference. C lang does not support references, so pass all parameters as
11628 // pointers.
11629 // Create 'T omp_in;' variable.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011630 auto *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011631 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011632 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
11633 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
11634 // uses semantics of argument handles by value, but it should be passed by
11635 // reference. C lang does not support references, so pass all parameters as
11636 // pointers.
11637 // Create 'T omp_out;' variable.
11638 auto *OmpOutParm =
11639 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
11640 if (S != nullptr) {
11641 PushOnScopeChains(OmpInParm, S);
11642 PushOnScopeChains(OmpOutParm, S);
11643 } else {
11644 DRD->addDecl(OmpInParm);
11645 DRD->addDecl(OmpOutParm);
11646 }
11647}
11648
11649void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
11650 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11651 DiscardCleanupsInEvaluationContext();
11652 PopExpressionEvaluationContext();
11653
11654 PopDeclContext();
11655 PopFunctionScopeInfo();
11656
11657 if (Combiner != nullptr)
11658 DRD->setCombiner(Combiner);
11659 else
11660 DRD->setInvalidDecl();
11661}
11662
11663void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
11664 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11665
11666 // Enter new function scope.
11667 PushFunctionScope();
11668 getCurFunction()->setHasBranchProtectedScope();
11669
11670 if (S != nullptr)
11671 PushDeclContext(S, DRD);
11672 else
11673 CurContext = DRD;
11674
11675 PushExpressionEvaluationContext(PotentiallyEvaluated);
11676
11677 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011678 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
11679 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
11680 // uses semantics of argument handles by value, but it should be passed by
11681 // reference. C lang does not support references, so pass all parameters as
11682 // pointers.
11683 // Create 'T omp_priv;' variable.
11684 auto *OmpPrivParm =
11685 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011686 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
11687 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
11688 // uses semantics of argument handles by value, but it should be passed by
11689 // reference. C lang does not support references, so pass all parameters as
11690 // pointers.
11691 // Create 'T omp_orig;' variable.
11692 auto *OmpOrigParm =
11693 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011694 if (S != nullptr) {
11695 PushOnScopeChains(OmpPrivParm, S);
11696 PushOnScopeChains(OmpOrigParm, S);
11697 } else {
11698 DRD->addDecl(OmpPrivParm);
11699 DRD->addDecl(OmpOrigParm);
11700 }
11701}
11702
11703void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D,
11704 Expr *Initializer) {
11705 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11706 DiscardCleanupsInEvaluationContext();
11707 PopExpressionEvaluationContext();
11708
11709 PopDeclContext();
11710 PopFunctionScopeInfo();
11711
11712 if (Initializer != nullptr)
11713 DRD->setInitializer(Initializer);
11714 else
11715 DRD->setInvalidDecl();
11716}
11717
11718Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
11719 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
11720 for (auto *D : DeclReductions.get()) {
11721 if (IsValid) {
11722 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11723 if (S != nullptr)
11724 PushOnScopeChains(DRD, S, /*AddToContext=*/false);
11725 } else
11726 D->setInvalidDecl();
11727 }
11728 return DeclReductions;
11729}
11730
David Majnemer9d168222016-08-05 17:44:54 +000011731OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
Kelvin Li099bb8c2015-11-24 20:50:12 +000011732 SourceLocation StartLoc,
11733 SourceLocation LParenLoc,
11734 SourceLocation EndLoc) {
11735 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +000011736
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011737 // OpenMP [teams Constrcut, Restrictions]
11738 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000011739 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
11740 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011741 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000011742
11743 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11744}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011745
11746OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
11747 SourceLocation StartLoc,
11748 SourceLocation LParenLoc,
11749 SourceLocation EndLoc) {
11750 Expr *ValExpr = ThreadLimit;
11751
11752 // OpenMP [teams Constrcut, Restrictions]
11753 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000011754 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
11755 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011756 return nullptr;
11757
David Majnemer9d168222016-08-05 17:44:54 +000011758 return new (Context)
11759 OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011760}
Alexey Bataeva0569352015-12-01 10:17:31 +000011761
11762OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
11763 SourceLocation StartLoc,
11764 SourceLocation LParenLoc,
11765 SourceLocation EndLoc) {
11766 Expr *ValExpr = Priority;
11767
11768 // OpenMP [2.9.1, task Constrcut]
11769 // The priority-value is a non-negative numerical scalar expression.
11770 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
11771 /*StrictlyPositive=*/false))
11772 return nullptr;
11773
11774 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11775}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000011776
11777OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
11778 SourceLocation StartLoc,
11779 SourceLocation LParenLoc,
11780 SourceLocation EndLoc) {
11781 Expr *ValExpr = Grainsize;
11782
11783 // OpenMP [2.9.2, taskloop Constrcut]
11784 // The parameter of the grainsize clause must be a positive integer
11785 // expression.
11786 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
11787 /*StrictlyPositive=*/true))
11788 return nullptr;
11789
11790 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11791}
Alexey Bataev382967a2015-12-08 12:06:20 +000011792
11793OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
11794 SourceLocation StartLoc,
11795 SourceLocation LParenLoc,
11796 SourceLocation EndLoc) {
11797 Expr *ValExpr = NumTasks;
11798
11799 // OpenMP [2.9.2, taskloop Constrcut]
11800 // The parameter of the num_tasks clause must be a positive integer
11801 // expression.
11802 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
11803 /*StrictlyPositive=*/true))
11804 return nullptr;
11805
11806 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11807}
11808
Alexey Bataev28c75412015-12-15 08:19:24 +000011809OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
11810 SourceLocation LParenLoc,
11811 SourceLocation EndLoc) {
11812 // OpenMP [2.13.2, critical construct, Description]
11813 // ... where hint-expression is an integer constant expression that evaluates
11814 // to a valid lock hint.
11815 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
11816 if (HintExpr.isInvalid())
11817 return nullptr;
11818 return new (Context)
11819 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
11820}
11821
Carlo Bertollib4adf552016-01-15 18:50:31 +000011822OMPClause *Sema::ActOnOpenMPDistScheduleClause(
11823 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
11824 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
11825 SourceLocation EndLoc) {
11826 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
11827 std::string Values;
11828 Values += "'";
11829 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
11830 Values += "'";
11831 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
11832 << Values << getOpenMPClauseName(OMPC_dist_schedule);
11833 return nullptr;
11834 }
11835 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000011836 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000011837 if (ChunkSize) {
11838 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
11839 !ChunkSize->isInstantiationDependent() &&
11840 !ChunkSize->containsUnexpandedParameterPack()) {
11841 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
11842 ExprResult Val =
11843 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
11844 if (Val.isInvalid())
11845 return nullptr;
11846
11847 ValExpr = Val.get();
11848
11849 // OpenMP [2.7.1, Restrictions]
11850 // chunk_size must be a loop invariant integer expression with a positive
11851 // value.
11852 llvm::APSInt Result;
11853 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
11854 if (Result.isSigned() && !Result.isStrictlyPositive()) {
11855 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
11856 << "dist_schedule" << ChunkSize->getSourceRange();
11857 return nullptr;
11858 }
Alexey Bataevb46cdea2016-06-15 11:20:48 +000011859 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
11860 !CurContext->isDependentContext()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +000011861 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
11862 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
11863 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000011864 }
11865 }
11866 }
11867
11868 return new (Context)
11869 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000011870 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000011871}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000011872
11873OMPClause *Sema::ActOnOpenMPDefaultmapClause(
11874 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
11875 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
11876 SourceLocation KindLoc, SourceLocation EndLoc) {
11877 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
David Majnemer9d168222016-08-05 17:44:54 +000011878 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) {
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000011879 std::string Value;
11880 SourceLocation Loc;
11881 Value += "'";
11882 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
11883 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000011884 OMPC_DEFAULTMAP_MODIFIER_tofrom);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000011885 Loc = MLoc;
11886 } else {
11887 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000011888 OMPC_DEFAULTMAP_scalar);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000011889 Loc = KindLoc;
11890 }
11891 Value += "'";
11892 Diag(Loc, diag::err_omp_unexpected_clause_value)
11893 << Value << getOpenMPClauseName(OMPC_defaultmap);
11894 return nullptr;
11895 }
11896
11897 return new (Context)
11898 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
11899}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011900
11901bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
11902 DeclContext *CurLexicalContext = getCurLexicalContext();
11903 if (!CurLexicalContext->isFileContext() &&
11904 !CurLexicalContext->isExternCContext() &&
11905 !CurLexicalContext->isExternCXXContext()) {
11906 Diag(Loc, diag::err_omp_region_not_file_context);
11907 return false;
11908 }
11909 if (IsInOpenMPDeclareTargetContext) {
11910 Diag(Loc, diag::err_omp_enclosed_declare_target);
11911 return false;
11912 }
11913
11914 IsInOpenMPDeclareTargetContext = true;
11915 return true;
11916}
11917
11918void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
11919 assert(IsInOpenMPDeclareTargetContext &&
11920 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
11921
11922 IsInOpenMPDeclareTargetContext = false;
11923}
11924
David Majnemer9d168222016-08-05 17:44:54 +000011925void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope,
11926 CXXScopeSpec &ScopeSpec,
11927 const DeclarationNameInfo &Id,
11928 OMPDeclareTargetDeclAttr::MapTypeTy MT,
11929 NamedDeclSetType &SameDirectiveDecls) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011930 LookupResult Lookup(*this, Id, LookupOrdinaryName);
11931 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
11932
11933 if (Lookup.isAmbiguous())
11934 return;
11935 Lookup.suppressDiagnostics();
11936
11937 if (!Lookup.isSingleResult()) {
11938 if (TypoCorrection Corrected =
11939 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
11940 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
11941 CTK_ErrorRecovery)) {
11942 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
11943 << Id.getName());
11944 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
11945 return;
11946 }
11947
11948 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
11949 return;
11950 }
11951
11952 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
11953 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
11954 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
11955 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
11956
11957 if (!ND->hasAttr<OMPDeclareTargetDeclAttr>()) {
11958 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
11959 ND->addAttr(A);
11960 if (ASTMutationListener *ML = Context.getASTMutationListener())
11961 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
11962 checkDeclIsAllowedInOpenMPTarget(nullptr, ND);
11963 } else if (ND->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() != MT) {
11964 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
11965 << Id.getName();
11966 }
11967 } else
11968 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
11969}
11970
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011971static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
11972 Sema &SemaRef, Decl *D) {
11973 if (!D)
11974 return;
11975 Decl *LD = nullptr;
11976 if (isa<TagDecl>(D)) {
11977 LD = cast<TagDecl>(D)->getDefinition();
11978 } else if (isa<VarDecl>(D)) {
11979 LD = cast<VarDecl>(D)->getDefinition();
11980
11981 // If this is an implicit variable that is legal and we do not need to do
11982 // anything.
11983 if (cast<VarDecl>(D)->isImplicit()) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011984 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11985 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11986 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011987 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011988 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011989 return;
11990 }
11991
11992 } else if (isa<FunctionDecl>(D)) {
11993 const FunctionDecl *FD = nullptr;
11994 if (cast<FunctionDecl>(D)->hasBody(FD))
11995 LD = const_cast<FunctionDecl *>(FD);
11996
11997 // If the definition is associated with the current declaration in the
11998 // target region (it can be e.g. a lambda) that is legal and we do not need
11999 // to do anything else.
12000 if (LD == D) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012001 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
12002 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
12003 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012004 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012005 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012006 return;
12007 }
12008 }
12009 if (!LD)
12010 LD = D;
12011 if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() &&
12012 (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) {
12013 // Outlined declaration is not declared target.
12014 if (LD->isOutOfLine()) {
12015 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
12016 SemaRef.Diag(SL, diag::note_used_here) << SR;
12017 } else {
12018 DeclContext *DC = LD->getDeclContext();
12019 while (DC) {
12020 if (isa<FunctionDecl>(DC) &&
12021 cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>())
12022 break;
12023 DC = DC->getParent();
12024 }
12025 if (DC)
12026 return;
12027
12028 // Is not declared in target context.
12029 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
12030 SemaRef.Diag(SL, diag::note_used_here) << SR;
12031 }
12032 // Mark decl as declared target to prevent further diagnostic.
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012033 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
12034 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
12035 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012036 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012037 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012038 }
12039}
12040
12041static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
12042 Sema &SemaRef, DSAStackTy *Stack,
12043 ValueDecl *VD) {
12044 if (VD->hasAttr<OMPDeclareTargetDeclAttr>())
12045 return true;
12046 if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType()))
12047 return false;
12048 return true;
12049}
12050
12051void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D) {
12052 if (!D || D->isInvalidDecl())
12053 return;
12054 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
12055 SourceLocation SL = E ? E->getLocStart() : D->getLocation();
12056 // 2.10.6: threadprivate variable cannot appear in a declare target directive.
12057 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
12058 if (DSAStack->isThreadPrivate(VD)) {
12059 Diag(SL, diag::err_omp_threadprivate_in_target);
12060 ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
12061 return;
12062 }
12063 }
12064 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
12065 // Problem if any with var declared with incomplete type will be reported
12066 // as normal, so no need to check it here.
12067 if ((E || !VD->getType()->isIncompleteType()) &&
12068 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) {
12069 // Mark decl as declared target to prevent further diagnostic.
12070 if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012071 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
12072 Context, OMPDeclareTargetDeclAttr::MT_To);
12073 VD->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012074 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012075 ML->DeclarationMarkedOpenMPDeclareTarget(VD, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012076 }
12077 return;
12078 }
12079 }
12080 if (!E) {
12081 // Checking declaration inside declare target region.
12082 if (!D->hasAttr<OMPDeclareTargetDeclAttr>() &&
12083 (isa<VarDecl>(D) || isa<FunctionDecl>(D))) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012084 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
12085 Context, OMPDeclareTargetDeclAttr::MT_To);
12086 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012087 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012088 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012089 }
12090 return;
12091 }
12092 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
12093}
Samuel Antao661c0902016-05-26 17:39:58 +000012094
12095OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
12096 SourceLocation StartLoc,
12097 SourceLocation LParenLoc,
12098 SourceLocation EndLoc) {
12099 MappableVarListInfo MVLI(VarList);
12100 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc);
12101 if (MVLI.ProcessedVarList.empty())
12102 return nullptr;
12103
12104 return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc,
12105 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
12106 MVLI.VarComponents);
12107}
Samuel Antaoec172c62016-05-26 17:49:04 +000012108
12109OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
12110 SourceLocation StartLoc,
12111 SourceLocation LParenLoc,
12112 SourceLocation EndLoc) {
12113 MappableVarListInfo MVLI(VarList);
12114 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc);
12115 if (MVLI.ProcessedVarList.empty())
12116 return nullptr;
12117
12118 return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc,
12119 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
12120 MVLI.VarComponents);
12121}
Carlo Bertolli2404b172016-07-13 15:37:16 +000012122
12123OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
12124 SourceLocation StartLoc,
12125 SourceLocation LParenLoc,
12126 SourceLocation EndLoc) {
Samuel Antaocc10b852016-07-28 14:23:26 +000012127 MappableVarListInfo MVLI(VarList);
12128 SmallVector<Expr *, 8> PrivateCopies;
12129 SmallVector<Expr *, 8> Inits;
12130
Carlo Bertolli2404b172016-07-13 15:37:16 +000012131 for (auto &RefExpr : VarList) {
12132 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
12133 SourceLocation ELoc;
12134 SourceRange ERange;
12135 Expr *SimpleRefExpr = RefExpr;
12136 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
12137 if (Res.second) {
12138 // It will be analyzed later.
Samuel Antaocc10b852016-07-28 14:23:26 +000012139 MVLI.ProcessedVarList.push_back(RefExpr);
12140 PrivateCopies.push_back(nullptr);
12141 Inits.push_back(nullptr);
Carlo Bertolli2404b172016-07-13 15:37:16 +000012142 }
12143 ValueDecl *D = Res.first;
12144 if (!D)
12145 continue;
12146
12147 QualType Type = D->getType();
Samuel Antaocc10b852016-07-28 14:23:26 +000012148 Type = Type.getNonReferenceType().getUnqualifiedType();
12149
12150 auto *VD = dyn_cast<VarDecl>(D);
12151
12152 // Item should be a pointer or reference to pointer.
12153 if (!Type->isPointerType()) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000012154 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
12155 << 0 << RefExpr->getSourceRange();
12156 continue;
12157 }
Samuel Antaocc10b852016-07-28 14:23:26 +000012158
12159 // Build the private variable and the expression that refers to it.
12160 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
12161 D->hasAttrs() ? &D->getAttrs() : nullptr);
12162 if (VDPrivate->isInvalidDecl())
12163 continue;
12164
12165 CurContext->addDecl(VDPrivate);
12166 auto VDPrivateRefExpr = buildDeclRefExpr(
12167 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
12168
12169 // Add temporary variable to initialize the private copy of the pointer.
12170 auto *VDInit =
12171 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
12172 auto *VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
12173 RefExpr->getExprLoc());
12174 AddInitializerToDecl(VDPrivate,
12175 DefaultLvalueConversion(VDInitRefExpr).get(),
12176 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
12177
12178 // If required, build a capture to implement the privatization initialized
12179 // with the current list item value.
12180 DeclRefExpr *Ref = nullptr;
12181 if (!VD)
12182 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
12183 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
12184 PrivateCopies.push_back(VDPrivateRefExpr);
12185 Inits.push_back(VDInitRefExpr);
12186
12187 // We need to add a data sharing attribute for this variable to make sure it
12188 // is correctly captured. A variable that shows up in a use_device_ptr has
12189 // similar properties of a first private variable.
12190 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
12191
12192 // Create a mappable component for the list item. List items in this clause
12193 // only need a component.
12194 MVLI.VarBaseDeclarations.push_back(D);
12195 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
12196 MVLI.VarComponents.back().push_back(
12197 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D));
Carlo Bertolli2404b172016-07-13 15:37:16 +000012198 }
12199
Samuel Antaocc10b852016-07-28 14:23:26 +000012200 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli2404b172016-07-13 15:37:16 +000012201 return nullptr;
12202
Samuel Antaocc10b852016-07-28 14:23:26 +000012203 return OMPUseDevicePtrClause::Create(
12204 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
12205 PrivateCopies, Inits, MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli2404b172016-07-13 15:37:16 +000012206}
Carlo Bertolli70594e92016-07-13 17:16:49 +000012207
12208OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
12209 SourceLocation StartLoc,
12210 SourceLocation LParenLoc,
12211 SourceLocation EndLoc) {
Samuel Antao6890b092016-07-28 14:25:09 +000012212 MappableVarListInfo MVLI(VarList);
Carlo Bertolli70594e92016-07-13 17:16:49 +000012213 for (auto &RefExpr : VarList) {
12214 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
12215 SourceLocation ELoc;
12216 SourceRange ERange;
12217 Expr *SimpleRefExpr = RefExpr;
12218 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
12219 if (Res.second) {
12220 // It will be analyzed later.
Samuel Antao6890b092016-07-28 14:25:09 +000012221 MVLI.ProcessedVarList.push_back(RefExpr);
Carlo Bertolli70594e92016-07-13 17:16:49 +000012222 }
12223 ValueDecl *D = Res.first;
12224 if (!D)
12225 continue;
12226
12227 QualType Type = D->getType();
12228 // item should be a pointer or array or reference to pointer or array
12229 if (!Type.getNonReferenceType()->isPointerType() &&
12230 !Type.getNonReferenceType()->isArrayType()) {
12231 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
12232 << 0 << RefExpr->getSourceRange();
12233 continue;
12234 }
Samuel Antao6890b092016-07-28 14:25:09 +000012235
12236 // Check if the declaration in the clause does not show up in any data
12237 // sharing attribute.
12238 auto DVar = DSAStack->getTopDSA(D, false);
12239 if (isOpenMPPrivate(DVar.CKind)) {
12240 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
12241 << getOpenMPClauseName(DVar.CKind)
12242 << getOpenMPClauseName(OMPC_is_device_ptr)
12243 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
12244 ReportOriginalDSA(*this, DSAStack, D, DVar);
12245 continue;
12246 }
12247
12248 Expr *ConflictExpr;
12249 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +000012250 D, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +000012251 [&ConflictExpr](
12252 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
12253 OpenMPClauseKind) -> bool {
12254 ConflictExpr = R.front().getAssociatedExpression();
12255 return true;
12256 })) {
12257 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
12258 Diag(ConflictExpr->getExprLoc(), diag::note_used_here)
12259 << ConflictExpr->getSourceRange();
12260 continue;
12261 }
12262
12263 // Store the components in the stack so that they can be used to check
12264 // against other clauses later on.
12265 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D);
12266 DSAStack->addMappableExpressionComponents(
12267 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
12268
12269 // Record the expression we've just processed.
12270 MVLI.ProcessedVarList.push_back(SimpleRefExpr);
12271
12272 // Create a mappable component for the list item. List items in this clause
12273 // only need a component. We use a null declaration to signal fields in
12274 // 'this'.
12275 assert((isa<DeclRefExpr>(SimpleRefExpr) ||
12276 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
12277 "Unexpected device pointer expression!");
12278 MVLI.VarBaseDeclarations.push_back(
12279 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr);
12280 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
12281 MVLI.VarComponents.back().push_back(MC);
Carlo Bertolli70594e92016-07-13 17:16:49 +000012282 }
12283
Samuel Antao6890b092016-07-28 14:25:09 +000012284 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli70594e92016-07-13 17:16:49 +000012285 return nullptr;
12286
Samuel Antao6890b092016-07-28 14:25:09 +000012287 return OMPIsDevicePtrClause::Create(
12288 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
12289 MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli70594e92016-07-13 17:16:49 +000012290}