blob: dcd19c8d817d093e59915700f20d5a4d5a2510e8 [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 Antao4c8035b2016-12-12 18:00:20 +0000329 /// Do the check specified in \a Check to all component lists and return true
330 /// if any issue is found.
Samuel Antao90927002016-04-26 14:54:23 +0000331 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 Antao4c8035b2016-12-12 18:00:20 +0000358 /// Create a new mappable expression component list associated with a given
359 /// declaration and initialize it with the provided list of components.
Samuel Antao90927002016-04-26 14:54:23 +0000360 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
Samuel Antao4c8035b2016-12-12 18:00:20 +0000922 // behavior.
Samuel Antao6890b092016-07-28 14:25:09 +0000923 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);
Richard Smith3beb7c62017-01-12 02:27:38 +00001092 ActOnUninitializedDecl(VDPrivate);
Alexey Bataev38e89532015-04-16 04:54:05 +00001093 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 }
Alexey Bataev7fcacd82016-11-28 15:55:15 +00001559 } else
1560 Visit(E->getBase());
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001561 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001562 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001563 for (auto *C : S->clauses()) {
1564 // Skip analysis of arguments of implicitly defined firstprivate clause
1565 // for task directives.
1566 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1567 for (auto *CC : C->children()) {
1568 if (CC)
1569 Visit(CC);
1570 }
1571 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001572 }
1573 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001574 for (auto *C : S->children()) {
1575 if (C && !isa<OMPExecutableDirective>(C))
1576 Visit(C);
1577 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001578 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001579
1580 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001581 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001582 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001583 return VarsWithInheritedDSA;
1584 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001585
Alexey Bataev7ff55242014-06-19 09:13:45 +00001586 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1587 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001588};
Alexey Bataeved09d242014-05-28 05:53:51 +00001589} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001590
Alexey Bataevbae9a792014-06-27 10:37:06 +00001591void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001592 switch (DKind) {
Kelvin Li70a12c52016-07-13 21:51:49 +00001593 case OMPD_parallel:
1594 case OMPD_parallel_for:
1595 case OMPD_parallel_for_simd:
1596 case OMPD_parallel_sections:
Kelvin Libf594a52016-12-17 05:48:59 +00001597 case OMPD_teams:
1598 case OMPD_target_teams: {
Alexey Bataev9959db52014-05-06 10:08:46 +00001599 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001600 QualType KmpInt32PtrTy =
1601 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001602 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001603 std::make_pair(".global_tid.", KmpInt32PtrTy),
1604 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1605 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001606 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001607 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1608 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001609 break;
1610 }
Kelvin Li70a12c52016-07-13 21:51:49 +00001611 case OMPD_simd:
1612 case OMPD_for:
1613 case OMPD_for_simd:
1614 case OMPD_sections:
1615 case OMPD_section:
1616 case OMPD_single:
1617 case OMPD_master:
1618 case OMPD_critical:
Kelvin Lia579b912016-07-14 02:54:56 +00001619 case OMPD_taskgroup:
1620 case OMPD_distribute:
Kelvin Li70a12c52016-07-13 21:51:49 +00001621 case OMPD_ordered:
1622 case OMPD_atomic:
1623 case OMPD_target_data:
1624 case OMPD_target:
Arpith Chacko Jacob42793e02017-01-18 15:36:05 +00001625 case OMPD_target_parallel:
Kelvin Li70a12c52016-07-13 21:51:49 +00001626 case OMPD_target_parallel_for:
Kelvin Li986330c2016-07-20 22:57:10 +00001627 case OMPD_target_parallel_for_simd:
1628 case OMPD_target_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001629 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001630 std::make_pair(StringRef(), QualType()) // __context with shared vars
1631 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001632 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1633 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001634 break;
1635 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001636 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001637 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001638 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1639 FunctionProtoType::ExtProtoInfo EPI;
1640 EPI.Variadic = true;
1641 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001642 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001643 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataev48591dd2016-04-20 04:01:36 +00001644 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1645 std::make_pair(".privates.", Context.VoidPtrTy.withConst()),
1646 std::make_pair(".copy_fn.",
1647 Context.getPointerType(CopyFnType).withConst()),
1648 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001649 std::make_pair(StringRef(), QualType()) // __context with shared vars
1650 };
1651 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1652 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001653 // Mark this captured region as inlined, because we don't use outlined
1654 // function directly.
1655 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1656 AlwaysInlineAttr::CreateImplicit(
1657 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001658 break;
1659 }
Alexey Bataev1e73ef32016-04-28 12:14:51 +00001660 case OMPD_taskloop:
1661 case OMPD_taskloop_simd: {
Alexey Bataev7292c292016-04-25 12:22:29 +00001662 QualType KmpInt32Ty =
1663 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
1664 QualType KmpUInt64Ty =
1665 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
1666 QualType KmpInt64Ty =
1667 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
1668 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1669 FunctionProtoType::ExtProtoInfo EPI;
1670 EPI.Variadic = true;
1671 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev49f6e782015-12-01 04:18:41 +00001672 Sema::CapturedParamNameType Params[] = {
Alexey Bataev7292c292016-04-25 12:22:29 +00001673 std::make_pair(".global_tid.", KmpInt32Ty),
1674 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1675 std::make_pair(".privates.",
1676 Context.VoidPtrTy.withConst().withRestrict()),
1677 std::make_pair(
1678 ".copy_fn.",
1679 Context.getPointerType(CopyFnType).withConst().withRestrict()),
1680 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
1681 std::make_pair(".lb.", KmpUInt64Ty),
1682 std::make_pair(".ub.", KmpUInt64Ty), std::make_pair(".st.", KmpInt64Ty),
1683 std::make_pair(".liter.", KmpInt32Ty),
Alexey Bataev49f6e782015-12-01 04:18:41 +00001684 std::make_pair(StringRef(), QualType()) // __context with shared vars
1685 };
1686 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1687 Params);
Alexey Bataev7292c292016-04-25 12:22:29 +00001688 // Mark this captured region as inlined, because we don't use outlined
1689 // function directly.
1690 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1691 AlwaysInlineAttr::CreateImplicit(
1692 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev49f6e782015-12-01 04:18:41 +00001693 break;
1694 }
Kelvin Li4a39add2016-07-05 05:00:15 +00001695 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001696 case OMPD_distribute_simd:
Kelvin Li02532872016-08-05 14:37:37 +00001697 case OMPD_distribute_parallel_for:
Kelvin Li4e325f72016-10-25 12:50:55 +00001698 case OMPD_teams_distribute:
Kelvin Li579e41c2016-11-30 23:51:03 +00001699 case OMPD_teams_distribute_simd:
Kelvin Li7ade93f2016-12-09 03:24:30 +00001700 case OMPD_teams_distribute_parallel_for_simd:
Kelvin Li83c451e2016-12-25 04:52:54 +00001701 case OMPD_teams_distribute_parallel_for:
Kelvin Li80e8f562016-12-29 22:16:30 +00001702 case OMPD_target_teams_distribute:
Kelvin Li1851df52017-01-03 05:23:48 +00001703 case OMPD_target_teams_distribute_parallel_for:
Kelvin Lida681182017-01-10 18:08:18 +00001704 case OMPD_target_teams_distribute_parallel_for_simd:
1705 case OMPD_target_teams_distribute_simd: {
Carlo Bertolli9925f152016-06-27 14:55:37 +00001706 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1707 QualType KmpInt32PtrTy =
1708 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
1709 Sema::CapturedParamNameType Params[] = {
1710 std::make_pair(".global_tid.", KmpInt32PtrTy),
1711 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1712 std::make_pair(".previous.lb.", Context.getSizeType()),
1713 std::make_pair(".previous.ub.", Context.getSizeType()),
1714 std::make_pair(StringRef(), QualType()) // __context with shared vars
1715 };
1716 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1717 Params);
1718 break;
1719 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001720 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001721 case OMPD_taskyield:
1722 case OMPD_barrier:
1723 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001724 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001725 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001726 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001727 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001728 case OMPD_target_exit_data:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001729 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001730 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001731 case OMPD_declare_target:
1732 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +00001733 case OMPD_target_update:
Alexey Bataev9959db52014-05-06 10:08:46 +00001734 llvm_unreachable("OpenMP Directive is not allowed");
1735 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001736 llvm_unreachable("Unknown OpenMP directive");
1737 }
1738}
1739
Alexey Bataev3392d762016-02-16 11:18:12 +00001740static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00001741 Expr *CaptureExpr, bool WithInit,
1742 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001743 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00001744 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00001745 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00001746 QualType Ty = Init->getType();
1747 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1748 if (S.getLangOpts().CPlusPlus)
1749 Ty = C.getLValueReferenceType(Ty);
1750 else {
1751 Ty = C.getPointerType(Ty);
1752 ExprResult Res =
1753 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1754 if (!Res.isUsable())
1755 return nullptr;
1756 Init = Res.get();
1757 }
Alexey Bataev61205072016-03-02 04:57:40 +00001758 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00001759 }
Alexey Bataeva7206b92016-12-20 16:51:02 +00001760 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty,
1761 CaptureExpr->getLocStart());
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001762 if (!WithInit)
1763 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
Alexey Bataev4244be22016-02-11 05:35:55 +00001764 S.CurContext->addHiddenDecl(CED);
Richard Smith3beb7c62017-01-12 02:27:38 +00001765 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001766 return CED;
1767}
1768
Alexey Bataev61205072016-03-02 04:57:40 +00001769static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
1770 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00001771 OMPCapturedExprDecl *CD;
1772 if (auto *VD = S.IsOpenMPCapturedDecl(D))
1773 CD = cast<OMPCapturedExprDecl>(VD);
1774 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00001775 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
1776 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001777 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00001778 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00001779}
1780
Alexey Bataev5a3af132016-03-29 08:58:54 +00001781static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
1782 if (!Ref) {
1783 auto *CD =
1784 buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."),
1785 CaptureExpr, /*WithInit=*/true, /*AsExpression=*/true);
1786 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1787 CaptureExpr->getExprLoc());
1788 }
1789 ExprResult Res = Ref;
1790 if (!S.getLangOpts().CPlusPlus &&
1791 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
1792 Ref->getType()->isPointerType())
1793 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
1794 if (!Res.isUsable())
1795 return ExprError();
1796 return CaptureExpr->isGLValue() ? Res : S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00001797}
1798
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001799StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1800 ArrayRef<OMPClause *> Clauses) {
1801 if (!S.isUsable()) {
Arpith Chacko Jacob42793e02017-01-18 15:36:05 +00001802 ActOnCapturedRegionError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001803 return StmtError();
1804 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001805
1806 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001807 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001808 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001809 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001810 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001811 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001812 Clause->getClauseKind() == OMPC_copyprivate ||
1813 (getLangOpts().OpenMPUseTLS &&
1814 getASTContext().getTargetInfo().isTLSSupported() &&
1815 Clause->getClauseKind() == OMPC_copyin)) {
1816 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001817 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001818 for (auto *VarRef : Clause->children()) {
1819 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001820 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001821 }
1822 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001823 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001824 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev040d5402015-05-12 08:35:28 +00001825 // Mark all variables in private list clauses as used in inner region.
1826 // Required for proper codegen of combined directives.
1827 // TODO: add processing for other clauses.
Alexey Bataev3392d762016-02-16 11:18:12 +00001828 if (auto *C = OMPClauseWithPreInit::get(Clause)) {
Alexey Bataev005248a2016-02-25 05:25:57 +00001829 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
1830 for (auto *D : DS->decls())
Alexey Bataev3392d762016-02-16 11:18:12 +00001831 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
1832 }
Alexey Bataev4244be22016-02-11 05:35:55 +00001833 }
Alexey Bataev005248a2016-02-25 05:25:57 +00001834 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
1835 if (auto *E = C->getPostUpdateExpr())
1836 MarkDeclarationsReferencedInExpr(E);
1837 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001838 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001839 if (Clause->getClauseKind() == OMPC_schedule)
1840 SC = cast<OMPScheduleClause>(Clause);
1841 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001842 OC = cast<OMPOrderedClause>(Clause);
1843 else if (Clause->getClauseKind() == OMPC_linear)
1844 LCs.push_back(cast<OMPLinearClause>(Clause));
1845 }
Arpith Chacko Jacob42793e02017-01-18 15:36:05 +00001846 bool ErrorFound = false;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001847 // OpenMP, 2.7.1 Loop Construct, Restrictions
1848 // The nonmonotonic modifier cannot be specified if an ordered clause is
1849 // specified.
1850 if (SC &&
1851 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1852 SC->getSecondScheduleModifier() ==
1853 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1854 OC) {
1855 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1856 ? SC->getFirstScheduleModifierLoc()
1857 : SC->getSecondScheduleModifierLoc(),
1858 diag::err_omp_schedule_nonmonotonic_ordered)
1859 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1860 ErrorFound = true;
1861 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001862 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1863 for (auto *C : LCs) {
1864 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1865 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1866 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001867 ErrorFound = true;
1868 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001869 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1870 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1871 OC->getNumForLoops()) {
1872 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1873 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1874 ErrorFound = true;
1875 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001876 if (ErrorFound) {
Arpith Chacko Jacob42793e02017-01-18 15:36:05 +00001877 ActOnCapturedRegionError();
Alexey Bataev993d2802015-12-28 06:23:08 +00001878 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001879 }
Arpith Chacko Jacob42793e02017-01-18 15:36:05 +00001880 return ActOnCapturedRegionEnd(S.get());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001881}
1882
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001883static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1884 OpenMPDirectiveKind CurrentRegion,
1885 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001886 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001887 SourceLocation StartLoc) {
Alexey Bataev549210e2014-06-24 04:39:47 +00001888 if (Stack->getCurScope()) {
1889 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00001890 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00001891 bool NestingProhibited = false;
1892 bool CloseNesting = true;
David Majnemer9d168222016-08-05 17:44:54 +00001893 bool OrphanSeen = false;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001894 enum {
1895 NoRecommend,
1896 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00001897 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001898 ShouldBeInTargetRegion,
1899 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001900 } Recommend = NoRecommend;
Kelvin Lifd8b5742016-07-01 14:30:25 +00001901 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00001902 // OpenMP [2.16, Nesting of Regions]
1903 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001904 // OpenMP [2.8.1,simd Construct, Restrictions]
Kelvin Lifd8b5742016-07-01 14:30:25 +00001905 // An ordered construct with the simd clause is the only OpenMP
1906 // construct that can appear in the simd region.
David Majnemer9d168222016-08-05 17:44:54 +00001907 // Allowing a SIMD construct nested in another SIMD construct is an
Kelvin Lifd8b5742016-07-01 14:30:25 +00001908 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
1909 // message.
1910 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
1911 ? diag::err_omp_prohibited_region_simd
1912 : diag::warn_omp_nesting_simd);
1913 return CurrentRegion != OMPD_simd;
Alexey Bataev549210e2014-06-24 04:39:47 +00001914 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001915 if (ParentRegion == OMPD_atomic) {
1916 // OpenMP [2.16, Nesting of Regions]
1917 // OpenMP constructs may not be nested inside an atomic region.
1918 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
1919 return true;
1920 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001921 if (CurrentRegion == OMPD_section) {
1922 // OpenMP [2.7.2, sections Construct, Restrictions]
1923 // Orphaned section directives are prohibited. That is, the section
1924 // directives must appear within the sections construct and must not be
1925 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001926 if (ParentRegion != OMPD_sections &&
1927 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001928 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1929 << (ParentRegion != OMPD_unknown)
1930 << getOpenMPDirectiveName(ParentRegion);
1931 return true;
1932 }
1933 return false;
1934 }
Kelvin Li2b51f722016-07-26 04:32:50 +00001935 // Allow some constructs (except teams) to be orphaned (they could be
David Majnemer9d168222016-08-05 17:44:54 +00001936 // used in functions, called from OpenMP regions with the required
Kelvin Li2b51f722016-07-26 04:32:50 +00001937 // preconditions).
Kelvin Libf594a52016-12-17 05:48:59 +00001938 if (ParentRegion == OMPD_unknown &&
1939 !isOpenMPNestingTeamsDirective(CurrentRegion))
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001940 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00001941 if (CurrentRegion == OMPD_cancellation_point ||
1942 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001943 // OpenMP [2.16, Nesting of Regions]
1944 // A cancellation point construct for which construct-type-clause is
1945 // taskgroup must be nested inside a task construct. A cancellation
1946 // point construct for which construct-type-clause is not taskgroup must
1947 // be closely nested inside an OpenMP construct that matches the type
1948 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00001949 // A cancel construct for which construct-type-clause is taskgroup must be
1950 // nested inside a task construct. A cancel construct for which
1951 // construct-type-clause is not taskgroup must be closely nested inside an
1952 // OpenMP construct that matches the type specified in
1953 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001954 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001955 !((CancelRegion == OMPD_parallel &&
1956 (ParentRegion == OMPD_parallel ||
1957 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00001958 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001959 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
1960 ParentRegion == OMPD_target_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001961 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
1962 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00001963 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
1964 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001965 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00001966 // OpenMP [2.16, Nesting of Regions]
1967 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001968 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00001969 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00001970 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001971 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
1972 // OpenMP [2.16, Nesting of Regions]
1973 // A critical region may not be nested (closely or otherwise) inside a
1974 // critical region with the same name. Note that this restriction is not
1975 // sufficient to prevent deadlock.
1976 SourceLocation PreviousCriticalLoc;
David Majnemer9d168222016-08-05 17:44:54 +00001977 bool DeadLock = Stack->hasDirective(
1978 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
1979 const DeclarationNameInfo &DNI,
1980 SourceLocation Loc) -> bool {
1981 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
1982 PreviousCriticalLoc = Loc;
1983 return true;
1984 } else
1985 return false;
1986 },
1987 false /* skip top directive */);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001988 if (DeadLock) {
1989 SemaRef.Diag(StartLoc,
1990 diag::err_omp_prohibited_region_critical_same_name)
1991 << CurrentName.getName();
1992 if (PreviousCriticalLoc.isValid())
1993 SemaRef.Diag(PreviousCriticalLoc,
1994 diag::note_omp_previous_critical_region);
1995 return true;
1996 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001997 } else if (CurrentRegion == OMPD_barrier) {
1998 // OpenMP [2.16, Nesting of Regions]
1999 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002000 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00002001 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2002 isOpenMPTaskingDirective(ParentRegion) ||
2003 ParentRegion == OMPD_master ||
2004 ParentRegion == OMPD_critical ||
2005 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00002006 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Kelvin Li579e41c2016-11-30 23:51:03 +00002007 !isOpenMPParallelDirective(CurrentRegion) &&
2008 !isOpenMPTeamsDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002009 // OpenMP [2.16, Nesting of Regions]
2010 // A worksharing region may not be closely nested inside a worksharing,
2011 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00002012 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2013 isOpenMPTaskingDirective(ParentRegion) ||
2014 ParentRegion == OMPD_master ||
2015 ParentRegion == OMPD_critical ||
2016 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002017 Recommend = ShouldBeInParallelRegion;
2018 } else if (CurrentRegion == OMPD_ordered) {
2019 // OpenMP [2.16, Nesting of Regions]
2020 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002021 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002022 // An ordered region must be closely nested inside a loop region (or
2023 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002024 // OpenMP [2.8.1,simd Construct, Restrictions]
2025 // An ordered construct with the simd clause is the only OpenMP construct
2026 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002027 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00002028 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002029 !(isOpenMPSimdDirective(ParentRegion) ||
2030 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002031 Recommend = ShouldBeInOrderedRegion;
Kelvin Libf594a52016-12-17 05:48:59 +00002032 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00002033 // OpenMP [2.16, Nesting of Regions]
2034 // If specified, a teams construct must be contained within a target
2035 // construct.
2036 NestingProhibited = ParentRegion != OMPD_target;
Kelvin Li2b51f722016-07-26 04:32:50 +00002037 OrphanSeen = ParentRegion == OMPD_unknown;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002038 Recommend = ShouldBeInTargetRegion;
2039 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2040 }
Kelvin Libf594a52016-12-17 05:48:59 +00002041 if (!NestingProhibited &&
2042 !isOpenMPTargetExecutionDirective(CurrentRegion) &&
2043 !isOpenMPTargetDataManagementDirective(CurrentRegion) &&
2044 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00002045 // OpenMP [2.16, Nesting of Regions]
2046 // distribute, parallel, parallel sections, parallel workshare, and the
2047 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2048 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002049 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2050 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002051 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002052 }
David Majnemer9d168222016-08-05 17:44:54 +00002053 if (!NestingProhibited &&
Kelvin Li02532872016-08-05 14:37:37 +00002054 isOpenMPNestingDistributeDirective(CurrentRegion)) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002055 // OpenMP 4.5 [2.17 Nesting of Regions]
2056 // The region associated with the distribute construct must be strictly
2057 // nested inside a teams region
Kelvin Libf594a52016-12-17 05:48:59 +00002058 NestingProhibited =
2059 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002060 Recommend = ShouldBeInTeamsRegion;
2061 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002062 if (!NestingProhibited &&
2063 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2064 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2065 // OpenMP 4.5 [2.17 Nesting of Regions]
2066 // If a target, target update, target data, target enter data, or
2067 // target exit data construct is encountered during execution of a
2068 // target region, the behavior is unspecified.
2069 NestingProhibited = Stack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +00002070 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
2071 SourceLocation) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002072 if (isOpenMPTargetExecutionDirective(K)) {
2073 OffendingRegion = K;
2074 return true;
2075 } else
2076 return false;
2077 },
2078 false /* don't skip top directive */);
2079 CloseNesting = false;
2080 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002081 if (NestingProhibited) {
Kelvin Li2b51f722016-07-26 04:32:50 +00002082 if (OrphanSeen) {
2083 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
2084 << getOpenMPDirectiveName(CurrentRegion) << Recommend;
2085 } else {
2086 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
2087 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2088 << Recommend << getOpenMPDirectiveName(CurrentRegion);
2089 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002090 return true;
2091 }
2092 }
2093 return false;
2094}
2095
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002096static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2097 ArrayRef<OMPClause *> Clauses,
2098 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2099 bool ErrorFound = false;
2100 unsigned NamedModifiersNumber = 0;
2101 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2102 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002103 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002104 for (const auto *C : Clauses) {
2105 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2106 // At most one if clause without a directive-name-modifier can appear on
2107 // the directive.
2108 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2109 if (FoundNameModifiers[CurNM]) {
2110 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2111 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2112 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2113 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002114 } else if (CurNM != OMPD_unknown) {
2115 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002116 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002117 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002118 FoundNameModifiers[CurNM] = IC;
2119 if (CurNM == OMPD_unknown)
2120 continue;
2121 // Check if the specified name modifier is allowed for the current
2122 // directive.
2123 // At most one if clause with the particular directive-name-modifier can
2124 // appear on the directive.
2125 bool MatchFound = false;
2126 for (auto NM : AllowedNameModifiers) {
2127 if (CurNM == NM) {
2128 MatchFound = true;
2129 break;
2130 }
2131 }
2132 if (!MatchFound) {
2133 S.Diag(IC->getNameModifierLoc(),
2134 diag::err_omp_wrong_if_directive_name_modifier)
2135 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2136 ErrorFound = true;
2137 }
2138 }
2139 }
2140 // If any if clause on the directive includes a directive-name-modifier then
2141 // all if clauses on the directive must include a directive-name-modifier.
2142 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2143 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2144 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2145 diag::err_omp_no_more_if_clause);
2146 } else {
2147 std::string Values;
2148 std::string Sep(", ");
2149 unsigned AllowedCnt = 0;
2150 unsigned TotalAllowedNum =
2151 AllowedNameModifiers.size() - NamedModifiersNumber;
2152 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2153 ++Cnt) {
2154 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2155 if (!FoundNameModifiers[NM]) {
2156 Values += "'";
2157 Values += getOpenMPDirectiveName(NM);
2158 Values += "'";
2159 if (AllowedCnt + 2 == TotalAllowedNum)
2160 Values += " or ";
2161 else if (AllowedCnt + 1 != TotalAllowedNum)
2162 Values += Sep;
2163 ++AllowedCnt;
2164 }
2165 }
2166 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2167 diag::err_omp_unnamed_if_clause)
2168 << (TotalAllowedNum > 1) << Values;
2169 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002170 for (auto Loc : NameModifierLoc) {
2171 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2172 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002173 ErrorFound = true;
2174 }
2175 return ErrorFound;
2176}
2177
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002178StmtResult Sema::ActOnOpenMPExecutableDirective(
2179 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2180 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2181 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002182 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002183 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2184 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002185 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002186
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002187 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002188 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002189 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002190 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002191 if (AStmt) {
2192 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2193
2194 // Check default data sharing attributes for referenced variables.
2195 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2196 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2197 if (DSAChecker.isErrorFound())
2198 return StmtError();
2199 // Generate list of implicitly defined firstprivate variables.
2200 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002201
2202 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2203 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2204 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2205 SourceLocation(), SourceLocation())) {
2206 ClausesWithImplicit.push_back(Implicit);
2207 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2208 DSAChecker.getImplicitFirstprivate().size();
2209 } else
2210 ErrorFound = true;
2211 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002212 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002213
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002214 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002215 switch (Kind) {
2216 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002217 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2218 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002219 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002220 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002221 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002222 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2223 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002224 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002225 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002226 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2227 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002228 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002229 case OMPD_for_simd:
2230 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2231 EndLoc, VarsWithInheritedDSA);
2232 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002233 case OMPD_sections:
2234 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2235 EndLoc);
2236 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002237 case OMPD_section:
2238 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002239 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002240 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2241 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002242 case OMPD_single:
2243 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2244 EndLoc);
2245 break;
Alexander Musman80c22892014-07-17 08:54:58 +00002246 case OMPD_master:
2247 assert(ClausesWithImplicit.empty() &&
2248 "No clauses are allowed for 'omp master' directive");
2249 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2250 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002251 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00002252 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
2253 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002254 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002255 case OMPD_parallel_for:
2256 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2257 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002258 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002259 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002260 case OMPD_parallel_for_simd:
2261 Res = ActOnOpenMPParallelForSimdDirective(
2262 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002263 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00002264 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002265 case OMPD_parallel_sections:
2266 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2267 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002268 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002269 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002270 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002271 Res =
2272 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002273 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002274 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00002275 case OMPD_taskyield:
2276 assert(ClausesWithImplicit.empty() &&
2277 "No clauses are allowed for 'omp taskyield' directive");
2278 assert(AStmt == nullptr &&
2279 "No associated statement allowed for 'omp taskyield' directive");
2280 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2281 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002282 case OMPD_barrier:
2283 assert(ClausesWithImplicit.empty() &&
2284 "No clauses are allowed for 'omp barrier' directive");
2285 assert(AStmt == nullptr &&
2286 "No associated statement allowed for 'omp barrier' directive");
2287 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2288 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00002289 case OMPD_taskwait:
2290 assert(ClausesWithImplicit.empty() &&
2291 "No clauses are allowed for 'omp taskwait' directive");
2292 assert(AStmt == nullptr &&
2293 "No associated statement allowed for 'omp taskwait' directive");
2294 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
2295 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002296 case OMPD_taskgroup:
2297 assert(ClausesWithImplicit.empty() &&
2298 "No clauses are allowed for 'omp taskgroup' directive");
2299 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
2300 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00002301 case OMPD_flush:
2302 assert(AStmt == nullptr &&
2303 "No associated statement allowed for 'omp flush' directive");
2304 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
2305 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002306 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00002307 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
2308 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002309 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00002310 case OMPD_atomic:
2311 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
2312 EndLoc);
2313 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002314 case OMPD_teams:
2315 Res =
2316 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2317 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002318 case OMPD_target:
2319 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
2320 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002321 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002322 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002323 case OMPD_target_parallel:
2324 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
2325 StartLoc, EndLoc);
2326 AllowedNameModifiers.push_back(OMPD_target);
2327 AllowedNameModifiers.push_back(OMPD_parallel);
2328 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002329 case OMPD_target_parallel_for:
2330 Res = ActOnOpenMPTargetParallelForDirective(
2331 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2332 AllowedNameModifiers.push_back(OMPD_target);
2333 AllowedNameModifiers.push_back(OMPD_parallel);
2334 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002335 case OMPD_cancellation_point:
2336 assert(ClausesWithImplicit.empty() &&
2337 "No clauses are allowed for 'omp cancellation point' directive");
2338 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
2339 "cancellation point' directive");
2340 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
2341 break;
Alexey Bataev80909872015-07-02 11:25:17 +00002342 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00002343 assert(AStmt == nullptr &&
2344 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00002345 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
2346 CancelRegion);
2347 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00002348 break;
Michael Wong65f367f2015-07-21 13:44:28 +00002349 case OMPD_target_data:
2350 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
2351 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002352 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00002353 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00002354 case OMPD_target_enter_data:
2355 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
2356 EndLoc);
2357 AllowedNameModifiers.push_back(OMPD_target_enter_data);
2358 break;
Samuel Antao72590762016-01-19 20:04:50 +00002359 case OMPD_target_exit_data:
2360 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
2361 EndLoc);
2362 AllowedNameModifiers.push_back(OMPD_target_exit_data);
2363 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00002364 case OMPD_taskloop:
2365 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
2366 EndLoc, VarsWithInheritedDSA);
2367 AllowedNameModifiers.push_back(OMPD_taskloop);
2368 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002369 case OMPD_taskloop_simd:
2370 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2371 EndLoc, VarsWithInheritedDSA);
2372 AllowedNameModifiers.push_back(OMPD_taskloop);
2373 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002374 case OMPD_distribute:
2375 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
2376 EndLoc, VarsWithInheritedDSA);
2377 break;
Samuel Antao686c70c2016-05-26 17:30:50 +00002378 case OMPD_target_update:
2379 assert(!AStmt && "Statement is not allowed for target update");
2380 Res =
2381 ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, EndLoc);
2382 AllowedNameModifiers.push_back(OMPD_target_update);
2383 break;
Carlo Bertolli9925f152016-06-27 14:55:37 +00002384 case OMPD_distribute_parallel_for:
2385 Res = ActOnOpenMPDistributeParallelForDirective(
2386 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2387 AllowedNameModifiers.push_back(OMPD_parallel);
2388 break;
Kelvin Li4a39add2016-07-05 05:00:15 +00002389 case OMPD_distribute_parallel_for_simd:
2390 Res = ActOnOpenMPDistributeParallelForSimdDirective(
2391 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2392 AllowedNameModifiers.push_back(OMPD_parallel);
2393 break;
Kelvin Li787f3fc2016-07-06 04:45:38 +00002394 case OMPD_distribute_simd:
2395 Res = ActOnOpenMPDistributeSimdDirective(
2396 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2397 break;
Kelvin Lia579b912016-07-14 02:54:56 +00002398 case OMPD_target_parallel_for_simd:
2399 Res = ActOnOpenMPTargetParallelForSimdDirective(
2400 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2401 AllowedNameModifiers.push_back(OMPD_target);
2402 AllowedNameModifiers.push_back(OMPD_parallel);
2403 break;
Kelvin Li986330c2016-07-20 22:57:10 +00002404 case OMPD_target_simd:
2405 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2406 EndLoc, VarsWithInheritedDSA);
2407 AllowedNameModifiers.push_back(OMPD_target);
2408 break;
Kelvin Li02532872016-08-05 14:37:37 +00002409 case OMPD_teams_distribute:
David Majnemer9d168222016-08-05 17:44:54 +00002410 Res = ActOnOpenMPTeamsDistributeDirective(
2411 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Kelvin Li02532872016-08-05 14:37:37 +00002412 break;
Kelvin Li4e325f72016-10-25 12:50:55 +00002413 case OMPD_teams_distribute_simd:
2414 Res = ActOnOpenMPTeamsDistributeSimdDirective(
2415 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2416 break;
Kelvin Li579e41c2016-11-30 23:51:03 +00002417 case OMPD_teams_distribute_parallel_for_simd:
2418 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
2419 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2420 AllowedNameModifiers.push_back(OMPD_parallel);
2421 break;
Kelvin Li7ade93f2016-12-09 03:24:30 +00002422 case OMPD_teams_distribute_parallel_for:
2423 Res = ActOnOpenMPTeamsDistributeParallelForDirective(
2424 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2425 AllowedNameModifiers.push_back(OMPD_parallel);
2426 break;
Kelvin Libf594a52016-12-17 05:48:59 +00002427 case OMPD_target_teams:
2428 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc,
2429 EndLoc);
2430 AllowedNameModifiers.push_back(OMPD_target);
2431 break;
Kelvin Li83c451e2016-12-25 04:52:54 +00002432 case OMPD_target_teams_distribute:
2433 Res = ActOnOpenMPTargetTeamsDistributeDirective(
2434 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2435 AllowedNameModifiers.push_back(OMPD_target);
2436 break;
Kelvin Li80e8f562016-12-29 22:16:30 +00002437 case OMPD_target_teams_distribute_parallel_for:
2438 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
2439 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2440 AllowedNameModifiers.push_back(OMPD_target);
2441 AllowedNameModifiers.push_back(OMPD_parallel);
2442 break;
Kelvin Li1851df52017-01-03 05:23:48 +00002443 case OMPD_target_teams_distribute_parallel_for_simd:
2444 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
2445 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2446 AllowedNameModifiers.push_back(OMPD_target);
2447 AllowedNameModifiers.push_back(OMPD_parallel);
2448 break;
Kelvin Lida681182017-01-10 18:08:18 +00002449 case OMPD_target_teams_distribute_simd:
2450 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
2451 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2452 AllowedNameModifiers.push_back(OMPD_target);
2453 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00002454 case OMPD_declare_target:
2455 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002456 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002457 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00002458 case OMPD_declare_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002459 llvm_unreachable("OpenMP Directive is not allowed");
2460 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002461 llvm_unreachable("Unknown OpenMP directive");
2462 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002463
Alexey Bataev4acb8592014-07-07 13:01:15 +00002464 for (auto P : VarsWithInheritedDSA) {
2465 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
2466 << P.first << P.second->getSourceRange();
2467 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002468 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
2469
2470 if (!AllowedNameModifiers.empty())
2471 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
2472 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002473
Alexey Bataeved09d242014-05-28 05:53:51 +00002474 if (ErrorFound)
2475 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002476 return Res;
2477}
2478
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002479Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
2480 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00002481 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00002482 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
2483 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00002484 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00002485 assert(Linears.size() == LinModifiers.size());
2486 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00002487 if (!DG || DG.get().isNull())
2488 return DeclGroupPtrTy();
2489
2490 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00002491 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00002492 return DG;
2493 }
2494 auto *ADecl = DG.get().getSingleDecl();
2495 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
2496 ADecl = FTD->getTemplatedDecl();
2497
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002498 auto *FD = dyn_cast<FunctionDecl>(ADecl);
2499 if (!FD) {
2500 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00002501 return DeclGroupPtrTy();
2502 }
2503
Alexey Bataev2af33e32016-04-07 12:45:37 +00002504 // OpenMP [2.8.2, declare simd construct, Description]
2505 // The parameter of the simdlen clause must be a constant positive integer
2506 // expression.
2507 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002508 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00002509 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002510 // OpenMP [2.8.2, declare simd construct, Description]
2511 // The special this pointer can be used as if was one of the arguments to the
2512 // function in any of the linear, aligned, or uniform clauses.
2513 // The uniform clause declares one or more arguments to have an invariant
2514 // value for all concurrent invocations of the function in the execution of a
2515 // single SIMD loop.
Alexey Bataevecba70f2016-04-12 11:02:11 +00002516 llvm::DenseMap<Decl *, Expr *> UniformedArgs;
2517 Expr *UniformedLinearThis = nullptr;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002518 for (auto *E : Uniforms) {
2519 E = E->IgnoreParenImpCasts();
2520 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
2521 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
2522 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
2523 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00002524 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
2525 UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E));
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002526 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00002527 }
2528 if (isa<CXXThisExpr>(E)) {
2529 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002530 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00002531 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002532 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
2533 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00002534 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00002535 // OpenMP [2.8.2, declare simd construct, Description]
2536 // The aligned clause declares that the object to which each list item points
2537 // is aligned to the number of bytes expressed in the optional parameter of
2538 // the aligned clause.
2539 // The special this pointer can be used as if was one of the arguments to the
2540 // function in any of the linear, aligned, or uniform clauses.
2541 // The type of list items appearing in the aligned clause must be array,
2542 // pointer, reference to array, or reference to pointer.
2543 llvm::DenseMap<Decl *, Expr *> AlignedArgs;
2544 Expr *AlignedThis = nullptr;
2545 for (auto *E : Aligneds) {
2546 E = E->IgnoreParenImpCasts();
2547 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
2548 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
2549 auto *CanonPVD = PVD->getCanonicalDecl();
2550 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
2551 FD->getParamDecl(PVD->getFunctionScopeIndex())
2552 ->getCanonicalDecl() == CanonPVD) {
2553 // OpenMP [2.8.1, simd construct, Restrictions]
2554 // A list-item cannot appear in more than one aligned clause.
2555 if (AlignedArgs.count(CanonPVD) > 0) {
2556 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
2557 << 1 << E->getSourceRange();
2558 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
2559 diag::note_omp_explicit_dsa)
2560 << getOpenMPClauseName(OMPC_aligned);
2561 continue;
2562 }
2563 AlignedArgs[CanonPVD] = E;
2564 QualType QTy = PVD->getType()
2565 .getNonReferenceType()
2566 .getUnqualifiedType()
2567 .getCanonicalType();
2568 const Type *Ty = QTy.getTypePtrOrNull();
2569 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
2570 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
2571 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
2572 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
2573 }
2574 continue;
2575 }
2576 }
2577 if (isa<CXXThisExpr>(E)) {
2578 if (AlignedThis) {
2579 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
2580 << 2 << E->getSourceRange();
2581 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
2582 << getOpenMPClauseName(OMPC_aligned);
2583 }
2584 AlignedThis = E;
2585 continue;
2586 }
2587 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
2588 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
2589 }
2590 // The optional parameter of the aligned clause, alignment, must be a constant
2591 // positive integer expression. If no optional parameter is specified,
2592 // implementation-defined default alignments for SIMD instructions on the
2593 // target platforms are assumed.
2594 SmallVector<Expr *, 4> NewAligns;
2595 for (auto *E : Alignments) {
2596 ExprResult Align;
2597 if (E)
2598 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
2599 NewAligns.push_back(Align.get());
2600 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00002601 // OpenMP [2.8.2, declare simd construct, Description]
2602 // The linear clause declares one or more list items to be private to a SIMD
2603 // lane and to have a linear relationship with respect to the iteration space
2604 // of a loop.
2605 // The special this pointer can be used as if was one of the arguments to the
2606 // function in any of the linear, aligned, or uniform clauses.
2607 // When a linear-step expression is specified in a linear clause it must be
2608 // either a constant integer expression or an integer-typed parameter that is
2609 // specified in a uniform clause on the directive.
2610 llvm::DenseMap<Decl *, Expr *> LinearArgs;
2611 const bool IsUniformedThis = UniformedLinearThis != nullptr;
2612 auto MI = LinModifiers.begin();
2613 for (auto *E : Linears) {
2614 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
2615 ++MI;
2616 E = E->IgnoreParenImpCasts();
2617 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
2618 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
2619 auto *CanonPVD = PVD->getCanonicalDecl();
2620 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
2621 FD->getParamDecl(PVD->getFunctionScopeIndex())
2622 ->getCanonicalDecl() == CanonPVD) {
2623 // OpenMP [2.15.3.7, linear Clause, Restrictions]
2624 // A list-item cannot appear in more than one linear clause.
2625 if (LinearArgs.count(CanonPVD) > 0) {
2626 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
2627 << getOpenMPClauseName(OMPC_linear)
2628 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
2629 Diag(LinearArgs[CanonPVD]->getExprLoc(),
2630 diag::note_omp_explicit_dsa)
2631 << getOpenMPClauseName(OMPC_linear);
2632 continue;
2633 }
2634 // Each argument can appear in at most one uniform or linear clause.
2635 if (UniformedArgs.count(CanonPVD) > 0) {
2636 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
2637 << getOpenMPClauseName(OMPC_linear)
2638 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
2639 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
2640 diag::note_omp_explicit_dsa)
2641 << getOpenMPClauseName(OMPC_uniform);
2642 continue;
2643 }
2644 LinearArgs[CanonPVD] = E;
2645 if (E->isValueDependent() || E->isTypeDependent() ||
2646 E->isInstantiationDependent() ||
2647 E->containsUnexpandedParameterPack())
2648 continue;
2649 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
2650 PVD->getOriginalType());
2651 continue;
2652 }
2653 }
2654 if (isa<CXXThisExpr>(E)) {
2655 if (UniformedLinearThis) {
2656 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
2657 << getOpenMPClauseName(OMPC_linear)
2658 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
2659 << E->getSourceRange();
2660 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
2661 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
2662 : OMPC_linear);
2663 continue;
2664 }
2665 UniformedLinearThis = E;
2666 if (E->isValueDependent() || E->isTypeDependent() ||
2667 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
2668 continue;
2669 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
2670 E->getType());
2671 continue;
2672 }
2673 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
2674 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
2675 }
2676 Expr *Step = nullptr;
2677 Expr *NewStep = nullptr;
2678 SmallVector<Expr *, 4> NewSteps;
2679 for (auto *E : Steps) {
2680 // Skip the same step expression, it was checked already.
2681 if (Step == E || !E) {
2682 NewSteps.push_back(E ? NewStep : nullptr);
2683 continue;
2684 }
2685 Step = E;
2686 if (auto *DRE = dyn_cast<DeclRefExpr>(Step))
2687 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
2688 auto *CanonPVD = PVD->getCanonicalDecl();
2689 if (UniformedArgs.count(CanonPVD) == 0) {
2690 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
2691 << Step->getSourceRange();
2692 } else if (E->isValueDependent() || E->isTypeDependent() ||
2693 E->isInstantiationDependent() ||
2694 E->containsUnexpandedParameterPack() ||
2695 CanonPVD->getType()->hasIntegerRepresentation())
2696 NewSteps.push_back(Step);
2697 else {
2698 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
2699 << Step->getSourceRange();
2700 }
2701 continue;
2702 }
2703 NewStep = Step;
2704 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
2705 !Step->isInstantiationDependent() &&
2706 !Step->containsUnexpandedParameterPack()) {
2707 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
2708 .get();
2709 if (NewStep)
2710 NewStep = VerifyIntegerConstantExpression(NewStep).get();
2711 }
2712 NewSteps.push_back(NewStep);
2713 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00002714 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
2715 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00002716 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00002717 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
2718 const_cast<Expr **>(Linears.data()), Linears.size(),
2719 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
2720 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00002721 ADecl->addAttr(NewAttr);
2722 return ConvertDeclToDeclGroup(ADecl);
2723}
2724
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002725StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
2726 Stmt *AStmt,
2727 SourceLocation StartLoc,
2728 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002729 if (!AStmt)
2730 return StmtError();
2731
Alexey Bataev9959db52014-05-06 10:08:46 +00002732 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2733 // 1.2.2 OpenMP Language Terminology
2734 // Structured block - An executable statement with a single entry at the
2735 // top and a single exit at the bottom.
2736 // The point of exit cannot be a branch out of the structured block.
2737 // longjmp() and throw() must not violate the entry/exit criteria.
2738 CS->getCapturedDecl()->setNothrow();
2739
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002740 getCurFunction()->setHasBranchProtectedScope();
2741
Alexey Bataev25e5b442015-09-15 12:52:43 +00002742 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
2743 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002744}
2745
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002746namespace {
2747/// \brief Helper class for checking canonical form of the OpenMP loops and
2748/// extracting iteration space of each loop in the loop nest, that will be used
2749/// for IR generation.
2750class OpenMPIterationSpaceChecker {
2751 /// \brief Reference to Sema.
2752 Sema &SemaRef;
2753 /// \brief A location for diagnostics (when there is no some better location).
2754 SourceLocation DefaultLoc;
2755 /// \brief A location for diagnostics (when increment is not compatible).
2756 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002757 /// \brief A source location for referring to loop init later.
2758 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002759 /// \brief A source location for referring to condition later.
2760 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002761 /// \brief A source location for referring to increment later.
2762 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002763 /// \brief Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00002764 ValueDecl *LCDecl = nullptr;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002765 /// \brief Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00002766 Expr *LCRef = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002767 /// \brief Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00002768 Expr *LB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002769 /// \brief Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00002770 Expr *UB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002771 /// \brief Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00002772 Expr *Step = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002773 /// \brief This flag is true when condition is one of:
2774 /// Var < UB
2775 /// Var <= UB
2776 /// UB > Var
2777 /// UB >= Var
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00002778 bool TestIsLessOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002779 /// \brief This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00002780 bool TestIsStrictOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002781 /// \brief This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00002782 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002783
2784public:
2785 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00002786 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002787 /// \brief Check init-expr for canonical loop form and save loop counter
2788 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00002789 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002790 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
2791 /// for less/greater and for strict/non-strict comparison.
2792 bool CheckCond(Expr *S);
2793 /// \brief Check incr-expr for canonical loop form and return true if it
2794 /// does not conform, otherwise save loop step (#Step).
2795 bool CheckInc(Expr *S);
2796 /// \brief Return the loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00002797 ValueDecl *GetLoopDecl() const { return LCDecl; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002798 /// \brief Return the reference expression to loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00002799 Expr *GetLoopDeclRefExpr() const { return LCRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002800 /// \brief Source range of the loop init.
2801 SourceRange GetInitSrcRange() const { return InitSrcRange; }
2802 /// \brief Source range of the loop condition.
2803 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
2804 /// \brief Source range of the loop increment.
2805 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
2806 /// \brief True if the step should be subtracted.
2807 bool ShouldSubtractStep() const { return SubtractStep; }
2808 /// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00002809 Expr *
2810 BuildNumIterations(Scope *S, const bool LimitedType,
2811 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00002812 /// \brief Build the precondition expression for the loops.
Alexey Bataev5a3af132016-03-29 08:58:54 +00002813 Expr *BuildPreCond(Scope *S, Expr *Cond,
2814 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002815 /// \brief Build reference expression to the counter be used for codegen.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00002816 DeclRefExpr *BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures,
2817 DSAStackTy &DSA) const;
Alexey Bataeva8899172015-08-06 12:30:57 +00002818 /// \brief Build reference expression to the private counter be used for
2819 /// codegen.
2820 Expr *BuildPrivateCounterVar() const;
David Majnemer9d168222016-08-05 17:44:54 +00002821 /// \brief Build initialization of the counter be used for codegen.
Alexander Musmana5f070a2014-10-01 06:03:56 +00002822 Expr *BuildCounterInit() const;
2823 /// \brief Build step of the counter be used for codegen.
2824 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002825 /// \brief Return true if any expression is dependent.
2826 bool Dependent() const;
2827
2828private:
2829 /// \brief Check the right-hand side of an assignment in the increment
2830 /// expression.
2831 bool CheckIncRHS(Expr *RHS);
2832 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00002833 bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002834 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00002835 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00002836 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002837 /// \brief Helper to set loop increment.
2838 bool SetStep(Expr *NewStep, bool Subtract);
2839};
2840
2841bool OpenMPIterationSpaceChecker::Dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00002842 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002843 assert(!LB && !UB && !Step);
2844 return false;
2845 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00002846 return LCDecl->getType()->isDependentType() ||
2847 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
2848 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002849}
2850
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00002851static Expr *getExprAsWritten(Expr *E) {
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002852 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
2853 E = ExprTemp->getSubExpr();
2854
2855 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
2856 E = MTE->GetTemporaryExpr();
2857
2858 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
2859 E = Binder->getSubExpr();
2860
2861 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
2862 E = ICE->getSubExprAsWritten();
2863 return E->IgnoreParens();
2864}
2865
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00002866bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl,
2867 Expr *NewLCRefExpr,
2868 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002869 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00002870 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002871 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00002872 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002873 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00002874 LCDecl = getCanonicalDecl(NewLCDecl);
2875 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002876 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
2877 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002878 if ((Ctor->isCopyOrMoveConstructor() ||
2879 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2880 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002881 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002882 LB = NewLB;
2883 return false;
2884}
2885
2886bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00002887 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002888 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00002889 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
2890 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002891 if (!NewUB)
2892 return true;
2893 UB = NewUB;
2894 TestIsLessOp = LessOp;
2895 TestIsStrictOp = StrictOp;
2896 ConditionSrcRange = SR;
2897 ConditionLoc = SL;
2898 return false;
2899}
2900
2901bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
2902 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00002903 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002904 if (!NewStep)
2905 return true;
2906 if (!NewStep->isValueDependent()) {
2907 // Check that the step is integer expression.
2908 SourceLocation StepLoc = NewStep->getLocStart();
2909 ExprResult Val =
2910 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
2911 if (Val.isInvalid())
2912 return true;
2913 NewStep = Val.get();
2914
2915 // OpenMP [2.6, Canonical Loop Form, Restrictions]
2916 // If test-expr is of form var relational-op b and relational-op is < or
2917 // <= then incr-expr must cause var to increase on each iteration of the
2918 // loop. If test-expr is of form var relational-op b and relational-op is
2919 // > or >= then incr-expr must cause var to decrease on each iteration of
2920 // the loop.
2921 // If test-expr is of form b relational-op var and relational-op is < or
2922 // <= then incr-expr must cause var to decrease on each iteration of the
2923 // loop. If test-expr is of form b relational-op var and relational-op is
2924 // > or >= then incr-expr must cause var to increase on each iteration of
2925 // the loop.
2926 llvm::APSInt Result;
2927 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
2928 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
2929 bool IsConstNeg =
2930 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002931 bool IsConstPos =
2932 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002933 bool IsConstZero = IsConstant && !Result.getBoolValue();
2934 if (UB && (IsConstZero ||
2935 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00002936 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002937 SemaRef.Diag(NewStep->getExprLoc(),
2938 diag::err_omp_loop_incr_not_compatible)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00002939 << LCDecl << TestIsLessOp << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002940 SemaRef.Diag(ConditionLoc,
2941 diag::note_omp_loop_cond_requres_compatible_incr)
2942 << TestIsLessOp << ConditionSrcRange;
2943 return true;
2944 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002945 if (TestIsLessOp == Subtract) {
David Majnemer9d168222016-08-05 17:44:54 +00002946 NewStep =
2947 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep)
2948 .get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00002949 Subtract = !Subtract;
2950 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002951 }
2952
2953 Step = NewStep;
2954 SubtractStep = Subtract;
2955 return false;
2956}
2957
Alexey Bataev9c821032015-04-30 04:23:23 +00002958bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002959 // Check init-expr for canonical loop form and save loop counter
2960 // variable - #Var and its initialization value - #LB.
2961 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
2962 // var = lb
2963 // integer-type var = lb
2964 // random-access-iterator-type var = lb
2965 // pointer-type var = lb
2966 //
2967 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00002968 if (EmitDiags) {
2969 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
2970 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002971 return true;
2972 }
Tim Shen4a05bb82016-06-21 20:29:17 +00002973 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
2974 if (!ExprTemp->cleanupsHaveSideEffects())
2975 S = ExprTemp->getSubExpr();
2976
Alexander Musmana5f070a2014-10-01 06:03:56 +00002977 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002978 if (Expr *E = dyn_cast<Expr>(S))
2979 S = E->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00002980 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00002981 if (BO->getOpcode() == BO_Assign) {
2982 auto *LHS = BO->getLHS()->IgnoreParens();
2983 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
2984 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
2985 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
2986 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
2987 return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
2988 }
2989 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
2990 if (ME->isArrow() &&
2991 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
2992 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
2993 }
2994 }
David Majnemer9d168222016-08-05 17:44:54 +00002995 } else if (auto *DS = dyn_cast<DeclStmt>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002996 if (DS->isSingleDecl()) {
David Majnemer9d168222016-08-05 17:44:54 +00002997 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00002998 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002999 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00003000 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003001 SemaRef.Diag(S->getLocStart(),
3002 diag::ext_omp_loop_not_canonical_init)
3003 << S->getSourceRange();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003004 return SetLCDeclAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003005 }
3006 }
3007 }
David Majnemer9d168222016-08-05 17:44:54 +00003008 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003009 if (CE->getOperator() == OO_Equal) {
3010 auto *LHS = CE->getArg(0);
David Majnemer9d168222016-08-05 17:44:54 +00003011 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003012 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3013 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3014 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3015 return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
3016 }
3017 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3018 if (ME->isArrow() &&
3019 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3020 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3021 }
3022 }
3023 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003024
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003025 if (Dependent() || SemaRef.CurContext->isDependentContext())
3026 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00003027 if (EmitDiags) {
3028 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3029 << S->getSourceRange();
3030 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003031 return true;
3032}
3033
Alexey Bataev23b69422014-06-18 07:08:49 +00003034/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003035/// variable (which may be the loop variable) if possible.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003036static const ValueDecl *GetInitLCDecl(Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003037 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00003038 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003039 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003040 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3041 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003042 if ((Ctor->isCopyOrMoveConstructor() ||
3043 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3044 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003045 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003046 if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
3047 if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
3048 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
3049 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3050 return getCanonicalDecl(ME->getMemberDecl());
3051 return getCanonicalDecl(VD);
3052 }
3053 }
3054 if (auto *ME = dyn_cast_or_null<MemberExpr>(E))
3055 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3056 return getCanonicalDecl(ME->getMemberDecl());
3057 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003058}
3059
3060bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3061 // Check test-expr for canonical form, save upper-bound UB, flags for
3062 // less/greater and for strict/non-strict comparison.
3063 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3064 // var relational-op b
3065 // b relational-op var
3066 //
3067 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003068 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003069 return true;
3070 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003071 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003072 SourceLocation CondLoc = S->getLocStart();
David Majnemer9d168222016-08-05 17:44:54 +00003073 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003074 if (BO->isRelationalOp()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003075 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003076 return SetUB(BO->getRHS(),
3077 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3078 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3079 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003080 if (GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003081 return SetUB(BO->getLHS(),
3082 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3083 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3084 BO->getSourceRange(), BO->getOperatorLoc());
3085 }
David Majnemer9d168222016-08-05 17:44:54 +00003086 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003087 if (CE->getNumArgs() == 2) {
3088 auto Op = CE->getOperator();
3089 switch (Op) {
3090 case OO_Greater:
3091 case OO_GreaterEqual:
3092 case OO_Less:
3093 case OO_LessEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003094 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003095 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3096 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3097 CE->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003098 if (GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003099 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3100 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3101 CE->getOperatorLoc());
3102 break;
3103 default:
3104 break;
3105 }
3106 }
3107 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003108 if (Dependent() || SemaRef.CurContext->isDependentContext())
3109 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003110 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003111 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003112 return true;
3113}
3114
3115bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3116 // RHS of canonical loop form increment can be:
3117 // var + incr
3118 // incr + var
3119 // var - incr
3120 //
3121 RHS = RHS->IgnoreParenImpCasts();
David Majnemer9d168222016-08-05 17:44:54 +00003122 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003123 if (BO->isAdditiveOp()) {
3124 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003125 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003126 return SetStep(BO->getRHS(), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003127 if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003128 return SetStep(BO->getLHS(), false);
3129 }
David Majnemer9d168222016-08-05 17:44:54 +00003130 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003131 bool IsAdd = CE->getOperator() == OO_Plus;
3132 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003133 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003134 return SetStep(CE->getArg(1), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003135 if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003136 return SetStep(CE->getArg(0), false);
3137 }
3138 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003139 if (Dependent() || SemaRef.CurContext->isDependentContext())
3140 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003141 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003142 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003143 return true;
3144}
3145
3146bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3147 // Check incr-expr for canonical loop form and return true if it
3148 // does not conform.
3149 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3150 // ++var
3151 // var++
3152 // --var
3153 // var--
3154 // var += incr
3155 // var -= incr
3156 // var = var + incr
3157 // var = incr + var
3158 // var = var - incr
3159 //
3160 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003161 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003162 return true;
3163 }
Tim Shen4a05bb82016-06-21 20:29:17 +00003164 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
3165 if (!ExprTemp->cleanupsHaveSideEffects())
3166 S = ExprTemp->getSubExpr();
3167
Alexander Musmana5f070a2014-10-01 06:03:56 +00003168 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003169 S = S->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00003170 if (auto *UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003171 if (UO->isIncrementDecrementOp() &&
3172 GetInitLCDecl(UO->getSubExpr()) == LCDecl)
David Majnemer9d168222016-08-05 17:44:54 +00003173 return SetStep(SemaRef
3174 .ActOnIntegerConstant(UO->getLocStart(),
3175 (UO->isDecrementOp() ? -1 : 1))
3176 .get(),
3177 false);
3178 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003179 switch (BO->getOpcode()) {
3180 case BO_AddAssign:
3181 case BO_SubAssign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003182 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003183 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3184 break;
3185 case BO_Assign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003186 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003187 return CheckIncRHS(BO->getRHS());
3188 break;
3189 default:
3190 break;
3191 }
David Majnemer9d168222016-08-05 17:44:54 +00003192 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003193 switch (CE->getOperator()) {
3194 case OO_PlusPlus:
3195 case OO_MinusMinus:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003196 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
David Majnemer9d168222016-08-05 17:44:54 +00003197 return SetStep(SemaRef
3198 .ActOnIntegerConstant(
3199 CE->getLocStart(),
3200 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
3201 .get(),
3202 false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003203 break;
3204 case OO_PlusEqual:
3205 case OO_MinusEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003206 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003207 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3208 break;
3209 case OO_Equal:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003210 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003211 return CheckIncRHS(CE->getArg(1));
3212 break;
3213 default:
3214 break;
3215 }
3216 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003217 if (Dependent() || SemaRef.CurContext->isDependentContext())
3218 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003219 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003220 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003221 return true;
3222}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003223
Alexey Bataev5a3af132016-03-29 08:58:54 +00003224static ExprResult
3225tryBuildCapture(Sema &SemaRef, Expr *Capture,
3226 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00003227 if (SemaRef.CurContext->isDependentContext())
3228 return ExprResult(Capture);
Alexey Bataev5a3af132016-03-29 08:58:54 +00003229 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
3230 return SemaRef.PerformImplicitConversion(
3231 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
3232 /*AllowExplicit=*/true);
3233 auto I = Captures.find(Capture);
3234 if (I != Captures.end())
3235 return buildCapture(SemaRef, Capture, I->second);
3236 DeclRefExpr *Ref = nullptr;
3237 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
3238 Captures[Capture] = Ref;
3239 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003240}
3241
Alexander Musmana5f070a2014-10-01 06:03:56 +00003242/// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003243Expr *OpenMPIterationSpaceChecker::BuildNumIterations(
3244 Scope *S, const bool LimitedType,
3245 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003246 ExprResult Diff;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003247 auto VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003248 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003249 SemaRef.getLangOpts().CPlusPlus) {
3250 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003251 auto *UBExpr = TestIsLessOp ? UB : LB;
3252 auto *LBExpr = TestIsLessOp ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00003253 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
3254 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003255 if (!Upper || !Lower)
3256 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003257
3258 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3259
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003260 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003261 // BuildBinOp already emitted error, this one is to point user to upper
3262 // and lower bound, and to tell what is passed to 'operator-'.
3263 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3264 << Upper->getSourceRange() << Lower->getSourceRange();
3265 return nullptr;
3266 }
3267 }
3268
3269 if (!Diff.isUsable())
3270 return nullptr;
3271
3272 // Upper - Lower [- 1]
3273 if (TestIsStrictOp)
3274 Diff = SemaRef.BuildBinOp(
3275 S, DefaultLoc, BO_Sub, Diff.get(),
3276 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3277 if (!Diff.isUsable())
3278 return nullptr;
3279
3280 // Upper - Lower [- 1] + Step
Alexey Bataev5a3af132016-03-29 08:58:54 +00003281 auto NewStep = tryBuildCapture(SemaRef, Step, Captures);
3282 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003283 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003284 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003285 if (!Diff.isUsable())
3286 return nullptr;
3287
3288 // Parentheses (for dumping/debugging purposes only).
3289 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3290 if (!Diff.isUsable())
3291 return nullptr;
3292
3293 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003294 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003295 if (!Diff.isUsable())
3296 return nullptr;
3297
Alexander Musman174b3ca2014-10-06 11:16:29 +00003298 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003299 QualType Type = Diff.get()->getType();
3300 auto &C = SemaRef.Context;
3301 bool UseVarType = VarType->hasIntegerRepresentation() &&
3302 C.getTypeSize(Type) > C.getTypeSize(VarType);
3303 if (!Type->isIntegerType() || UseVarType) {
3304 unsigned NewSize =
3305 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
3306 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
3307 : Type->hasSignedIntegerRepresentation();
3308 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00003309 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
3310 Diff = SemaRef.PerformImplicitConversion(
3311 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
3312 if (!Diff.isUsable())
3313 return nullptr;
3314 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003315 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003316 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00003317 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
3318 if (NewSize != C.getTypeSize(Type)) {
3319 if (NewSize < C.getTypeSize(Type)) {
3320 assert(NewSize == 64 && "incorrect loop var size");
3321 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
3322 << InitSrcRange << ConditionSrcRange;
3323 }
3324 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003325 NewSize, Type->hasSignedIntegerRepresentation() ||
3326 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00003327 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
3328 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
3329 Sema::AA_Converting, true);
3330 if (!Diff.isUsable())
3331 return nullptr;
3332 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003333 }
3334 }
3335
Alexander Musmana5f070a2014-10-01 06:03:56 +00003336 return Diff.get();
3337}
3338
Alexey Bataev5a3af132016-03-29 08:58:54 +00003339Expr *OpenMPIterationSpaceChecker::BuildPreCond(
3340 Scope *S, Expr *Cond,
3341 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003342 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
3343 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3344 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003345
Alexey Bataev5a3af132016-03-29 08:58:54 +00003346 auto NewLB = tryBuildCapture(SemaRef, LB, Captures);
3347 auto NewUB = tryBuildCapture(SemaRef, UB, Captures);
3348 if (!NewLB.isUsable() || !NewUB.isUsable())
3349 return nullptr;
3350
Alexey Bataev62dbb972015-04-22 11:59:37 +00003351 auto CondExpr = SemaRef.BuildBinOp(
3352 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
3353 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003354 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003355 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00003356 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
3357 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00003358 CondExpr = SemaRef.PerformImplicitConversion(
3359 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
3360 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003361 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00003362 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3363 // Otherwise use original loop conditon and evaluate it in runtime.
3364 return CondExpr.isUsable() ? CondExpr.get() : Cond;
3365}
3366
Alexander Musmana5f070a2014-10-01 06:03:56 +00003367/// \brief Build reference expression to the counter be used for codegen.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003368DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar(
Alexey Bataev5dff95c2016-04-22 03:56:56 +00003369 llvm::MapVector<Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003370 auto *VD = dyn_cast<VarDecl>(LCDecl);
3371 if (!VD) {
3372 VD = SemaRef.IsOpenMPCapturedDecl(LCDecl);
3373 auto *Ref = buildDeclRefExpr(
3374 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00003375 DSAStackTy::DSAVarData Data = DSA.getTopDSA(LCDecl, /*FromParent=*/false);
3376 // If the loop control decl is explicitly marked as private, do not mark it
3377 // as captured again.
3378 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
3379 Captures.insert(std::make_pair(LCRef, Ref));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003380 return Ref;
3381 }
3382 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00003383 DefaultLoc);
3384}
3385
3386Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003387 if (LCDecl && !LCDecl->isInvalidDecl()) {
3388 auto Type = LCDecl->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003389 auto *PrivateVar =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003390 buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(),
3391 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00003392 if (PrivateVar->isInvalidDecl())
3393 return nullptr;
3394 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
3395 }
3396 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003397}
3398
Samuel Antao4c8035b2016-12-12 18:00:20 +00003399/// \brief Build initialization of the counter to be used for codegen.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003400Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
3401
3402/// \brief Build step of the counter be used for codegen.
3403Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
3404
3405/// \brief Iteration space of a single for loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00003406struct LoopIterationSpace final {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003407 /// \brief Condition of the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00003408 Expr *PreCond = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003409 /// \brief This expression calculates the number of iterations in the loop.
3410 /// It is always possible to calculate it before starting the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00003411 Expr *NumIterations = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003412 /// \brief The loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00003413 Expr *CounterVar = nullptr;
Alexey Bataeva8899172015-08-06 12:30:57 +00003414 /// \brief Private loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00003415 Expr *PrivateCounterVar = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003416 /// \brief This is initializer for the initial value of #CounterVar.
Alexey Bataev8b427062016-05-25 12:36:08 +00003417 Expr *CounterInit = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003418 /// \brief This is step for the #CounterVar used to generate its update:
3419 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
Alexey Bataev8b427062016-05-25 12:36:08 +00003420 Expr *CounterStep = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003421 /// \brief Should step be subtracted?
Alexey Bataev8b427062016-05-25 12:36:08 +00003422 bool Subtract = false;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003423 /// \brief Source range of the loop init.
3424 SourceRange InitSrcRange;
3425 /// \brief Source range of the loop condition.
3426 SourceRange CondSrcRange;
3427 /// \brief Source range of the loop increment.
3428 SourceRange IncSrcRange;
3429};
3430
Alexey Bataev23b69422014-06-18 07:08:49 +00003431} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003432
Alexey Bataev9c821032015-04-30 04:23:23 +00003433void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
3434 assert(getLangOpts().OpenMP && "OpenMP is not active.");
3435 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003436 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
3437 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00003438 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
3439 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003440 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
3441 if (auto *D = ISC.GetLoopDecl()) {
3442 auto *VD = dyn_cast<VarDecl>(D);
3443 if (!VD) {
3444 if (auto *Private = IsOpenMPCapturedDecl(D))
3445 VD = Private;
3446 else {
3447 auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(),
3448 /*WithInit=*/false);
3449 VD = cast<VarDecl>(Ref->getDecl());
3450 }
3451 }
3452 DSAStack->addLoopControlVariable(D, VD);
3453 }
3454 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003455 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00003456 }
3457}
3458
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003459/// \brief Called on a for stmt to check and extract its iteration space
3460/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00003461static bool CheckOpenMPIterationSpace(
3462 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
3463 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003464 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003465 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00003466 LoopIterationSpace &ResultIterSpace,
3467 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003468 // OpenMP [2.6, Canonical Loop Form]
3469 // for (init-expr; test-expr; incr-expr) structured-block
David Majnemer9d168222016-08-05 17:44:54 +00003470 auto *For = dyn_cast_or_null<ForStmt>(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003471 if (!For) {
3472 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003473 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
3474 << getOpenMPDirectiveName(DKind) << NestedLoopCount
3475 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
3476 if (NestedLoopCount > 1) {
3477 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
3478 SemaRef.Diag(DSA.getConstructLoc(),
3479 diag::note_omp_collapse_ordered_expr)
3480 << 2 << CollapseLoopCountExpr->getSourceRange()
3481 << OrderedLoopCountExpr->getSourceRange();
3482 else if (CollapseLoopCountExpr)
3483 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3484 diag::note_omp_collapse_ordered_expr)
3485 << 0 << CollapseLoopCountExpr->getSourceRange();
3486 else
3487 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3488 diag::note_omp_collapse_ordered_expr)
3489 << 1 << OrderedLoopCountExpr->getSourceRange();
3490 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003491 return true;
3492 }
3493 assert(For->getBody());
3494
3495 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3496
3497 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003498 auto Init = For->getInit();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003499 if (ISC.CheckInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003500 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003501
3502 bool HasErrors = false;
3503
3504 // Check loop variable's type.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003505 if (auto *LCDecl = ISC.GetLoopDecl()) {
3506 auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003507
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003508 // OpenMP [2.6, Canonical Loop Form]
3509 // Var is one of the following:
3510 // A variable of signed or unsigned integer type.
3511 // For C++, a variable of a random access iterator type.
3512 // For C, a variable of a pointer type.
3513 auto VarType = LCDecl->getType().getNonReferenceType();
3514 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3515 !VarType->isPointerType() &&
3516 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3517 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3518 << SemaRef.getLangOpts().CPlusPlus;
3519 HasErrors = true;
3520 }
3521
3522 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
3523 // a Construct
3524 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3525 // parallel for construct is (are) private.
3526 // The loop iteration variable in the associated for-loop of a simd
3527 // construct with just one associated for-loop is linear with a
3528 // constant-linear-step that is the increment of the associated for-loop.
3529 // Exclude loop var from the list of variables with implicitly defined data
3530 // sharing attributes.
3531 VarsWithImplicitDSA.erase(LCDecl);
3532
3533 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
3534 // in a Construct, C/C++].
3535 // The loop iteration variable in the associated for-loop of a simd
3536 // construct with just one associated for-loop may be listed in a linear
3537 // clause with a constant-linear-step that is the increment of the
3538 // associated for-loop.
3539 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3540 // parallel for construct may be listed in a private or lastprivate clause.
3541 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
3542 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3543 // declared in the loop and it is predetermined as a private.
3544 auto PredeterminedCKind =
3545 isOpenMPSimdDirective(DKind)
3546 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3547 : OMPC_private;
3548 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
3549 DVar.CKind != PredeterminedCKind) ||
3550 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
3551 isOpenMPDistributeDirective(DKind)) &&
3552 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
3553 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
3554 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
3555 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
3556 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
3557 << getOpenMPClauseName(PredeterminedCKind);
3558 if (DVar.RefExpr == nullptr)
3559 DVar.CKind = PredeterminedCKind;
3560 ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
3561 HasErrors = true;
3562 } else if (LoopDeclRefExpr != nullptr) {
3563 // Make the loop iteration variable private (for worksharing constructs),
3564 // linear (for simd directives with the only one associated loop) or
3565 // lastprivate (for simd directives with several collapsed or ordered
3566 // loops).
3567 if (DVar.CKind == OMPC_unknown)
Alexey Bataev7ace49d2016-05-17 08:55:33 +00003568 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
3569 [](OpenMPDirectiveKind) -> bool { return true; },
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003570 /*FromParent=*/false);
3571 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
3572 }
3573
3574 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
3575
3576 // Check test-expr.
3577 HasErrors |= ISC.CheckCond(For->getCond());
3578
3579 // Check incr-expr.
3580 HasErrors |= ISC.CheckInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003581 }
3582
Alexander Musmana5f070a2014-10-01 06:03:56 +00003583 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003584 return HasErrors;
3585
Alexander Musmana5f070a2014-10-01 06:03:56 +00003586 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003587 ResultIterSpace.PreCond =
3588 ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures);
Alexander Musman174b3ca2014-10-06 11:16:29 +00003589 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00003590 DSA.getCurScope(),
3591 (isOpenMPWorksharingDirective(DKind) ||
3592 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
3593 Captures);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00003594 ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures, DSA);
Alexey Bataeva8899172015-08-06 12:30:57 +00003595 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003596 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
3597 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
3598 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
3599 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
3600 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
3601 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
3602
Alexey Bataev62dbb972015-04-22 11:59:37 +00003603 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
3604 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003605 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00003606 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003607 ResultIterSpace.CounterInit == nullptr ||
3608 ResultIterSpace.CounterStep == nullptr);
3609
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003610 return HasErrors;
3611}
3612
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003613/// \brief Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003614static ExprResult
3615BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
3616 ExprResult Start,
3617 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003618 // Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003619 auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
3620 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003621 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00003622 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00003623 VarRef.get()->getType())) {
3624 NewStart = SemaRef.PerformImplicitConversion(
3625 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
3626 /*AllowExplicit=*/true);
3627 if (!NewStart.isUsable())
3628 return ExprError();
3629 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003630
3631 auto Init =
3632 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
3633 return Init;
3634}
3635
Alexander Musmana5f070a2014-10-01 06:03:56 +00003636/// \brief Build 'VarRef = Start + Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003637static ExprResult
3638BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc,
3639 ExprResult VarRef, ExprResult Start, ExprResult Iter,
3640 ExprResult Step, bool Subtract,
3641 llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003642 // Add parentheses (for debugging purposes only).
3643 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
3644 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
3645 !Step.isUsable())
3646 return ExprError();
3647
Alexey Bataev5a3af132016-03-29 08:58:54 +00003648 ExprResult NewStep = Step;
3649 if (Captures)
3650 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003651 if (NewStep.isInvalid())
3652 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003653 ExprResult Update =
3654 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003655 if (!Update.isUsable())
3656 return ExprError();
3657
Alexey Bataevc0214e02016-02-16 12:13:49 +00003658 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
3659 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003660 ExprResult NewStart = Start;
3661 if (Captures)
3662 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003663 if (NewStart.isInvalid())
3664 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003665
Alexey Bataevc0214e02016-02-16 12:13:49 +00003666 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
3667 ExprResult SavedUpdate = Update;
3668 ExprResult UpdateVal;
3669 if (VarRef.get()->getType()->isOverloadableType() ||
3670 NewStart.get()->getType()->isOverloadableType() ||
3671 Update.get()->getType()->isOverloadableType()) {
3672 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3673 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
3674 Update =
3675 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
3676 if (Update.isUsable()) {
3677 UpdateVal =
3678 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
3679 VarRef.get(), SavedUpdate.get());
3680 if (UpdateVal.isUsable()) {
3681 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
3682 UpdateVal.get());
3683 }
3684 }
3685 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3686 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003687
Alexey Bataevc0214e02016-02-16 12:13:49 +00003688 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
3689 if (!Update.isUsable() || !UpdateVal.isUsable()) {
3690 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
3691 NewStart.get(), SavedUpdate.get());
3692 if (!Update.isUsable())
3693 return ExprError();
3694
Alexey Bataev11481f52016-02-17 10:29:05 +00003695 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
3696 VarRef.get()->getType())) {
3697 Update = SemaRef.PerformImplicitConversion(
3698 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
3699 if (!Update.isUsable())
3700 return ExprError();
3701 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00003702
3703 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
3704 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003705 return Update;
3706}
3707
3708/// \brief Convert integer expression \a E to make it have at least \a Bits
3709/// bits.
David Majnemer9d168222016-08-05 17:44:54 +00003710static ExprResult WidenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003711 if (E == nullptr)
3712 return ExprError();
3713 auto &C = SemaRef.Context;
3714 QualType OldType = E->getType();
3715 unsigned HasBits = C.getTypeSize(OldType);
3716 if (HasBits >= Bits)
3717 return ExprResult(E);
3718 // OK to convert to signed, because new type has more bits than old.
3719 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
3720 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
3721 true);
3722}
3723
3724/// \brief Check if the given expression \a E is a constant integer that fits
3725/// into \a Bits bits.
3726static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
3727 if (E == nullptr)
3728 return false;
3729 llvm::APSInt Result;
3730 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
3731 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
3732 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003733}
3734
Alexey Bataev5a3af132016-03-29 08:58:54 +00003735/// Build preinits statement for the given declarations.
3736static Stmt *buildPreInits(ASTContext &Context,
3737 SmallVectorImpl<Decl *> &PreInits) {
3738 if (!PreInits.empty()) {
3739 return new (Context) DeclStmt(
3740 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
3741 SourceLocation(), SourceLocation());
3742 }
3743 return nullptr;
3744}
3745
3746/// Build preinits statement for the given declarations.
3747static Stmt *buildPreInits(ASTContext &Context,
3748 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
3749 if (!Captures.empty()) {
3750 SmallVector<Decl *, 16> PreInits;
3751 for (auto &Pair : Captures)
3752 PreInits.push_back(Pair.second->getDecl());
3753 return buildPreInits(Context, PreInits);
3754 }
3755 return nullptr;
3756}
3757
3758/// Build postupdate expression for the given list of postupdates expressions.
3759static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
3760 Expr *PostUpdate = nullptr;
3761 if (!PostUpdates.empty()) {
3762 for (auto *E : PostUpdates) {
3763 Expr *ConvE = S.BuildCStyleCastExpr(
3764 E->getExprLoc(),
3765 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
3766 E->getExprLoc(), E)
3767 .get();
3768 PostUpdate = PostUpdate
3769 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
3770 PostUpdate, ConvE)
3771 .get()
3772 : ConvE;
3773 }
3774 }
3775 return PostUpdate;
3776}
3777
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003778/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00003779/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
3780/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003781static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00003782CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
3783 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
3784 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003785 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00003786 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003787 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003788 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003789 // Found 'collapse' clause - calculate collapse number.
3790 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003791 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00003792 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00003793 }
3794 if (OrderedLoopCountExpr) {
3795 // Found 'ordered' clause - calculate collapse number.
3796 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00003797 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
3798 if (Result.getLimitedValue() < NestedLoopCount) {
3799 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3800 diag::err_omp_wrong_ordered_loop_count)
3801 << OrderedLoopCountExpr->getSourceRange();
3802 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3803 diag::note_collapse_loop_count)
3804 << CollapseLoopCountExpr->getSourceRange();
3805 }
3806 NestedLoopCount = Result.getLimitedValue();
3807 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003808 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003809 // This is helper routine for loop directives (e.g., 'for', 'simd',
3810 // 'for simd', etc.).
Alexey Bataev5a3af132016-03-29 08:58:54 +00003811 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003812 SmallVector<LoopIterationSpace, 4> IterSpaces;
3813 IterSpaces.resize(NestedLoopCount);
3814 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003815 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003816 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003817 NestedLoopCount, CollapseLoopCountExpr,
3818 OrderedLoopCountExpr, VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00003819 IterSpaces[Cnt], Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00003820 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003821 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003822 // OpenMP [2.8.1, simd construct, Restrictions]
3823 // All loops associated with the construct must be perfectly nested; that
3824 // is, there must be no intervening code nor any OpenMP directive between
3825 // any two loops.
3826 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003827 }
3828
Alexander Musmana5f070a2014-10-01 06:03:56 +00003829 Built.clear(/* size */ NestedLoopCount);
3830
3831 if (SemaRef.CurContext->isDependentContext())
3832 return NestedLoopCount;
3833
3834 // An example of what is generated for the following code:
3835 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00003836 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00003837 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003838 // for (k = 0; k < NK; ++k)
3839 // for (j = J0; j < NJ; j+=2) {
3840 // <loop body>
3841 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003842 //
3843 // We generate the code below.
3844 // Note: the loop body may be outlined in CodeGen.
3845 // Note: some counters may be C++ classes, operator- is used to find number of
3846 // iterations and operator+= to calculate counter value.
3847 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
3848 // or i64 is currently supported).
3849 //
3850 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
3851 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
3852 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
3853 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
3854 // // similar updates for vars in clauses (e.g. 'linear')
3855 // <loop body (using local i and j)>
3856 // }
3857 // i = NI; // assign final values of counters
3858 // j = NJ;
3859 //
3860
3861 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
3862 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003863 // Precondition tests if there is at least one iteration (all conditions are
3864 // true).
3865 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003866 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003867 ExprResult LastIteration32 = WidenIterationCount(
David Majnemer9d168222016-08-05 17:44:54 +00003868 32 /* Bits */, SemaRef
3869 .PerformImplicitConversion(
3870 N0->IgnoreImpCasts(), N0->getType(),
3871 Sema::AA_Converting, /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003872 .get(),
3873 SemaRef);
3874 ExprResult LastIteration64 = WidenIterationCount(
David Majnemer9d168222016-08-05 17:44:54 +00003875 64 /* Bits */, SemaRef
3876 .PerformImplicitConversion(
3877 N0->IgnoreImpCasts(), N0->getType(),
3878 Sema::AA_Converting, /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003879 .get(),
3880 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003881
3882 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
3883 return NestedLoopCount;
3884
3885 auto &C = SemaRef.Context;
3886 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
3887
3888 Scope *CurScope = DSA.getCurScope();
3889 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003890 if (PreCond.isUsable()) {
Alexey Bataeva7206b92016-12-20 16:51:02 +00003891 PreCond =
3892 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd,
3893 PreCond.get(), IterSpaces[Cnt].PreCond);
Alexey Bataev62dbb972015-04-22 11:59:37 +00003894 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003895 auto N = IterSpaces[Cnt].NumIterations;
Alexey Bataeva7206b92016-12-20 16:51:02 +00003896 SourceLocation Loc = N->getExprLoc();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003897 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
3898 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003899 LastIteration32 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00003900 CurScope, Loc, BO_Mul, LastIteration32.get(),
David Majnemer9d168222016-08-05 17:44:54 +00003901 SemaRef
3902 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3903 Sema::AA_Converting,
3904 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003905 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003906 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003907 LastIteration64 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00003908 CurScope, Loc, BO_Mul, LastIteration64.get(),
David Majnemer9d168222016-08-05 17:44:54 +00003909 SemaRef
3910 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3911 Sema::AA_Converting,
3912 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003913 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003914 }
3915
3916 // Choose either the 32-bit or 64-bit version.
3917 ExprResult LastIteration = LastIteration64;
3918 if (LastIteration32.isUsable() &&
3919 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
3920 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
3921 FitsInto(
3922 32 /* Bits */,
3923 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
3924 LastIteration64.get(), SemaRef)))
3925 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00003926 QualType VType = LastIteration.get()->getType();
3927 QualType RealVType = VType;
3928 QualType StrideVType = VType;
3929 if (isOpenMPTaskLoopDirective(DKind)) {
3930 VType =
3931 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
3932 StrideVType =
3933 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
3934 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003935
3936 if (!LastIteration.isUsable())
3937 return 0;
3938
3939 // Save the number of iterations.
3940 ExprResult NumIterations = LastIteration;
3941 {
3942 LastIteration = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00003943 CurScope, LastIteration.get()->getExprLoc(), BO_Sub,
3944 LastIteration.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003945 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3946 if (!LastIteration.isUsable())
3947 return 0;
3948 }
3949
3950 // Calculate the last iteration number beforehand instead of doing this on
3951 // each iteration. Do not do this if the number of iterations may be kfold-ed.
3952 llvm::APSInt Result;
3953 bool IsConstant =
3954 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
3955 ExprResult CalcLastIteration;
3956 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00003957 ExprResult SaveRef =
3958 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003959 LastIteration = SaveRef;
3960
3961 // Prepare SaveRef + 1.
3962 NumIterations = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00003963 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003964 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3965 if (!NumIterations.isUsable())
3966 return 0;
3967 }
3968
3969 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
3970
David Majnemer9d168222016-08-05 17:44:54 +00003971 // Build variables passed into runtime, necessary for worksharing directives.
Carlo Bertolli9925f152016-06-27 14:55:37 +00003972 ExprResult LB, UB, IL, ST, EUB, PrevLB, PrevUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003973 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
3974 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003975 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003976 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
3977 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00003978 SemaRef.AddInitializerToDecl(LBDecl,
3979 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3980 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00003981
3982 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003983 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
3984 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003985 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
Richard Smith3beb7c62017-01-12 02:27:38 +00003986 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00003987
3988 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
3989 // This will be used to implement clause 'lastprivate'.
3990 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00003991 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
3992 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00003993 SemaRef.AddInitializerToDecl(ILDecl,
3994 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3995 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00003996
3997 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00003998 VarDecl *STDecl =
3999 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
4000 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00004001 SemaRef.AddInitializerToDecl(STDecl,
4002 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4003 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00004004
4005 // Build expression: UB = min(UB, LastIteration)
David Majnemer9d168222016-08-05 17:44:54 +00004006 // It is necessary for CodeGen of directives with static scheduling.
Alexander Musmanc6388682014-12-15 07:07:06 +00004007 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4008 UB.get(), LastIteration.get());
4009 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4010 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4011 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4012 CondOp.get());
4013 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
Carlo Bertolli9925f152016-06-27 14:55:37 +00004014
4015 // If we have a combined directive that combines 'distribute', 'for' or
4016 // 'simd' we need to be able to access the bounds of the schedule of the
4017 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
4018 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
4019 if (isOpenMPLoopBoundSharingDirective(DKind)) {
4020 auto *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
4021
4022 // We expect to have at least 2 more parameters than the 'parallel'
4023 // directive does - the lower and upper bounds of the previous schedule.
4024 assert(CD->getNumParams() >= 4 &&
4025 "Unexpected number of parameters in loop combined directive");
4026
4027 // Set the proper type for the bounds given what we learned from the
4028 // enclosed loops.
4029 auto *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
4030 auto *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
4031
4032 // Previous lower and upper bounds are obtained from the region
4033 // parameters.
4034 PrevLB =
4035 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
4036 PrevUB =
4037 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
4038 }
Alexander Musmanc6388682014-12-15 07:07:06 +00004039 }
4040
4041 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004042 ExprResult IV;
4043 ExprResult Init;
4044 {
Alexey Bataev7292c292016-04-25 12:22:29 +00004045 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
4046 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
David Majnemer9d168222016-08-05 17:44:54 +00004047 Expr *RHS =
4048 (isOpenMPWorksharingDirective(DKind) ||
4049 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
4050 ? LB.get()
4051 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004052 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4053 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004054 }
4055
Alexander Musmanc6388682014-12-15 07:07:06 +00004056 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004057 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00004058 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004059 (isOpenMPWorksharingDirective(DKind) ||
4060 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004061 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4062 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4063 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004064
4065 // Loop increment (IV = IV + 1)
4066 SourceLocation IncLoc;
4067 ExprResult Inc =
4068 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4069 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4070 if (!Inc.isUsable())
4071 return 0;
4072 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00004073 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4074 if (!Inc.isUsable())
4075 return 0;
4076
4077 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4078 // Used for directives with static scheduling.
4079 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004080 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4081 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004082 // LB + ST
4083 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4084 if (!NextLB.isUsable())
4085 return 0;
4086 // LB = LB + ST
4087 NextLB =
4088 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4089 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4090 if (!NextLB.isUsable())
4091 return 0;
4092 // UB + ST
4093 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4094 if (!NextUB.isUsable())
4095 return 0;
4096 // UB = UB + ST
4097 NextUB =
4098 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4099 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4100 if (!NextUB.isUsable())
4101 return 0;
4102 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004103
4104 // Build updates and final values of the loop counters.
4105 bool HasErrors = false;
4106 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004107 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004108 Built.Updates.resize(NestedLoopCount);
4109 Built.Finals.resize(NestedLoopCount);
Alexey Bataev8b427062016-05-25 12:36:08 +00004110 SmallVector<Expr *, 4> LoopMultipliers;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004111 {
4112 ExprResult Div;
4113 // Go from inner nested loop to outer.
4114 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4115 LoopIterationSpace &IS = IterSpaces[Cnt];
4116 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4117 // Build: Iter = (IV / Div) % IS.NumIters
4118 // where Div is product of previous iterations' IS.NumIters.
4119 ExprResult Iter;
4120 if (Div.isUsable()) {
4121 Iter =
4122 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4123 } else {
4124 Iter = IV;
4125 assert((Cnt == (int)NestedLoopCount - 1) &&
4126 "unusable div expected on first iteration only");
4127 }
4128
4129 if (Cnt != 0 && Iter.isUsable())
4130 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4131 IS.NumIterations);
4132 if (!Iter.isUsable()) {
4133 HasErrors = true;
4134 break;
4135 }
4136
Alexey Bataev39f915b82015-05-08 10:41:21 +00004137 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004138 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
4139 auto *CounterVar = buildDeclRefExpr(SemaRef, VD, IS.CounterVar->getType(),
4140 IS.CounterVar->getExprLoc(),
4141 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004142 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004143 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004144 if (!Init.isUsable()) {
4145 HasErrors = true;
4146 break;
4147 }
Alexey Bataev5a3af132016-03-29 08:58:54 +00004148 ExprResult Update = BuildCounterUpdate(
4149 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
4150 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004151 if (!Update.isUsable()) {
4152 HasErrors = true;
4153 break;
4154 }
4155
4156 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4157 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00004158 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004159 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004160 if (!Final.isUsable()) {
4161 HasErrors = true;
4162 break;
4163 }
4164
4165 // Build Div for the next iteration: Div <- Div * IS.NumIters
4166 if (Cnt != 0) {
4167 if (Div.isUnset())
4168 Div = IS.NumIterations;
4169 else
4170 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4171 IS.NumIterations);
4172
4173 // Add parentheses (for debugging purposes only).
4174 if (Div.isUsable())
Alexey Bataev8b427062016-05-25 12:36:08 +00004175 Div = tryBuildCapture(SemaRef, Div.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004176 if (!Div.isUsable()) {
4177 HasErrors = true;
4178 break;
4179 }
Alexey Bataev8b427062016-05-25 12:36:08 +00004180 LoopMultipliers.push_back(Div.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004181 }
4182 if (!Update.isUsable() || !Final.isUsable()) {
4183 HasErrors = true;
4184 break;
4185 }
4186 // Save results
4187 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004188 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004189 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004190 Built.Updates[Cnt] = Update.get();
4191 Built.Finals[Cnt] = Final.get();
4192 }
4193 }
4194
4195 if (HasErrors)
4196 return 0;
4197
4198 // Save results
4199 Built.IterationVarRef = IV.get();
4200 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004201 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004202 Built.CalcLastIteration =
4203 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004204 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00004205 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004206 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004207 Built.Init = Init.get();
4208 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004209 Built.LB = LB.get();
4210 Built.UB = UB.get();
4211 Built.IL = IL.get();
4212 Built.ST = ST.get();
4213 Built.EUB = EUB.get();
4214 Built.NLB = NextLB.get();
4215 Built.NUB = NextUB.get();
Carlo Bertolli9925f152016-06-27 14:55:37 +00004216 Built.PrevLB = PrevLB.get();
4217 Built.PrevUB = PrevUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004218
Alexey Bataev8b427062016-05-25 12:36:08 +00004219 Expr *CounterVal = SemaRef.DefaultLvalueConversion(IV.get()).get();
4220 // Fill data for doacross depend clauses.
4221 for (auto Pair : DSA.getDoacrossDependClauses()) {
4222 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
4223 Pair.first->setCounterValue(CounterVal);
4224 else {
4225 if (NestedLoopCount != Pair.second.size() ||
4226 NestedLoopCount != LoopMultipliers.size() + 1) {
4227 // Erroneous case - clause has some problems.
4228 Pair.first->setCounterValue(CounterVal);
4229 continue;
4230 }
4231 assert(Pair.first->getDependencyKind() == OMPC_DEPEND_sink);
4232 auto I = Pair.second.rbegin();
4233 auto IS = IterSpaces.rbegin();
4234 auto ILM = LoopMultipliers.rbegin();
4235 Expr *UpCounterVal = CounterVal;
4236 Expr *Multiplier = nullptr;
4237 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4238 if (I->first) {
4239 assert(IS->CounterStep);
4240 Expr *NormalizedOffset =
4241 SemaRef
4242 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Div,
4243 I->first, IS->CounterStep)
4244 .get();
4245 if (Multiplier) {
4246 NormalizedOffset =
4247 SemaRef
4248 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Mul,
4249 NormalizedOffset, Multiplier)
4250 .get();
4251 }
4252 assert(I->second == OO_Plus || I->second == OO_Minus);
4253 BinaryOperatorKind BOK = (I->second == OO_Plus) ? BO_Add : BO_Sub;
David Majnemer9d168222016-08-05 17:44:54 +00004254 UpCounterVal = SemaRef
4255 .BuildBinOp(CurScope, I->first->getExprLoc(), BOK,
4256 UpCounterVal, NormalizedOffset)
4257 .get();
Alexey Bataev8b427062016-05-25 12:36:08 +00004258 }
4259 Multiplier = *ILM;
4260 ++I;
4261 ++IS;
4262 ++ILM;
4263 }
4264 Pair.first->setCounterValue(UpCounterVal);
4265 }
4266 }
4267
Alexey Bataevabfc0692014-06-25 06:52:00 +00004268 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004269}
4270
Alexey Bataev10e775f2015-07-30 11:36:16 +00004271static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004272 auto CollapseClauses =
4273 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4274 if (CollapseClauses.begin() != CollapseClauses.end())
4275 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004276 return nullptr;
4277}
4278
Alexey Bataev10e775f2015-07-30 11:36:16 +00004279static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004280 auto OrderedClauses =
4281 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4282 if (OrderedClauses.begin() != OrderedClauses.end())
4283 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004284 return nullptr;
4285}
4286
Kelvin Lic5609492016-07-15 04:39:07 +00004287static bool checkSimdlenSafelenSpecified(Sema &S,
4288 const ArrayRef<OMPClause *> Clauses) {
4289 OMPSafelenClause *Safelen = nullptr;
4290 OMPSimdlenClause *Simdlen = nullptr;
4291
4292 for (auto *Clause : Clauses) {
4293 if (Clause->getClauseKind() == OMPC_safelen)
4294 Safelen = cast<OMPSafelenClause>(Clause);
4295 else if (Clause->getClauseKind() == OMPC_simdlen)
4296 Simdlen = cast<OMPSimdlenClause>(Clause);
4297 if (Safelen && Simdlen)
4298 break;
4299 }
4300
4301 if (Simdlen && Safelen) {
4302 llvm::APSInt SimdlenRes, SafelenRes;
4303 auto SimdlenLength = Simdlen->getSimdlen();
4304 auto SafelenLength = Safelen->getSafelen();
4305 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
4306 SimdlenLength->isInstantiationDependent() ||
4307 SimdlenLength->containsUnexpandedParameterPack())
4308 return false;
4309 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
4310 SafelenLength->isInstantiationDependent() ||
4311 SafelenLength->containsUnexpandedParameterPack())
4312 return false;
4313 SimdlenLength->EvaluateAsInt(SimdlenRes, S.Context);
4314 SafelenLength->EvaluateAsInt(SafelenRes, S.Context);
4315 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
4316 // If both simdlen and safelen clauses are specified, the value of the
4317 // simdlen parameter must be less than or equal to the value of the safelen
4318 // parameter.
4319 if (SimdlenRes > SafelenRes) {
4320 S.Diag(SimdlenLength->getExprLoc(),
4321 diag::err_omp_wrong_simdlen_safelen_values)
4322 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
4323 return true;
4324 }
Alexey Bataev66b15b52015-08-21 11:14:16 +00004325 }
4326 return false;
4327}
4328
Alexey Bataev4acb8592014-07-07 13:01:15 +00004329StmtResult Sema::ActOnOpenMPSimdDirective(
4330 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4331 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004332 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004333 if (!AStmt)
4334 return StmtError();
4335
4336 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004337 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004338 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4339 // define the nested loops number.
4340 unsigned NestedLoopCount = CheckOpenMPLoop(
4341 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4342 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004343 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004344 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004345
Alexander Musmana5f070a2014-10-01 06:03:56 +00004346 assert((CurContext->isDependentContext() || B.builtAll()) &&
4347 "omp simd loop exprs were not built");
4348
Alexander Musman3276a272015-03-21 10:12:56 +00004349 if (!CurContext->isDependentContext()) {
4350 // Finalize the clauses that need pre-built expressions for CodeGen.
4351 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00004352 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexander Musman3276a272015-03-21 10:12:56 +00004353 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004354 B.NumIterations, *this, CurScope,
4355 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00004356 return StmtError();
4357 }
4358 }
4359
Kelvin Lic5609492016-07-15 04:39:07 +00004360 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00004361 return StmtError();
4362
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004363 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004364 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4365 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004366}
4367
Alexey Bataev4acb8592014-07-07 13:01:15 +00004368StmtResult Sema::ActOnOpenMPForDirective(
4369 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4370 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004371 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004372 if (!AStmt)
4373 return StmtError();
4374
4375 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004376 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004377 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4378 // define the nested loops number.
4379 unsigned NestedLoopCount = CheckOpenMPLoop(
4380 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4381 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004382 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00004383 return StmtError();
4384
Alexander Musmana5f070a2014-10-01 06:03:56 +00004385 assert((CurContext->isDependentContext() || B.builtAll()) &&
4386 "omp for loop exprs were not built");
4387
Alexey Bataev54acd402015-08-04 11:18:19 +00004388 if (!CurContext->isDependentContext()) {
4389 // Finalize the clauses that need pre-built expressions for CodeGen.
4390 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00004391 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00004392 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004393 B.NumIterations, *this, CurScope,
4394 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00004395 return StmtError();
4396 }
4397 }
4398
Alexey Bataevf29276e2014-06-18 04:14:57 +00004399 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004400 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004401 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004402}
4403
Alexander Musmanf82886e2014-09-18 05:12:34 +00004404StmtResult Sema::ActOnOpenMPForSimdDirective(
4405 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4406 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004407 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004408 if (!AStmt)
4409 return StmtError();
4410
4411 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004412 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004413 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4414 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00004415 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004416 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
4417 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4418 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004419 if (NestedLoopCount == 0)
4420 return StmtError();
4421
Alexander Musmanc6388682014-12-15 07:07:06 +00004422 assert((CurContext->isDependentContext() || B.builtAll()) &&
4423 "omp for simd loop exprs were not built");
4424
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00004425 if (!CurContext->isDependentContext()) {
4426 // Finalize the clauses that need pre-built expressions for CodeGen.
4427 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00004428 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00004429 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004430 B.NumIterations, *this, CurScope,
4431 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00004432 return StmtError();
4433 }
4434 }
4435
Kelvin Lic5609492016-07-15 04:39:07 +00004436 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00004437 return StmtError();
4438
Alexander Musmanf82886e2014-09-18 05:12:34 +00004439 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004440 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4441 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004442}
4443
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004444StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
4445 Stmt *AStmt,
4446 SourceLocation StartLoc,
4447 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004448 if (!AStmt)
4449 return StmtError();
4450
4451 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004452 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00004453 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004454 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00004455 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004456 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004457 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004458 return StmtError();
4459 // All associated statements must be '#pragma omp section' except for
4460 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004461 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004462 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4463 if (SectionStmt)
4464 Diag(SectionStmt->getLocStart(),
4465 diag::err_omp_sections_substmt_not_section);
4466 return StmtError();
4467 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004468 cast<OMPSectionDirective>(SectionStmt)
4469 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004470 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004471 } else {
4472 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
4473 return StmtError();
4474 }
4475
4476 getCurFunction()->setHasBranchProtectedScope();
4477
Alexey Bataev25e5b442015-09-15 12:52:43 +00004478 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4479 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004480}
4481
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004482StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
4483 SourceLocation StartLoc,
4484 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004485 if (!AStmt)
4486 return StmtError();
4487
4488 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004489
4490 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00004491 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004492
Alexey Bataev25e5b442015-09-15 12:52:43 +00004493 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
4494 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004495}
4496
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004497StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
4498 Stmt *AStmt,
4499 SourceLocation StartLoc,
4500 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004501 if (!AStmt)
4502 return StmtError();
4503
4504 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00004505
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004506 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00004507
Alexey Bataev3255bf32015-01-19 05:20:46 +00004508 // OpenMP [2.7.3, single Construct, Restrictions]
4509 // The copyprivate clause must not be used with the nowait clause.
4510 OMPClause *Nowait = nullptr;
4511 OMPClause *Copyprivate = nullptr;
4512 for (auto *Clause : Clauses) {
4513 if (Clause->getClauseKind() == OMPC_nowait)
4514 Nowait = Clause;
4515 else if (Clause->getClauseKind() == OMPC_copyprivate)
4516 Copyprivate = Clause;
4517 if (Copyprivate && Nowait) {
4518 Diag(Copyprivate->getLocStart(),
4519 diag::err_omp_single_copyprivate_with_nowait);
4520 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
4521 return StmtError();
4522 }
4523 }
4524
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004525 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4526}
4527
Alexander Musman80c22892014-07-17 08:54:58 +00004528StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
4529 SourceLocation StartLoc,
4530 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004531 if (!AStmt)
4532 return StmtError();
4533
4534 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00004535
4536 getCurFunction()->setHasBranchProtectedScope();
4537
4538 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
4539}
4540
Alexey Bataev28c75412015-12-15 08:19:24 +00004541StmtResult Sema::ActOnOpenMPCriticalDirective(
4542 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
4543 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004544 if (!AStmt)
4545 return StmtError();
4546
4547 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004548
Alexey Bataev28c75412015-12-15 08:19:24 +00004549 bool ErrorFound = false;
4550 llvm::APSInt Hint;
4551 SourceLocation HintLoc;
4552 bool DependentHint = false;
4553 for (auto *C : Clauses) {
4554 if (C->getClauseKind() == OMPC_hint) {
4555 if (!DirName.getName()) {
4556 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
4557 ErrorFound = true;
4558 }
4559 Expr *E = cast<OMPHintClause>(C)->getHint();
4560 if (E->isTypeDependent() || E->isValueDependent() ||
4561 E->isInstantiationDependent())
4562 DependentHint = true;
4563 else {
4564 Hint = E->EvaluateKnownConstInt(Context);
4565 HintLoc = C->getLocStart();
4566 }
4567 }
4568 }
4569 if (ErrorFound)
4570 return StmtError();
4571 auto Pair = DSAStack->getCriticalWithHint(DirName);
4572 if (Pair.first && DirName.getName() && !DependentHint) {
4573 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
4574 Diag(StartLoc, diag::err_omp_critical_with_hint);
4575 if (HintLoc.isValid()) {
4576 Diag(HintLoc, diag::note_omp_critical_hint_here)
4577 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
4578 } else
4579 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
4580 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
4581 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
4582 << 1
4583 << C->getHint()->EvaluateKnownConstInt(Context).toString(
4584 /*Radix=*/10, /*Signed=*/false);
4585 } else
4586 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
4587 }
4588 }
4589
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004590 getCurFunction()->setHasBranchProtectedScope();
4591
Alexey Bataev28c75412015-12-15 08:19:24 +00004592 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
4593 Clauses, AStmt);
4594 if (!Pair.first && DirName.getName() && !DependentHint)
4595 DSAStack->addCriticalWithHint(Dir, Hint);
4596 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004597}
4598
Alexey Bataev4acb8592014-07-07 13:01:15 +00004599StmtResult Sema::ActOnOpenMPParallelForDirective(
4600 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4601 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004602 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004603 if (!AStmt)
4604 return StmtError();
4605
Alexey Bataev4acb8592014-07-07 13:01:15 +00004606 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4607 // 1.2.2 OpenMP Language Terminology
4608 // Structured block - An executable statement with a single entry at the
4609 // top and a single exit at the bottom.
4610 // The point of exit cannot be a branch out of the structured block.
4611 // longjmp() and throw() must not violate the entry/exit criteria.
4612 CS->getCapturedDecl()->setNothrow();
4613
Alexander Musmanc6388682014-12-15 07:07:06 +00004614 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004615 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4616 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004617 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004618 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
4619 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4620 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00004621 if (NestedLoopCount == 0)
4622 return StmtError();
4623
Alexander Musmana5f070a2014-10-01 06:03:56 +00004624 assert((CurContext->isDependentContext() || B.builtAll()) &&
4625 "omp parallel for loop exprs were not built");
4626
Alexey Bataev54acd402015-08-04 11:18:19 +00004627 if (!CurContext->isDependentContext()) {
4628 // Finalize the clauses that need pre-built expressions for CodeGen.
4629 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00004630 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00004631 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004632 B.NumIterations, *this, CurScope,
4633 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00004634 return StmtError();
4635 }
4636 }
4637
Alexey Bataev4acb8592014-07-07 13:01:15 +00004638 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004639 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004640 NestedLoopCount, Clauses, AStmt, B,
4641 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00004642}
4643
Alexander Musmane4e893b2014-09-23 09:33:00 +00004644StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
4645 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4646 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004647 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004648 if (!AStmt)
4649 return StmtError();
4650
Alexander Musmane4e893b2014-09-23 09:33:00 +00004651 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4652 // 1.2.2 OpenMP Language Terminology
4653 // Structured block - An executable statement with a single entry at the
4654 // top and a single exit at the bottom.
4655 // The point of exit cannot be a branch out of the structured block.
4656 // longjmp() and throw() must not violate the entry/exit criteria.
4657 CS->getCapturedDecl()->setNothrow();
4658
Alexander Musmanc6388682014-12-15 07:07:06 +00004659 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004660 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4661 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00004662 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004663 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
4664 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4665 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004666 if (NestedLoopCount == 0)
4667 return StmtError();
4668
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00004669 if (!CurContext->isDependentContext()) {
4670 // Finalize the clauses that need pre-built expressions for CodeGen.
4671 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00004672 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00004673 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004674 B.NumIterations, *this, CurScope,
4675 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00004676 return StmtError();
4677 }
4678 }
4679
Kelvin Lic5609492016-07-15 04:39:07 +00004680 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00004681 return StmtError();
4682
Alexander Musmane4e893b2014-09-23 09:33:00 +00004683 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004684 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00004685 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004686}
4687
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004688StmtResult
4689Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
4690 Stmt *AStmt, SourceLocation StartLoc,
4691 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004692 if (!AStmt)
4693 return StmtError();
4694
4695 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004696 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00004697 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004698 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00004699 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004700 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004701 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004702 return StmtError();
4703 // All associated statements must be '#pragma omp section' except for
4704 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004705 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004706 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4707 if (SectionStmt)
4708 Diag(SectionStmt->getLocStart(),
4709 diag::err_omp_parallel_sections_substmt_not_section);
4710 return StmtError();
4711 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004712 cast<OMPSectionDirective>(SectionStmt)
4713 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004714 }
4715 } else {
4716 Diag(AStmt->getLocStart(),
4717 diag::err_omp_parallel_sections_not_compound_stmt);
4718 return StmtError();
4719 }
4720
4721 getCurFunction()->setHasBranchProtectedScope();
4722
Alexey Bataev25e5b442015-09-15 12:52:43 +00004723 return OMPParallelSectionsDirective::Create(
4724 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004725}
4726
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004727StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
4728 Stmt *AStmt, SourceLocation StartLoc,
4729 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004730 if (!AStmt)
4731 return StmtError();
4732
David Majnemer9d168222016-08-05 17:44:54 +00004733 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004734 // 1.2.2 OpenMP Language Terminology
4735 // Structured block - An executable statement with a single entry at the
4736 // top and a single exit at the bottom.
4737 // The point of exit cannot be a branch out of the structured block.
4738 // longjmp() and throw() must not violate the entry/exit criteria.
4739 CS->getCapturedDecl()->setNothrow();
4740
4741 getCurFunction()->setHasBranchProtectedScope();
4742
Alexey Bataev25e5b442015-09-15 12:52:43 +00004743 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4744 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004745}
4746
Alexey Bataev68446b72014-07-18 07:47:19 +00004747StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
4748 SourceLocation EndLoc) {
4749 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
4750}
4751
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00004752StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
4753 SourceLocation EndLoc) {
4754 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
4755}
4756
Alexey Bataev2df347a2014-07-18 10:17:07 +00004757StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
4758 SourceLocation EndLoc) {
4759 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
4760}
4761
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004762StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
4763 SourceLocation StartLoc,
4764 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004765 if (!AStmt)
4766 return StmtError();
4767
4768 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004769
4770 getCurFunction()->setHasBranchProtectedScope();
4771
4772 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
4773}
4774
Alexey Bataev6125da92014-07-21 11:26:11 +00004775StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
4776 SourceLocation StartLoc,
4777 SourceLocation EndLoc) {
4778 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
4779 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
4780}
4781
Alexey Bataev346265e2015-09-25 10:37:12 +00004782StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
4783 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004784 SourceLocation StartLoc,
4785 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00004786 OMPClause *DependFound = nullptr;
4787 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004788 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00004789 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00004790 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004791 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00004792 for (auto *C : Clauses) {
4793 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
4794 DependFound = C;
4795 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
4796 if (DependSourceClause) {
4797 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
4798 << getOpenMPDirectiveName(OMPD_ordered)
4799 << getOpenMPClauseName(OMPC_depend) << 2;
4800 ErrorFound = true;
4801 } else
4802 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004803 if (DependSinkClause) {
4804 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
4805 << 0;
4806 ErrorFound = true;
4807 }
4808 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
4809 if (DependSourceClause) {
4810 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
4811 << 1;
4812 ErrorFound = true;
4813 }
4814 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00004815 }
4816 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00004817 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004818 else if (C->getClauseKind() == OMPC_simd)
4819 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00004820 }
Alexey Bataeveb482352015-12-18 05:05:56 +00004821 if (!ErrorFound && !SC &&
4822 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004823 // OpenMP [2.8.1,simd Construct, Restrictions]
4824 // An ordered construct with the simd clause is the only OpenMP construct
4825 // that can appear in the simd region.
4826 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00004827 ErrorFound = true;
4828 } else if (DependFound && (TC || SC)) {
4829 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
4830 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
4831 ErrorFound = true;
4832 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
4833 Diag(DependFound->getLocStart(),
4834 diag::err_omp_ordered_directive_without_param);
4835 ErrorFound = true;
4836 } else if (TC || Clauses.empty()) {
4837 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
4838 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
4839 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
4840 << (TC != nullptr);
4841 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
4842 ErrorFound = true;
4843 }
4844 }
4845 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004846 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00004847
4848 if (AStmt) {
4849 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
4850
4851 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004852 }
Alexey Bataev346265e2015-09-25 10:37:12 +00004853
4854 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004855}
4856
Alexey Bataev1d160b12015-03-13 12:27:31 +00004857namespace {
4858/// \brief Helper class for checking expression in 'omp atomic [update]'
4859/// construct.
4860class OpenMPAtomicUpdateChecker {
4861 /// \brief Error results for atomic update expressions.
4862 enum ExprAnalysisErrorCode {
4863 /// \brief A statement is not an expression statement.
4864 NotAnExpression,
4865 /// \brief Expression is not builtin binary or unary operation.
4866 NotABinaryOrUnaryExpression,
4867 /// \brief Unary operation is not post-/pre- increment/decrement operation.
4868 NotAnUnaryIncDecExpression,
4869 /// \brief An expression is not of scalar type.
4870 NotAScalarType,
4871 /// \brief A binary operation is not an assignment operation.
4872 NotAnAssignmentOp,
4873 /// \brief RHS part of the binary operation is not a binary expression.
4874 NotABinaryExpression,
4875 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
4876 /// expression.
4877 NotABinaryOperator,
4878 /// \brief RHS binary operation does not have reference to the updated LHS
4879 /// part.
4880 NotAnUpdateExpression,
4881 /// \brief No errors is found.
4882 NoError
4883 };
4884 /// \brief Reference to Sema.
4885 Sema &SemaRef;
4886 /// \brief A location for note diagnostics (when error is found).
4887 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004888 /// \brief 'x' lvalue part of the source atomic expression.
4889 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004890 /// \brief 'expr' rvalue part of the source atomic expression.
4891 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004892 /// \brief Helper expression of the form
4893 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4894 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4895 Expr *UpdateExpr;
4896 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
4897 /// important for non-associative operations.
4898 bool IsXLHSInRHSPart;
4899 BinaryOperatorKind Op;
4900 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004901 /// \brief true if the source expression is a postfix unary operation, false
4902 /// if it is a prefix unary operation.
4903 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004904
4905public:
4906 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00004907 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00004908 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00004909 /// \brief Check specified statement that it is suitable for 'atomic update'
4910 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00004911 /// expression. If DiagId and NoteId == 0, then only check is performed
4912 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00004913 /// \param DiagId Diagnostic which should be emitted if error is found.
4914 /// \param NoteId Diagnostic note for the main error message.
4915 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00004916 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004917 /// \brief Return the 'x' lvalue part of the source atomic expression.
4918 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00004919 /// \brief Return the 'expr' rvalue part of the source atomic expression.
4920 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00004921 /// \brief Return the update expression used in calculation of the updated
4922 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4923 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4924 Expr *getUpdateExpr() const { return UpdateExpr; }
4925 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
4926 /// false otherwise.
4927 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
4928
Alexey Bataevb78ca832015-04-01 03:33:17 +00004929 /// \brief true if the source expression is a postfix unary operation, false
4930 /// if it is a prefix unary operation.
4931 bool isPostfixUpdate() const { return IsPostfixUpdate; }
4932
Alexey Bataev1d160b12015-03-13 12:27:31 +00004933private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00004934 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
4935 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004936};
4937} // namespace
4938
4939bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
4940 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
4941 ExprAnalysisErrorCode ErrorFound = NoError;
4942 SourceLocation ErrorLoc, NoteLoc;
4943 SourceRange ErrorRange, NoteRange;
4944 // Allowed constructs are:
4945 // x = x binop expr;
4946 // x = expr binop x;
4947 if (AtomicBinOp->getOpcode() == BO_Assign) {
4948 X = AtomicBinOp->getLHS();
4949 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
4950 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
4951 if (AtomicInnerBinOp->isMultiplicativeOp() ||
4952 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
4953 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00004954 Op = AtomicInnerBinOp->getOpcode();
4955 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004956 auto *LHS = AtomicInnerBinOp->getLHS();
4957 auto *RHS = AtomicInnerBinOp->getRHS();
4958 llvm::FoldingSetNodeID XId, LHSId, RHSId;
4959 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
4960 /*Canonical=*/true);
4961 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
4962 /*Canonical=*/true);
4963 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
4964 /*Canonical=*/true);
4965 if (XId == LHSId) {
4966 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004967 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004968 } else if (XId == RHSId) {
4969 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004970 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004971 } else {
4972 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4973 ErrorRange = AtomicInnerBinOp->getSourceRange();
4974 NoteLoc = X->getExprLoc();
4975 NoteRange = X->getSourceRange();
4976 ErrorFound = NotAnUpdateExpression;
4977 }
4978 } else {
4979 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4980 ErrorRange = AtomicInnerBinOp->getSourceRange();
4981 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
4982 NoteRange = SourceRange(NoteLoc, NoteLoc);
4983 ErrorFound = NotABinaryOperator;
4984 }
4985 } else {
4986 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
4987 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
4988 ErrorFound = NotABinaryExpression;
4989 }
4990 } else {
4991 ErrorLoc = AtomicBinOp->getExprLoc();
4992 ErrorRange = AtomicBinOp->getSourceRange();
4993 NoteLoc = AtomicBinOp->getOperatorLoc();
4994 NoteRange = SourceRange(NoteLoc, NoteLoc);
4995 ErrorFound = NotAnAssignmentOp;
4996 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004997 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004998 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
4999 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5000 return true;
5001 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005002 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005003 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005004}
5005
5006bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5007 unsigned NoteId) {
5008 ExprAnalysisErrorCode ErrorFound = NoError;
5009 SourceLocation ErrorLoc, NoteLoc;
5010 SourceRange ErrorRange, NoteRange;
5011 // Allowed constructs are:
5012 // x++;
5013 // x--;
5014 // ++x;
5015 // --x;
5016 // x binop= expr;
5017 // x = x binop expr;
5018 // x = expr binop x;
5019 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5020 AtomicBody = AtomicBody->IgnoreParenImpCasts();
5021 if (AtomicBody->getType()->isScalarType() ||
5022 AtomicBody->isInstantiationDependent()) {
5023 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5024 AtomicBody->IgnoreParenImpCasts())) {
5025 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005026 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00005027 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00005028 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005029 E = AtomicCompAssignOp->getRHS();
Kelvin Li4f161cf2016-07-20 19:41:17 +00005030 X = AtomicCompAssignOp->getLHS()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005031 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005032 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5033 AtomicBody->IgnoreParenImpCasts())) {
5034 // Check for Binary Operation
David Majnemer9d168222016-08-05 17:44:54 +00005035 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
Alexey Bataevb4505a72015-03-30 05:20:59 +00005036 return true;
David Majnemer9d168222016-08-05 17:44:54 +00005037 } else if (auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
5038 AtomicBody->IgnoreParenImpCasts())) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005039 // Check for Unary Operation
5040 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005041 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005042 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5043 OpLoc = AtomicUnaryOp->getOperatorLoc();
Kelvin Li4f161cf2016-07-20 19:41:17 +00005044 X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005045 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5046 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005047 } else {
5048 ErrorFound = NotAnUnaryIncDecExpression;
5049 ErrorLoc = AtomicUnaryOp->getExprLoc();
5050 ErrorRange = AtomicUnaryOp->getSourceRange();
5051 NoteLoc = AtomicUnaryOp->getOperatorLoc();
5052 NoteRange = SourceRange(NoteLoc, NoteLoc);
5053 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005054 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005055 ErrorFound = NotABinaryOrUnaryExpression;
5056 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5057 NoteRange = ErrorRange = AtomicBody->getSourceRange();
5058 }
5059 } else {
5060 ErrorFound = NotAScalarType;
5061 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
5062 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5063 }
5064 } else {
5065 ErrorFound = NotAnExpression;
5066 NoteLoc = ErrorLoc = S->getLocStart();
5067 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5068 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005069 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005070 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5071 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5072 return true;
5073 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005074 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005075 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005076 // Build an update expression of form 'OpaqueValueExpr(x) binop
5077 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
5078 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
5079 auto *OVEX = new (SemaRef.getASTContext())
5080 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
5081 auto *OVEExpr = new (SemaRef.getASTContext())
5082 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
5083 auto Update =
5084 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
5085 IsXLHSInRHSPart ? OVEExpr : OVEX);
5086 if (Update.isInvalid())
5087 return true;
5088 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
5089 Sema::AA_Casting);
5090 if (Update.isInvalid())
5091 return true;
5092 UpdateExpr = Update.get();
5093 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00005094 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005095}
5096
Alexey Bataev0162e452014-07-22 10:10:35 +00005097StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5098 Stmt *AStmt,
5099 SourceLocation StartLoc,
5100 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005101 if (!AStmt)
5102 return StmtError();
5103
David Majnemer9d168222016-08-05 17:44:54 +00005104 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00005105 // 1.2.2 OpenMP Language Terminology
5106 // Structured block - An executable statement with a single entry at the
5107 // top and a single exit at the bottom.
5108 // The point of exit cannot be a branch out of the structured block.
5109 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00005110 OpenMPClauseKind AtomicKind = OMPC_unknown;
5111 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005112 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00005113 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00005114 C->getClauseKind() == OMPC_update ||
5115 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00005116 if (AtomicKind != OMPC_unknown) {
5117 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5118 << SourceRange(C->getLocStart(), C->getLocEnd());
5119 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5120 << getOpenMPClauseName(AtomicKind);
5121 } else {
5122 AtomicKind = C->getClauseKind();
5123 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005124 }
5125 }
5126 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005127
Alexey Bataev459dec02014-07-24 06:46:57 +00005128 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00005129 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5130 Body = EWC->getSubExpr();
5131
Alexey Bataev62cec442014-11-18 10:14:22 +00005132 Expr *X = nullptr;
5133 Expr *V = nullptr;
5134 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005135 Expr *UE = nullptr;
5136 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005137 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00005138 // OpenMP [2.12.6, atomic Construct]
5139 // In the next expressions:
5140 // * x and v (as applicable) are both l-value expressions with scalar type.
5141 // * During the execution of an atomic region, multiple syntactic
5142 // occurrences of x must designate the same storage location.
5143 // * Neither of v and expr (as applicable) may access the storage location
5144 // designated by x.
5145 // * Neither of x and expr (as applicable) may access the storage location
5146 // designated by v.
5147 // * expr is an expression with scalar type.
5148 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5149 // * binop, binop=, ++, and -- are not overloaded operators.
5150 // * The expression x binop expr must be numerically equivalent to x binop
5151 // (expr). This requirement is satisfied if the operators in expr have
5152 // precedence greater than binop, or by using parentheses around expr or
5153 // subexpressions of expr.
5154 // * The expression expr binop x must be numerically equivalent to (expr)
5155 // binop x. This requirement is satisfied if the operators in expr have
5156 // precedence equal to or greater than binop, or by using parentheses around
5157 // expr or subexpressions of expr.
5158 // * For forms that allow multiple occurrences of x, the number of times
5159 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00005160 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005161 enum {
5162 NotAnExpression,
5163 NotAnAssignmentOp,
5164 NotAScalarType,
5165 NotAnLValue,
5166 NoError
5167 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00005168 SourceLocation ErrorLoc, NoteLoc;
5169 SourceRange ErrorRange, NoteRange;
5170 // If clause is read:
5171 // v = x;
David Majnemer9d168222016-08-05 17:44:54 +00005172 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5173 auto *AtomicBinOp =
Alexey Bataev62cec442014-11-18 10:14:22 +00005174 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5175 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5176 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5177 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5178 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5179 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5180 if (!X->isLValue() || !V->isLValue()) {
5181 auto NotLValueExpr = X->isLValue() ? V : X;
5182 ErrorFound = NotAnLValue;
5183 ErrorLoc = AtomicBinOp->getExprLoc();
5184 ErrorRange = AtomicBinOp->getSourceRange();
5185 NoteLoc = NotLValueExpr->getExprLoc();
5186 NoteRange = NotLValueExpr->getSourceRange();
5187 }
5188 } else if (!X->isInstantiationDependent() ||
5189 !V->isInstantiationDependent()) {
5190 auto NotScalarExpr =
5191 (X->isInstantiationDependent() || X->getType()->isScalarType())
5192 ? V
5193 : X;
5194 ErrorFound = NotAScalarType;
5195 ErrorLoc = AtomicBinOp->getExprLoc();
5196 ErrorRange = AtomicBinOp->getSourceRange();
5197 NoteLoc = NotScalarExpr->getExprLoc();
5198 NoteRange = NotScalarExpr->getSourceRange();
5199 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005200 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00005201 ErrorFound = NotAnAssignmentOp;
5202 ErrorLoc = AtomicBody->getExprLoc();
5203 ErrorRange = AtomicBody->getSourceRange();
5204 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5205 : AtomicBody->getExprLoc();
5206 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5207 : AtomicBody->getSourceRange();
5208 }
5209 } else {
5210 ErrorFound = NotAnExpression;
5211 NoteLoc = ErrorLoc = Body->getLocStart();
5212 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005213 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005214 if (ErrorFound != NoError) {
5215 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
5216 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005217 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5218 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00005219 return StmtError();
5220 } else if (CurContext->isDependentContext())
5221 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00005222 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005223 enum {
5224 NotAnExpression,
5225 NotAnAssignmentOp,
5226 NotAScalarType,
5227 NotAnLValue,
5228 NoError
5229 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005230 SourceLocation ErrorLoc, NoteLoc;
5231 SourceRange ErrorRange, NoteRange;
5232 // If clause is write:
5233 // x = expr;
David Majnemer9d168222016-08-05 17:44:54 +00005234 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5235 auto *AtomicBinOp =
Alexey Bataevf33eba62014-11-28 07:21:40 +00005236 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5237 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00005238 X = AtomicBinOp->getLHS();
5239 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00005240 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5241 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
5242 if (!X->isLValue()) {
5243 ErrorFound = NotAnLValue;
5244 ErrorLoc = AtomicBinOp->getExprLoc();
5245 ErrorRange = AtomicBinOp->getSourceRange();
5246 NoteLoc = X->getExprLoc();
5247 NoteRange = X->getSourceRange();
5248 }
5249 } else if (!X->isInstantiationDependent() ||
5250 !E->isInstantiationDependent()) {
5251 auto NotScalarExpr =
5252 (X->isInstantiationDependent() || X->getType()->isScalarType())
5253 ? E
5254 : X;
5255 ErrorFound = NotAScalarType;
5256 ErrorLoc = AtomicBinOp->getExprLoc();
5257 ErrorRange = AtomicBinOp->getSourceRange();
5258 NoteLoc = NotScalarExpr->getExprLoc();
5259 NoteRange = NotScalarExpr->getSourceRange();
5260 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005261 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00005262 ErrorFound = NotAnAssignmentOp;
5263 ErrorLoc = AtomicBody->getExprLoc();
5264 ErrorRange = AtomicBody->getSourceRange();
5265 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5266 : AtomicBody->getExprLoc();
5267 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5268 : AtomicBody->getSourceRange();
5269 }
5270 } else {
5271 ErrorFound = NotAnExpression;
5272 NoteLoc = ErrorLoc = Body->getLocStart();
5273 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005274 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00005275 if (ErrorFound != NoError) {
5276 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
5277 << ErrorRange;
5278 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5279 << NoteRange;
5280 return StmtError();
5281 } else if (CurContext->isDependentContext())
5282 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005283 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005284 // If clause is update:
5285 // x++;
5286 // x--;
5287 // ++x;
5288 // --x;
5289 // x binop= expr;
5290 // x = x binop expr;
5291 // x = expr binop x;
5292 OpenMPAtomicUpdateChecker Checker(*this);
5293 if (Checker.checkStatement(
5294 Body, (AtomicKind == OMPC_update)
5295 ? diag::err_omp_atomic_update_not_expression_statement
5296 : diag::err_omp_atomic_not_expression_statement,
5297 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00005298 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005299 if (!CurContext->isDependentContext()) {
5300 E = Checker.getExpr();
5301 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005302 UE = Checker.getUpdateExpr();
5303 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00005304 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005305 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005306 enum {
5307 NotAnAssignmentOp,
5308 NotACompoundStatement,
5309 NotTwoSubstatements,
5310 NotASpecificExpression,
5311 NoError
5312 } ErrorFound = NoError;
5313 SourceLocation ErrorLoc, NoteLoc;
5314 SourceRange ErrorRange, NoteRange;
5315 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5316 // If clause is a capture:
5317 // v = x++;
5318 // v = x--;
5319 // v = ++x;
5320 // v = --x;
5321 // v = x binop= expr;
5322 // v = x = x binop expr;
5323 // v = x = expr binop x;
5324 auto *AtomicBinOp =
5325 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5326 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5327 V = AtomicBinOp->getLHS();
5328 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5329 OpenMPAtomicUpdateChecker Checker(*this);
5330 if (Checker.checkStatement(
5331 Body, diag::err_omp_atomic_capture_not_expression_statement,
5332 diag::note_omp_atomic_update))
5333 return StmtError();
5334 E = Checker.getExpr();
5335 X = Checker.getX();
5336 UE = Checker.getUpdateExpr();
5337 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5338 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00005339 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005340 ErrorLoc = AtomicBody->getExprLoc();
5341 ErrorRange = AtomicBody->getSourceRange();
5342 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5343 : AtomicBody->getExprLoc();
5344 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5345 : AtomicBody->getSourceRange();
5346 ErrorFound = NotAnAssignmentOp;
5347 }
5348 if (ErrorFound != NoError) {
5349 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
5350 << ErrorRange;
5351 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5352 return StmtError();
5353 } else if (CurContext->isDependentContext()) {
5354 UE = V = E = X = nullptr;
5355 }
5356 } else {
5357 // If clause is a capture:
5358 // { v = x; x = expr; }
5359 // { v = x; x++; }
5360 // { v = x; x--; }
5361 // { v = x; ++x; }
5362 // { v = x; --x; }
5363 // { v = x; x binop= expr; }
5364 // { v = x; x = x binop expr; }
5365 // { v = x; x = expr binop x; }
5366 // { x++; v = x; }
5367 // { x--; v = x; }
5368 // { ++x; v = x; }
5369 // { --x; v = x; }
5370 // { x binop= expr; v = x; }
5371 // { x = x binop expr; v = x; }
5372 // { x = expr binop x; v = x; }
5373 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
5374 // Check that this is { expr1; expr2; }
5375 if (CS->size() == 2) {
5376 auto *First = CS->body_front();
5377 auto *Second = CS->body_back();
5378 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
5379 First = EWC->getSubExpr()->IgnoreParenImpCasts();
5380 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
5381 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
5382 // Need to find what subexpression is 'v' and what is 'x'.
5383 OpenMPAtomicUpdateChecker Checker(*this);
5384 bool IsUpdateExprFound = !Checker.checkStatement(Second);
5385 BinaryOperator *BinOp = nullptr;
5386 if (IsUpdateExprFound) {
5387 BinOp = dyn_cast<BinaryOperator>(First);
5388 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5389 }
5390 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5391 // { v = x; x++; }
5392 // { v = x; x--; }
5393 // { v = x; ++x; }
5394 // { v = x; --x; }
5395 // { v = x; x binop= expr; }
5396 // { v = x; x = x binop expr; }
5397 // { v = x; x = expr binop x; }
5398 // Check that the first expression has form v = x.
5399 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5400 llvm::FoldingSetNodeID XId, PossibleXId;
5401 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5402 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5403 IsUpdateExprFound = XId == PossibleXId;
5404 if (IsUpdateExprFound) {
5405 V = BinOp->getLHS();
5406 X = Checker.getX();
5407 E = Checker.getExpr();
5408 UE = Checker.getUpdateExpr();
5409 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005410 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005411 }
5412 }
5413 if (!IsUpdateExprFound) {
5414 IsUpdateExprFound = !Checker.checkStatement(First);
5415 BinOp = nullptr;
5416 if (IsUpdateExprFound) {
5417 BinOp = dyn_cast<BinaryOperator>(Second);
5418 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5419 }
5420 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5421 // { x++; v = x; }
5422 // { x--; v = x; }
5423 // { ++x; v = x; }
5424 // { --x; v = x; }
5425 // { x binop= expr; v = x; }
5426 // { x = x binop expr; v = x; }
5427 // { x = expr binop x; v = x; }
5428 // Check that the second expression has form v = x.
5429 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5430 llvm::FoldingSetNodeID XId, PossibleXId;
5431 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5432 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5433 IsUpdateExprFound = XId == PossibleXId;
5434 if (IsUpdateExprFound) {
5435 V = BinOp->getLHS();
5436 X = Checker.getX();
5437 E = Checker.getExpr();
5438 UE = Checker.getUpdateExpr();
5439 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005440 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005441 }
5442 }
5443 }
5444 if (!IsUpdateExprFound) {
5445 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00005446 auto *FirstExpr = dyn_cast<Expr>(First);
5447 auto *SecondExpr = dyn_cast<Expr>(Second);
5448 if (!FirstExpr || !SecondExpr ||
5449 !(FirstExpr->isInstantiationDependent() ||
5450 SecondExpr->isInstantiationDependent())) {
5451 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
5452 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005453 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00005454 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
5455 : First->getLocStart();
5456 NoteRange = ErrorRange = FirstBinOp
5457 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00005458 : SourceRange(ErrorLoc, ErrorLoc);
5459 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005460 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
5461 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
5462 ErrorFound = NotAnAssignmentOp;
5463 NoteLoc = ErrorLoc = SecondBinOp
5464 ? SecondBinOp->getOperatorLoc()
5465 : Second->getLocStart();
5466 NoteRange = ErrorRange =
5467 SecondBinOp ? SecondBinOp->getSourceRange()
5468 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00005469 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005470 auto *PossibleXRHSInFirst =
5471 FirstBinOp->getRHS()->IgnoreParenImpCasts();
5472 auto *PossibleXLHSInSecond =
5473 SecondBinOp->getLHS()->IgnoreParenImpCasts();
5474 llvm::FoldingSetNodeID X1Id, X2Id;
5475 PossibleXRHSInFirst->Profile(X1Id, Context,
5476 /*Canonical=*/true);
5477 PossibleXLHSInSecond->Profile(X2Id, Context,
5478 /*Canonical=*/true);
5479 IsUpdateExprFound = X1Id == X2Id;
5480 if (IsUpdateExprFound) {
5481 V = FirstBinOp->getLHS();
5482 X = SecondBinOp->getLHS();
5483 E = SecondBinOp->getRHS();
5484 UE = nullptr;
5485 IsXLHSInRHSPart = false;
5486 IsPostfixUpdate = true;
5487 } else {
5488 ErrorFound = NotASpecificExpression;
5489 ErrorLoc = FirstBinOp->getExprLoc();
5490 ErrorRange = FirstBinOp->getSourceRange();
5491 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
5492 NoteRange = SecondBinOp->getRHS()->getSourceRange();
5493 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005494 }
5495 }
5496 }
5497 }
5498 } else {
5499 NoteLoc = ErrorLoc = Body->getLocStart();
5500 NoteRange = ErrorRange =
5501 SourceRange(Body->getLocStart(), Body->getLocStart());
5502 ErrorFound = NotTwoSubstatements;
5503 }
5504 } else {
5505 NoteLoc = ErrorLoc = Body->getLocStart();
5506 NoteRange = ErrorRange =
5507 SourceRange(Body->getLocStart(), Body->getLocStart());
5508 ErrorFound = NotACompoundStatement;
5509 }
5510 if (ErrorFound != NoError) {
5511 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
5512 << ErrorRange;
5513 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5514 return StmtError();
5515 } else if (CurContext->isDependentContext()) {
5516 UE = V = E = X = nullptr;
5517 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005518 }
Alexey Bataevdea47612014-07-23 07:46:59 +00005519 }
Alexey Bataev0162e452014-07-22 10:10:35 +00005520
5521 getCurFunction()->setHasBranchProtectedScope();
5522
Alexey Bataev62cec442014-11-18 10:14:22 +00005523 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00005524 X, V, E, UE, IsXLHSInRHSPart,
5525 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00005526}
5527
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005528StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
5529 Stmt *AStmt,
5530 SourceLocation StartLoc,
5531 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005532 if (!AStmt)
5533 return StmtError();
5534
Samuel Antao4af1b7b2015-12-02 17:44:43 +00005535 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5536 // 1.2.2 OpenMP Language Terminology
5537 // Structured block - An executable statement with a single entry at the
5538 // top and a single exit at the bottom.
5539 // The point of exit cannot be a branch out of the structured block.
5540 // longjmp() and throw() must not violate the entry/exit criteria.
5541 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005542
Alexey Bataev13314bf2014-10-09 04:18:56 +00005543 // OpenMP [2.16, Nesting of Regions]
5544 // If specified, a teams construct must be contained within a target
5545 // construct. That target construct must contain no statements or directives
5546 // outside of the teams construct.
5547 if (DSAStack->hasInnerTeamsRegion()) {
5548 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
5549 bool OMPTeamsFound = true;
5550 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
5551 auto I = CS->body_begin();
5552 while (I != CS->body_end()) {
David Majnemer9d168222016-08-05 17:44:54 +00005553 auto *OED = dyn_cast<OMPExecutableDirective>(*I);
Alexey Bataev13314bf2014-10-09 04:18:56 +00005554 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
5555 OMPTeamsFound = false;
5556 break;
5557 }
5558 ++I;
5559 }
5560 assert(I != CS->body_end() && "Not found statement");
5561 S = *I;
Kelvin Li3834dce2016-06-27 19:15:43 +00005562 } else {
5563 auto *OED = dyn_cast<OMPExecutableDirective>(S);
5564 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
Alexey Bataev13314bf2014-10-09 04:18:56 +00005565 }
5566 if (!OMPTeamsFound) {
5567 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
5568 Diag(DSAStack->getInnerTeamsRegionLoc(),
5569 diag::note_omp_nested_teams_construct_here);
5570 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
5571 << isa<OMPExecutableDirective>(S);
5572 return StmtError();
5573 }
5574 }
5575
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005576 getCurFunction()->setHasBranchProtectedScope();
5577
5578 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5579}
5580
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00005581StmtResult
5582Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
5583 Stmt *AStmt, SourceLocation StartLoc,
5584 SourceLocation EndLoc) {
5585 if (!AStmt)
5586 return StmtError();
5587
5588 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5589 // 1.2.2 OpenMP Language Terminology
5590 // Structured block - An executable statement with a single entry at the
5591 // top and a single exit at the bottom.
5592 // The point of exit cannot be a branch out of the structured block.
5593 // longjmp() and throw() must not violate the entry/exit criteria.
5594 CS->getCapturedDecl()->setNothrow();
5595
5596 getCurFunction()->setHasBranchProtectedScope();
5597
5598 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5599 AStmt);
5600}
5601
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00005602StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
5603 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5604 SourceLocation EndLoc,
5605 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5606 if (!AStmt)
5607 return StmtError();
5608
5609 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5610 // 1.2.2 OpenMP Language Terminology
5611 // Structured block - An executable statement with a single entry at the
5612 // top and a single exit at the bottom.
5613 // The point of exit cannot be a branch out of the structured block.
5614 // longjmp() and throw() must not violate the entry/exit criteria.
5615 CS->getCapturedDecl()->setNothrow();
5616
5617 OMPLoopDirective::HelperExprs B;
5618 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5619 // define the nested loops number.
5620 unsigned NestedLoopCount =
5621 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
5622 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5623 VarsWithImplicitDSA, B);
5624 if (NestedLoopCount == 0)
5625 return StmtError();
5626
5627 assert((CurContext->isDependentContext() || B.builtAll()) &&
5628 "omp target parallel for loop exprs were not built");
5629
5630 if (!CurContext->isDependentContext()) {
5631 // Finalize the clauses that need pre-built expressions for CodeGen.
5632 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005633 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00005634 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005635 B.NumIterations, *this, CurScope,
5636 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00005637 return StmtError();
5638 }
5639 }
5640
5641 getCurFunction()->setHasBranchProtectedScope();
5642 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
5643 NestedLoopCount, Clauses, AStmt,
5644 B, DSAStack->isCancelRegion());
5645}
5646
Samuel Antaodf67fc42016-01-19 19:15:56 +00005647/// \brief Check for existence of a map clause in the list of clauses.
5648static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
5649 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
5650 I != E; ++I) {
5651 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
5652 return true;
5653 }
5654 }
5655
5656 return false;
5657}
5658
Michael Wong65f367f2015-07-21 13:44:28 +00005659StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
5660 Stmt *AStmt,
5661 SourceLocation StartLoc,
5662 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005663 if (!AStmt)
5664 return StmtError();
5665
5666 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5667
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00005668 // OpenMP [2.10.1, Restrictions, p. 97]
5669 // At least one map clause must appear on the directive.
5670 if (!HasMapClause(Clauses)) {
David Majnemer9d168222016-08-05 17:44:54 +00005671 Diag(StartLoc, diag::err_omp_no_map_for_directive)
5672 << getOpenMPDirectiveName(OMPD_target_data);
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00005673 return StmtError();
5674 }
5675
Michael Wong65f367f2015-07-21 13:44:28 +00005676 getCurFunction()->setHasBranchProtectedScope();
5677
5678 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
5679 AStmt);
5680}
5681
Samuel Antaodf67fc42016-01-19 19:15:56 +00005682StmtResult
5683Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
5684 SourceLocation StartLoc,
5685 SourceLocation EndLoc) {
5686 // OpenMP [2.10.2, Restrictions, p. 99]
5687 // At least one map clause must appear on the directive.
5688 if (!HasMapClause(Clauses)) {
5689 Diag(StartLoc, diag::err_omp_no_map_for_directive)
5690 << getOpenMPDirectiveName(OMPD_target_enter_data);
5691 return StmtError();
5692 }
5693
5694 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
5695 Clauses);
5696}
5697
Samuel Antao72590762016-01-19 20:04:50 +00005698StmtResult
5699Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
5700 SourceLocation StartLoc,
5701 SourceLocation EndLoc) {
5702 // OpenMP [2.10.3, Restrictions, p. 102]
5703 // At least one map clause must appear on the directive.
5704 if (!HasMapClause(Clauses)) {
5705 Diag(StartLoc, diag::err_omp_no_map_for_directive)
5706 << getOpenMPDirectiveName(OMPD_target_exit_data);
5707 return StmtError();
5708 }
5709
5710 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
5711}
5712
Samuel Antao686c70c2016-05-26 17:30:50 +00005713StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
5714 SourceLocation StartLoc,
5715 SourceLocation EndLoc) {
Samuel Antao686c70c2016-05-26 17:30:50 +00005716 bool seenMotionClause = false;
Samuel Antao661c0902016-05-26 17:39:58 +00005717 for (auto *C : Clauses) {
Samuel Antaoec172c62016-05-26 17:49:04 +00005718 if (C->getClauseKind() == OMPC_to || C->getClauseKind() == OMPC_from)
Samuel Antao661c0902016-05-26 17:39:58 +00005719 seenMotionClause = true;
5720 }
Samuel Antao686c70c2016-05-26 17:30:50 +00005721 if (!seenMotionClause) {
5722 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
5723 return StmtError();
5724 }
5725 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses);
5726}
5727
Alexey Bataev13314bf2014-10-09 04:18:56 +00005728StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
5729 Stmt *AStmt, SourceLocation StartLoc,
5730 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005731 if (!AStmt)
5732 return StmtError();
5733
Alexey Bataev13314bf2014-10-09 04:18:56 +00005734 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5735 // 1.2.2 OpenMP Language Terminology
5736 // Structured block - An executable statement with a single entry at the
5737 // top and a single exit at the bottom.
5738 // The point of exit cannot be a branch out of the structured block.
5739 // longjmp() and throw() must not violate the entry/exit criteria.
5740 CS->getCapturedDecl()->setNothrow();
5741
5742 getCurFunction()->setHasBranchProtectedScope();
5743
5744 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5745}
5746
Alexey Bataev6d4ed052015-07-01 06:57:41 +00005747StmtResult
5748Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
5749 SourceLocation EndLoc,
5750 OpenMPDirectiveKind CancelRegion) {
5751 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5752 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5753 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5754 << getOpenMPDirectiveName(CancelRegion);
5755 return StmtError();
5756 }
5757 if (DSAStack->isParentNowaitRegion()) {
5758 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
5759 return StmtError();
5760 }
5761 if (DSAStack->isParentOrderedRegion()) {
5762 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
5763 return StmtError();
5764 }
5765 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
5766 CancelRegion);
5767}
5768
Alexey Bataev87933c72015-09-18 08:07:34 +00005769StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
5770 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00005771 SourceLocation EndLoc,
5772 OpenMPDirectiveKind CancelRegion) {
5773 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5774 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5775 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5776 << getOpenMPDirectiveName(CancelRegion);
5777 return StmtError();
5778 }
5779 if (DSAStack->isParentNowaitRegion()) {
5780 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
5781 return StmtError();
5782 }
5783 if (DSAStack->isParentOrderedRegion()) {
5784 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
5785 return StmtError();
5786 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005787 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00005788 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5789 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00005790}
5791
Alexey Bataev382967a2015-12-08 12:06:20 +00005792static bool checkGrainsizeNumTasksClauses(Sema &S,
5793 ArrayRef<OMPClause *> Clauses) {
5794 OMPClause *PrevClause = nullptr;
5795 bool ErrorFound = false;
5796 for (auto *C : Clauses) {
5797 if (C->getClauseKind() == OMPC_grainsize ||
5798 C->getClauseKind() == OMPC_num_tasks) {
5799 if (!PrevClause)
5800 PrevClause = C;
5801 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
5802 S.Diag(C->getLocStart(),
5803 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
5804 << getOpenMPClauseName(C->getClauseKind())
5805 << getOpenMPClauseName(PrevClause->getClauseKind());
5806 S.Diag(PrevClause->getLocStart(),
5807 diag::note_omp_previous_grainsize_num_tasks)
5808 << getOpenMPClauseName(PrevClause->getClauseKind());
5809 ErrorFound = true;
5810 }
5811 }
5812 }
5813 return ErrorFound;
5814}
5815
Alexey Bataev49f6e782015-12-01 04:18:41 +00005816StmtResult Sema::ActOnOpenMPTaskLoopDirective(
5817 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5818 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005819 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00005820 if (!AStmt)
5821 return StmtError();
5822
5823 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5824 OMPLoopDirective::HelperExprs B;
5825 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5826 // define the nested loops number.
5827 unsigned NestedLoopCount =
5828 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005829 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00005830 VarsWithImplicitDSA, B);
5831 if (NestedLoopCount == 0)
5832 return StmtError();
5833
5834 assert((CurContext->isDependentContext() || B.builtAll()) &&
5835 "omp for loop exprs were not built");
5836
Alexey Bataev382967a2015-12-08 12:06:20 +00005837 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
5838 // The grainsize clause and num_tasks clause are mutually exclusive and may
5839 // not appear on the same taskloop directive.
5840 if (checkGrainsizeNumTasksClauses(*this, Clauses))
5841 return StmtError();
5842
Alexey Bataev49f6e782015-12-01 04:18:41 +00005843 getCurFunction()->setHasBranchProtectedScope();
5844 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
5845 NestedLoopCount, Clauses, AStmt, B);
5846}
5847
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005848StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
5849 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5850 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005851 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005852 if (!AStmt)
5853 return StmtError();
5854
5855 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5856 OMPLoopDirective::HelperExprs B;
5857 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5858 // define the nested loops number.
5859 unsigned NestedLoopCount =
5860 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
5861 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
5862 VarsWithImplicitDSA, B);
5863 if (NestedLoopCount == 0)
5864 return StmtError();
5865
5866 assert((CurContext->isDependentContext() || B.builtAll()) &&
5867 "omp for loop exprs were not built");
5868
Alexey Bataev5a3af132016-03-29 08:58:54 +00005869 if (!CurContext->isDependentContext()) {
5870 // Finalize the clauses that need pre-built expressions for CodeGen.
5871 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005872 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev5a3af132016-03-29 08:58:54 +00005873 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005874 B.NumIterations, *this, CurScope,
5875 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00005876 return StmtError();
5877 }
5878 }
5879
Alexey Bataev382967a2015-12-08 12:06:20 +00005880 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
5881 // The grainsize clause and num_tasks clause are mutually exclusive and may
5882 // not appear on the same taskloop directive.
5883 if (checkGrainsizeNumTasksClauses(*this, Clauses))
5884 return StmtError();
5885
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005886 getCurFunction()->setHasBranchProtectedScope();
5887 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
5888 NestedLoopCount, Clauses, AStmt, B);
5889}
5890
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005891StmtResult Sema::ActOnOpenMPDistributeDirective(
5892 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5893 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005894 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005895 if (!AStmt)
5896 return StmtError();
5897
5898 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5899 OMPLoopDirective::HelperExprs B;
5900 // In presence of clause 'collapse' with number of loops, it will
5901 // define the nested loops number.
5902 unsigned NestedLoopCount =
5903 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
5904 nullptr /*ordered not a clause on distribute*/, AStmt,
5905 *this, *DSAStack, VarsWithImplicitDSA, B);
5906 if (NestedLoopCount == 0)
5907 return StmtError();
5908
5909 assert((CurContext->isDependentContext() || B.builtAll()) &&
5910 "omp for loop exprs were not built");
5911
5912 getCurFunction()->setHasBranchProtectedScope();
5913 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
5914 NestedLoopCount, Clauses, AStmt, B);
5915}
5916
Carlo Bertolli9925f152016-06-27 14:55:37 +00005917StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
5918 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5919 SourceLocation EndLoc,
5920 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5921 if (!AStmt)
5922 return StmtError();
5923
5924 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5925 // 1.2.2 OpenMP Language Terminology
5926 // Structured block - An executable statement with a single entry at the
5927 // top and a single exit at the bottom.
5928 // The point of exit cannot be a branch out of the structured block.
5929 // longjmp() and throw() must not violate the entry/exit criteria.
5930 CS->getCapturedDecl()->setNothrow();
5931
5932 OMPLoopDirective::HelperExprs B;
5933 // In presence of clause 'collapse' with number of loops, it will
5934 // define the nested loops number.
5935 unsigned NestedLoopCount = CheckOpenMPLoop(
5936 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
5937 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
5938 VarsWithImplicitDSA, B);
5939 if (NestedLoopCount == 0)
5940 return StmtError();
5941
5942 assert((CurContext->isDependentContext() || B.builtAll()) &&
5943 "omp for loop exprs were not built");
5944
5945 getCurFunction()->setHasBranchProtectedScope();
5946 return OMPDistributeParallelForDirective::Create(
5947 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
5948}
5949
Kelvin Li4a39add2016-07-05 05:00:15 +00005950StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
5951 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5952 SourceLocation EndLoc,
5953 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5954 if (!AStmt)
5955 return StmtError();
5956
5957 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5958 // 1.2.2 OpenMP Language Terminology
5959 // Structured block - An executable statement with a single entry at the
5960 // top and a single exit at the bottom.
5961 // The point of exit cannot be a branch out of the structured block.
5962 // longjmp() and throw() must not violate the entry/exit criteria.
5963 CS->getCapturedDecl()->setNothrow();
5964
5965 OMPLoopDirective::HelperExprs B;
5966 // In presence of clause 'collapse' with number of loops, it will
5967 // define the nested loops number.
5968 unsigned NestedLoopCount = CheckOpenMPLoop(
5969 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
5970 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
5971 VarsWithImplicitDSA, B);
5972 if (NestedLoopCount == 0)
5973 return StmtError();
5974
5975 assert((CurContext->isDependentContext() || B.builtAll()) &&
5976 "omp for loop exprs were not built");
5977
Kelvin Lic5609492016-07-15 04:39:07 +00005978 if (checkSimdlenSafelenSpecified(*this, Clauses))
5979 return StmtError();
5980
Kelvin Li4a39add2016-07-05 05:00:15 +00005981 getCurFunction()->setHasBranchProtectedScope();
5982 return OMPDistributeParallelForSimdDirective::Create(
5983 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
5984}
5985
Kelvin Li787f3fc2016-07-06 04:45:38 +00005986StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
5987 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5988 SourceLocation EndLoc,
5989 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5990 if (!AStmt)
5991 return StmtError();
5992
5993 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5994 // 1.2.2 OpenMP Language Terminology
5995 // Structured block - An executable statement with a single entry at the
5996 // top and a single exit at the bottom.
5997 // The point of exit cannot be a branch out of the structured block.
5998 // longjmp() and throw() must not violate the entry/exit criteria.
5999 CS->getCapturedDecl()->setNothrow();
6000
6001 OMPLoopDirective::HelperExprs B;
6002 // In presence of clause 'collapse' with number of loops, it will
6003 // define the nested loops number.
6004 unsigned NestedLoopCount =
6005 CheckOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
6006 nullptr /*ordered not a clause on distribute*/, AStmt,
6007 *this, *DSAStack, VarsWithImplicitDSA, B);
6008 if (NestedLoopCount == 0)
6009 return StmtError();
6010
6011 assert((CurContext->isDependentContext() || B.builtAll()) &&
6012 "omp for loop exprs were not built");
6013
Kelvin Lic5609492016-07-15 04:39:07 +00006014 if (checkSimdlenSafelenSpecified(*this, Clauses))
6015 return StmtError();
6016
Kelvin Li787f3fc2016-07-06 04:45:38 +00006017 getCurFunction()->setHasBranchProtectedScope();
6018 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
6019 NestedLoopCount, Clauses, AStmt, B);
6020}
6021
Kelvin Lia579b912016-07-14 02:54:56 +00006022StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
6023 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6024 SourceLocation EndLoc,
6025 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6026 if (!AStmt)
6027 return StmtError();
6028
6029 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6030 // 1.2.2 OpenMP Language Terminology
6031 // Structured block - An executable statement with a single entry at the
6032 // top and a single exit at the bottom.
6033 // The point of exit cannot be a branch out of the structured block.
6034 // longjmp() and throw() must not violate the entry/exit criteria.
6035 CS->getCapturedDecl()->setNothrow();
6036
6037 OMPLoopDirective::HelperExprs B;
6038 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6039 // define the nested loops number.
6040 unsigned NestedLoopCount = CheckOpenMPLoop(
6041 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
6042 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6043 VarsWithImplicitDSA, B);
6044 if (NestedLoopCount == 0)
6045 return StmtError();
6046
6047 assert((CurContext->isDependentContext() || B.builtAll()) &&
6048 "omp target parallel for simd loop exprs were not built");
6049
6050 if (!CurContext->isDependentContext()) {
6051 // Finalize the clauses that need pre-built expressions for CodeGen.
6052 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00006053 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Lia579b912016-07-14 02:54:56 +00006054 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6055 B.NumIterations, *this, CurScope,
6056 DSAStack))
6057 return StmtError();
6058 }
6059 }
Kelvin Lic5609492016-07-15 04:39:07 +00006060 if (checkSimdlenSafelenSpecified(*this, Clauses))
Kelvin Lia579b912016-07-14 02:54:56 +00006061 return StmtError();
6062
6063 getCurFunction()->setHasBranchProtectedScope();
6064 return OMPTargetParallelForSimdDirective::Create(
6065 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
6066}
6067
Kelvin Li986330c2016-07-20 22:57:10 +00006068StmtResult Sema::ActOnOpenMPTargetSimdDirective(
6069 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6070 SourceLocation EndLoc,
6071 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6072 if (!AStmt)
6073 return StmtError();
6074
6075 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6076 // 1.2.2 OpenMP Language Terminology
6077 // Structured block - An executable statement with a single entry at the
6078 // top and a single exit at the bottom.
6079 // The point of exit cannot be a branch out of the structured block.
6080 // longjmp() and throw() must not violate the entry/exit criteria.
6081 CS->getCapturedDecl()->setNothrow();
6082
6083 OMPLoopDirective::HelperExprs B;
6084 // In presence of clause 'collapse' with number of loops, it will define the
6085 // nested loops number.
David Majnemer9d168222016-08-05 17:44:54 +00006086 unsigned NestedLoopCount =
Kelvin Li986330c2016-07-20 22:57:10 +00006087 CheckOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
6088 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6089 VarsWithImplicitDSA, B);
6090 if (NestedLoopCount == 0)
6091 return StmtError();
6092
6093 assert((CurContext->isDependentContext() || B.builtAll()) &&
6094 "omp target simd loop exprs were not built");
6095
6096 if (!CurContext->isDependentContext()) {
6097 // Finalize the clauses that need pre-built expressions for CodeGen.
6098 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00006099 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Li986330c2016-07-20 22:57:10 +00006100 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6101 B.NumIterations, *this, CurScope,
6102 DSAStack))
6103 return StmtError();
6104 }
6105 }
6106
6107 if (checkSimdlenSafelenSpecified(*this, Clauses))
6108 return StmtError();
6109
6110 getCurFunction()->setHasBranchProtectedScope();
6111 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
6112 NestedLoopCount, Clauses, AStmt, B);
6113}
6114
Kelvin Li02532872016-08-05 14:37:37 +00006115StmtResult Sema::ActOnOpenMPTeamsDistributeDirective(
6116 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6117 SourceLocation EndLoc,
6118 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6119 if (!AStmt)
6120 return StmtError();
6121
6122 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6123 // 1.2.2 OpenMP Language Terminology
6124 // Structured block - An executable statement with a single entry at the
6125 // top and a single exit at the bottom.
6126 // The point of exit cannot be a branch out of the structured block.
6127 // longjmp() and throw() must not violate the entry/exit criteria.
6128 CS->getCapturedDecl()->setNothrow();
6129
6130 OMPLoopDirective::HelperExprs B;
6131 // In presence of clause 'collapse' with number of loops, it will
6132 // define the nested loops number.
6133 unsigned NestedLoopCount =
6134 CheckOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses),
6135 nullptr /*ordered not a clause on distribute*/, AStmt,
6136 *this, *DSAStack, VarsWithImplicitDSA, B);
6137 if (NestedLoopCount == 0)
6138 return StmtError();
6139
6140 assert((CurContext->isDependentContext() || B.builtAll()) &&
6141 "omp teams distribute loop exprs were not built");
6142
6143 getCurFunction()->setHasBranchProtectedScope();
David Majnemer9d168222016-08-05 17:44:54 +00006144 return OMPTeamsDistributeDirective::Create(
6145 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Kelvin Li02532872016-08-05 14:37:37 +00006146}
6147
Kelvin Li4e325f72016-10-25 12:50:55 +00006148StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective(
6149 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6150 SourceLocation EndLoc,
6151 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6152 if (!AStmt)
6153 return StmtError();
6154
6155 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6156 // 1.2.2 OpenMP Language Terminology
6157 // Structured block - An executable statement with a single entry at the
6158 // top and a single exit at the bottom.
6159 // The point of exit cannot be a branch out of the structured block.
6160 // longjmp() and throw() must not violate the entry/exit criteria.
6161 CS->getCapturedDecl()->setNothrow();
6162
6163 OMPLoopDirective::HelperExprs B;
6164 // In presence of clause 'collapse' with number of loops, it will
6165 // define the nested loops number.
Samuel Antao4c8035b2016-12-12 18:00:20 +00006166 unsigned NestedLoopCount = CheckOpenMPLoop(
6167 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses),
6168 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
6169 VarsWithImplicitDSA, B);
Kelvin Li4e325f72016-10-25 12:50:55 +00006170
6171 if (NestedLoopCount == 0)
6172 return StmtError();
6173
6174 assert((CurContext->isDependentContext() || B.builtAll()) &&
6175 "omp teams distribute simd loop exprs were not built");
6176
6177 if (!CurContext->isDependentContext()) {
6178 // Finalize the clauses that need pre-built expressions for CodeGen.
6179 for (auto C : Clauses) {
6180 if (auto *LC = dyn_cast<OMPLinearClause>(C))
6181 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6182 B.NumIterations, *this, CurScope,
6183 DSAStack))
6184 return StmtError();
6185 }
6186 }
6187
6188 if (checkSimdlenSafelenSpecified(*this, Clauses))
6189 return StmtError();
6190
6191 getCurFunction()->setHasBranchProtectedScope();
6192 return OMPTeamsDistributeSimdDirective::Create(
6193 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
6194}
6195
Kelvin Li579e41c2016-11-30 23:51:03 +00006196StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective(
6197 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6198 SourceLocation EndLoc,
6199 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6200 if (!AStmt)
6201 return StmtError();
6202
6203 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6204 // 1.2.2 OpenMP Language Terminology
6205 // Structured block - An executable statement with a single entry at the
6206 // top and a single exit at the bottom.
6207 // The point of exit cannot be a branch out of the structured block.
6208 // longjmp() and throw() must not violate the entry/exit criteria.
6209 CS->getCapturedDecl()->setNothrow();
6210
6211 OMPLoopDirective::HelperExprs B;
6212 // In presence of clause 'collapse' with number of loops, it will
6213 // define the nested loops number.
6214 auto NestedLoopCount = CheckOpenMPLoop(
6215 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
6216 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
6217 VarsWithImplicitDSA, B);
6218
6219 if (NestedLoopCount == 0)
6220 return StmtError();
6221
6222 assert((CurContext->isDependentContext() || B.builtAll()) &&
6223 "omp for loop exprs were not built");
6224
6225 if (!CurContext->isDependentContext()) {
6226 // Finalize the clauses that need pre-built expressions for CodeGen.
6227 for (auto C : Clauses) {
6228 if (auto *LC = dyn_cast<OMPLinearClause>(C))
6229 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6230 B.NumIterations, *this, CurScope,
6231 DSAStack))
6232 return StmtError();
6233 }
6234 }
6235
6236 if (checkSimdlenSafelenSpecified(*this, Clauses))
6237 return StmtError();
6238
6239 getCurFunction()->setHasBranchProtectedScope();
6240 return OMPTeamsDistributeParallelForSimdDirective::Create(
6241 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
6242}
6243
Kelvin Li7ade93f2016-12-09 03:24:30 +00006244StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective(
6245 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6246 SourceLocation EndLoc,
6247 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6248 if (!AStmt)
6249 return StmtError();
6250
6251 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6252 // 1.2.2 OpenMP Language Terminology
6253 // Structured block - An executable statement with a single entry at the
6254 // top and a single exit at the bottom.
6255 // The point of exit cannot be a branch out of the structured block.
6256 // longjmp() and throw() must not violate the entry/exit criteria.
6257 CS->getCapturedDecl()->setNothrow();
6258
6259 OMPLoopDirective::HelperExprs B;
6260 // In presence of clause 'collapse' with number of loops, it will
6261 // define the nested loops number.
6262 unsigned NestedLoopCount = CheckOpenMPLoop(
6263 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
6264 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
6265 VarsWithImplicitDSA, B);
6266
6267 if (NestedLoopCount == 0)
6268 return StmtError();
6269
6270 assert((CurContext->isDependentContext() || B.builtAll()) &&
6271 "omp for loop exprs were not built");
6272
6273 if (!CurContext->isDependentContext()) {
6274 // Finalize the clauses that need pre-built expressions for CodeGen.
6275 for (auto C : Clauses) {
6276 if (auto *LC = dyn_cast<OMPLinearClause>(C))
6277 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6278 B.NumIterations, *this, CurScope,
6279 DSAStack))
6280 return StmtError();
6281 }
6282 }
6283
6284 getCurFunction()->setHasBranchProtectedScope();
6285 return OMPTeamsDistributeParallelForDirective::Create(
6286 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
6287}
6288
Kelvin Libf594a52016-12-17 05:48:59 +00006289StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses,
6290 Stmt *AStmt,
6291 SourceLocation StartLoc,
6292 SourceLocation EndLoc) {
6293 if (!AStmt)
6294 return StmtError();
6295
6296 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6297 // 1.2.2 OpenMP Language Terminology
6298 // Structured block - An executable statement with a single entry at the
6299 // top and a single exit at the bottom.
6300 // The point of exit cannot be a branch out of the structured block.
6301 // longjmp() and throw() must not violate the entry/exit criteria.
6302 CS->getCapturedDecl()->setNothrow();
6303
6304 getCurFunction()->setHasBranchProtectedScope();
6305
6306 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses,
6307 AStmt);
6308}
6309
Kelvin Li83c451e2016-12-25 04:52:54 +00006310StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective(
6311 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6312 SourceLocation EndLoc,
6313 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6314 if (!AStmt)
6315 return StmtError();
6316
6317 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6318 // 1.2.2 OpenMP Language Terminology
6319 // Structured block - An executable statement with a single entry at the
6320 // top and a single exit at the bottom.
6321 // The point of exit cannot be a branch out of the structured block.
6322 // longjmp() and throw() must not violate the entry/exit criteria.
6323 CS->getCapturedDecl()->setNothrow();
6324
6325 OMPLoopDirective::HelperExprs B;
6326 // In presence of clause 'collapse' with number of loops, it will
6327 // define the nested loops number.
6328 auto NestedLoopCount = CheckOpenMPLoop(
6329 OMPD_target_teams_distribute,
6330 getCollapseNumberExpr(Clauses),
6331 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
6332 VarsWithImplicitDSA, B);
6333 if (NestedLoopCount == 0)
6334 return StmtError();
6335
6336 assert((CurContext->isDependentContext() || B.builtAll()) &&
6337 "omp target teams distribute loop exprs were not built");
6338
6339 getCurFunction()->setHasBranchProtectedScope();
6340 return OMPTargetTeamsDistributeDirective::Create(
6341 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
6342}
6343
Kelvin Li80e8f562016-12-29 22:16:30 +00006344StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective(
6345 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6346 SourceLocation EndLoc,
6347 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6348 if (!AStmt)
6349 return StmtError();
6350
6351 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6352 // 1.2.2 OpenMP Language Terminology
6353 // Structured block - An executable statement with a single entry at the
6354 // top and a single exit at the bottom.
6355 // The point of exit cannot be a branch out of the structured block.
6356 // longjmp() and throw() must not violate the entry/exit criteria.
6357 CS->getCapturedDecl()->setNothrow();
6358
6359 OMPLoopDirective::HelperExprs B;
6360 // In presence of clause 'collapse' with number of loops, it will
6361 // define the nested loops number.
6362 auto NestedLoopCount = CheckOpenMPLoop(
6363 OMPD_target_teams_distribute_parallel_for,
6364 getCollapseNumberExpr(Clauses),
6365 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
6366 VarsWithImplicitDSA, B);
6367 if (NestedLoopCount == 0)
6368 return StmtError();
6369
6370 assert((CurContext->isDependentContext() || B.builtAll()) &&
6371 "omp target teams distribute parallel for loop exprs were not built");
6372
6373 if (!CurContext->isDependentContext()) {
6374 // Finalize the clauses that need pre-built expressions for CodeGen.
6375 for (auto C : Clauses) {
6376 if (auto *LC = dyn_cast<OMPLinearClause>(C))
6377 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6378 B.NumIterations, *this, CurScope,
6379 DSAStack))
6380 return StmtError();
6381 }
6382 }
6383
6384 getCurFunction()->setHasBranchProtectedScope();
6385 return OMPTargetTeamsDistributeParallelForDirective::Create(
6386 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
6387}
6388
Kelvin Li1851df52017-01-03 05:23:48 +00006389StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
6390 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6391 SourceLocation EndLoc,
6392 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6393 if (!AStmt)
6394 return StmtError();
6395
6396 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6397 // 1.2.2 OpenMP Language Terminology
6398 // Structured block - An executable statement with a single entry at the
6399 // top and a single exit at the bottom.
6400 // The point of exit cannot be a branch out of the structured block.
6401 // longjmp() and throw() must not violate the entry/exit criteria.
6402 CS->getCapturedDecl()->setNothrow();
6403
6404 OMPLoopDirective::HelperExprs B;
6405 // In presence of clause 'collapse' with number of loops, it will
6406 // define the nested loops number.
6407 auto NestedLoopCount = CheckOpenMPLoop(
6408 OMPD_target_teams_distribute_parallel_for_simd,
6409 getCollapseNumberExpr(Clauses),
6410 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
6411 VarsWithImplicitDSA, B);
6412 if (NestedLoopCount == 0)
6413 return StmtError();
6414
6415 assert((CurContext->isDependentContext() || B.builtAll()) &&
6416 "omp target teams distribute parallel for simd loop exprs were not "
6417 "built");
6418
6419 if (!CurContext->isDependentContext()) {
6420 // Finalize the clauses that need pre-built expressions for CodeGen.
6421 for (auto C : Clauses) {
6422 if (auto *LC = dyn_cast<OMPLinearClause>(C))
6423 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6424 B.NumIterations, *this, CurScope,
6425 DSAStack))
6426 return StmtError();
6427 }
6428 }
6429
6430 getCurFunction()->setHasBranchProtectedScope();
6431 return OMPTargetTeamsDistributeParallelForSimdDirective::Create(
6432 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
6433}
6434
Kelvin Lida681182017-01-10 18:08:18 +00006435StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective(
6436 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6437 SourceLocation EndLoc,
6438 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6439 if (!AStmt)
6440 return StmtError();
6441
6442 auto *CS = cast<CapturedStmt>(AStmt);
6443 // 1.2.2 OpenMP Language Terminology
6444 // Structured block - An executable statement with a single entry at the
6445 // top and a single exit at the bottom.
6446 // The point of exit cannot be a branch out of the structured block.
6447 // longjmp() and throw() must not violate the entry/exit criteria.
6448 CS->getCapturedDecl()->setNothrow();
6449
6450 OMPLoopDirective::HelperExprs B;
6451 // In presence of clause 'collapse' with number of loops, it will
6452 // define the nested loops number.
6453 auto NestedLoopCount = CheckOpenMPLoop(
6454 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses),
6455 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
6456 VarsWithImplicitDSA, B);
6457 if (NestedLoopCount == 0)
6458 return StmtError();
6459
6460 assert((CurContext->isDependentContext() || B.builtAll()) &&
6461 "omp target teams distribute simd loop exprs were not built");
6462
6463 getCurFunction()->setHasBranchProtectedScope();
6464 return OMPTargetTeamsDistributeSimdDirective::Create(
6465 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
6466}
6467
Alexey Bataeved09d242014-05-28 05:53:51 +00006468OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006469 SourceLocation StartLoc,
6470 SourceLocation LParenLoc,
6471 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006472 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006473 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00006474 case OMPC_final:
6475 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
6476 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00006477 case OMPC_num_threads:
6478 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
6479 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006480 case OMPC_safelen:
6481 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
6482 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00006483 case OMPC_simdlen:
6484 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
6485 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00006486 case OMPC_collapse:
6487 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
6488 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006489 case OMPC_ordered:
6490 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
6491 break;
Michael Wonge710d542015-08-07 16:16:36 +00006492 case OMPC_device:
6493 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
6494 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00006495 case OMPC_num_teams:
6496 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
6497 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006498 case OMPC_thread_limit:
6499 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
6500 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00006501 case OMPC_priority:
6502 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
6503 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006504 case OMPC_grainsize:
6505 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
6506 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00006507 case OMPC_num_tasks:
6508 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
6509 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00006510 case OMPC_hint:
6511 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
6512 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006513 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006514 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006515 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006516 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006517 case OMPC_private:
6518 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006519 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006520 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006521 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006522 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006523 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006524 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006525 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00006526 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006527 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006528 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006529 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006530 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006531 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006532 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006533 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006534 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006535 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006536 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00006537 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006538 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006539 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00006540 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006541 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006542 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006543 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00006544 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00006545 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00006546 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00006547 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00006548 case OMPC_is_device_ptr:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006549 llvm_unreachable("Clause is not allowed.");
6550 }
6551 return Res;
6552}
6553
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006554OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
6555 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006556 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006557 SourceLocation NameModifierLoc,
6558 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006559 SourceLocation EndLoc) {
6560 Expr *ValExpr = Condition;
6561 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6562 !Condition->isInstantiationDependent() &&
6563 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00006564 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006565 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006566 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006567
Richard Smith03a4aa32016-06-23 19:02:52 +00006568 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006569 }
6570
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006571 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
6572 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006573}
6574
Alexey Bataev3778b602014-07-17 07:32:53 +00006575OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
6576 SourceLocation StartLoc,
6577 SourceLocation LParenLoc,
6578 SourceLocation EndLoc) {
6579 Expr *ValExpr = Condition;
6580 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6581 !Condition->isInstantiationDependent() &&
6582 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00006583 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataev3778b602014-07-17 07:32:53 +00006584 if (Val.isInvalid())
6585 return nullptr;
6586
Richard Smith03a4aa32016-06-23 19:02:52 +00006587 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataev3778b602014-07-17 07:32:53 +00006588 }
6589
6590 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
6591}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006592ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
6593 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00006594 if (!Op)
6595 return ExprError();
6596
6597 class IntConvertDiagnoser : public ICEConvertDiagnoser {
6598 public:
6599 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00006600 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00006601 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
6602 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006603 return S.Diag(Loc, diag::err_omp_not_integral) << T;
6604 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006605 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
6606 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006607 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
6608 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006609 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
6610 QualType T,
6611 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006612 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
6613 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006614 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
6615 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006616 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006617 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006618 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006619 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
6620 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006621 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
6622 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006623 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
6624 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006625 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006626 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006627 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006628 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
6629 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006630 llvm_unreachable("conversion functions are permitted");
6631 }
6632 } ConvertDiagnoser;
6633 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
6634}
6635
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006636static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00006637 OpenMPClauseKind CKind,
6638 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006639 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
6640 !ValExpr->isInstantiationDependent()) {
6641 SourceLocation Loc = ValExpr->getExprLoc();
6642 ExprResult Value =
6643 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
6644 if (Value.isInvalid())
6645 return false;
6646
6647 ValExpr = Value.get();
6648 // The expression must evaluate to a non-negative integer value.
6649 llvm::APSInt Result;
6650 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00006651 Result.isSigned() &&
6652 !((!StrictlyPositive && Result.isNonNegative()) ||
6653 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006654 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006655 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6656 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006657 return false;
6658 }
6659 }
6660 return true;
6661}
6662
Alexey Bataev568a8332014-03-06 06:15:19 +00006663OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
6664 SourceLocation StartLoc,
6665 SourceLocation LParenLoc,
6666 SourceLocation EndLoc) {
6667 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00006668
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006669 // OpenMP [2.5, Restrictions]
6670 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00006671 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
6672 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006673 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00006674
Alexey Bataeved09d242014-05-28 05:53:51 +00006675 return new (Context)
6676 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00006677}
6678
Alexey Bataev62c87d22014-03-21 04:51:18 +00006679ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006680 OpenMPClauseKind CKind,
6681 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006682 if (!E)
6683 return ExprError();
6684 if (E->isValueDependent() || E->isTypeDependent() ||
6685 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00006686 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006687 llvm::APSInt Result;
6688 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
6689 if (ICE.isInvalid())
6690 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006691 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
6692 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006693 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006694 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6695 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00006696 return ExprError();
6697 }
Alexander Musman09184fe2014-09-30 05:29:28 +00006698 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
6699 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
6700 << E->getSourceRange();
6701 return ExprError();
6702 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006703 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
6704 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00006705 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006706 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00006707 return ICE;
6708}
6709
6710OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
6711 SourceLocation LParenLoc,
6712 SourceLocation EndLoc) {
6713 // OpenMP [2.8.1, simd construct, Description]
6714 // The parameter of the safelen clause must be a constant
6715 // positive integer expression.
6716 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
6717 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006718 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006719 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006720 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00006721}
6722
Alexey Bataev66b15b52015-08-21 11:14:16 +00006723OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
6724 SourceLocation LParenLoc,
6725 SourceLocation EndLoc) {
6726 // OpenMP [2.8.1, simd construct, Description]
6727 // The parameter of the simdlen clause must be a constant
6728 // positive integer expression.
6729 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
6730 if (Simdlen.isInvalid())
6731 return nullptr;
6732 return new (Context)
6733 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
6734}
6735
Alexander Musman64d33f12014-06-04 07:53:32 +00006736OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
6737 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00006738 SourceLocation LParenLoc,
6739 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00006740 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006741 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00006742 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006743 // The parameter of the collapse clause must be a constant
6744 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00006745 ExprResult NumForLoopsResult =
6746 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
6747 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00006748 return nullptr;
6749 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00006750 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00006751}
6752
Alexey Bataev10e775f2015-07-30 11:36:16 +00006753OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
6754 SourceLocation EndLoc,
6755 SourceLocation LParenLoc,
6756 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00006757 // OpenMP [2.7.1, loop construct, Description]
6758 // OpenMP [2.8.1, simd construct, Description]
6759 // OpenMP [2.9.6, distribute construct, Description]
6760 // The parameter of the ordered clause must be a constant
6761 // positive integer expression if any.
6762 if (NumForLoops && LParenLoc.isValid()) {
6763 ExprResult NumForLoopsResult =
6764 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
6765 if (NumForLoopsResult.isInvalid())
6766 return nullptr;
6767 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00006768 } else
6769 NumForLoops = nullptr;
6770 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00006771 return new (Context)
6772 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
6773}
6774
Alexey Bataeved09d242014-05-28 05:53:51 +00006775OMPClause *Sema::ActOnOpenMPSimpleClause(
6776 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
6777 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006778 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006779 switch (Kind) {
6780 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006781 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00006782 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
6783 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006784 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006785 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00006786 Res = ActOnOpenMPProcBindClause(
6787 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
6788 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006789 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006790 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006791 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006792 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006793 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006794 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006795 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006796 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006797 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006798 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006799 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006800 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006801 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006802 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006803 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006804 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006805 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006806 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006807 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006808 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006809 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006810 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006811 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006812 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006813 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006814 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006815 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006816 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006817 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006818 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006819 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006820 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006821 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006822 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006823 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006824 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006825 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006826 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006827 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006828 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006829 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006830 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006831 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00006832 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00006833 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00006834 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00006835 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00006836 case OMPC_is_device_ptr:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006837 llvm_unreachable("Clause is not allowed.");
6838 }
6839 return Res;
6840}
6841
Alexey Bataev6402bca2015-12-28 07:25:51 +00006842static std::string
6843getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
6844 ArrayRef<unsigned> Exclude = llvm::None) {
6845 std::string Values;
6846 unsigned Bound = Last >= 2 ? Last - 2 : 0;
6847 unsigned Skipped = Exclude.size();
6848 auto S = Exclude.begin(), E = Exclude.end();
6849 for (unsigned i = First; i < Last; ++i) {
6850 if (std::find(S, E, i) != E) {
6851 --Skipped;
6852 continue;
6853 }
6854 Values += "'";
6855 Values += getOpenMPSimpleClauseTypeName(K, i);
6856 Values += "'";
6857 if (i == Bound - Skipped)
6858 Values += " or ";
6859 else if (i != Bound + 1 - Skipped)
6860 Values += ", ";
6861 }
6862 return Values;
6863}
6864
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006865OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
6866 SourceLocation KindKwLoc,
6867 SourceLocation StartLoc,
6868 SourceLocation LParenLoc,
6869 SourceLocation EndLoc) {
6870 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00006871 static_assert(OMPC_DEFAULT_unknown > 0,
6872 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006873 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006874 << getListOfPossibleValues(OMPC_default, /*First=*/0,
6875 /*Last=*/OMPC_DEFAULT_unknown)
6876 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006877 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006878 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00006879 switch (Kind) {
6880 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006881 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006882 break;
6883 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006884 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006885 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006886 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006887 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00006888 break;
6889 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006890 return new (Context)
6891 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006892}
6893
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006894OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
6895 SourceLocation KindKwLoc,
6896 SourceLocation StartLoc,
6897 SourceLocation LParenLoc,
6898 SourceLocation EndLoc) {
6899 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006900 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006901 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
6902 /*Last=*/OMPC_PROC_BIND_unknown)
6903 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006904 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006905 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006906 return new (Context)
6907 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006908}
6909
Alexey Bataev56dafe82014-06-20 07:16:17 +00006910OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006911 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006912 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006913 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006914 SourceLocation EndLoc) {
6915 OMPClause *Res = nullptr;
6916 switch (Kind) {
6917 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006918 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
6919 assert(Argument.size() == NumberOfElements &&
6920 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006921 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006922 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
6923 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
6924 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
6925 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
6926 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006927 break;
6928 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006929 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
6930 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
6931 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
6932 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006933 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00006934 case OMPC_dist_schedule:
6935 Res = ActOnOpenMPDistScheduleClause(
6936 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
6937 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
6938 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006939 case OMPC_defaultmap:
6940 enum { Modifier, DefaultmapKind };
6941 Res = ActOnOpenMPDefaultmapClause(
6942 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
6943 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
David Majnemer9d168222016-08-05 17:44:54 +00006944 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind],
6945 EndLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006946 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00006947 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006948 case OMPC_num_threads:
6949 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006950 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006951 case OMPC_collapse:
6952 case OMPC_default:
6953 case OMPC_proc_bind:
6954 case OMPC_private:
6955 case OMPC_firstprivate:
6956 case OMPC_lastprivate:
6957 case OMPC_shared:
6958 case OMPC_reduction:
6959 case OMPC_linear:
6960 case OMPC_aligned:
6961 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006962 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006963 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006964 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006965 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006966 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006967 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006968 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006969 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006970 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006971 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006972 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006973 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006974 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006975 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006976 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006977 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006978 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006979 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006980 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006981 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006982 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006983 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006984 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006985 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006986 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00006987 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00006988 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00006989 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00006990 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00006991 case OMPC_is_device_ptr:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006992 llvm_unreachable("Clause is not allowed.");
6993 }
6994 return Res;
6995}
6996
Alexey Bataev6402bca2015-12-28 07:25:51 +00006997static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
6998 OpenMPScheduleClauseModifier M2,
6999 SourceLocation M1Loc, SourceLocation M2Loc) {
7000 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
7001 SmallVector<unsigned, 2> Excluded;
7002 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
7003 Excluded.push_back(M2);
7004 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
7005 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
7006 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
7007 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
7008 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
7009 << getListOfPossibleValues(OMPC_schedule,
7010 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
7011 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7012 Excluded)
7013 << getOpenMPClauseName(OMPC_schedule);
7014 return true;
7015 }
7016 return false;
7017}
7018
Alexey Bataev56dafe82014-06-20 07:16:17 +00007019OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007020 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007021 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007022 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
7023 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
7024 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
7025 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
7026 return nullptr;
7027 // OpenMP, 2.7.1, Loop Construct, Restrictions
7028 // Either the monotonic modifier or the nonmonotonic modifier can be specified
7029 // but not both.
7030 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
7031 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
7032 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
7033 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
7034 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
7035 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
7036 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
7037 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
7038 return nullptr;
7039 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007040 if (Kind == OMPC_SCHEDULE_unknown) {
7041 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00007042 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
7043 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
7044 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7045 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7046 Exclude);
7047 } else {
7048 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7049 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007050 }
7051 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
7052 << Values << getOpenMPClauseName(OMPC_schedule);
7053 return nullptr;
7054 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00007055 // OpenMP, 2.7.1, Loop Construct, Restrictions
7056 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
7057 // schedule(guided).
7058 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
7059 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
7060 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
7061 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
7062 diag::err_omp_schedule_nonmonotonic_static);
7063 return nullptr;
7064 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007065 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00007066 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00007067 if (ChunkSize) {
7068 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
7069 !ChunkSize->isInstantiationDependent() &&
7070 !ChunkSize->containsUnexpandedParameterPack()) {
7071 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
7072 ExprResult Val =
7073 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
7074 if (Val.isInvalid())
7075 return nullptr;
7076
7077 ValExpr = Val.get();
7078
7079 // OpenMP [2.7.1, Restrictions]
7080 // chunk_size must be a loop invariant integer expression with a positive
7081 // value.
7082 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00007083 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
7084 if (Result.isSigned() && !Result.isStrictlyPositive()) {
7085 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00007086 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00007087 return nullptr;
7088 }
Alexey Bataevb46cdea2016-06-15 11:20:48 +00007089 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
7090 !CurContext->isDependentContext()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00007091 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
7092 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
7093 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007094 }
7095 }
7096 }
7097
Alexey Bataev6402bca2015-12-28 07:25:51 +00007098 return new (Context)
7099 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00007100 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007101}
7102
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007103OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
7104 SourceLocation StartLoc,
7105 SourceLocation EndLoc) {
7106 OMPClause *Res = nullptr;
7107 switch (Kind) {
7108 case OMPC_ordered:
7109 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
7110 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00007111 case OMPC_nowait:
7112 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
7113 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007114 case OMPC_untied:
7115 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
7116 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007117 case OMPC_mergeable:
7118 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
7119 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007120 case OMPC_read:
7121 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
7122 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00007123 case OMPC_write:
7124 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
7125 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00007126 case OMPC_update:
7127 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
7128 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00007129 case OMPC_capture:
7130 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
7131 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007132 case OMPC_seq_cst:
7133 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
7134 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00007135 case OMPC_threads:
7136 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
7137 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007138 case OMPC_simd:
7139 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
7140 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00007141 case OMPC_nogroup:
7142 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
7143 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007144 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007145 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007146 case OMPC_num_threads:
7147 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007148 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007149 case OMPC_collapse:
7150 case OMPC_schedule:
7151 case OMPC_private:
7152 case OMPC_firstprivate:
7153 case OMPC_lastprivate:
7154 case OMPC_shared:
7155 case OMPC_reduction:
7156 case OMPC_linear:
7157 case OMPC_aligned:
7158 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007159 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007160 case OMPC_default:
7161 case OMPC_proc_bind:
7162 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007163 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007164 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007165 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007166 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007167 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007168 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007169 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007170 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00007171 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007172 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007173 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007174 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007175 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007176 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007177 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007178 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007179 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007180 case OMPC_is_device_ptr:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007181 llvm_unreachable("Clause is not allowed.");
7182 }
7183 return Res;
7184}
7185
Alexey Bataev236070f2014-06-20 11:19:47 +00007186OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
7187 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007188 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00007189 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
7190}
7191
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007192OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
7193 SourceLocation EndLoc) {
7194 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
7195}
7196
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007197OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
7198 SourceLocation EndLoc) {
7199 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
7200}
7201
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007202OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
7203 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007204 return new (Context) OMPReadClause(StartLoc, EndLoc);
7205}
7206
Alexey Bataevdea47612014-07-23 07:46:59 +00007207OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
7208 SourceLocation EndLoc) {
7209 return new (Context) OMPWriteClause(StartLoc, EndLoc);
7210}
7211
Alexey Bataev67a4f222014-07-23 10:25:33 +00007212OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
7213 SourceLocation EndLoc) {
7214 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
7215}
7216
Alexey Bataev459dec02014-07-24 06:46:57 +00007217OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
7218 SourceLocation EndLoc) {
7219 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
7220}
7221
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007222OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
7223 SourceLocation EndLoc) {
7224 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
7225}
7226
Alexey Bataev346265e2015-09-25 10:37:12 +00007227OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
7228 SourceLocation EndLoc) {
7229 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
7230}
7231
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007232OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
7233 SourceLocation EndLoc) {
7234 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
7235}
7236
Alexey Bataevb825de12015-12-07 10:51:44 +00007237OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
7238 SourceLocation EndLoc) {
7239 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
7240}
7241
Alexey Bataevc5e02582014-06-16 07:08:35 +00007242OMPClause *Sema::ActOnOpenMPVarListClause(
7243 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
7244 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
7245 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007246 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00007247 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
7248 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
7249 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007250 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007251 switch (Kind) {
7252 case OMPC_private:
7253 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7254 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007255 case OMPC_firstprivate:
7256 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7257 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007258 case OMPC_lastprivate:
7259 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7260 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007261 case OMPC_shared:
7262 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
7263 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007264 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00007265 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
7266 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007267 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00007268 case OMPC_linear:
7269 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00007270 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00007271 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007272 case OMPC_aligned:
7273 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
7274 ColonLoc, EndLoc);
7275 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007276 case OMPC_copyin:
7277 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
7278 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007279 case OMPC_copyprivate:
7280 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7281 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00007282 case OMPC_flush:
7283 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
7284 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007285 case OMPC_depend:
David Majnemer9d168222016-08-05 17:44:54 +00007286 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
Kelvin Li0bff7af2015-11-23 05:32:03 +00007287 StartLoc, LParenLoc, EndLoc);
7288 break;
7289 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00007290 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
7291 DepLinMapLoc, ColonLoc, VarList, StartLoc,
7292 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007293 break;
Samuel Antao661c0902016-05-26 17:39:58 +00007294 case OMPC_to:
7295 Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc);
7296 break;
Samuel Antaoec172c62016-05-26 17:49:04 +00007297 case OMPC_from:
7298 Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc);
7299 break;
Carlo Bertolli2404b172016-07-13 15:37:16 +00007300 case OMPC_use_device_ptr:
7301 Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
7302 break;
Carlo Bertolli70594e92016-07-13 17:16:49 +00007303 case OMPC_is_device_ptr:
7304 Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
7305 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007306 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007307 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00007308 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00007309 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007310 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00007311 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007312 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007313 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007314 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007315 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007316 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007317 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007318 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007319 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007320 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007321 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007322 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007323 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007324 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00007325 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007326 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007327 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007328 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007329 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007330 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007331 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007332 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007333 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007334 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007335 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007336 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007337 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007338 case OMPC_uniform:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007339 llvm_unreachable("Clause is not allowed.");
7340 }
7341 return Res;
7342}
7343
Alexey Bataev90c228f2016-02-08 09:29:13 +00007344ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00007345 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00007346 ExprResult Res = BuildDeclRefExpr(
7347 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
7348 if (!Res.isUsable())
7349 return ExprError();
7350 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
7351 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
7352 if (!Res.isUsable())
7353 return ExprError();
7354 }
7355 if (VK != VK_LValue && Res.get()->isGLValue()) {
7356 Res = DefaultLvalueConversion(Res.get());
7357 if (!Res.isUsable())
7358 return ExprError();
7359 }
7360 return Res;
7361}
7362
Alexey Bataev60da77e2016-02-29 05:54:20 +00007363static std::pair<ValueDecl *, bool>
7364getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
7365 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007366 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7367 RefExpr->containsUnexpandedParameterPack())
7368 return std::make_pair(nullptr, true);
7369
Alexey Bataevd985eda2016-02-10 11:29:16 +00007370 // OpenMP [3.1, C/C++]
7371 // A list item is a variable name.
7372 // OpenMP [2.9.3.3, Restrictions, p.1]
7373 // A variable that is part of another variable (as an array or
7374 // structure element) cannot appear in a private clause.
7375 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00007376 enum {
7377 NoArrayExpr = -1,
7378 ArraySubscript = 0,
7379 OMPArraySection = 1
7380 } IsArrayExpr = NoArrayExpr;
7381 if (AllowArraySection) {
7382 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
7383 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7384 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7385 Base = TempASE->getBase()->IgnoreParenImpCasts();
7386 RefExpr = Base;
7387 IsArrayExpr = ArraySubscript;
7388 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
7389 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7390 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7391 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7392 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7393 Base = TempASE->getBase()->IgnoreParenImpCasts();
7394 RefExpr = Base;
7395 IsArrayExpr = OMPArraySection;
7396 }
7397 }
7398 ELoc = RefExpr->getExprLoc();
7399 ERange = RefExpr->getSourceRange();
7400 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007401 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
7402 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
7403 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
7404 (S.getCurrentThisType().isNull() || !ME ||
7405 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
7406 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00007407 if (IsArrayExpr != NoArrayExpr)
7408 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
7409 << ERange;
7410 else {
7411 S.Diag(ELoc,
7412 AllowArraySection
7413 ? diag::err_omp_expected_var_name_member_expr_or_array_item
7414 : diag::err_omp_expected_var_name_member_expr)
7415 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
7416 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007417 return std::make_pair(nullptr, false);
7418 }
7419 return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
7420}
7421
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007422OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
7423 SourceLocation StartLoc,
7424 SourceLocation LParenLoc,
7425 SourceLocation EndLoc) {
7426 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00007427 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00007428 for (auto &RefExpr : VarList) {
7429 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007430 SourceLocation ELoc;
7431 SourceRange ERange;
7432 Expr *SimpleRefExpr = RefExpr;
7433 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007434 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007435 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007436 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007437 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007438 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007439 ValueDecl *D = Res.first;
7440 if (!D)
7441 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007442
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007443 QualType Type = D->getType();
7444 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007445
7446 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7447 // A variable that appears in a private clause must not have an incomplete
7448 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007449 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007450 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007451 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007452
Alexey Bataev758e55e2013-09-06 18:03:48 +00007453 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7454 // in a Construct]
7455 // Variables with the predetermined data-sharing attributes may not be
7456 // listed in data-sharing attributes clauses, except for the cases
7457 // listed below. For these exceptions only, listing a predetermined
7458 // variable in a data-sharing attribute clause is allowed and overrides
7459 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007460 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007461 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007462 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7463 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007464 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007465 continue;
7466 }
7467
Kelvin Libf594a52016-12-17 05:48:59 +00007468 auto CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007469 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007470 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Kelvin Libf594a52016-12-17 05:48:59 +00007471 isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007472 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7473 << getOpenMPClauseName(OMPC_private) << Type
Kelvin Libf594a52016-12-17 05:48:59 +00007474 << getOpenMPDirectiveName(CurrDir);
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007475 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007476 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007477 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007478 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007479 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007480 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007481 continue;
7482 }
7483
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007484 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
7485 // A list item cannot appear in both a map clause and a data-sharing
7486 // attribute clause on the same construct
Kelvin Libf594a52016-12-17 05:48:59 +00007487 if (CurrDir == OMPD_target || CurrDir == OMPD_target_parallel ||
Kelvin Lida681182017-01-10 18:08:18 +00007488 CurrDir == OMPD_target_teams ||
Kelvin Li80e8f562016-12-29 22:16:30 +00007489 CurrDir == OMPD_target_teams_distribute ||
Kelvin Li1851df52017-01-03 05:23:48 +00007490 CurrDir == OMPD_target_teams_distribute_parallel_for ||
Kelvin Lic4bfc6f2017-01-10 04:26:44 +00007491 CurrDir == OMPD_target_teams_distribute_parallel_for_simd ||
Kelvin Lida681182017-01-10 18:08:18 +00007492 CurrDir == OMPD_target_teams_distribute_simd ||
Kelvin Li41010322017-01-10 05:15:35 +00007493 CurrDir == OMPD_target_parallel_for_simd ||
7494 CurrDir == OMPD_target_parallel_for) {
Samuel Antao6890b092016-07-28 14:25:09 +00007495 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +00007496 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +00007497 VD, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +00007498 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
7499 OpenMPClauseKind WhereFoundClauseKind) -> bool {
7500 ConflictKind = WhereFoundClauseKind;
7501 return true;
7502 })) {
7503 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007504 << getOpenMPClauseName(OMPC_private)
Samuel Antao6890b092016-07-28 14:25:09 +00007505 << getOpenMPClauseName(ConflictKind)
Kelvin Libf594a52016-12-17 05:48:59 +00007506 << getOpenMPDirectiveName(CurrDir);
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007507 ReportOriginalDSA(*this, DSAStack, D, DVar);
7508 continue;
7509 }
7510 }
7511
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007512 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
7513 // A variable of class type (or array thereof) that appears in a private
7514 // clause requires an accessible, unambiguous default constructor for the
7515 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00007516 // Generate helper private variable and initialize it with the default
7517 // value. The address of the original variable is replaced by the address of
7518 // the new private variable in CodeGen. This new variable is not added to
7519 // IdResolver, so the code in the OpenMP region uses original variable for
7520 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007521 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007522 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7523 D->hasAttrs() ? &D->getAttrs() : nullptr);
Richard Smith3beb7c62017-01-12 02:27:38 +00007524 ActOnUninitializedDecl(VDPrivate);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007525 if (VDPrivate->isInvalidDecl())
7526 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007527 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007528 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007529
Alexey Bataev90c228f2016-02-08 09:29:13 +00007530 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00007531 if (!VD && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00007532 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00007533 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00007534 Vars.push_back((VD || CurContext->isDependentContext())
7535 ? RefExpr->IgnoreParens()
7536 : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007537 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007538 }
7539
Alexey Bataeved09d242014-05-28 05:53:51 +00007540 if (Vars.empty())
7541 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007542
Alexey Bataev03b340a2014-10-21 03:16:40 +00007543 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7544 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007545}
7546
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007547namespace {
7548class DiagsUninitializedSeveretyRAII {
7549private:
7550 DiagnosticsEngine &Diags;
7551 SourceLocation SavedLoc;
7552 bool IsIgnored;
7553
7554public:
7555 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
7556 bool IsIgnored)
7557 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
7558 if (!IsIgnored) {
7559 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
7560 /*Map*/ diag::Severity::Ignored, Loc);
7561 }
7562 }
7563 ~DiagsUninitializedSeveretyRAII() {
7564 if (!IsIgnored)
7565 Diags.popMappings(SavedLoc);
7566 }
7567};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007568}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007569
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007570OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
7571 SourceLocation StartLoc,
7572 SourceLocation LParenLoc,
7573 SourceLocation EndLoc) {
7574 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007575 SmallVector<Expr *, 8> PrivateCopies;
7576 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00007577 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007578 bool IsImplicitClause =
7579 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
7580 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
7581
Alexey Bataeved09d242014-05-28 05:53:51 +00007582 for (auto &RefExpr : VarList) {
7583 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007584 SourceLocation ELoc;
7585 SourceRange ERange;
7586 Expr *SimpleRefExpr = RefExpr;
7587 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007588 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007589 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007590 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007591 PrivateCopies.push_back(nullptr);
7592 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007593 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007594 ValueDecl *D = Res.first;
7595 if (!D)
7596 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007597
Alexey Bataev60da77e2016-02-29 05:54:20 +00007598 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00007599 QualType Type = D->getType();
7600 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007601
7602 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7603 // A variable that appears in a private clause must not have an incomplete
7604 // type or a reference type.
7605 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00007606 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007607 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007608 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007609
7610 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
7611 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00007612 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007613 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007614 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007615
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007616 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00007617 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007618 if (!IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007619 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev005248a2016-02-25 05:25:57 +00007620 TopDVar = DVar;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007621 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007622 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
7623 // A list item that specifies a given variable may not appear in more
7624 // than one clause on the same directive, except that a variable may be
7625 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007626 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00007627 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007628 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007629 << getOpenMPClauseName(DVar.CKind)
7630 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007631 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007632 continue;
7633 }
7634
7635 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7636 // in a Construct]
7637 // Variables with the predetermined data-sharing attributes may not be
7638 // listed in data-sharing attributes clauses, except for the cases
7639 // listed below. For these exceptions only, listing a predetermined
7640 // variable in a data-sharing attribute clause is allowed and overrides
7641 // the variable's predetermined data-sharing attributes.
7642 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7643 // in a Construct, C/C++, p.2]
7644 // Variables with const-qualified type having no mutable member may be
7645 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00007646 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007647 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
7648 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007649 << getOpenMPClauseName(DVar.CKind)
7650 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007651 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007652 continue;
7653 }
7654
Alexey Bataevf29276e2014-06-18 04:14:57 +00007655 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007656 // OpenMP [2.9.3.4, Restrictions, p.2]
7657 // A list item that is private within a parallel region must not appear
7658 // in a firstprivate clause on a worksharing construct if any of the
7659 // worksharing regions arising from the worksharing construct ever bind
7660 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00007661 if (isOpenMPWorksharingDirective(CurrDir) &&
Kelvin Li579e41c2016-11-30 23:51:03 +00007662 !isOpenMPParallelDirective(CurrDir) &&
7663 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007664 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007665 if (DVar.CKind != OMPC_shared &&
7666 (isOpenMPParallelDirective(DVar.DKind) ||
7667 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00007668 Diag(ELoc, diag::err_omp_required_access)
7669 << getOpenMPClauseName(OMPC_firstprivate)
7670 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007671 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007672 continue;
7673 }
7674 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007675 // OpenMP [2.9.3.4, Restrictions, p.3]
7676 // A list item that appears in a reduction clause of a parallel construct
7677 // must not appear in a firstprivate clause on a worksharing or task
7678 // construct if any of the worksharing or task regions arising from the
7679 // worksharing or task construct ever bind to any of the parallel regions
7680 // arising from the parallel construct.
7681 // OpenMP [2.9.3.4, Restrictions, p.4]
7682 // A list item that appears in a reduction clause in worksharing
7683 // construct must not appear in a firstprivate clause in a task construct
7684 // encountered during execution of any of the worksharing regions arising
7685 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +00007686 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00007687 DVar = DSAStack->hasInnermostDSA(
7688 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
7689 [](OpenMPDirectiveKind K) -> bool {
7690 return isOpenMPParallelDirective(K) ||
7691 isOpenMPWorksharingDirective(K);
7692 },
7693 false);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007694 if (DVar.CKind == OMPC_reduction &&
7695 (isOpenMPParallelDirective(DVar.DKind) ||
7696 isOpenMPWorksharingDirective(DVar.DKind))) {
7697 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
7698 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007699 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007700 continue;
7701 }
7702 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007703
7704 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7705 // A list item that is private within a teams region must not appear in a
7706 // firstprivate clause on a distribute construct if any of the distribute
7707 // regions arising from the distribute construct ever bind to any of the
7708 // teams regions arising from the teams construct.
7709 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7710 // A list item that appears in a reduction clause of a teams construct
7711 // must not appear in a firstprivate clause on a distribute construct if
7712 // any of the distribute regions arising from the distribute construct
7713 // ever bind to any of the teams regions arising from the teams construct.
7714 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7715 // A list item may appear in a firstprivate or lastprivate clause but not
7716 // both.
7717 if (CurrDir == OMPD_distribute) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00007718 DVar = DSAStack->hasInnermostDSA(
7719 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_private; },
7720 [](OpenMPDirectiveKind K) -> bool {
7721 return isOpenMPTeamsDirective(K);
7722 },
7723 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007724 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
7725 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007726 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007727 continue;
7728 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00007729 DVar = DSAStack->hasInnermostDSA(
7730 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
7731 [](OpenMPDirectiveKind K) -> bool {
7732 return isOpenMPTeamsDirective(K);
7733 },
7734 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007735 if (DVar.CKind == OMPC_reduction &&
7736 isOpenMPTeamsDirective(DVar.DKind)) {
7737 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007738 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007739 continue;
7740 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007741 DVar = DSAStack->getTopDSA(D, false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007742 if (DVar.CKind == OMPC_lastprivate) {
7743 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007744 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007745 continue;
7746 }
7747 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007748 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
7749 // A list item cannot appear in both a map clause and a data-sharing
7750 // attribute clause on the same construct
Kelvin Libf594a52016-12-17 05:48:59 +00007751 if (CurrDir == OMPD_target || CurrDir == OMPD_target_parallel ||
Kelvin Lida681182017-01-10 18:08:18 +00007752 CurrDir == OMPD_target_teams ||
Kelvin Li80e8f562016-12-29 22:16:30 +00007753 CurrDir == OMPD_target_teams_distribute ||
Kelvin Li1851df52017-01-03 05:23:48 +00007754 CurrDir == OMPD_target_teams_distribute_parallel_for ||
Kelvin Lic4bfc6f2017-01-10 04:26:44 +00007755 CurrDir == OMPD_target_teams_distribute_parallel_for_simd ||
Kelvin Lida681182017-01-10 18:08:18 +00007756 CurrDir == OMPD_target_teams_distribute_simd ||
Kelvin Li41010322017-01-10 05:15:35 +00007757 CurrDir == OMPD_target_parallel_for_simd ||
7758 CurrDir == OMPD_target_parallel_for) {
Samuel Antao6890b092016-07-28 14:25:09 +00007759 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +00007760 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +00007761 VD, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +00007762 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
7763 OpenMPClauseKind WhereFoundClauseKind) -> bool {
7764 ConflictKind = WhereFoundClauseKind;
7765 return true;
7766 })) {
7767 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007768 << getOpenMPClauseName(OMPC_firstprivate)
Samuel Antao6890b092016-07-28 14:25:09 +00007769 << getOpenMPClauseName(ConflictKind)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007770 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7771 ReportOriginalDSA(*this, DSAStack, D, DVar);
7772 continue;
7773 }
7774 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007775 }
7776
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007777 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007778 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00007779 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007780 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7781 << getOpenMPClauseName(OMPC_firstprivate) << Type
7782 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7783 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007784 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007785 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00007786 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007787 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00007788 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007789 continue;
7790 }
7791
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007792 Type = Type.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007793 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7794 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007795 // Generate helper private variable and initialize it with the value of the
7796 // original variable. The address of the original variable is replaced by
7797 // the address of the new private variable in the CodeGen. This new variable
7798 // is not added to IdResolver, so the code in the OpenMP region uses
7799 // original variable for proper diagnostics and variable capturing.
7800 Expr *VDInitRefExpr = nullptr;
7801 // For arrays generate initializer for single element and replace it by the
7802 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007803 if (Type->isArrayType()) {
7804 auto VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007805 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007806 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007807 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007808 ElemType = ElemType.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007809 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007810 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00007811 InitializedEntity Entity =
7812 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007813 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
7814
7815 InitializationSequence InitSeq(*this, Entity, Kind, Init);
7816 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
7817 if (Result.isInvalid())
7818 VDPrivate->setInvalidDecl();
7819 else
7820 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007821 // Remove temp variable declaration.
7822 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007823 } else {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007824 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
7825 ".firstprivate.temp");
7826 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
7827 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00007828 AddInitializerToDecl(VDPrivate,
7829 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +00007830 /*DirectInit=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007831 }
7832 if (VDPrivate->isInvalidDecl()) {
7833 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007834 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007835 diag::note_omp_task_predetermined_firstprivate_here);
7836 }
7837 continue;
7838 }
7839 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007840 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00007841 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
7842 RefExpr->getExprLoc());
7843 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00007844 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev005248a2016-02-25 05:25:57 +00007845 if (TopDVar.CKind == OMPC_lastprivate)
7846 Ref = TopDVar.PrivateCopy;
7847 else {
Alexey Bataev61205072016-03-02 04:57:40 +00007848 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataev005248a2016-02-25 05:25:57 +00007849 if (!IsOpenMPCapturedDecl(D))
7850 ExprCaptures.push_back(Ref->getDecl());
7851 }
Alexey Bataev417089f2016-02-17 13:19:37 +00007852 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007853 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00007854 Vars.push_back((VD || CurContext->isDependentContext())
7855 ? RefExpr->IgnoreParens()
7856 : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007857 PrivateCopies.push_back(VDPrivateRefExpr);
7858 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007859 }
7860
Alexey Bataeved09d242014-05-28 05:53:51 +00007861 if (Vars.empty())
7862 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007863
7864 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +00007865 Vars, PrivateCopies, Inits,
7866 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007867}
7868
Alexander Musman1bb328c2014-06-04 13:06:39 +00007869OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
7870 SourceLocation StartLoc,
7871 SourceLocation LParenLoc,
7872 SourceLocation EndLoc) {
7873 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00007874 SmallVector<Expr *, 8> SrcExprs;
7875 SmallVector<Expr *, 8> DstExprs;
7876 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +00007877 SmallVector<Decl *, 4> ExprCaptures;
7878 SmallVector<Expr *, 4> ExprPostUpdates;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007879 for (auto &RefExpr : VarList) {
7880 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007881 SourceLocation ELoc;
7882 SourceRange ERange;
7883 Expr *SimpleRefExpr = RefExpr;
7884 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007885 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00007886 // It will be analyzed later.
7887 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007888 SrcExprs.push_back(nullptr);
7889 DstExprs.push_back(nullptr);
7890 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007891 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00007892 ValueDecl *D = Res.first;
7893 if (!D)
7894 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007895
Alexey Bataev74caaf22016-02-20 04:09:36 +00007896 QualType Type = D->getType();
7897 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007898
7899 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
7900 // A variable that appears in a lastprivate clause must not have an
7901 // incomplete type or a reference type.
7902 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +00007903 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +00007904 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007905 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007906
7907 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7908 // in a Construct]
7909 // Variables with the predetermined data-sharing attributes may not be
7910 // listed in data-sharing attributes clauses, except for the cases
7911 // listed below.
Alexey Bataev74caaf22016-02-20 04:09:36 +00007912 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007913 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
7914 DVar.CKind != OMPC_firstprivate &&
7915 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
7916 Diag(ELoc, diag::err_omp_wrong_dsa)
7917 << getOpenMPClauseName(DVar.CKind)
7918 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007919 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007920 continue;
7921 }
7922
Alexey Bataevf29276e2014-06-18 04:14:57 +00007923 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7924 // OpenMP [2.14.3.5, Restrictions, p.2]
7925 // A list item that is private within a parallel region, or that appears in
7926 // the reduction clause of a parallel construct, must not appear in a
7927 // lastprivate clause on a worksharing construct if any of the corresponding
7928 // worksharing regions ever binds to any of the corresponding parallel
7929 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00007930 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00007931 if (isOpenMPWorksharingDirective(CurrDir) &&
Kelvin Li579e41c2016-11-30 23:51:03 +00007932 !isOpenMPParallelDirective(CurrDir) &&
7933 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +00007934 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007935 if (DVar.CKind != OMPC_shared) {
7936 Diag(ELoc, diag::err_omp_required_access)
7937 << getOpenMPClauseName(OMPC_lastprivate)
7938 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007939 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007940 continue;
7941 }
7942 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00007943
7944 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7945 // A list item may appear in a firstprivate or lastprivate clause but not
7946 // both.
7947 if (CurrDir == OMPD_distribute) {
7948 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
7949 if (DVar.CKind == OMPC_firstprivate) {
7950 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
7951 ReportOriginalDSA(*this, DSAStack, D, DVar);
7952 continue;
7953 }
7954 }
7955
Alexander Musman1bb328c2014-06-04 13:06:39 +00007956 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00007957 // A variable of class type (or array thereof) that appears in a
7958 // lastprivate clause requires an accessible, unambiguous default
7959 // constructor for the class type, unless the list item is also specified
7960 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00007961 // A variable of class type (or array thereof) that appears in a
7962 // lastprivate clause requires an accessible, unambiguous copy assignment
7963 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00007964 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00007965 auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007966 Type.getUnqualifiedType(), ".lastprivate.src",
Alexey Bataev74caaf22016-02-20 04:09:36 +00007967 D->hasAttrs() ? &D->getAttrs() : nullptr);
7968 auto *PseudoSrcExpr =
7969 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00007970 auto *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +00007971 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +00007972 D->hasAttrs() ? &D->getAttrs() : nullptr);
7973 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00007974 // For arrays generate assignment operation for single element and replace
7975 // it by the original array element in CodeGen.
Alexey Bataev74caaf22016-02-20 04:09:36 +00007976 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
Alexey Bataev38e89532015-04-16 04:54:05 +00007977 PseudoDstExpr, PseudoSrcExpr);
7978 if (AssignmentOp.isInvalid())
7979 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +00007980 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00007981 /*DiscardedValue=*/true);
7982 if (AssignmentOp.isInvalid())
7983 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007984
Alexey Bataev74caaf22016-02-20 04:09:36 +00007985 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00007986 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev005248a2016-02-25 05:25:57 +00007987 if (TopDVar.CKind == OMPC_firstprivate)
7988 Ref = TopDVar.PrivateCopy;
7989 else {
Alexey Bataev61205072016-03-02 04:57:40 +00007990 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00007991 if (!IsOpenMPCapturedDecl(D))
7992 ExprCaptures.push_back(Ref->getDecl());
7993 }
7994 if (TopDVar.CKind == OMPC_firstprivate ||
7995 (!IsOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00007996 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +00007997 ExprResult RefRes = DefaultLvalueConversion(Ref);
7998 if (!RefRes.isUsable())
7999 continue;
8000 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008001 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
8002 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008003 if (!PostUpdateRes.isUsable())
8004 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008005 ExprPostUpdates.push_back(
8006 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008007 }
8008 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008009 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008010 Vars.push_back((VD || CurContext->isDependentContext())
8011 ? RefExpr->IgnoreParens()
8012 : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +00008013 SrcExprs.push_back(PseudoSrcExpr);
8014 DstExprs.push_back(PseudoDstExpr);
8015 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00008016 }
8017
8018 if (Vars.empty())
8019 return nullptr;
8020
8021 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +00008022 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008023 buildPreInits(Context, ExprCaptures),
8024 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +00008025}
8026
Alexey Bataev758e55e2013-09-06 18:03:48 +00008027OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
8028 SourceLocation StartLoc,
8029 SourceLocation LParenLoc,
8030 SourceLocation EndLoc) {
8031 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00008032 for (auto &RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008033 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008034 SourceLocation ELoc;
8035 SourceRange ERange;
8036 Expr *SimpleRefExpr = RefExpr;
8037 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008038 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00008039 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008040 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008041 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008042 ValueDecl *D = Res.first;
8043 if (!D)
8044 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008045
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008046 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008047 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8048 // in a Construct]
8049 // Variables with the predetermined data-sharing attributes may not be
8050 // listed in data-sharing attributes clauses, except for the cases
8051 // listed below. For these exceptions only, listing a predetermined
8052 // variable in a data-sharing attribute clause is allowed and overrides
8053 // the variable's predetermined data-sharing attributes.
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008054 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00008055 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
8056 DVar.RefExpr) {
8057 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8058 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008059 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008060 continue;
8061 }
8062
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008063 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008064 if (!VD && IsOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00008065 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008066 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008067 Vars.push_back((VD || !Ref || CurContext->isDependentContext())
8068 ? RefExpr->IgnoreParens()
8069 : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008070 }
8071
Alexey Bataeved09d242014-05-28 05:53:51 +00008072 if (Vars.empty())
8073 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008074
8075 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
8076}
8077
Alexey Bataevc5e02582014-06-16 07:08:35 +00008078namespace {
8079class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
8080 DSAStackTy *Stack;
8081
8082public:
8083 bool VisitDeclRefExpr(DeclRefExpr *E) {
8084 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008085 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008086 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
8087 return false;
8088 if (DVar.CKind != OMPC_unknown)
8089 return true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008090 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
8091 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
8092 false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008093 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00008094 return true;
8095 return false;
8096 }
8097 return false;
8098 }
8099 bool VisitStmt(Stmt *S) {
8100 for (auto Child : S->children()) {
8101 if (Child && Visit(Child))
8102 return true;
8103 }
8104 return false;
8105 }
Alexey Bataev23b69422014-06-18 07:08:49 +00008106 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00008107};
Alexey Bataev23b69422014-06-18 07:08:49 +00008108} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00008109
Alexey Bataev60da77e2016-02-29 05:54:20 +00008110namespace {
8111// Transform MemberExpression for specified FieldDecl of current class to
8112// DeclRefExpr to specified OMPCapturedExprDecl.
8113class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
8114 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
8115 ValueDecl *Field;
8116 DeclRefExpr *CapturedExpr;
8117
8118public:
8119 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
8120 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
8121
8122 ExprResult TransformMemberExpr(MemberExpr *E) {
8123 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
8124 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +00008125 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008126 return CapturedExpr;
8127 }
8128 return BaseTransform::TransformMemberExpr(E);
8129 }
8130 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
8131};
8132} // namespace
8133
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008134template <typename T>
8135static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups,
8136 const llvm::function_ref<T(ValueDecl *)> &Gen) {
8137 for (auto &Set : Lookups) {
8138 for (auto *D : Set) {
8139 if (auto Res = Gen(cast<ValueDecl>(D)))
8140 return Res;
8141 }
8142 }
8143 return T();
8144}
8145
8146static ExprResult
8147buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
8148 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
8149 const DeclarationNameInfo &ReductionId, QualType Ty,
8150 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
8151 if (ReductionIdScopeSpec.isInvalid())
8152 return ExprError();
8153 SmallVector<UnresolvedSet<8>, 4> Lookups;
8154 if (S) {
8155 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
8156 Lookup.suppressDiagnostics();
8157 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
8158 auto *D = Lookup.getRepresentativeDecl();
8159 do {
8160 S = S->getParent();
8161 } while (S && !S->isDeclScope(D));
8162 if (S)
8163 S = S->getParent();
8164 Lookups.push_back(UnresolvedSet<8>());
8165 Lookups.back().append(Lookup.begin(), Lookup.end());
8166 Lookup.clear();
8167 }
8168 } else if (auto *ULE =
8169 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
8170 Lookups.push_back(UnresolvedSet<8>());
8171 Decl *PrevD = nullptr;
David Majnemer9d168222016-08-05 17:44:54 +00008172 for (auto *D : ULE->decls()) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008173 if (D == PrevD)
8174 Lookups.push_back(UnresolvedSet<8>());
8175 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
8176 Lookups.back().addDecl(DRD);
8177 PrevD = D;
8178 }
8179 }
8180 if (Ty->isDependentType() || Ty->isInstantiationDependentType() ||
8181 Ty->containsUnexpandedParameterPack() ||
8182 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool {
8183 return !D->isInvalidDecl() &&
8184 (D->getType()->isDependentType() ||
8185 D->getType()->isInstantiationDependentType() ||
8186 D->getType()->containsUnexpandedParameterPack());
8187 })) {
8188 UnresolvedSet<8> ResSet;
8189 for (auto &Set : Lookups) {
8190 ResSet.append(Set.begin(), Set.end());
8191 // The last item marks the end of all declarations at the specified scope.
8192 ResSet.addDecl(Set[Set.size() - 1]);
8193 }
8194 return UnresolvedLookupExpr::Create(
8195 SemaRef.Context, /*NamingClass=*/nullptr,
8196 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
8197 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
8198 }
8199 if (auto *VD = filterLookupForUDR<ValueDecl *>(
8200 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
8201 if (!D->isInvalidDecl() &&
8202 SemaRef.Context.hasSameType(D->getType(), Ty))
8203 return D;
8204 return nullptr;
8205 }))
8206 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8207 if (auto *VD = filterLookupForUDR<ValueDecl *>(
8208 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
8209 if (!D->isInvalidDecl() &&
8210 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
8211 !Ty.isMoreQualifiedThan(D->getType()))
8212 return D;
8213 return nullptr;
8214 })) {
8215 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
8216 /*DetectVirtual=*/false);
8217 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
8218 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
8219 VD->getType().getUnqualifiedType()))) {
8220 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
8221 /*DiagID=*/0) !=
8222 Sema::AR_inaccessible) {
8223 SemaRef.BuildBasePathArray(Paths, BasePath);
8224 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8225 }
8226 }
8227 }
8228 }
8229 if (ReductionIdScopeSpec.isSet()) {
8230 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
8231 return ExprError();
8232 }
8233 return ExprEmpty();
8234}
8235
Alexey Bataevc5e02582014-06-16 07:08:35 +00008236OMPClause *Sema::ActOnOpenMPReductionClause(
8237 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
8238 SourceLocation ColonLoc, SourceLocation EndLoc,
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008239 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
8240 ArrayRef<Expr *> UnresolvedReductions) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00008241 auto DN = ReductionId.getName();
8242 auto OOK = DN.getCXXOverloadedOperator();
8243 BinaryOperatorKind BOK = BO_Comma;
8244
8245 // OpenMP [2.14.3.6, reduction clause]
8246 // C
8247 // reduction-identifier is either an identifier or one of the following
8248 // operators: +, -, *, &, |, ^, && and ||
8249 // C++
8250 // reduction-identifier is either an id-expression or one of the following
8251 // operators: +, -, *, &, |, ^, && and ||
8252 // FIXME: Only 'min' and 'max' identifiers are supported for now.
8253 switch (OOK) {
8254 case OO_Plus:
8255 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008256 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008257 break;
8258 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008259 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008260 break;
8261 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008262 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008263 break;
8264 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008265 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008266 break;
8267 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008268 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008269 break;
8270 case OO_AmpAmp:
8271 BOK = BO_LAnd;
8272 break;
8273 case OO_PipePipe:
8274 BOK = BO_LOr;
8275 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008276 case OO_New:
8277 case OO_Delete:
8278 case OO_Array_New:
8279 case OO_Array_Delete:
8280 case OO_Slash:
8281 case OO_Percent:
8282 case OO_Tilde:
8283 case OO_Exclaim:
8284 case OO_Equal:
8285 case OO_Less:
8286 case OO_Greater:
8287 case OO_LessEqual:
8288 case OO_GreaterEqual:
8289 case OO_PlusEqual:
8290 case OO_MinusEqual:
8291 case OO_StarEqual:
8292 case OO_SlashEqual:
8293 case OO_PercentEqual:
8294 case OO_CaretEqual:
8295 case OO_AmpEqual:
8296 case OO_PipeEqual:
8297 case OO_LessLess:
8298 case OO_GreaterGreater:
8299 case OO_LessLessEqual:
8300 case OO_GreaterGreaterEqual:
8301 case OO_EqualEqual:
8302 case OO_ExclaimEqual:
8303 case OO_PlusPlus:
8304 case OO_MinusMinus:
8305 case OO_Comma:
8306 case OO_ArrowStar:
8307 case OO_Arrow:
8308 case OO_Call:
8309 case OO_Subscript:
8310 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00008311 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008312 case NUM_OVERLOADED_OPERATORS:
8313 llvm_unreachable("Unexpected reduction identifier");
8314 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00008315 if (auto II = DN.getAsIdentifierInfo()) {
8316 if (II->isStr("max"))
8317 BOK = BO_GT;
8318 else if (II->isStr("min"))
8319 BOK = BO_LT;
8320 }
8321 break;
8322 }
8323 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008324 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +00008325 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008326 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008327
8328 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008329 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008330 SmallVector<Expr *, 8> LHSs;
8331 SmallVector<Expr *, 8> RHSs;
8332 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev61205072016-03-02 04:57:40 +00008333 SmallVector<Decl *, 4> ExprCaptures;
8334 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008335 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
8336 bool FirstIter = true;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008337 for (auto RefExpr : VarList) {
8338 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +00008339 // OpenMP [2.1, C/C++]
8340 // A list item is a variable or array section, subject to the restrictions
8341 // specified in Section 2.4 on page 42 and in each of the sections
8342 // describing clauses and directives for which a list appears.
8343 // OpenMP [2.14.3.3, Restrictions, p.1]
8344 // A variable that is part of another variable (as an array or
8345 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008346 if (!FirstIter && IR != ER)
8347 ++IR;
8348 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008349 SourceLocation ELoc;
8350 SourceRange ERange;
8351 Expr *SimpleRefExpr = RefExpr;
8352 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8353 /*AllowArraySection=*/true);
8354 if (Res.second) {
8355 // It will be analyzed later.
8356 Vars.push_back(RefExpr);
8357 Privates.push_back(nullptr);
8358 LHSs.push_back(nullptr);
8359 RHSs.push_back(nullptr);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008360 // Try to find 'declare reduction' corresponding construct before using
8361 // builtin/overloaded operators.
8362 QualType Type = Context.DependentTy;
8363 CXXCastPath BasePath;
8364 ExprResult DeclareReductionRef = buildDeclareReductionRef(
8365 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
8366 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
8367 if (CurContext->isDependentContext() &&
8368 (DeclareReductionRef.isUnset() ||
8369 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
8370 ReductionOps.push_back(DeclareReductionRef.get());
8371 else
8372 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008373 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00008374 ValueDecl *D = Res.first;
8375 if (!D)
8376 continue;
8377
Alexey Bataeva1764212015-09-30 09:22:36 +00008378 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008379 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
8380 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
8381 if (ASE)
Alexey Bataev31300ed2016-02-04 11:27:03 +00008382 Type = ASE->getType().getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008383 else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008384 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
8385 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
8386 Type = ATy->getElementType();
8387 else
8388 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +00008389 Type = Type.getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008390 } else
8391 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
8392 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +00008393
Alexey Bataevc5e02582014-06-16 07:08:35 +00008394 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8395 // A variable that appears in a private clause must not have an incomplete
8396 // type or a reference type.
8397 if (RequireCompleteType(ELoc, Type,
8398 diag::err_omp_reduction_incomplete_type))
8399 continue;
8400 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00008401 // A list item that appears in a reduction clause must not be
8402 // const-qualified.
8403 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008404 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00008405 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008406 if (!ASE && !OASE) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008407 bool IsDecl = !VD ||
8408 VD->isThisDeclarationADefinition(Context) ==
8409 VarDecl::DeclarationOnly;
8410 Diag(D->getLocation(),
Alexey Bataeva1764212015-09-30 09:22:36 +00008411 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +00008412 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +00008413 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008414 continue;
8415 }
8416 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
8417 // If a list-item is a reference type then it must bind to the same object
8418 // for all threads of the team.
Alexey Bataev60da77e2016-02-29 05:54:20 +00008419 if (!ASE && !OASE && VD) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008420 VarDecl *VDDef = VD->getDefinition();
David Sheinkman92589992016-10-04 14:41:36 +00008421 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008422 DSARefChecker Check(DSAStack);
8423 if (Check.Visit(VDDef->getInit())) {
8424 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
8425 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
8426 continue;
8427 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008428 }
8429 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008430
Alexey Bataevc5e02582014-06-16 07:08:35 +00008431 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
8432 // in a Construct]
8433 // Variables with the predetermined data-sharing attributes may not be
8434 // listed in data-sharing attributes clauses, except for the cases
8435 // listed below. For these exceptions only, listing a predetermined
8436 // variable in a data-sharing attribute clause is allowed and overrides
8437 // the variable's predetermined data-sharing attributes.
8438 // OpenMP [2.14.3.6, Restrictions, p.3]
8439 // Any number of reduction clauses can be specified on the directive,
8440 // but a list item can appear only once in the reduction clauses for that
8441 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00008442 DSAStackTy::DSAVarData DVar;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008443 DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008444 if (DVar.CKind == OMPC_reduction) {
8445 Diag(ELoc, diag::err_omp_once_referenced)
8446 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008447 if (DVar.RefExpr)
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008448 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008449 } else if (DVar.CKind != OMPC_unknown) {
8450 Diag(ELoc, diag::err_omp_wrong_dsa)
8451 << getOpenMPClauseName(DVar.CKind)
8452 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008453 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008454 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008455 }
8456
8457 // OpenMP [2.14.3.6, Restrictions, p.1]
8458 // A list item that appears in a reduction clause of a worksharing
8459 // construct must be shared in the parallel regions to which any of the
8460 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008461 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8462 if (isOpenMPWorksharingDirective(CurrDir) &&
Kelvin Li579e41c2016-11-30 23:51:03 +00008463 !isOpenMPParallelDirective(CurrDir) &&
8464 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008465 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008466 if (DVar.CKind != OMPC_shared) {
8467 Diag(ELoc, diag::err_omp_required_access)
8468 << getOpenMPClauseName(OMPC_reduction)
8469 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008470 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008471 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00008472 }
8473 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008474
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008475 // Try to find 'declare reduction' corresponding construct before using
8476 // builtin/overloaded operators.
8477 CXXCastPath BasePath;
8478 ExprResult DeclareReductionRef = buildDeclareReductionRef(
8479 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
8480 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
8481 if (DeclareReductionRef.isInvalid())
8482 continue;
8483 if (CurContext->isDependentContext() &&
8484 (DeclareReductionRef.isUnset() ||
8485 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
8486 Vars.push_back(RefExpr);
8487 Privates.push_back(nullptr);
8488 LHSs.push_back(nullptr);
8489 RHSs.push_back(nullptr);
8490 ReductionOps.push_back(DeclareReductionRef.get());
8491 continue;
8492 }
8493 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
8494 // Not allowed reduction identifier is found.
8495 Diag(ReductionId.getLocStart(),
8496 diag::err_omp_unknown_reduction_identifier)
8497 << Type << ReductionIdRange;
8498 continue;
8499 }
8500
8501 // OpenMP [2.14.3.6, reduction clause, Restrictions]
8502 // The type of a list item that appears in a reduction clause must be valid
8503 // for the reduction-identifier. For a max or min reduction in C, the type
8504 // of the list item must be an allowed arithmetic data type: char, int,
8505 // float, double, or _Bool, possibly modified with long, short, signed, or
8506 // unsigned. For a max or min reduction in C++, the type of the list item
8507 // must be an allowed arithmetic data type: char, wchar_t, int, float,
8508 // double, or bool, possibly modified with long, short, signed, or unsigned.
8509 if (DeclareReductionRef.isUnset()) {
8510 if ((BOK == BO_GT || BOK == BO_LT) &&
8511 !(Type->isScalarType() ||
8512 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
8513 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
8514 << getLangOpts().CPlusPlus;
8515 if (!ASE && !OASE) {
8516 bool IsDecl = !VD ||
8517 VD->isThisDeclarationADefinition(Context) ==
8518 VarDecl::DeclarationOnly;
8519 Diag(D->getLocation(),
8520 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8521 << D;
8522 }
8523 continue;
8524 }
8525 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
8526 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
8527 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
8528 if (!ASE && !OASE) {
8529 bool IsDecl = !VD ||
8530 VD->isThisDeclarationADefinition(Context) ==
8531 VarDecl::DeclarationOnly;
8532 Diag(D->getLocation(),
8533 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8534 << D;
8535 }
8536 continue;
8537 }
8538 }
8539
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008540 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008541 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
Alexey Bataev60da77e2016-02-29 05:54:20 +00008542 D->hasAttrs() ? &D->getAttrs() : nullptr);
8543 auto *RHSVD = buildVarDecl(*this, ELoc, Type, D->getName(),
8544 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008545 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00008546 if (OASE ||
Alexey Bataev60da77e2016-02-29 05:54:20 +00008547 (!ASE &&
8548 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
David Majnemer9d168222016-08-05 17:44:54 +00008549 // For arrays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008550 // Create pseudo array type for private copy. The size for this array will
8551 // be generated during codegen.
8552 // For array subscripts or single variables Private Ty is the same as Type
8553 // (type of the variable or single array element).
8554 PrivateTy = Context.getVariableArrayType(
8555 Type, new (Context) OpaqueValueExpr(SourceLocation(),
8556 Context.getSizeType(), VK_RValue),
8557 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +00008558 } else if (!ASE && !OASE &&
8559 Context.getAsArrayType(D->getType().getNonReferenceType()))
8560 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008561 // Private copy.
Alexey Bataev60da77e2016-02-29 05:54:20 +00008562 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, D->getName(),
8563 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008564 // Add initializer for private variable.
8565 Expr *Init = nullptr;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008566 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
8567 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
8568 if (DeclareReductionRef.isUsable()) {
8569 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
8570 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
8571 if (DRD->getInitializer()) {
8572 Init = DRDRef;
8573 RHSVD->setInit(DRDRef);
8574 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008575 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008576 } else {
8577 switch (BOK) {
8578 case BO_Add:
8579 case BO_Xor:
8580 case BO_Or:
8581 case BO_LOr:
8582 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
8583 if (Type->isScalarType() || Type->isAnyComplexType())
8584 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
8585 break;
8586 case BO_Mul:
8587 case BO_LAnd:
8588 if (Type->isScalarType() || Type->isAnyComplexType()) {
8589 // '*' and '&&' reduction ops - initializer is '1'.
8590 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00008591 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008592 break;
8593 case BO_And: {
8594 // '&' reduction op - initializer is '~0'.
8595 QualType OrigType = Type;
8596 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
8597 Type = ComplexTy->getElementType();
8598 if (Type->isRealFloatingType()) {
8599 llvm::APFloat InitValue =
8600 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
8601 /*isIEEE=*/true);
8602 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8603 Type, ELoc);
8604 } else if (Type->isScalarType()) {
8605 auto Size = Context.getTypeSize(Type);
8606 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
8607 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
8608 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8609 }
8610 if (Init && OrigType->isAnyComplexType()) {
8611 // Init = 0xFFFF + 0xFFFFi;
8612 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
8613 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
8614 }
8615 Type = OrigType;
8616 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008617 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008618 case BO_LT:
8619 case BO_GT: {
8620 // 'min' reduction op - initializer is 'Largest representable number in
8621 // the reduction list item type'.
8622 // 'max' reduction op - initializer is 'Least representable number in
8623 // the reduction list item type'.
8624 if (Type->isIntegerType() || Type->isPointerType()) {
8625 bool IsSigned = Type->hasSignedIntegerRepresentation();
8626 auto Size = Context.getTypeSize(Type);
8627 QualType IntTy =
8628 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
8629 llvm::APInt InitValue =
8630 (BOK != BO_LT)
8631 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
8632 : llvm::APInt::getMinValue(Size)
8633 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
8634 : llvm::APInt::getMaxValue(Size);
8635 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8636 if (Type->isPointerType()) {
8637 // Cast to pointer type.
8638 auto CastExpr = BuildCStyleCastExpr(
8639 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
8640 SourceLocation(), Init);
8641 if (CastExpr.isInvalid())
8642 continue;
8643 Init = CastExpr.get();
8644 }
8645 } else if (Type->isRealFloatingType()) {
8646 llvm::APFloat InitValue = llvm::APFloat::getLargest(
8647 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
8648 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8649 Type, ELoc);
8650 }
8651 break;
8652 }
8653 case BO_PtrMemD:
8654 case BO_PtrMemI:
8655 case BO_MulAssign:
8656 case BO_Div:
8657 case BO_Rem:
8658 case BO_Sub:
8659 case BO_Shl:
8660 case BO_Shr:
8661 case BO_LE:
8662 case BO_GE:
8663 case BO_EQ:
8664 case BO_NE:
8665 case BO_AndAssign:
8666 case BO_XorAssign:
8667 case BO_OrAssign:
8668 case BO_Assign:
8669 case BO_AddAssign:
8670 case BO_SubAssign:
8671 case BO_DivAssign:
8672 case BO_RemAssign:
8673 case BO_ShlAssign:
8674 case BO_ShrAssign:
8675 case BO_Comma:
8676 llvm_unreachable("Unexpected reduction operation");
8677 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008678 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008679 if (Init && DeclareReductionRef.isUnset()) {
Richard Smith3beb7c62017-01-12 02:27:38 +00008680 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008681 } else if (!Init)
Richard Smith3beb7c62017-01-12 02:27:38 +00008682 ActOnUninitializedDecl(RHSVD);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008683 if (RHSVD->isInvalidDecl())
8684 continue;
8685 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008686 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
8687 << ReductionIdRange;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008688 bool IsDecl =
8689 !VD ||
8690 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8691 Diag(D->getLocation(),
8692 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8693 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008694 continue;
8695 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008696 // Store initializer for single element in private copy. Will be used during
8697 // codegen.
8698 PrivateVD->setInit(RHSVD->getInit());
8699 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008700 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008701 ExprResult ReductionOp;
8702 if (DeclareReductionRef.isUsable()) {
8703 QualType RedTy = DeclareReductionRef.get()->getType();
8704 QualType PtrRedTy = Context.getPointerType(RedTy);
8705 ExprResult LHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
8706 ExprResult RHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
8707 if (!BasePath.empty()) {
8708 LHS = DefaultLvalueConversion(LHS.get());
8709 RHS = DefaultLvalueConversion(RHS.get());
8710 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
8711 CK_UncheckedDerivedToBase, LHS.get(),
8712 &BasePath, LHS.get()->getValueKind());
8713 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
8714 CK_UncheckedDerivedToBase, RHS.get(),
8715 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008716 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008717 FunctionProtoType::ExtProtoInfo EPI;
8718 QualType Params[] = {PtrRedTy, PtrRedTy};
8719 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
8720 auto *OVE = new (Context) OpaqueValueExpr(
8721 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
8722 DefaultLvalueConversion(DeclareReductionRef.get()).get());
8723 Expr *Args[] = {LHS.get(), RHS.get()};
8724 ReductionOp = new (Context)
8725 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
8726 } else {
8727 ReductionOp = BuildBinOp(DSAStack->getCurScope(),
8728 ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE);
8729 if (ReductionOp.isUsable()) {
8730 if (BOK != BO_LT && BOK != BO_GT) {
8731 ReductionOp =
8732 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8733 BO_Assign, LHSDRE, ReductionOp.get());
8734 } else {
8735 auto *ConditionalOp = new (Context) ConditionalOperator(
8736 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
8737 RHSDRE, Type, VK_LValue, OK_Ordinary);
8738 ReductionOp =
8739 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8740 BO_Assign, LHSDRE, ConditionalOp);
8741 }
8742 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
8743 }
8744 if (ReductionOp.isInvalid())
8745 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008746 }
8747
Alexey Bataev60da77e2016-02-29 05:54:20 +00008748 DeclRefExpr *Ref = nullptr;
8749 Expr *VarsExpr = RefExpr->IgnoreParens();
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008750 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008751 if (ASE || OASE) {
8752 TransformExprToCaptures RebuildToCapture(*this, D);
8753 VarsExpr =
8754 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
8755 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +00008756 } else {
8757 VarsExpr = Ref =
8758 buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +00008759 }
8760 if (!IsOpenMPCapturedDecl(D)) {
8761 ExprCaptures.push_back(Ref->getDecl());
8762 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
8763 ExprResult RefRes = DefaultLvalueConversion(Ref);
8764 if (!RefRes.isUsable())
8765 continue;
8766 ExprResult PostUpdateRes =
8767 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
8768 SimpleRefExpr, RefRes.get());
8769 if (!PostUpdateRes.isUsable())
8770 continue;
8771 ExprPostUpdates.push_back(
8772 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +00008773 }
8774 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00008775 }
8776 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
8777 Vars.push_back(VarsExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008778 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008779 LHSs.push_back(LHSDRE);
8780 RHSs.push_back(RHSDRE);
8781 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008782 }
8783
8784 if (Vars.empty())
8785 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +00008786
Alexey Bataevc5e02582014-06-16 07:08:35 +00008787 return OMPReductionClause::Create(
8788 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008789 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008790 LHSs, RHSs, ReductionOps, buildPreInits(Context, ExprCaptures),
8791 buildPostUpdate(*this, ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +00008792}
8793
Alexey Bataevecba70f2016-04-12 11:02:11 +00008794bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
8795 SourceLocation LinLoc) {
8796 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
8797 LinKind == OMPC_LINEAR_unknown) {
8798 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
8799 return true;
8800 }
8801 return false;
8802}
8803
8804bool Sema::CheckOpenMPLinearDecl(ValueDecl *D, SourceLocation ELoc,
8805 OpenMPLinearClauseKind LinKind,
8806 QualType Type) {
8807 auto *VD = dyn_cast_or_null<VarDecl>(D);
8808 // A variable must not have an incomplete type or a reference type.
8809 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
8810 return true;
8811 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
8812 !Type->isReferenceType()) {
8813 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
8814 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
8815 return true;
8816 }
8817 Type = Type.getNonReferenceType();
8818
8819 // A list item must not be const-qualified.
8820 if (Type.isConstant(Context)) {
8821 Diag(ELoc, diag::err_omp_const_variable)
8822 << getOpenMPClauseName(OMPC_linear);
8823 if (D) {
8824 bool IsDecl =
8825 !VD ||
8826 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8827 Diag(D->getLocation(),
8828 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8829 << D;
8830 }
8831 return true;
8832 }
8833
8834 // A list item must be of integral or pointer type.
8835 Type = Type.getUnqualifiedType().getCanonicalType();
8836 const auto *Ty = Type.getTypePtrOrNull();
8837 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
8838 !Ty->isPointerType())) {
8839 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
8840 if (D) {
8841 bool IsDecl =
8842 !VD ||
8843 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8844 Diag(D->getLocation(),
8845 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8846 << D;
8847 }
8848 return true;
8849 }
8850 return false;
8851}
8852
Alexey Bataev182227b2015-08-20 10:54:39 +00008853OMPClause *Sema::ActOnOpenMPLinearClause(
8854 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
8855 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
8856 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008857 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008858 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00008859 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008860 SmallVector<Decl *, 4> ExprCaptures;
8861 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +00008862 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +00008863 LinKind = OMPC_LINEAR_val;
Alexey Bataeved09d242014-05-28 05:53:51 +00008864 for (auto &RefExpr : VarList) {
8865 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008866 SourceLocation ELoc;
8867 SourceRange ERange;
8868 Expr *SimpleRefExpr = RefExpr;
8869 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8870 /*AllowArraySection=*/false);
8871 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008872 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008873 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008874 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00008875 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008876 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008877 ValueDecl *D = Res.first;
8878 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +00008879 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +00008880
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008881 QualType Type = D->getType();
8882 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +00008883
8884 // OpenMP [2.14.3.7, linear clause]
8885 // A list-item cannot appear in more than one linear clause.
8886 // A list-item that appears in a linear clause cannot appear in any
8887 // other data-sharing attribute clause.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008888 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00008889 if (DVar.RefExpr) {
8890 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8891 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008892 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00008893 continue;
8894 }
8895
Alexey Bataevecba70f2016-04-12 11:02:11 +00008896 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +00008897 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00008898 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +00008899
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008900 // Build private copy of original var.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008901 auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(),
8902 D->hasAttrs() ? &D->getAttrs() : nullptr);
8903 auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +00008904 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008905 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008906 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008907 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008908 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev78849fb2016-03-09 09:49:00 +00008909 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
8910 if (!IsOpenMPCapturedDecl(D)) {
8911 ExprCaptures.push_back(Ref->getDecl());
8912 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
8913 ExprResult RefRes = DefaultLvalueConversion(Ref);
8914 if (!RefRes.isUsable())
8915 continue;
8916 ExprResult PostUpdateRes =
8917 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
8918 SimpleRefExpr, RefRes.get());
8919 if (!PostUpdateRes.isUsable())
8920 continue;
8921 ExprPostUpdates.push_back(
8922 IgnoredValueConversions(PostUpdateRes.get()).get());
8923 }
8924 }
8925 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008926 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008927 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008928 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008929 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008930 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +00008931 /*DirectInit=*/false);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008932 auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
8933
8934 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008935 Vars.push_back((VD || CurContext->isDependentContext())
8936 ? RefExpr->IgnoreParens()
8937 : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008938 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00008939 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00008940 }
8941
8942 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008943 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008944
8945 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00008946 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008947 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
8948 !Step->isInstantiationDependent() &&
8949 !Step->containsUnexpandedParameterPack()) {
8950 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00008951 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00008952 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008953 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00008954 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00008955
Alexander Musman3276a272015-03-21 10:12:56 +00008956 // Build var to save the step value.
8957 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008958 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00008959 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008960 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00008961 ExprResult CalcStep =
8962 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008963 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00008964
Alexander Musman8dba6642014-04-22 13:09:42 +00008965 // Warn about zero linear step (it would be probably better specified as
8966 // making corresponding variables 'const').
8967 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00008968 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
8969 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00008970 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
8971 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00008972 if (!IsConstant && CalcStep.isUsable()) {
8973 // Calculate the step beforehand instead of doing this on each iteration.
8974 // (This is not used if the number of iterations may be kfold-ed).
8975 CalcStepExpr = CalcStep.get();
8976 }
Alexander Musman8dba6642014-04-22 13:09:42 +00008977 }
8978
Alexey Bataev182227b2015-08-20 10:54:39 +00008979 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
8980 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008981 StepExpr, CalcStepExpr,
8982 buildPreInits(Context, ExprCaptures),
8983 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +00008984}
8985
Alexey Bataev5dff95c2016-04-22 03:56:56 +00008986static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
8987 Expr *NumIterations, Sema &SemaRef,
8988 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +00008989 // Walk the vars and build update/final expressions for the CodeGen.
8990 SmallVector<Expr *, 8> Updates;
8991 SmallVector<Expr *, 8> Finals;
8992 Expr *Step = Clause.getStep();
8993 Expr *CalcStep = Clause.getCalcStep();
8994 // OpenMP [2.14.3.7, linear clause]
8995 // If linear-step is not specified it is assumed to be 1.
8996 if (Step == nullptr)
8997 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00008998 else if (CalcStep) {
Alexander Musman3276a272015-03-21 10:12:56 +00008999 Step = cast<BinaryOperator>(CalcStep)->getLHS();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009000 }
Alexander Musman3276a272015-03-21 10:12:56 +00009001 bool HasErrors = false;
9002 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009003 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009004 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00009005 for (auto &RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009006 SourceLocation ELoc;
9007 SourceRange ERange;
9008 Expr *SimpleRefExpr = RefExpr;
9009 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange,
9010 /*AllowArraySection=*/false);
9011 ValueDecl *D = Res.first;
9012 if (Res.second || !D) {
9013 Updates.push_back(nullptr);
9014 Finals.push_back(nullptr);
9015 HasErrors = true;
9016 continue;
9017 }
9018 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) {
9019 D = cast<MemberExpr>(CED->getInit()->IgnoreParenImpCasts())
9020 ->getMemberDecl();
9021 }
9022 auto &&Info = Stack->isLoopControlVariable(D);
Alexander Musman3276a272015-03-21 10:12:56 +00009023 Expr *InitExpr = *CurInit;
9024
9025 // Build privatized reference to the current linear var.
David Majnemer9d168222016-08-05 17:44:54 +00009026 auto *DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009027 Expr *CapturedRef;
9028 if (LinKind == OMPC_LINEAR_uval)
9029 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
9030 else
9031 CapturedRef =
9032 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
9033 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
9034 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009035
9036 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009037 ExprResult Update;
9038 if (!Info.first) {
9039 Update =
9040 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
9041 InitExpr, IV, Step, /* Subtract */ false);
9042 } else
9043 Update = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009044 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
9045 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009046
9047 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009048 ExprResult Final;
9049 if (!Info.first) {
9050 Final = BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
9051 InitExpr, NumIterations, Step,
9052 /* Subtract */ false);
9053 } else
9054 Final = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009055 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
9056 /*DiscardedValue=*/true);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009057
Alexander Musman3276a272015-03-21 10:12:56 +00009058 if (!Update.isUsable() || !Final.isUsable()) {
9059 Updates.push_back(nullptr);
9060 Finals.push_back(nullptr);
9061 HasErrors = true;
9062 } else {
9063 Updates.push_back(Update.get());
9064 Finals.push_back(Final.get());
9065 }
Richard Trieucc3949d2016-02-18 22:34:54 +00009066 ++CurInit;
9067 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00009068 }
9069 Clause.setUpdates(Updates);
9070 Clause.setFinals(Finals);
9071 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00009072}
9073
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009074OMPClause *Sema::ActOnOpenMPAlignedClause(
9075 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
9076 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
9077
9078 SmallVector<Expr *, 8> Vars;
9079 for (auto &RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +00009080 assert(RefExpr && "NULL expr in OpenMP linear clause.");
9081 SourceLocation ELoc;
9082 SourceRange ERange;
9083 Expr *SimpleRefExpr = RefExpr;
9084 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9085 /*AllowArraySection=*/false);
9086 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009087 // It will be analyzed later.
9088 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009089 }
Alexey Bataev1efd1662016-03-29 10:59:56 +00009090 ValueDecl *D = Res.first;
9091 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009092 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009093
Alexey Bataev1efd1662016-03-29 10:59:56 +00009094 QualType QType = D->getType();
9095 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009096
9097 // OpenMP [2.8.1, simd construct, Restrictions]
9098 // The type of list items appearing in the aligned clause must be
9099 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009100 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009101 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +00009102 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009103 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009104 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009105 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +00009106 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009107 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +00009108 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009109 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009110 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009111 continue;
9112 }
9113
9114 // OpenMP [2.8.1, simd construct, Restrictions]
9115 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev1efd1662016-03-29 10:59:56 +00009116 if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00009117 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009118 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
9119 << getOpenMPClauseName(OMPC_aligned);
9120 continue;
9121 }
9122
Alexey Bataev1efd1662016-03-29 10:59:56 +00009123 DeclRefExpr *Ref = nullptr;
9124 if (!VD && IsOpenMPCapturedDecl(D))
9125 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
9126 Vars.push_back(DefaultFunctionArrayConversion(
9127 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
9128 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009129 }
9130
9131 // OpenMP [2.8.1, simd construct, Description]
9132 // The parameter of the aligned clause, alignment, must be a constant
9133 // positive integer expression.
9134 // If no optional parameter is specified, implementation-defined default
9135 // alignments for SIMD instructions on the target platforms are assumed.
9136 if (Alignment != nullptr) {
9137 ExprResult AlignResult =
9138 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
9139 if (AlignResult.isInvalid())
9140 return nullptr;
9141 Alignment = AlignResult.get();
9142 }
9143 if (Vars.empty())
9144 return nullptr;
9145
9146 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
9147 EndLoc, Vars, Alignment);
9148}
9149
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009150OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
9151 SourceLocation StartLoc,
9152 SourceLocation LParenLoc,
9153 SourceLocation EndLoc) {
9154 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009155 SmallVector<Expr *, 8> SrcExprs;
9156 SmallVector<Expr *, 8> DstExprs;
9157 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00009158 for (auto &RefExpr : VarList) {
9159 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
9160 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009161 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009162 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009163 SrcExprs.push_back(nullptr);
9164 DstExprs.push_back(nullptr);
9165 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009166 continue;
9167 }
9168
Alexey Bataeved09d242014-05-28 05:53:51 +00009169 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009170 // OpenMP [2.1, C/C++]
9171 // A list item is a variable name.
9172 // OpenMP [2.14.4.1, Restrictions, p.1]
9173 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00009174 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009175 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009176 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
9177 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009178 continue;
9179 }
9180
9181 Decl *D = DE->getDecl();
9182 VarDecl *VD = cast<VarDecl>(D);
9183
9184 QualType Type = VD->getType();
9185 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
9186 // It will be analyzed later.
9187 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009188 SrcExprs.push_back(nullptr);
9189 DstExprs.push_back(nullptr);
9190 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009191 continue;
9192 }
9193
9194 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
9195 // A list item that appears in a copyin clause must be threadprivate.
9196 if (!DSAStack->isThreadPrivate(VD)) {
9197 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00009198 << getOpenMPClauseName(OMPC_copyin)
9199 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009200 continue;
9201 }
9202
9203 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9204 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00009205 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009206 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009207 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009208 auto *SrcVD =
9209 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
9210 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00009211 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009212 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
9213 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009214 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
9215 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009216 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009217 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009218 // For arrays generate assignment operation for single element and replace
9219 // it by the original array element in CodeGen.
9220 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
9221 PseudoDstExpr, PseudoSrcExpr);
9222 if (AssignmentOp.isInvalid())
9223 continue;
9224 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
9225 /*DiscardedValue=*/true);
9226 if (AssignmentOp.isInvalid())
9227 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009228
9229 DSAStack->addDSA(VD, DE, OMPC_copyin);
9230 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009231 SrcExprs.push_back(PseudoSrcExpr);
9232 DstExprs.push_back(PseudoDstExpr);
9233 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009234 }
9235
Alexey Bataeved09d242014-05-28 05:53:51 +00009236 if (Vars.empty())
9237 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009238
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009239 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9240 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009241}
9242
Alexey Bataevbae9a792014-06-27 10:37:06 +00009243OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
9244 SourceLocation StartLoc,
9245 SourceLocation LParenLoc,
9246 SourceLocation EndLoc) {
9247 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00009248 SmallVector<Expr *, 8> SrcExprs;
9249 SmallVector<Expr *, 8> DstExprs;
9250 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009251 for (auto &RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +00009252 assert(RefExpr && "NULL expr in OpenMP linear clause.");
9253 SourceLocation ELoc;
9254 SourceRange ERange;
9255 Expr *SimpleRefExpr = RefExpr;
9256 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9257 /*AllowArraySection=*/false);
9258 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00009259 // It will be analyzed later.
9260 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00009261 SrcExprs.push_back(nullptr);
9262 DstExprs.push_back(nullptr);
9263 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009264 }
Alexey Bataeve122da12016-03-17 10:50:17 +00009265 ValueDecl *D = Res.first;
9266 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +00009267 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009268
Alexey Bataeve122da12016-03-17 10:50:17 +00009269 QualType Type = D->getType();
9270 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009271
9272 // OpenMP [2.14.4.2, Restrictions, p.2]
9273 // A list item that appears in a copyprivate clause may not appear in a
9274 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +00009275 if (!VD || !DSAStack->isThreadPrivate(VD)) {
9276 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00009277 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
9278 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00009279 Diag(ELoc, diag::err_omp_wrong_dsa)
9280 << getOpenMPClauseName(DVar.CKind)
9281 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve122da12016-03-17 10:50:17 +00009282 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009283 continue;
9284 }
9285
9286 // OpenMP [2.11.4.2, Restrictions, p.1]
9287 // All list items that appear in a copyprivate clause must be either
9288 // threadprivate or private in the enclosing context.
9289 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +00009290 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009291 if (DVar.CKind == OMPC_shared) {
9292 Diag(ELoc, diag::err_omp_required_access)
9293 << getOpenMPClauseName(OMPC_copyprivate)
9294 << "threadprivate or private in the enclosing context";
Alexey Bataeve122da12016-03-17 10:50:17 +00009295 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009296 continue;
9297 }
9298 }
9299 }
9300
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009301 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009302 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009303 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009304 << getOpenMPClauseName(OMPC_copyprivate) << Type
9305 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009306 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +00009307 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009308 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +00009309 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009310 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +00009311 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009312 continue;
9313 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009314
Alexey Bataevbae9a792014-06-27 10:37:06 +00009315 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9316 // A variable of class type (or array thereof) that appears in a
9317 // copyin clause requires an accessible, unambiguous copy assignment
9318 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009319 Type = Context.getBaseElementType(Type.getNonReferenceType())
9320 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00009321 auto *SrcVD =
Alexey Bataeve122da12016-03-17 10:50:17 +00009322 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src",
9323 D->hasAttrs() ? &D->getAttrs() : nullptr);
9324 auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
Alexey Bataev420d45b2015-04-14 05:11:24 +00009325 auto *DstVD =
Alexey Bataeve122da12016-03-17 10:50:17 +00009326 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst",
9327 D->hasAttrs() ? &D->getAttrs() : nullptr);
David Majnemer9d168222016-08-05 17:44:54 +00009328 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataeve122da12016-03-17 10:50:17 +00009329 auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
Alexey Bataeva63048e2015-03-23 06:18:07 +00009330 PseudoDstExpr, PseudoSrcExpr);
9331 if (AssignmentOp.isInvalid())
9332 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +00009333 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +00009334 /*DiscardedValue=*/true);
9335 if (AssignmentOp.isInvalid())
9336 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009337
9338 // No need to mark vars as copyprivate, they are already threadprivate or
9339 // implicitly private.
Alexey Bataeve122da12016-03-17 10:50:17 +00009340 assert(VD || IsOpenMPCapturedDecl(D));
9341 Vars.push_back(
9342 VD ? RefExpr->IgnoreParens()
9343 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +00009344 SrcExprs.push_back(PseudoSrcExpr);
9345 DstExprs.push_back(PseudoDstExpr);
9346 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00009347 }
9348
9349 if (Vars.empty())
9350 return nullptr;
9351
Alexey Bataeva63048e2015-03-23 06:18:07 +00009352 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
9353 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009354}
9355
Alexey Bataev6125da92014-07-21 11:26:11 +00009356OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
9357 SourceLocation StartLoc,
9358 SourceLocation LParenLoc,
9359 SourceLocation EndLoc) {
9360 if (VarList.empty())
9361 return nullptr;
9362
9363 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
9364}
Alexey Bataevdea47612014-07-23 07:46:59 +00009365
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009366OMPClause *
9367Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
9368 SourceLocation DepLoc, SourceLocation ColonLoc,
9369 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
9370 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00009371 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009372 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +00009373 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009374 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +00009375 return nullptr;
9376 }
9377 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009378 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
9379 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00009380 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009381 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009382 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
9383 /*Last=*/OMPC_DEPEND_unknown, Except)
9384 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009385 return nullptr;
9386 }
9387 SmallVector<Expr *, 8> Vars;
Alexey Bataev8b427062016-05-25 12:36:08 +00009388 DSAStackTy::OperatorOffsetTy OpsOffs;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009389 llvm::APSInt DepCounter(/*BitWidth=*/32);
9390 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
9391 if (DepKind == OMPC_DEPEND_sink) {
9392 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
9393 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
9394 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009395 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009396 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009397 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
9398 DSAStack->getParentOrderedRegionParam()) {
9399 for (auto &RefExpr : VarList) {
9400 assert(RefExpr && "NULL expr in OpenMP shared clause.");
Alexey Bataev8b427062016-05-25 12:36:08 +00009401 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009402 // It will be analyzed later.
9403 Vars.push_back(RefExpr);
9404 continue;
9405 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009406
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009407 SourceLocation ELoc = RefExpr->getExprLoc();
9408 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
9409 if (DepKind == OMPC_DEPEND_sink) {
9410 if (DepCounter >= TotalDepCount) {
9411 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
9412 continue;
9413 }
9414 ++DepCounter;
9415 // OpenMP [2.13.9, Summary]
9416 // depend(dependence-type : vec), where dependence-type is:
9417 // 'sink' and where vec is the iteration vector, which has the form:
9418 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
9419 // where n is the value specified by the ordered clause in the loop
9420 // directive, xi denotes the loop iteration variable of the i-th nested
9421 // loop associated with the loop directive, and di is a constant
9422 // non-negative integer.
Alexey Bataev8b427062016-05-25 12:36:08 +00009423 if (CurContext->isDependentContext()) {
9424 // It will be analyzed later.
9425 Vars.push_back(RefExpr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009426 continue;
9427 }
Alexey Bataev8b427062016-05-25 12:36:08 +00009428 SimpleExpr = SimpleExpr->IgnoreImplicit();
9429 OverloadedOperatorKind OOK = OO_None;
9430 SourceLocation OOLoc;
9431 Expr *LHS = SimpleExpr;
9432 Expr *RHS = nullptr;
9433 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
9434 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
9435 OOLoc = BO->getOperatorLoc();
9436 LHS = BO->getLHS()->IgnoreParenImpCasts();
9437 RHS = BO->getRHS()->IgnoreParenImpCasts();
9438 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
9439 OOK = OCE->getOperator();
9440 OOLoc = OCE->getOperatorLoc();
9441 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
9442 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
9443 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
9444 OOK = MCE->getMethodDecl()
9445 ->getNameInfo()
9446 .getName()
9447 .getCXXOverloadedOperator();
9448 OOLoc = MCE->getCallee()->getExprLoc();
9449 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
9450 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
9451 }
9452 SourceLocation ELoc;
9453 SourceRange ERange;
9454 auto Res = getPrivateItem(*this, LHS, ELoc, ERange,
9455 /*AllowArraySection=*/false);
9456 if (Res.second) {
9457 // It will be analyzed later.
9458 Vars.push_back(RefExpr);
9459 }
9460 ValueDecl *D = Res.first;
9461 if (!D)
9462 continue;
9463
9464 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
9465 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
9466 continue;
9467 }
9468 if (RHS) {
9469 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
9470 RHS, OMPC_depend, /*StrictlyPositive=*/false);
9471 if (RHSRes.isInvalid())
9472 continue;
9473 }
9474 if (!CurContext->isDependentContext() &&
9475 DSAStack->getParentOrderedRegionParam() &&
9476 DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
9477 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
9478 << DSAStack->getParentLoopControlVariable(
9479 DepCounter.getZExtValue());
9480 continue;
9481 }
9482 OpsOffs.push_back({RHS, OOK});
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009483 } else {
9484 // OpenMP [2.11.1.1, Restrictions, p.3]
9485 // A variable that is part of another variable (such as a field of a
9486 // structure) but is not an array element or an array section cannot
9487 // appear in a depend clause.
9488 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
9489 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
9490 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
9491 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
9492 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev31300ed2016-02-04 11:27:03 +00009493 (ASE &&
9494 !ASE->getBase()
9495 ->getType()
9496 .getNonReferenceType()
9497 ->isPointerType() &&
9498 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009499 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
9500 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009501 continue;
9502 }
9503 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009504 Vars.push_back(RefExpr->IgnoreParenImpCasts());
9505 }
9506
9507 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
9508 TotalDepCount > VarList.size() &&
9509 DSAStack->getParentOrderedRegionParam()) {
9510 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
9511 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
9512 }
9513 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
9514 Vars.empty())
9515 return nullptr;
9516 }
Alexey Bataev8b427062016-05-25 12:36:08 +00009517 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
9518 DepKind, DepLoc, ColonLoc, Vars);
9519 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source)
9520 DSAStack->addDoacrossDependClause(C, OpsOffs);
9521 return C;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009522}
Michael Wonge710d542015-08-07 16:16:36 +00009523
9524OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
9525 SourceLocation LParenLoc,
9526 SourceLocation EndLoc) {
9527 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00009528
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009529 // OpenMP [2.9.1, Restrictions]
9530 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009531 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
9532 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009533 return nullptr;
9534
Michael Wonge710d542015-08-07 16:16:36 +00009535 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9536}
Kelvin Li0bff7af2015-11-23 05:32:03 +00009537
9538static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
9539 DSAStackTy *Stack, CXXRecordDecl *RD) {
9540 if (!RD || RD->isInvalidDecl())
9541 return true;
9542
9543 auto QTy = SemaRef.Context.getRecordType(RD);
9544 if (RD->isDynamicClass()) {
9545 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9546 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
9547 return false;
9548 }
9549 auto *DC = RD;
9550 bool IsCorrect = true;
9551 for (auto *I : DC->decls()) {
9552 if (I) {
9553 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
9554 if (MD->isStatic()) {
9555 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9556 SemaRef.Diag(MD->getLocation(),
9557 diag::note_omp_static_member_in_target);
9558 IsCorrect = false;
9559 }
9560 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
9561 if (VD->isStaticDataMember()) {
9562 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9563 SemaRef.Diag(VD->getLocation(),
9564 diag::note_omp_static_member_in_target);
9565 IsCorrect = false;
9566 }
9567 }
9568 }
9569 }
9570
9571 for (auto &I : RD->bases()) {
9572 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
9573 I.getType()->getAsCXXRecordDecl()))
9574 IsCorrect = false;
9575 }
9576 return IsCorrect;
9577}
9578
9579static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
9580 DSAStackTy *Stack, QualType QTy) {
9581 NamedDecl *ND;
9582 if (QTy->isIncompleteType(&ND)) {
9583 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
9584 return false;
9585 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
David Majnemer9d168222016-08-05 17:44:54 +00009586 if (!RD->isInvalidDecl() && !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
Kelvin Li0bff7af2015-11-23 05:32:03 +00009587 return false;
9588 }
9589 return true;
9590}
9591
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009592/// \brief Return true if it can be proven that the provided array expression
9593/// (array section or array subscript) does NOT specify the whole size of the
9594/// array whose base type is \a BaseQTy.
9595static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
9596 const Expr *E,
9597 QualType BaseQTy) {
9598 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
9599
9600 // If this is an array subscript, it refers to the whole size if the size of
9601 // the dimension is constant and equals 1. Also, an array section assumes the
9602 // format of an array subscript if no colon is used.
9603 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
9604 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
9605 return ATy->getSize().getSExtValue() != 1;
9606 // Size can't be evaluated statically.
9607 return false;
9608 }
9609
9610 assert(OASE && "Expecting array section if not an array subscript.");
9611 auto *LowerBound = OASE->getLowerBound();
9612 auto *Length = OASE->getLength();
9613
9614 // If there is a lower bound that does not evaluates to zero, we are not
David Majnemer9d168222016-08-05 17:44:54 +00009615 // covering the whole dimension.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009616 if (LowerBound) {
9617 llvm::APSInt ConstLowerBound;
9618 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
9619 return false; // Can't get the integer value as a constant.
9620 if (ConstLowerBound.getSExtValue())
9621 return true;
9622 }
9623
9624 // If we don't have a length we covering the whole dimension.
9625 if (!Length)
9626 return false;
9627
9628 // If the base is a pointer, we don't have a way to get the size of the
9629 // pointee.
9630 if (BaseQTy->isPointerType())
9631 return false;
9632
9633 // We can only check if the length is the same as the size of the dimension
9634 // if we have a constant array.
9635 auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
9636 if (!CATy)
9637 return false;
9638
9639 llvm::APSInt ConstLength;
9640 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
9641 return false; // Can't get the integer value as a constant.
9642
9643 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
9644}
9645
9646// Return true if it can be proven that the provided array expression (array
9647// section or array subscript) does NOT specify a single element of the array
9648// whose base type is \a BaseQTy.
9649static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
David Majnemer9d168222016-08-05 17:44:54 +00009650 const Expr *E,
9651 QualType BaseQTy) {
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009652 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
9653
9654 // An array subscript always refer to a single element. Also, an array section
9655 // assumes the format of an array subscript if no colon is used.
9656 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
9657 return false;
9658
9659 assert(OASE && "Expecting array section if not an array subscript.");
9660 auto *Length = OASE->getLength();
9661
9662 // If we don't have a length we have to check if the array has unitary size
9663 // for this dimension. Also, we should always expect a length if the base type
9664 // is pointer.
9665 if (!Length) {
9666 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
9667 return ATy->getSize().getSExtValue() != 1;
9668 // We cannot assume anything.
9669 return false;
9670 }
9671
9672 // Check if the length evaluates to 1.
9673 llvm::APSInt ConstLength;
9674 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
9675 return false; // Can't get the integer value as a constant.
9676
9677 return ConstLength.getSExtValue() != 1;
9678}
9679
Samuel Antao661c0902016-05-26 17:39:58 +00009680// Return the expression of the base of the mappable expression or null if it
9681// cannot be determined and do all the necessary checks to see if the expression
9682// is valid as a standalone mappable expression. In the process, record all the
Samuel Antao90927002016-04-26 14:54:23 +00009683// components of the expression.
9684static Expr *CheckMapClauseExpressionBase(
9685 Sema &SemaRef, Expr *E,
Samuel Antao661c0902016-05-26 17:39:58 +00009686 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
9687 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009688 SourceLocation ELoc = E->getExprLoc();
9689 SourceRange ERange = E->getSourceRange();
9690
9691 // The base of elements of list in a map clause have to be either:
9692 // - a reference to variable or field.
9693 // - a member expression.
9694 // - an array expression.
9695 //
9696 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
9697 // reference to 'r'.
9698 //
9699 // If we have:
9700 //
9701 // struct SS {
9702 // Bla S;
9703 // foo() {
9704 // #pragma omp target map (S.Arr[:12]);
9705 // }
9706 // }
9707 //
9708 // We want to retrieve the member expression 'this->S';
9709
9710 Expr *RelevantExpr = nullptr;
9711
Samuel Antao5de996e2016-01-22 20:21:36 +00009712 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
9713 // If a list item is an array section, it must specify contiguous storage.
9714 //
9715 // For this restriction it is sufficient that we make sure only references
9716 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009717 // exist except in the rightmost expression (unless they cover the whole
9718 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +00009719 //
9720 // r.ArrS[3:5].Arr[6:7]
9721 //
9722 // r.ArrS[3:5].x
9723 //
9724 // but these would be valid:
9725 // r.ArrS[3].Arr[6:7]
9726 //
9727 // r.ArrS[3].x
9728
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009729 bool AllowUnitySizeArraySection = true;
9730 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +00009731
Dmitry Polukhin644a9252016-03-11 07:58:34 +00009732 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009733 E = E->IgnoreParenImpCasts();
9734
9735 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
9736 if (!isa<VarDecl>(CurE->getDecl()))
9737 break;
9738
9739 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009740
9741 // If we got a reference to a declaration, we should not expect any array
9742 // section before that.
9743 AllowUnitySizeArraySection = false;
9744 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +00009745
9746 // Record the component.
9747 CurComponents.push_back(OMPClauseMappableExprCommon::MappableComponent(
9748 CurE, CurE->getDecl()));
Samuel Antao5de996e2016-01-22 20:21:36 +00009749 continue;
9750 }
9751
9752 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
9753 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
9754
9755 if (isa<CXXThisExpr>(BaseE))
9756 // We found a base expression: this->Val.
9757 RelevantExpr = CurE;
9758 else
9759 E = BaseE;
9760
9761 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
9762 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
9763 << CurE->getSourceRange();
9764 break;
9765 }
9766
9767 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
9768
9769 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
9770 // A bit-field cannot appear in a map clause.
9771 //
9772 if (FD->isBitField()) {
Samuel Antao661c0902016-05-26 17:39:58 +00009773 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
9774 << CurE->getSourceRange() << getOpenMPClauseName(CKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009775 break;
9776 }
9777
9778 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9779 // If the type of a list item is a reference to a type T then the type
9780 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009781 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +00009782
9783 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
9784 // A list item cannot be a variable that is a member of a structure with
9785 // a union type.
9786 //
9787 if (auto *RT = CurType->getAs<RecordType>())
9788 if (RT->isUnionType()) {
9789 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
9790 << CurE->getSourceRange();
9791 break;
9792 }
9793
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009794 // If we got a member expression, we should not expect any array section
9795 // before that:
9796 //
9797 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
9798 // If a list item is an element of a structure, only the rightmost symbol
9799 // of the variable reference can be an array section.
9800 //
9801 AllowUnitySizeArraySection = false;
9802 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +00009803
9804 // Record the component.
9805 CurComponents.push_back(
9806 OMPClauseMappableExprCommon::MappableComponent(CurE, FD));
Samuel Antao5de996e2016-01-22 20:21:36 +00009807 continue;
9808 }
9809
9810 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
9811 E = CurE->getBase()->IgnoreParenImpCasts();
9812
9813 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
9814 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9815 << 0 << CurE->getSourceRange();
9816 break;
9817 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009818
9819 // If we got an array subscript that express the whole dimension we
9820 // can have any array expressions before. If it only expressing part of
9821 // the dimension, we can only have unitary-size array expressions.
9822 if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
9823 E->getType()))
9824 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +00009825
9826 // Record the component - we don't have any declaration associated.
9827 CurComponents.push_back(
9828 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +00009829 continue;
9830 }
9831
9832 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009833 E = CurE->getBase()->IgnoreParenImpCasts();
9834
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009835 auto CurType =
9836 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
9837
Samuel Antao5de996e2016-01-22 20:21:36 +00009838 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9839 // If the type of a list item is a reference to a type T then the type
9840 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +00009841 if (CurType->isReferenceType())
9842 CurType = CurType->getPointeeType();
9843
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009844 bool IsPointer = CurType->isAnyPointerType();
9845
9846 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009847 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9848 << 0 << CurE->getSourceRange();
9849 break;
9850 }
9851
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009852 bool NotWhole =
9853 CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
9854 bool NotUnity =
9855 CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
9856
Samuel Antaodab51bb2016-07-18 23:22:11 +00009857 if (AllowWholeSizeArraySection) {
9858 // Any array section is currently allowed. Allowing a whole size array
9859 // section implies allowing a unity array section as well.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009860 //
9861 // If this array section refers to the whole dimension we can still
9862 // accept other array sections before this one, except if the base is a
9863 // pointer. Otherwise, only unitary sections are accepted.
9864 if (NotWhole || IsPointer)
9865 AllowWholeSizeArraySection = false;
Samuel Antaodab51bb2016-07-18 23:22:11 +00009866 } else if (AllowUnitySizeArraySection && NotUnity) {
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009867 // A unity or whole array section is not allowed and that is not
9868 // compatible with the properties of the current array section.
9869 SemaRef.Diag(
9870 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
9871 << CurE->getSourceRange();
9872 break;
9873 }
Samuel Antao90927002016-04-26 14:54:23 +00009874
9875 // Record the component - we don't have any declaration associated.
9876 CurComponents.push_back(
9877 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +00009878 continue;
9879 }
9880
9881 // If nothing else worked, this is not a valid map clause expression.
9882 SemaRef.Diag(ELoc,
9883 diag::err_omp_expected_named_var_member_or_array_expression)
9884 << ERange;
9885 break;
9886 }
9887
9888 return RelevantExpr;
9889}
9890
9891// Return true if expression E associated with value VD has conflicts with other
9892// map information.
Samuel Antao90927002016-04-26 14:54:23 +00009893static bool CheckMapConflicts(
9894 Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD, Expr *E,
9895 bool CurrentRegionOnly,
Samuel Antao661c0902016-05-26 17:39:58 +00009896 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
9897 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009898 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +00009899 SourceLocation ELoc = E->getExprLoc();
9900 SourceRange ERange = E->getSourceRange();
9901
9902 // In order to easily check the conflicts we need to match each component of
9903 // the expression under test with the components of the expressions that are
9904 // already in the stack.
9905
Samuel Antao5de996e2016-01-22 20:21:36 +00009906 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +00009907 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +00009908 "Map clause expression with unexpected base!");
9909
9910 // Variables to help detecting enclosing problems in data environment nests.
9911 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +00009912 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +00009913
Samuel Antao90927002016-04-26 14:54:23 +00009914 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
9915 VD, CurrentRegionOnly,
9916 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
Samuel Antao6890b092016-07-28 14:25:09 +00009917 StackComponents,
9918 OpenMPClauseKind) -> bool {
Samuel Antao90927002016-04-26 14:54:23 +00009919
Samuel Antao5de996e2016-01-22 20:21:36 +00009920 assert(!StackComponents.empty() &&
9921 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +00009922 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +00009923 "Map clause expression with unexpected base!");
9924
Samuel Antao90927002016-04-26 14:54:23 +00009925 // The whole expression in the stack.
9926 auto *RE = StackComponents.front().getAssociatedExpression();
9927
Samuel Antao5de996e2016-01-22 20:21:36 +00009928 // Expressions must start from the same base. Here we detect at which
9929 // point both expressions diverge from each other and see if we can
9930 // detect if the memory referred to both expressions is contiguous and
9931 // do not overlap.
9932 auto CI = CurComponents.rbegin();
9933 auto CE = CurComponents.rend();
9934 auto SI = StackComponents.rbegin();
9935 auto SE = StackComponents.rend();
9936 for (; CI != CE && SI != SE; ++CI, ++SI) {
9937
9938 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
9939 // At most one list item can be an array item derived from a given
9940 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +00009941 if (CurrentRegionOnly &&
9942 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
9943 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
9944 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
9945 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
9946 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +00009947 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +00009948 << CI->getAssociatedExpression()->getSourceRange();
9949 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
9950 diag::note_used_here)
9951 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +00009952 return true;
9953 }
9954
9955 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +00009956 if (CI->getAssociatedExpression()->getStmtClass() !=
9957 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +00009958 break;
9959
9960 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +00009961 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +00009962 break;
9963 }
Kelvin Li9f645ae2016-07-18 22:49:16 +00009964 // Check if the extra components of the expressions in the enclosing
9965 // data environment are redundant for the current base declaration.
9966 // If they are, the maps completely overlap, which is legal.
9967 for (; SI != SE; ++SI) {
9968 QualType Type;
9969 if (auto *ASE =
David Majnemer9d168222016-08-05 17:44:54 +00009970 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
Kelvin Li9f645ae2016-07-18 22:49:16 +00009971 Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
David Majnemer9d168222016-08-05 17:44:54 +00009972 } else if (auto *OASE = dyn_cast<OMPArraySectionExpr>(
9973 SI->getAssociatedExpression())) {
Kelvin Li9f645ae2016-07-18 22:49:16 +00009974 auto *E = OASE->getBase()->IgnoreParenImpCasts();
9975 Type =
9976 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
9977 }
9978 if (Type.isNull() || Type->isAnyPointerType() ||
9979 CheckArrayExpressionDoesNotReferToWholeSize(
9980 SemaRef, SI->getAssociatedExpression(), Type))
9981 break;
9982 }
Samuel Antao5de996e2016-01-22 20:21:36 +00009983
9984 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
9985 // List items of map clauses in the same construct must not share
9986 // original storage.
9987 //
9988 // If the expressions are exactly the same or one is a subset of the
9989 // other, it means they are sharing storage.
9990 if (CI == CE && SI == SE) {
9991 if (CurrentRegionOnly) {
Samuel Antao661c0902016-05-26 17:39:58 +00009992 if (CKind == OMPC_map)
9993 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
9994 else {
Samuel Antaoec172c62016-05-26 17:49:04 +00009995 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +00009996 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
9997 << ERange;
9998 }
Samuel Antao5de996e2016-01-22 20:21:36 +00009999 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10000 << RE->getSourceRange();
10001 return true;
10002 } else {
10003 // If we find the same expression in the enclosing data environment,
10004 // that is legal.
10005 IsEnclosedByDataEnvironmentExpr = true;
10006 return false;
10007 }
10008 }
10009
Samuel Antao90927002016-04-26 14:54:23 +000010010 QualType DerivedType =
10011 std::prev(CI)->getAssociatedDeclaration()->getType();
10012 SourceLocation DerivedLoc =
10013 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000010014
10015 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10016 // If the type of a list item is a reference to a type T then the type
10017 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000010018 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010019
10020 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
10021 // A variable for which the type is pointer and an array section
10022 // derived from that variable must not appear as list items of map
10023 // clauses of the same construct.
10024 //
10025 // Also, cover one of the cases in:
10026 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10027 // If any part of the original storage of a list item has corresponding
10028 // storage in the device data environment, all of the original storage
10029 // must have corresponding storage in the device data environment.
10030 //
10031 if (DerivedType->isAnyPointerType()) {
10032 if (CI == CE || SI == SE) {
10033 SemaRef.Diag(
10034 DerivedLoc,
10035 diag::err_omp_pointer_mapped_along_with_derived_section)
10036 << DerivedLoc;
10037 } else {
10038 assert(CI != CE && SI != SE);
10039 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
10040 << DerivedLoc;
10041 }
10042 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10043 << RE->getSourceRange();
10044 return true;
10045 }
10046
10047 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10048 // List items of map clauses in the same construct must not share
10049 // original storage.
10050 //
10051 // An expression is a subset of the other.
10052 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
Samuel Antao661c0902016-05-26 17:39:58 +000010053 if (CKind == OMPC_map)
10054 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10055 else {
Samuel Antaoec172c62016-05-26 17:49:04 +000010056 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000010057 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
10058 << ERange;
10059 }
Samuel Antao5de996e2016-01-22 20:21:36 +000010060 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10061 << RE->getSourceRange();
10062 return true;
10063 }
10064
10065 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000010066 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000010067 if (!CurrentRegionOnly && SI != SE)
10068 EnclosingExpr = RE;
10069
10070 // The current expression is a subset of the expression in the data
10071 // environment.
10072 IsEnclosedByDataEnvironmentExpr |=
10073 (!CurrentRegionOnly && CI != CE && SI == SE);
10074
10075 return false;
10076 });
10077
10078 if (CurrentRegionOnly)
10079 return FoundError;
10080
10081 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10082 // If any part of the original storage of a list item has corresponding
10083 // storage in the device data environment, all of the original storage must
10084 // have corresponding storage in the device data environment.
10085 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
10086 // If a list item is an element of a structure, and a different element of
10087 // the structure has a corresponding list item in the device data environment
10088 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000010089 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000010090 // data environment prior to the task encountering the construct.
10091 //
10092 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
10093 SemaRef.Diag(ELoc,
10094 diag::err_omp_original_storage_is_shared_and_does_not_contain)
10095 << ERange;
10096 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
10097 << EnclosingExpr->getSourceRange();
10098 return true;
10099 }
10100
10101 return FoundError;
10102}
10103
Samuel Antao661c0902016-05-26 17:39:58 +000010104namespace {
10105// Utility struct that gathers all the related lists associated with a mappable
10106// expression.
10107struct MappableVarListInfo final {
10108 // The list of expressions.
10109 ArrayRef<Expr *> VarList;
10110 // The list of processed expressions.
10111 SmallVector<Expr *, 16> ProcessedVarList;
10112 // The mappble components for each expression.
10113 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
10114 // The base declaration of the variable.
10115 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
10116
10117 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
10118 // We have a list of components and base declarations for each entry in the
10119 // variable list.
10120 VarComponents.reserve(VarList.size());
10121 VarBaseDeclarations.reserve(VarList.size());
10122 }
10123};
10124}
10125
10126// Check the validity of the provided variable list for the provided clause kind
10127// \a CKind. In the check process the valid expressions, and mappable expression
10128// components and variables are extracted and used to fill \a Vars,
10129// \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and
10130// \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'.
10131static void
10132checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS,
10133 OpenMPClauseKind CKind, MappableVarListInfo &MVLI,
10134 SourceLocation StartLoc,
10135 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
10136 bool IsMapTypeImplicit = false) {
Samuel Antaoec172c62016-05-26 17:49:04 +000010137 // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
10138 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
Samuel Antao661c0902016-05-26 17:39:58 +000010139 "Unexpected clause kind with mappable expressions!");
Kelvin Li0bff7af2015-11-23 05:32:03 +000010140
Samuel Antao90927002016-04-26 14:54:23 +000010141 // Keep track of the mappable components and base declarations in this clause.
10142 // Each entry in the list is going to have a list of components associated. We
10143 // record each set of the components so that we can build the clause later on.
10144 // In the end we should have the same amount of declarations and component
10145 // lists.
Samuel Antao90927002016-04-26 14:54:23 +000010146
Samuel Antao661c0902016-05-26 17:39:58 +000010147 for (auto &RE : MVLI.VarList) {
Samuel Antaoec172c62016-05-26 17:49:04 +000010148 assert(RE && "Null expr in omp to/from/map clause");
Kelvin Li0bff7af2015-11-23 05:32:03 +000010149 SourceLocation ELoc = RE->getExprLoc();
10150
Kelvin Li0bff7af2015-11-23 05:32:03 +000010151 auto *VE = RE->IgnoreParenLValueCasts();
10152
10153 if (VE->isValueDependent() || VE->isTypeDependent() ||
10154 VE->isInstantiationDependent() ||
10155 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010156 // We can only analyze this information once the missing information is
10157 // resolved.
Samuel Antao661c0902016-05-26 17:39:58 +000010158 MVLI.ProcessedVarList.push_back(RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010159 continue;
10160 }
10161
10162 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010163
Samuel Antao5de996e2016-01-22 20:21:36 +000010164 if (!RE->IgnoreParenImpCasts()->isLValue()) {
Samuel Antao661c0902016-05-26 17:39:58 +000010165 SemaRef.Diag(ELoc,
10166 diag::err_omp_expected_named_var_member_or_array_expression)
Samuel Antao5de996e2016-01-22 20:21:36 +000010167 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010168 continue;
10169 }
10170
Samuel Antao90927002016-04-26 14:54:23 +000010171 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
10172 ValueDecl *CurDeclaration = nullptr;
10173
10174 // Obtain the array or member expression bases if required. Also, fill the
10175 // components array with all the components identified in the process.
Samuel Antao661c0902016-05-26 17:39:58 +000010176 auto *BE =
10177 CheckMapClauseExpressionBase(SemaRef, SimpleExpr, CurComponents, CKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010178 if (!BE)
10179 continue;
10180
Samuel Antao90927002016-04-26 14:54:23 +000010181 assert(!CurComponents.empty() &&
10182 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000010183
Samuel Antao90927002016-04-26 14:54:23 +000010184 // For the following checks, we rely on the base declaration which is
10185 // expected to be associated with the last component. The declaration is
10186 // expected to be a variable or a field (if 'this' is being mapped).
10187 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
10188 assert(CurDeclaration && "Null decl on map clause.");
10189 assert(
10190 CurDeclaration->isCanonicalDecl() &&
10191 "Expecting components to have associated only canonical declarations.");
10192
10193 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
10194 auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000010195
10196 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000010197 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000010198
10199 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
Samuel Antao661c0902016-05-26 17:39:58 +000010200 // threadprivate variables cannot appear in a map clause.
10201 // OpenMP 4.5 [2.10.5, target update Construct]
10202 // threadprivate variables cannot appear in a from clause.
10203 if (VD && DSAS->isThreadPrivate(VD)) {
10204 auto DVar = DSAS->getTopDSA(VD, false);
10205 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
10206 << getOpenMPClauseName(CKind);
10207 ReportOriginalDSA(SemaRef, DSAS, VD, DVar);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010208 continue;
10209 }
10210
Samuel Antao5de996e2016-01-22 20:21:36 +000010211 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
10212 // A list item cannot appear in both a map clause and a data-sharing
10213 // attribute clause on the same construct.
Kelvin Li0bff7af2015-11-23 05:32:03 +000010214
Samuel Antao5de996e2016-01-22 20:21:36 +000010215 // Check conflicts with other map clause expressions. We check the conflicts
10216 // with the current construct separately from the enclosing data
Samuel Antao661c0902016-05-26 17:39:58 +000010217 // environment, because the restrictions are different. We only have to
10218 // check conflicts across regions for the map clauses.
10219 if (CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
10220 /*CurrentRegionOnly=*/true, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000010221 break;
Samuel Antao661c0902016-05-26 17:39:58 +000010222 if (CKind == OMPC_map &&
10223 CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
10224 /*CurrentRegionOnly=*/false, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000010225 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000010226
Samuel Antao661c0902016-05-26 17:39:58 +000010227 // OpenMP 4.5 [2.10.5, target update Construct]
Samuel Antao5de996e2016-01-22 20:21:36 +000010228 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10229 // If the type of a list item is a reference to a type T then the type will
10230 // be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000010231 QualType Type = CurDeclaration->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010232
Samuel Antao661c0902016-05-26 17:39:58 +000010233 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
10234 // A list item in a to or from clause must have a mappable type.
Samuel Antao5de996e2016-01-22 20:21:36 +000010235 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000010236 // A list item must have a mappable type.
Samuel Antao661c0902016-05-26 17:39:58 +000010237 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
10238 DSAS, Type))
Kelvin Li0bff7af2015-11-23 05:32:03 +000010239 continue;
10240
Samuel Antao661c0902016-05-26 17:39:58 +000010241 if (CKind == OMPC_map) {
10242 // target enter data
10243 // OpenMP [2.10.2, Restrictions, p. 99]
10244 // A map-type must be specified in all map clauses and must be either
10245 // to or alloc.
10246 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
10247 if (DKind == OMPD_target_enter_data &&
10248 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
10249 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
10250 << (IsMapTypeImplicit ? 1 : 0)
10251 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
10252 << getOpenMPDirectiveName(DKind);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010253 continue;
10254 }
Samuel Antao661c0902016-05-26 17:39:58 +000010255
10256 // target exit_data
10257 // OpenMP [2.10.3, Restrictions, p. 102]
10258 // A map-type must be specified in all map clauses and must be either
10259 // from, release, or delete.
10260 if (DKind == OMPD_target_exit_data &&
10261 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
10262 MapType == OMPC_MAP_delete)) {
10263 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
10264 << (IsMapTypeImplicit ? 1 : 0)
10265 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
10266 << getOpenMPDirectiveName(DKind);
10267 continue;
10268 }
10269
10270 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
10271 // A list item cannot appear in both a map clause and a data-sharing
10272 // attribute clause on the same construct
Kelvin Li83c451e2016-12-25 04:52:54 +000010273 if ((DKind == OMPD_target || DKind == OMPD_target_teams ||
Kelvin Li80e8f562016-12-29 22:16:30 +000010274 DKind == OMPD_target_teams_distribute ||
Kelvin Li1851df52017-01-03 05:23:48 +000010275 DKind == OMPD_target_teams_distribute_parallel_for ||
Kelvin Lida681182017-01-10 18:08:18 +000010276 DKind == OMPD_target_teams_distribute_parallel_for_simd ||
10277 DKind == OMPD_target_teams_distribute_simd) && VD) {
Samuel Antao661c0902016-05-26 17:39:58 +000010278 auto DVar = DSAS->getTopDSA(VD, false);
10279 if (isOpenMPPrivate(DVar.CKind)) {
Samuel Antao6890b092016-07-28 14:25:09 +000010280 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Samuel Antao661c0902016-05-26 17:39:58 +000010281 << getOpenMPClauseName(DVar.CKind)
Samuel Antao6890b092016-07-28 14:25:09 +000010282 << getOpenMPClauseName(OMPC_map)
Samuel Antao661c0902016-05-26 17:39:58 +000010283 << getOpenMPDirectiveName(DSAS->getCurrentDirective());
10284 ReportOriginalDSA(SemaRef, DSAS, CurDeclaration, DVar);
10285 continue;
10286 }
10287 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010288 }
10289
Samuel Antao90927002016-04-26 14:54:23 +000010290 // Save the current expression.
Samuel Antao661c0902016-05-26 17:39:58 +000010291 MVLI.ProcessedVarList.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000010292
10293 // Store the components in the stack so that they can be used to check
10294 // against other clauses later on.
Samuel Antao6890b092016-07-28 14:25:09 +000010295 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
10296 /*WhereFoundClauseKind=*/OMPC_map);
Samuel Antao90927002016-04-26 14:54:23 +000010297
10298 // Save the components and declaration to create the clause. For purposes of
10299 // the clause creation, any component list that has has base 'this' uses
Samuel Antao686c70c2016-05-26 17:30:50 +000010300 // null as base declaration.
Samuel Antao661c0902016-05-26 17:39:58 +000010301 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
10302 MVLI.VarComponents.back().append(CurComponents.begin(),
10303 CurComponents.end());
10304 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
10305 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010306 }
Samuel Antao661c0902016-05-26 17:39:58 +000010307}
10308
10309OMPClause *
10310Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
10311 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
10312 SourceLocation MapLoc, SourceLocation ColonLoc,
10313 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
10314 SourceLocation LParenLoc, SourceLocation EndLoc) {
10315 MappableVarListInfo MVLI(VarList);
10316 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc,
10317 MapType, IsMapTypeImplicit);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010318
Samuel Antao5de996e2016-01-22 20:21:36 +000010319 // We need to produce a map clause even if we don't have variables so that
10320 // other diagnostics related with non-existing map clauses are accurate.
Samuel Antao661c0902016-05-26 17:39:58 +000010321 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10322 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
10323 MVLI.VarComponents, MapTypeModifier, MapType,
10324 IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010325}
Kelvin Li099bb8c2015-11-24 20:50:12 +000010326
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010327QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
10328 TypeResult ParsedType) {
10329 assert(ParsedType.isUsable());
10330
10331 QualType ReductionType = GetTypeFromParser(ParsedType.get());
10332 if (ReductionType.isNull())
10333 return QualType();
10334
10335 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
10336 // A type name in a declare reduction directive cannot be a function type, an
10337 // array type, a reference type, or a type qualified with const, volatile or
10338 // restrict.
10339 if (ReductionType.hasQualifiers()) {
10340 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
10341 return QualType();
10342 }
10343
10344 if (ReductionType->isFunctionType()) {
10345 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
10346 return QualType();
10347 }
10348 if (ReductionType->isReferenceType()) {
10349 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
10350 return QualType();
10351 }
10352 if (ReductionType->isArrayType()) {
10353 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
10354 return QualType();
10355 }
10356 return ReductionType;
10357}
10358
10359Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
10360 Scope *S, DeclContext *DC, DeclarationName Name,
10361 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
10362 AccessSpecifier AS, Decl *PrevDeclInScope) {
10363 SmallVector<Decl *, 8> Decls;
10364 Decls.reserve(ReductionTypes.size());
10365
10366 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
10367 ForRedeclaration);
10368 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
10369 // A reduction-identifier may not be re-declared in the current scope for the
10370 // same type or for a type that is compatible according to the base language
10371 // rules.
10372 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
10373 OMPDeclareReductionDecl *PrevDRD = nullptr;
10374 bool InCompoundScope = true;
10375 if (S != nullptr) {
10376 // Find previous declaration with the same name not referenced in other
10377 // declarations.
10378 FunctionScopeInfo *ParentFn = getEnclosingFunction();
10379 InCompoundScope =
10380 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
10381 LookupName(Lookup, S);
10382 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
10383 /*AllowInlineNamespace=*/false);
10384 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
10385 auto Filter = Lookup.makeFilter();
10386 while (Filter.hasNext()) {
10387 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
10388 if (InCompoundScope) {
10389 auto I = UsedAsPrevious.find(PrevDecl);
10390 if (I == UsedAsPrevious.end())
10391 UsedAsPrevious[PrevDecl] = false;
10392 if (auto *D = PrevDecl->getPrevDeclInScope())
10393 UsedAsPrevious[D] = true;
10394 }
10395 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
10396 PrevDecl->getLocation();
10397 }
10398 Filter.done();
10399 if (InCompoundScope) {
10400 for (auto &PrevData : UsedAsPrevious) {
10401 if (!PrevData.second) {
10402 PrevDRD = PrevData.first;
10403 break;
10404 }
10405 }
10406 }
10407 } else if (PrevDeclInScope != nullptr) {
10408 auto *PrevDRDInScope = PrevDRD =
10409 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
10410 do {
10411 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
10412 PrevDRDInScope->getLocation();
10413 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
10414 } while (PrevDRDInScope != nullptr);
10415 }
10416 for (auto &TyData : ReductionTypes) {
10417 auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
10418 bool Invalid = false;
10419 if (I != PreviousRedeclTypes.end()) {
10420 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
10421 << TyData.first;
10422 Diag(I->second, diag::note_previous_definition);
10423 Invalid = true;
10424 }
10425 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
10426 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
10427 Name, TyData.first, PrevDRD);
10428 DC->addDecl(DRD);
10429 DRD->setAccess(AS);
10430 Decls.push_back(DRD);
10431 if (Invalid)
10432 DRD->setInvalidDecl();
10433 else
10434 PrevDRD = DRD;
10435 }
10436
10437 return DeclGroupPtrTy::make(
10438 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
10439}
10440
10441void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
10442 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10443
10444 // Enter new function scope.
10445 PushFunctionScope();
10446 getCurFunction()->setHasBranchProtectedScope();
10447 getCurFunction()->setHasOMPDeclareReductionCombiner();
10448
10449 if (S != nullptr)
10450 PushDeclContext(S, DRD);
10451 else
10452 CurContext = DRD;
10453
10454 PushExpressionEvaluationContext(PotentiallyEvaluated);
10455
10456 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010457 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
10458 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
10459 // uses semantics of argument handles by value, but it should be passed by
10460 // reference. C lang does not support references, so pass all parameters as
10461 // pointers.
10462 // Create 'T omp_in;' variable.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010463 auto *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010464 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010465 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
10466 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
10467 // uses semantics of argument handles by value, but it should be passed by
10468 // reference. C lang does not support references, so pass all parameters as
10469 // pointers.
10470 // Create 'T omp_out;' variable.
10471 auto *OmpOutParm =
10472 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
10473 if (S != nullptr) {
10474 PushOnScopeChains(OmpInParm, S);
10475 PushOnScopeChains(OmpOutParm, S);
10476 } else {
10477 DRD->addDecl(OmpInParm);
10478 DRD->addDecl(OmpOutParm);
10479 }
10480}
10481
10482void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
10483 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10484 DiscardCleanupsInEvaluationContext();
10485 PopExpressionEvaluationContext();
10486
10487 PopDeclContext();
10488 PopFunctionScopeInfo();
10489
10490 if (Combiner != nullptr)
10491 DRD->setCombiner(Combiner);
10492 else
10493 DRD->setInvalidDecl();
10494}
10495
10496void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
10497 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10498
10499 // Enter new function scope.
10500 PushFunctionScope();
10501 getCurFunction()->setHasBranchProtectedScope();
10502
10503 if (S != nullptr)
10504 PushDeclContext(S, DRD);
10505 else
10506 CurContext = DRD;
10507
10508 PushExpressionEvaluationContext(PotentiallyEvaluated);
10509
10510 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010511 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
10512 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
10513 // uses semantics of argument handles by value, but it should be passed by
10514 // reference. C lang does not support references, so pass all parameters as
10515 // pointers.
10516 // Create 'T omp_priv;' variable.
10517 auto *OmpPrivParm =
10518 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010519 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
10520 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
10521 // uses semantics of argument handles by value, but it should be passed by
10522 // reference. C lang does not support references, so pass all parameters as
10523 // pointers.
10524 // Create 'T omp_orig;' variable.
10525 auto *OmpOrigParm =
10526 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010527 if (S != nullptr) {
10528 PushOnScopeChains(OmpPrivParm, S);
10529 PushOnScopeChains(OmpOrigParm, S);
10530 } else {
10531 DRD->addDecl(OmpPrivParm);
10532 DRD->addDecl(OmpOrigParm);
10533 }
10534}
10535
10536void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D,
10537 Expr *Initializer) {
10538 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10539 DiscardCleanupsInEvaluationContext();
10540 PopExpressionEvaluationContext();
10541
10542 PopDeclContext();
10543 PopFunctionScopeInfo();
10544
10545 if (Initializer != nullptr)
10546 DRD->setInitializer(Initializer);
10547 else
10548 DRD->setInvalidDecl();
10549}
10550
10551Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
10552 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
10553 for (auto *D : DeclReductions.get()) {
10554 if (IsValid) {
10555 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10556 if (S != nullptr)
10557 PushOnScopeChains(DRD, S, /*AddToContext=*/false);
10558 } else
10559 D->setInvalidDecl();
10560 }
10561 return DeclReductions;
10562}
10563
David Majnemer9d168222016-08-05 17:44:54 +000010564OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
Kelvin Li099bb8c2015-11-24 20:50:12 +000010565 SourceLocation StartLoc,
10566 SourceLocation LParenLoc,
10567 SourceLocation EndLoc) {
10568 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +000010569
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010570 // OpenMP [teams Constrcut, Restrictions]
10571 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010572 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
10573 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010574 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000010575
10576 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10577}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010578
10579OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
10580 SourceLocation StartLoc,
10581 SourceLocation LParenLoc,
10582 SourceLocation EndLoc) {
10583 Expr *ValExpr = ThreadLimit;
10584
10585 // OpenMP [teams Constrcut, Restrictions]
10586 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010587 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
10588 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010589 return nullptr;
10590
David Majnemer9d168222016-08-05 17:44:54 +000010591 return new (Context)
10592 OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010593}
Alexey Bataeva0569352015-12-01 10:17:31 +000010594
10595OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
10596 SourceLocation StartLoc,
10597 SourceLocation LParenLoc,
10598 SourceLocation EndLoc) {
10599 Expr *ValExpr = Priority;
10600
10601 // OpenMP [2.9.1, task Constrcut]
10602 // The priority-value is a non-negative numerical scalar expression.
10603 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
10604 /*StrictlyPositive=*/false))
10605 return nullptr;
10606
10607 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10608}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000010609
10610OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
10611 SourceLocation StartLoc,
10612 SourceLocation LParenLoc,
10613 SourceLocation EndLoc) {
10614 Expr *ValExpr = Grainsize;
10615
10616 // OpenMP [2.9.2, taskloop Constrcut]
10617 // The parameter of the grainsize clause must be a positive integer
10618 // expression.
10619 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
10620 /*StrictlyPositive=*/true))
10621 return nullptr;
10622
10623 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10624}
Alexey Bataev382967a2015-12-08 12:06:20 +000010625
10626OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
10627 SourceLocation StartLoc,
10628 SourceLocation LParenLoc,
10629 SourceLocation EndLoc) {
10630 Expr *ValExpr = NumTasks;
10631
10632 // OpenMP [2.9.2, taskloop Constrcut]
10633 // The parameter of the num_tasks clause must be a positive integer
10634 // expression.
10635 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
10636 /*StrictlyPositive=*/true))
10637 return nullptr;
10638
10639 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10640}
10641
Alexey Bataev28c75412015-12-15 08:19:24 +000010642OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
10643 SourceLocation LParenLoc,
10644 SourceLocation EndLoc) {
10645 // OpenMP [2.13.2, critical construct, Description]
10646 // ... where hint-expression is an integer constant expression that evaluates
10647 // to a valid lock hint.
10648 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
10649 if (HintExpr.isInvalid())
10650 return nullptr;
10651 return new (Context)
10652 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
10653}
10654
Carlo Bertollib4adf552016-01-15 18:50:31 +000010655OMPClause *Sema::ActOnOpenMPDistScheduleClause(
10656 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
10657 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
10658 SourceLocation EndLoc) {
10659 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
10660 std::string Values;
10661 Values += "'";
10662 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
10663 Values += "'";
10664 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
10665 << Values << getOpenMPClauseName(OMPC_dist_schedule);
10666 return nullptr;
10667 }
10668 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000010669 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000010670 if (ChunkSize) {
10671 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
10672 !ChunkSize->isInstantiationDependent() &&
10673 !ChunkSize->containsUnexpandedParameterPack()) {
10674 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
10675 ExprResult Val =
10676 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
10677 if (Val.isInvalid())
10678 return nullptr;
10679
10680 ValExpr = Val.get();
10681
10682 // OpenMP [2.7.1, Restrictions]
10683 // chunk_size must be a loop invariant integer expression with a positive
10684 // value.
10685 llvm::APSInt Result;
10686 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
10687 if (Result.isSigned() && !Result.isStrictlyPositive()) {
10688 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
10689 << "dist_schedule" << ChunkSize->getSourceRange();
10690 return nullptr;
10691 }
Alexey Bataevb46cdea2016-06-15 11:20:48 +000010692 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
10693 !CurContext->isDependentContext()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +000010694 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
10695 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
10696 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000010697 }
10698 }
10699 }
10700
10701 return new (Context)
10702 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000010703 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000010704}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000010705
10706OMPClause *Sema::ActOnOpenMPDefaultmapClause(
10707 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
10708 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
10709 SourceLocation KindLoc, SourceLocation EndLoc) {
10710 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
David Majnemer9d168222016-08-05 17:44:54 +000010711 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) {
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000010712 std::string Value;
10713 SourceLocation Loc;
10714 Value += "'";
10715 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
10716 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000010717 OMPC_DEFAULTMAP_MODIFIER_tofrom);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000010718 Loc = MLoc;
10719 } else {
10720 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000010721 OMPC_DEFAULTMAP_scalar);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000010722 Loc = KindLoc;
10723 }
10724 Value += "'";
10725 Diag(Loc, diag::err_omp_unexpected_clause_value)
10726 << Value << getOpenMPClauseName(OMPC_defaultmap);
10727 return nullptr;
10728 }
10729
10730 return new (Context)
10731 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
10732}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010733
10734bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
10735 DeclContext *CurLexicalContext = getCurLexicalContext();
10736 if (!CurLexicalContext->isFileContext() &&
10737 !CurLexicalContext->isExternCContext() &&
10738 !CurLexicalContext->isExternCXXContext()) {
10739 Diag(Loc, diag::err_omp_region_not_file_context);
10740 return false;
10741 }
10742 if (IsInOpenMPDeclareTargetContext) {
10743 Diag(Loc, diag::err_omp_enclosed_declare_target);
10744 return false;
10745 }
10746
10747 IsInOpenMPDeclareTargetContext = true;
10748 return true;
10749}
10750
10751void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
10752 assert(IsInOpenMPDeclareTargetContext &&
10753 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
10754
10755 IsInOpenMPDeclareTargetContext = false;
10756}
10757
David Majnemer9d168222016-08-05 17:44:54 +000010758void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope,
10759 CXXScopeSpec &ScopeSpec,
10760 const DeclarationNameInfo &Id,
10761 OMPDeclareTargetDeclAttr::MapTypeTy MT,
10762 NamedDeclSetType &SameDirectiveDecls) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000010763 LookupResult Lookup(*this, Id, LookupOrdinaryName);
10764 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
10765
10766 if (Lookup.isAmbiguous())
10767 return;
10768 Lookup.suppressDiagnostics();
10769
10770 if (!Lookup.isSingleResult()) {
10771 if (TypoCorrection Corrected =
10772 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
10773 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
10774 CTK_ErrorRecovery)) {
10775 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
10776 << Id.getName());
10777 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
10778 return;
10779 }
10780
10781 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
10782 return;
10783 }
10784
10785 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
10786 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
10787 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
10788 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
10789
10790 if (!ND->hasAttr<OMPDeclareTargetDeclAttr>()) {
10791 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
10792 ND->addAttr(A);
10793 if (ASTMutationListener *ML = Context.getASTMutationListener())
10794 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
10795 checkDeclIsAllowedInOpenMPTarget(nullptr, ND);
10796 } else if (ND->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() != MT) {
10797 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
10798 << Id.getName();
10799 }
10800 } else
10801 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
10802}
10803
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010804static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
10805 Sema &SemaRef, Decl *D) {
10806 if (!D)
10807 return;
10808 Decl *LD = nullptr;
10809 if (isa<TagDecl>(D)) {
10810 LD = cast<TagDecl>(D)->getDefinition();
10811 } else if (isa<VarDecl>(D)) {
10812 LD = cast<VarDecl>(D)->getDefinition();
10813
10814 // If this is an implicit variable that is legal and we do not need to do
10815 // anything.
10816 if (cast<VarDecl>(D)->isImplicit()) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000010817 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
10818 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
10819 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010820 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000010821 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010822 return;
10823 }
10824
10825 } else if (isa<FunctionDecl>(D)) {
10826 const FunctionDecl *FD = nullptr;
10827 if (cast<FunctionDecl>(D)->hasBody(FD))
10828 LD = const_cast<FunctionDecl *>(FD);
10829
10830 // If the definition is associated with the current declaration in the
10831 // target region (it can be e.g. a lambda) that is legal and we do not need
10832 // to do anything else.
10833 if (LD == D) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000010834 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
10835 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
10836 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010837 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000010838 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010839 return;
10840 }
10841 }
10842 if (!LD)
10843 LD = D;
10844 if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() &&
10845 (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) {
10846 // Outlined declaration is not declared target.
10847 if (LD->isOutOfLine()) {
10848 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
10849 SemaRef.Diag(SL, diag::note_used_here) << SR;
10850 } else {
10851 DeclContext *DC = LD->getDeclContext();
10852 while (DC) {
10853 if (isa<FunctionDecl>(DC) &&
10854 cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>())
10855 break;
10856 DC = DC->getParent();
10857 }
10858 if (DC)
10859 return;
10860
10861 // Is not declared in target context.
10862 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
10863 SemaRef.Diag(SL, diag::note_used_here) << SR;
10864 }
10865 // Mark decl as declared target to prevent further diagnostic.
Dmitry Polukhind69b5052016-05-09 14:59:13 +000010866 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
10867 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
10868 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010869 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000010870 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010871 }
10872}
10873
10874static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
10875 Sema &SemaRef, DSAStackTy *Stack,
10876 ValueDecl *VD) {
10877 if (VD->hasAttr<OMPDeclareTargetDeclAttr>())
10878 return true;
10879 if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType()))
10880 return false;
10881 return true;
10882}
10883
10884void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D) {
10885 if (!D || D->isInvalidDecl())
10886 return;
10887 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
10888 SourceLocation SL = E ? E->getLocStart() : D->getLocation();
10889 // 2.10.6: threadprivate variable cannot appear in a declare target directive.
10890 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
10891 if (DSAStack->isThreadPrivate(VD)) {
10892 Diag(SL, diag::err_omp_threadprivate_in_target);
10893 ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
10894 return;
10895 }
10896 }
10897 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
10898 // Problem if any with var declared with incomplete type will be reported
10899 // as normal, so no need to check it here.
10900 if ((E || !VD->getType()->isIncompleteType()) &&
10901 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) {
10902 // Mark decl as declared target to prevent further diagnostic.
10903 if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000010904 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
10905 Context, OMPDeclareTargetDeclAttr::MT_To);
10906 VD->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010907 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000010908 ML->DeclarationMarkedOpenMPDeclareTarget(VD, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010909 }
10910 return;
10911 }
10912 }
10913 if (!E) {
10914 // Checking declaration inside declare target region.
10915 if (!D->hasAttr<OMPDeclareTargetDeclAttr>() &&
10916 (isa<VarDecl>(D) || isa<FunctionDecl>(D))) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000010917 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
10918 Context, OMPDeclareTargetDeclAttr::MT_To);
10919 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010920 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000010921 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010922 }
10923 return;
10924 }
10925 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
10926}
Samuel Antao661c0902016-05-26 17:39:58 +000010927
10928OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
10929 SourceLocation StartLoc,
10930 SourceLocation LParenLoc,
10931 SourceLocation EndLoc) {
10932 MappableVarListInfo MVLI(VarList);
10933 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc);
10934 if (MVLI.ProcessedVarList.empty())
10935 return nullptr;
10936
10937 return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10938 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
10939 MVLI.VarComponents);
10940}
Samuel Antaoec172c62016-05-26 17:49:04 +000010941
10942OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
10943 SourceLocation StartLoc,
10944 SourceLocation LParenLoc,
10945 SourceLocation EndLoc) {
10946 MappableVarListInfo MVLI(VarList);
10947 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc);
10948 if (MVLI.ProcessedVarList.empty())
10949 return nullptr;
10950
10951 return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10952 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
10953 MVLI.VarComponents);
10954}
Carlo Bertolli2404b172016-07-13 15:37:16 +000010955
10956OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
10957 SourceLocation StartLoc,
10958 SourceLocation LParenLoc,
10959 SourceLocation EndLoc) {
Samuel Antaocc10b852016-07-28 14:23:26 +000010960 MappableVarListInfo MVLI(VarList);
10961 SmallVector<Expr *, 8> PrivateCopies;
10962 SmallVector<Expr *, 8> Inits;
10963
Carlo Bertolli2404b172016-07-13 15:37:16 +000010964 for (auto &RefExpr : VarList) {
10965 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
10966 SourceLocation ELoc;
10967 SourceRange ERange;
10968 Expr *SimpleRefExpr = RefExpr;
10969 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
10970 if (Res.second) {
10971 // It will be analyzed later.
Samuel Antaocc10b852016-07-28 14:23:26 +000010972 MVLI.ProcessedVarList.push_back(RefExpr);
10973 PrivateCopies.push_back(nullptr);
10974 Inits.push_back(nullptr);
Carlo Bertolli2404b172016-07-13 15:37:16 +000010975 }
10976 ValueDecl *D = Res.first;
10977 if (!D)
10978 continue;
10979
10980 QualType Type = D->getType();
Samuel Antaocc10b852016-07-28 14:23:26 +000010981 Type = Type.getNonReferenceType().getUnqualifiedType();
10982
10983 auto *VD = dyn_cast<VarDecl>(D);
10984
10985 // Item should be a pointer or reference to pointer.
10986 if (!Type->isPointerType()) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000010987 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
10988 << 0 << RefExpr->getSourceRange();
10989 continue;
10990 }
Samuel Antaocc10b852016-07-28 14:23:26 +000010991
10992 // Build the private variable and the expression that refers to it.
10993 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
10994 D->hasAttrs() ? &D->getAttrs() : nullptr);
10995 if (VDPrivate->isInvalidDecl())
10996 continue;
10997
10998 CurContext->addDecl(VDPrivate);
10999 auto VDPrivateRefExpr = buildDeclRefExpr(
11000 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
11001
11002 // Add temporary variable to initialize the private copy of the pointer.
11003 auto *VDInit =
11004 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
11005 auto *VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
11006 RefExpr->getExprLoc());
11007 AddInitializerToDecl(VDPrivate,
11008 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000011009 /*DirectInit=*/false);
Samuel Antaocc10b852016-07-28 14:23:26 +000011010
11011 // If required, build a capture to implement the privatization initialized
11012 // with the current list item value.
11013 DeclRefExpr *Ref = nullptr;
11014 if (!VD)
11015 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
11016 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
11017 PrivateCopies.push_back(VDPrivateRefExpr);
11018 Inits.push_back(VDInitRefExpr);
11019
11020 // We need to add a data sharing attribute for this variable to make sure it
11021 // is correctly captured. A variable that shows up in a use_device_ptr has
11022 // similar properties of a first private variable.
11023 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
11024
11025 // Create a mappable component for the list item. List items in this clause
11026 // only need a component.
11027 MVLI.VarBaseDeclarations.push_back(D);
11028 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
11029 MVLI.VarComponents.back().push_back(
11030 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D));
Carlo Bertolli2404b172016-07-13 15:37:16 +000011031 }
11032
Samuel Antaocc10b852016-07-28 14:23:26 +000011033 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli2404b172016-07-13 15:37:16 +000011034 return nullptr;
11035
Samuel Antaocc10b852016-07-28 14:23:26 +000011036 return OMPUseDevicePtrClause::Create(
11037 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
11038 PrivateCopies, Inits, MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli2404b172016-07-13 15:37:16 +000011039}
Carlo Bertolli70594e92016-07-13 17:16:49 +000011040
11041OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
11042 SourceLocation StartLoc,
11043 SourceLocation LParenLoc,
11044 SourceLocation EndLoc) {
Samuel Antao6890b092016-07-28 14:25:09 +000011045 MappableVarListInfo MVLI(VarList);
Carlo Bertolli70594e92016-07-13 17:16:49 +000011046 for (auto &RefExpr : VarList) {
Kelvin Li84376252016-12-14 15:39:58 +000011047 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause.");
Carlo Bertolli70594e92016-07-13 17:16:49 +000011048 SourceLocation ELoc;
11049 SourceRange ERange;
11050 Expr *SimpleRefExpr = RefExpr;
11051 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
11052 if (Res.second) {
11053 // It will be analyzed later.
Samuel Antao6890b092016-07-28 14:25:09 +000011054 MVLI.ProcessedVarList.push_back(RefExpr);
Carlo Bertolli70594e92016-07-13 17:16:49 +000011055 }
11056 ValueDecl *D = Res.first;
11057 if (!D)
11058 continue;
11059
11060 QualType Type = D->getType();
11061 // item should be a pointer or array or reference to pointer or array
11062 if (!Type.getNonReferenceType()->isPointerType() &&
11063 !Type.getNonReferenceType()->isArrayType()) {
11064 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
11065 << 0 << RefExpr->getSourceRange();
11066 continue;
11067 }
Samuel Antao6890b092016-07-28 14:25:09 +000011068
11069 // Check if the declaration in the clause does not show up in any data
11070 // sharing attribute.
11071 auto DVar = DSAStack->getTopDSA(D, false);
11072 if (isOpenMPPrivate(DVar.CKind)) {
11073 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
11074 << getOpenMPClauseName(DVar.CKind)
11075 << getOpenMPClauseName(OMPC_is_device_ptr)
11076 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
11077 ReportOriginalDSA(*this, DSAStack, D, DVar);
11078 continue;
11079 }
11080
11081 Expr *ConflictExpr;
11082 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +000011083 D, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +000011084 [&ConflictExpr](
11085 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
11086 OpenMPClauseKind) -> bool {
11087 ConflictExpr = R.front().getAssociatedExpression();
11088 return true;
11089 })) {
11090 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
11091 Diag(ConflictExpr->getExprLoc(), diag::note_used_here)
11092 << ConflictExpr->getSourceRange();
11093 continue;
11094 }
11095
11096 // Store the components in the stack so that they can be used to check
11097 // against other clauses later on.
11098 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D);
11099 DSAStack->addMappableExpressionComponents(
11100 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
11101
11102 // Record the expression we've just processed.
11103 MVLI.ProcessedVarList.push_back(SimpleRefExpr);
11104
11105 // Create a mappable component for the list item. List items in this clause
11106 // only need a component. We use a null declaration to signal fields in
11107 // 'this'.
11108 assert((isa<DeclRefExpr>(SimpleRefExpr) ||
11109 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
11110 "Unexpected device pointer expression!");
11111 MVLI.VarBaseDeclarations.push_back(
11112 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr);
11113 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
11114 MVLI.VarComponents.back().push_back(MC);
Carlo Bertolli70594e92016-07-13 17:16:49 +000011115 }
11116
Samuel Antao6890b092016-07-28 14:25:09 +000011117 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli70594e92016-07-13 17:16:49 +000011118 return nullptr;
11119
Samuel Antao6890b092016-07-28 14:25:09 +000011120 return OMPIsDevicePtrClause::Create(
11121 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
11122 MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli70594e92016-07-13 17:16:49 +000011123}