blob: 7dcd81f69d8ca6e642c45cfeca50488e0cde3279 [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:
Diana Picus8b44bbc2016-08-18 09:25:07 +00001696 case OMPD_teams_distribute: {
Carlo Bertolli9925f152016-06-27 14:55:37 +00001697 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1698 QualType KmpInt32PtrTy =
1699 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
1700 Sema::CapturedParamNameType Params[] = {
1701 std::make_pair(".global_tid.", KmpInt32PtrTy),
1702 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1703 std::make_pair(".previous.lb.", Context.getSizeType()),
1704 std::make_pair(".previous.ub.", Context.getSizeType()),
1705 std::make_pair(StringRef(), QualType()) // __context with shared vars
1706 };
1707 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1708 Params);
1709 break;
1710 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001711 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001712 case OMPD_taskyield:
1713 case OMPD_barrier:
1714 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001715 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001716 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001717 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001718 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001719 case OMPD_target_exit_data:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001720 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001721 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001722 case OMPD_declare_target:
1723 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +00001724 case OMPD_target_update:
Alexey Bataev9959db52014-05-06 10:08:46 +00001725 llvm_unreachable("OpenMP Directive is not allowed");
1726 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001727 llvm_unreachable("Unknown OpenMP directive");
1728 }
1729}
1730
Alexey Bataev3392d762016-02-16 11:18:12 +00001731static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00001732 Expr *CaptureExpr, bool WithInit,
1733 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001734 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00001735 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00001736 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00001737 QualType Ty = Init->getType();
1738 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1739 if (S.getLangOpts().CPlusPlus)
1740 Ty = C.getLValueReferenceType(Ty);
1741 else {
1742 Ty = C.getPointerType(Ty);
1743 ExprResult Res =
1744 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1745 if (!Res.isUsable())
1746 return nullptr;
1747 Init = Res.get();
1748 }
Alexey Bataev61205072016-03-02 04:57:40 +00001749 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00001750 }
1751 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001752 if (!WithInit)
1753 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
Alexey Bataev4244be22016-02-11 05:35:55 +00001754 S.CurContext->addHiddenDecl(CED);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001755 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false,
1756 /*TypeMayContainAuto=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00001757 return CED;
1758}
1759
Alexey Bataev61205072016-03-02 04:57:40 +00001760static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
1761 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00001762 OMPCapturedExprDecl *CD;
1763 if (auto *VD = S.IsOpenMPCapturedDecl(D))
1764 CD = cast<OMPCapturedExprDecl>(VD);
1765 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00001766 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
1767 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001768 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00001769 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00001770}
1771
Alexey Bataev5a3af132016-03-29 08:58:54 +00001772static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
1773 if (!Ref) {
1774 auto *CD =
1775 buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."),
1776 CaptureExpr, /*WithInit=*/true, /*AsExpression=*/true);
1777 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1778 CaptureExpr->getExprLoc());
1779 }
1780 ExprResult Res = Ref;
1781 if (!S.getLangOpts().CPlusPlus &&
1782 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
1783 Ref->getType()->isPointerType())
1784 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
1785 if (!Res.isUsable())
1786 return ExprError();
1787 return CaptureExpr->isGLValue() ? Res : S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00001788}
1789
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001790StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1791 ArrayRef<OMPClause *> Clauses) {
1792 if (!S.isUsable()) {
1793 ActOnCapturedRegionError();
1794 return StmtError();
1795 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001796
1797 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001798 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001799 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001800 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001801 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001802 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001803 Clause->getClauseKind() == OMPC_copyprivate ||
1804 (getLangOpts().OpenMPUseTLS &&
1805 getASTContext().getTargetInfo().isTLSSupported() &&
1806 Clause->getClauseKind() == OMPC_copyin)) {
1807 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001808 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001809 for (auto *VarRef : Clause->children()) {
1810 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001811 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001812 }
1813 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001814 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001815 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev040d5402015-05-12 08:35:28 +00001816 // Mark all variables in private list clauses as used in inner region.
1817 // Required for proper codegen of combined directives.
1818 // TODO: add processing for other clauses.
Alexey Bataev3392d762016-02-16 11:18:12 +00001819 if (auto *C = OMPClauseWithPreInit::get(Clause)) {
Alexey Bataev005248a2016-02-25 05:25:57 +00001820 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
1821 for (auto *D : DS->decls())
Alexey Bataev3392d762016-02-16 11:18:12 +00001822 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
1823 }
Alexey Bataev4244be22016-02-11 05:35:55 +00001824 }
Alexey Bataev005248a2016-02-25 05:25:57 +00001825 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
1826 if (auto *E = C->getPostUpdateExpr())
1827 MarkDeclarationsReferencedInExpr(E);
1828 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001829 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001830 if (Clause->getClauseKind() == OMPC_schedule)
1831 SC = cast<OMPScheduleClause>(Clause);
1832 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001833 OC = cast<OMPOrderedClause>(Clause);
1834 else if (Clause->getClauseKind() == OMPC_linear)
1835 LCs.push_back(cast<OMPLinearClause>(Clause));
1836 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001837 bool ErrorFound = false;
1838 // OpenMP, 2.7.1 Loop Construct, Restrictions
1839 // The nonmonotonic modifier cannot be specified if an ordered clause is
1840 // specified.
1841 if (SC &&
1842 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1843 SC->getSecondScheduleModifier() ==
1844 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1845 OC) {
1846 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1847 ? SC->getFirstScheduleModifierLoc()
1848 : SC->getSecondScheduleModifierLoc(),
1849 diag::err_omp_schedule_nonmonotonic_ordered)
1850 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1851 ErrorFound = true;
1852 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001853 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1854 for (auto *C : LCs) {
1855 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1856 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1857 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001858 ErrorFound = true;
1859 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001860 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1861 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1862 OC->getNumForLoops()) {
1863 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1864 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1865 ErrorFound = true;
1866 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001867 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001868 ActOnCapturedRegionError();
1869 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001870 }
1871 return ActOnCapturedRegionEnd(S.get());
1872}
1873
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001874static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1875 OpenMPDirectiveKind CurrentRegion,
1876 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001877 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001878 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001879 // Allowed nesting of constructs
1880 // +------------------+-----------------+------------------------------------+
1881 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1882 // +------------------+-----------------+------------------------------------+
1883 // | parallel | parallel | * |
1884 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001885 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001886 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001887 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001888 // | parallel | simd | * |
1889 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001890 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001891 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001892 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001893 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001894 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001895 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001896 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001897 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001898 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001899 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001900 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001901 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001902 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001903 // | parallel | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001904 // | parallel | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001905 // | parallel | target parallel | * |
1906 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001907 // | parallel | target enter | * |
1908 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001909 // | parallel | target exit | * |
1910 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001911 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001912 // | parallel | cancellation | |
1913 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001914 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001915 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001916 // | parallel | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00001917 // | parallel | distribute | + |
1918 // | parallel | distribute | + |
1919 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00001920 // | parallel | distribute | + |
1921 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00001922 // | parallel | distribute simd | + |
Kelvin Li986330c2016-07-20 22:57:10 +00001923 // | parallel | target simd | * |
Kelvin Li02532872016-08-05 14:37:37 +00001924 // | parallel | teams distribute| + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001925 // +------------------+-----------------+------------------------------------+
1926 // | for | parallel | * |
1927 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001928 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001929 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001930 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001931 // | for | simd | * |
1932 // | for | sections | + |
1933 // | for | section | + |
1934 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001935 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001936 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001937 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001938 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001939 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001940 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001941 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001942 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001943 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001944 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001945 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001946 // | for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001947 // | for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001948 // | for | target parallel | * |
1949 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001950 // | for | target enter | * |
1951 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001952 // | for | target exit | * |
1953 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001954 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001955 // | for | cancellation | |
1956 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001957 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001958 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001959 // | for | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00001960 // | for | distribute | + |
1961 // | for | distribute | + |
1962 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00001963 // | for | distribute | + |
1964 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00001965 // | for | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00001966 // | for | target parallel | + |
1967 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00001968 // | for | target simd | * |
Kelvin Li02532872016-08-05 14:37:37 +00001969 // | for | teams distribute| + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001970 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001971 // | master | parallel | * |
1972 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001973 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001974 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001975 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001976 // | master | simd | * |
1977 // | master | sections | + |
1978 // | master | section | + |
1979 // | master | single | + |
1980 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001981 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001982 // | master |parallel sections| * |
1983 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001984 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001985 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001986 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001987 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001988 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001989 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001990 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001991 // | master | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001992 // | master | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001993 // | master | target parallel | * |
1994 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001995 // | master | target enter | * |
1996 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001997 // | master | target exit | * |
1998 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001999 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002000 // | master | cancellation | |
2001 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002002 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002003 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002004 // | master | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002005 // | master | distribute | + |
2006 // | master | distribute | + |
2007 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002008 // | master | distribute | + |
2009 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002010 // | master | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002011 // | master | target parallel | + |
2012 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002013 // | master | target simd | * |
Kelvin Li02532872016-08-05 14:37:37 +00002014 // | master | teams distribute| + |
Alexander Musman80c22892014-07-17 08:54:58 +00002015 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002016 // | critical | parallel | * |
2017 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002018 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002019 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002020 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002021 // | critical | simd | * |
2022 // | critical | sections | + |
2023 // | critical | section | + |
2024 // | critical | single | + |
2025 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002026 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002027 // | critical |parallel sections| * |
2028 // | critical | task | * |
2029 // | critical | taskyield | * |
2030 // | critical | barrier | + |
2031 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002032 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002033 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002034 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002035 // | critical | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002036 // | critical | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002037 // | critical | target parallel | * |
2038 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002039 // | critical | target enter | * |
2040 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002041 // | critical | target exit | * |
2042 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002043 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002044 // | critical | cancellation | |
2045 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002046 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002047 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002048 // | critical | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002049 // | critical | distribute | + |
2050 // | critical | distribute | + |
2051 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002052 // | critical | distribute | + |
2053 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002054 // | critical | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002055 // | critical | target parallel | + |
2056 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002057 // | critical | target simd | * |
Kelvin Li02532872016-08-05 14:37:37 +00002058 // | critical | teams distribute| + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002059 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002060 // | simd | parallel | |
2061 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002062 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00002063 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002064 // | simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002065 // | simd | simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002066 // | simd | sections | |
2067 // | simd | section | |
2068 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002069 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002070 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002071 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002072 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00002073 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002074 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002075 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002076 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00002077 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002078 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002079 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002080 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002081 // | simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002082 // | simd | target parallel | |
2083 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002084 // | simd | target enter | |
2085 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002086 // | simd | target exit | |
2087 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002088 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002089 // | simd | cancellation | |
2090 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002091 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002092 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002093 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002094 // | simd | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002095 // | simd | distribute | |
2096 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002097 // | simd | distribute | |
2098 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002099 // | simd | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002100 // | simd | target parallel | |
2101 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002102 // | simd | target simd | |
Kelvin Li02532872016-08-05 14:37:37 +00002103 // | simd | teams distribute| |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002104 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00002105 // | for simd | parallel | |
2106 // | for simd | for | |
2107 // | for simd | for simd | |
2108 // | for simd | master | |
2109 // | for simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002110 // | for simd | simd | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002111 // | for simd | sections | |
2112 // | for simd | section | |
2113 // | for simd | single | |
2114 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002115 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002116 // | for simd |parallel sections| |
2117 // | for simd | task | |
2118 // | for simd | taskyield | |
2119 // | for simd | barrier | |
2120 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002121 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002122 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002123 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002124 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002125 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002126 // | for simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002127 // | for simd | target parallel | |
2128 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002129 // | for simd | target enter | |
2130 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002131 // | for simd | target exit | |
2132 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002133 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002134 // | for simd | cancellation | |
2135 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002136 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002137 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002138 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002139 // | for simd | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002140 // | for simd | distribute | |
2141 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002142 // | for simd | distribute | |
2143 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002144 // | for simd | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002145 // | for simd | target parallel | |
2146 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002147 // | for simd | target simd | |
Kelvin Li02532872016-08-05 14:37:37 +00002148 // | for simd | teams distribute| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002149 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00002150 // | parallel for simd| parallel | |
2151 // | parallel for simd| for | |
2152 // | parallel for simd| for simd | |
2153 // | parallel for simd| master | |
2154 // | parallel for simd| critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002155 // | parallel for simd| simd | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002156 // | parallel for simd| sections | |
2157 // | parallel for simd| section | |
2158 // | parallel for simd| single | |
2159 // | parallel for simd| parallel for | |
2160 // | parallel for simd|parallel for simd| |
2161 // | parallel for simd|parallel sections| |
2162 // | parallel for simd| task | |
2163 // | parallel for simd| taskyield | |
2164 // | parallel for simd| barrier | |
2165 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002166 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002167 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002168 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002169 // | parallel for simd| atomic | |
2170 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002171 // | parallel for simd| target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002172 // | parallel for simd| target parallel | |
2173 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002174 // | parallel for simd| target enter | |
2175 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002176 // | parallel for simd| target exit | |
2177 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002178 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002179 // | parallel for simd| cancellation | |
2180 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002181 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002182 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002183 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002184 // | parallel for simd| distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002185 // | parallel for simd| distribute | |
2186 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002187 // | parallel for simd| distribute | |
2188 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002189 // | parallel for simd| distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002190 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002191 // | parallel for simd| target simd | |
Kelvin Li02532872016-08-05 14:37:37 +00002192 // | parallel for simd| teams distribute| |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002193 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002194 // | sections | parallel | * |
2195 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002196 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002197 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002198 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002199 // | sections | simd | * |
2200 // | sections | sections | + |
2201 // | sections | section | * |
2202 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002203 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002204 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002205 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002206 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002207 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002208 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002209 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002210 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002211 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002212 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002213 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002214 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002215 // | sections | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002216 // | sections | target parallel | * |
2217 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002218 // | sections | target enter | * |
2219 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002220 // | sections | target exit | * |
2221 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002222 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002223 // | sections | cancellation | |
2224 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002225 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002226 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002227 // | sections | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002228 // | sections | distribute | + |
2229 // | sections | distribute | + |
2230 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002231 // | sections | distribute | + |
2232 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002233 // | sections | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002234 // | sections | target parallel | + |
2235 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002236 // | sections | target simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002237 // +------------------+-----------------+------------------------------------+
2238 // | section | parallel | * |
2239 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002240 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002241 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002242 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002243 // | section | simd | * |
2244 // | section | sections | + |
2245 // | section | section | + |
2246 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002247 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002248 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002249 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002250 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002251 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002252 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002253 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002254 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002255 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002256 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002257 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002258 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002259 // | section | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002260 // | section | target parallel | * |
2261 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002262 // | section | target enter | * |
2263 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002264 // | section | target exit | * |
2265 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002266 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002267 // | section | cancellation | |
2268 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002269 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002270 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002271 // | section | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002272 // | section | distribute | + |
2273 // | section | distribute | + |
2274 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002275 // | section | distribute | + |
2276 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002277 // | section | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002278 // | section | target parallel | + |
2279 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002280 // | section | target simd | * |
Kelvin Li02532872016-08-05 14:37:37 +00002281 // | section | teams distrubte | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002282 // +------------------+-----------------+------------------------------------+
2283 // | single | parallel | * |
2284 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002285 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002286 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002287 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002288 // | single | simd | * |
2289 // | single | sections | + |
2290 // | single | section | + |
2291 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002292 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002293 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002294 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002295 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002296 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002297 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002298 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002299 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002300 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002301 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002302 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002303 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002304 // | single | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002305 // | single | target parallel | * |
2306 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002307 // | single | target enter | * |
2308 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002309 // | single | target exit | * |
2310 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002311 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002312 // | single | cancellation | |
2313 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002314 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002315 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002316 // | single | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002317 // | single | distribute | + |
2318 // | single | distribute | + |
2319 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002320 // | single | distribute | + |
2321 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002322 // | single | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002323 // | single | target parallel | + |
2324 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002325 // | single | target simd | * |
Kelvin Li02532872016-08-05 14:37:37 +00002326 // | single | teams distrubte | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002327 // +------------------+-----------------+------------------------------------+
2328 // | parallel for | parallel | * |
2329 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002330 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002331 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002332 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002333 // | parallel for | simd | * |
2334 // | parallel for | sections | + |
2335 // | parallel for | section | + |
2336 // | parallel for | single | + |
2337 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002338 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002339 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002340 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002341 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002342 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002343 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002344 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002345 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002346 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002347 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002348 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002349 // | parallel for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002350 // | parallel for | target parallel | * |
2351 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002352 // | parallel for | target enter | * |
2353 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002354 // | parallel for | target exit | * |
2355 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002356 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002357 // | parallel for | cancellation | |
2358 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002359 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002360 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002361 // | parallel for | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002362 // | parallel for | distribute | + |
2363 // | parallel for | distribute | + |
2364 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002365 // | parallel for | distribute | + |
2366 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002367 // | parallel for | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002368 // | parallel for | target parallel | + |
2369 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002370 // | parallel for | target simd | * |
Kelvin Li02532872016-08-05 14:37:37 +00002371 // | parallel for | teams distribute| + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002372 // +------------------+-----------------+------------------------------------+
2373 // | parallel sections| parallel | * |
2374 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002375 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002376 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002377 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002378 // | parallel sections| simd | * |
2379 // | parallel sections| sections | + |
2380 // | parallel sections| section | * |
2381 // | parallel sections| single | + |
2382 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002383 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002384 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002385 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002386 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002387 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002388 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002389 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002390 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002391 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002392 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002393 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002394 // | parallel sections| target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002395 // | parallel sections| target parallel | * |
2396 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002397 // | parallel sections| target enter | * |
2398 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002399 // | parallel sections| target exit | * |
2400 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002401 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002402 // | parallel sections| cancellation | |
2403 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002404 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002405 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002406 // | parallel sections| taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002407 // | parallel sections| distribute | + |
2408 // | parallel sections| distribute | + |
2409 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002410 // | parallel sections| distribute | + |
2411 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002412 // | parallel sections| distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002413 // | parallel sections| target parallel | + |
2414 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002415 // | parallel sections| target simd | * |
Kelvin Li02532872016-08-05 14:37:37 +00002416 // | parallel sections| teams distribute| + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002417 // +------------------+-----------------+------------------------------------+
2418 // | task | parallel | * |
2419 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002420 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002421 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002422 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002423 // | task | simd | * |
2424 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002425 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002426 // | task | single | + |
2427 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002428 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002429 // | task |parallel sections| * |
2430 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002431 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002432 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002433 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002434 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002435 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002436 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002437 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002438 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002439 // | task | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002440 // | task | target parallel | * |
2441 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002442 // | task | target enter | * |
2443 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002444 // | task | target exit | * |
2445 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002446 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002447 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002448 // | | point | ! |
2449 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002450 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002451 // | task | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002452 // | task | distribute | + |
2453 // | task | distribute | + |
2454 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002455 // | task | distribute | + |
2456 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002457 // | task | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002458 // | task | target parallel | + |
2459 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002460 // | task | target simd | * |
Kelvin Li02532872016-08-05 14:37:37 +00002461 // | task | teams distribute| + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002462 // +------------------+-----------------+------------------------------------+
2463 // | ordered | parallel | * |
2464 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002465 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002466 // | ordered | master | * |
2467 // | ordered | critical | * |
2468 // | ordered | simd | * |
2469 // | ordered | sections | + |
2470 // | ordered | section | + |
2471 // | ordered | single | + |
2472 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002473 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002474 // | ordered |parallel sections| * |
2475 // | ordered | task | * |
2476 // | ordered | taskyield | * |
2477 // | ordered | barrier | + |
2478 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002479 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002480 // | ordered | flush | * |
2481 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002482 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002483 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002484 // | ordered | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002485 // | ordered | target parallel | * |
2486 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002487 // | ordered | target enter | * |
2488 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002489 // | ordered | target exit | * |
2490 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002491 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002492 // | ordered | cancellation | |
2493 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002494 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002495 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002496 // | ordered | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002497 // | ordered | distribute | + |
2498 // | ordered | distribute | + |
2499 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002500 // | ordered | distribute | + |
2501 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002502 // | ordered | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002503 // | ordered | target parallel | + |
2504 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002505 // | ordered | target simd | * |
Kelvin Li02532872016-08-05 14:37:37 +00002506 // | ordered | teams distribute| + |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002507 // +------------------+-----------------+------------------------------------+
2508 // | atomic | parallel | |
2509 // | atomic | for | |
2510 // | atomic | for simd | |
2511 // | atomic | master | |
2512 // | atomic | critical | |
2513 // | atomic | simd | |
2514 // | atomic | sections | |
2515 // | atomic | section | |
2516 // | atomic | single | |
2517 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002518 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002519 // | atomic |parallel sections| |
2520 // | atomic | task | |
2521 // | atomic | taskyield | |
2522 // | atomic | barrier | |
2523 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002524 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002525 // | atomic | flush | |
2526 // | atomic | ordered | |
2527 // | atomic | atomic | |
2528 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002529 // | atomic | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002530 // | atomic | target parallel | |
2531 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002532 // | atomic | target enter | |
2533 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002534 // | atomic | target exit | |
2535 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002536 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002537 // | atomic | cancellation | |
2538 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002539 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002540 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002541 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002542 // | atomic | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002543 // | atomic | distribute | |
2544 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002545 // | atomic | distribute | |
2546 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002547 // | atomic | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002548 // | atomic | target parallel | |
2549 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002550 // | atomic | target simd | |
Kelvin Li02532872016-08-05 14:37:37 +00002551 // | atomic | teams distribute| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002552 // +------------------+-----------------+------------------------------------+
2553 // | target | parallel | * |
2554 // | target | for | * |
2555 // | target | for simd | * |
2556 // | target | master | * |
2557 // | target | critical | * |
2558 // | target | simd | * |
2559 // | target | sections | * |
2560 // | target | section | * |
2561 // | target | single | * |
2562 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002563 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002564 // | target |parallel sections| * |
2565 // | target | task | * |
2566 // | target | taskyield | * |
2567 // | target | barrier | * |
2568 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002569 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002570 // | target | flush | * |
2571 // | target | ordered | * |
2572 // | target | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002573 // | target | target | |
2574 // | target | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002575 // | target | target parallel | |
2576 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002577 // | target | target enter | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002578 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002579 // | target | target exit | |
Samuel Antao72590762016-01-19 20:04:50 +00002580 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002581 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002582 // | target | cancellation | |
2583 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002584 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002585 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002586 // | target | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002587 // | target | distribute | + |
2588 // | target | distribute | + |
2589 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002590 // | target | distribute | + |
2591 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002592 // | target | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002593 // | target | target parallel | |
2594 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002595 // | target | target simd | |
Kelvin Li02532872016-08-05 14:37:37 +00002596 // | target | teams distribute| |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002597 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002598 // | target parallel | parallel | * |
2599 // | target parallel | for | * |
2600 // | target parallel | for simd | * |
2601 // | target parallel | master | * |
2602 // | target parallel | critical | * |
2603 // | target parallel | simd | * |
2604 // | target parallel | sections | * |
2605 // | target parallel | section | * |
2606 // | target parallel | single | * |
2607 // | target parallel | parallel for | * |
2608 // | target parallel |parallel for simd| * |
2609 // | target parallel |parallel sections| * |
2610 // | target parallel | task | * |
2611 // | target parallel | taskyield | * |
2612 // | target parallel | barrier | * |
2613 // | target parallel | taskwait | * |
2614 // | target parallel | taskgroup | * |
2615 // | target parallel | flush | * |
2616 // | target parallel | ordered | * |
2617 // | target parallel | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002618 // | target parallel | target | |
2619 // | target parallel | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002620 // | target parallel | target parallel | |
2621 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002622 // | target parallel | target enter | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002623 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002624 // | target parallel | target exit | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002625 // | | data | |
2626 // | target parallel | teams | |
2627 // | target parallel | cancellation | |
2628 // | | point | ! |
2629 // | target parallel | cancel | ! |
2630 // | target parallel | taskloop | * |
2631 // | target parallel | taskloop simd | * |
2632 // | target parallel | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002633 // | target parallel | distribute | |
2634 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002635 // | target parallel | distribute | |
2636 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002637 // | target parallel | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002638 // | target parallel | target parallel | |
2639 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002640 // | target parallel | target simd | |
Kelvin Li02532872016-08-05 14:37:37 +00002641 // | target parallel | teams distribute| + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002642 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002643 // | target parallel | parallel | * |
2644 // | for | | |
2645 // | target parallel | for | * |
2646 // | for | | |
2647 // | target parallel | for simd | * |
2648 // | for | | |
2649 // | target parallel | master | * |
2650 // | for | | |
2651 // | target parallel | critical | * |
2652 // | for | | |
2653 // | target parallel | simd | * |
2654 // | for | | |
2655 // | target parallel | sections | * |
2656 // | for | | |
2657 // | target parallel | section | * |
2658 // | for | | |
2659 // | target parallel | single | * |
2660 // | for | | |
2661 // | target parallel | parallel for | * |
2662 // | for | | |
2663 // | target parallel |parallel for simd| * |
2664 // | for | | |
2665 // | target parallel |parallel sections| * |
2666 // | for | | |
2667 // | target parallel | task | * |
2668 // | for | | |
2669 // | target parallel | taskyield | * |
2670 // | for | | |
2671 // | target parallel | barrier | * |
2672 // | for | | |
2673 // | target parallel | taskwait | * |
2674 // | for | | |
2675 // | target parallel | taskgroup | * |
2676 // | for | | |
2677 // | target parallel | flush | * |
2678 // | for | | |
2679 // | target parallel | ordered | * |
2680 // | for | | |
2681 // | target parallel | atomic | * |
2682 // | for | | |
2683 // | target parallel | target | |
2684 // | for | | |
2685 // | target parallel | target parallel | |
2686 // | for | | |
2687 // | target parallel | target parallel | |
2688 // | for | for | |
2689 // | target parallel | target enter | |
2690 // | for | data | |
2691 // | target parallel | target exit | |
2692 // | for | data | |
2693 // | target parallel | teams | |
2694 // | for | | |
2695 // | target parallel | cancellation | |
2696 // | for | point | ! |
2697 // | target parallel | cancel | ! |
2698 // | for | | |
2699 // | target parallel | taskloop | * |
2700 // | for | | |
2701 // | target parallel | taskloop simd | * |
2702 // | for | | |
2703 // | target parallel | distribute | |
2704 // | for | | |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002705 // | target parallel | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002706 // | for | parallel for | |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002707 // | target parallel | distribute | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002708 // | for |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002709 // | target parallel | distribute simd | |
2710 // | for | | |
Kelvin Lia579b912016-07-14 02:54:56 +00002711 // | target parallel | target parallel | |
2712 // | for | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002713 // | target parallel | target simd | |
2714 // | for | | |
Kelvin Li02532872016-08-05 14:37:37 +00002715 // | target parallel | teams distribute| |
2716 // | for | | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002717 // +------------------+-----------------+------------------------------------+
Alexey Bataev13314bf2014-10-09 04:18:56 +00002718 // | teams | parallel | * |
2719 // | teams | for | + |
2720 // | teams | for simd | + |
2721 // | teams | master | + |
2722 // | teams | critical | + |
2723 // | teams | simd | + |
2724 // | teams | sections | + |
2725 // | teams | section | + |
2726 // | teams | single | + |
2727 // | teams | parallel for | * |
2728 // | teams |parallel for simd| * |
2729 // | teams |parallel sections| * |
2730 // | teams | task | + |
2731 // | teams | taskyield | + |
2732 // | teams | barrier | + |
2733 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002734 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002735 // | teams | flush | + |
2736 // | teams | ordered | + |
2737 // | teams | atomic | + |
2738 // | teams | target | + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002739 // | teams | target parallel | + |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002740 // | teams | target parallel | + |
2741 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002742 // | teams | target enter | + |
2743 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002744 // | teams | target exit | + |
2745 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002746 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002747 // | teams | cancellation | |
2748 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002749 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002750 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002751 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002752 // | teams | distribute | ! |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002753 // | teams | distribute | ! |
2754 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002755 // | teams | distribute | ! |
2756 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002757 // | teams | distribute simd | ! |
Kelvin Lia579b912016-07-14 02:54:56 +00002758 // | teams | target parallel | + |
2759 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002760 // | teams | target simd | + |
Kelvin Li02532872016-08-05 14:37:37 +00002761 // | teams | teams distribute| + |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002762 // +------------------+-----------------+------------------------------------+
2763 // | taskloop | parallel | * |
2764 // | taskloop | for | + |
2765 // | taskloop | for simd | + |
2766 // | taskloop | master | + |
2767 // | taskloop | critical | * |
2768 // | taskloop | simd | * |
2769 // | taskloop | sections | + |
2770 // | taskloop | section | + |
2771 // | taskloop | single | + |
2772 // | taskloop | parallel for | * |
2773 // | taskloop |parallel for simd| * |
2774 // | taskloop |parallel sections| * |
2775 // | taskloop | task | * |
2776 // | taskloop | taskyield | * |
2777 // | taskloop | barrier | + |
2778 // | taskloop | taskwait | * |
2779 // | taskloop | taskgroup | * |
2780 // | taskloop | flush | * |
2781 // | taskloop | ordered | + |
2782 // | taskloop | atomic | * |
2783 // | taskloop | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002784 // | taskloop | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002785 // | taskloop | target parallel | * |
2786 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002787 // | taskloop | target enter | * |
2788 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002789 // | taskloop | target exit | * |
2790 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002791 // | taskloop | teams | + |
2792 // | taskloop | cancellation | |
2793 // | | point | |
2794 // | taskloop | cancel | |
2795 // | taskloop | taskloop | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002796 // | taskloop | distribute | + |
2797 // | taskloop | distribute | + |
2798 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002799 // | taskloop | distribute | + |
2800 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002801 // | taskloop | distribute simd | + |
Kelvin Lia579b912016-07-14 02:54:56 +00002802 // | taskloop | target parallel | * |
2803 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002804 // | taskloop | target simd | * |
Kelvin Li02532872016-08-05 14:37:37 +00002805 // | taskloop | teams distribute| + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002806 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002807 // | taskloop simd | parallel | |
2808 // | taskloop simd | for | |
2809 // | taskloop simd | for simd | |
2810 // | taskloop simd | master | |
2811 // | taskloop simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002812 // | taskloop simd | simd | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002813 // | taskloop simd | sections | |
2814 // | taskloop simd | section | |
2815 // | taskloop simd | single | |
2816 // | taskloop simd | parallel for | |
2817 // | taskloop simd |parallel for simd| |
2818 // | taskloop simd |parallel sections| |
2819 // | taskloop simd | task | |
2820 // | taskloop simd | taskyield | |
2821 // | taskloop simd | barrier | |
2822 // | taskloop simd | taskwait | |
2823 // | taskloop simd | taskgroup | |
2824 // | taskloop simd | flush | |
2825 // | taskloop simd | ordered | + (with simd clause) |
2826 // | taskloop simd | atomic | |
2827 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002828 // | taskloop simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002829 // | taskloop simd | target parallel | |
2830 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002831 // | taskloop simd | target enter | |
2832 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002833 // | taskloop simd | target exit | |
2834 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002835 // | taskloop simd | teams | |
2836 // | taskloop simd | cancellation | |
2837 // | | point | |
2838 // | taskloop simd | cancel | |
2839 // | taskloop simd | taskloop | |
2840 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002841 // | taskloop simd | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002842 // | taskloop simd | distribute | |
2843 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002844 // | taskloop simd | distribute | |
2845 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002846 // | taskloop simd | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002847 // | taskloop simd | target parallel | |
2848 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002849 // | taskloop simd | target simd | |
Kelvin Li02532872016-08-05 14:37:37 +00002850 // | taskloop simd | teams distribute| |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002851 // +------------------+-----------------+------------------------------------+
2852 // | distribute | parallel | * |
2853 // | distribute | for | * |
2854 // | distribute | for simd | * |
2855 // | distribute | master | * |
2856 // | distribute | critical | * |
2857 // | distribute | simd | * |
2858 // | distribute | sections | * |
2859 // | distribute | section | * |
2860 // | distribute | single | * |
2861 // | distribute | parallel for | * |
2862 // | distribute |parallel for simd| * |
2863 // | distribute |parallel sections| * |
2864 // | distribute | task | * |
2865 // | distribute | taskyield | * |
2866 // | distribute | barrier | * |
2867 // | distribute | taskwait | * |
2868 // | distribute | taskgroup | * |
2869 // | distribute | flush | * |
2870 // | distribute | ordered | + |
2871 // | distribute | atomic | * |
2872 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002873 // | distribute | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002874 // | distribute | target parallel | |
2875 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002876 // | distribute | target enter | |
2877 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002878 // | distribute | target exit | |
2879 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002880 // | distribute | teams | |
2881 // | distribute | cancellation | + |
2882 // | | point | |
2883 // | distribute | cancel | + |
2884 // | distribute | taskloop | * |
2885 // | distribute | taskloop simd | * |
2886 // | distribute | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002887 // | distribute | distribute | |
2888 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002889 // | distribute | distribute | |
2890 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002891 // | distribute | distribute simd | |
Kelvin Lia579b912016-07-14 02:54:56 +00002892 // | distribute | target parallel | |
2893 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002894 // | distribute | target simd | |
Kelvin Li02532872016-08-05 14:37:37 +00002895 // | distribute | teams distribute| |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002896 // +------------------+-----------------+------------------------------------+
2897 // | distribute | parallel | * |
2898 // | parallel for | | |
2899 // | distribute | for | * |
2900 // | parallel for | | |
2901 // | distribute | for simd | * |
2902 // | parallel for | | |
2903 // | distribute | master | * |
2904 // | parallel for | | |
2905 // | distribute | critical | * |
2906 // | parallel for | | |
2907 // | distribute | simd | * |
2908 // | parallel for | | |
2909 // | distribute | sections | * |
2910 // | parallel for | | |
2911 // | distribute | section | * |
2912 // | parallel for | | |
2913 // | distribute | single | * |
2914 // | parallel for | | |
2915 // | distribute | parallel for | * |
2916 // | parallel for | | |
2917 // | distribute |parallel for simd| * |
2918 // | parallel for | | |
2919 // | distribute |parallel sections| * |
2920 // | parallel for | | |
2921 // | distribute | task | * |
2922 // | parallel for | | |
2923 // | parallel for | | |
2924 // | distribute | taskyield | * |
2925 // | parallel for | | |
2926 // | distribute | barrier | * |
2927 // | parallel for | | |
2928 // | distribute | taskwait | * |
2929 // | parallel for | | |
2930 // | distribute | taskgroup | * |
2931 // | parallel for | | |
2932 // | distribute | flush | * |
2933 // | parallel for | | |
2934 // | distribute | ordered | + |
2935 // | parallel for | | |
2936 // | distribute | atomic | * |
2937 // | parallel for | | |
2938 // | distribute | target | |
2939 // | parallel for | | |
2940 // | distribute | target parallel | |
2941 // | parallel for | | |
2942 // | distribute | target parallel | |
2943 // | parallel for | for | |
2944 // | distribute | target enter | |
2945 // | parallel for | data | |
2946 // | distribute | target exit | |
2947 // | parallel for | data | |
2948 // | distribute | teams | |
2949 // | parallel for | | |
2950 // | distribute | cancellation | + |
2951 // | parallel for | point | |
2952 // | distribute | cancel | + |
2953 // | parallel for | | |
2954 // | distribute | taskloop | * |
2955 // | parallel for | | |
2956 // | distribute | taskloop simd | * |
2957 // | parallel for | | |
2958 // | distribute | distribute | |
2959 // | parallel for | | |
2960 // | distribute | distribute | |
2961 // | parallel for | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002962 // | distribute | distribute | |
2963 // | parallel for |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002964 // | distribute | distribute simd | |
2965 // | parallel for | | |
Kelvin Lia579b912016-07-14 02:54:56 +00002966 // | distribute | target parallel | |
2967 // | parallel for | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00002968 // | distribute | target simd | |
2969 // | parallel for | | |
Kelvin Li02532872016-08-05 14:37:37 +00002970 // | distribute | teams distribute| |
2971 // | parallel for | | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002972 // +------------------+-----------------+------------------------------------+
2973 // | distribute | parallel | * |
2974 // | parallel for simd| | |
2975 // | distribute | for | * |
2976 // | parallel for simd| | |
2977 // | distribute | for simd | * |
2978 // | parallel for simd| | |
2979 // | distribute | master | * |
2980 // | parallel for simd| | |
2981 // | distribute | critical | * |
2982 // | parallel for simd| | |
2983 // | distribute | simd | * |
2984 // | parallel for simd| | |
2985 // | distribute | sections | * |
2986 // | parallel for simd| | |
2987 // | distribute | section | * |
2988 // | parallel for simd| | |
2989 // | distribute | single | * |
2990 // | parallel for simd| | |
2991 // | distribute | parallel for | * |
2992 // | parallel for simd| | |
2993 // | distribute |parallel for simd| * |
2994 // | parallel for simd| | |
2995 // | distribute |parallel sections| * |
2996 // | parallel for simd| | |
2997 // | distribute | task | * |
2998 // | parallel for simd| | |
2999 // | distribute | taskyield | * |
3000 // | parallel for simd| | |
3001 // | distribute | barrier | * |
3002 // | parallel for simd| | |
3003 // | distribute | taskwait | * |
3004 // | parallel for simd| | |
3005 // | distribute | taskgroup | * |
3006 // | parallel for simd| | |
3007 // | distribute | flush | * |
3008 // | parallel for simd| | |
3009 // | distribute | ordered | + |
3010 // | parallel for simd| | |
3011 // | distribute | atomic | * |
3012 // | parallel for simd| | |
3013 // | distribute | target | |
3014 // | parallel for simd| | |
3015 // | distribute | target parallel | |
3016 // | parallel for simd| | |
3017 // | distribute | target parallel | |
3018 // | parallel for simd| for | |
3019 // | distribute | target enter | |
3020 // | parallel for simd| data | |
3021 // | distribute | target exit | |
3022 // | parallel for simd| data | |
3023 // | distribute | teams | |
3024 // | parallel for simd| | |
3025 // | distribute | cancellation | + |
3026 // | parallel for simd| point | |
3027 // | distribute | cancel | + |
3028 // | parallel for simd| | |
3029 // | distribute | taskloop | * |
3030 // | parallel for simd| | |
3031 // | distribute | taskloop simd | * |
3032 // | parallel for simd| | |
3033 // | distribute | distribute | |
3034 // | parallel for simd| | |
3035 // | distribute | distribute | * |
3036 // | parallel for simd| parallel for | |
3037 // | distribute | distribute | * |
3038 // | parallel for simd|parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00003039 // | distribute | distribute simd | * |
3040 // | parallel for simd| | |
Kelvin Lia579b912016-07-14 02:54:56 +00003041 // | distribute | target parallel | |
3042 // | parallel for simd| for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00003043 // | distribute | target simd | |
3044 // | parallel for simd| | |
Kelvin Li02532872016-08-05 14:37:37 +00003045 // | distribute | teams distribute| |
3046 // | parallel for simd| | |
Kelvin Li787f3fc2016-07-06 04:45:38 +00003047 // +------------------+-----------------+------------------------------------+
3048 // | distribute simd | parallel | * |
3049 // | distribute simd | for | * |
3050 // | distribute simd | for simd | * |
3051 // | distribute simd | master | * |
3052 // | distribute simd | critical | * |
3053 // | distribute simd | simd | * |
3054 // | distribute simd | sections | * |
3055 // | distribute simd | section | * |
3056 // | distribute simd | single | * |
3057 // | distribute simd | parallel for | * |
3058 // | distribute simd |parallel for simd| * |
3059 // | distribute simd |parallel sections| * |
3060 // | distribute simd | task | * |
3061 // | distribute simd | taskyield | * |
3062 // | distribute simd | barrier | * |
3063 // | distribute simd | taskwait | * |
3064 // | distribute simd | taskgroup | * |
3065 // | distribute simd | flush | * |
3066 // | distribute simd | ordered | + |
3067 // | distribute simd | atomic | * |
3068 // | distribute simd | target | * |
3069 // | distribute simd | target parallel | * |
3070 // | distribute simd | target parallel | * |
3071 // | | for | |
3072 // | distribute simd | target enter | * |
3073 // | | data | |
3074 // | distribute simd | target exit | * |
3075 // | | data | |
3076 // | distribute simd | teams | * |
3077 // | distribute simd | cancellation | + |
3078 // | | point | |
3079 // | distribute simd | cancel | + |
3080 // | distribute simd | taskloop | * |
3081 // | distribute simd | taskloop simd | * |
3082 // | distribute simd | distribute | |
3083 // | distribute simd | distribute | * |
3084 // | | parallel for | |
3085 // | distribute simd | distribute | * |
3086 // | |parallel for simd| |
3087 // | distribute simd | distribute simd | * |
Kelvin Lia579b912016-07-14 02:54:56 +00003088 // | distribute simd | target parallel | * |
3089 // | | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00003090 // | distribute simd | target simd | * |
Kelvin Li02532872016-08-05 14:37:37 +00003091 // | distribute simd | teams distribute| * |
Kelvin Lia579b912016-07-14 02:54:56 +00003092 // +------------------+-----------------+------------------------------------+
3093 // | target parallel | parallel | * |
3094 // | for simd | | |
3095 // | target parallel | for | * |
3096 // | for simd | | |
3097 // | target parallel | for simd | * |
3098 // | for simd | | |
3099 // | target parallel | master | * |
3100 // | for simd | | |
3101 // | target parallel | critical | * |
3102 // | for simd | | |
3103 // | target parallel | simd | ! |
3104 // | for simd | | |
3105 // | target parallel | sections | * |
3106 // | for simd | | |
3107 // | target parallel | section | * |
3108 // | for simd | | |
3109 // | target parallel | single | * |
3110 // | for simd | | |
3111 // | target parallel | parallel for | * |
3112 // | for simd | | |
3113 // | target parallel |parallel for simd| * |
3114 // | for simd | | |
3115 // | target parallel |parallel sections| * |
3116 // | for simd | | |
3117 // | target parallel | task | * |
3118 // | for simd | | |
3119 // | target parallel | taskyield | * |
3120 // | for simd | | |
3121 // | target parallel | barrier | * |
3122 // | for simd | | |
3123 // | target parallel | taskwait | * |
3124 // | for simd | | |
3125 // | target parallel | taskgroup | * |
3126 // | for simd | | |
3127 // | target parallel | flush | * |
3128 // | for simd | | |
3129 // | target parallel | ordered | + (with simd clause) |
3130 // | for simd | | |
3131 // | target parallel | atomic | * |
3132 // | for simd | | |
3133 // | target parallel | target | * |
3134 // | for simd | | |
3135 // | target parallel | target parallel | * |
3136 // | for simd | | |
3137 // | target parallel | target parallel | * |
3138 // | for simd | for | |
3139 // | target parallel | target enter | * |
3140 // | for simd | data | |
3141 // | target parallel | target exit | * |
3142 // | for simd | data | |
3143 // | target parallel | teams | * |
3144 // | for simd | | |
3145 // | target parallel | cancellation | * |
3146 // | for simd | point | |
3147 // | target parallel | cancel | * |
3148 // | for simd | | |
3149 // | target parallel | taskloop | * |
3150 // | for simd | | |
3151 // | target parallel | taskloop simd | * |
3152 // | for simd | | |
3153 // | target parallel | distribute | * |
3154 // | for simd | | |
3155 // | target parallel | distribute | * |
3156 // | for simd | parallel for | |
3157 // | target parallel | distribute | * |
3158 // | for simd |parallel for simd| |
3159 // | target parallel | distribute simd | * |
3160 // | for simd | | |
3161 // | target parallel | target parallel | * |
3162 // | for simd | for simd | |
Kelvin Li986330c2016-07-20 22:57:10 +00003163 // | target parallel | target simd | * |
3164 // | for simd | | |
Kelvin Li02532872016-08-05 14:37:37 +00003165 // | target parallel | teams distribute| * |
3166 // | for simd | | |
Kelvin Li986330c2016-07-20 22:57:10 +00003167 // +------------------+-----------------+------------------------------------+
3168 // | target simd | parallel | |
3169 // | target simd | for | |
3170 // | target simd | for simd | |
3171 // | target simd | master | |
3172 // | target simd | critical | |
3173 // | target simd | simd | |
3174 // | target simd | sections | |
3175 // | target simd | section | |
3176 // | target simd | single | |
3177 // | target simd | parallel for | |
3178 // | target simd |parallel for simd| |
3179 // | target simd |parallel sections| |
3180 // | target simd | task | |
3181 // | target simd | taskyield | |
3182 // | target simd | barrier | |
3183 // | target simd | taskwait | |
3184 // | target simd | taskgroup | |
3185 // | target simd | flush | |
3186 // | target simd | ordered | + (with simd clause) |
3187 // | target simd | atomic | |
3188 // | target simd | target | |
3189 // | target simd | target parallel | |
3190 // | target simd | target parallel | |
3191 // | | for | |
3192 // | target simd | target enter | |
3193 // | | data | |
3194 // | target simd | target exit | |
3195 // | | data | |
3196 // | target simd | teams | |
3197 // | target simd | cancellation | |
3198 // | | point | |
3199 // | target simd | cancel | |
3200 // | target simd | taskloop | |
3201 // | target simd | taskloop simd | |
3202 // | target simd | distribute | |
3203 // | target simd | distribute | |
3204 // | | parallel for | |
3205 // | target simd | distribute | |
3206 // | |parallel for simd| |
3207 // | target simd | distribute simd | |
3208 // | target simd | target parallel | |
3209 // | | for simd | |
3210 // | target simd | target simd | |
Kelvin Li02532872016-08-05 14:37:37 +00003211 // | target simd | teams distribute| |
3212 // +------------------+-----------------+------------------------------------+
3213 // | teams distribute | parallel | |
3214 // | teams distribute | for | |
3215 // | teams distribute | for simd | |
3216 // | teams distribute | master | |
3217 // | teams distribute | critical | |
3218 // | teams distribute | simd | |
3219 // | teams distribute | sections | |
3220 // | teams distribute | section | |
3221 // | teams distribute | single | |
3222 // | teams distribute | parallel for | |
3223 // | teams distribute |parallel for simd| |
3224 // | teams distribute |parallel sections| |
3225 // | teams distribute | task | |
3226 // | teams distribute | taskyield | |
3227 // | teams distribute | barrier | |
3228 // | teams distribute | taskwait | |
3229 // | teams distribute | taskgroup | |
3230 // | teams distribute | flush | |
3231 // | teams distribute | ordered | + (with simd clause) |
3232 // | teams distribute | atomic | |
3233 // | teams distribute | target | |
3234 // | teams distribute | target parallel | |
3235 // | teams distribute | target parallel | |
3236 // | | for | |
3237 // | teams distribute | target enter | |
3238 // | | data | |
3239 // | teams distribute | target exit | |
3240 // | | data | |
3241 // | teams distribute | teams | |
3242 // | teams distribute | cancellation | |
3243 // | | point | |
3244 // | teams distribute | cancel | |
3245 // | teams distribute | taskloop | |
3246 // | teams distribute | taskloop simd | |
3247 // | teams distribute | distribute | |
3248 // | teams distribute | distribute | |
3249 // | | parallel for | |
3250 // | teams distribute | distribute | |
3251 // | |parallel for simd| |
3252 // | teams distribute | distribute simd | |
3253 // | teams distribute | target parallel | |
3254 // | | for simd | |
3255 // | teams distribute | teams distribute| |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003256 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00003257 if (Stack->getCurScope()) {
3258 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003259 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003260 bool NestingProhibited = false;
3261 bool CloseNesting = true;
David Majnemer9d168222016-08-05 17:44:54 +00003262 bool OrphanSeen = false;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003263 enum {
3264 NoRecommend,
3265 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00003266 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003267 ShouldBeInTargetRegion,
3268 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003269 } Recommend = NoRecommend;
Kelvin Lifd8b5742016-07-01 14:30:25 +00003270 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003271 // OpenMP [2.16, Nesting of Regions]
3272 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003273 // OpenMP [2.8.1,simd Construct, Restrictions]
Kelvin Lifd8b5742016-07-01 14:30:25 +00003274 // An ordered construct with the simd clause is the only OpenMP
3275 // construct that can appear in the simd region.
David Majnemer9d168222016-08-05 17:44:54 +00003276 // Allowing a SIMD construct nested in another SIMD construct is an
Kelvin Lifd8b5742016-07-01 14:30:25 +00003277 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
3278 // message.
3279 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
3280 ? diag::err_omp_prohibited_region_simd
3281 : diag::warn_omp_nesting_simd);
3282 return CurrentRegion != OMPD_simd;
Alexey Bataev549210e2014-06-24 04:39:47 +00003283 }
Alexey Bataev0162e452014-07-22 10:10:35 +00003284 if (ParentRegion == OMPD_atomic) {
3285 // OpenMP [2.16, Nesting of Regions]
3286 // OpenMP constructs may not be nested inside an atomic region.
3287 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
3288 return true;
3289 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003290 if (CurrentRegion == OMPD_section) {
3291 // OpenMP [2.7.2, sections Construct, Restrictions]
3292 // Orphaned section directives are prohibited. That is, the section
3293 // directives must appear within the sections construct and must not be
3294 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003295 if (ParentRegion != OMPD_sections &&
3296 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003297 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
3298 << (ParentRegion != OMPD_unknown)
3299 << getOpenMPDirectiveName(ParentRegion);
3300 return true;
3301 }
3302 return false;
3303 }
Kelvin Li2b51f722016-07-26 04:32:50 +00003304 // Allow some constructs (except teams) to be orphaned (they could be
David Majnemer9d168222016-08-05 17:44:54 +00003305 // used in functions, called from OpenMP regions with the required
Kelvin Li2b51f722016-07-26 04:32:50 +00003306 // preconditions).
3307 if (ParentRegion == OMPD_unknown && !isOpenMPTeamsDirective(CurrentRegion))
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003308 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00003309 if (CurrentRegion == OMPD_cancellation_point ||
3310 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003311 // OpenMP [2.16, Nesting of Regions]
3312 // A cancellation point construct for which construct-type-clause is
3313 // taskgroup must be nested inside a task construct. A cancellation
3314 // point construct for which construct-type-clause is not taskgroup must
3315 // be closely nested inside an OpenMP construct that matches the type
3316 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00003317 // A cancel construct for which construct-type-clause is taskgroup must be
3318 // nested inside a task construct. A cancel construct for which
3319 // construct-type-clause is not taskgroup must be closely nested inside an
3320 // OpenMP construct that matches the type specified in
3321 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003322 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003323 !((CancelRegion == OMPD_parallel &&
3324 (ParentRegion == OMPD_parallel ||
3325 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00003326 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003327 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
3328 ParentRegion == OMPD_target_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003329 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
3330 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00003331 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
3332 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003333 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00003334 // OpenMP [2.16, Nesting of Regions]
3335 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003336 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00003337 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003338 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003339 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
3340 // OpenMP [2.16, Nesting of Regions]
3341 // A critical region may not be nested (closely or otherwise) inside a
3342 // critical region with the same name. Note that this restriction is not
3343 // sufficient to prevent deadlock.
3344 SourceLocation PreviousCriticalLoc;
David Majnemer9d168222016-08-05 17:44:54 +00003345 bool DeadLock = Stack->hasDirective(
3346 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
3347 const DeclarationNameInfo &DNI,
3348 SourceLocation Loc) -> bool {
3349 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
3350 PreviousCriticalLoc = Loc;
3351 return true;
3352 } else
3353 return false;
3354 },
3355 false /* skip top directive */);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003356 if (DeadLock) {
3357 SemaRef.Diag(StartLoc,
3358 diag::err_omp_prohibited_region_critical_same_name)
3359 << CurrentName.getName();
3360 if (PreviousCriticalLoc.isValid())
3361 SemaRef.Diag(PreviousCriticalLoc,
3362 diag::note_omp_previous_critical_region);
3363 return true;
3364 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003365 } else if (CurrentRegion == OMPD_barrier) {
3366 // OpenMP [2.16, Nesting of Regions]
3367 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003368 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003369 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3370 isOpenMPTaskingDirective(ParentRegion) ||
3371 ParentRegion == OMPD_master ||
3372 ParentRegion == OMPD_critical ||
3373 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00003374 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00003375 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003376 // OpenMP [2.16, Nesting of Regions]
3377 // A worksharing region may not be closely nested inside a worksharing,
3378 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003379 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3380 isOpenMPTaskingDirective(ParentRegion) ||
3381 ParentRegion == OMPD_master ||
3382 ParentRegion == OMPD_critical ||
3383 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003384 Recommend = ShouldBeInParallelRegion;
3385 } else if (CurrentRegion == OMPD_ordered) {
3386 // OpenMP [2.16, Nesting of Regions]
3387 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00003388 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003389 // An ordered region must be closely nested inside a loop region (or
3390 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003391 // OpenMP [2.8.1,simd Construct, Restrictions]
3392 // An ordered construct with the simd clause is the only OpenMP construct
3393 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003394 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003395 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003396 !(isOpenMPSimdDirective(ParentRegion) ||
3397 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003398 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003399 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
3400 // OpenMP [2.16, Nesting of Regions]
3401 // If specified, a teams construct must be contained within a target
3402 // construct.
3403 NestingProhibited = ParentRegion != OMPD_target;
Kelvin Li2b51f722016-07-26 04:32:50 +00003404 OrphanSeen = ParentRegion == OMPD_unknown;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003405 Recommend = ShouldBeInTargetRegion;
3406 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
3407 }
Kelvin Li02532872016-08-05 14:37:37 +00003408 if (!NestingProhibited && ParentRegion == OMPD_teams) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00003409 // OpenMP [2.16, Nesting of Regions]
3410 // distribute, parallel, parallel sections, parallel workshare, and the
3411 // parallel loop and parallel loop SIMD constructs are the only OpenMP
3412 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003413 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
3414 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00003415 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003416 }
David Majnemer9d168222016-08-05 17:44:54 +00003417 if (!NestingProhibited &&
Kelvin Li02532872016-08-05 14:37:37 +00003418 isOpenMPNestingDistributeDirective(CurrentRegion)) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003419 // OpenMP 4.5 [2.17 Nesting of Regions]
3420 // The region associated with the distribute construct must be strictly
3421 // nested inside a teams region
Kelvin Li02532872016-08-05 14:37:37 +00003422 NestingProhibited = ParentRegion != OMPD_teams;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003423 Recommend = ShouldBeInTeamsRegion;
3424 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003425 if (!NestingProhibited &&
3426 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
3427 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
3428 // OpenMP 4.5 [2.17 Nesting of Regions]
3429 // If a target, target update, target data, target enter data, or
3430 // target exit data construct is encountered during execution of a
3431 // target region, the behavior is unspecified.
3432 NestingProhibited = Stack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +00003433 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
3434 SourceLocation) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003435 if (isOpenMPTargetExecutionDirective(K)) {
3436 OffendingRegion = K;
3437 return true;
3438 } else
3439 return false;
3440 },
3441 false /* don't skip top directive */);
3442 CloseNesting = false;
3443 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003444 if (NestingProhibited) {
Kelvin Li2b51f722016-07-26 04:32:50 +00003445 if (OrphanSeen) {
3446 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
3447 << getOpenMPDirectiveName(CurrentRegion) << Recommend;
3448 } else {
3449 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
3450 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
3451 << Recommend << getOpenMPDirectiveName(CurrentRegion);
3452 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003453 return true;
3454 }
3455 }
3456 return false;
3457}
3458
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003459static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
3460 ArrayRef<OMPClause *> Clauses,
3461 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
3462 bool ErrorFound = false;
3463 unsigned NamedModifiersNumber = 0;
3464 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
3465 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00003466 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003467 for (const auto *C : Clauses) {
3468 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
3469 // At most one if clause without a directive-name-modifier can appear on
3470 // the directive.
3471 OpenMPDirectiveKind CurNM = IC->getNameModifier();
3472 if (FoundNameModifiers[CurNM]) {
3473 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
3474 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
3475 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
3476 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003477 } else if (CurNM != OMPD_unknown) {
3478 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003479 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003480 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003481 FoundNameModifiers[CurNM] = IC;
3482 if (CurNM == OMPD_unknown)
3483 continue;
3484 // Check if the specified name modifier is allowed for the current
3485 // directive.
3486 // At most one if clause with the particular directive-name-modifier can
3487 // appear on the directive.
3488 bool MatchFound = false;
3489 for (auto NM : AllowedNameModifiers) {
3490 if (CurNM == NM) {
3491 MatchFound = true;
3492 break;
3493 }
3494 }
3495 if (!MatchFound) {
3496 S.Diag(IC->getNameModifierLoc(),
3497 diag::err_omp_wrong_if_directive_name_modifier)
3498 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
3499 ErrorFound = true;
3500 }
3501 }
3502 }
3503 // If any if clause on the directive includes a directive-name-modifier then
3504 // all if clauses on the directive must include a directive-name-modifier.
3505 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3506 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
3507 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
3508 diag::err_omp_no_more_if_clause);
3509 } else {
3510 std::string Values;
3511 std::string Sep(", ");
3512 unsigned AllowedCnt = 0;
3513 unsigned TotalAllowedNum =
3514 AllowedNameModifiers.size() - NamedModifiersNumber;
3515 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3516 ++Cnt) {
3517 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3518 if (!FoundNameModifiers[NM]) {
3519 Values += "'";
3520 Values += getOpenMPDirectiveName(NM);
3521 Values += "'";
3522 if (AllowedCnt + 2 == TotalAllowedNum)
3523 Values += " or ";
3524 else if (AllowedCnt + 1 != TotalAllowedNum)
3525 Values += Sep;
3526 ++AllowedCnt;
3527 }
3528 }
3529 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
3530 diag::err_omp_unnamed_if_clause)
3531 << (TotalAllowedNum > 1) << Values;
3532 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00003533 for (auto Loc : NameModifierLoc) {
3534 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3535 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003536 ErrorFound = true;
3537 }
3538 return ErrorFound;
3539}
3540
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003541StmtResult Sema::ActOnOpenMPExecutableDirective(
3542 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3543 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3544 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003545 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003546 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
3547 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00003548 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003549
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003550 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003551 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003552 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00003553 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00003554 if (AStmt) {
3555 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3556
3557 // Check default data sharing attributes for referenced variables.
3558 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
3559 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
3560 if (DSAChecker.isErrorFound())
3561 return StmtError();
3562 // Generate list of implicitly defined firstprivate variables.
3563 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00003564
3565 if (!DSAChecker.getImplicitFirstprivate().empty()) {
3566 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
3567 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
3568 SourceLocation(), SourceLocation())) {
3569 ClausesWithImplicit.push_back(Implicit);
3570 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
3571 DSAChecker.getImplicitFirstprivate().size();
3572 } else
3573 ErrorFound = true;
3574 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003575 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003576
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003577 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003578 switch (Kind) {
3579 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00003580 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3581 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003582 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003583 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003584 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003585 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3586 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003587 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003588 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003589 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3590 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003591 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003592 case OMPD_for_simd:
3593 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3594 EndLoc, VarsWithInheritedDSA);
3595 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003596 case OMPD_sections:
3597 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3598 EndLoc);
3599 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003600 case OMPD_section:
3601 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003602 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003603 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3604 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003605 case OMPD_single:
3606 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3607 EndLoc);
3608 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003609 case OMPD_master:
3610 assert(ClausesWithImplicit.empty() &&
3611 "No clauses are allowed for 'omp master' directive");
3612 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3613 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003614 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003615 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3616 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003617 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003618 case OMPD_parallel_for:
3619 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3620 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003621 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003622 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003623 case OMPD_parallel_for_simd:
3624 Res = ActOnOpenMPParallelForSimdDirective(
3625 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003626 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003627 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003628 case OMPD_parallel_sections:
3629 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3630 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003631 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003632 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003633 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003634 Res =
3635 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003636 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003637 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003638 case OMPD_taskyield:
3639 assert(ClausesWithImplicit.empty() &&
3640 "No clauses are allowed for 'omp taskyield' directive");
3641 assert(AStmt == nullptr &&
3642 "No associated statement allowed for 'omp taskyield' directive");
3643 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3644 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003645 case OMPD_barrier:
3646 assert(ClausesWithImplicit.empty() &&
3647 "No clauses are allowed for 'omp barrier' directive");
3648 assert(AStmt == nullptr &&
3649 "No associated statement allowed for 'omp barrier' directive");
3650 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3651 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003652 case OMPD_taskwait:
3653 assert(ClausesWithImplicit.empty() &&
3654 "No clauses are allowed for 'omp taskwait' directive");
3655 assert(AStmt == nullptr &&
3656 "No associated statement allowed for 'omp taskwait' directive");
3657 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3658 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003659 case OMPD_taskgroup:
3660 assert(ClausesWithImplicit.empty() &&
3661 "No clauses are allowed for 'omp taskgroup' directive");
3662 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3663 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003664 case OMPD_flush:
3665 assert(AStmt == nullptr &&
3666 "No associated statement allowed for 'omp flush' directive");
3667 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3668 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003669 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003670 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3671 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003672 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003673 case OMPD_atomic:
3674 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3675 EndLoc);
3676 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003677 case OMPD_teams:
3678 Res =
3679 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3680 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003681 case OMPD_target:
3682 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3683 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003684 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003685 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003686 case OMPD_target_parallel:
3687 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3688 StartLoc, EndLoc);
3689 AllowedNameModifiers.push_back(OMPD_target);
3690 AllowedNameModifiers.push_back(OMPD_parallel);
3691 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003692 case OMPD_target_parallel_for:
3693 Res = ActOnOpenMPTargetParallelForDirective(
3694 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3695 AllowedNameModifiers.push_back(OMPD_target);
3696 AllowedNameModifiers.push_back(OMPD_parallel);
3697 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003698 case OMPD_cancellation_point:
3699 assert(ClausesWithImplicit.empty() &&
3700 "No clauses are allowed for 'omp cancellation point' directive");
3701 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3702 "cancellation point' directive");
3703 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3704 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003705 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003706 assert(AStmt == nullptr &&
3707 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003708 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3709 CancelRegion);
3710 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003711 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003712 case OMPD_target_data:
3713 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3714 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003715 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003716 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003717 case OMPD_target_enter_data:
3718 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3719 EndLoc);
3720 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3721 break;
Samuel Antao72590762016-01-19 20:04:50 +00003722 case OMPD_target_exit_data:
3723 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3724 EndLoc);
3725 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3726 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003727 case OMPD_taskloop:
3728 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3729 EndLoc, VarsWithInheritedDSA);
3730 AllowedNameModifiers.push_back(OMPD_taskloop);
3731 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003732 case OMPD_taskloop_simd:
3733 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3734 EndLoc, VarsWithInheritedDSA);
3735 AllowedNameModifiers.push_back(OMPD_taskloop);
3736 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003737 case OMPD_distribute:
3738 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3739 EndLoc, VarsWithInheritedDSA);
3740 break;
Samuel Antao686c70c2016-05-26 17:30:50 +00003741 case OMPD_target_update:
3742 assert(!AStmt && "Statement is not allowed for target update");
3743 Res =
3744 ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, EndLoc);
3745 AllowedNameModifiers.push_back(OMPD_target_update);
3746 break;
Carlo Bertolli9925f152016-06-27 14:55:37 +00003747 case OMPD_distribute_parallel_for:
3748 Res = ActOnOpenMPDistributeParallelForDirective(
3749 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3750 AllowedNameModifiers.push_back(OMPD_parallel);
3751 break;
Kelvin Li4a39add2016-07-05 05:00:15 +00003752 case OMPD_distribute_parallel_for_simd:
3753 Res = ActOnOpenMPDistributeParallelForSimdDirective(
3754 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3755 AllowedNameModifiers.push_back(OMPD_parallel);
3756 break;
Kelvin Li787f3fc2016-07-06 04:45:38 +00003757 case OMPD_distribute_simd:
3758 Res = ActOnOpenMPDistributeSimdDirective(
3759 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3760 break;
Kelvin Lia579b912016-07-14 02:54:56 +00003761 case OMPD_target_parallel_for_simd:
3762 Res = ActOnOpenMPTargetParallelForSimdDirective(
3763 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3764 AllowedNameModifiers.push_back(OMPD_target);
3765 AllowedNameModifiers.push_back(OMPD_parallel);
3766 break;
Kelvin Li986330c2016-07-20 22:57:10 +00003767 case OMPD_target_simd:
3768 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3769 EndLoc, VarsWithInheritedDSA);
3770 AllowedNameModifiers.push_back(OMPD_target);
3771 break;
Kelvin Li02532872016-08-05 14:37:37 +00003772 case OMPD_teams_distribute:
David Majnemer9d168222016-08-05 17:44:54 +00003773 Res = ActOnOpenMPTeamsDistributeDirective(
3774 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Kelvin Li02532872016-08-05 14:37:37 +00003775 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003776 case OMPD_declare_target:
3777 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003778 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003779 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003780 case OMPD_declare_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003781 llvm_unreachable("OpenMP Directive is not allowed");
3782 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003783 llvm_unreachable("Unknown OpenMP directive");
3784 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003785
Alexey Bataev4acb8592014-07-07 13:01:15 +00003786 for (auto P : VarsWithInheritedDSA) {
3787 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3788 << P.first << P.second->getSourceRange();
3789 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003790 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3791
3792 if (!AllowedNameModifiers.empty())
3793 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3794 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003795
Alexey Bataeved09d242014-05-28 05:53:51 +00003796 if (ErrorFound)
3797 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003798 return Res;
3799}
3800
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003801Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3802 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00003803 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00003804 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3805 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003806 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00003807 assert(Linears.size() == LinModifiers.size());
3808 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00003809 if (!DG || DG.get().isNull())
3810 return DeclGroupPtrTy();
3811
3812 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00003813 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003814 return DG;
3815 }
3816 auto *ADecl = DG.get().getSingleDecl();
3817 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3818 ADecl = FTD->getTemplatedDecl();
3819
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003820 auto *FD = dyn_cast<FunctionDecl>(ADecl);
3821 if (!FD) {
3822 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003823 return DeclGroupPtrTy();
3824 }
3825
Alexey Bataev2af33e32016-04-07 12:45:37 +00003826 // OpenMP [2.8.2, declare simd construct, Description]
3827 // The parameter of the simdlen clause must be a constant positive integer
3828 // expression.
3829 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003830 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00003831 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003832 // OpenMP [2.8.2, declare simd construct, Description]
3833 // The special this pointer can be used as if was one of the arguments to the
3834 // function in any of the linear, aligned, or uniform clauses.
3835 // The uniform clause declares one or more arguments to have an invariant
3836 // value for all concurrent invocations of the function in the execution of a
3837 // single SIMD loop.
Alexey Bataevecba70f2016-04-12 11:02:11 +00003838 llvm::DenseMap<Decl *, Expr *> UniformedArgs;
3839 Expr *UniformedLinearThis = nullptr;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003840 for (auto *E : Uniforms) {
3841 E = E->IgnoreParenImpCasts();
3842 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3843 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
3844 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3845 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00003846 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
3847 UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E));
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003848 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003849 }
3850 if (isa<CXXThisExpr>(E)) {
3851 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003852 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003853 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003854 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3855 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00003856 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00003857 // OpenMP [2.8.2, declare simd construct, Description]
3858 // The aligned clause declares that the object to which each list item points
3859 // is aligned to the number of bytes expressed in the optional parameter of
3860 // the aligned clause.
3861 // The special this pointer can be used as if was one of the arguments to the
3862 // function in any of the linear, aligned, or uniform clauses.
3863 // The type of list items appearing in the aligned clause must be array,
3864 // pointer, reference to array, or reference to pointer.
3865 llvm::DenseMap<Decl *, Expr *> AlignedArgs;
3866 Expr *AlignedThis = nullptr;
3867 for (auto *E : Aligneds) {
3868 E = E->IgnoreParenImpCasts();
3869 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3870 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3871 auto *CanonPVD = PVD->getCanonicalDecl();
3872 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3873 FD->getParamDecl(PVD->getFunctionScopeIndex())
3874 ->getCanonicalDecl() == CanonPVD) {
3875 // OpenMP [2.8.1, simd construct, Restrictions]
3876 // A list-item cannot appear in more than one aligned clause.
3877 if (AlignedArgs.count(CanonPVD) > 0) {
3878 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3879 << 1 << E->getSourceRange();
3880 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3881 diag::note_omp_explicit_dsa)
3882 << getOpenMPClauseName(OMPC_aligned);
3883 continue;
3884 }
3885 AlignedArgs[CanonPVD] = E;
3886 QualType QTy = PVD->getType()
3887 .getNonReferenceType()
3888 .getUnqualifiedType()
3889 .getCanonicalType();
3890 const Type *Ty = QTy.getTypePtrOrNull();
3891 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3892 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3893 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3894 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3895 }
3896 continue;
3897 }
3898 }
3899 if (isa<CXXThisExpr>(E)) {
3900 if (AlignedThis) {
3901 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3902 << 2 << E->getSourceRange();
3903 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3904 << getOpenMPClauseName(OMPC_aligned);
3905 }
3906 AlignedThis = E;
3907 continue;
3908 }
3909 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3910 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3911 }
3912 // The optional parameter of the aligned clause, alignment, must be a constant
3913 // positive integer expression. If no optional parameter is specified,
3914 // implementation-defined default alignments for SIMD instructions on the
3915 // target platforms are assumed.
3916 SmallVector<Expr *, 4> NewAligns;
3917 for (auto *E : Alignments) {
3918 ExprResult Align;
3919 if (E)
3920 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3921 NewAligns.push_back(Align.get());
3922 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00003923 // OpenMP [2.8.2, declare simd construct, Description]
3924 // The linear clause declares one or more list items to be private to a SIMD
3925 // lane and to have a linear relationship with respect to the iteration space
3926 // of a loop.
3927 // The special this pointer can be used as if was one of the arguments to the
3928 // function in any of the linear, aligned, or uniform clauses.
3929 // When a linear-step expression is specified in a linear clause it must be
3930 // either a constant integer expression or an integer-typed parameter that is
3931 // specified in a uniform clause on the directive.
3932 llvm::DenseMap<Decl *, Expr *> LinearArgs;
3933 const bool IsUniformedThis = UniformedLinearThis != nullptr;
3934 auto MI = LinModifiers.begin();
3935 for (auto *E : Linears) {
3936 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3937 ++MI;
3938 E = E->IgnoreParenImpCasts();
3939 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3940 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3941 auto *CanonPVD = PVD->getCanonicalDecl();
3942 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3943 FD->getParamDecl(PVD->getFunctionScopeIndex())
3944 ->getCanonicalDecl() == CanonPVD) {
3945 // OpenMP [2.15.3.7, linear Clause, Restrictions]
3946 // A list-item cannot appear in more than one linear clause.
3947 if (LinearArgs.count(CanonPVD) > 0) {
3948 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3949 << getOpenMPClauseName(OMPC_linear)
3950 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3951 Diag(LinearArgs[CanonPVD]->getExprLoc(),
3952 diag::note_omp_explicit_dsa)
3953 << getOpenMPClauseName(OMPC_linear);
3954 continue;
3955 }
3956 // Each argument can appear in at most one uniform or linear clause.
3957 if (UniformedArgs.count(CanonPVD) > 0) {
3958 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3959 << getOpenMPClauseName(OMPC_linear)
3960 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3961 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3962 diag::note_omp_explicit_dsa)
3963 << getOpenMPClauseName(OMPC_uniform);
3964 continue;
3965 }
3966 LinearArgs[CanonPVD] = E;
3967 if (E->isValueDependent() || E->isTypeDependent() ||
3968 E->isInstantiationDependent() ||
3969 E->containsUnexpandedParameterPack())
3970 continue;
3971 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3972 PVD->getOriginalType());
3973 continue;
3974 }
3975 }
3976 if (isa<CXXThisExpr>(E)) {
3977 if (UniformedLinearThis) {
3978 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3979 << getOpenMPClauseName(OMPC_linear)
3980 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3981 << E->getSourceRange();
3982 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3983 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3984 : OMPC_linear);
3985 continue;
3986 }
3987 UniformedLinearThis = E;
3988 if (E->isValueDependent() || E->isTypeDependent() ||
3989 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3990 continue;
3991 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3992 E->getType());
3993 continue;
3994 }
3995 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3996 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3997 }
3998 Expr *Step = nullptr;
3999 Expr *NewStep = nullptr;
4000 SmallVector<Expr *, 4> NewSteps;
4001 for (auto *E : Steps) {
4002 // Skip the same step expression, it was checked already.
4003 if (Step == E || !E) {
4004 NewSteps.push_back(E ? NewStep : nullptr);
4005 continue;
4006 }
4007 Step = E;
4008 if (auto *DRE = dyn_cast<DeclRefExpr>(Step))
4009 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
4010 auto *CanonPVD = PVD->getCanonicalDecl();
4011 if (UniformedArgs.count(CanonPVD) == 0) {
4012 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
4013 << Step->getSourceRange();
4014 } else if (E->isValueDependent() || E->isTypeDependent() ||
4015 E->isInstantiationDependent() ||
4016 E->containsUnexpandedParameterPack() ||
4017 CanonPVD->getType()->hasIntegerRepresentation())
4018 NewSteps.push_back(Step);
4019 else {
4020 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
4021 << Step->getSourceRange();
4022 }
4023 continue;
4024 }
4025 NewStep = Step;
4026 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
4027 !Step->isInstantiationDependent() &&
4028 !Step->containsUnexpandedParameterPack()) {
4029 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
4030 .get();
4031 if (NewStep)
4032 NewStep = VerifyIntegerConstantExpression(NewStep).get();
4033 }
4034 NewSteps.push_back(NewStep);
4035 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00004036 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
4037 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00004038 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00004039 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
4040 const_cast<Expr **>(Linears.data()), Linears.size(),
4041 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
4042 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00004043 ADecl->addAttr(NewAttr);
4044 return ConvertDeclToDeclGroup(ADecl);
4045}
4046
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004047StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
4048 Stmt *AStmt,
4049 SourceLocation StartLoc,
4050 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004051 if (!AStmt)
4052 return StmtError();
4053
Alexey Bataev9959db52014-05-06 10:08:46 +00004054 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4055 // 1.2.2 OpenMP Language Terminology
4056 // Structured block - An executable statement with a single entry at the
4057 // top and a single exit at the bottom.
4058 // The point of exit cannot be a branch out of the structured block.
4059 // longjmp() and throw() must not violate the entry/exit criteria.
4060 CS->getCapturedDecl()->setNothrow();
4061
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004062 getCurFunction()->setHasBranchProtectedScope();
4063
Alexey Bataev25e5b442015-09-15 12:52:43 +00004064 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4065 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004066}
4067
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004068namespace {
4069/// \brief Helper class for checking canonical form of the OpenMP loops and
4070/// extracting iteration space of each loop in the loop nest, that will be used
4071/// for IR generation.
4072class OpenMPIterationSpaceChecker {
4073 /// \brief Reference to Sema.
4074 Sema &SemaRef;
4075 /// \brief A location for diagnostics (when there is no some better location).
4076 SourceLocation DefaultLoc;
4077 /// \brief A location for diagnostics (when increment is not compatible).
4078 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004079 /// \brief A source location for referring to loop init later.
4080 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004081 /// \brief A source location for referring to condition later.
4082 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004083 /// \brief A source location for referring to increment later.
4084 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004085 /// \brief Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004086 ValueDecl *LCDecl = nullptr;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00004087 /// \brief Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004088 Expr *LCRef = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004089 /// \brief Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004090 Expr *LB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004091 /// \brief Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004092 Expr *UB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004093 /// \brief Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004094 Expr *Step = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004095 /// \brief This flag is true when condition is one of:
4096 /// Var < UB
4097 /// Var <= UB
4098 /// UB > Var
4099 /// UB >= Var
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004100 bool TestIsLessOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004101 /// \brief This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004102 bool TestIsStrictOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004103 /// \brief This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004104 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004105
4106public:
4107 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004108 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004109 /// \brief Check init-expr for canonical loop form and save loop counter
4110 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00004111 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004112 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
4113 /// for less/greater and for strict/non-strict comparison.
4114 bool CheckCond(Expr *S);
4115 /// \brief Check incr-expr for canonical loop form and return true if it
4116 /// does not conform, otherwise save loop step (#Step).
4117 bool CheckInc(Expr *S);
4118 /// \brief Return the loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004119 ValueDecl *GetLoopDecl() const { return LCDecl; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00004120 /// \brief Return the reference expression to loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004121 Expr *GetLoopDeclRefExpr() const { return LCRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004122 /// \brief Source range of the loop init.
4123 SourceRange GetInitSrcRange() const { return InitSrcRange; }
4124 /// \brief Source range of the loop condition.
4125 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
4126 /// \brief Source range of the loop increment.
4127 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
4128 /// \brief True if the step should be subtracted.
4129 bool ShouldSubtractStep() const { return SubtractStep; }
4130 /// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004131 Expr *
4132 BuildNumIterations(Scope *S, const bool LimitedType,
4133 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00004134 /// \brief Build the precondition expression for the loops.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004135 Expr *BuildPreCond(Scope *S, Expr *Cond,
4136 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004137 /// \brief Build reference expression to the counter be used for codegen.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004138 DeclRefExpr *BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures,
4139 DSAStackTy &DSA) const;
Alexey Bataeva8899172015-08-06 12:30:57 +00004140 /// \brief Build reference expression to the private counter be used for
4141 /// codegen.
4142 Expr *BuildPrivateCounterVar() const;
David Majnemer9d168222016-08-05 17:44:54 +00004143 /// \brief Build initialization of the counter be used for codegen.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004144 Expr *BuildCounterInit() const;
4145 /// \brief Build step of the counter be used for codegen.
4146 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004147 /// \brief Return true if any expression is dependent.
4148 bool Dependent() const;
4149
4150private:
4151 /// \brief Check the right-hand side of an assignment in the increment
4152 /// expression.
4153 bool CheckIncRHS(Expr *RHS);
4154 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004155 bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004156 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00004157 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00004158 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004159 /// \brief Helper to set loop increment.
4160 bool SetStep(Expr *NewStep, bool Subtract);
4161};
4162
4163bool OpenMPIterationSpaceChecker::Dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004164 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004165 assert(!LB && !UB && !Step);
4166 return false;
4167 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004168 return LCDecl->getType()->isDependentType() ||
4169 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
4170 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004171}
4172
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004173static Expr *getExprAsWritten(Expr *E) {
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004174 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
4175 E = ExprTemp->getSubExpr();
4176
4177 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
4178 E = MTE->GetTemporaryExpr();
4179
4180 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
4181 E = Binder->getSubExpr();
4182
4183 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
4184 E = ICE->getSubExprAsWritten();
4185 return E->IgnoreParens();
4186}
4187
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004188bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl,
4189 Expr *NewLCRefExpr,
4190 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004191 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004192 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00004193 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004194 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004195 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004196 LCDecl = getCanonicalDecl(NewLCDecl);
4197 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004198 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
4199 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004200 if ((Ctor->isCopyOrMoveConstructor() ||
4201 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4202 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004203 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004204 LB = NewLB;
4205 return false;
4206}
4207
4208bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00004209 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004210 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004211 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
4212 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004213 if (!NewUB)
4214 return true;
4215 UB = NewUB;
4216 TestIsLessOp = LessOp;
4217 TestIsStrictOp = StrictOp;
4218 ConditionSrcRange = SR;
4219 ConditionLoc = SL;
4220 return false;
4221}
4222
4223bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
4224 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004225 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004226 if (!NewStep)
4227 return true;
4228 if (!NewStep->isValueDependent()) {
4229 // Check that the step is integer expression.
4230 SourceLocation StepLoc = NewStep->getLocStart();
4231 ExprResult Val =
4232 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
4233 if (Val.isInvalid())
4234 return true;
4235 NewStep = Val.get();
4236
4237 // OpenMP [2.6, Canonical Loop Form, Restrictions]
4238 // If test-expr is of form var relational-op b and relational-op is < or
4239 // <= then incr-expr must cause var to increase on each iteration of the
4240 // loop. If test-expr is of form var relational-op b and relational-op is
4241 // > or >= then incr-expr must cause var to decrease on each iteration of
4242 // the loop.
4243 // If test-expr is of form b relational-op var and relational-op is < or
4244 // <= then incr-expr must cause var to decrease on each iteration of the
4245 // loop. If test-expr is of form b relational-op var and relational-op is
4246 // > or >= then incr-expr must cause var to increase on each iteration of
4247 // the loop.
4248 llvm::APSInt Result;
4249 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
4250 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
4251 bool IsConstNeg =
4252 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004253 bool IsConstPos =
4254 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004255 bool IsConstZero = IsConstant && !Result.getBoolValue();
4256 if (UB && (IsConstZero ||
4257 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00004258 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004259 SemaRef.Diag(NewStep->getExprLoc(),
4260 diag::err_omp_loop_incr_not_compatible)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004261 << LCDecl << TestIsLessOp << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004262 SemaRef.Diag(ConditionLoc,
4263 diag::note_omp_loop_cond_requres_compatible_incr)
4264 << TestIsLessOp << ConditionSrcRange;
4265 return true;
4266 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004267 if (TestIsLessOp == Subtract) {
David Majnemer9d168222016-08-05 17:44:54 +00004268 NewStep =
4269 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep)
4270 .get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004271 Subtract = !Subtract;
4272 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004273 }
4274
4275 Step = NewStep;
4276 SubtractStep = Subtract;
4277 return false;
4278}
4279
Alexey Bataev9c821032015-04-30 04:23:23 +00004280bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004281 // Check init-expr for canonical loop form and save loop counter
4282 // variable - #Var and its initialization value - #LB.
4283 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
4284 // var = lb
4285 // integer-type var = lb
4286 // random-access-iterator-type var = lb
4287 // pointer-type var = lb
4288 //
4289 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00004290 if (EmitDiags) {
4291 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
4292 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004293 return true;
4294 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004295 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4296 if (!ExprTemp->cleanupsHaveSideEffects())
4297 S = ExprTemp->getSubExpr();
4298
Alexander Musmana5f070a2014-10-01 06:03:56 +00004299 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004300 if (Expr *E = dyn_cast<Expr>(S))
4301 S = E->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00004302 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004303 if (BO->getOpcode() == BO_Assign) {
4304 auto *LHS = BO->getLHS()->IgnoreParens();
4305 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
4306 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4307 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4308 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4309 return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
4310 }
4311 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4312 if (ME->isArrow() &&
4313 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4314 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4315 }
4316 }
David Majnemer9d168222016-08-05 17:44:54 +00004317 } else if (auto *DS = dyn_cast<DeclStmt>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004318 if (DS->isSingleDecl()) {
David Majnemer9d168222016-08-05 17:44:54 +00004319 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00004320 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004321 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00004322 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004323 SemaRef.Diag(S->getLocStart(),
4324 diag::ext_omp_loop_not_canonical_init)
4325 << S->getSourceRange();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004326 return SetLCDeclAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004327 }
4328 }
4329 }
David Majnemer9d168222016-08-05 17:44:54 +00004330 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004331 if (CE->getOperator() == OO_Equal) {
4332 auto *LHS = CE->getArg(0);
David Majnemer9d168222016-08-05 17:44:54 +00004333 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004334 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4335 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4336 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4337 return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
4338 }
4339 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4340 if (ME->isArrow() &&
4341 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4342 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4343 }
4344 }
4345 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004346
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004347 if (Dependent() || SemaRef.CurContext->isDependentContext())
4348 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00004349 if (EmitDiags) {
4350 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
4351 << S->getSourceRange();
4352 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004353 return true;
4354}
4355
Alexey Bataev23b69422014-06-18 07:08:49 +00004356/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004357/// variable (which may be the loop variable) if possible.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004358static const ValueDecl *GetInitLCDecl(Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004359 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00004360 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004361 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004362 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
4363 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004364 if ((Ctor->isCopyOrMoveConstructor() ||
4365 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4366 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004367 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004368 if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
4369 if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
4370 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
4371 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4372 return getCanonicalDecl(ME->getMemberDecl());
4373 return getCanonicalDecl(VD);
4374 }
4375 }
4376 if (auto *ME = dyn_cast_or_null<MemberExpr>(E))
4377 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4378 return getCanonicalDecl(ME->getMemberDecl());
4379 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004380}
4381
4382bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
4383 // Check test-expr for canonical form, save upper-bound UB, flags for
4384 // less/greater and for strict/non-strict comparison.
4385 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4386 // var relational-op b
4387 // b relational-op var
4388 //
4389 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004390 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004391 return true;
4392 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004393 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004394 SourceLocation CondLoc = S->getLocStart();
David Majnemer9d168222016-08-05 17:44:54 +00004395 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004396 if (BO->isRelationalOp()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004397 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004398 return SetUB(BO->getRHS(),
4399 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
4400 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4401 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004402 if (GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004403 return SetUB(BO->getLHS(),
4404 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
4405 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4406 BO->getSourceRange(), BO->getOperatorLoc());
4407 }
David Majnemer9d168222016-08-05 17:44:54 +00004408 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004409 if (CE->getNumArgs() == 2) {
4410 auto Op = CE->getOperator();
4411 switch (Op) {
4412 case OO_Greater:
4413 case OO_GreaterEqual:
4414 case OO_Less:
4415 case OO_LessEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004416 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004417 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
4418 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4419 CE->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004420 if (GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004421 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
4422 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4423 CE->getOperatorLoc());
4424 break;
4425 default:
4426 break;
4427 }
4428 }
4429 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004430 if (Dependent() || SemaRef.CurContext->isDependentContext())
4431 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004432 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004433 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004434 return true;
4435}
4436
4437bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
4438 // RHS of canonical loop form increment can be:
4439 // var + incr
4440 // incr + var
4441 // var - incr
4442 //
4443 RHS = RHS->IgnoreParenImpCasts();
David Majnemer9d168222016-08-05 17:44:54 +00004444 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004445 if (BO->isAdditiveOp()) {
4446 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004447 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004448 return SetStep(BO->getRHS(), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004449 if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004450 return SetStep(BO->getLHS(), false);
4451 }
David Majnemer9d168222016-08-05 17:44:54 +00004452 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004453 bool IsAdd = CE->getOperator() == OO_Plus;
4454 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004455 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004456 return SetStep(CE->getArg(1), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004457 if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004458 return SetStep(CE->getArg(0), false);
4459 }
4460 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004461 if (Dependent() || SemaRef.CurContext->isDependentContext())
4462 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004463 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004464 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004465 return true;
4466}
4467
4468bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
4469 // Check incr-expr for canonical loop form and return true if it
4470 // does not conform.
4471 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4472 // ++var
4473 // var++
4474 // --var
4475 // var--
4476 // var += incr
4477 // var -= incr
4478 // var = var + incr
4479 // var = incr + var
4480 // var = var - incr
4481 //
4482 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004483 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004484 return true;
4485 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004486 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4487 if (!ExprTemp->cleanupsHaveSideEffects())
4488 S = ExprTemp->getSubExpr();
4489
Alexander Musmana5f070a2014-10-01 06:03:56 +00004490 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004491 S = S->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00004492 if (auto *UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004493 if (UO->isIncrementDecrementOp() &&
4494 GetInitLCDecl(UO->getSubExpr()) == LCDecl)
David Majnemer9d168222016-08-05 17:44:54 +00004495 return SetStep(SemaRef
4496 .ActOnIntegerConstant(UO->getLocStart(),
4497 (UO->isDecrementOp() ? -1 : 1))
4498 .get(),
4499 false);
4500 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004501 switch (BO->getOpcode()) {
4502 case BO_AddAssign:
4503 case BO_SubAssign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004504 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004505 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
4506 break;
4507 case BO_Assign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004508 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004509 return CheckIncRHS(BO->getRHS());
4510 break;
4511 default:
4512 break;
4513 }
David Majnemer9d168222016-08-05 17:44:54 +00004514 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004515 switch (CE->getOperator()) {
4516 case OO_PlusPlus:
4517 case OO_MinusMinus:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004518 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
David Majnemer9d168222016-08-05 17:44:54 +00004519 return SetStep(SemaRef
4520 .ActOnIntegerConstant(
4521 CE->getLocStart(),
4522 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
4523 .get(),
4524 false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004525 break;
4526 case OO_PlusEqual:
4527 case OO_MinusEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004528 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004529 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
4530 break;
4531 case OO_Equal:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004532 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004533 return CheckIncRHS(CE->getArg(1));
4534 break;
4535 default:
4536 break;
4537 }
4538 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004539 if (Dependent() || SemaRef.CurContext->isDependentContext())
4540 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004541 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004542 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004543 return true;
4544}
Alexander Musmana5f070a2014-10-01 06:03:56 +00004545
Alexey Bataev5a3af132016-03-29 08:58:54 +00004546static ExprResult
4547tryBuildCapture(Sema &SemaRef, Expr *Capture,
4548 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00004549 if (SemaRef.CurContext->isDependentContext())
4550 return ExprResult(Capture);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004551 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
4552 return SemaRef.PerformImplicitConversion(
4553 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
4554 /*AllowExplicit=*/true);
4555 auto I = Captures.find(Capture);
4556 if (I != Captures.end())
4557 return buildCapture(SemaRef, Capture, I->second);
4558 DeclRefExpr *Ref = nullptr;
4559 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4560 Captures[Capture] = Ref;
4561 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004562}
4563
Alexander Musmana5f070a2014-10-01 06:03:56 +00004564/// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004565Expr *OpenMPIterationSpaceChecker::BuildNumIterations(
4566 Scope *S, const bool LimitedType,
4567 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004568 ExprResult Diff;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004569 auto VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004570 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004571 SemaRef.getLangOpts().CPlusPlus) {
4572 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004573 auto *UBExpr = TestIsLessOp ? UB : LB;
4574 auto *LBExpr = TestIsLessOp ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00004575 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
4576 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004577 if (!Upper || !Lower)
4578 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004579
4580 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4581
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004582 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004583 // BuildBinOp already emitted error, this one is to point user to upper
4584 // and lower bound, and to tell what is passed to 'operator-'.
4585 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
4586 << Upper->getSourceRange() << Lower->getSourceRange();
4587 return nullptr;
4588 }
4589 }
4590
4591 if (!Diff.isUsable())
4592 return nullptr;
4593
4594 // Upper - Lower [- 1]
4595 if (TestIsStrictOp)
4596 Diff = SemaRef.BuildBinOp(
4597 S, DefaultLoc, BO_Sub, Diff.get(),
4598 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4599 if (!Diff.isUsable())
4600 return nullptr;
4601
4602 // Upper - Lower [- 1] + Step
Alexey Bataev5a3af132016-03-29 08:58:54 +00004603 auto NewStep = tryBuildCapture(SemaRef, Step, Captures);
4604 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004605 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004606 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004607 if (!Diff.isUsable())
4608 return nullptr;
4609
4610 // Parentheses (for dumping/debugging purposes only).
4611 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4612 if (!Diff.isUsable())
4613 return nullptr;
4614
4615 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004616 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004617 if (!Diff.isUsable())
4618 return nullptr;
4619
Alexander Musman174b3ca2014-10-06 11:16:29 +00004620 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004621 QualType Type = Diff.get()->getType();
4622 auto &C = SemaRef.Context;
4623 bool UseVarType = VarType->hasIntegerRepresentation() &&
4624 C.getTypeSize(Type) > C.getTypeSize(VarType);
4625 if (!Type->isIntegerType() || UseVarType) {
4626 unsigned NewSize =
4627 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4628 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4629 : Type->hasSignedIntegerRepresentation();
4630 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00004631 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4632 Diff = SemaRef.PerformImplicitConversion(
4633 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4634 if (!Diff.isUsable())
4635 return nullptr;
4636 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004637 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004638 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00004639 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4640 if (NewSize != C.getTypeSize(Type)) {
4641 if (NewSize < C.getTypeSize(Type)) {
4642 assert(NewSize == 64 && "incorrect loop var size");
4643 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4644 << InitSrcRange << ConditionSrcRange;
4645 }
4646 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004647 NewSize, Type->hasSignedIntegerRepresentation() ||
4648 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00004649 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4650 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4651 Sema::AA_Converting, true);
4652 if (!Diff.isUsable())
4653 return nullptr;
4654 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004655 }
4656 }
4657
Alexander Musmana5f070a2014-10-01 06:03:56 +00004658 return Diff.get();
4659}
4660
Alexey Bataev5a3af132016-03-29 08:58:54 +00004661Expr *OpenMPIterationSpaceChecker::BuildPreCond(
4662 Scope *S, Expr *Cond,
4663 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004664 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4665 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4666 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004667
Alexey Bataev5a3af132016-03-29 08:58:54 +00004668 auto NewLB = tryBuildCapture(SemaRef, LB, Captures);
4669 auto NewUB = tryBuildCapture(SemaRef, UB, Captures);
4670 if (!NewLB.isUsable() || !NewUB.isUsable())
4671 return nullptr;
4672
Alexey Bataev62dbb972015-04-22 11:59:37 +00004673 auto CondExpr = SemaRef.BuildBinOp(
4674 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
4675 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004676 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004677 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004678 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4679 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00004680 CondExpr = SemaRef.PerformImplicitConversion(
4681 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4682 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004683 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00004684 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4685 // Otherwise use original loop conditon and evaluate it in runtime.
4686 return CondExpr.isUsable() ? CondExpr.get() : Cond;
4687}
4688
Alexander Musmana5f070a2014-10-01 06:03:56 +00004689/// \brief Build reference expression to the counter be used for codegen.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004690DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar(
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004691 llvm::MapVector<Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004692 auto *VD = dyn_cast<VarDecl>(LCDecl);
4693 if (!VD) {
4694 VD = SemaRef.IsOpenMPCapturedDecl(LCDecl);
4695 auto *Ref = buildDeclRefExpr(
4696 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004697 DSAStackTy::DSAVarData Data = DSA.getTopDSA(LCDecl, /*FromParent=*/false);
4698 // If the loop control decl is explicitly marked as private, do not mark it
4699 // as captured again.
4700 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4701 Captures.insert(std::make_pair(LCRef, Ref));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004702 return Ref;
4703 }
4704 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00004705 DefaultLoc);
4706}
4707
4708Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004709 if (LCDecl && !LCDecl->isInvalidDecl()) {
4710 auto Type = LCDecl->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00004711 auto *PrivateVar =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004712 buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(),
4713 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00004714 if (PrivateVar->isInvalidDecl())
4715 return nullptr;
4716 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4717 }
4718 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004719}
4720
David Majnemer9d168222016-08-05 17:44:54 +00004721/// \brief Build instillation of the counter be used for codegen.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004722Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
4723
4724/// \brief Build step of the counter be used for codegen.
4725Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
4726
4727/// \brief Iteration space of a single for loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004728struct LoopIterationSpace final {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004729 /// \brief Condition of the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004730 Expr *PreCond = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004731 /// \brief This expression calculates the number of iterations in the loop.
4732 /// It is always possible to calculate it before starting the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004733 Expr *NumIterations = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004734 /// \brief The loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004735 Expr *CounterVar = nullptr;
Alexey Bataeva8899172015-08-06 12:30:57 +00004736 /// \brief Private loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004737 Expr *PrivateCounterVar = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004738 /// \brief This is initializer for the initial value of #CounterVar.
Alexey Bataev8b427062016-05-25 12:36:08 +00004739 Expr *CounterInit = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004740 /// \brief This is step for the #CounterVar used to generate its update:
4741 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
Alexey Bataev8b427062016-05-25 12:36:08 +00004742 Expr *CounterStep = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004743 /// \brief Should step be subtracted?
Alexey Bataev8b427062016-05-25 12:36:08 +00004744 bool Subtract = false;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004745 /// \brief Source range of the loop init.
4746 SourceRange InitSrcRange;
4747 /// \brief Source range of the loop condition.
4748 SourceRange CondSrcRange;
4749 /// \brief Source range of the loop increment.
4750 SourceRange IncSrcRange;
4751};
4752
Alexey Bataev23b69422014-06-18 07:08:49 +00004753} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004754
Alexey Bataev9c821032015-04-30 04:23:23 +00004755void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4756 assert(getLangOpts().OpenMP && "OpenMP is not active.");
4757 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004758 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4759 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00004760 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4761 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004762 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
4763 if (auto *D = ISC.GetLoopDecl()) {
4764 auto *VD = dyn_cast<VarDecl>(D);
4765 if (!VD) {
4766 if (auto *Private = IsOpenMPCapturedDecl(D))
4767 VD = Private;
4768 else {
4769 auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(),
4770 /*WithInit=*/false);
4771 VD = cast<VarDecl>(Ref->getDecl());
4772 }
4773 }
4774 DSAStack->addLoopControlVariable(D, VD);
4775 }
4776 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004777 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00004778 }
4779}
4780
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004781/// \brief Called on a for stmt to check and extract its iteration space
4782/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00004783static bool CheckOpenMPIterationSpace(
4784 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4785 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004786 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004787 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004788 LoopIterationSpace &ResultIterSpace,
4789 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004790 // OpenMP [2.6, Canonical Loop Form]
4791 // for (init-expr; test-expr; incr-expr) structured-block
David Majnemer9d168222016-08-05 17:44:54 +00004792 auto *For = dyn_cast_or_null<ForStmt>(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004793 if (!For) {
4794 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004795 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
4796 << getOpenMPDirectiveName(DKind) << NestedLoopCount
4797 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
4798 if (NestedLoopCount > 1) {
4799 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4800 SemaRef.Diag(DSA.getConstructLoc(),
4801 diag::note_omp_collapse_ordered_expr)
4802 << 2 << CollapseLoopCountExpr->getSourceRange()
4803 << OrderedLoopCountExpr->getSourceRange();
4804 else if (CollapseLoopCountExpr)
4805 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4806 diag::note_omp_collapse_ordered_expr)
4807 << 0 << CollapseLoopCountExpr->getSourceRange();
4808 else
4809 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4810 diag::note_omp_collapse_ordered_expr)
4811 << 1 << OrderedLoopCountExpr->getSourceRange();
4812 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004813 return true;
4814 }
4815 assert(For->getBody());
4816
4817 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4818
4819 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00004820 auto Init = For->getInit();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004821 if (ISC.CheckInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004822 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004823
4824 bool HasErrors = false;
4825
4826 // Check loop variable's type.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004827 if (auto *LCDecl = ISC.GetLoopDecl()) {
4828 auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004829
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004830 // OpenMP [2.6, Canonical Loop Form]
4831 // Var is one of the following:
4832 // A variable of signed or unsigned integer type.
4833 // For C++, a variable of a random access iterator type.
4834 // For C, a variable of a pointer type.
4835 auto VarType = LCDecl->getType().getNonReferenceType();
4836 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4837 !VarType->isPointerType() &&
4838 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
4839 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
4840 << SemaRef.getLangOpts().CPlusPlus;
4841 HasErrors = true;
4842 }
4843
4844 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4845 // a Construct
4846 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4847 // parallel for construct is (are) private.
4848 // The loop iteration variable in the associated for-loop of a simd
4849 // construct with just one associated for-loop is linear with a
4850 // constant-linear-step that is the increment of the associated for-loop.
4851 // Exclude loop var from the list of variables with implicitly defined data
4852 // sharing attributes.
4853 VarsWithImplicitDSA.erase(LCDecl);
4854
4855 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4856 // in a Construct, C/C++].
4857 // The loop iteration variable in the associated for-loop of a simd
4858 // construct with just one associated for-loop may be listed in a linear
4859 // clause with a constant-linear-step that is the increment of the
4860 // associated for-loop.
4861 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4862 // parallel for construct may be listed in a private or lastprivate clause.
4863 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4864 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4865 // declared in the loop and it is predetermined as a private.
4866 auto PredeterminedCKind =
4867 isOpenMPSimdDirective(DKind)
4868 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4869 : OMPC_private;
4870 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4871 DVar.CKind != PredeterminedCKind) ||
4872 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4873 isOpenMPDistributeDirective(DKind)) &&
4874 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4875 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4876 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4877 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
4878 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4879 << getOpenMPClauseName(PredeterminedCKind);
4880 if (DVar.RefExpr == nullptr)
4881 DVar.CKind = PredeterminedCKind;
4882 ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
4883 HasErrors = true;
4884 } else if (LoopDeclRefExpr != nullptr) {
4885 // Make the loop iteration variable private (for worksharing constructs),
4886 // linear (for simd directives with the only one associated loop) or
4887 // lastprivate (for simd directives with several collapsed or ordered
4888 // loops).
4889 if (DVar.CKind == OMPC_unknown)
Alexey Bataev7ace49d2016-05-17 08:55:33 +00004890 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
4891 [](OpenMPDirectiveKind) -> bool { return true; },
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004892 /*FromParent=*/false);
4893 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4894 }
4895
4896 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4897
4898 // Check test-expr.
4899 HasErrors |= ISC.CheckCond(For->getCond());
4900
4901 // Check incr-expr.
4902 HasErrors |= ISC.CheckInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004903 }
4904
Alexander Musmana5f070a2014-10-01 06:03:56 +00004905 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004906 return HasErrors;
4907
Alexander Musmana5f070a2014-10-01 06:03:56 +00004908 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004909 ResultIterSpace.PreCond =
4910 ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures);
Alexander Musman174b3ca2014-10-06 11:16:29 +00004911 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004912 DSA.getCurScope(),
4913 (isOpenMPWorksharingDirective(DKind) ||
4914 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4915 Captures);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004916 ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures, DSA);
Alexey Bataeva8899172015-08-06 12:30:57 +00004917 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004918 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
4919 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
4920 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
4921 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4922 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4923 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4924
Alexey Bataev62dbb972015-04-22 11:59:37 +00004925 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4926 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004927 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004928 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004929 ResultIterSpace.CounterInit == nullptr ||
4930 ResultIterSpace.CounterStep == nullptr);
4931
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004932 return HasErrors;
4933}
4934
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004935/// \brief Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004936static ExprResult
4937BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4938 ExprResult Start,
4939 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004940 // Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004941 auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
4942 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004943 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004944 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00004945 VarRef.get()->getType())) {
4946 NewStart = SemaRef.PerformImplicitConversion(
4947 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4948 /*AllowExplicit=*/true);
4949 if (!NewStart.isUsable())
4950 return ExprError();
4951 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004952
4953 auto Init =
4954 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4955 return Init;
4956}
4957
Alexander Musmana5f070a2014-10-01 06:03:56 +00004958/// \brief Build 'VarRef = Start + Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004959static ExprResult
4960BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc,
4961 ExprResult VarRef, ExprResult Start, ExprResult Iter,
4962 ExprResult Step, bool Subtract,
4963 llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004964 // Add parentheses (for debugging purposes only).
4965 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4966 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4967 !Step.isUsable())
4968 return ExprError();
4969
Alexey Bataev5a3af132016-03-29 08:58:54 +00004970 ExprResult NewStep = Step;
4971 if (Captures)
4972 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004973 if (NewStep.isInvalid())
4974 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004975 ExprResult Update =
4976 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004977 if (!Update.isUsable())
4978 return ExprError();
4979
Alexey Bataevc0214e02016-02-16 12:13:49 +00004980 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4981 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004982 ExprResult NewStart = Start;
4983 if (Captures)
4984 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004985 if (NewStart.isInvalid())
4986 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004987
Alexey Bataevc0214e02016-02-16 12:13:49 +00004988 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4989 ExprResult SavedUpdate = Update;
4990 ExprResult UpdateVal;
4991 if (VarRef.get()->getType()->isOverloadableType() ||
4992 NewStart.get()->getType()->isOverloadableType() ||
4993 Update.get()->getType()->isOverloadableType()) {
4994 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4995 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4996 Update =
4997 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4998 if (Update.isUsable()) {
4999 UpdateVal =
5000 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
5001 VarRef.get(), SavedUpdate.get());
5002 if (UpdateVal.isUsable()) {
5003 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
5004 UpdateVal.get());
5005 }
5006 }
5007 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
5008 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005009
Alexey Bataevc0214e02016-02-16 12:13:49 +00005010 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
5011 if (!Update.isUsable() || !UpdateVal.isUsable()) {
5012 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
5013 NewStart.get(), SavedUpdate.get());
5014 if (!Update.isUsable())
5015 return ExprError();
5016
Alexey Bataev11481f52016-02-17 10:29:05 +00005017 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
5018 VarRef.get()->getType())) {
5019 Update = SemaRef.PerformImplicitConversion(
5020 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
5021 if (!Update.isUsable())
5022 return ExprError();
5023 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00005024
5025 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
5026 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005027 return Update;
5028}
5029
5030/// \brief Convert integer expression \a E to make it have at least \a Bits
5031/// bits.
David Majnemer9d168222016-08-05 17:44:54 +00005032static ExprResult WidenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00005033 if (E == nullptr)
5034 return ExprError();
5035 auto &C = SemaRef.Context;
5036 QualType OldType = E->getType();
5037 unsigned HasBits = C.getTypeSize(OldType);
5038 if (HasBits >= Bits)
5039 return ExprResult(E);
5040 // OK to convert to signed, because new type has more bits than old.
5041 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
5042 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
5043 true);
5044}
5045
5046/// \brief Check if the given expression \a E is a constant integer that fits
5047/// into \a Bits bits.
5048static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
5049 if (E == nullptr)
5050 return false;
5051 llvm::APSInt Result;
5052 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
5053 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
5054 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005055}
5056
Alexey Bataev5a3af132016-03-29 08:58:54 +00005057/// Build preinits statement for the given declarations.
5058static Stmt *buildPreInits(ASTContext &Context,
5059 SmallVectorImpl<Decl *> &PreInits) {
5060 if (!PreInits.empty()) {
5061 return new (Context) DeclStmt(
5062 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
5063 SourceLocation(), SourceLocation());
5064 }
5065 return nullptr;
5066}
5067
5068/// Build preinits statement for the given declarations.
5069static Stmt *buildPreInits(ASTContext &Context,
5070 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
5071 if (!Captures.empty()) {
5072 SmallVector<Decl *, 16> PreInits;
5073 for (auto &Pair : Captures)
5074 PreInits.push_back(Pair.second->getDecl());
5075 return buildPreInits(Context, PreInits);
5076 }
5077 return nullptr;
5078}
5079
5080/// Build postupdate expression for the given list of postupdates expressions.
5081static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
5082 Expr *PostUpdate = nullptr;
5083 if (!PostUpdates.empty()) {
5084 for (auto *E : PostUpdates) {
5085 Expr *ConvE = S.BuildCStyleCastExpr(
5086 E->getExprLoc(),
5087 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
5088 E->getExprLoc(), E)
5089 .get();
5090 PostUpdate = PostUpdate
5091 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
5092 PostUpdate, ConvE)
5093 .get()
5094 : ConvE;
5095 }
5096 }
5097 return PostUpdate;
5098}
5099
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005100/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00005101/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
5102/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005103static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00005104CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
5105 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
5106 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005107 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00005108 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005109 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005110 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005111 // Found 'collapse' clause - calculate collapse number.
5112 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005113 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005114 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005115 }
5116 if (OrderedLoopCountExpr) {
5117 // Found 'ordered' clause - calculate collapse number.
5118 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005119 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
5120 if (Result.getLimitedValue() < NestedLoopCount) {
5121 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
5122 diag::err_omp_wrong_ordered_loop_count)
5123 << OrderedLoopCountExpr->getSourceRange();
5124 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
5125 diag::note_collapse_loop_count)
5126 << CollapseLoopCountExpr->getSourceRange();
5127 }
5128 NestedLoopCount = Result.getLimitedValue();
5129 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005130 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005131 // This is helper routine for loop directives (e.g., 'for', 'simd',
5132 // 'for simd', etc.).
Alexey Bataev5a3af132016-03-29 08:58:54 +00005133 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005134 SmallVector<LoopIterationSpace, 4> IterSpaces;
5135 IterSpaces.resize(NestedLoopCount);
5136 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005137 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005138 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00005139 NestedLoopCount, CollapseLoopCountExpr,
5140 OrderedLoopCountExpr, VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005141 IterSpaces[Cnt], Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00005142 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005143 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005144 // OpenMP [2.8.1, simd construct, Restrictions]
5145 // All loops associated with the construct must be perfectly nested; that
5146 // is, there must be no intervening code nor any OpenMP directive between
5147 // any two loops.
5148 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005149 }
5150
Alexander Musmana5f070a2014-10-01 06:03:56 +00005151 Built.clear(/* size */ NestedLoopCount);
5152
5153 if (SemaRef.CurContext->isDependentContext())
5154 return NestedLoopCount;
5155
5156 // An example of what is generated for the following code:
5157 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00005158 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00005159 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00005160 // for (k = 0; k < NK; ++k)
5161 // for (j = J0; j < NJ; j+=2) {
5162 // <loop body>
5163 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005164 //
5165 // We generate the code below.
5166 // Note: the loop body may be outlined in CodeGen.
5167 // Note: some counters may be C++ classes, operator- is used to find number of
5168 // iterations and operator+= to calculate counter value.
5169 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
5170 // or i64 is currently supported).
5171 //
5172 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
5173 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
5174 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
5175 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
5176 // // similar updates for vars in clauses (e.g. 'linear')
5177 // <loop body (using local i and j)>
5178 // }
5179 // i = NI; // assign final values of counters
5180 // j = NJ;
5181 //
5182
5183 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
5184 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00005185 // Precondition tests if there is at least one iteration (all conditions are
5186 // true).
5187 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005188 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005189 ExprResult LastIteration32 = WidenIterationCount(
David Majnemer9d168222016-08-05 17:44:54 +00005190 32 /* Bits */, SemaRef
5191 .PerformImplicitConversion(
5192 N0->IgnoreImpCasts(), N0->getType(),
5193 Sema::AA_Converting, /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005194 .get(),
5195 SemaRef);
5196 ExprResult LastIteration64 = WidenIterationCount(
David Majnemer9d168222016-08-05 17:44:54 +00005197 64 /* Bits */, SemaRef
5198 .PerformImplicitConversion(
5199 N0->IgnoreImpCasts(), N0->getType(),
5200 Sema::AA_Converting, /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005201 .get(),
5202 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005203
5204 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
5205 return NestedLoopCount;
5206
5207 auto &C = SemaRef.Context;
5208 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
5209
5210 Scope *CurScope = DSA.getCurScope();
5211 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00005212 if (PreCond.isUsable()) {
5213 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
5214 PreCond.get(), IterSpaces[Cnt].PreCond);
5215 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005216 auto N = IterSpaces[Cnt].NumIterations;
5217 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
5218 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005219 LastIteration32 = SemaRef.BuildBinOp(
5220 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
David Majnemer9d168222016-08-05 17:44:54 +00005221 SemaRef
5222 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5223 Sema::AA_Converting,
5224 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005225 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005226 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005227 LastIteration64 = SemaRef.BuildBinOp(
5228 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
David Majnemer9d168222016-08-05 17:44:54 +00005229 SemaRef
5230 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5231 Sema::AA_Converting,
5232 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005233 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005234 }
5235
5236 // Choose either the 32-bit or 64-bit version.
5237 ExprResult LastIteration = LastIteration64;
5238 if (LastIteration32.isUsable() &&
5239 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
5240 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
5241 FitsInto(
5242 32 /* Bits */,
5243 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
5244 LastIteration64.get(), SemaRef)))
5245 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00005246 QualType VType = LastIteration.get()->getType();
5247 QualType RealVType = VType;
5248 QualType StrideVType = VType;
5249 if (isOpenMPTaskLoopDirective(DKind)) {
5250 VType =
5251 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
5252 StrideVType =
5253 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
5254 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005255
5256 if (!LastIteration.isUsable())
5257 return 0;
5258
5259 // Save the number of iterations.
5260 ExprResult NumIterations = LastIteration;
5261 {
5262 LastIteration = SemaRef.BuildBinOp(
5263 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
5264 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5265 if (!LastIteration.isUsable())
5266 return 0;
5267 }
5268
5269 // Calculate the last iteration number beforehand instead of doing this on
5270 // each iteration. Do not do this if the number of iterations may be kfold-ed.
5271 llvm::APSInt Result;
5272 bool IsConstant =
5273 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
5274 ExprResult CalcLastIteration;
5275 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005276 ExprResult SaveRef =
5277 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005278 LastIteration = SaveRef;
5279
5280 // Prepare SaveRef + 1.
5281 NumIterations = SemaRef.BuildBinOp(
Alexey Bataev5a3af132016-03-29 08:58:54 +00005282 CurScope, SourceLocation(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00005283 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5284 if (!NumIterations.isUsable())
5285 return 0;
5286 }
5287
5288 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
5289
David Majnemer9d168222016-08-05 17:44:54 +00005290 // Build variables passed into runtime, necessary for worksharing directives.
Carlo Bertolli9925f152016-06-27 14:55:37 +00005291 ExprResult LB, UB, IL, ST, EUB, PrevLB, PrevUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005292 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5293 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005294 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005295 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
5296 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005297 SemaRef.AddInitializerToDecl(
5298 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5299 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5300
5301 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005302 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
5303 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005304 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
5305 /*DirectInit*/ false,
5306 /*TypeMayContainAuto*/ false);
5307
5308 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
5309 // This will be used to implement clause 'lastprivate'.
5310 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005311 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
5312 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005313 SemaRef.AddInitializerToDecl(
5314 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5315 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5316
5317 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00005318 VarDecl *STDecl =
5319 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
5320 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005321 SemaRef.AddInitializerToDecl(
5322 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
5323 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5324
5325 // Build expression: UB = min(UB, LastIteration)
David Majnemer9d168222016-08-05 17:44:54 +00005326 // It is necessary for CodeGen of directives with static scheduling.
Alexander Musmanc6388682014-12-15 07:07:06 +00005327 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
5328 UB.get(), LastIteration.get());
5329 ExprResult CondOp = SemaRef.ActOnConditionalOp(
5330 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
5331 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
5332 CondOp.get());
5333 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
Carlo Bertolli9925f152016-06-27 14:55:37 +00005334
5335 // If we have a combined directive that combines 'distribute', 'for' or
5336 // 'simd' we need to be able to access the bounds of the schedule of the
5337 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
5338 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
5339 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5340 auto *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
5341
5342 // We expect to have at least 2 more parameters than the 'parallel'
5343 // directive does - the lower and upper bounds of the previous schedule.
5344 assert(CD->getNumParams() >= 4 &&
5345 "Unexpected number of parameters in loop combined directive");
5346
5347 // Set the proper type for the bounds given what we learned from the
5348 // enclosed loops.
5349 auto *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
5350 auto *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
5351
5352 // Previous lower and upper bounds are obtained from the region
5353 // parameters.
5354 PrevLB =
5355 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
5356 PrevUB =
5357 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
5358 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005359 }
5360
5361 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005362 ExprResult IV;
5363 ExprResult Init;
5364 {
Alexey Bataev7292c292016-04-25 12:22:29 +00005365 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
5366 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
David Majnemer9d168222016-08-05 17:44:54 +00005367 Expr *RHS =
5368 (isOpenMPWorksharingDirective(DKind) ||
5369 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
5370 ? LB.get()
5371 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005372 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
5373 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005374 }
5375
Alexander Musmanc6388682014-12-15 07:07:06 +00005376 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005377 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00005378 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005379 (isOpenMPWorksharingDirective(DKind) ||
5380 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00005381 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
5382 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
5383 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005384
5385 // Loop increment (IV = IV + 1)
5386 SourceLocation IncLoc;
5387 ExprResult Inc =
5388 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
5389 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
5390 if (!Inc.isUsable())
5391 return 0;
5392 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00005393 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
5394 if (!Inc.isUsable())
5395 return 0;
5396
5397 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
5398 // Used for directives with static scheduling.
5399 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005400 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5401 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005402 // LB + ST
5403 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
5404 if (!NextLB.isUsable())
5405 return 0;
5406 // LB = LB + ST
5407 NextLB =
5408 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
5409 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
5410 if (!NextLB.isUsable())
5411 return 0;
5412 // UB + ST
5413 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
5414 if (!NextUB.isUsable())
5415 return 0;
5416 // UB = UB + ST
5417 NextUB =
5418 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
5419 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
5420 if (!NextUB.isUsable())
5421 return 0;
5422 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005423
5424 // Build updates and final values of the loop counters.
5425 bool HasErrors = false;
5426 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005427 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005428 Built.Updates.resize(NestedLoopCount);
5429 Built.Finals.resize(NestedLoopCount);
Alexey Bataev8b427062016-05-25 12:36:08 +00005430 SmallVector<Expr *, 4> LoopMultipliers;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005431 {
5432 ExprResult Div;
5433 // Go from inner nested loop to outer.
5434 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5435 LoopIterationSpace &IS = IterSpaces[Cnt];
5436 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
5437 // Build: Iter = (IV / Div) % IS.NumIters
5438 // where Div is product of previous iterations' IS.NumIters.
5439 ExprResult Iter;
5440 if (Div.isUsable()) {
5441 Iter =
5442 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
5443 } else {
5444 Iter = IV;
5445 assert((Cnt == (int)NestedLoopCount - 1) &&
5446 "unusable div expected on first iteration only");
5447 }
5448
5449 if (Cnt != 0 && Iter.isUsable())
5450 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
5451 IS.NumIterations);
5452 if (!Iter.isUsable()) {
5453 HasErrors = true;
5454 break;
5455 }
5456
Alexey Bataev39f915b82015-05-08 10:41:21 +00005457 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005458 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
5459 auto *CounterVar = buildDeclRefExpr(SemaRef, VD, IS.CounterVar->getType(),
5460 IS.CounterVar->getExprLoc(),
5461 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005462 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005463 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005464 if (!Init.isUsable()) {
5465 HasErrors = true;
5466 break;
5467 }
Alexey Bataev5a3af132016-03-29 08:58:54 +00005468 ExprResult Update = BuildCounterUpdate(
5469 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
5470 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005471 if (!Update.isUsable()) {
5472 HasErrors = true;
5473 break;
5474 }
5475
5476 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
5477 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00005478 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005479 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005480 if (!Final.isUsable()) {
5481 HasErrors = true;
5482 break;
5483 }
5484
5485 // Build Div for the next iteration: Div <- Div * IS.NumIters
5486 if (Cnt != 0) {
5487 if (Div.isUnset())
5488 Div = IS.NumIterations;
5489 else
5490 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
5491 IS.NumIterations);
5492
5493 // Add parentheses (for debugging purposes only).
5494 if (Div.isUsable())
Alexey Bataev8b427062016-05-25 12:36:08 +00005495 Div = tryBuildCapture(SemaRef, Div.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005496 if (!Div.isUsable()) {
5497 HasErrors = true;
5498 break;
5499 }
Alexey Bataev8b427062016-05-25 12:36:08 +00005500 LoopMultipliers.push_back(Div.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005501 }
5502 if (!Update.isUsable() || !Final.isUsable()) {
5503 HasErrors = true;
5504 break;
5505 }
5506 // Save results
5507 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00005508 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005509 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005510 Built.Updates[Cnt] = Update.get();
5511 Built.Finals[Cnt] = Final.get();
5512 }
5513 }
5514
5515 if (HasErrors)
5516 return 0;
5517
5518 // Save results
5519 Built.IterationVarRef = IV.get();
5520 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00005521 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00005522 Built.CalcLastIteration =
5523 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005524 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00005525 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005526 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005527 Built.Init = Init.get();
5528 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005529 Built.LB = LB.get();
5530 Built.UB = UB.get();
5531 Built.IL = IL.get();
5532 Built.ST = ST.get();
5533 Built.EUB = EUB.get();
5534 Built.NLB = NextLB.get();
5535 Built.NUB = NextUB.get();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005536 Built.PrevLB = PrevLB.get();
5537 Built.PrevUB = PrevUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005538
Alexey Bataev8b427062016-05-25 12:36:08 +00005539 Expr *CounterVal = SemaRef.DefaultLvalueConversion(IV.get()).get();
5540 // Fill data for doacross depend clauses.
5541 for (auto Pair : DSA.getDoacrossDependClauses()) {
5542 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
5543 Pair.first->setCounterValue(CounterVal);
5544 else {
5545 if (NestedLoopCount != Pair.second.size() ||
5546 NestedLoopCount != LoopMultipliers.size() + 1) {
5547 // Erroneous case - clause has some problems.
5548 Pair.first->setCounterValue(CounterVal);
5549 continue;
5550 }
5551 assert(Pair.first->getDependencyKind() == OMPC_DEPEND_sink);
5552 auto I = Pair.second.rbegin();
5553 auto IS = IterSpaces.rbegin();
5554 auto ILM = LoopMultipliers.rbegin();
5555 Expr *UpCounterVal = CounterVal;
5556 Expr *Multiplier = nullptr;
5557 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5558 if (I->first) {
5559 assert(IS->CounterStep);
5560 Expr *NormalizedOffset =
5561 SemaRef
5562 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Div,
5563 I->first, IS->CounterStep)
5564 .get();
5565 if (Multiplier) {
5566 NormalizedOffset =
5567 SemaRef
5568 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Mul,
5569 NormalizedOffset, Multiplier)
5570 .get();
5571 }
5572 assert(I->second == OO_Plus || I->second == OO_Minus);
5573 BinaryOperatorKind BOK = (I->second == OO_Plus) ? BO_Add : BO_Sub;
David Majnemer9d168222016-08-05 17:44:54 +00005574 UpCounterVal = SemaRef
5575 .BuildBinOp(CurScope, I->first->getExprLoc(), BOK,
5576 UpCounterVal, NormalizedOffset)
5577 .get();
Alexey Bataev8b427062016-05-25 12:36:08 +00005578 }
5579 Multiplier = *ILM;
5580 ++I;
5581 ++IS;
5582 ++ILM;
5583 }
5584 Pair.first->setCounterValue(UpCounterVal);
5585 }
5586 }
5587
Alexey Bataevabfc0692014-06-25 06:52:00 +00005588 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005589}
5590
Alexey Bataev10e775f2015-07-30 11:36:16 +00005591static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005592 auto CollapseClauses =
5593 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
5594 if (CollapseClauses.begin() != CollapseClauses.end())
5595 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005596 return nullptr;
5597}
5598
Alexey Bataev10e775f2015-07-30 11:36:16 +00005599static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005600 auto OrderedClauses =
5601 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
5602 if (OrderedClauses.begin() != OrderedClauses.end())
5603 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005604 return nullptr;
5605}
5606
Kelvin Lic5609492016-07-15 04:39:07 +00005607static bool checkSimdlenSafelenSpecified(Sema &S,
5608 const ArrayRef<OMPClause *> Clauses) {
5609 OMPSafelenClause *Safelen = nullptr;
5610 OMPSimdlenClause *Simdlen = nullptr;
5611
5612 for (auto *Clause : Clauses) {
5613 if (Clause->getClauseKind() == OMPC_safelen)
5614 Safelen = cast<OMPSafelenClause>(Clause);
5615 else if (Clause->getClauseKind() == OMPC_simdlen)
5616 Simdlen = cast<OMPSimdlenClause>(Clause);
5617 if (Safelen && Simdlen)
5618 break;
5619 }
5620
5621 if (Simdlen && Safelen) {
5622 llvm::APSInt SimdlenRes, SafelenRes;
5623 auto SimdlenLength = Simdlen->getSimdlen();
5624 auto SafelenLength = Safelen->getSafelen();
5625 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
5626 SimdlenLength->isInstantiationDependent() ||
5627 SimdlenLength->containsUnexpandedParameterPack())
5628 return false;
5629 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
5630 SafelenLength->isInstantiationDependent() ||
5631 SafelenLength->containsUnexpandedParameterPack())
5632 return false;
5633 SimdlenLength->EvaluateAsInt(SimdlenRes, S.Context);
5634 SafelenLength->EvaluateAsInt(SafelenRes, S.Context);
5635 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
5636 // If both simdlen and safelen clauses are specified, the value of the
5637 // simdlen parameter must be less than or equal to the value of the safelen
5638 // parameter.
5639 if (SimdlenRes > SafelenRes) {
5640 S.Diag(SimdlenLength->getExprLoc(),
5641 diag::err_omp_wrong_simdlen_safelen_values)
5642 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
5643 return true;
5644 }
Alexey Bataev66b15b52015-08-21 11:14:16 +00005645 }
5646 return false;
5647}
5648
Alexey Bataev4acb8592014-07-07 13:01:15 +00005649StmtResult Sema::ActOnOpenMPSimdDirective(
5650 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5651 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005652 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005653 if (!AStmt)
5654 return StmtError();
5655
5656 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005657 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005658 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5659 // define the nested loops number.
5660 unsigned NestedLoopCount = CheckOpenMPLoop(
5661 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5662 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005663 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005664 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005665
Alexander Musmana5f070a2014-10-01 06:03:56 +00005666 assert((CurContext->isDependentContext() || B.builtAll()) &&
5667 "omp simd loop exprs were not built");
5668
Alexander Musman3276a272015-03-21 10:12:56 +00005669 if (!CurContext->isDependentContext()) {
5670 // Finalize the clauses that need pre-built expressions for CodeGen.
5671 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005672 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexander Musman3276a272015-03-21 10:12:56 +00005673 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005674 B.NumIterations, *this, CurScope,
5675 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00005676 return StmtError();
5677 }
5678 }
5679
Kelvin Lic5609492016-07-15 04:39:07 +00005680 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005681 return StmtError();
5682
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005683 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005684 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5685 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005686}
5687
Alexey Bataev4acb8592014-07-07 13:01:15 +00005688StmtResult Sema::ActOnOpenMPForDirective(
5689 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5690 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005691 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005692 if (!AStmt)
5693 return StmtError();
5694
5695 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005696 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005697 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5698 // define the nested loops number.
5699 unsigned NestedLoopCount = CheckOpenMPLoop(
5700 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5701 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005702 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005703 return StmtError();
5704
Alexander Musmana5f070a2014-10-01 06:03:56 +00005705 assert((CurContext->isDependentContext() || B.builtAll()) &&
5706 "omp for loop exprs were not built");
5707
Alexey Bataev54acd402015-08-04 11:18:19 +00005708 if (!CurContext->isDependentContext()) {
5709 // Finalize the clauses that need pre-built expressions for CodeGen.
5710 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005711 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00005712 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005713 B.NumIterations, *this, CurScope,
5714 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005715 return StmtError();
5716 }
5717 }
5718
Alexey Bataevf29276e2014-06-18 04:14:57 +00005719 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005720 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005721 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005722}
5723
Alexander Musmanf82886e2014-09-18 05:12:34 +00005724StmtResult Sema::ActOnOpenMPForSimdDirective(
5725 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5726 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005727 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005728 if (!AStmt)
5729 return StmtError();
5730
5731 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005732 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005733 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5734 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00005735 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005736 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
5737 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5738 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005739 if (NestedLoopCount == 0)
5740 return StmtError();
5741
Alexander Musmanc6388682014-12-15 07:07:06 +00005742 assert((CurContext->isDependentContext() || B.builtAll()) &&
5743 "omp for simd loop exprs were not built");
5744
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005745 if (!CurContext->isDependentContext()) {
5746 // Finalize the clauses that need pre-built expressions for CodeGen.
5747 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005748 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005749 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005750 B.NumIterations, *this, CurScope,
5751 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005752 return StmtError();
5753 }
5754 }
5755
Kelvin Lic5609492016-07-15 04:39:07 +00005756 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005757 return StmtError();
5758
Alexander Musmanf82886e2014-09-18 05:12:34 +00005759 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005760 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5761 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005762}
5763
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005764StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5765 Stmt *AStmt,
5766 SourceLocation StartLoc,
5767 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005768 if (!AStmt)
5769 return StmtError();
5770
5771 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005772 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00005773 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005774 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00005775 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005776 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005777 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005778 return StmtError();
5779 // All associated statements must be '#pragma omp section' except for
5780 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005781 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005782 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5783 if (SectionStmt)
5784 Diag(SectionStmt->getLocStart(),
5785 diag::err_omp_sections_substmt_not_section);
5786 return StmtError();
5787 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005788 cast<OMPSectionDirective>(SectionStmt)
5789 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005790 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005791 } else {
5792 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
5793 return StmtError();
5794 }
5795
5796 getCurFunction()->setHasBranchProtectedScope();
5797
Alexey Bataev25e5b442015-09-15 12:52:43 +00005798 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5799 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005800}
5801
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005802StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5803 SourceLocation StartLoc,
5804 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005805 if (!AStmt)
5806 return StmtError();
5807
5808 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005809
5810 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00005811 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005812
Alexey Bataev25e5b442015-09-15 12:52:43 +00005813 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5814 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005815}
5816
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005817StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5818 Stmt *AStmt,
5819 SourceLocation StartLoc,
5820 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005821 if (!AStmt)
5822 return StmtError();
5823
5824 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00005825
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005826 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00005827
Alexey Bataev3255bf32015-01-19 05:20:46 +00005828 // OpenMP [2.7.3, single Construct, Restrictions]
5829 // The copyprivate clause must not be used with the nowait clause.
5830 OMPClause *Nowait = nullptr;
5831 OMPClause *Copyprivate = nullptr;
5832 for (auto *Clause : Clauses) {
5833 if (Clause->getClauseKind() == OMPC_nowait)
5834 Nowait = Clause;
5835 else if (Clause->getClauseKind() == OMPC_copyprivate)
5836 Copyprivate = Clause;
5837 if (Copyprivate && Nowait) {
5838 Diag(Copyprivate->getLocStart(),
5839 diag::err_omp_single_copyprivate_with_nowait);
5840 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
5841 return StmtError();
5842 }
5843 }
5844
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005845 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5846}
5847
Alexander Musman80c22892014-07-17 08:54:58 +00005848StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5849 SourceLocation StartLoc,
5850 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005851 if (!AStmt)
5852 return StmtError();
5853
5854 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00005855
5856 getCurFunction()->setHasBranchProtectedScope();
5857
5858 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5859}
5860
Alexey Bataev28c75412015-12-15 08:19:24 +00005861StmtResult Sema::ActOnOpenMPCriticalDirective(
5862 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5863 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005864 if (!AStmt)
5865 return StmtError();
5866
5867 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005868
Alexey Bataev28c75412015-12-15 08:19:24 +00005869 bool ErrorFound = false;
5870 llvm::APSInt Hint;
5871 SourceLocation HintLoc;
5872 bool DependentHint = false;
5873 for (auto *C : Clauses) {
5874 if (C->getClauseKind() == OMPC_hint) {
5875 if (!DirName.getName()) {
5876 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
5877 ErrorFound = true;
5878 }
5879 Expr *E = cast<OMPHintClause>(C)->getHint();
5880 if (E->isTypeDependent() || E->isValueDependent() ||
5881 E->isInstantiationDependent())
5882 DependentHint = true;
5883 else {
5884 Hint = E->EvaluateKnownConstInt(Context);
5885 HintLoc = C->getLocStart();
5886 }
5887 }
5888 }
5889 if (ErrorFound)
5890 return StmtError();
5891 auto Pair = DSAStack->getCriticalWithHint(DirName);
5892 if (Pair.first && DirName.getName() && !DependentHint) {
5893 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5894 Diag(StartLoc, diag::err_omp_critical_with_hint);
5895 if (HintLoc.isValid()) {
5896 Diag(HintLoc, diag::note_omp_critical_hint_here)
5897 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
5898 } else
5899 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
5900 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
5901 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
5902 << 1
5903 << C->getHint()->EvaluateKnownConstInt(Context).toString(
5904 /*Radix=*/10, /*Signed=*/false);
5905 } else
5906 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
5907 }
5908 }
5909
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005910 getCurFunction()->setHasBranchProtectedScope();
5911
Alexey Bataev28c75412015-12-15 08:19:24 +00005912 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5913 Clauses, AStmt);
5914 if (!Pair.first && DirName.getName() && !DependentHint)
5915 DSAStack->addCriticalWithHint(Dir, Hint);
5916 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005917}
5918
Alexey Bataev4acb8592014-07-07 13:01:15 +00005919StmtResult Sema::ActOnOpenMPParallelForDirective(
5920 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5921 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005922 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005923 if (!AStmt)
5924 return StmtError();
5925
Alexey Bataev4acb8592014-07-07 13:01:15 +00005926 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5927 // 1.2.2 OpenMP Language Terminology
5928 // Structured block - An executable statement with a single entry at the
5929 // top and a single exit at the bottom.
5930 // The point of exit cannot be a branch out of the structured block.
5931 // longjmp() and throw() must not violate the entry/exit criteria.
5932 CS->getCapturedDecl()->setNothrow();
5933
Alexander Musmanc6388682014-12-15 07:07:06 +00005934 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005935 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5936 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005937 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005938 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
5939 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5940 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005941 if (NestedLoopCount == 0)
5942 return StmtError();
5943
Alexander Musmana5f070a2014-10-01 06:03:56 +00005944 assert((CurContext->isDependentContext() || B.builtAll()) &&
5945 "omp parallel for loop exprs were not built");
5946
Alexey Bataev54acd402015-08-04 11:18:19 +00005947 if (!CurContext->isDependentContext()) {
5948 // Finalize the clauses that need pre-built expressions for CodeGen.
5949 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005950 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00005951 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005952 B.NumIterations, *this, CurScope,
5953 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005954 return StmtError();
5955 }
5956 }
5957
Alexey Bataev4acb8592014-07-07 13:01:15 +00005958 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005959 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005960 NestedLoopCount, Clauses, AStmt, B,
5961 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00005962}
5963
Alexander Musmane4e893b2014-09-23 09:33:00 +00005964StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5965 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5966 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005967 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005968 if (!AStmt)
5969 return StmtError();
5970
Alexander Musmane4e893b2014-09-23 09:33:00 +00005971 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5972 // 1.2.2 OpenMP Language Terminology
5973 // Structured block - An executable statement with a single entry at the
5974 // top and a single exit at the bottom.
5975 // The point of exit cannot be a branch out of the structured block.
5976 // longjmp() and throw() must not violate the entry/exit criteria.
5977 CS->getCapturedDecl()->setNothrow();
5978
Alexander Musmanc6388682014-12-15 07:07:06 +00005979 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005980 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5981 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00005982 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005983 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
5984 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5985 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005986 if (NestedLoopCount == 0)
5987 return StmtError();
5988
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005989 if (!CurContext->isDependentContext()) {
5990 // Finalize the clauses that need pre-built expressions for CodeGen.
5991 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005992 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005993 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005994 B.NumIterations, *this, CurScope,
5995 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005996 return StmtError();
5997 }
5998 }
5999
Kelvin Lic5609492016-07-15 04:39:07 +00006000 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00006001 return StmtError();
6002
Alexander Musmane4e893b2014-09-23 09:33:00 +00006003 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00006004 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00006005 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00006006}
6007
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006008StmtResult
6009Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
6010 Stmt *AStmt, SourceLocation StartLoc,
6011 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006012 if (!AStmt)
6013 return StmtError();
6014
6015 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006016 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00006017 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006018 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00006019 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006020 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00006021 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006022 return StmtError();
6023 // All associated statements must be '#pragma omp section' except for
6024 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00006025 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006026 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
6027 if (SectionStmt)
6028 Diag(SectionStmt->getLocStart(),
6029 diag::err_omp_parallel_sections_substmt_not_section);
6030 return StmtError();
6031 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006032 cast<OMPSectionDirective>(SectionStmt)
6033 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006034 }
6035 } else {
6036 Diag(AStmt->getLocStart(),
6037 diag::err_omp_parallel_sections_not_compound_stmt);
6038 return StmtError();
6039 }
6040
6041 getCurFunction()->setHasBranchProtectedScope();
6042
Alexey Bataev25e5b442015-09-15 12:52:43 +00006043 return OMPParallelSectionsDirective::Create(
6044 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006045}
6046
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006047StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
6048 Stmt *AStmt, SourceLocation StartLoc,
6049 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006050 if (!AStmt)
6051 return StmtError();
6052
David Majnemer9d168222016-08-05 17:44:54 +00006053 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006054 // 1.2.2 OpenMP Language Terminology
6055 // Structured block - An executable statement with a single entry at the
6056 // top and a single exit at the bottom.
6057 // The point of exit cannot be a branch out of the structured block.
6058 // longjmp() and throw() must not violate the entry/exit criteria.
6059 CS->getCapturedDecl()->setNothrow();
6060
6061 getCurFunction()->setHasBranchProtectedScope();
6062
Alexey Bataev25e5b442015-09-15 12:52:43 +00006063 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
6064 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006065}
6066
Alexey Bataev68446b72014-07-18 07:47:19 +00006067StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
6068 SourceLocation EndLoc) {
6069 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
6070}
6071
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00006072StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
6073 SourceLocation EndLoc) {
6074 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
6075}
6076
Alexey Bataev2df347a2014-07-18 10:17:07 +00006077StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
6078 SourceLocation EndLoc) {
6079 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
6080}
6081
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006082StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
6083 SourceLocation StartLoc,
6084 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006085 if (!AStmt)
6086 return StmtError();
6087
6088 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006089
6090 getCurFunction()->setHasBranchProtectedScope();
6091
6092 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
6093}
6094
Alexey Bataev6125da92014-07-21 11:26:11 +00006095StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
6096 SourceLocation StartLoc,
6097 SourceLocation EndLoc) {
6098 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
6099 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
6100}
6101
Alexey Bataev346265e2015-09-25 10:37:12 +00006102StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
6103 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006104 SourceLocation StartLoc,
6105 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00006106 OMPClause *DependFound = nullptr;
6107 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006108 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00006109 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00006110 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006111 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00006112 for (auto *C : Clauses) {
6113 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
6114 DependFound = C;
6115 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
6116 if (DependSourceClause) {
6117 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
6118 << getOpenMPDirectiveName(OMPD_ordered)
6119 << getOpenMPClauseName(OMPC_depend) << 2;
6120 ErrorFound = true;
6121 } else
6122 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006123 if (DependSinkClause) {
6124 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
6125 << 0;
6126 ErrorFound = true;
6127 }
6128 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
6129 if (DependSourceClause) {
6130 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
6131 << 1;
6132 ErrorFound = true;
6133 }
6134 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00006135 }
6136 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00006137 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006138 else if (C->getClauseKind() == OMPC_simd)
6139 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00006140 }
Alexey Bataeveb482352015-12-18 05:05:56 +00006141 if (!ErrorFound && !SC &&
6142 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006143 // OpenMP [2.8.1,simd Construct, Restrictions]
6144 // An ordered construct with the simd clause is the only OpenMP construct
6145 // that can appear in the simd region.
6146 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00006147 ErrorFound = true;
6148 } else if (DependFound && (TC || SC)) {
6149 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
6150 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
6151 ErrorFound = true;
6152 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
6153 Diag(DependFound->getLocStart(),
6154 diag::err_omp_ordered_directive_without_param);
6155 ErrorFound = true;
6156 } else if (TC || Clauses.empty()) {
6157 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
6158 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
6159 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
6160 << (TC != nullptr);
6161 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
6162 ErrorFound = true;
6163 }
6164 }
6165 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006166 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00006167
6168 if (AStmt) {
6169 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6170
6171 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006172 }
Alexey Bataev346265e2015-09-25 10:37:12 +00006173
6174 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006175}
6176
Alexey Bataev1d160b12015-03-13 12:27:31 +00006177namespace {
6178/// \brief Helper class for checking expression in 'omp atomic [update]'
6179/// construct.
6180class OpenMPAtomicUpdateChecker {
6181 /// \brief Error results for atomic update expressions.
6182 enum ExprAnalysisErrorCode {
6183 /// \brief A statement is not an expression statement.
6184 NotAnExpression,
6185 /// \brief Expression is not builtin binary or unary operation.
6186 NotABinaryOrUnaryExpression,
6187 /// \brief Unary operation is not post-/pre- increment/decrement operation.
6188 NotAnUnaryIncDecExpression,
6189 /// \brief An expression is not of scalar type.
6190 NotAScalarType,
6191 /// \brief A binary operation is not an assignment operation.
6192 NotAnAssignmentOp,
6193 /// \brief RHS part of the binary operation is not a binary expression.
6194 NotABinaryExpression,
6195 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
6196 /// expression.
6197 NotABinaryOperator,
6198 /// \brief RHS binary operation does not have reference to the updated LHS
6199 /// part.
6200 NotAnUpdateExpression,
6201 /// \brief No errors is found.
6202 NoError
6203 };
6204 /// \brief Reference to Sema.
6205 Sema &SemaRef;
6206 /// \brief A location for note diagnostics (when error is found).
6207 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006208 /// \brief 'x' lvalue part of the source atomic expression.
6209 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006210 /// \brief 'expr' rvalue part of the source atomic expression.
6211 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006212 /// \brief Helper expression of the form
6213 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6214 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6215 Expr *UpdateExpr;
6216 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
6217 /// important for non-associative operations.
6218 bool IsXLHSInRHSPart;
6219 BinaryOperatorKind Op;
6220 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006221 /// \brief true if the source expression is a postfix unary operation, false
6222 /// if it is a prefix unary operation.
6223 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006224
6225public:
6226 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00006227 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00006228 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00006229 /// \brief Check specified statement that it is suitable for 'atomic update'
6230 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00006231 /// expression. If DiagId and NoteId == 0, then only check is performed
6232 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006233 /// \param DiagId Diagnostic which should be emitted if error is found.
6234 /// \param NoteId Diagnostic note for the main error message.
6235 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00006236 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00006237 /// \brief Return the 'x' lvalue part of the source atomic expression.
6238 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00006239 /// \brief Return the 'expr' rvalue part of the source atomic expression.
6240 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00006241 /// \brief Return the update expression used in calculation of the updated
6242 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6243 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6244 Expr *getUpdateExpr() const { return UpdateExpr; }
6245 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
6246 /// false otherwise.
6247 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
6248
Alexey Bataevb78ca832015-04-01 03:33:17 +00006249 /// \brief true if the source expression is a postfix unary operation, false
6250 /// if it is a prefix unary operation.
6251 bool isPostfixUpdate() const { return IsPostfixUpdate; }
6252
Alexey Bataev1d160b12015-03-13 12:27:31 +00006253private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00006254 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
6255 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00006256};
6257} // namespace
6258
6259bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
6260 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
6261 ExprAnalysisErrorCode ErrorFound = NoError;
6262 SourceLocation ErrorLoc, NoteLoc;
6263 SourceRange ErrorRange, NoteRange;
6264 // Allowed constructs are:
6265 // x = x binop expr;
6266 // x = expr binop x;
6267 if (AtomicBinOp->getOpcode() == BO_Assign) {
6268 X = AtomicBinOp->getLHS();
6269 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
6270 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
6271 if (AtomicInnerBinOp->isMultiplicativeOp() ||
6272 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
6273 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006274 Op = AtomicInnerBinOp->getOpcode();
6275 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006276 auto *LHS = AtomicInnerBinOp->getLHS();
6277 auto *RHS = AtomicInnerBinOp->getRHS();
6278 llvm::FoldingSetNodeID XId, LHSId, RHSId;
6279 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
6280 /*Canonical=*/true);
6281 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
6282 /*Canonical=*/true);
6283 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
6284 /*Canonical=*/true);
6285 if (XId == LHSId) {
6286 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006287 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006288 } else if (XId == RHSId) {
6289 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006290 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006291 } else {
6292 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6293 ErrorRange = AtomicInnerBinOp->getSourceRange();
6294 NoteLoc = X->getExprLoc();
6295 NoteRange = X->getSourceRange();
6296 ErrorFound = NotAnUpdateExpression;
6297 }
6298 } else {
6299 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6300 ErrorRange = AtomicInnerBinOp->getSourceRange();
6301 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
6302 NoteRange = SourceRange(NoteLoc, NoteLoc);
6303 ErrorFound = NotABinaryOperator;
6304 }
6305 } else {
6306 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
6307 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
6308 ErrorFound = NotABinaryExpression;
6309 }
6310 } else {
6311 ErrorLoc = AtomicBinOp->getExprLoc();
6312 ErrorRange = AtomicBinOp->getSourceRange();
6313 NoteLoc = AtomicBinOp->getOperatorLoc();
6314 NoteRange = SourceRange(NoteLoc, NoteLoc);
6315 ErrorFound = NotAnAssignmentOp;
6316 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006317 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006318 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6319 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6320 return true;
6321 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006322 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006323 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006324}
6325
6326bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
6327 unsigned NoteId) {
6328 ExprAnalysisErrorCode ErrorFound = NoError;
6329 SourceLocation ErrorLoc, NoteLoc;
6330 SourceRange ErrorRange, NoteRange;
6331 // Allowed constructs are:
6332 // x++;
6333 // x--;
6334 // ++x;
6335 // --x;
6336 // x binop= expr;
6337 // x = x binop expr;
6338 // x = expr binop x;
6339 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
6340 AtomicBody = AtomicBody->IgnoreParenImpCasts();
6341 if (AtomicBody->getType()->isScalarType() ||
6342 AtomicBody->isInstantiationDependent()) {
6343 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
6344 AtomicBody->IgnoreParenImpCasts())) {
6345 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00006346 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006347 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00006348 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006349 E = AtomicCompAssignOp->getRHS();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006350 X = AtomicCompAssignOp->getLHS()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006351 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006352 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
6353 AtomicBody->IgnoreParenImpCasts())) {
6354 // Check for Binary Operation
David Majnemer9d168222016-08-05 17:44:54 +00006355 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
Alexey Bataevb4505a72015-03-30 05:20:59 +00006356 return true;
David Majnemer9d168222016-08-05 17:44:54 +00006357 } else if (auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
6358 AtomicBody->IgnoreParenImpCasts())) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006359 // Check for Unary Operation
6360 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006361 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006362 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
6363 OpLoc = AtomicUnaryOp->getOperatorLoc();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006364 X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006365 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
6366 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006367 } else {
6368 ErrorFound = NotAnUnaryIncDecExpression;
6369 ErrorLoc = AtomicUnaryOp->getExprLoc();
6370 ErrorRange = AtomicUnaryOp->getSourceRange();
6371 NoteLoc = AtomicUnaryOp->getOperatorLoc();
6372 NoteRange = SourceRange(NoteLoc, NoteLoc);
6373 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006374 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006375 ErrorFound = NotABinaryOrUnaryExpression;
6376 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
6377 NoteRange = ErrorRange = AtomicBody->getSourceRange();
6378 }
6379 } else {
6380 ErrorFound = NotAScalarType;
6381 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
6382 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6383 }
6384 } else {
6385 ErrorFound = NotAnExpression;
6386 NoteLoc = ErrorLoc = S->getLocStart();
6387 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6388 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006389 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006390 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6391 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6392 return true;
6393 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006394 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006395 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006396 // Build an update expression of form 'OpaqueValueExpr(x) binop
6397 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
6398 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
6399 auto *OVEX = new (SemaRef.getASTContext())
6400 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
6401 auto *OVEExpr = new (SemaRef.getASTContext())
6402 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
6403 auto Update =
6404 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
6405 IsXLHSInRHSPart ? OVEExpr : OVEX);
6406 if (Update.isInvalid())
6407 return true;
6408 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
6409 Sema::AA_Casting);
6410 if (Update.isInvalid())
6411 return true;
6412 UpdateExpr = Update.get();
6413 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00006414 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006415}
6416
Alexey Bataev0162e452014-07-22 10:10:35 +00006417StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
6418 Stmt *AStmt,
6419 SourceLocation StartLoc,
6420 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006421 if (!AStmt)
6422 return StmtError();
6423
David Majnemer9d168222016-08-05 17:44:54 +00006424 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00006425 // 1.2.2 OpenMP Language Terminology
6426 // Structured block - An executable statement with a single entry at the
6427 // top and a single exit at the bottom.
6428 // The point of exit cannot be a branch out of the structured block.
6429 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00006430 OpenMPClauseKind AtomicKind = OMPC_unknown;
6431 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006432 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00006433 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00006434 C->getClauseKind() == OMPC_update ||
6435 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00006436 if (AtomicKind != OMPC_unknown) {
6437 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
6438 << SourceRange(C->getLocStart(), C->getLocEnd());
6439 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
6440 << getOpenMPClauseName(AtomicKind);
6441 } else {
6442 AtomicKind = C->getClauseKind();
6443 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006444 }
6445 }
6446 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006447
Alexey Bataev459dec02014-07-24 06:46:57 +00006448 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00006449 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
6450 Body = EWC->getSubExpr();
6451
Alexey Bataev62cec442014-11-18 10:14:22 +00006452 Expr *X = nullptr;
6453 Expr *V = nullptr;
6454 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006455 Expr *UE = nullptr;
6456 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006457 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00006458 // OpenMP [2.12.6, atomic Construct]
6459 // In the next expressions:
6460 // * x and v (as applicable) are both l-value expressions with scalar type.
6461 // * During the execution of an atomic region, multiple syntactic
6462 // occurrences of x must designate the same storage location.
6463 // * Neither of v and expr (as applicable) may access the storage location
6464 // designated by x.
6465 // * Neither of x and expr (as applicable) may access the storage location
6466 // designated by v.
6467 // * expr is an expression with scalar type.
6468 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
6469 // * binop, binop=, ++, and -- are not overloaded operators.
6470 // * The expression x binop expr must be numerically equivalent to x binop
6471 // (expr). This requirement is satisfied if the operators in expr have
6472 // precedence greater than binop, or by using parentheses around expr or
6473 // subexpressions of expr.
6474 // * The expression expr binop x must be numerically equivalent to (expr)
6475 // binop x. This requirement is satisfied if the operators in expr have
6476 // precedence equal to or greater than binop, or by using parentheses around
6477 // expr or subexpressions of expr.
6478 // * For forms that allow multiple occurrences of x, the number of times
6479 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00006480 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006481 enum {
6482 NotAnExpression,
6483 NotAnAssignmentOp,
6484 NotAScalarType,
6485 NotAnLValue,
6486 NoError
6487 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00006488 SourceLocation ErrorLoc, NoteLoc;
6489 SourceRange ErrorRange, NoteRange;
6490 // If clause is read:
6491 // v = x;
David Majnemer9d168222016-08-05 17:44:54 +00006492 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6493 auto *AtomicBinOp =
Alexey Bataev62cec442014-11-18 10:14:22 +00006494 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6495 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6496 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6497 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
6498 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6499 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
6500 if (!X->isLValue() || !V->isLValue()) {
6501 auto NotLValueExpr = X->isLValue() ? V : X;
6502 ErrorFound = NotAnLValue;
6503 ErrorLoc = AtomicBinOp->getExprLoc();
6504 ErrorRange = AtomicBinOp->getSourceRange();
6505 NoteLoc = NotLValueExpr->getExprLoc();
6506 NoteRange = NotLValueExpr->getSourceRange();
6507 }
6508 } else if (!X->isInstantiationDependent() ||
6509 !V->isInstantiationDependent()) {
6510 auto NotScalarExpr =
6511 (X->isInstantiationDependent() || X->getType()->isScalarType())
6512 ? V
6513 : X;
6514 ErrorFound = NotAScalarType;
6515 ErrorLoc = AtomicBinOp->getExprLoc();
6516 ErrorRange = AtomicBinOp->getSourceRange();
6517 NoteLoc = NotScalarExpr->getExprLoc();
6518 NoteRange = NotScalarExpr->getSourceRange();
6519 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006520 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00006521 ErrorFound = NotAnAssignmentOp;
6522 ErrorLoc = AtomicBody->getExprLoc();
6523 ErrorRange = AtomicBody->getSourceRange();
6524 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6525 : AtomicBody->getExprLoc();
6526 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6527 : AtomicBody->getSourceRange();
6528 }
6529 } else {
6530 ErrorFound = NotAnExpression;
6531 NoteLoc = ErrorLoc = Body->getLocStart();
6532 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006533 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006534 if (ErrorFound != NoError) {
6535 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
6536 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006537 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6538 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00006539 return StmtError();
6540 } else if (CurContext->isDependentContext())
6541 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00006542 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006543 enum {
6544 NotAnExpression,
6545 NotAnAssignmentOp,
6546 NotAScalarType,
6547 NotAnLValue,
6548 NoError
6549 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006550 SourceLocation ErrorLoc, NoteLoc;
6551 SourceRange ErrorRange, NoteRange;
6552 // If clause is write:
6553 // x = expr;
David Majnemer9d168222016-08-05 17:44:54 +00006554 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6555 auto *AtomicBinOp =
Alexey Bataevf33eba62014-11-28 07:21:40 +00006556 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6557 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00006558 X = AtomicBinOp->getLHS();
6559 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006560 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6561 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
6562 if (!X->isLValue()) {
6563 ErrorFound = NotAnLValue;
6564 ErrorLoc = AtomicBinOp->getExprLoc();
6565 ErrorRange = AtomicBinOp->getSourceRange();
6566 NoteLoc = X->getExprLoc();
6567 NoteRange = X->getSourceRange();
6568 }
6569 } else if (!X->isInstantiationDependent() ||
6570 !E->isInstantiationDependent()) {
6571 auto NotScalarExpr =
6572 (X->isInstantiationDependent() || X->getType()->isScalarType())
6573 ? E
6574 : X;
6575 ErrorFound = NotAScalarType;
6576 ErrorLoc = AtomicBinOp->getExprLoc();
6577 ErrorRange = AtomicBinOp->getSourceRange();
6578 NoteLoc = NotScalarExpr->getExprLoc();
6579 NoteRange = NotScalarExpr->getSourceRange();
6580 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006581 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00006582 ErrorFound = NotAnAssignmentOp;
6583 ErrorLoc = AtomicBody->getExprLoc();
6584 ErrorRange = AtomicBody->getSourceRange();
6585 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6586 : AtomicBody->getExprLoc();
6587 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6588 : AtomicBody->getSourceRange();
6589 }
6590 } else {
6591 ErrorFound = NotAnExpression;
6592 NoteLoc = ErrorLoc = Body->getLocStart();
6593 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006594 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00006595 if (ErrorFound != NoError) {
6596 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
6597 << ErrorRange;
6598 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6599 << NoteRange;
6600 return StmtError();
6601 } else if (CurContext->isDependentContext())
6602 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006603 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006604 // If clause is update:
6605 // x++;
6606 // x--;
6607 // ++x;
6608 // --x;
6609 // x binop= expr;
6610 // x = x binop expr;
6611 // x = expr binop x;
6612 OpenMPAtomicUpdateChecker Checker(*this);
6613 if (Checker.checkStatement(
6614 Body, (AtomicKind == OMPC_update)
6615 ? diag::err_omp_atomic_update_not_expression_statement
6616 : diag::err_omp_atomic_not_expression_statement,
6617 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00006618 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006619 if (!CurContext->isDependentContext()) {
6620 E = Checker.getExpr();
6621 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006622 UE = Checker.getUpdateExpr();
6623 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00006624 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006625 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006626 enum {
6627 NotAnAssignmentOp,
6628 NotACompoundStatement,
6629 NotTwoSubstatements,
6630 NotASpecificExpression,
6631 NoError
6632 } ErrorFound = NoError;
6633 SourceLocation ErrorLoc, NoteLoc;
6634 SourceRange ErrorRange, NoteRange;
6635 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6636 // If clause is a capture:
6637 // v = x++;
6638 // v = x--;
6639 // v = ++x;
6640 // v = --x;
6641 // v = x binop= expr;
6642 // v = x = x binop expr;
6643 // v = x = expr binop x;
6644 auto *AtomicBinOp =
6645 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6646 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6647 V = AtomicBinOp->getLHS();
6648 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6649 OpenMPAtomicUpdateChecker Checker(*this);
6650 if (Checker.checkStatement(
6651 Body, diag::err_omp_atomic_capture_not_expression_statement,
6652 diag::note_omp_atomic_update))
6653 return StmtError();
6654 E = Checker.getExpr();
6655 X = Checker.getX();
6656 UE = Checker.getUpdateExpr();
6657 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6658 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00006659 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006660 ErrorLoc = AtomicBody->getExprLoc();
6661 ErrorRange = AtomicBody->getSourceRange();
6662 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6663 : AtomicBody->getExprLoc();
6664 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6665 : AtomicBody->getSourceRange();
6666 ErrorFound = NotAnAssignmentOp;
6667 }
6668 if (ErrorFound != NoError) {
6669 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6670 << ErrorRange;
6671 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6672 return StmtError();
6673 } else if (CurContext->isDependentContext()) {
6674 UE = V = E = X = nullptr;
6675 }
6676 } else {
6677 // If clause is a capture:
6678 // { v = x; x = expr; }
6679 // { v = x; x++; }
6680 // { v = x; x--; }
6681 // { v = x; ++x; }
6682 // { v = x; --x; }
6683 // { v = x; x binop= expr; }
6684 // { v = x; x = x binop expr; }
6685 // { v = x; x = expr binop x; }
6686 // { x++; v = x; }
6687 // { x--; v = x; }
6688 // { ++x; v = x; }
6689 // { --x; v = x; }
6690 // { x binop= expr; v = x; }
6691 // { x = x binop expr; v = x; }
6692 // { x = expr binop x; v = x; }
6693 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6694 // Check that this is { expr1; expr2; }
6695 if (CS->size() == 2) {
6696 auto *First = CS->body_front();
6697 auto *Second = CS->body_back();
6698 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6699 First = EWC->getSubExpr()->IgnoreParenImpCasts();
6700 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6701 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6702 // Need to find what subexpression is 'v' and what is 'x'.
6703 OpenMPAtomicUpdateChecker Checker(*this);
6704 bool IsUpdateExprFound = !Checker.checkStatement(Second);
6705 BinaryOperator *BinOp = nullptr;
6706 if (IsUpdateExprFound) {
6707 BinOp = dyn_cast<BinaryOperator>(First);
6708 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6709 }
6710 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6711 // { v = x; x++; }
6712 // { v = x; x--; }
6713 // { v = x; ++x; }
6714 // { v = x; --x; }
6715 // { v = x; x binop= expr; }
6716 // { v = x; x = x binop expr; }
6717 // { v = x; x = expr binop x; }
6718 // Check that the first expression has form v = x.
6719 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6720 llvm::FoldingSetNodeID XId, PossibleXId;
6721 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6722 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6723 IsUpdateExprFound = XId == PossibleXId;
6724 if (IsUpdateExprFound) {
6725 V = BinOp->getLHS();
6726 X = Checker.getX();
6727 E = Checker.getExpr();
6728 UE = Checker.getUpdateExpr();
6729 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006730 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006731 }
6732 }
6733 if (!IsUpdateExprFound) {
6734 IsUpdateExprFound = !Checker.checkStatement(First);
6735 BinOp = nullptr;
6736 if (IsUpdateExprFound) {
6737 BinOp = dyn_cast<BinaryOperator>(Second);
6738 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6739 }
6740 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6741 // { x++; v = x; }
6742 // { x--; v = x; }
6743 // { ++x; v = x; }
6744 // { --x; v = x; }
6745 // { x binop= expr; v = x; }
6746 // { x = x binop expr; v = x; }
6747 // { x = expr binop x; v = x; }
6748 // Check that the second expression has form v = x.
6749 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6750 llvm::FoldingSetNodeID XId, PossibleXId;
6751 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6752 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6753 IsUpdateExprFound = XId == PossibleXId;
6754 if (IsUpdateExprFound) {
6755 V = BinOp->getLHS();
6756 X = Checker.getX();
6757 E = Checker.getExpr();
6758 UE = Checker.getUpdateExpr();
6759 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006760 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006761 }
6762 }
6763 }
6764 if (!IsUpdateExprFound) {
6765 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00006766 auto *FirstExpr = dyn_cast<Expr>(First);
6767 auto *SecondExpr = dyn_cast<Expr>(Second);
6768 if (!FirstExpr || !SecondExpr ||
6769 !(FirstExpr->isInstantiationDependent() ||
6770 SecondExpr->isInstantiationDependent())) {
6771 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6772 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006773 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00006774 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
6775 : First->getLocStart();
6776 NoteRange = ErrorRange = FirstBinOp
6777 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00006778 : SourceRange(ErrorLoc, ErrorLoc);
6779 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006780 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6781 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6782 ErrorFound = NotAnAssignmentOp;
6783 NoteLoc = ErrorLoc = SecondBinOp
6784 ? SecondBinOp->getOperatorLoc()
6785 : Second->getLocStart();
6786 NoteRange = ErrorRange =
6787 SecondBinOp ? SecondBinOp->getSourceRange()
6788 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00006789 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006790 auto *PossibleXRHSInFirst =
6791 FirstBinOp->getRHS()->IgnoreParenImpCasts();
6792 auto *PossibleXLHSInSecond =
6793 SecondBinOp->getLHS()->IgnoreParenImpCasts();
6794 llvm::FoldingSetNodeID X1Id, X2Id;
6795 PossibleXRHSInFirst->Profile(X1Id, Context,
6796 /*Canonical=*/true);
6797 PossibleXLHSInSecond->Profile(X2Id, Context,
6798 /*Canonical=*/true);
6799 IsUpdateExprFound = X1Id == X2Id;
6800 if (IsUpdateExprFound) {
6801 V = FirstBinOp->getLHS();
6802 X = SecondBinOp->getLHS();
6803 E = SecondBinOp->getRHS();
6804 UE = nullptr;
6805 IsXLHSInRHSPart = false;
6806 IsPostfixUpdate = true;
6807 } else {
6808 ErrorFound = NotASpecificExpression;
6809 ErrorLoc = FirstBinOp->getExprLoc();
6810 ErrorRange = FirstBinOp->getSourceRange();
6811 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6812 NoteRange = SecondBinOp->getRHS()->getSourceRange();
6813 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006814 }
6815 }
6816 }
6817 }
6818 } else {
6819 NoteLoc = ErrorLoc = Body->getLocStart();
6820 NoteRange = ErrorRange =
6821 SourceRange(Body->getLocStart(), Body->getLocStart());
6822 ErrorFound = NotTwoSubstatements;
6823 }
6824 } else {
6825 NoteLoc = ErrorLoc = Body->getLocStart();
6826 NoteRange = ErrorRange =
6827 SourceRange(Body->getLocStart(), Body->getLocStart());
6828 ErrorFound = NotACompoundStatement;
6829 }
6830 if (ErrorFound != NoError) {
6831 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6832 << ErrorRange;
6833 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6834 return StmtError();
6835 } else if (CurContext->isDependentContext()) {
6836 UE = V = E = X = nullptr;
6837 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006838 }
Alexey Bataevdea47612014-07-23 07:46:59 +00006839 }
Alexey Bataev0162e452014-07-22 10:10:35 +00006840
6841 getCurFunction()->setHasBranchProtectedScope();
6842
Alexey Bataev62cec442014-11-18 10:14:22 +00006843 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00006844 X, V, E, UE, IsXLHSInRHSPart,
6845 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00006846}
6847
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006848StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6849 Stmt *AStmt,
6850 SourceLocation StartLoc,
6851 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006852 if (!AStmt)
6853 return StmtError();
6854
Samuel Antao4af1b7b2015-12-02 17:44:43 +00006855 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6856 // 1.2.2 OpenMP Language Terminology
6857 // Structured block - An executable statement with a single entry at the
6858 // top and a single exit at the bottom.
6859 // The point of exit cannot be a branch out of the structured block.
6860 // longjmp() and throw() must not violate the entry/exit criteria.
6861 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006862
Alexey Bataev13314bf2014-10-09 04:18:56 +00006863 // OpenMP [2.16, Nesting of Regions]
6864 // If specified, a teams construct must be contained within a target
6865 // construct. That target construct must contain no statements or directives
6866 // outside of the teams construct.
6867 if (DSAStack->hasInnerTeamsRegion()) {
6868 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
6869 bool OMPTeamsFound = true;
6870 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
6871 auto I = CS->body_begin();
6872 while (I != CS->body_end()) {
David Majnemer9d168222016-08-05 17:44:54 +00006873 auto *OED = dyn_cast<OMPExecutableDirective>(*I);
Alexey Bataev13314bf2014-10-09 04:18:56 +00006874 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6875 OMPTeamsFound = false;
6876 break;
6877 }
6878 ++I;
6879 }
6880 assert(I != CS->body_end() && "Not found statement");
6881 S = *I;
Kelvin Li3834dce2016-06-27 19:15:43 +00006882 } else {
6883 auto *OED = dyn_cast<OMPExecutableDirective>(S);
6884 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
Alexey Bataev13314bf2014-10-09 04:18:56 +00006885 }
6886 if (!OMPTeamsFound) {
6887 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6888 Diag(DSAStack->getInnerTeamsRegionLoc(),
6889 diag::note_omp_nested_teams_construct_here);
6890 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
6891 << isa<OMPExecutableDirective>(S);
6892 return StmtError();
6893 }
6894 }
6895
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006896 getCurFunction()->setHasBranchProtectedScope();
6897
6898 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6899}
6900
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006901StmtResult
6902Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6903 Stmt *AStmt, SourceLocation StartLoc,
6904 SourceLocation EndLoc) {
6905 if (!AStmt)
6906 return StmtError();
6907
6908 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6909 // 1.2.2 OpenMP Language Terminology
6910 // Structured block - An executable statement with a single entry at the
6911 // top and a single exit at the bottom.
6912 // The point of exit cannot be a branch out of the structured block.
6913 // longjmp() and throw() must not violate the entry/exit criteria.
6914 CS->getCapturedDecl()->setNothrow();
6915
6916 getCurFunction()->setHasBranchProtectedScope();
6917
6918 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6919 AStmt);
6920}
6921
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006922StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6923 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6924 SourceLocation EndLoc,
6925 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6926 if (!AStmt)
6927 return StmtError();
6928
6929 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6930 // 1.2.2 OpenMP Language Terminology
6931 // Structured block - An executable statement with a single entry at the
6932 // top and a single exit at the bottom.
6933 // The point of exit cannot be a branch out of the structured block.
6934 // longjmp() and throw() must not violate the entry/exit criteria.
6935 CS->getCapturedDecl()->setNothrow();
6936
6937 OMPLoopDirective::HelperExprs B;
6938 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6939 // define the nested loops number.
6940 unsigned NestedLoopCount =
6941 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
6942 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6943 VarsWithImplicitDSA, B);
6944 if (NestedLoopCount == 0)
6945 return StmtError();
6946
6947 assert((CurContext->isDependentContext() || B.builtAll()) &&
6948 "omp target parallel for loop exprs were not built");
6949
6950 if (!CurContext->isDependentContext()) {
6951 // Finalize the clauses that need pre-built expressions for CodeGen.
6952 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00006953 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006954 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006955 B.NumIterations, *this, CurScope,
6956 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006957 return StmtError();
6958 }
6959 }
6960
6961 getCurFunction()->setHasBranchProtectedScope();
6962 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
6963 NestedLoopCount, Clauses, AStmt,
6964 B, DSAStack->isCancelRegion());
6965}
6966
Samuel Antaodf67fc42016-01-19 19:15:56 +00006967/// \brief Check for existence of a map clause in the list of clauses.
6968static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
6969 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
6970 I != E; ++I) {
6971 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
6972 return true;
6973 }
6974 }
6975
6976 return false;
6977}
6978
Michael Wong65f367f2015-07-21 13:44:28 +00006979StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6980 Stmt *AStmt,
6981 SourceLocation StartLoc,
6982 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006983 if (!AStmt)
6984 return StmtError();
6985
6986 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6987
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006988 // OpenMP [2.10.1, Restrictions, p. 97]
6989 // At least one map clause must appear on the directive.
6990 if (!HasMapClause(Clauses)) {
David Majnemer9d168222016-08-05 17:44:54 +00006991 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6992 << getOpenMPDirectiveName(OMPD_target_data);
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006993 return StmtError();
6994 }
6995
Michael Wong65f367f2015-07-21 13:44:28 +00006996 getCurFunction()->setHasBranchProtectedScope();
6997
6998 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6999 AStmt);
7000}
7001
Samuel Antaodf67fc42016-01-19 19:15:56 +00007002StmtResult
7003Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
7004 SourceLocation StartLoc,
7005 SourceLocation EndLoc) {
7006 // OpenMP [2.10.2, Restrictions, p. 99]
7007 // At least one map clause must appear on the directive.
7008 if (!HasMapClause(Clauses)) {
7009 Diag(StartLoc, diag::err_omp_no_map_for_directive)
7010 << getOpenMPDirectiveName(OMPD_target_enter_data);
7011 return StmtError();
7012 }
7013
7014 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
7015 Clauses);
7016}
7017
Samuel Antao72590762016-01-19 20:04:50 +00007018StmtResult
7019Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
7020 SourceLocation StartLoc,
7021 SourceLocation EndLoc) {
7022 // OpenMP [2.10.3, Restrictions, p. 102]
7023 // At least one map clause must appear on the directive.
7024 if (!HasMapClause(Clauses)) {
7025 Diag(StartLoc, diag::err_omp_no_map_for_directive)
7026 << getOpenMPDirectiveName(OMPD_target_exit_data);
7027 return StmtError();
7028 }
7029
7030 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
7031}
7032
Samuel Antao686c70c2016-05-26 17:30:50 +00007033StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
7034 SourceLocation StartLoc,
7035 SourceLocation EndLoc) {
Samuel Antao686c70c2016-05-26 17:30:50 +00007036 bool seenMotionClause = false;
Samuel Antao661c0902016-05-26 17:39:58 +00007037 for (auto *C : Clauses) {
Samuel Antaoec172c62016-05-26 17:49:04 +00007038 if (C->getClauseKind() == OMPC_to || C->getClauseKind() == OMPC_from)
Samuel Antao661c0902016-05-26 17:39:58 +00007039 seenMotionClause = true;
7040 }
Samuel Antao686c70c2016-05-26 17:30:50 +00007041 if (!seenMotionClause) {
7042 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
7043 return StmtError();
7044 }
7045 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses);
7046}
7047
Alexey Bataev13314bf2014-10-09 04:18:56 +00007048StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
7049 Stmt *AStmt, SourceLocation StartLoc,
7050 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007051 if (!AStmt)
7052 return StmtError();
7053
Alexey Bataev13314bf2014-10-09 04:18:56 +00007054 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7055 // 1.2.2 OpenMP Language Terminology
7056 // Structured block - An executable statement with a single entry at the
7057 // top and a single exit at the bottom.
7058 // The point of exit cannot be a branch out of the structured block.
7059 // longjmp() and throw() must not violate the entry/exit criteria.
7060 CS->getCapturedDecl()->setNothrow();
7061
7062 getCurFunction()->setHasBranchProtectedScope();
7063
7064 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
7065}
7066
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007067StmtResult
7068Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
7069 SourceLocation EndLoc,
7070 OpenMPDirectiveKind CancelRegion) {
7071 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
7072 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
7073 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
7074 << getOpenMPDirectiveName(CancelRegion);
7075 return StmtError();
7076 }
7077 if (DSAStack->isParentNowaitRegion()) {
7078 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
7079 return StmtError();
7080 }
7081 if (DSAStack->isParentOrderedRegion()) {
7082 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
7083 return StmtError();
7084 }
7085 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
7086 CancelRegion);
7087}
7088
Alexey Bataev87933c72015-09-18 08:07:34 +00007089StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
7090 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00007091 SourceLocation EndLoc,
7092 OpenMPDirectiveKind CancelRegion) {
7093 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
7094 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
7095 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
7096 << getOpenMPDirectiveName(CancelRegion);
7097 return StmtError();
7098 }
7099 if (DSAStack->isParentNowaitRegion()) {
7100 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
7101 return StmtError();
7102 }
7103 if (DSAStack->isParentOrderedRegion()) {
7104 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
7105 return StmtError();
7106 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00007107 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00007108 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
7109 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00007110}
7111
Alexey Bataev382967a2015-12-08 12:06:20 +00007112static bool checkGrainsizeNumTasksClauses(Sema &S,
7113 ArrayRef<OMPClause *> Clauses) {
7114 OMPClause *PrevClause = nullptr;
7115 bool ErrorFound = false;
7116 for (auto *C : Clauses) {
7117 if (C->getClauseKind() == OMPC_grainsize ||
7118 C->getClauseKind() == OMPC_num_tasks) {
7119 if (!PrevClause)
7120 PrevClause = C;
7121 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
7122 S.Diag(C->getLocStart(),
7123 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
7124 << getOpenMPClauseName(C->getClauseKind())
7125 << getOpenMPClauseName(PrevClause->getClauseKind());
7126 S.Diag(PrevClause->getLocStart(),
7127 diag::note_omp_previous_grainsize_num_tasks)
7128 << getOpenMPClauseName(PrevClause->getClauseKind());
7129 ErrorFound = true;
7130 }
7131 }
7132 }
7133 return ErrorFound;
7134}
7135
Alexey Bataev49f6e782015-12-01 04:18:41 +00007136StmtResult Sema::ActOnOpenMPTaskLoopDirective(
7137 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7138 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007139 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00007140 if (!AStmt)
7141 return StmtError();
7142
7143 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7144 OMPLoopDirective::HelperExprs B;
7145 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7146 // define the nested loops number.
7147 unsigned NestedLoopCount =
7148 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007149 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00007150 VarsWithImplicitDSA, B);
7151 if (NestedLoopCount == 0)
7152 return StmtError();
7153
7154 assert((CurContext->isDependentContext() || B.builtAll()) &&
7155 "omp for loop exprs were not built");
7156
Alexey Bataev382967a2015-12-08 12:06:20 +00007157 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7158 // The grainsize clause and num_tasks clause are mutually exclusive and may
7159 // not appear on the same taskloop directive.
7160 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7161 return StmtError();
7162
Alexey Bataev49f6e782015-12-01 04:18:41 +00007163 getCurFunction()->setHasBranchProtectedScope();
7164 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
7165 NestedLoopCount, Clauses, AStmt, B);
7166}
7167
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007168StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
7169 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7170 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007171 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007172 if (!AStmt)
7173 return StmtError();
7174
7175 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7176 OMPLoopDirective::HelperExprs B;
7177 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7178 // define the nested loops number.
7179 unsigned NestedLoopCount =
7180 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
7181 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
7182 VarsWithImplicitDSA, B);
7183 if (NestedLoopCount == 0)
7184 return StmtError();
7185
7186 assert((CurContext->isDependentContext() || B.builtAll()) &&
7187 "omp for loop exprs were not built");
7188
Alexey Bataev5a3af132016-03-29 08:58:54 +00007189 if (!CurContext->isDependentContext()) {
7190 // Finalize the clauses that need pre-built expressions for CodeGen.
7191 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007192 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007193 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007194 B.NumIterations, *this, CurScope,
7195 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007196 return StmtError();
7197 }
7198 }
7199
Alexey Bataev382967a2015-12-08 12:06:20 +00007200 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7201 // The grainsize clause and num_tasks clause are mutually exclusive and may
7202 // not appear on the same taskloop directive.
7203 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7204 return StmtError();
7205
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007206 getCurFunction()->setHasBranchProtectedScope();
7207 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
7208 NestedLoopCount, Clauses, AStmt, B);
7209}
7210
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007211StmtResult Sema::ActOnOpenMPDistributeDirective(
7212 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7213 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007214 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007215 if (!AStmt)
7216 return StmtError();
7217
7218 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7219 OMPLoopDirective::HelperExprs B;
7220 // In presence of clause 'collapse' with number of loops, it will
7221 // define the nested loops number.
7222 unsigned NestedLoopCount =
7223 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
7224 nullptr /*ordered not a clause on distribute*/, AStmt,
7225 *this, *DSAStack, VarsWithImplicitDSA, B);
7226 if (NestedLoopCount == 0)
7227 return StmtError();
7228
7229 assert((CurContext->isDependentContext() || B.builtAll()) &&
7230 "omp for loop exprs were not built");
7231
7232 getCurFunction()->setHasBranchProtectedScope();
7233 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
7234 NestedLoopCount, Clauses, AStmt, B);
7235}
7236
Carlo Bertolli9925f152016-06-27 14:55:37 +00007237StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
7238 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7239 SourceLocation EndLoc,
7240 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7241 if (!AStmt)
7242 return StmtError();
7243
7244 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7245 // 1.2.2 OpenMP Language Terminology
7246 // Structured block - An executable statement with a single entry at the
7247 // top and a single exit at the bottom.
7248 // The point of exit cannot be a branch out of the structured block.
7249 // longjmp() and throw() must not violate the entry/exit criteria.
7250 CS->getCapturedDecl()->setNothrow();
7251
7252 OMPLoopDirective::HelperExprs B;
7253 // In presence of clause 'collapse' with number of loops, it will
7254 // define the nested loops number.
7255 unsigned NestedLoopCount = CheckOpenMPLoop(
7256 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
7257 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
7258 VarsWithImplicitDSA, B);
7259 if (NestedLoopCount == 0)
7260 return StmtError();
7261
7262 assert((CurContext->isDependentContext() || B.builtAll()) &&
7263 "omp for loop exprs were not built");
7264
7265 getCurFunction()->setHasBranchProtectedScope();
7266 return OMPDistributeParallelForDirective::Create(
7267 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7268}
7269
Kelvin Li4a39add2016-07-05 05:00:15 +00007270StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
7271 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7272 SourceLocation EndLoc,
7273 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7274 if (!AStmt)
7275 return StmtError();
7276
7277 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7278 // 1.2.2 OpenMP Language Terminology
7279 // Structured block - An executable statement with a single entry at the
7280 // top and a single exit at the bottom.
7281 // The point of exit cannot be a branch out of the structured block.
7282 // longjmp() and throw() must not violate the entry/exit criteria.
7283 CS->getCapturedDecl()->setNothrow();
7284
7285 OMPLoopDirective::HelperExprs B;
7286 // In presence of clause 'collapse' with number of loops, it will
7287 // define the nested loops number.
7288 unsigned NestedLoopCount = CheckOpenMPLoop(
7289 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
7290 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
7291 VarsWithImplicitDSA, B);
7292 if (NestedLoopCount == 0)
7293 return StmtError();
7294
7295 assert((CurContext->isDependentContext() || B.builtAll()) &&
7296 "omp for loop exprs were not built");
7297
Kelvin Lic5609492016-07-15 04:39:07 +00007298 if (checkSimdlenSafelenSpecified(*this, Clauses))
7299 return StmtError();
7300
Kelvin Li4a39add2016-07-05 05:00:15 +00007301 getCurFunction()->setHasBranchProtectedScope();
7302 return OMPDistributeParallelForSimdDirective::Create(
7303 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7304}
7305
Kelvin Li787f3fc2016-07-06 04:45:38 +00007306StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
7307 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7308 SourceLocation EndLoc,
7309 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7310 if (!AStmt)
7311 return StmtError();
7312
7313 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7314 // 1.2.2 OpenMP Language Terminology
7315 // Structured block - An executable statement with a single entry at the
7316 // top and a single exit at the bottom.
7317 // The point of exit cannot be a branch out of the structured block.
7318 // longjmp() and throw() must not violate the entry/exit criteria.
7319 CS->getCapturedDecl()->setNothrow();
7320
7321 OMPLoopDirective::HelperExprs B;
7322 // In presence of clause 'collapse' with number of loops, it will
7323 // define the nested loops number.
7324 unsigned NestedLoopCount =
7325 CheckOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
7326 nullptr /*ordered not a clause on distribute*/, AStmt,
7327 *this, *DSAStack, VarsWithImplicitDSA, B);
7328 if (NestedLoopCount == 0)
7329 return StmtError();
7330
7331 assert((CurContext->isDependentContext() || B.builtAll()) &&
7332 "omp for loop exprs were not built");
7333
Kelvin Lic5609492016-07-15 04:39:07 +00007334 if (checkSimdlenSafelenSpecified(*this, Clauses))
7335 return StmtError();
7336
Kelvin Li787f3fc2016-07-06 04:45:38 +00007337 getCurFunction()->setHasBranchProtectedScope();
7338 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
7339 NestedLoopCount, Clauses, AStmt, B);
7340}
7341
Kelvin Lia579b912016-07-14 02:54:56 +00007342StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
7343 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7344 SourceLocation EndLoc,
7345 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7346 if (!AStmt)
7347 return StmtError();
7348
7349 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7350 // 1.2.2 OpenMP Language Terminology
7351 // Structured block - An executable statement with a single entry at the
7352 // top and a single exit at the bottom.
7353 // The point of exit cannot be a branch out of the structured block.
7354 // longjmp() and throw() must not violate the entry/exit criteria.
7355 CS->getCapturedDecl()->setNothrow();
7356
7357 OMPLoopDirective::HelperExprs B;
7358 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7359 // define the nested loops number.
7360 unsigned NestedLoopCount = CheckOpenMPLoop(
7361 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
7362 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
7363 VarsWithImplicitDSA, B);
7364 if (NestedLoopCount == 0)
7365 return StmtError();
7366
7367 assert((CurContext->isDependentContext() || B.builtAll()) &&
7368 "omp target parallel for simd loop exprs were not built");
7369
7370 if (!CurContext->isDependentContext()) {
7371 // Finalize the clauses that need pre-built expressions for CodeGen.
7372 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007373 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Lia579b912016-07-14 02:54:56 +00007374 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7375 B.NumIterations, *this, CurScope,
7376 DSAStack))
7377 return StmtError();
7378 }
7379 }
Kelvin Lic5609492016-07-15 04:39:07 +00007380 if (checkSimdlenSafelenSpecified(*this, Clauses))
Kelvin Lia579b912016-07-14 02:54:56 +00007381 return StmtError();
7382
7383 getCurFunction()->setHasBranchProtectedScope();
7384 return OMPTargetParallelForSimdDirective::Create(
7385 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7386}
7387
Kelvin Li986330c2016-07-20 22:57:10 +00007388StmtResult Sema::ActOnOpenMPTargetSimdDirective(
7389 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7390 SourceLocation EndLoc,
7391 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7392 if (!AStmt)
7393 return StmtError();
7394
7395 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7396 // 1.2.2 OpenMP Language Terminology
7397 // Structured block - An executable statement with a single entry at the
7398 // top and a single exit at the bottom.
7399 // The point of exit cannot be a branch out of the structured block.
7400 // longjmp() and throw() must not violate the entry/exit criteria.
7401 CS->getCapturedDecl()->setNothrow();
7402
7403 OMPLoopDirective::HelperExprs B;
7404 // In presence of clause 'collapse' with number of loops, it will define the
7405 // nested loops number.
David Majnemer9d168222016-08-05 17:44:54 +00007406 unsigned NestedLoopCount =
Kelvin Li986330c2016-07-20 22:57:10 +00007407 CheckOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
7408 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
7409 VarsWithImplicitDSA, B);
7410 if (NestedLoopCount == 0)
7411 return StmtError();
7412
7413 assert((CurContext->isDependentContext() || B.builtAll()) &&
7414 "omp target simd loop exprs were not built");
7415
7416 if (!CurContext->isDependentContext()) {
7417 // Finalize the clauses that need pre-built expressions for CodeGen.
7418 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007419 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Li986330c2016-07-20 22:57:10 +00007420 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7421 B.NumIterations, *this, CurScope,
7422 DSAStack))
7423 return StmtError();
7424 }
7425 }
7426
7427 if (checkSimdlenSafelenSpecified(*this, Clauses))
7428 return StmtError();
7429
7430 getCurFunction()->setHasBranchProtectedScope();
7431 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
7432 NestedLoopCount, Clauses, AStmt, B);
7433}
7434
Kelvin Li02532872016-08-05 14:37:37 +00007435StmtResult Sema::ActOnOpenMPTeamsDistributeDirective(
7436 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7437 SourceLocation EndLoc,
7438 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7439 if (!AStmt)
7440 return StmtError();
7441
7442 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7443 // 1.2.2 OpenMP Language Terminology
7444 // Structured block - An executable statement with a single entry at the
7445 // top and a single exit at the bottom.
7446 // The point of exit cannot be a branch out of the structured block.
7447 // longjmp() and throw() must not violate the entry/exit criteria.
7448 CS->getCapturedDecl()->setNothrow();
7449
7450 OMPLoopDirective::HelperExprs B;
7451 // In presence of clause 'collapse' with number of loops, it will
7452 // define the nested loops number.
7453 unsigned NestedLoopCount =
7454 CheckOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses),
7455 nullptr /*ordered not a clause on distribute*/, AStmt,
7456 *this, *DSAStack, VarsWithImplicitDSA, B);
7457 if (NestedLoopCount == 0)
7458 return StmtError();
7459
7460 assert((CurContext->isDependentContext() || B.builtAll()) &&
7461 "omp teams distribute loop exprs were not built");
7462
7463 getCurFunction()->setHasBranchProtectedScope();
David Majnemer9d168222016-08-05 17:44:54 +00007464 return OMPTeamsDistributeDirective::Create(
7465 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Kelvin Li02532872016-08-05 14:37:37 +00007466}
7467
Alexey Bataeved09d242014-05-28 05:53:51 +00007468OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007469 SourceLocation StartLoc,
7470 SourceLocation LParenLoc,
7471 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007472 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007473 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00007474 case OMPC_final:
7475 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
7476 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00007477 case OMPC_num_threads:
7478 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
7479 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007480 case OMPC_safelen:
7481 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
7482 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00007483 case OMPC_simdlen:
7484 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
7485 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00007486 case OMPC_collapse:
7487 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
7488 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00007489 case OMPC_ordered:
7490 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
7491 break;
Michael Wonge710d542015-08-07 16:16:36 +00007492 case OMPC_device:
7493 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
7494 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00007495 case OMPC_num_teams:
7496 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
7497 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007498 case OMPC_thread_limit:
7499 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
7500 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00007501 case OMPC_priority:
7502 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
7503 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007504 case OMPC_grainsize:
7505 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
7506 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00007507 case OMPC_num_tasks:
7508 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
7509 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00007510 case OMPC_hint:
7511 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
7512 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007513 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007514 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007515 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007516 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007517 case OMPC_private:
7518 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00007519 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007520 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007521 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00007522 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007523 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007524 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007525 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00007526 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007527 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007528 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007529 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007530 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007531 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007532 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007533 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007534 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007535 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007536 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00007537 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007538 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007539 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00007540 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007541 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007542 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007543 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007544 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007545 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007546 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007547 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007548 case OMPC_is_device_ptr:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007549 llvm_unreachable("Clause is not allowed.");
7550 }
7551 return Res;
7552}
7553
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007554OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
7555 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007556 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007557 SourceLocation NameModifierLoc,
7558 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007559 SourceLocation EndLoc) {
7560 Expr *ValExpr = Condition;
7561 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
7562 !Condition->isInstantiationDependent() &&
7563 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00007564 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007565 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007566 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007567
Richard Smith03a4aa32016-06-23 19:02:52 +00007568 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007569 }
7570
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007571 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
7572 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007573}
7574
Alexey Bataev3778b602014-07-17 07:32:53 +00007575OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
7576 SourceLocation StartLoc,
7577 SourceLocation LParenLoc,
7578 SourceLocation EndLoc) {
7579 Expr *ValExpr = Condition;
7580 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
7581 !Condition->isInstantiationDependent() &&
7582 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00007583 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataev3778b602014-07-17 07:32:53 +00007584 if (Val.isInvalid())
7585 return nullptr;
7586
Richard Smith03a4aa32016-06-23 19:02:52 +00007587 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataev3778b602014-07-17 07:32:53 +00007588 }
7589
7590 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
7591}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00007592ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
7593 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00007594 if (!Op)
7595 return ExprError();
7596
7597 class IntConvertDiagnoser : public ICEConvertDiagnoser {
7598 public:
7599 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00007600 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00007601 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
7602 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007603 return S.Diag(Loc, diag::err_omp_not_integral) << T;
7604 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007605 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
7606 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007607 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
7608 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007609 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
7610 QualType T,
7611 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007612 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
7613 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007614 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
7615 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007616 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00007617 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00007618 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007619 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
7620 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007621 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
7622 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007623 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
7624 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007625 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00007626 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00007627 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007628 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
7629 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007630 llvm_unreachable("conversion functions are permitted");
7631 }
7632 } ConvertDiagnoser;
7633 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
7634}
7635
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007636static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00007637 OpenMPClauseKind CKind,
7638 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007639 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
7640 !ValExpr->isInstantiationDependent()) {
7641 SourceLocation Loc = ValExpr->getExprLoc();
7642 ExprResult Value =
7643 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
7644 if (Value.isInvalid())
7645 return false;
7646
7647 ValExpr = Value.get();
7648 // The expression must evaluate to a non-negative integer value.
7649 llvm::APSInt Result;
7650 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00007651 Result.isSigned() &&
7652 !((!StrictlyPositive && Result.isNonNegative()) ||
7653 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007654 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00007655 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
7656 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007657 return false;
7658 }
7659 }
7660 return true;
7661}
7662
Alexey Bataev568a8332014-03-06 06:15:19 +00007663OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
7664 SourceLocation StartLoc,
7665 SourceLocation LParenLoc,
7666 SourceLocation EndLoc) {
7667 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00007668
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007669 // OpenMP [2.5, Restrictions]
7670 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00007671 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
7672 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007673 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00007674
Alexey Bataeved09d242014-05-28 05:53:51 +00007675 return new (Context)
7676 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00007677}
7678
Alexey Bataev62c87d22014-03-21 04:51:18 +00007679ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007680 OpenMPClauseKind CKind,
7681 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00007682 if (!E)
7683 return ExprError();
7684 if (E->isValueDependent() || E->isTypeDependent() ||
7685 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00007686 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007687 llvm::APSInt Result;
7688 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
7689 if (ICE.isInvalid())
7690 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007691 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
7692 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00007693 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007694 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
7695 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00007696 return ExprError();
7697 }
Alexander Musman09184fe2014-09-30 05:29:28 +00007698 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
7699 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
7700 << E->getSourceRange();
7701 return ExprError();
7702 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007703 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
7704 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00007705 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007706 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00007707 return ICE;
7708}
7709
7710OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
7711 SourceLocation LParenLoc,
7712 SourceLocation EndLoc) {
7713 // OpenMP [2.8.1, simd construct, Description]
7714 // The parameter of the safelen clause must be a constant
7715 // positive integer expression.
7716 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
7717 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007718 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007719 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00007720 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00007721}
7722
Alexey Bataev66b15b52015-08-21 11:14:16 +00007723OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
7724 SourceLocation LParenLoc,
7725 SourceLocation EndLoc) {
7726 // OpenMP [2.8.1, simd construct, Description]
7727 // The parameter of the simdlen clause must be a constant
7728 // positive integer expression.
7729 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
7730 if (Simdlen.isInvalid())
7731 return nullptr;
7732 return new (Context)
7733 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
7734}
7735
Alexander Musman64d33f12014-06-04 07:53:32 +00007736OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
7737 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00007738 SourceLocation LParenLoc,
7739 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00007740 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00007741 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00007742 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00007743 // The parameter of the collapse clause must be a constant
7744 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00007745 ExprResult NumForLoopsResult =
7746 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
7747 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00007748 return nullptr;
7749 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00007750 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00007751}
7752
Alexey Bataev10e775f2015-07-30 11:36:16 +00007753OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
7754 SourceLocation EndLoc,
7755 SourceLocation LParenLoc,
7756 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00007757 // OpenMP [2.7.1, loop construct, Description]
7758 // OpenMP [2.8.1, simd construct, Description]
7759 // OpenMP [2.9.6, distribute construct, Description]
7760 // The parameter of the ordered clause must be a constant
7761 // positive integer expression if any.
7762 if (NumForLoops && LParenLoc.isValid()) {
7763 ExprResult NumForLoopsResult =
7764 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
7765 if (NumForLoopsResult.isInvalid())
7766 return nullptr;
7767 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00007768 } else
7769 NumForLoops = nullptr;
7770 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00007771 return new (Context)
7772 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
7773}
7774
Alexey Bataeved09d242014-05-28 05:53:51 +00007775OMPClause *Sema::ActOnOpenMPSimpleClause(
7776 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
7777 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007778 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007779 switch (Kind) {
7780 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00007781 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00007782 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
7783 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007784 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007785 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00007786 Res = ActOnOpenMPProcBindClause(
7787 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
7788 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007789 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007790 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007791 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00007792 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00007793 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007794 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00007795 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007796 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007797 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007798 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00007799 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00007800 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007801 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00007802 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007803 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007804 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007805 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007806 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007807 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007808 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007809 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007810 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007811 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007812 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007813 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007814 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007815 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007816 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007817 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007818 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007819 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007820 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007821 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007822 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007823 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007824 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007825 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007826 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007827 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007828 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007829 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007830 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007831 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007832 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007833 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007834 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007835 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007836 case OMPC_is_device_ptr:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007837 llvm_unreachable("Clause is not allowed.");
7838 }
7839 return Res;
7840}
7841
Alexey Bataev6402bca2015-12-28 07:25:51 +00007842static std::string
7843getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
7844 ArrayRef<unsigned> Exclude = llvm::None) {
7845 std::string Values;
7846 unsigned Bound = Last >= 2 ? Last - 2 : 0;
7847 unsigned Skipped = Exclude.size();
7848 auto S = Exclude.begin(), E = Exclude.end();
7849 for (unsigned i = First; i < Last; ++i) {
7850 if (std::find(S, E, i) != E) {
7851 --Skipped;
7852 continue;
7853 }
7854 Values += "'";
7855 Values += getOpenMPSimpleClauseTypeName(K, i);
7856 Values += "'";
7857 if (i == Bound - Skipped)
7858 Values += " or ";
7859 else if (i != Bound + 1 - Skipped)
7860 Values += ", ";
7861 }
7862 return Values;
7863}
7864
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007865OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
7866 SourceLocation KindKwLoc,
7867 SourceLocation StartLoc,
7868 SourceLocation LParenLoc,
7869 SourceLocation EndLoc) {
7870 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00007871 static_assert(OMPC_DEFAULT_unknown > 0,
7872 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007873 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00007874 << getListOfPossibleValues(OMPC_default, /*First=*/0,
7875 /*Last=*/OMPC_DEFAULT_unknown)
7876 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007877 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007878 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00007879 switch (Kind) {
7880 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007881 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007882 break;
7883 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007884 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007885 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007886 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007887 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00007888 break;
7889 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007890 return new (Context)
7891 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007892}
7893
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007894OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
7895 SourceLocation KindKwLoc,
7896 SourceLocation StartLoc,
7897 SourceLocation LParenLoc,
7898 SourceLocation EndLoc) {
7899 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007900 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00007901 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
7902 /*Last=*/OMPC_PROC_BIND_unknown)
7903 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007904 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007905 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007906 return new (Context)
7907 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007908}
7909
Alexey Bataev56dafe82014-06-20 07:16:17 +00007910OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007911 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007912 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007913 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007914 SourceLocation EndLoc) {
7915 OMPClause *Res = nullptr;
7916 switch (Kind) {
7917 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00007918 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
7919 assert(Argument.size() == NumberOfElements &&
7920 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007921 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007922 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
7923 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
7924 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
7925 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
7926 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007927 break;
7928 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00007929 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
7930 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
7931 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
7932 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007933 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00007934 case OMPC_dist_schedule:
7935 Res = ActOnOpenMPDistScheduleClause(
7936 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
7937 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
7938 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007939 case OMPC_defaultmap:
7940 enum { Modifier, DefaultmapKind };
7941 Res = ActOnOpenMPDefaultmapClause(
7942 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
7943 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
David Majnemer9d168222016-08-05 17:44:54 +00007944 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind],
7945 EndLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007946 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00007947 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007948 case OMPC_num_threads:
7949 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007950 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007951 case OMPC_collapse:
7952 case OMPC_default:
7953 case OMPC_proc_bind:
7954 case OMPC_private:
7955 case OMPC_firstprivate:
7956 case OMPC_lastprivate:
7957 case OMPC_shared:
7958 case OMPC_reduction:
7959 case OMPC_linear:
7960 case OMPC_aligned:
7961 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007962 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007963 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007964 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007965 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007966 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007967 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007968 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007969 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007970 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007971 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007972 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007973 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007974 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007975 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007976 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007977 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007978 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007979 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007980 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007981 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007982 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007983 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007984 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007985 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007986 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007987 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007988 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007989 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007990 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007991 case OMPC_is_device_ptr:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007992 llvm_unreachable("Clause is not allowed.");
7993 }
7994 return Res;
7995}
7996
Alexey Bataev6402bca2015-12-28 07:25:51 +00007997static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
7998 OpenMPScheduleClauseModifier M2,
7999 SourceLocation M1Loc, SourceLocation M2Loc) {
8000 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
8001 SmallVector<unsigned, 2> Excluded;
8002 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
8003 Excluded.push_back(M2);
8004 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
8005 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
8006 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
8007 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
8008 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
8009 << getListOfPossibleValues(OMPC_schedule,
8010 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
8011 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
8012 Excluded)
8013 << getOpenMPClauseName(OMPC_schedule);
8014 return true;
8015 }
8016 return false;
8017}
8018
Alexey Bataev56dafe82014-06-20 07:16:17 +00008019OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00008020 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00008021 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00008022 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
8023 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
8024 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
8025 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
8026 return nullptr;
8027 // OpenMP, 2.7.1, Loop Construct, Restrictions
8028 // Either the monotonic modifier or the nonmonotonic modifier can be specified
8029 // but not both.
8030 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
8031 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
8032 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
8033 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
8034 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
8035 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
8036 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
8037 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
8038 return nullptr;
8039 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00008040 if (Kind == OMPC_SCHEDULE_unknown) {
8041 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00008042 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
8043 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
8044 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
8045 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
8046 Exclude);
8047 } else {
8048 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
8049 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00008050 }
8051 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
8052 << Values << getOpenMPClauseName(OMPC_schedule);
8053 return nullptr;
8054 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00008055 // OpenMP, 2.7.1, Loop Construct, Restrictions
8056 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
8057 // schedule(guided).
8058 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
8059 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
8060 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
8061 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
8062 diag::err_omp_schedule_nonmonotonic_static);
8063 return nullptr;
8064 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00008065 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00008066 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00008067 if (ChunkSize) {
8068 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
8069 !ChunkSize->isInstantiationDependent() &&
8070 !ChunkSize->containsUnexpandedParameterPack()) {
8071 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
8072 ExprResult Val =
8073 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
8074 if (Val.isInvalid())
8075 return nullptr;
8076
8077 ValExpr = Val.get();
8078
8079 // OpenMP [2.7.1, Restrictions]
8080 // chunk_size must be a loop invariant integer expression with a positive
8081 // value.
8082 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00008083 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
8084 if (Result.isSigned() && !Result.isStrictlyPositive()) {
8085 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00008086 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00008087 return nullptr;
8088 }
Alexey Bataevb46cdea2016-06-15 11:20:48 +00008089 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
8090 !CurContext->isDependentContext()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00008091 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
8092 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8093 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00008094 }
8095 }
8096 }
8097
Alexey Bataev6402bca2015-12-28 07:25:51 +00008098 return new (Context)
8099 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00008100 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00008101}
8102
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008103OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
8104 SourceLocation StartLoc,
8105 SourceLocation EndLoc) {
8106 OMPClause *Res = nullptr;
8107 switch (Kind) {
8108 case OMPC_ordered:
8109 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
8110 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00008111 case OMPC_nowait:
8112 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
8113 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008114 case OMPC_untied:
8115 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
8116 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008117 case OMPC_mergeable:
8118 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
8119 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008120 case OMPC_read:
8121 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
8122 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00008123 case OMPC_write:
8124 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
8125 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00008126 case OMPC_update:
8127 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
8128 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00008129 case OMPC_capture:
8130 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
8131 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008132 case OMPC_seq_cst:
8133 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
8134 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00008135 case OMPC_threads:
8136 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
8137 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008138 case OMPC_simd:
8139 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
8140 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00008141 case OMPC_nogroup:
8142 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
8143 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008144 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00008145 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008146 case OMPC_num_threads:
8147 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008148 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008149 case OMPC_collapse:
8150 case OMPC_schedule:
8151 case OMPC_private:
8152 case OMPC_firstprivate:
8153 case OMPC_lastprivate:
8154 case OMPC_shared:
8155 case OMPC_reduction:
8156 case OMPC_linear:
8157 case OMPC_aligned:
8158 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008159 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008160 case OMPC_default:
8161 case OMPC_proc_bind:
8162 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00008163 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008164 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00008165 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008166 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00008167 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008168 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00008169 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008170 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00008171 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00008172 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008173 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008174 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008175 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008176 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00008177 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00008178 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00008179 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00008180 case OMPC_is_device_ptr:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008181 llvm_unreachable("Clause is not allowed.");
8182 }
8183 return Res;
8184}
8185
Alexey Bataev236070f2014-06-20 11:19:47 +00008186OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
8187 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00008188 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00008189 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
8190}
8191
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008192OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
8193 SourceLocation EndLoc) {
8194 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
8195}
8196
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008197OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
8198 SourceLocation EndLoc) {
8199 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
8200}
8201
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008202OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
8203 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008204 return new (Context) OMPReadClause(StartLoc, EndLoc);
8205}
8206
Alexey Bataevdea47612014-07-23 07:46:59 +00008207OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
8208 SourceLocation EndLoc) {
8209 return new (Context) OMPWriteClause(StartLoc, EndLoc);
8210}
8211
Alexey Bataev67a4f222014-07-23 10:25:33 +00008212OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
8213 SourceLocation EndLoc) {
8214 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
8215}
8216
Alexey Bataev459dec02014-07-24 06:46:57 +00008217OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
8218 SourceLocation EndLoc) {
8219 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
8220}
8221
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008222OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
8223 SourceLocation EndLoc) {
8224 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
8225}
8226
Alexey Bataev346265e2015-09-25 10:37:12 +00008227OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
8228 SourceLocation EndLoc) {
8229 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
8230}
8231
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008232OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
8233 SourceLocation EndLoc) {
8234 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
8235}
8236
Alexey Bataevb825de12015-12-07 10:51:44 +00008237OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
8238 SourceLocation EndLoc) {
8239 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
8240}
8241
Alexey Bataevc5e02582014-06-16 07:08:35 +00008242OMPClause *Sema::ActOnOpenMPVarListClause(
8243 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
8244 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
8245 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008246 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00008247 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
8248 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
8249 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008250 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008251 switch (Kind) {
8252 case OMPC_private:
8253 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8254 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008255 case OMPC_firstprivate:
8256 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8257 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008258 case OMPC_lastprivate:
8259 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8260 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008261 case OMPC_shared:
8262 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
8263 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008264 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00008265 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
8266 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008267 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00008268 case OMPC_linear:
8269 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00008270 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00008271 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008272 case OMPC_aligned:
8273 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
8274 ColonLoc, EndLoc);
8275 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008276 case OMPC_copyin:
8277 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
8278 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008279 case OMPC_copyprivate:
8280 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8281 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00008282 case OMPC_flush:
8283 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
8284 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008285 case OMPC_depend:
David Majnemer9d168222016-08-05 17:44:54 +00008286 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
Kelvin Li0bff7af2015-11-23 05:32:03 +00008287 StartLoc, LParenLoc, EndLoc);
8288 break;
8289 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00008290 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
8291 DepLinMapLoc, ColonLoc, VarList, StartLoc,
8292 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008293 break;
Samuel Antao661c0902016-05-26 17:39:58 +00008294 case OMPC_to:
8295 Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc);
8296 break;
Samuel Antaoec172c62016-05-26 17:49:04 +00008297 case OMPC_from:
8298 Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc);
8299 break;
Carlo Bertolli2404b172016-07-13 15:37:16 +00008300 case OMPC_use_device_ptr:
8301 Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
8302 break;
Carlo Bertolli70594e92016-07-13 17:16:49 +00008303 case OMPC_is_device_ptr:
8304 Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
8305 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008306 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00008307 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00008308 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00008309 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008310 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00008311 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008312 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008313 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008314 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008315 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00008316 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008317 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008318 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008319 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008320 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008321 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008322 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008323 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008324 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00008325 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00008326 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008327 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00008328 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008329 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00008330 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008331 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00008332 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00008333 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00008334 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008335 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008336 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008337 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008338 case OMPC_uniform:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008339 llvm_unreachable("Clause is not allowed.");
8340 }
8341 return Res;
8342}
8343
Alexey Bataev90c228f2016-02-08 09:29:13 +00008344ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00008345 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00008346 ExprResult Res = BuildDeclRefExpr(
8347 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
8348 if (!Res.isUsable())
8349 return ExprError();
8350 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
8351 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
8352 if (!Res.isUsable())
8353 return ExprError();
8354 }
8355 if (VK != VK_LValue && Res.get()->isGLValue()) {
8356 Res = DefaultLvalueConversion(Res.get());
8357 if (!Res.isUsable())
8358 return ExprError();
8359 }
8360 return Res;
8361}
8362
Alexey Bataev60da77e2016-02-29 05:54:20 +00008363static std::pair<ValueDecl *, bool>
8364getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
8365 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008366 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
8367 RefExpr->containsUnexpandedParameterPack())
8368 return std::make_pair(nullptr, true);
8369
Alexey Bataevd985eda2016-02-10 11:29:16 +00008370 // OpenMP [3.1, C/C++]
8371 // A list item is a variable name.
8372 // OpenMP [2.9.3.3, Restrictions, p.1]
8373 // A variable that is part of another variable (as an array or
8374 // structure element) cannot appear in a private clause.
8375 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008376 enum {
8377 NoArrayExpr = -1,
8378 ArraySubscript = 0,
8379 OMPArraySection = 1
8380 } IsArrayExpr = NoArrayExpr;
8381 if (AllowArraySection) {
8382 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
8383 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
8384 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
8385 Base = TempASE->getBase()->IgnoreParenImpCasts();
8386 RefExpr = Base;
8387 IsArrayExpr = ArraySubscript;
8388 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
8389 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
8390 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
8391 Base = TempOASE->getBase()->IgnoreParenImpCasts();
8392 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
8393 Base = TempASE->getBase()->IgnoreParenImpCasts();
8394 RefExpr = Base;
8395 IsArrayExpr = OMPArraySection;
8396 }
8397 }
8398 ELoc = RefExpr->getExprLoc();
8399 ERange = RefExpr->getSourceRange();
8400 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008401 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
8402 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
8403 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
8404 (S.getCurrentThisType().isNull() || !ME ||
8405 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
8406 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008407 if (IsArrayExpr != NoArrayExpr)
8408 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
8409 << ERange;
8410 else {
8411 S.Diag(ELoc,
8412 AllowArraySection
8413 ? diag::err_omp_expected_var_name_member_expr_or_array_item
8414 : diag::err_omp_expected_var_name_member_expr)
8415 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
8416 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008417 return std::make_pair(nullptr, false);
8418 }
8419 return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
8420}
8421
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008422OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
8423 SourceLocation StartLoc,
8424 SourceLocation LParenLoc,
8425 SourceLocation EndLoc) {
8426 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00008427 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00008428 for (auto &RefExpr : VarList) {
8429 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008430 SourceLocation ELoc;
8431 SourceRange ERange;
8432 Expr *SimpleRefExpr = RefExpr;
8433 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008434 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008435 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008436 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008437 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008438 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008439 ValueDecl *D = Res.first;
8440 if (!D)
8441 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008442
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008443 QualType Type = D->getType();
8444 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008445
8446 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8447 // A variable that appears in a private clause must not have an incomplete
8448 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008449 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008450 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008451 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008452
Alexey Bataev758e55e2013-09-06 18:03:48 +00008453 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8454 // in a Construct]
8455 // Variables with the predetermined data-sharing attributes may not be
8456 // listed in data-sharing attributes clauses, except for the cases
8457 // listed below. For these exceptions only, listing a predetermined
8458 // variable in a data-sharing attribute clause is allowed and overrides
8459 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008460 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008461 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00008462 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8463 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008464 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008465 continue;
8466 }
8467
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008468 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008469 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00008470 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008471 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
8472 << getOpenMPClauseName(OMPC_private) << Type
8473 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8474 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008475 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008476 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008477 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008478 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008479 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008480 continue;
8481 }
8482
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008483 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
8484 // A list item cannot appear in both a map clause and a data-sharing
8485 // attribute clause on the same construct
8486 if (DSAStack->getCurrentDirective() == OMPD_target) {
Samuel Antao6890b092016-07-28 14:25:09 +00008487 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +00008488 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +00008489 VD, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +00008490 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
8491 OpenMPClauseKind WhereFoundClauseKind) -> bool {
8492 ConflictKind = WhereFoundClauseKind;
8493 return true;
8494 })) {
8495 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008496 << getOpenMPClauseName(OMPC_private)
Samuel Antao6890b092016-07-28 14:25:09 +00008497 << getOpenMPClauseName(ConflictKind)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008498 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8499 ReportOriginalDSA(*this, DSAStack, D, DVar);
8500 continue;
8501 }
8502 }
8503
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008504 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
8505 // A variable of class type (or array thereof) that appears in a private
8506 // clause requires an accessible, unambiguous default constructor for the
8507 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00008508 // Generate helper private variable and initialize it with the default
8509 // value. The address of the original variable is replaced by the address of
8510 // the new private variable in CodeGen. This new variable is not added to
8511 // IdResolver, so the code in the OpenMP region uses original variable for
8512 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008513 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008514 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
8515 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008516 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008517 if (VDPrivate->isInvalidDecl())
8518 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008519 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008520 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008521
Alexey Bataev90c228f2016-02-08 09:29:13 +00008522 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008523 if (!VD && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00008524 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00008525 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008526 Vars.push_back((VD || CurContext->isDependentContext())
8527 ? RefExpr->IgnoreParens()
8528 : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008529 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008530 }
8531
Alexey Bataeved09d242014-05-28 05:53:51 +00008532 if (Vars.empty())
8533 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008534
Alexey Bataev03b340a2014-10-21 03:16:40 +00008535 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8536 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008537}
8538
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008539namespace {
8540class DiagsUninitializedSeveretyRAII {
8541private:
8542 DiagnosticsEngine &Diags;
8543 SourceLocation SavedLoc;
8544 bool IsIgnored;
8545
8546public:
8547 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
8548 bool IsIgnored)
8549 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
8550 if (!IsIgnored) {
8551 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
8552 /*Map*/ diag::Severity::Ignored, Loc);
8553 }
8554 }
8555 ~DiagsUninitializedSeveretyRAII() {
8556 if (!IsIgnored)
8557 Diags.popMappings(SavedLoc);
8558 }
8559};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00008560}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008561
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008562OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
8563 SourceLocation StartLoc,
8564 SourceLocation LParenLoc,
8565 SourceLocation EndLoc) {
8566 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008567 SmallVector<Expr *, 8> PrivateCopies;
8568 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00008569 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008570 bool IsImplicitClause =
8571 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
8572 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
8573
Alexey Bataeved09d242014-05-28 05:53:51 +00008574 for (auto &RefExpr : VarList) {
8575 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008576 SourceLocation ELoc;
8577 SourceRange ERange;
8578 Expr *SimpleRefExpr = RefExpr;
8579 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008580 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008581 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008582 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008583 PrivateCopies.push_back(nullptr);
8584 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008585 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008586 ValueDecl *D = Res.first;
8587 if (!D)
8588 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008589
Alexey Bataev60da77e2016-02-29 05:54:20 +00008590 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00008591 QualType Type = D->getType();
8592 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008593
8594 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8595 // A variable that appears in a private clause must not have an incomplete
8596 // type or a reference type.
8597 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00008598 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008599 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008600 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008601
8602 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
8603 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00008604 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008605 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008606 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008607
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008608 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00008609 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008610 if (!IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008611 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev005248a2016-02-25 05:25:57 +00008612 TopDVar = DVar;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008613 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008614 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
8615 // A list item that specifies a given variable may not appear in more
8616 // than one clause on the same directive, except that a variable may be
8617 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008618 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00008619 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008620 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00008621 << getOpenMPClauseName(DVar.CKind)
8622 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008623 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008624 continue;
8625 }
8626
8627 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8628 // in a Construct]
8629 // Variables with the predetermined data-sharing attributes may not be
8630 // listed in data-sharing attributes clauses, except for the cases
8631 // listed below. For these exceptions only, listing a predetermined
8632 // variable in a data-sharing attribute clause is allowed and overrides
8633 // the variable's predetermined data-sharing attributes.
8634 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8635 // in a Construct, C/C++, p.2]
8636 // Variables with const-qualified type having no mutable member may be
8637 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00008638 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008639 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
8640 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00008641 << getOpenMPClauseName(DVar.CKind)
8642 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008643 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008644 continue;
8645 }
8646
Alexey Bataevf29276e2014-06-18 04:14:57 +00008647 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008648 // OpenMP [2.9.3.4, Restrictions, p.2]
8649 // A list item that is private within a parallel region must not appear
8650 // in a firstprivate clause on a worksharing construct if any of the
8651 // worksharing regions arising from the worksharing construct ever bind
8652 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00008653 if (isOpenMPWorksharingDirective(CurrDir) &&
8654 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008655 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008656 if (DVar.CKind != OMPC_shared &&
8657 (isOpenMPParallelDirective(DVar.DKind) ||
8658 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00008659 Diag(ELoc, diag::err_omp_required_access)
8660 << getOpenMPClauseName(OMPC_firstprivate)
8661 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008662 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008663 continue;
8664 }
8665 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008666 // OpenMP [2.9.3.4, Restrictions, p.3]
8667 // A list item that appears in a reduction clause of a parallel construct
8668 // must not appear in a firstprivate clause on a worksharing or task
8669 // construct if any of the worksharing or task regions arising from the
8670 // worksharing or task construct ever bind to any of the parallel regions
8671 // arising from the parallel construct.
8672 // OpenMP [2.9.3.4, Restrictions, p.4]
8673 // A list item that appears in a reduction clause in worksharing
8674 // construct must not appear in a firstprivate clause in a task construct
8675 // encountered during execution of any of the worksharing regions arising
8676 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +00008677 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008678 DVar = DSAStack->hasInnermostDSA(
8679 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
8680 [](OpenMPDirectiveKind K) -> bool {
8681 return isOpenMPParallelDirective(K) ||
8682 isOpenMPWorksharingDirective(K);
8683 },
8684 false);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008685 if (DVar.CKind == OMPC_reduction &&
8686 (isOpenMPParallelDirective(DVar.DKind) ||
8687 isOpenMPWorksharingDirective(DVar.DKind))) {
8688 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
8689 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008690 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008691 continue;
8692 }
8693 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008694
8695 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
8696 // A list item that is private within a teams region must not appear in a
8697 // firstprivate clause on a distribute construct if any of the distribute
8698 // regions arising from the distribute construct ever bind to any of the
8699 // teams regions arising from the teams construct.
8700 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
8701 // A list item that appears in a reduction clause of a teams construct
8702 // must not appear in a firstprivate clause on a distribute construct if
8703 // any of the distribute regions arising from the distribute construct
8704 // ever bind to any of the teams regions arising from the teams construct.
8705 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8706 // A list item may appear in a firstprivate or lastprivate clause but not
8707 // both.
8708 if (CurrDir == OMPD_distribute) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008709 DVar = DSAStack->hasInnermostDSA(
8710 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_private; },
8711 [](OpenMPDirectiveKind K) -> bool {
8712 return isOpenMPTeamsDirective(K);
8713 },
8714 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008715 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
8716 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008717 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008718 continue;
8719 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008720 DVar = DSAStack->hasInnermostDSA(
8721 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
8722 [](OpenMPDirectiveKind K) -> bool {
8723 return isOpenMPTeamsDirective(K);
8724 },
8725 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008726 if (DVar.CKind == OMPC_reduction &&
8727 isOpenMPTeamsDirective(DVar.DKind)) {
8728 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008729 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008730 continue;
8731 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008732 DVar = DSAStack->getTopDSA(D, false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008733 if (DVar.CKind == OMPC_lastprivate) {
8734 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008735 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008736 continue;
8737 }
8738 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008739 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
8740 // A list item cannot appear in both a map clause and a data-sharing
8741 // attribute clause on the same construct
8742 if (CurrDir == OMPD_target) {
Samuel Antao6890b092016-07-28 14:25:09 +00008743 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +00008744 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +00008745 VD, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +00008746 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
8747 OpenMPClauseKind WhereFoundClauseKind) -> bool {
8748 ConflictKind = WhereFoundClauseKind;
8749 return true;
8750 })) {
8751 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008752 << getOpenMPClauseName(OMPC_firstprivate)
Samuel Antao6890b092016-07-28 14:25:09 +00008753 << getOpenMPClauseName(ConflictKind)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008754 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8755 ReportOriginalDSA(*this, DSAStack, D, DVar);
8756 continue;
8757 }
8758 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008759 }
8760
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008761 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008762 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00008763 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008764 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
8765 << getOpenMPClauseName(OMPC_firstprivate) << Type
8766 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8767 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00008768 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008769 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00008770 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008771 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00008772 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008773 continue;
8774 }
8775
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008776 Type = Type.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008777 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
8778 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008779 // Generate helper private variable and initialize it with the value of the
8780 // original variable. The address of the original variable is replaced by
8781 // the address of the new private variable in the CodeGen. This new variable
8782 // is not added to IdResolver, so the code in the OpenMP region uses
8783 // original variable for proper diagnostics and variable capturing.
8784 Expr *VDInitRefExpr = nullptr;
8785 // For arrays generate initializer for single element and replace it by the
8786 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008787 if (Type->isArrayType()) {
8788 auto VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00008789 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008790 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008791 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008792 ElemType = ElemType.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008793 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008794 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00008795 InitializedEntity Entity =
8796 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008797 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
8798
8799 InitializationSequence InitSeq(*this, Entity, Kind, Init);
8800 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
8801 if (Result.isInvalid())
8802 VDPrivate->setInvalidDecl();
8803 else
8804 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008805 // Remove temp variable declaration.
8806 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008807 } else {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008808 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
8809 ".firstprivate.temp");
8810 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
8811 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00008812 AddInitializerToDecl(VDPrivate,
8813 DefaultLvalueConversion(VDInitRefExpr).get(),
8814 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008815 }
8816 if (VDPrivate->isInvalidDecl()) {
8817 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008818 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008819 diag::note_omp_task_predetermined_firstprivate_here);
8820 }
8821 continue;
8822 }
8823 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008824 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00008825 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
8826 RefExpr->getExprLoc());
8827 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008828 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008829 if (TopDVar.CKind == OMPC_lastprivate)
8830 Ref = TopDVar.PrivateCopy;
8831 else {
Alexey Bataev61205072016-03-02 04:57:40 +00008832 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataev005248a2016-02-25 05:25:57 +00008833 if (!IsOpenMPCapturedDecl(D))
8834 ExprCaptures.push_back(Ref->getDecl());
8835 }
Alexey Bataev417089f2016-02-17 13:19:37 +00008836 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008837 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008838 Vars.push_back((VD || CurContext->isDependentContext())
8839 ? RefExpr->IgnoreParens()
8840 : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008841 PrivateCopies.push_back(VDPrivateRefExpr);
8842 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008843 }
8844
Alexey Bataeved09d242014-05-28 05:53:51 +00008845 if (Vars.empty())
8846 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008847
8848 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008849 Vars, PrivateCopies, Inits,
8850 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008851}
8852
Alexander Musman1bb328c2014-06-04 13:06:39 +00008853OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
8854 SourceLocation StartLoc,
8855 SourceLocation LParenLoc,
8856 SourceLocation EndLoc) {
8857 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00008858 SmallVector<Expr *, 8> SrcExprs;
8859 SmallVector<Expr *, 8> DstExprs;
8860 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +00008861 SmallVector<Decl *, 4> ExprCaptures;
8862 SmallVector<Expr *, 4> ExprPostUpdates;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008863 for (auto &RefExpr : VarList) {
8864 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008865 SourceLocation ELoc;
8866 SourceRange ERange;
8867 Expr *SimpleRefExpr = RefExpr;
8868 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008869 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00008870 // It will be analyzed later.
8871 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00008872 SrcExprs.push_back(nullptr);
8873 DstExprs.push_back(nullptr);
8874 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008875 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00008876 ValueDecl *D = Res.first;
8877 if (!D)
8878 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008879
Alexey Bataev74caaf22016-02-20 04:09:36 +00008880 QualType Type = D->getType();
8881 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008882
8883 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
8884 // A variable that appears in a lastprivate clause must not have an
8885 // incomplete type or a reference type.
8886 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +00008887 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +00008888 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008889 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00008890
8891 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
8892 // in a Construct]
8893 // Variables with the predetermined data-sharing attributes may not be
8894 // listed in data-sharing attributes clauses, except for the cases
8895 // listed below.
Alexey Bataev74caaf22016-02-20 04:09:36 +00008896 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008897 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
8898 DVar.CKind != OMPC_firstprivate &&
8899 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
8900 Diag(ELoc, diag::err_omp_wrong_dsa)
8901 << getOpenMPClauseName(DVar.CKind)
8902 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008903 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008904 continue;
8905 }
8906
Alexey Bataevf29276e2014-06-18 04:14:57 +00008907 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8908 // OpenMP [2.14.3.5, Restrictions, p.2]
8909 // A list item that is private within a parallel region, or that appears in
8910 // the reduction clause of a parallel construct, must not appear in a
8911 // lastprivate clause on a worksharing construct if any of the corresponding
8912 // worksharing regions ever binds to any of the corresponding parallel
8913 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00008914 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00008915 if (isOpenMPWorksharingDirective(CurrDir) &&
8916 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +00008917 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008918 if (DVar.CKind != OMPC_shared) {
8919 Diag(ELoc, diag::err_omp_required_access)
8920 << getOpenMPClauseName(OMPC_lastprivate)
8921 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008922 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008923 continue;
8924 }
8925 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00008926
8927 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8928 // A list item may appear in a firstprivate or lastprivate clause but not
8929 // both.
8930 if (CurrDir == OMPD_distribute) {
8931 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8932 if (DVar.CKind == OMPC_firstprivate) {
8933 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
8934 ReportOriginalDSA(*this, DSAStack, D, DVar);
8935 continue;
8936 }
8937 }
8938
Alexander Musman1bb328c2014-06-04 13:06:39 +00008939 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00008940 // A variable of class type (or array thereof) that appears in a
8941 // lastprivate clause requires an accessible, unambiguous default
8942 // constructor for the class type, unless the list item is also specified
8943 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00008944 // A variable of class type (or array thereof) that appears in a
8945 // lastprivate clause requires an accessible, unambiguous copy assignment
8946 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00008947 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008948 auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008949 Type.getUnqualifiedType(), ".lastprivate.src",
Alexey Bataev74caaf22016-02-20 04:09:36 +00008950 D->hasAttrs() ? &D->getAttrs() : nullptr);
8951 auto *PseudoSrcExpr =
8952 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00008953 auto *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008954 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +00008955 D->hasAttrs() ? &D->getAttrs() : nullptr);
8956 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00008957 // For arrays generate assignment operation for single element and replace
8958 // it by the original array element in CodeGen.
Alexey Bataev74caaf22016-02-20 04:09:36 +00008959 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
Alexey Bataev38e89532015-04-16 04:54:05 +00008960 PseudoDstExpr, PseudoSrcExpr);
8961 if (AssignmentOp.isInvalid())
8962 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +00008963 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00008964 /*DiscardedValue=*/true);
8965 if (AssignmentOp.isInvalid())
8966 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008967
Alexey Bataev74caaf22016-02-20 04:09:36 +00008968 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008969 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008970 if (TopDVar.CKind == OMPC_firstprivate)
8971 Ref = TopDVar.PrivateCopy;
8972 else {
Alexey Bataev61205072016-03-02 04:57:40 +00008973 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00008974 if (!IsOpenMPCapturedDecl(D))
8975 ExprCaptures.push_back(Ref->getDecl());
8976 }
8977 if (TopDVar.CKind == OMPC_firstprivate ||
8978 (!IsOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008979 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008980 ExprResult RefRes = DefaultLvalueConversion(Ref);
8981 if (!RefRes.isUsable())
8982 continue;
8983 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008984 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
8985 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008986 if (!PostUpdateRes.isUsable())
8987 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008988 ExprPostUpdates.push_back(
8989 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008990 }
8991 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008992 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008993 Vars.push_back((VD || CurContext->isDependentContext())
8994 ? RefExpr->IgnoreParens()
8995 : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +00008996 SrcExprs.push_back(PseudoSrcExpr);
8997 DstExprs.push_back(PseudoDstExpr);
8998 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00008999 }
9000
9001 if (Vars.empty())
9002 return nullptr;
9003
9004 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +00009005 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009006 buildPreInits(Context, ExprCaptures),
9007 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +00009008}
9009
Alexey Bataev758e55e2013-09-06 18:03:48 +00009010OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
9011 SourceLocation StartLoc,
9012 SourceLocation LParenLoc,
9013 SourceLocation EndLoc) {
9014 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00009015 for (auto &RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009016 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009017 SourceLocation ELoc;
9018 SourceRange ERange;
9019 Expr *SimpleRefExpr = RefExpr;
9020 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009021 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00009022 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009023 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009024 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009025 ValueDecl *D = Res.first;
9026 if (!D)
9027 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +00009028
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009029 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009030 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9031 // in a Construct]
9032 // Variables with the predetermined data-sharing attributes may not be
9033 // listed in data-sharing attributes clauses, except for the cases
9034 // listed below. For these exceptions only, listing a predetermined
9035 // variable in a data-sharing attribute clause is allowed and overrides
9036 // the variable's predetermined data-sharing attributes.
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009037 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00009038 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
9039 DVar.RefExpr) {
9040 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9041 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009042 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009043 continue;
9044 }
9045
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009046 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009047 if (!VD && IsOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00009048 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009049 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009050 Vars.push_back((VD || !Ref || CurContext->isDependentContext())
9051 ? RefExpr->IgnoreParens()
9052 : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009053 }
9054
Alexey Bataeved09d242014-05-28 05:53:51 +00009055 if (Vars.empty())
9056 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00009057
9058 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
9059}
9060
Alexey Bataevc5e02582014-06-16 07:08:35 +00009061namespace {
9062class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
9063 DSAStackTy *Stack;
9064
9065public:
9066 bool VisitDeclRefExpr(DeclRefExpr *E) {
9067 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009068 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00009069 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
9070 return false;
9071 if (DVar.CKind != OMPC_unknown)
9072 return true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +00009073 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
9074 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
9075 false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00009076 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00009077 return true;
9078 return false;
9079 }
9080 return false;
9081 }
9082 bool VisitStmt(Stmt *S) {
9083 for (auto Child : S->children()) {
9084 if (Child && Visit(Child))
9085 return true;
9086 }
9087 return false;
9088 }
Alexey Bataev23b69422014-06-18 07:08:49 +00009089 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00009090};
Alexey Bataev23b69422014-06-18 07:08:49 +00009091} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00009092
Alexey Bataev60da77e2016-02-29 05:54:20 +00009093namespace {
9094// Transform MemberExpression for specified FieldDecl of current class to
9095// DeclRefExpr to specified OMPCapturedExprDecl.
9096class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
9097 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
9098 ValueDecl *Field;
9099 DeclRefExpr *CapturedExpr;
9100
9101public:
9102 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
9103 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
9104
9105 ExprResult TransformMemberExpr(MemberExpr *E) {
9106 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
9107 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +00009108 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009109 return CapturedExpr;
9110 }
9111 return BaseTransform::TransformMemberExpr(E);
9112 }
9113 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
9114};
9115} // namespace
9116
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009117template <typename T>
9118static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups,
9119 const llvm::function_ref<T(ValueDecl *)> &Gen) {
9120 for (auto &Set : Lookups) {
9121 for (auto *D : Set) {
9122 if (auto Res = Gen(cast<ValueDecl>(D)))
9123 return Res;
9124 }
9125 }
9126 return T();
9127}
9128
9129static ExprResult
9130buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
9131 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
9132 const DeclarationNameInfo &ReductionId, QualType Ty,
9133 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
9134 if (ReductionIdScopeSpec.isInvalid())
9135 return ExprError();
9136 SmallVector<UnresolvedSet<8>, 4> Lookups;
9137 if (S) {
9138 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
9139 Lookup.suppressDiagnostics();
9140 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
9141 auto *D = Lookup.getRepresentativeDecl();
9142 do {
9143 S = S->getParent();
9144 } while (S && !S->isDeclScope(D));
9145 if (S)
9146 S = S->getParent();
9147 Lookups.push_back(UnresolvedSet<8>());
9148 Lookups.back().append(Lookup.begin(), Lookup.end());
9149 Lookup.clear();
9150 }
9151 } else if (auto *ULE =
9152 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
9153 Lookups.push_back(UnresolvedSet<8>());
9154 Decl *PrevD = nullptr;
David Majnemer9d168222016-08-05 17:44:54 +00009155 for (auto *D : ULE->decls()) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009156 if (D == PrevD)
9157 Lookups.push_back(UnresolvedSet<8>());
9158 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
9159 Lookups.back().addDecl(DRD);
9160 PrevD = D;
9161 }
9162 }
9163 if (Ty->isDependentType() || Ty->isInstantiationDependentType() ||
9164 Ty->containsUnexpandedParameterPack() ||
9165 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool {
9166 return !D->isInvalidDecl() &&
9167 (D->getType()->isDependentType() ||
9168 D->getType()->isInstantiationDependentType() ||
9169 D->getType()->containsUnexpandedParameterPack());
9170 })) {
9171 UnresolvedSet<8> ResSet;
9172 for (auto &Set : Lookups) {
9173 ResSet.append(Set.begin(), Set.end());
9174 // The last item marks the end of all declarations at the specified scope.
9175 ResSet.addDecl(Set[Set.size() - 1]);
9176 }
9177 return UnresolvedLookupExpr::Create(
9178 SemaRef.Context, /*NamingClass=*/nullptr,
9179 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
9180 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
9181 }
9182 if (auto *VD = filterLookupForUDR<ValueDecl *>(
9183 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
9184 if (!D->isInvalidDecl() &&
9185 SemaRef.Context.hasSameType(D->getType(), Ty))
9186 return D;
9187 return nullptr;
9188 }))
9189 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
9190 if (auto *VD = filterLookupForUDR<ValueDecl *>(
9191 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
9192 if (!D->isInvalidDecl() &&
9193 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
9194 !Ty.isMoreQualifiedThan(D->getType()))
9195 return D;
9196 return nullptr;
9197 })) {
9198 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
9199 /*DetectVirtual=*/false);
9200 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
9201 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
9202 VD->getType().getUnqualifiedType()))) {
9203 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
9204 /*DiagID=*/0) !=
9205 Sema::AR_inaccessible) {
9206 SemaRef.BuildBasePathArray(Paths, BasePath);
9207 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
9208 }
9209 }
9210 }
9211 }
9212 if (ReductionIdScopeSpec.isSet()) {
9213 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
9214 return ExprError();
9215 }
9216 return ExprEmpty();
9217}
9218
Alexey Bataevc5e02582014-06-16 07:08:35 +00009219OMPClause *Sema::ActOnOpenMPReductionClause(
9220 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
9221 SourceLocation ColonLoc, SourceLocation EndLoc,
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009222 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
9223 ArrayRef<Expr *> UnresolvedReductions) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00009224 auto DN = ReductionId.getName();
9225 auto OOK = DN.getCXXOverloadedOperator();
9226 BinaryOperatorKind BOK = BO_Comma;
9227
9228 // OpenMP [2.14.3.6, reduction clause]
9229 // C
9230 // reduction-identifier is either an identifier or one of the following
9231 // operators: +, -, *, &, |, ^, && and ||
9232 // C++
9233 // reduction-identifier is either an id-expression or one of the following
9234 // operators: +, -, *, &, |, ^, && and ||
9235 // FIXME: Only 'min' and 'max' identifiers are supported for now.
9236 switch (OOK) {
9237 case OO_Plus:
9238 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009239 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009240 break;
9241 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009242 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009243 break;
9244 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009245 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009246 break;
9247 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009248 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009249 break;
9250 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009251 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009252 break;
9253 case OO_AmpAmp:
9254 BOK = BO_LAnd;
9255 break;
9256 case OO_PipePipe:
9257 BOK = BO_LOr;
9258 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009259 case OO_New:
9260 case OO_Delete:
9261 case OO_Array_New:
9262 case OO_Array_Delete:
9263 case OO_Slash:
9264 case OO_Percent:
9265 case OO_Tilde:
9266 case OO_Exclaim:
9267 case OO_Equal:
9268 case OO_Less:
9269 case OO_Greater:
9270 case OO_LessEqual:
9271 case OO_GreaterEqual:
9272 case OO_PlusEqual:
9273 case OO_MinusEqual:
9274 case OO_StarEqual:
9275 case OO_SlashEqual:
9276 case OO_PercentEqual:
9277 case OO_CaretEqual:
9278 case OO_AmpEqual:
9279 case OO_PipeEqual:
9280 case OO_LessLess:
9281 case OO_GreaterGreater:
9282 case OO_LessLessEqual:
9283 case OO_GreaterGreaterEqual:
9284 case OO_EqualEqual:
9285 case OO_ExclaimEqual:
9286 case OO_PlusPlus:
9287 case OO_MinusMinus:
9288 case OO_Comma:
9289 case OO_ArrowStar:
9290 case OO_Arrow:
9291 case OO_Call:
9292 case OO_Subscript:
9293 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00009294 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009295 case NUM_OVERLOADED_OPERATORS:
9296 llvm_unreachable("Unexpected reduction identifier");
9297 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00009298 if (auto II = DN.getAsIdentifierInfo()) {
9299 if (II->isStr("max"))
9300 BOK = BO_GT;
9301 else if (II->isStr("min"))
9302 BOK = BO_LT;
9303 }
9304 break;
9305 }
9306 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009307 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +00009308 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009309 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009310
9311 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009312 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009313 SmallVector<Expr *, 8> LHSs;
9314 SmallVector<Expr *, 8> RHSs;
9315 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev61205072016-03-02 04:57:40 +00009316 SmallVector<Decl *, 4> ExprCaptures;
9317 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009318 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
9319 bool FirstIter = true;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009320 for (auto RefExpr : VarList) {
9321 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +00009322 // OpenMP [2.1, C/C++]
9323 // A list item is a variable or array section, subject to the restrictions
9324 // specified in Section 2.4 on page 42 and in each of the sections
9325 // describing clauses and directives for which a list appears.
9326 // OpenMP [2.14.3.3, Restrictions, p.1]
9327 // A variable that is part of another variable (as an array or
9328 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009329 if (!FirstIter && IR != ER)
9330 ++IR;
9331 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009332 SourceLocation ELoc;
9333 SourceRange ERange;
9334 Expr *SimpleRefExpr = RefExpr;
9335 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9336 /*AllowArraySection=*/true);
9337 if (Res.second) {
9338 // It will be analyzed later.
9339 Vars.push_back(RefExpr);
9340 Privates.push_back(nullptr);
9341 LHSs.push_back(nullptr);
9342 RHSs.push_back(nullptr);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009343 // Try to find 'declare reduction' corresponding construct before using
9344 // builtin/overloaded operators.
9345 QualType Type = Context.DependentTy;
9346 CXXCastPath BasePath;
9347 ExprResult DeclareReductionRef = buildDeclareReductionRef(
9348 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
9349 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
9350 if (CurContext->isDependentContext() &&
9351 (DeclareReductionRef.isUnset() ||
9352 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
9353 ReductionOps.push_back(DeclareReductionRef.get());
9354 else
9355 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00009356 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00009357 ValueDecl *D = Res.first;
9358 if (!D)
9359 continue;
9360
Alexey Bataeva1764212015-09-30 09:22:36 +00009361 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009362 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
9363 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
9364 if (ASE)
Alexey Bataev31300ed2016-02-04 11:27:03 +00009365 Type = ASE->getType().getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009366 else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009367 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
9368 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
9369 Type = ATy->getElementType();
9370 else
9371 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +00009372 Type = Type.getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009373 } else
9374 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
9375 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +00009376
Alexey Bataevc5e02582014-06-16 07:08:35 +00009377 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9378 // A variable that appears in a private clause must not have an incomplete
9379 // type or a reference type.
9380 if (RequireCompleteType(ELoc, Type,
9381 diag::err_omp_reduction_incomplete_type))
9382 continue;
9383 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00009384 // A list item that appears in a reduction clause must not be
9385 // const-qualified.
9386 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009387 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00009388 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009389 if (!ASE && !OASE) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009390 bool IsDecl = !VD ||
9391 VD->isThisDeclarationADefinition(Context) ==
9392 VarDecl::DeclarationOnly;
9393 Diag(D->getLocation(),
Alexey Bataeva1764212015-09-30 09:22:36 +00009394 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +00009395 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +00009396 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00009397 continue;
9398 }
9399 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
9400 // If a list-item is a reference type then it must bind to the same object
9401 // for all threads of the team.
Alexey Bataev60da77e2016-02-29 05:54:20 +00009402 if (!ASE && !OASE && VD) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009403 VarDecl *VDDef = VD->getDefinition();
David Sheinkman92589992016-10-04 14:41:36 +00009404 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009405 DSARefChecker Check(DSAStack);
9406 if (Check.Visit(VDDef->getInit())) {
9407 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
9408 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
9409 continue;
9410 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00009411 }
9412 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009413
Alexey Bataevc5e02582014-06-16 07:08:35 +00009414 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
9415 // in a Construct]
9416 // Variables with the predetermined data-sharing attributes may not be
9417 // listed in data-sharing attributes clauses, except for the cases
9418 // listed below. For these exceptions only, listing a predetermined
9419 // variable in a data-sharing attribute clause is allowed and overrides
9420 // the variable's predetermined data-sharing attributes.
9421 // OpenMP [2.14.3.6, Restrictions, p.3]
9422 // Any number of reduction clauses can be specified on the directive,
9423 // but a list item can appear only once in the reduction clauses for that
9424 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00009425 DSAStackTy::DSAVarData DVar;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009426 DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009427 if (DVar.CKind == OMPC_reduction) {
9428 Diag(ELoc, diag::err_omp_once_referenced)
9429 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009430 if (DVar.RefExpr)
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009431 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009432 } else if (DVar.CKind != OMPC_unknown) {
9433 Diag(ELoc, diag::err_omp_wrong_dsa)
9434 << getOpenMPClauseName(DVar.CKind)
9435 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009436 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009437 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009438 }
9439
9440 // OpenMP [2.14.3.6, Restrictions, p.1]
9441 // A list item that appears in a reduction clause of a worksharing
9442 // construct must be shared in the parallel regions to which any of the
9443 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009444 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
9445 if (isOpenMPWorksharingDirective(CurrDir) &&
9446 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009447 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009448 if (DVar.CKind != OMPC_shared) {
9449 Diag(ELoc, diag::err_omp_required_access)
9450 << getOpenMPClauseName(OMPC_reduction)
9451 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009452 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009453 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00009454 }
9455 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009456
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009457 // Try to find 'declare reduction' corresponding construct before using
9458 // builtin/overloaded operators.
9459 CXXCastPath BasePath;
9460 ExprResult DeclareReductionRef = buildDeclareReductionRef(
9461 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
9462 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
9463 if (DeclareReductionRef.isInvalid())
9464 continue;
9465 if (CurContext->isDependentContext() &&
9466 (DeclareReductionRef.isUnset() ||
9467 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
9468 Vars.push_back(RefExpr);
9469 Privates.push_back(nullptr);
9470 LHSs.push_back(nullptr);
9471 RHSs.push_back(nullptr);
9472 ReductionOps.push_back(DeclareReductionRef.get());
9473 continue;
9474 }
9475 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
9476 // Not allowed reduction identifier is found.
9477 Diag(ReductionId.getLocStart(),
9478 diag::err_omp_unknown_reduction_identifier)
9479 << Type << ReductionIdRange;
9480 continue;
9481 }
9482
9483 // OpenMP [2.14.3.6, reduction clause, Restrictions]
9484 // The type of a list item that appears in a reduction clause must be valid
9485 // for the reduction-identifier. For a max or min reduction in C, the type
9486 // of the list item must be an allowed arithmetic data type: char, int,
9487 // float, double, or _Bool, possibly modified with long, short, signed, or
9488 // unsigned. For a max or min reduction in C++, the type of the list item
9489 // must be an allowed arithmetic data type: char, wchar_t, int, float,
9490 // double, or bool, possibly modified with long, short, signed, or unsigned.
9491 if (DeclareReductionRef.isUnset()) {
9492 if ((BOK == BO_GT || BOK == BO_LT) &&
9493 !(Type->isScalarType() ||
9494 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
9495 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
9496 << getLangOpts().CPlusPlus;
9497 if (!ASE && !OASE) {
9498 bool IsDecl = !VD ||
9499 VD->isThisDeclarationADefinition(Context) ==
9500 VarDecl::DeclarationOnly;
9501 Diag(D->getLocation(),
9502 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9503 << D;
9504 }
9505 continue;
9506 }
9507 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
9508 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
9509 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
9510 if (!ASE && !OASE) {
9511 bool IsDecl = !VD ||
9512 VD->isThisDeclarationADefinition(Context) ==
9513 VarDecl::DeclarationOnly;
9514 Diag(D->getLocation(),
9515 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9516 << D;
9517 }
9518 continue;
9519 }
9520 }
9521
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009522 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009523 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
Alexey Bataev60da77e2016-02-29 05:54:20 +00009524 D->hasAttrs() ? &D->getAttrs() : nullptr);
9525 auto *RHSVD = buildVarDecl(*this, ELoc, Type, D->getName(),
9526 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009527 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00009528 if (OASE ||
Alexey Bataev60da77e2016-02-29 05:54:20 +00009529 (!ASE &&
9530 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
David Majnemer9d168222016-08-05 17:44:54 +00009531 // For arrays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009532 // Create pseudo array type for private copy. The size for this array will
9533 // be generated during codegen.
9534 // For array subscripts or single variables Private Ty is the same as Type
9535 // (type of the variable or single array element).
9536 PrivateTy = Context.getVariableArrayType(
9537 Type, new (Context) OpaqueValueExpr(SourceLocation(),
9538 Context.getSizeType(), VK_RValue),
9539 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +00009540 } else if (!ASE && !OASE &&
9541 Context.getAsArrayType(D->getType().getNonReferenceType()))
9542 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009543 // Private copy.
Alexey Bataev60da77e2016-02-29 05:54:20 +00009544 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, D->getName(),
9545 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009546 // Add initializer for private variable.
9547 Expr *Init = nullptr;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009548 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
9549 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
9550 if (DeclareReductionRef.isUsable()) {
9551 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
9552 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
9553 if (DRD->getInitializer()) {
9554 Init = DRDRef;
9555 RHSVD->setInit(DRDRef);
9556 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009557 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009558 } else {
9559 switch (BOK) {
9560 case BO_Add:
9561 case BO_Xor:
9562 case BO_Or:
9563 case BO_LOr:
9564 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
9565 if (Type->isScalarType() || Type->isAnyComplexType())
9566 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
9567 break;
9568 case BO_Mul:
9569 case BO_LAnd:
9570 if (Type->isScalarType() || Type->isAnyComplexType()) {
9571 // '*' and '&&' reduction ops - initializer is '1'.
9572 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00009573 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009574 break;
9575 case BO_And: {
9576 // '&' reduction op - initializer is '~0'.
9577 QualType OrigType = Type;
9578 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
9579 Type = ComplexTy->getElementType();
9580 if (Type->isRealFloatingType()) {
9581 llvm::APFloat InitValue =
9582 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
9583 /*isIEEE=*/true);
9584 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
9585 Type, ELoc);
9586 } else if (Type->isScalarType()) {
9587 auto Size = Context.getTypeSize(Type);
9588 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
9589 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
9590 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
9591 }
9592 if (Init && OrigType->isAnyComplexType()) {
9593 // Init = 0xFFFF + 0xFFFFi;
9594 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
9595 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
9596 }
9597 Type = OrigType;
9598 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009599 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009600 case BO_LT:
9601 case BO_GT: {
9602 // 'min' reduction op - initializer is 'Largest representable number in
9603 // the reduction list item type'.
9604 // 'max' reduction op - initializer is 'Least representable number in
9605 // the reduction list item type'.
9606 if (Type->isIntegerType() || Type->isPointerType()) {
9607 bool IsSigned = Type->hasSignedIntegerRepresentation();
9608 auto Size = Context.getTypeSize(Type);
9609 QualType IntTy =
9610 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
9611 llvm::APInt InitValue =
9612 (BOK != BO_LT)
9613 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
9614 : llvm::APInt::getMinValue(Size)
9615 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
9616 : llvm::APInt::getMaxValue(Size);
9617 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
9618 if (Type->isPointerType()) {
9619 // Cast to pointer type.
9620 auto CastExpr = BuildCStyleCastExpr(
9621 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
9622 SourceLocation(), Init);
9623 if (CastExpr.isInvalid())
9624 continue;
9625 Init = CastExpr.get();
9626 }
9627 } else if (Type->isRealFloatingType()) {
9628 llvm::APFloat InitValue = llvm::APFloat::getLargest(
9629 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
9630 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
9631 Type, ELoc);
9632 }
9633 break;
9634 }
9635 case BO_PtrMemD:
9636 case BO_PtrMemI:
9637 case BO_MulAssign:
9638 case BO_Div:
9639 case BO_Rem:
9640 case BO_Sub:
9641 case BO_Shl:
9642 case BO_Shr:
9643 case BO_LE:
9644 case BO_GE:
9645 case BO_EQ:
9646 case BO_NE:
9647 case BO_AndAssign:
9648 case BO_XorAssign:
9649 case BO_OrAssign:
9650 case BO_Assign:
9651 case BO_AddAssign:
9652 case BO_SubAssign:
9653 case BO_DivAssign:
9654 case BO_RemAssign:
9655 case BO_ShlAssign:
9656 case BO_ShrAssign:
9657 case BO_Comma:
9658 llvm_unreachable("Unexpected reduction operation");
9659 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009660 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009661 if (Init && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009662 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
9663 /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009664 } else if (!Init)
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009665 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009666 if (RHSVD->isInvalidDecl())
9667 continue;
9668 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009669 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
9670 << ReductionIdRange;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009671 bool IsDecl =
9672 !VD ||
9673 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9674 Diag(D->getLocation(),
9675 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9676 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009677 continue;
9678 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009679 // Store initializer for single element in private copy. Will be used during
9680 // codegen.
9681 PrivateVD->setInit(RHSVD->getInit());
9682 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009683 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009684 ExprResult ReductionOp;
9685 if (DeclareReductionRef.isUsable()) {
9686 QualType RedTy = DeclareReductionRef.get()->getType();
9687 QualType PtrRedTy = Context.getPointerType(RedTy);
9688 ExprResult LHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
9689 ExprResult RHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
9690 if (!BasePath.empty()) {
9691 LHS = DefaultLvalueConversion(LHS.get());
9692 RHS = DefaultLvalueConversion(RHS.get());
9693 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
9694 CK_UncheckedDerivedToBase, LHS.get(),
9695 &BasePath, LHS.get()->getValueKind());
9696 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
9697 CK_UncheckedDerivedToBase, RHS.get(),
9698 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009699 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009700 FunctionProtoType::ExtProtoInfo EPI;
9701 QualType Params[] = {PtrRedTy, PtrRedTy};
9702 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
9703 auto *OVE = new (Context) OpaqueValueExpr(
9704 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
9705 DefaultLvalueConversion(DeclareReductionRef.get()).get());
9706 Expr *Args[] = {LHS.get(), RHS.get()};
9707 ReductionOp = new (Context)
9708 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
9709 } else {
9710 ReductionOp = BuildBinOp(DSAStack->getCurScope(),
9711 ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE);
9712 if (ReductionOp.isUsable()) {
9713 if (BOK != BO_LT && BOK != BO_GT) {
9714 ReductionOp =
9715 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
9716 BO_Assign, LHSDRE, ReductionOp.get());
9717 } else {
9718 auto *ConditionalOp = new (Context) ConditionalOperator(
9719 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
9720 RHSDRE, Type, VK_LValue, OK_Ordinary);
9721 ReductionOp =
9722 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
9723 BO_Assign, LHSDRE, ConditionalOp);
9724 }
9725 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
9726 }
9727 if (ReductionOp.isInvalid())
9728 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009729 }
9730
Alexey Bataev60da77e2016-02-29 05:54:20 +00009731 DeclRefExpr *Ref = nullptr;
9732 Expr *VarsExpr = RefExpr->IgnoreParens();
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009733 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009734 if (ASE || OASE) {
9735 TransformExprToCaptures RebuildToCapture(*this, D);
9736 VarsExpr =
9737 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
9738 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +00009739 } else {
9740 VarsExpr = Ref =
9741 buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +00009742 }
9743 if (!IsOpenMPCapturedDecl(D)) {
9744 ExprCaptures.push_back(Ref->getDecl());
9745 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
9746 ExprResult RefRes = DefaultLvalueConversion(Ref);
9747 if (!RefRes.isUsable())
9748 continue;
9749 ExprResult PostUpdateRes =
9750 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
9751 SimpleRefExpr, RefRes.get());
9752 if (!PostUpdateRes.isUsable())
9753 continue;
9754 ExprPostUpdates.push_back(
9755 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +00009756 }
9757 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00009758 }
9759 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
9760 Vars.push_back(VarsExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009761 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009762 LHSs.push_back(LHSDRE);
9763 RHSs.push_back(RHSDRE);
9764 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009765 }
9766
9767 if (Vars.empty())
9768 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +00009769
Alexey Bataevc5e02582014-06-16 07:08:35 +00009770 return OMPReductionClause::Create(
9771 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009772 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009773 LHSs, RHSs, ReductionOps, buildPreInits(Context, ExprCaptures),
9774 buildPostUpdate(*this, ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +00009775}
9776
Alexey Bataevecba70f2016-04-12 11:02:11 +00009777bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
9778 SourceLocation LinLoc) {
9779 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
9780 LinKind == OMPC_LINEAR_unknown) {
9781 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
9782 return true;
9783 }
9784 return false;
9785}
9786
9787bool Sema::CheckOpenMPLinearDecl(ValueDecl *D, SourceLocation ELoc,
9788 OpenMPLinearClauseKind LinKind,
9789 QualType Type) {
9790 auto *VD = dyn_cast_or_null<VarDecl>(D);
9791 // A variable must not have an incomplete type or a reference type.
9792 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
9793 return true;
9794 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
9795 !Type->isReferenceType()) {
9796 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
9797 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
9798 return true;
9799 }
9800 Type = Type.getNonReferenceType();
9801
9802 // A list item must not be const-qualified.
9803 if (Type.isConstant(Context)) {
9804 Diag(ELoc, diag::err_omp_const_variable)
9805 << getOpenMPClauseName(OMPC_linear);
9806 if (D) {
9807 bool IsDecl =
9808 !VD ||
9809 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9810 Diag(D->getLocation(),
9811 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9812 << D;
9813 }
9814 return true;
9815 }
9816
9817 // A list item must be of integral or pointer type.
9818 Type = Type.getUnqualifiedType().getCanonicalType();
9819 const auto *Ty = Type.getTypePtrOrNull();
9820 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
9821 !Ty->isPointerType())) {
9822 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
9823 if (D) {
9824 bool IsDecl =
9825 !VD ||
9826 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9827 Diag(D->getLocation(),
9828 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9829 << D;
9830 }
9831 return true;
9832 }
9833 return false;
9834}
9835
Alexey Bataev182227b2015-08-20 10:54:39 +00009836OMPClause *Sema::ActOnOpenMPLinearClause(
9837 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
9838 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
9839 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00009840 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009841 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00009842 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +00009843 SmallVector<Decl *, 4> ExprCaptures;
9844 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +00009845 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +00009846 LinKind = OMPC_LINEAR_val;
Alexey Bataeved09d242014-05-28 05:53:51 +00009847 for (auto &RefExpr : VarList) {
9848 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009849 SourceLocation ELoc;
9850 SourceRange ERange;
9851 Expr *SimpleRefExpr = RefExpr;
9852 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9853 /*AllowArraySection=*/false);
9854 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +00009855 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009856 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009857 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00009858 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00009859 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009860 ValueDecl *D = Res.first;
9861 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +00009862 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +00009863
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009864 QualType Type = D->getType();
9865 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +00009866
9867 // OpenMP [2.14.3.7, linear clause]
9868 // A list-item cannot appear in more than one linear clause.
9869 // A list-item that appears in a linear clause cannot appear in any
9870 // other data-sharing attribute clause.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009871 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00009872 if (DVar.RefExpr) {
9873 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9874 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009875 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00009876 continue;
9877 }
9878
Alexey Bataevecba70f2016-04-12 11:02:11 +00009879 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +00009880 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00009881 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +00009882
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009883 // Build private copy of original var.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009884 auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(),
9885 D->hasAttrs() ? &D->getAttrs() : nullptr);
9886 auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +00009887 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009888 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009889 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009890 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009891 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev78849fb2016-03-09 09:49:00 +00009892 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
9893 if (!IsOpenMPCapturedDecl(D)) {
9894 ExprCaptures.push_back(Ref->getDecl());
9895 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
9896 ExprResult RefRes = DefaultLvalueConversion(Ref);
9897 if (!RefRes.isUsable())
9898 continue;
9899 ExprResult PostUpdateRes =
9900 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
9901 SimpleRefExpr, RefRes.get());
9902 if (!PostUpdateRes.isUsable())
9903 continue;
9904 ExprPostUpdates.push_back(
9905 IgnoredValueConversions(PostUpdateRes.get()).get());
9906 }
9907 }
9908 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009909 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009910 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009911 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009912 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009913 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009914 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
9915 auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
9916
9917 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009918 Vars.push_back((VD || CurContext->isDependentContext())
9919 ? RefExpr->IgnoreParens()
9920 : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009921 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00009922 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00009923 }
9924
9925 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009926 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00009927
9928 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00009929 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00009930 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
9931 !Step->isInstantiationDependent() &&
9932 !Step->containsUnexpandedParameterPack()) {
9933 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00009934 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00009935 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009936 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00009937 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00009938
Alexander Musman3276a272015-03-21 10:12:56 +00009939 // Build var to save the step value.
9940 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00009941 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00009942 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00009943 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00009944 ExprResult CalcStep =
9945 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009946 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00009947
Alexander Musman8dba6642014-04-22 13:09:42 +00009948 // Warn about zero linear step (it would be probably better specified as
9949 // making corresponding variables 'const').
9950 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00009951 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
9952 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00009953 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
9954 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00009955 if (!IsConstant && CalcStep.isUsable()) {
9956 // Calculate the step beforehand instead of doing this on each iteration.
9957 // (This is not used if the number of iterations may be kfold-ed).
9958 CalcStepExpr = CalcStep.get();
9959 }
Alexander Musman8dba6642014-04-22 13:09:42 +00009960 }
9961
Alexey Bataev182227b2015-08-20 10:54:39 +00009962 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
9963 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009964 StepExpr, CalcStepExpr,
9965 buildPreInits(Context, ExprCaptures),
9966 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +00009967}
9968
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009969static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
9970 Expr *NumIterations, Sema &SemaRef,
9971 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +00009972 // Walk the vars and build update/final expressions for the CodeGen.
9973 SmallVector<Expr *, 8> Updates;
9974 SmallVector<Expr *, 8> Finals;
9975 Expr *Step = Clause.getStep();
9976 Expr *CalcStep = Clause.getCalcStep();
9977 // OpenMP [2.14.3.7, linear clause]
9978 // If linear-step is not specified it is assumed to be 1.
9979 if (Step == nullptr)
9980 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009981 else if (CalcStep) {
Alexander Musman3276a272015-03-21 10:12:56 +00009982 Step = cast<BinaryOperator>(CalcStep)->getLHS();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009983 }
Alexander Musman3276a272015-03-21 10:12:56 +00009984 bool HasErrors = false;
9985 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009986 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009987 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00009988 for (auto &RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009989 SourceLocation ELoc;
9990 SourceRange ERange;
9991 Expr *SimpleRefExpr = RefExpr;
9992 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange,
9993 /*AllowArraySection=*/false);
9994 ValueDecl *D = Res.first;
9995 if (Res.second || !D) {
9996 Updates.push_back(nullptr);
9997 Finals.push_back(nullptr);
9998 HasErrors = true;
9999 continue;
10000 }
10001 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) {
10002 D = cast<MemberExpr>(CED->getInit()->IgnoreParenImpCasts())
10003 ->getMemberDecl();
10004 }
10005 auto &&Info = Stack->isLoopControlVariable(D);
Alexander Musman3276a272015-03-21 10:12:56 +000010006 Expr *InitExpr = *CurInit;
10007
10008 // Build privatized reference to the current linear var.
David Majnemer9d168222016-08-05 17:44:54 +000010009 auto *DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000010010 Expr *CapturedRef;
10011 if (LinKind == OMPC_LINEAR_uval)
10012 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
10013 else
10014 CapturedRef =
10015 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
10016 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
10017 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +000010018
10019 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000010020 ExprResult Update;
10021 if (!Info.first) {
10022 Update =
10023 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
10024 InitExpr, IV, Step, /* Subtract */ false);
10025 } else
10026 Update = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +000010027 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
10028 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +000010029
10030 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000010031 ExprResult Final;
10032 if (!Info.first) {
10033 Final = BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
10034 InitExpr, NumIterations, Step,
10035 /* Subtract */ false);
10036 } else
10037 Final = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +000010038 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
10039 /*DiscardedValue=*/true);
Alexey Bataev5dff95c2016-04-22 03:56:56 +000010040
Alexander Musman3276a272015-03-21 10:12:56 +000010041 if (!Update.isUsable() || !Final.isUsable()) {
10042 Updates.push_back(nullptr);
10043 Finals.push_back(nullptr);
10044 HasErrors = true;
10045 } else {
10046 Updates.push_back(Update.get());
10047 Finals.push_back(Final.get());
10048 }
Richard Trieucc3949d2016-02-18 22:34:54 +000010049 ++CurInit;
10050 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +000010051 }
10052 Clause.setUpdates(Updates);
10053 Clause.setFinals(Finals);
10054 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +000010055}
10056
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010057OMPClause *Sema::ActOnOpenMPAlignedClause(
10058 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
10059 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
10060
10061 SmallVector<Expr *, 8> Vars;
10062 for (auto &RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +000010063 assert(RefExpr && "NULL expr in OpenMP linear clause.");
10064 SourceLocation ELoc;
10065 SourceRange ERange;
10066 Expr *SimpleRefExpr = RefExpr;
10067 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
10068 /*AllowArraySection=*/false);
10069 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010070 // It will be analyzed later.
10071 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010072 }
Alexey Bataev1efd1662016-03-29 10:59:56 +000010073 ValueDecl *D = Res.first;
10074 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010075 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010076
Alexey Bataev1efd1662016-03-29 10:59:56 +000010077 QualType QType = D->getType();
10078 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010079
10080 // OpenMP [2.8.1, simd construct, Restrictions]
10081 // The type of list items appearing in the aligned clause must be
10082 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010083 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010084 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +000010085 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010086 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +000010087 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010088 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +000010089 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010090 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +000010091 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010092 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +000010093 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010094 continue;
10095 }
10096
10097 // OpenMP [2.8.1, simd construct, Restrictions]
10098 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev1efd1662016-03-29 10:59:56 +000010099 if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +000010100 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010101 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
10102 << getOpenMPClauseName(OMPC_aligned);
10103 continue;
10104 }
10105
Alexey Bataev1efd1662016-03-29 10:59:56 +000010106 DeclRefExpr *Ref = nullptr;
10107 if (!VD && IsOpenMPCapturedDecl(D))
10108 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
10109 Vars.push_back(DefaultFunctionArrayConversion(
10110 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
10111 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010112 }
10113
10114 // OpenMP [2.8.1, simd construct, Description]
10115 // The parameter of the aligned clause, alignment, must be a constant
10116 // positive integer expression.
10117 // If no optional parameter is specified, implementation-defined default
10118 // alignments for SIMD instructions on the target platforms are assumed.
10119 if (Alignment != nullptr) {
10120 ExprResult AlignResult =
10121 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
10122 if (AlignResult.isInvalid())
10123 return nullptr;
10124 Alignment = AlignResult.get();
10125 }
10126 if (Vars.empty())
10127 return nullptr;
10128
10129 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
10130 EndLoc, Vars, Alignment);
10131}
10132
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010133OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
10134 SourceLocation StartLoc,
10135 SourceLocation LParenLoc,
10136 SourceLocation EndLoc) {
10137 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010138 SmallVector<Expr *, 8> SrcExprs;
10139 SmallVector<Expr *, 8> DstExprs;
10140 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +000010141 for (auto &RefExpr : VarList) {
10142 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
10143 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010144 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000010145 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010146 SrcExprs.push_back(nullptr);
10147 DstExprs.push_back(nullptr);
10148 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010149 continue;
10150 }
10151
Alexey Bataeved09d242014-05-28 05:53:51 +000010152 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010153 // OpenMP [2.1, C/C++]
10154 // A list item is a variable name.
10155 // OpenMP [2.14.4.1, Restrictions, p.1]
10156 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +000010157 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010158 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000010159 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
10160 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010161 continue;
10162 }
10163
10164 Decl *D = DE->getDecl();
10165 VarDecl *VD = cast<VarDecl>(D);
10166
10167 QualType Type = VD->getType();
10168 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
10169 // It will be analyzed later.
10170 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010171 SrcExprs.push_back(nullptr);
10172 DstExprs.push_back(nullptr);
10173 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010174 continue;
10175 }
10176
10177 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
10178 // A list item that appears in a copyin clause must be threadprivate.
10179 if (!DSAStack->isThreadPrivate(VD)) {
10180 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +000010181 << getOpenMPClauseName(OMPC_copyin)
10182 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010183 continue;
10184 }
10185
10186 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
10187 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +000010188 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010189 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010190 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000010191 auto *SrcVD =
10192 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
10193 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +000010194 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010195 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
10196 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000010197 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
10198 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010199 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010200 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010201 // For arrays generate assignment operation for single element and replace
10202 // it by the original array element in CodeGen.
10203 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
10204 PseudoDstExpr, PseudoSrcExpr);
10205 if (AssignmentOp.isInvalid())
10206 continue;
10207 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
10208 /*DiscardedValue=*/true);
10209 if (AssignmentOp.isInvalid())
10210 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010211
10212 DSAStack->addDSA(VD, DE, OMPC_copyin);
10213 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010214 SrcExprs.push_back(PseudoSrcExpr);
10215 DstExprs.push_back(PseudoDstExpr);
10216 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010217 }
10218
Alexey Bataeved09d242014-05-28 05:53:51 +000010219 if (Vars.empty())
10220 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010221
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010222 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
10223 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010224}
10225
Alexey Bataevbae9a792014-06-27 10:37:06 +000010226OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
10227 SourceLocation StartLoc,
10228 SourceLocation LParenLoc,
10229 SourceLocation EndLoc) {
10230 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +000010231 SmallVector<Expr *, 8> SrcExprs;
10232 SmallVector<Expr *, 8> DstExprs;
10233 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +000010234 for (auto &RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +000010235 assert(RefExpr && "NULL expr in OpenMP linear clause.");
10236 SourceLocation ELoc;
10237 SourceRange ERange;
10238 Expr *SimpleRefExpr = RefExpr;
10239 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
10240 /*AllowArraySection=*/false);
10241 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000010242 // It will be analyzed later.
10243 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000010244 SrcExprs.push_back(nullptr);
10245 DstExprs.push_back(nullptr);
10246 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010247 }
Alexey Bataeve122da12016-03-17 10:50:17 +000010248 ValueDecl *D = Res.first;
10249 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +000010250 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000010251
Alexey Bataeve122da12016-03-17 10:50:17 +000010252 QualType Type = D->getType();
10253 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010254
10255 // OpenMP [2.14.4.2, Restrictions, p.2]
10256 // A list item that appears in a copyprivate clause may not appear in a
10257 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +000010258 if (!VD || !DSAStack->isThreadPrivate(VD)) {
10259 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +000010260 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
10261 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000010262 Diag(ELoc, diag::err_omp_wrong_dsa)
10263 << getOpenMPClauseName(DVar.CKind)
10264 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve122da12016-03-17 10:50:17 +000010265 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010266 continue;
10267 }
10268
10269 // OpenMP [2.11.4.2, Restrictions, p.1]
10270 // All list items that appear in a copyprivate clause must be either
10271 // threadprivate or private in the enclosing context.
10272 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +000010273 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010274 if (DVar.CKind == OMPC_shared) {
10275 Diag(ELoc, diag::err_omp_required_access)
10276 << getOpenMPClauseName(OMPC_copyprivate)
10277 << "threadprivate or private in the enclosing context";
Alexey Bataeve122da12016-03-17 10:50:17 +000010278 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010279 continue;
10280 }
10281 }
10282 }
10283
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010284 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000010285 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010286 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010287 << getOpenMPClauseName(OMPC_copyprivate) << Type
10288 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010289 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +000010290 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010291 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +000010292 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010293 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +000010294 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010295 continue;
10296 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010297
Alexey Bataevbae9a792014-06-27 10:37:06 +000010298 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
10299 // A variable of class type (or array thereof) that appears in a
10300 // copyin clause requires an accessible, unambiguous copy assignment
10301 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010302 Type = Context.getBaseElementType(Type.getNonReferenceType())
10303 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +000010304 auto *SrcVD =
Alexey Bataeve122da12016-03-17 10:50:17 +000010305 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src",
10306 D->hasAttrs() ? &D->getAttrs() : nullptr);
10307 auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
Alexey Bataev420d45b2015-04-14 05:11:24 +000010308 auto *DstVD =
Alexey Bataeve122da12016-03-17 10:50:17 +000010309 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst",
10310 D->hasAttrs() ? &D->getAttrs() : nullptr);
David Majnemer9d168222016-08-05 17:44:54 +000010311 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataeve122da12016-03-17 10:50:17 +000010312 auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
Alexey Bataeva63048e2015-03-23 06:18:07 +000010313 PseudoDstExpr, PseudoSrcExpr);
10314 if (AssignmentOp.isInvalid())
10315 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +000010316 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +000010317 /*DiscardedValue=*/true);
10318 if (AssignmentOp.isInvalid())
10319 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000010320
10321 // No need to mark vars as copyprivate, they are already threadprivate or
10322 // implicitly private.
Alexey Bataeve122da12016-03-17 10:50:17 +000010323 assert(VD || IsOpenMPCapturedDecl(D));
10324 Vars.push_back(
10325 VD ? RefExpr->IgnoreParens()
10326 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +000010327 SrcExprs.push_back(PseudoSrcExpr);
10328 DstExprs.push_back(PseudoDstExpr);
10329 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +000010330 }
10331
10332 if (Vars.empty())
10333 return nullptr;
10334
Alexey Bataeva63048e2015-03-23 06:18:07 +000010335 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10336 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010337}
10338
Alexey Bataev6125da92014-07-21 11:26:11 +000010339OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
10340 SourceLocation StartLoc,
10341 SourceLocation LParenLoc,
10342 SourceLocation EndLoc) {
10343 if (VarList.empty())
10344 return nullptr;
10345
10346 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
10347}
Alexey Bataevdea47612014-07-23 07:46:59 +000010348
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010349OMPClause *
10350Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
10351 SourceLocation DepLoc, SourceLocation ColonLoc,
10352 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
10353 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +000010354 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010355 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +000010356 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000010357 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +000010358 return nullptr;
10359 }
10360 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010361 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
10362 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +000010363 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010364 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000010365 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
10366 /*Last=*/OMPC_DEPEND_unknown, Except)
10367 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010368 return nullptr;
10369 }
10370 SmallVector<Expr *, 8> Vars;
Alexey Bataev8b427062016-05-25 12:36:08 +000010371 DSAStackTy::OperatorOffsetTy OpsOffs;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010372 llvm::APSInt DepCounter(/*BitWidth=*/32);
10373 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
10374 if (DepKind == OMPC_DEPEND_sink) {
10375 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
10376 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
10377 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010378 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010379 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010380 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
10381 DSAStack->getParentOrderedRegionParam()) {
10382 for (auto &RefExpr : VarList) {
10383 assert(RefExpr && "NULL expr in OpenMP shared clause.");
Alexey Bataev8b427062016-05-25 12:36:08 +000010384 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010385 // It will be analyzed later.
10386 Vars.push_back(RefExpr);
10387 continue;
10388 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010389
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010390 SourceLocation ELoc = RefExpr->getExprLoc();
10391 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
10392 if (DepKind == OMPC_DEPEND_sink) {
10393 if (DepCounter >= TotalDepCount) {
10394 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
10395 continue;
10396 }
10397 ++DepCounter;
10398 // OpenMP [2.13.9, Summary]
10399 // depend(dependence-type : vec), where dependence-type is:
10400 // 'sink' and where vec is the iteration vector, which has the form:
10401 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
10402 // where n is the value specified by the ordered clause in the loop
10403 // directive, xi denotes the loop iteration variable of the i-th nested
10404 // loop associated with the loop directive, and di is a constant
10405 // non-negative integer.
Alexey Bataev8b427062016-05-25 12:36:08 +000010406 if (CurContext->isDependentContext()) {
10407 // It will be analyzed later.
10408 Vars.push_back(RefExpr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010409 continue;
10410 }
Alexey Bataev8b427062016-05-25 12:36:08 +000010411 SimpleExpr = SimpleExpr->IgnoreImplicit();
10412 OverloadedOperatorKind OOK = OO_None;
10413 SourceLocation OOLoc;
10414 Expr *LHS = SimpleExpr;
10415 Expr *RHS = nullptr;
10416 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
10417 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
10418 OOLoc = BO->getOperatorLoc();
10419 LHS = BO->getLHS()->IgnoreParenImpCasts();
10420 RHS = BO->getRHS()->IgnoreParenImpCasts();
10421 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
10422 OOK = OCE->getOperator();
10423 OOLoc = OCE->getOperatorLoc();
10424 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
10425 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
10426 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
10427 OOK = MCE->getMethodDecl()
10428 ->getNameInfo()
10429 .getName()
10430 .getCXXOverloadedOperator();
10431 OOLoc = MCE->getCallee()->getExprLoc();
10432 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
10433 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
10434 }
10435 SourceLocation ELoc;
10436 SourceRange ERange;
10437 auto Res = getPrivateItem(*this, LHS, ELoc, ERange,
10438 /*AllowArraySection=*/false);
10439 if (Res.second) {
10440 // It will be analyzed later.
10441 Vars.push_back(RefExpr);
10442 }
10443 ValueDecl *D = Res.first;
10444 if (!D)
10445 continue;
10446
10447 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
10448 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
10449 continue;
10450 }
10451 if (RHS) {
10452 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
10453 RHS, OMPC_depend, /*StrictlyPositive=*/false);
10454 if (RHSRes.isInvalid())
10455 continue;
10456 }
10457 if (!CurContext->isDependentContext() &&
10458 DSAStack->getParentOrderedRegionParam() &&
10459 DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
10460 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
10461 << DSAStack->getParentLoopControlVariable(
10462 DepCounter.getZExtValue());
10463 continue;
10464 }
10465 OpsOffs.push_back({RHS, OOK});
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010466 } else {
10467 // OpenMP [2.11.1.1, Restrictions, p.3]
10468 // A variable that is part of another variable (such as a field of a
10469 // structure) but is not an array element or an array section cannot
10470 // appear in a depend clause.
10471 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
10472 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
10473 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
10474 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
10475 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev31300ed2016-02-04 11:27:03 +000010476 (ASE &&
10477 !ASE->getBase()
10478 ->getType()
10479 .getNonReferenceType()
10480 ->isPointerType() &&
10481 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000010482 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
10483 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010484 continue;
10485 }
10486 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010487 Vars.push_back(RefExpr->IgnoreParenImpCasts());
10488 }
10489
10490 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
10491 TotalDepCount > VarList.size() &&
10492 DSAStack->getParentOrderedRegionParam()) {
10493 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
10494 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
10495 }
10496 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
10497 Vars.empty())
10498 return nullptr;
10499 }
Alexey Bataev8b427062016-05-25 12:36:08 +000010500 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10501 DepKind, DepLoc, ColonLoc, Vars);
10502 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source)
10503 DSAStack->addDoacrossDependClause(C, OpsOffs);
10504 return C;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010505}
Michael Wonge710d542015-08-07 16:16:36 +000010506
10507OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
10508 SourceLocation LParenLoc,
10509 SourceLocation EndLoc) {
10510 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +000010511
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010512 // OpenMP [2.9.1, Restrictions]
10513 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010514 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
10515 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010516 return nullptr;
10517
Michael Wonge710d542015-08-07 16:16:36 +000010518 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10519}
Kelvin Li0bff7af2015-11-23 05:32:03 +000010520
10521static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
10522 DSAStackTy *Stack, CXXRecordDecl *RD) {
10523 if (!RD || RD->isInvalidDecl())
10524 return true;
10525
Alexey Bataevc9bd03d2015-12-17 06:55:08 +000010526 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
10527 if (auto *CTD = CTSD->getSpecializedTemplate())
10528 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010529 auto QTy = SemaRef.Context.getRecordType(RD);
10530 if (RD->isDynamicClass()) {
10531 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10532 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
10533 return false;
10534 }
10535 auto *DC = RD;
10536 bool IsCorrect = true;
10537 for (auto *I : DC->decls()) {
10538 if (I) {
10539 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
10540 if (MD->isStatic()) {
10541 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10542 SemaRef.Diag(MD->getLocation(),
10543 diag::note_omp_static_member_in_target);
10544 IsCorrect = false;
10545 }
10546 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
10547 if (VD->isStaticDataMember()) {
10548 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10549 SemaRef.Diag(VD->getLocation(),
10550 diag::note_omp_static_member_in_target);
10551 IsCorrect = false;
10552 }
10553 }
10554 }
10555 }
10556
10557 for (auto &I : RD->bases()) {
10558 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
10559 I.getType()->getAsCXXRecordDecl()))
10560 IsCorrect = false;
10561 }
10562 return IsCorrect;
10563}
10564
10565static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
10566 DSAStackTy *Stack, QualType QTy) {
10567 NamedDecl *ND;
10568 if (QTy->isIncompleteType(&ND)) {
10569 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
10570 return false;
10571 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
David Majnemer9d168222016-08-05 17:44:54 +000010572 if (!RD->isInvalidDecl() && !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
Kelvin Li0bff7af2015-11-23 05:32:03 +000010573 return false;
10574 }
10575 return true;
10576}
10577
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010578/// \brief Return true if it can be proven that the provided array expression
10579/// (array section or array subscript) does NOT specify the whole size of the
10580/// array whose base type is \a BaseQTy.
10581static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
10582 const Expr *E,
10583 QualType BaseQTy) {
10584 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
10585
10586 // If this is an array subscript, it refers to the whole size if the size of
10587 // the dimension is constant and equals 1. Also, an array section assumes the
10588 // format of an array subscript if no colon is used.
10589 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
10590 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
10591 return ATy->getSize().getSExtValue() != 1;
10592 // Size can't be evaluated statically.
10593 return false;
10594 }
10595
10596 assert(OASE && "Expecting array section if not an array subscript.");
10597 auto *LowerBound = OASE->getLowerBound();
10598 auto *Length = OASE->getLength();
10599
10600 // If there is a lower bound that does not evaluates to zero, we are not
David Majnemer9d168222016-08-05 17:44:54 +000010601 // covering the whole dimension.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010602 if (LowerBound) {
10603 llvm::APSInt ConstLowerBound;
10604 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
10605 return false; // Can't get the integer value as a constant.
10606 if (ConstLowerBound.getSExtValue())
10607 return true;
10608 }
10609
10610 // If we don't have a length we covering the whole dimension.
10611 if (!Length)
10612 return false;
10613
10614 // If the base is a pointer, we don't have a way to get the size of the
10615 // pointee.
10616 if (BaseQTy->isPointerType())
10617 return false;
10618
10619 // We can only check if the length is the same as the size of the dimension
10620 // if we have a constant array.
10621 auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
10622 if (!CATy)
10623 return false;
10624
10625 llvm::APSInt ConstLength;
10626 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
10627 return false; // Can't get the integer value as a constant.
10628
10629 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
10630}
10631
10632// Return true if it can be proven that the provided array expression (array
10633// section or array subscript) does NOT specify a single element of the array
10634// whose base type is \a BaseQTy.
10635static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
David Majnemer9d168222016-08-05 17:44:54 +000010636 const Expr *E,
10637 QualType BaseQTy) {
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010638 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
10639
10640 // An array subscript always refer to a single element. Also, an array section
10641 // assumes the format of an array subscript if no colon is used.
10642 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
10643 return false;
10644
10645 assert(OASE && "Expecting array section if not an array subscript.");
10646 auto *Length = OASE->getLength();
10647
10648 // If we don't have a length we have to check if the array has unitary size
10649 // for this dimension. Also, we should always expect a length if the base type
10650 // is pointer.
10651 if (!Length) {
10652 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
10653 return ATy->getSize().getSExtValue() != 1;
10654 // We cannot assume anything.
10655 return false;
10656 }
10657
10658 // Check if the length evaluates to 1.
10659 llvm::APSInt ConstLength;
10660 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
10661 return false; // Can't get the integer value as a constant.
10662
10663 return ConstLength.getSExtValue() != 1;
10664}
10665
Samuel Antao661c0902016-05-26 17:39:58 +000010666// Return the expression of the base of the mappable expression or null if it
10667// cannot be determined and do all the necessary checks to see if the expression
10668// is valid as a standalone mappable expression. In the process, record all the
Samuel Antao90927002016-04-26 14:54:23 +000010669// components of the expression.
10670static Expr *CheckMapClauseExpressionBase(
10671 Sema &SemaRef, Expr *E,
Samuel Antao661c0902016-05-26 17:39:58 +000010672 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
10673 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010674 SourceLocation ELoc = E->getExprLoc();
10675 SourceRange ERange = E->getSourceRange();
10676
10677 // The base of elements of list in a map clause have to be either:
10678 // - a reference to variable or field.
10679 // - a member expression.
10680 // - an array expression.
10681 //
10682 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
10683 // reference to 'r'.
10684 //
10685 // If we have:
10686 //
10687 // struct SS {
10688 // Bla S;
10689 // foo() {
10690 // #pragma omp target map (S.Arr[:12]);
10691 // }
10692 // }
10693 //
10694 // We want to retrieve the member expression 'this->S';
10695
10696 Expr *RelevantExpr = nullptr;
10697
Samuel Antao5de996e2016-01-22 20:21:36 +000010698 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
10699 // If a list item is an array section, it must specify contiguous storage.
10700 //
10701 // For this restriction it is sufficient that we make sure only references
10702 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010703 // exist except in the rightmost expression (unless they cover the whole
10704 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +000010705 //
10706 // r.ArrS[3:5].Arr[6:7]
10707 //
10708 // r.ArrS[3:5].x
10709 //
10710 // but these would be valid:
10711 // r.ArrS[3].Arr[6:7]
10712 //
10713 // r.ArrS[3].x
10714
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010715 bool AllowUnitySizeArraySection = true;
10716 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +000010717
Dmitry Polukhin644a9252016-03-11 07:58:34 +000010718 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010719 E = E->IgnoreParenImpCasts();
10720
10721 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
10722 if (!isa<VarDecl>(CurE->getDecl()))
10723 break;
10724
10725 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010726
10727 // If we got a reference to a declaration, we should not expect any array
10728 // section before that.
10729 AllowUnitySizeArraySection = false;
10730 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010731
10732 // Record the component.
10733 CurComponents.push_back(OMPClauseMappableExprCommon::MappableComponent(
10734 CurE, CurE->getDecl()));
Samuel Antao5de996e2016-01-22 20:21:36 +000010735 continue;
10736 }
10737
10738 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
10739 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
10740
10741 if (isa<CXXThisExpr>(BaseE))
10742 // We found a base expression: this->Val.
10743 RelevantExpr = CurE;
10744 else
10745 E = BaseE;
10746
10747 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
10748 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
10749 << CurE->getSourceRange();
10750 break;
10751 }
10752
10753 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
10754
10755 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
10756 // A bit-field cannot appear in a map clause.
10757 //
10758 if (FD->isBitField()) {
Samuel Antao661c0902016-05-26 17:39:58 +000010759 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
10760 << CurE->getSourceRange() << getOpenMPClauseName(CKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010761 break;
10762 }
10763
10764 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10765 // If the type of a list item is a reference to a type T then the type
10766 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010767 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010768
10769 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
10770 // A list item cannot be a variable that is a member of a structure with
10771 // a union type.
10772 //
10773 if (auto *RT = CurType->getAs<RecordType>())
10774 if (RT->isUnionType()) {
10775 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
10776 << CurE->getSourceRange();
10777 break;
10778 }
10779
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010780 // If we got a member expression, we should not expect any array section
10781 // before that:
10782 //
10783 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
10784 // If a list item is an element of a structure, only the rightmost symbol
10785 // of the variable reference can be an array section.
10786 //
10787 AllowUnitySizeArraySection = false;
10788 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010789
10790 // Record the component.
10791 CurComponents.push_back(
10792 OMPClauseMappableExprCommon::MappableComponent(CurE, FD));
Samuel Antao5de996e2016-01-22 20:21:36 +000010793 continue;
10794 }
10795
10796 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
10797 E = CurE->getBase()->IgnoreParenImpCasts();
10798
10799 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
10800 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
10801 << 0 << CurE->getSourceRange();
10802 break;
10803 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010804
10805 // If we got an array subscript that express the whole dimension we
10806 // can have any array expressions before. If it only expressing part of
10807 // the dimension, we can only have unitary-size array expressions.
10808 if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
10809 E->getType()))
10810 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010811
10812 // Record the component - we don't have any declaration associated.
10813 CurComponents.push_back(
10814 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +000010815 continue;
10816 }
10817
10818 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010819 E = CurE->getBase()->IgnoreParenImpCasts();
10820
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010821 auto CurType =
10822 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
10823
Samuel Antao5de996e2016-01-22 20:21:36 +000010824 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10825 // If the type of a list item is a reference to a type T then the type
10826 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +000010827 if (CurType->isReferenceType())
10828 CurType = CurType->getPointeeType();
10829
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010830 bool IsPointer = CurType->isAnyPointerType();
10831
10832 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010833 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
10834 << 0 << CurE->getSourceRange();
10835 break;
10836 }
10837
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010838 bool NotWhole =
10839 CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
10840 bool NotUnity =
10841 CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
10842
Samuel Antaodab51bb2016-07-18 23:22:11 +000010843 if (AllowWholeSizeArraySection) {
10844 // Any array section is currently allowed. Allowing a whole size array
10845 // section implies allowing a unity array section as well.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010846 //
10847 // If this array section refers to the whole dimension we can still
10848 // accept other array sections before this one, except if the base is a
10849 // pointer. Otherwise, only unitary sections are accepted.
10850 if (NotWhole || IsPointer)
10851 AllowWholeSizeArraySection = false;
Samuel Antaodab51bb2016-07-18 23:22:11 +000010852 } else if (AllowUnitySizeArraySection && NotUnity) {
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010853 // A unity or whole array section is not allowed and that is not
10854 // compatible with the properties of the current array section.
10855 SemaRef.Diag(
10856 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
10857 << CurE->getSourceRange();
10858 break;
10859 }
Samuel Antao90927002016-04-26 14:54:23 +000010860
10861 // Record the component - we don't have any declaration associated.
10862 CurComponents.push_back(
10863 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +000010864 continue;
10865 }
10866
10867 // If nothing else worked, this is not a valid map clause expression.
10868 SemaRef.Diag(ELoc,
10869 diag::err_omp_expected_named_var_member_or_array_expression)
10870 << ERange;
10871 break;
10872 }
10873
10874 return RelevantExpr;
10875}
10876
10877// Return true if expression E associated with value VD has conflicts with other
10878// map information.
Samuel Antao90927002016-04-26 14:54:23 +000010879static bool CheckMapConflicts(
10880 Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD, Expr *E,
10881 bool CurrentRegionOnly,
Samuel Antao661c0902016-05-26 17:39:58 +000010882 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
10883 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010884 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +000010885 SourceLocation ELoc = E->getExprLoc();
10886 SourceRange ERange = E->getSourceRange();
10887
10888 // In order to easily check the conflicts we need to match each component of
10889 // the expression under test with the components of the expressions that are
10890 // already in the stack.
10891
Samuel Antao5de996e2016-01-22 20:21:36 +000010892 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000010893 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000010894 "Map clause expression with unexpected base!");
10895
10896 // Variables to help detecting enclosing problems in data environment nests.
10897 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +000010898 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000010899
Samuel Antao90927002016-04-26 14:54:23 +000010900 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
10901 VD, CurrentRegionOnly,
10902 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
Samuel Antao6890b092016-07-28 14:25:09 +000010903 StackComponents,
10904 OpenMPClauseKind) -> bool {
Samuel Antao90927002016-04-26 14:54:23 +000010905
Samuel Antao5de996e2016-01-22 20:21:36 +000010906 assert(!StackComponents.empty() &&
10907 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000010908 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000010909 "Map clause expression with unexpected base!");
10910
Samuel Antao90927002016-04-26 14:54:23 +000010911 // The whole expression in the stack.
10912 auto *RE = StackComponents.front().getAssociatedExpression();
10913
Samuel Antao5de996e2016-01-22 20:21:36 +000010914 // Expressions must start from the same base. Here we detect at which
10915 // point both expressions diverge from each other and see if we can
10916 // detect if the memory referred to both expressions is contiguous and
10917 // do not overlap.
10918 auto CI = CurComponents.rbegin();
10919 auto CE = CurComponents.rend();
10920 auto SI = StackComponents.rbegin();
10921 auto SE = StackComponents.rend();
10922 for (; CI != CE && SI != SE; ++CI, ++SI) {
10923
10924 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
10925 // At most one list item can be an array item derived from a given
10926 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +000010927 if (CurrentRegionOnly &&
10928 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
10929 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
10930 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
10931 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
10932 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +000010933 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +000010934 << CI->getAssociatedExpression()->getSourceRange();
10935 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
10936 diag::note_used_here)
10937 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +000010938 return true;
10939 }
10940
10941 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +000010942 if (CI->getAssociatedExpression()->getStmtClass() !=
10943 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +000010944 break;
10945
10946 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +000010947 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +000010948 break;
10949 }
Kelvin Li9f645ae2016-07-18 22:49:16 +000010950 // Check if the extra components of the expressions in the enclosing
10951 // data environment are redundant for the current base declaration.
10952 // If they are, the maps completely overlap, which is legal.
10953 for (; SI != SE; ++SI) {
10954 QualType Type;
10955 if (auto *ASE =
David Majnemer9d168222016-08-05 17:44:54 +000010956 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
Kelvin Li9f645ae2016-07-18 22:49:16 +000010957 Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
David Majnemer9d168222016-08-05 17:44:54 +000010958 } else if (auto *OASE = dyn_cast<OMPArraySectionExpr>(
10959 SI->getAssociatedExpression())) {
Kelvin Li9f645ae2016-07-18 22:49:16 +000010960 auto *E = OASE->getBase()->IgnoreParenImpCasts();
10961 Type =
10962 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
10963 }
10964 if (Type.isNull() || Type->isAnyPointerType() ||
10965 CheckArrayExpressionDoesNotReferToWholeSize(
10966 SemaRef, SI->getAssociatedExpression(), Type))
10967 break;
10968 }
Samuel Antao5de996e2016-01-22 20:21:36 +000010969
10970 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10971 // List items of map clauses in the same construct must not share
10972 // original storage.
10973 //
10974 // If the expressions are exactly the same or one is a subset of the
10975 // other, it means they are sharing storage.
10976 if (CI == CE && SI == SE) {
10977 if (CurrentRegionOnly) {
Samuel Antao661c0902016-05-26 17:39:58 +000010978 if (CKind == OMPC_map)
10979 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10980 else {
Samuel Antaoec172c62016-05-26 17:49:04 +000010981 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000010982 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
10983 << ERange;
10984 }
Samuel Antao5de996e2016-01-22 20:21:36 +000010985 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10986 << RE->getSourceRange();
10987 return true;
10988 } else {
10989 // If we find the same expression in the enclosing data environment,
10990 // that is legal.
10991 IsEnclosedByDataEnvironmentExpr = true;
10992 return false;
10993 }
10994 }
10995
Samuel Antao90927002016-04-26 14:54:23 +000010996 QualType DerivedType =
10997 std::prev(CI)->getAssociatedDeclaration()->getType();
10998 SourceLocation DerivedLoc =
10999 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000011000
11001 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
11002 // If the type of a list item is a reference to a type T then the type
11003 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000011004 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000011005
11006 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
11007 // A variable for which the type is pointer and an array section
11008 // derived from that variable must not appear as list items of map
11009 // clauses of the same construct.
11010 //
11011 // Also, cover one of the cases in:
11012 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
11013 // If any part of the original storage of a list item has corresponding
11014 // storage in the device data environment, all of the original storage
11015 // must have corresponding storage in the device data environment.
11016 //
11017 if (DerivedType->isAnyPointerType()) {
11018 if (CI == CE || SI == SE) {
11019 SemaRef.Diag(
11020 DerivedLoc,
11021 diag::err_omp_pointer_mapped_along_with_derived_section)
11022 << DerivedLoc;
11023 } else {
11024 assert(CI != CE && SI != SE);
11025 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
11026 << DerivedLoc;
11027 }
11028 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
11029 << RE->getSourceRange();
11030 return true;
11031 }
11032
11033 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
11034 // List items of map clauses in the same construct must not share
11035 // original storage.
11036 //
11037 // An expression is a subset of the other.
11038 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
Samuel Antao661c0902016-05-26 17:39:58 +000011039 if (CKind == OMPC_map)
11040 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
11041 else {
Samuel Antaoec172c62016-05-26 17:49:04 +000011042 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000011043 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
11044 << ERange;
11045 }
Samuel Antao5de996e2016-01-22 20:21:36 +000011046 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
11047 << RE->getSourceRange();
11048 return true;
11049 }
11050
11051 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000011052 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000011053 if (!CurrentRegionOnly && SI != SE)
11054 EnclosingExpr = RE;
11055
11056 // The current expression is a subset of the expression in the data
11057 // environment.
11058 IsEnclosedByDataEnvironmentExpr |=
11059 (!CurrentRegionOnly && CI != CE && SI == SE);
11060
11061 return false;
11062 });
11063
11064 if (CurrentRegionOnly)
11065 return FoundError;
11066
11067 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
11068 // If any part of the original storage of a list item has corresponding
11069 // storage in the device data environment, all of the original storage must
11070 // have corresponding storage in the device data environment.
11071 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
11072 // If a list item is an element of a structure, and a different element of
11073 // the structure has a corresponding list item in the device data environment
11074 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000011075 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000011076 // data environment prior to the task encountering the construct.
11077 //
11078 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
11079 SemaRef.Diag(ELoc,
11080 diag::err_omp_original_storage_is_shared_and_does_not_contain)
11081 << ERange;
11082 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
11083 << EnclosingExpr->getSourceRange();
11084 return true;
11085 }
11086
11087 return FoundError;
11088}
11089
Samuel Antao661c0902016-05-26 17:39:58 +000011090namespace {
11091// Utility struct that gathers all the related lists associated with a mappable
11092// expression.
11093struct MappableVarListInfo final {
11094 // The list of expressions.
11095 ArrayRef<Expr *> VarList;
11096 // The list of processed expressions.
11097 SmallVector<Expr *, 16> ProcessedVarList;
11098 // The mappble components for each expression.
11099 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
11100 // The base declaration of the variable.
11101 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
11102
11103 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
11104 // We have a list of components and base declarations for each entry in the
11105 // variable list.
11106 VarComponents.reserve(VarList.size());
11107 VarBaseDeclarations.reserve(VarList.size());
11108 }
11109};
11110}
11111
11112// Check the validity of the provided variable list for the provided clause kind
11113// \a CKind. In the check process the valid expressions, and mappable expression
11114// components and variables are extracted and used to fill \a Vars,
11115// \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and
11116// \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'.
11117static void
11118checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS,
11119 OpenMPClauseKind CKind, MappableVarListInfo &MVLI,
11120 SourceLocation StartLoc,
11121 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
11122 bool IsMapTypeImplicit = false) {
Samuel Antaoec172c62016-05-26 17:49:04 +000011123 // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
11124 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
Samuel Antao661c0902016-05-26 17:39:58 +000011125 "Unexpected clause kind with mappable expressions!");
Kelvin Li0bff7af2015-11-23 05:32:03 +000011126
Samuel Antao90927002016-04-26 14:54:23 +000011127 // Keep track of the mappable components and base declarations in this clause.
11128 // Each entry in the list is going to have a list of components associated. We
11129 // record each set of the components so that we can build the clause later on.
11130 // In the end we should have the same amount of declarations and component
11131 // lists.
Samuel Antao90927002016-04-26 14:54:23 +000011132
Samuel Antao661c0902016-05-26 17:39:58 +000011133 for (auto &RE : MVLI.VarList) {
Samuel Antaoec172c62016-05-26 17:49:04 +000011134 assert(RE && "Null expr in omp to/from/map clause");
Kelvin Li0bff7af2015-11-23 05:32:03 +000011135 SourceLocation ELoc = RE->getExprLoc();
11136
Kelvin Li0bff7af2015-11-23 05:32:03 +000011137 auto *VE = RE->IgnoreParenLValueCasts();
11138
11139 if (VE->isValueDependent() || VE->isTypeDependent() ||
11140 VE->isInstantiationDependent() ||
11141 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000011142 // We can only analyze this information once the missing information is
11143 // resolved.
Samuel Antao661c0902016-05-26 17:39:58 +000011144 MVLI.ProcessedVarList.push_back(RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011145 continue;
11146 }
11147
11148 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000011149
Samuel Antao5de996e2016-01-22 20:21:36 +000011150 if (!RE->IgnoreParenImpCasts()->isLValue()) {
Samuel Antao661c0902016-05-26 17:39:58 +000011151 SemaRef.Diag(ELoc,
11152 diag::err_omp_expected_named_var_member_or_array_expression)
Samuel Antao5de996e2016-01-22 20:21:36 +000011153 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000011154 continue;
11155 }
11156
Samuel Antao90927002016-04-26 14:54:23 +000011157 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
11158 ValueDecl *CurDeclaration = nullptr;
11159
11160 // Obtain the array or member expression bases if required. Also, fill the
11161 // components array with all the components identified in the process.
Samuel Antao661c0902016-05-26 17:39:58 +000011162 auto *BE =
11163 CheckMapClauseExpressionBase(SemaRef, SimpleExpr, CurComponents, CKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000011164 if (!BE)
11165 continue;
11166
Samuel Antao90927002016-04-26 14:54:23 +000011167 assert(!CurComponents.empty() &&
11168 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000011169
Samuel Antao90927002016-04-26 14:54:23 +000011170 // For the following checks, we rely on the base declaration which is
11171 // expected to be associated with the last component. The declaration is
11172 // expected to be a variable or a field (if 'this' is being mapped).
11173 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
11174 assert(CurDeclaration && "Null decl on map clause.");
11175 assert(
11176 CurDeclaration->isCanonicalDecl() &&
11177 "Expecting components to have associated only canonical declarations.");
11178
11179 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
11180 auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000011181
11182 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000011183 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000011184
11185 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
Samuel Antao661c0902016-05-26 17:39:58 +000011186 // threadprivate variables cannot appear in a map clause.
11187 // OpenMP 4.5 [2.10.5, target update Construct]
11188 // threadprivate variables cannot appear in a from clause.
11189 if (VD && DSAS->isThreadPrivate(VD)) {
11190 auto DVar = DSAS->getTopDSA(VD, false);
11191 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
11192 << getOpenMPClauseName(CKind);
11193 ReportOriginalDSA(SemaRef, DSAS, VD, DVar);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011194 continue;
11195 }
11196
Samuel Antao5de996e2016-01-22 20:21:36 +000011197 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
11198 // A list item cannot appear in both a map clause and a data-sharing
11199 // attribute clause on the same construct.
Kelvin Li0bff7af2015-11-23 05:32:03 +000011200
Samuel Antao5de996e2016-01-22 20:21:36 +000011201 // Check conflicts with other map clause expressions. We check the conflicts
11202 // with the current construct separately from the enclosing data
Samuel Antao661c0902016-05-26 17:39:58 +000011203 // environment, because the restrictions are different. We only have to
11204 // check conflicts across regions for the map clauses.
11205 if (CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
11206 /*CurrentRegionOnly=*/true, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000011207 break;
Samuel Antao661c0902016-05-26 17:39:58 +000011208 if (CKind == OMPC_map &&
11209 CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
11210 /*CurrentRegionOnly=*/false, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000011211 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000011212
Samuel Antao661c0902016-05-26 17:39:58 +000011213 // OpenMP 4.5 [2.10.5, target update Construct]
Samuel Antao5de996e2016-01-22 20:21:36 +000011214 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
11215 // If the type of a list item is a reference to a type T then the type will
11216 // be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000011217 QualType Type = CurDeclaration->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000011218
Samuel Antao661c0902016-05-26 17:39:58 +000011219 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
11220 // A list item in a to or from clause must have a mappable type.
Samuel Antao5de996e2016-01-22 20:21:36 +000011221 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000011222 // A list item must have a mappable type.
Samuel Antao661c0902016-05-26 17:39:58 +000011223 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
11224 DSAS, Type))
Kelvin Li0bff7af2015-11-23 05:32:03 +000011225 continue;
11226
Samuel Antao661c0902016-05-26 17:39:58 +000011227 if (CKind == OMPC_map) {
11228 // target enter data
11229 // OpenMP [2.10.2, Restrictions, p. 99]
11230 // A map-type must be specified in all map clauses and must be either
11231 // to or alloc.
11232 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
11233 if (DKind == OMPD_target_enter_data &&
11234 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
11235 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
11236 << (IsMapTypeImplicit ? 1 : 0)
11237 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
11238 << getOpenMPDirectiveName(DKind);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000011239 continue;
11240 }
Samuel Antao661c0902016-05-26 17:39:58 +000011241
11242 // target exit_data
11243 // OpenMP [2.10.3, Restrictions, p. 102]
11244 // A map-type must be specified in all map clauses and must be either
11245 // from, release, or delete.
11246 if (DKind == OMPD_target_exit_data &&
11247 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
11248 MapType == OMPC_MAP_delete)) {
11249 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
11250 << (IsMapTypeImplicit ? 1 : 0)
11251 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
11252 << getOpenMPDirectiveName(DKind);
11253 continue;
11254 }
11255
11256 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
11257 // A list item cannot appear in both a map clause and a data-sharing
11258 // attribute clause on the same construct
11259 if (DKind == OMPD_target && VD) {
11260 auto DVar = DSAS->getTopDSA(VD, false);
11261 if (isOpenMPPrivate(DVar.CKind)) {
Samuel Antao6890b092016-07-28 14:25:09 +000011262 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Samuel Antao661c0902016-05-26 17:39:58 +000011263 << getOpenMPClauseName(DVar.CKind)
Samuel Antao6890b092016-07-28 14:25:09 +000011264 << getOpenMPClauseName(OMPC_map)
Samuel Antao661c0902016-05-26 17:39:58 +000011265 << getOpenMPDirectiveName(DSAS->getCurrentDirective());
11266 ReportOriginalDSA(SemaRef, DSAS, CurDeclaration, DVar);
11267 continue;
11268 }
11269 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +000011270 }
11271
Samuel Antao90927002016-04-26 14:54:23 +000011272 // Save the current expression.
Samuel Antao661c0902016-05-26 17:39:58 +000011273 MVLI.ProcessedVarList.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000011274
11275 // Store the components in the stack so that they can be used to check
11276 // against other clauses later on.
Samuel Antao6890b092016-07-28 14:25:09 +000011277 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
11278 /*WhereFoundClauseKind=*/OMPC_map);
Samuel Antao90927002016-04-26 14:54:23 +000011279
11280 // Save the components and declaration to create the clause. For purposes of
11281 // the clause creation, any component list that has has base 'this' uses
Samuel Antao686c70c2016-05-26 17:30:50 +000011282 // null as base declaration.
Samuel Antao661c0902016-05-26 17:39:58 +000011283 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
11284 MVLI.VarComponents.back().append(CurComponents.begin(),
11285 CurComponents.end());
11286 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
11287 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011288 }
Samuel Antao661c0902016-05-26 17:39:58 +000011289}
11290
11291OMPClause *
11292Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
11293 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
11294 SourceLocation MapLoc, SourceLocation ColonLoc,
11295 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
11296 SourceLocation LParenLoc, SourceLocation EndLoc) {
11297 MappableVarListInfo MVLI(VarList);
11298 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc,
11299 MapType, IsMapTypeImplicit);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011300
Samuel Antao5de996e2016-01-22 20:21:36 +000011301 // We need to produce a map clause even if we don't have variables so that
11302 // other diagnostics related with non-existing map clauses are accurate.
Samuel Antao661c0902016-05-26 17:39:58 +000011303 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11304 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11305 MVLI.VarComponents, MapTypeModifier, MapType,
11306 IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011307}
Kelvin Li099bb8c2015-11-24 20:50:12 +000011308
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011309QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
11310 TypeResult ParsedType) {
11311 assert(ParsedType.isUsable());
11312
11313 QualType ReductionType = GetTypeFromParser(ParsedType.get());
11314 if (ReductionType.isNull())
11315 return QualType();
11316
11317 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
11318 // A type name in a declare reduction directive cannot be a function type, an
11319 // array type, a reference type, or a type qualified with const, volatile or
11320 // restrict.
11321 if (ReductionType.hasQualifiers()) {
11322 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
11323 return QualType();
11324 }
11325
11326 if (ReductionType->isFunctionType()) {
11327 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
11328 return QualType();
11329 }
11330 if (ReductionType->isReferenceType()) {
11331 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
11332 return QualType();
11333 }
11334 if (ReductionType->isArrayType()) {
11335 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
11336 return QualType();
11337 }
11338 return ReductionType;
11339}
11340
11341Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
11342 Scope *S, DeclContext *DC, DeclarationName Name,
11343 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
11344 AccessSpecifier AS, Decl *PrevDeclInScope) {
11345 SmallVector<Decl *, 8> Decls;
11346 Decls.reserve(ReductionTypes.size());
11347
11348 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
11349 ForRedeclaration);
11350 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
11351 // A reduction-identifier may not be re-declared in the current scope for the
11352 // same type or for a type that is compatible according to the base language
11353 // rules.
11354 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
11355 OMPDeclareReductionDecl *PrevDRD = nullptr;
11356 bool InCompoundScope = true;
11357 if (S != nullptr) {
11358 // Find previous declaration with the same name not referenced in other
11359 // declarations.
11360 FunctionScopeInfo *ParentFn = getEnclosingFunction();
11361 InCompoundScope =
11362 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
11363 LookupName(Lookup, S);
11364 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
11365 /*AllowInlineNamespace=*/false);
11366 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
11367 auto Filter = Lookup.makeFilter();
11368 while (Filter.hasNext()) {
11369 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
11370 if (InCompoundScope) {
11371 auto I = UsedAsPrevious.find(PrevDecl);
11372 if (I == UsedAsPrevious.end())
11373 UsedAsPrevious[PrevDecl] = false;
11374 if (auto *D = PrevDecl->getPrevDeclInScope())
11375 UsedAsPrevious[D] = true;
11376 }
11377 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
11378 PrevDecl->getLocation();
11379 }
11380 Filter.done();
11381 if (InCompoundScope) {
11382 for (auto &PrevData : UsedAsPrevious) {
11383 if (!PrevData.second) {
11384 PrevDRD = PrevData.first;
11385 break;
11386 }
11387 }
11388 }
11389 } else if (PrevDeclInScope != nullptr) {
11390 auto *PrevDRDInScope = PrevDRD =
11391 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
11392 do {
11393 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
11394 PrevDRDInScope->getLocation();
11395 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
11396 } while (PrevDRDInScope != nullptr);
11397 }
11398 for (auto &TyData : ReductionTypes) {
11399 auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
11400 bool Invalid = false;
11401 if (I != PreviousRedeclTypes.end()) {
11402 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
11403 << TyData.first;
11404 Diag(I->second, diag::note_previous_definition);
11405 Invalid = true;
11406 }
11407 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
11408 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
11409 Name, TyData.first, PrevDRD);
11410 DC->addDecl(DRD);
11411 DRD->setAccess(AS);
11412 Decls.push_back(DRD);
11413 if (Invalid)
11414 DRD->setInvalidDecl();
11415 else
11416 PrevDRD = DRD;
11417 }
11418
11419 return DeclGroupPtrTy::make(
11420 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
11421}
11422
11423void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
11424 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11425
11426 // Enter new function scope.
11427 PushFunctionScope();
11428 getCurFunction()->setHasBranchProtectedScope();
11429 getCurFunction()->setHasOMPDeclareReductionCombiner();
11430
11431 if (S != nullptr)
11432 PushDeclContext(S, DRD);
11433 else
11434 CurContext = DRD;
11435
11436 PushExpressionEvaluationContext(PotentiallyEvaluated);
11437
11438 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011439 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
11440 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
11441 // uses semantics of argument handles by value, but it should be passed by
11442 // reference. C lang does not support references, so pass all parameters as
11443 // pointers.
11444 // Create 'T omp_in;' variable.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011445 auto *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011446 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011447 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
11448 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
11449 // uses semantics of argument handles by value, but it should be passed by
11450 // reference. C lang does not support references, so pass all parameters as
11451 // pointers.
11452 // Create 'T omp_out;' variable.
11453 auto *OmpOutParm =
11454 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
11455 if (S != nullptr) {
11456 PushOnScopeChains(OmpInParm, S);
11457 PushOnScopeChains(OmpOutParm, S);
11458 } else {
11459 DRD->addDecl(OmpInParm);
11460 DRD->addDecl(OmpOutParm);
11461 }
11462}
11463
11464void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
11465 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11466 DiscardCleanupsInEvaluationContext();
11467 PopExpressionEvaluationContext();
11468
11469 PopDeclContext();
11470 PopFunctionScopeInfo();
11471
11472 if (Combiner != nullptr)
11473 DRD->setCombiner(Combiner);
11474 else
11475 DRD->setInvalidDecl();
11476}
11477
11478void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
11479 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11480
11481 // Enter new function scope.
11482 PushFunctionScope();
11483 getCurFunction()->setHasBranchProtectedScope();
11484
11485 if (S != nullptr)
11486 PushDeclContext(S, DRD);
11487 else
11488 CurContext = DRD;
11489
11490 PushExpressionEvaluationContext(PotentiallyEvaluated);
11491
11492 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011493 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
11494 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
11495 // uses semantics of argument handles by value, but it should be passed by
11496 // reference. C lang does not support references, so pass all parameters as
11497 // pointers.
11498 // Create 'T omp_priv;' variable.
11499 auto *OmpPrivParm =
11500 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011501 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
11502 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
11503 // uses semantics of argument handles by value, but it should be passed by
11504 // reference. C lang does not support references, so pass all parameters as
11505 // pointers.
11506 // Create 'T omp_orig;' variable.
11507 auto *OmpOrigParm =
11508 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011509 if (S != nullptr) {
11510 PushOnScopeChains(OmpPrivParm, S);
11511 PushOnScopeChains(OmpOrigParm, S);
11512 } else {
11513 DRD->addDecl(OmpPrivParm);
11514 DRD->addDecl(OmpOrigParm);
11515 }
11516}
11517
11518void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D,
11519 Expr *Initializer) {
11520 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11521 DiscardCleanupsInEvaluationContext();
11522 PopExpressionEvaluationContext();
11523
11524 PopDeclContext();
11525 PopFunctionScopeInfo();
11526
11527 if (Initializer != nullptr)
11528 DRD->setInitializer(Initializer);
11529 else
11530 DRD->setInvalidDecl();
11531}
11532
11533Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
11534 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
11535 for (auto *D : DeclReductions.get()) {
11536 if (IsValid) {
11537 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11538 if (S != nullptr)
11539 PushOnScopeChains(DRD, S, /*AddToContext=*/false);
11540 } else
11541 D->setInvalidDecl();
11542 }
11543 return DeclReductions;
11544}
11545
David Majnemer9d168222016-08-05 17:44:54 +000011546OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
Kelvin Li099bb8c2015-11-24 20:50:12 +000011547 SourceLocation StartLoc,
11548 SourceLocation LParenLoc,
11549 SourceLocation EndLoc) {
11550 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +000011551
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011552 // OpenMP [teams Constrcut, Restrictions]
11553 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000011554 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
11555 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011556 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000011557
11558 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11559}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011560
11561OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
11562 SourceLocation StartLoc,
11563 SourceLocation LParenLoc,
11564 SourceLocation EndLoc) {
11565 Expr *ValExpr = ThreadLimit;
11566
11567 // OpenMP [teams Constrcut, Restrictions]
11568 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000011569 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
11570 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011571 return nullptr;
11572
David Majnemer9d168222016-08-05 17:44:54 +000011573 return new (Context)
11574 OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011575}
Alexey Bataeva0569352015-12-01 10:17:31 +000011576
11577OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
11578 SourceLocation StartLoc,
11579 SourceLocation LParenLoc,
11580 SourceLocation EndLoc) {
11581 Expr *ValExpr = Priority;
11582
11583 // OpenMP [2.9.1, task Constrcut]
11584 // The priority-value is a non-negative numerical scalar expression.
11585 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
11586 /*StrictlyPositive=*/false))
11587 return nullptr;
11588
11589 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11590}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000011591
11592OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
11593 SourceLocation StartLoc,
11594 SourceLocation LParenLoc,
11595 SourceLocation EndLoc) {
11596 Expr *ValExpr = Grainsize;
11597
11598 // OpenMP [2.9.2, taskloop Constrcut]
11599 // The parameter of the grainsize clause must be a positive integer
11600 // expression.
11601 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
11602 /*StrictlyPositive=*/true))
11603 return nullptr;
11604
11605 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11606}
Alexey Bataev382967a2015-12-08 12:06:20 +000011607
11608OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
11609 SourceLocation StartLoc,
11610 SourceLocation LParenLoc,
11611 SourceLocation EndLoc) {
11612 Expr *ValExpr = NumTasks;
11613
11614 // OpenMP [2.9.2, taskloop Constrcut]
11615 // The parameter of the num_tasks clause must be a positive integer
11616 // expression.
11617 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
11618 /*StrictlyPositive=*/true))
11619 return nullptr;
11620
11621 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11622}
11623
Alexey Bataev28c75412015-12-15 08:19:24 +000011624OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
11625 SourceLocation LParenLoc,
11626 SourceLocation EndLoc) {
11627 // OpenMP [2.13.2, critical construct, Description]
11628 // ... where hint-expression is an integer constant expression that evaluates
11629 // to a valid lock hint.
11630 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
11631 if (HintExpr.isInvalid())
11632 return nullptr;
11633 return new (Context)
11634 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
11635}
11636
Carlo Bertollib4adf552016-01-15 18:50:31 +000011637OMPClause *Sema::ActOnOpenMPDistScheduleClause(
11638 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
11639 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
11640 SourceLocation EndLoc) {
11641 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
11642 std::string Values;
11643 Values += "'";
11644 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
11645 Values += "'";
11646 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
11647 << Values << getOpenMPClauseName(OMPC_dist_schedule);
11648 return nullptr;
11649 }
11650 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000011651 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000011652 if (ChunkSize) {
11653 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
11654 !ChunkSize->isInstantiationDependent() &&
11655 !ChunkSize->containsUnexpandedParameterPack()) {
11656 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
11657 ExprResult Val =
11658 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
11659 if (Val.isInvalid())
11660 return nullptr;
11661
11662 ValExpr = Val.get();
11663
11664 // OpenMP [2.7.1, Restrictions]
11665 // chunk_size must be a loop invariant integer expression with a positive
11666 // value.
11667 llvm::APSInt Result;
11668 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
11669 if (Result.isSigned() && !Result.isStrictlyPositive()) {
11670 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
11671 << "dist_schedule" << ChunkSize->getSourceRange();
11672 return nullptr;
11673 }
Alexey Bataevb46cdea2016-06-15 11:20:48 +000011674 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
11675 !CurContext->isDependentContext()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +000011676 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
11677 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
11678 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000011679 }
11680 }
11681 }
11682
11683 return new (Context)
11684 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000011685 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000011686}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000011687
11688OMPClause *Sema::ActOnOpenMPDefaultmapClause(
11689 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
11690 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
11691 SourceLocation KindLoc, SourceLocation EndLoc) {
11692 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
David Majnemer9d168222016-08-05 17:44:54 +000011693 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) {
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000011694 std::string Value;
11695 SourceLocation Loc;
11696 Value += "'";
11697 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
11698 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000011699 OMPC_DEFAULTMAP_MODIFIER_tofrom);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000011700 Loc = MLoc;
11701 } else {
11702 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000011703 OMPC_DEFAULTMAP_scalar);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000011704 Loc = KindLoc;
11705 }
11706 Value += "'";
11707 Diag(Loc, diag::err_omp_unexpected_clause_value)
11708 << Value << getOpenMPClauseName(OMPC_defaultmap);
11709 return nullptr;
11710 }
11711
11712 return new (Context)
11713 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
11714}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011715
11716bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
11717 DeclContext *CurLexicalContext = getCurLexicalContext();
11718 if (!CurLexicalContext->isFileContext() &&
11719 !CurLexicalContext->isExternCContext() &&
11720 !CurLexicalContext->isExternCXXContext()) {
11721 Diag(Loc, diag::err_omp_region_not_file_context);
11722 return false;
11723 }
11724 if (IsInOpenMPDeclareTargetContext) {
11725 Diag(Loc, diag::err_omp_enclosed_declare_target);
11726 return false;
11727 }
11728
11729 IsInOpenMPDeclareTargetContext = true;
11730 return true;
11731}
11732
11733void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
11734 assert(IsInOpenMPDeclareTargetContext &&
11735 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
11736
11737 IsInOpenMPDeclareTargetContext = false;
11738}
11739
David Majnemer9d168222016-08-05 17:44:54 +000011740void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope,
11741 CXXScopeSpec &ScopeSpec,
11742 const DeclarationNameInfo &Id,
11743 OMPDeclareTargetDeclAttr::MapTypeTy MT,
11744 NamedDeclSetType &SameDirectiveDecls) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011745 LookupResult Lookup(*this, Id, LookupOrdinaryName);
11746 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
11747
11748 if (Lookup.isAmbiguous())
11749 return;
11750 Lookup.suppressDiagnostics();
11751
11752 if (!Lookup.isSingleResult()) {
11753 if (TypoCorrection Corrected =
11754 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
11755 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
11756 CTK_ErrorRecovery)) {
11757 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
11758 << Id.getName());
11759 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
11760 return;
11761 }
11762
11763 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
11764 return;
11765 }
11766
11767 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
11768 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
11769 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
11770 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
11771
11772 if (!ND->hasAttr<OMPDeclareTargetDeclAttr>()) {
11773 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
11774 ND->addAttr(A);
11775 if (ASTMutationListener *ML = Context.getASTMutationListener())
11776 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
11777 checkDeclIsAllowedInOpenMPTarget(nullptr, ND);
11778 } else if (ND->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() != MT) {
11779 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
11780 << Id.getName();
11781 }
11782 } else
11783 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
11784}
11785
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011786static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
11787 Sema &SemaRef, Decl *D) {
11788 if (!D)
11789 return;
11790 Decl *LD = nullptr;
11791 if (isa<TagDecl>(D)) {
11792 LD = cast<TagDecl>(D)->getDefinition();
11793 } else if (isa<VarDecl>(D)) {
11794 LD = cast<VarDecl>(D)->getDefinition();
11795
11796 // If this is an implicit variable that is legal and we do not need to do
11797 // anything.
11798 if (cast<VarDecl>(D)->isImplicit()) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011799 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11800 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11801 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011802 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011803 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011804 return;
11805 }
11806
11807 } else if (isa<FunctionDecl>(D)) {
11808 const FunctionDecl *FD = nullptr;
11809 if (cast<FunctionDecl>(D)->hasBody(FD))
11810 LD = const_cast<FunctionDecl *>(FD);
11811
11812 // If the definition is associated with the current declaration in the
11813 // target region (it can be e.g. a lambda) that is legal and we do not need
11814 // to do anything else.
11815 if (LD == D) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011816 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11817 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11818 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011819 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011820 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011821 return;
11822 }
11823 }
11824 if (!LD)
11825 LD = D;
11826 if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() &&
11827 (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) {
11828 // Outlined declaration is not declared target.
11829 if (LD->isOutOfLine()) {
11830 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
11831 SemaRef.Diag(SL, diag::note_used_here) << SR;
11832 } else {
11833 DeclContext *DC = LD->getDeclContext();
11834 while (DC) {
11835 if (isa<FunctionDecl>(DC) &&
11836 cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>())
11837 break;
11838 DC = DC->getParent();
11839 }
11840 if (DC)
11841 return;
11842
11843 // Is not declared in target context.
11844 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
11845 SemaRef.Diag(SL, diag::note_used_here) << SR;
11846 }
11847 // Mark decl as declared target to prevent further diagnostic.
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011848 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11849 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11850 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011851 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011852 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011853 }
11854}
11855
11856static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
11857 Sema &SemaRef, DSAStackTy *Stack,
11858 ValueDecl *VD) {
11859 if (VD->hasAttr<OMPDeclareTargetDeclAttr>())
11860 return true;
11861 if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType()))
11862 return false;
11863 return true;
11864}
11865
11866void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D) {
11867 if (!D || D->isInvalidDecl())
11868 return;
11869 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
11870 SourceLocation SL = E ? E->getLocStart() : D->getLocation();
11871 // 2.10.6: threadprivate variable cannot appear in a declare target directive.
11872 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
11873 if (DSAStack->isThreadPrivate(VD)) {
11874 Diag(SL, diag::err_omp_threadprivate_in_target);
11875 ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
11876 return;
11877 }
11878 }
11879 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
11880 // Problem if any with var declared with incomplete type will be reported
11881 // as normal, so no need to check it here.
11882 if ((E || !VD->getType()->isIncompleteType()) &&
11883 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) {
11884 // Mark decl as declared target to prevent further diagnostic.
11885 if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011886 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11887 Context, OMPDeclareTargetDeclAttr::MT_To);
11888 VD->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011889 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011890 ML->DeclarationMarkedOpenMPDeclareTarget(VD, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011891 }
11892 return;
11893 }
11894 }
11895 if (!E) {
11896 // Checking declaration inside declare target region.
11897 if (!D->hasAttr<OMPDeclareTargetDeclAttr>() &&
11898 (isa<VarDecl>(D) || isa<FunctionDecl>(D))) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011899 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11900 Context, OMPDeclareTargetDeclAttr::MT_To);
11901 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011902 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011903 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011904 }
11905 return;
11906 }
11907 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
11908}
Samuel Antao661c0902016-05-26 17:39:58 +000011909
11910OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
11911 SourceLocation StartLoc,
11912 SourceLocation LParenLoc,
11913 SourceLocation EndLoc) {
11914 MappableVarListInfo MVLI(VarList);
11915 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc);
11916 if (MVLI.ProcessedVarList.empty())
11917 return nullptr;
11918
11919 return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11920 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11921 MVLI.VarComponents);
11922}
Samuel Antaoec172c62016-05-26 17:49:04 +000011923
11924OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
11925 SourceLocation StartLoc,
11926 SourceLocation LParenLoc,
11927 SourceLocation EndLoc) {
11928 MappableVarListInfo MVLI(VarList);
11929 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc);
11930 if (MVLI.ProcessedVarList.empty())
11931 return nullptr;
11932
11933 return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11934 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11935 MVLI.VarComponents);
11936}
Carlo Bertolli2404b172016-07-13 15:37:16 +000011937
11938OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
11939 SourceLocation StartLoc,
11940 SourceLocation LParenLoc,
11941 SourceLocation EndLoc) {
Samuel Antaocc10b852016-07-28 14:23:26 +000011942 MappableVarListInfo MVLI(VarList);
11943 SmallVector<Expr *, 8> PrivateCopies;
11944 SmallVector<Expr *, 8> Inits;
11945
Carlo Bertolli2404b172016-07-13 15:37:16 +000011946 for (auto &RefExpr : VarList) {
11947 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
11948 SourceLocation ELoc;
11949 SourceRange ERange;
11950 Expr *SimpleRefExpr = RefExpr;
11951 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
11952 if (Res.second) {
11953 // It will be analyzed later.
Samuel Antaocc10b852016-07-28 14:23:26 +000011954 MVLI.ProcessedVarList.push_back(RefExpr);
11955 PrivateCopies.push_back(nullptr);
11956 Inits.push_back(nullptr);
Carlo Bertolli2404b172016-07-13 15:37:16 +000011957 }
11958 ValueDecl *D = Res.first;
11959 if (!D)
11960 continue;
11961
11962 QualType Type = D->getType();
Samuel Antaocc10b852016-07-28 14:23:26 +000011963 Type = Type.getNonReferenceType().getUnqualifiedType();
11964
11965 auto *VD = dyn_cast<VarDecl>(D);
11966
11967 // Item should be a pointer or reference to pointer.
11968 if (!Type->isPointerType()) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000011969 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
11970 << 0 << RefExpr->getSourceRange();
11971 continue;
11972 }
Samuel Antaocc10b852016-07-28 14:23:26 +000011973
11974 // Build the private variable and the expression that refers to it.
11975 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
11976 D->hasAttrs() ? &D->getAttrs() : nullptr);
11977 if (VDPrivate->isInvalidDecl())
11978 continue;
11979
11980 CurContext->addDecl(VDPrivate);
11981 auto VDPrivateRefExpr = buildDeclRefExpr(
11982 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
11983
11984 // Add temporary variable to initialize the private copy of the pointer.
11985 auto *VDInit =
11986 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
11987 auto *VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
11988 RefExpr->getExprLoc());
11989 AddInitializerToDecl(VDPrivate,
11990 DefaultLvalueConversion(VDInitRefExpr).get(),
11991 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
11992
11993 // If required, build a capture to implement the privatization initialized
11994 // with the current list item value.
11995 DeclRefExpr *Ref = nullptr;
11996 if (!VD)
11997 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
11998 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
11999 PrivateCopies.push_back(VDPrivateRefExpr);
12000 Inits.push_back(VDInitRefExpr);
12001
12002 // We need to add a data sharing attribute for this variable to make sure it
12003 // is correctly captured. A variable that shows up in a use_device_ptr has
12004 // similar properties of a first private variable.
12005 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
12006
12007 // Create a mappable component for the list item. List items in this clause
12008 // only need a component.
12009 MVLI.VarBaseDeclarations.push_back(D);
12010 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
12011 MVLI.VarComponents.back().push_back(
12012 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D));
Carlo Bertolli2404b172016-07-13 15:37:16 +000012013 }
12014
Samuel Antaocc10b852016-07-28 14:23:26 +000012015 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli2404b172016-07-13 15:37:16 +000012016 return nullptr;
12017
Samuel Antaocc10b852016-07-28 14:23:26 +000012018 return OMPUseDevicePtrClause::Create(
12019 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
12020 PrivateCopies, Inits, MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli2404b172016-07-13 15:37:16 +000012021}
Carlo Bertolli70594e92016-07-13 17:16:49 +000012022
12023OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
12024 SourceLocation StartLoc,
12025 SourceLocation LParenLoc,
12026 SourceLocation EndLoc) {
Samuel Antao6890b092016-07-28 14:25:09 +000012027 MappableVarListInfo MVLI(VarList);
Carlo Bertolli70594e92016-07-13 17:16:49 +000012028 for (auto &RefExpr : VarList) {
12029 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
12030 SourceLocation ELoc;
12031 SourceRange ERange;
12032 Expr *SimpleRefExpr = RefExpr;
12033 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
12034 if (Res.second) {
12035 // It will be analyzed later.
Samuel Antao6890b092016-07-28 14:25:09 +000012036 MVLI.ProcessedVarList.push_back(RefExpr);
Carlo Bertolli70594e92016-07-13 17:16:49 +000012037 }
12038 ValueDecl *D = Res.first;
12039 if (!D)
12040 continue;
12041
12042 QualType Type = D->getType();
12043 // item should be a pointer or array or reference to pointer or array
12044 if (!Type.getNonReferenceType()->isPointerType() &&
12045 !Type.getNonReferenceType()->isArrayType()) {
12046 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
12047 << 0 << RefExpr->getSourceRange();
12048 continue;
12049 }
Samuel Antao6890b092016-07-28 14:25:09 +000012050
12051 // Check if the declaration in the clause does not show up in any data
12052 // sharing attribute.
12053 auto DVar = DSAStack->getTopDSA(D, false);
12054 if (isOpenMPPrivate(DVar.CKind)) {
12055 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
12056 << getOpenMPClauseName(DVar.CKind)
12057 << getOpenMPClauseName(OMPC_is_device_ptr)
12058 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
12059 ReportOriginalDSA(*this, DSAStack, D, DVar);
12060 continue;
12061 }
12062
12063 Expr *ConflictExpr;
12064 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +000012065 D, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +000012066 [&ConflictExpr](
12067 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
12068 OpenMPClauseKind) -> bool {
12069 ConflictExpr = R.front().getAssociatedExpression();
12070 return true;
12071 })) {
12072 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
12073 Diag(ConflictExpr->getExprLoc(), diag::note_used_here)
12074 << ConflictExpr->getSourceRange();
12075 continue;
12076 }
12077
12078 // Store the components in the stack so that they can be used to check
12079 // against other clauses later on.
12080 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D);
12081 DSAStack->addMappableExpressionComponents(
12082 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
12083
12084 // Record the expression we've just processed.
12085 MVLI.ProcessedVarList.push_back(SimpleRefExpr);
12086
12087 // Create a mappable component for the list item. List items in this clause
12088 // only need a component. We use a null declaration to signal fields in
12089 // 'this'.
12090 assert((isa<DeclRefExpr>(SimpleRefExpr) ||
12091 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
12092 "Unexpected device pointer expression!");
12093 MVLI.VarBaseDeclarations.push_back(
12094 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr);
12095 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
12096 MVLI.VarComponents.back().push_back(MC);
Carlo Bertolli70594e92016-07-13 17:16:49 +000012097 }
12098
Samuel Antao6890b092016-07-28 14:25:09 +000012099 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli70594e92016-07-13 17:16:49 +000012100 return nullptr;
12101
Samuel Antao6890b092016-07-28 14:25:09 +000012102 return OMPIsDevicePtrClause::Create(
12103 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
12104 MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli70594e92016-07-13 17:16:49 +000012105}