blob: 8c7111fe646b5606718322265dc114006544a8c3 [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 Bataev9959db52014-05-06 10:08:46 +000025#include "clang/Basic/OpenMPKinds.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000026#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000027#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000028#include "clang/Sema/Scope.h"
29#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000030#include "clang/Sema/SemaInternal.h"
Alexey Bataevfa312f32017-07-21 18:48:21 +000031#include "llvm/ADT/PointerEmbeddedInt.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000032using namespace clang;
33
Alexey Bataev758e55e2013-09-06 18:03:48 +000034//===----------------------------------------------------------------------===//
35// Stack of data-sharing attributes for variables
36//===----------------------------------------------------------------------===//
37
Alexey Bataevf47c4b42017-09-26 13:47:31 +000038static Expr *CheckMapClauseExpressionBase(
39 Sema &SemaRef, Expr *E,
40 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
Alexey Bataevb7a9b742017-12-05 19:20:09 +000041 OpenMPClauseKind CKind, bool NoDiagnose);
Alexey Bataevf47c4b42017-09-26 13:47:31 +000042
Alexey Bataev758e55e2013-09-06 18:03:48 +000043namespace {
44/// \brief Default data sharing attributes, which can be applied to directive.
45enum DefaultDataSharingAttributes {
Alexey Bataeved09d242014-05-28 05:53:51 +000046 DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
47 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
Alexey Bataev2fd0cb22017-10-05 17:51:39 +000048 DSA_shared = 1 << 1, /// \brief Default data sharing attribute 'shared'.
49};
50
51/// Attributes of the defaultmap clause.
52enum DefaultMapAttributes {
53 DMA_unspecified, /// Default mapping is not specified.
54 DMA_tofrom_scalar, /// Default mapping is 'tofrom:scalar'.
Alexey Bataev758e55e2013-09-06 18:03:48 +000055};
Alexey Bataev7ff55242014-06-19 09:13:45 +000056
Alexey Bataev758e55e2013-09-06 18:03:48 +000057/// \brief Stack for tracking declarations used in OpenMP directives and
58/// clauses and their data-sharing attributes.
Alexey Bataev7ace49d2016-05-17 08:55:33 +000059class DSAStackTy final {
Alexey Bataev758e55e2013-09-06 18:03:48 +000060public:
Alexey Bataev7ace49d2016-05-17 08:55:33 +000061 struct DSAVarData final {
62 OpenMPDirectiveKind DKind = OMPD_unknown;
63 OpenMPClauseKind CKind = OMPC_unknown;
64 Expr *RefExpr = nullptr;
65 DeclRefExpr *PrivateCopy = nullptr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000066 SourceLocation ImplicitDSALoc;
Alexey Bataev4d4624c2017-07-20 16:47:47 +000067 DSAVarData() = default;
Alexey Bataevf189cb72017-07-24 14:52:13 +000068 DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind, Expr *RefExpr,
69 DeclRefExpr *PrivateCopy, SourceLocation ImplicitDSALoc)
70 : DKind(DKind), CKind(CKind), RefExpr(RefExpr),
71 PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000072 };
Alexey Bataev8b427062016-05-25 12:36:08 +000073 typedef llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>
74 OperatorOffsetTy;
Alexey Bataeved09d242014-05-28 05:53:51 +000075
Alexey Bataev758e55e2013-09-06 18:03:48 +000076private:
Alexey Bataev7ace49d2016-05-17 08:55:33 +000077 struct DSAInfo final {
78 OpenMPClauseKind Attributes = OMPC_unknown;
79 /// Pointer to a reference expression and a flag which shows that the
80 /// variable is marked as lastprivate(true) or not (false).
81 llvm::PointerIntPair<Expr *, 1, bool> RefExpr;
82 DeclRefExpr *PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +000083 };
Alexey Bataev90c228f2016-02-08 09:29:13 +000084 typedef llvm::DenseMap<ValueDecl *, DSAInfo> DeclSAMapTy;
85 typedef llvm::DenseMap<ValueDecl *, Expr *> AlignedMapTy;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +000086 typedef std::pair<unsigned, VarDecl *> LCDeclInfo;
87 typedef llvm::DenseMap<ValueDecl *, LCDeclInfo> LoopControlVariablesMapTy;
Samuel Antao6890b092016-07-28 14:25:09 +000088 /// Struct that associates a component with the clause kind where they are
89 /// found.
90 struct MappedExprComponentTy {
91 OMPClauseMappableExprCommon::MappableExprComponentLists Components;
92 OpenMPClauseKind Kind = OMPC_unknown;
93 };
94 typedef llvm::DenseMap<ValueDecl *, MappedExprComponentTy>
Samuel Antao90927002016-04-26 14:54:23 +000095 MappedExprComponentsTy;
Alexey Bataev28c75412015-12-15 08:19:24 +000096 typedef llvm::StringMap<std::pair<OMPCriticalDirective *, llvm::APSInt>>
97 CriticalsWithHintsTy;
Alexey Bataev8b427062016-05-25 12:36:08 +000098 typedef llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>
99 DoacrossDependMapTy;
Alexey Bataevfa312f32017-07-21 18:48:21 +0000100 struct ReductionData {
Alexey Bataevf87fa882017-07-21 19:26:22 +0000101 typedef llvm::PointerEmbeddedInt<BinaryOperatorKind, 16> BOKPtrType;
Alexey Bataevfa312f32017-07-21 18:48:21 +0000102 SourceRange ReductionRange;
Alexey Bataevf87fa882017-07-21 19:26:22 +0000103 llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp;
Alexey Bataevfa312f32017-07-21 18:48:21 +0000104 ReductionData() = default;
105 void set(BinaryOperatorKind BO, SourceRange RR) {
106 ReductionRange = RR;
107 ReductionOp = BO;
108 }
109 void set(const Expr *RefExpr, SourceRange RR) {
110 ReductionRange = RR;
111 ReductionOp = RefExpr;
112 }
113 };
114 typedef llvm::DenseMap<ValueDecl *, ReductionData> DeclReductionMapTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000115
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000116 struct SharingMapTy final {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000117 DeclSAMapTy SharingMap;
Alexey Bataevfa312f32017-07-21 18:48:21 +0000118 DeclReductionMapTy ReductionMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000119 AlignedMapTy AlignedMap;
Samuel Antao90927002016-04-26 14:54:23 +0000120 MappedExprComponentsTy MappedExprComponents;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000121 LoopControlVariablesMapTy LCVMap;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000122 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000123 SourceLocation DefaultAttrLoc;
Alexey Bataev2fd0cb22017-10-05 17:51:39 +0000124 DefaultMapAttributes DefaultMapAttr = DMA_unspecified;
125 SourceLocation DefaultMapAttrLoc;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000126 OpenMPDirectiveKind Directive = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000127 DeclarationNameInfo DirectiveName;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000128 Scope *CurScope = nullptr;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000129 SourceLocation ConstructLoc;
Alexey Bataev8b427062016-05-25 12:36:08 +0000130 /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to
131 /// get the data (loop counters etc.) about enclosing loop-based construct.
132 /// This data is required during codegen.
133 DoacrossDependMapTy DoacrossDepends;
Alexey Bataev346265e2015-09-25 10:37:12 +0000134 /// \brief first argument (Expr *) contains optional argument of the
135 /// 'ordered' clause, the second one is true if the regions has 'ordered'
136 /// clause, false otherwise.
137 llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000138 bool NowaitRegion = false;
139 bool CancelRegion = false;
140 unsigned AssociatedLoops = 1;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000141 SourceLocation InnerTeamsRegionLoc;
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000142 /// Reference to the taskgroup task_reduction reference expression.
143 Expr *TaskgroupReductionRef = nullptr;
Alexey Bataeved09d242014-05-28 05:53:51 +0000144 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000145 Scope *CurScope, SourceLocation Loc)
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000146 : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
147 ConstructLoc(Loc) {}
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000148 SharingMapTy() = default;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000149 };
150
Axel Naumann323862e2016-02-03 10:45:22 +0000151 typedef SmallVector<SharingMapTy, 4> StackTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000152
153 /// \brief Stack of used declaration and their data-sharing attributes.
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000154 DeclSAMapTy Threadprivates;
Alexey Bataev4b465392017-04-26 15:06:24 +0000155 const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr;
156 SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000157 /// \brief true, if check for DSA must be from parent directive, false, if
158 /// from current directive.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000159 OpenMPClauseKind ClauseKindMode = OMPC_unknown;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000160 Sema &SemaRef;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000161 bool ForceCapturing = false;
Alexey Bataev28c75412015-12-15 08:19:24 +0000162 CriticalsWithHintsTy Criticals;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000163
164 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
165
David Majnemer9d168222016-08-05 17:44:54 +0000166 DSAVarData getDSA(StackTy::reverse_iterator &Iter, ValueDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000167
168 /// \brief Checks if the variable is a local for OpenMP region.
169 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000170
Alexey Bataev4b465392017-04-26 15:06:24 +0000171 bool isStackEmpty() const {
172 return Stack.empty() ||
173 Stack.back().second != CurrentNonCapturingFunctionScope ||
174 Stack.back().first.empty();
175 }
176
Alexey Bataev758e55e2013-09-06 18:03:48 +0000177public:
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000178 explicit DSAStackTy(Sema &S) : SemaRef(S) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000179
Alexey Bataevaac108a2015-06-23 04:51:00 +0000180 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
Alexey Bataev3f82cfc2017-12-13 15:28:44 +0000181 OpenMPClauseKind getClauseParsingMode() const {
182 assert(isClauseParsingMode() && "Must be in clause parsing mode.");
183 return ClauseKindMode;
184 }
Alexey Bataevaac108a2015-06-23 04:51:00 +0000185 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000186
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000187 bool isForceVarCapturing() const { return ForceCapturing; }
188 void setForceVarCapturing(bool V) { ForceCapturing = V; }
189
Alexey Bataev758e55e2013-09-06 18:03:48 +0000190 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000191 Scope *CurScope, SourceLocation Loc) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000192 if (Stack.empty() ||
193 Stack.back().second != CurrentNonCapturingFunctionScope)
194 Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope);
195 Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc);
196 Stack.back().first.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000197 }
198
199 void pop() {
Alexey Bataev4b465392017-04-26 15:06:24 +0000200 assert(!Stack.back().first.empty() &&
201 "Data-sharing attributes stack is empty!");
202 Stack.back().first.pop_back();
203 }
204
205 /// Start new OpenMP region stack in new non-capturing function.
206 void pushFunction() {
207 const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction();
208 assert(!isa<CapturingScopeInfo>(CurFnScope));
209 CurrentNonCapturingFunctionScope = CurFnScope;
210 }
211 /// Pop region stack for non-capturing function.
212 void popFunction(const FunctionScopeInfo *OldFSI) {
213 if (!Stack.empty() && Stack.back().second == OldFSI) {
214 assert(Stack.back().first.empty());
215 Stack.pop_back();
216 }
217 CurrentNonCapturingFunctionScope = nullptr;
218 for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) {
219 if (!isa<CapturingScopeInfo>(FSI)) {
220 CurrentNonCapturingFunctionScope = FSI;
221 break;
222 }
223 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000224 }
225
Alexey Bataev28c75412015-12-15 08:19:24 +0000226 void addCriticalWithHint(OMPCriticalDirective *D, llvm::APSInt Hint) {
227 Criticals[D->getDirectiveName().getAsString()] = std::make_pair(D, Hint);
228 }
229 const std::pair<OMPCriticalDirective *, llvm::APSInt>
230 getCriticalWithHint(const DeclarationNameInfo &Name) const {
231 auto I = Criticals.find(Name.getAsString());
232 if (I != Criticals.end())
233 return I->second;
234 return std::make_pair(nullptr, llvm::APSInt());
235 }
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000236 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000237 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000238 /// for diagnostics.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000239 Expr *addUniqueAligned(ValueDecl *D, Expr *NewDE);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000240
Alexey Bataev9c821032015-04-30 04:23:23 +0000241 /// \brief Register specified variable as loop control variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000242 void addLoopControlVariable(ValueDecl *D, VarDecl *Capture);
Alexey Bataev9c821032015-04-30 04:23:23 +0000243 /// \brief Check if the specified variable is a loop control variable for
244 /// current region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000245 /// \return The index of the loop control variable in the list of associated
246 /// for-loops (from outer to inner).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000247 LCDeclInfo isLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000248 /// \brief Check if the specified variable is a loop control variable for
249 /// parent region.
250 /// \return The index of the loop control variable in the list of associated
251 /// for-loops (from outer to inner).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000252 LCDeclInfo isParentLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000253 /// \brief Get the loop control variable for the I-th loop (or nullptr) in
254 /// parent directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000255 ValueDecl *getParentLoopControlVariable(unsigned I);
Alexey Bataev9c821032015-04-30 04:23:23 +0000256
Alexey Bataev758e55e2013-09-06 18:03:48 +0000257 /// \brief Adds explicit data sharing attribute to the specified declaration.
Alexey Bataev90c228f2016-02-08 09:29:13 +0000258 void addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
259 DeclRefExpr *PrivateCopy = nullptr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000260
Alexey Bataevfa312f32017-07-21 18:48:21 +0000261 /// Adds additional information for the reduction items with the reduction id
262 /// represented as an operator.
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000263 void addTaskgroupReductionData(ValueDecl *D, SourceRange SR,
264 BinaryOperatorKind BOK);
Alexey Bataevfa312f32017-07-21 18:48:21 +0000265 /// Adds additional information for the reduction items with the reduction id
266 /// represented as reduction identifier.
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000267 void addTaskgroupReductionData(ValueDecl *D, SourceRange SR,
268 const Expr *ReductionRef);
Alexey Bataevfa312f32017-07-21 18:48:21 +0000269 /// Returns the location and reduction operation from the innermost parent
270 /// region for the given \p D.
Alexey Bataevf189cb72017-07-24 14:52:13 +0000271 DSAVarData getTopMostTaskgroupReductionData(ValueDecl *D, SourceRange &SR,
Alexey Bataev88202be2017-07-27 13:20:36 +0000272 BinaryOperatorKind &BOK,
273 Expr *&TaskgroupDescriptor);
Alexey Bataevfa312f32017-07-21 18:48:21 +0000274 /// Returns the location and reduction operation from the innermost parent
275 /// region for the given \p D.
Alexey Bataevf189cb72017-07-24 14:52:13 +0000276 DSAVarData getTopMostTaskgroupReductionData(ValueDecl *D, SourceRange &SR,
Alexey Bataev88202be2017-07-27 13:20:36 +0000277 const Expr *&ReductionRef,
278 Expr *&TaskgroupDescriptor);
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000279 /// Return reduction reference expression for the current taskgroup.
280 Expr *getTaskgroupReductionRef() const {
281 assert(Stack.back().first.back().Directive == OMPD_taskgroup &&
282 "taskgroup reference expression requested for non taskgroup "
283 "directive.");
284 return Stack.back().first.back().TaskgroupReductionRef;
285 }
Alexey Bataev88202be2017-07-27 13:20:36 +0000286 /// Checks if the given \p VD declaration is actually a taskgroup reduction
287 /// descriptor variable at the \p Level of OpenMP regions.
288 bool isTaskgroupReductionRef(ValueDecl *VD, unsigned Level) const {
289 return Stack.back().first[Level].TaskgroupReductionRef &&
290 cast<DeclRefExpr>(Stack.back().first[Level].TaskgroupReductionRef)
291 ->getDecl() == VD;
292 }
Alexey Bataevfa312f32017-07-21 18:48:21 +0000293
Alexey Bataev758e55e2013-09-06 18:03:48 +0000294 /// \brief Returns data sharing attributes from top of the stack for the
295 /// specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000296 DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000297 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000298 DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000299 /// \brief Checks if the specified variables has data-sharing attributes which
300 /// match specified \a CPred predicate in any directive which matches \a DPred
301 /// predicate.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000302 DSAVarData hasDSA(ValueDecl *D,
303 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
304 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
305 bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000306 /// \brief Checks if the specified variables has data-sharing attributes which
307 /// match specified \a CPred predicate in any innermost directive which
308 /// matches \a DPred predicate.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000309 DSAVarData
310 hasInnermostDSA(ValueDecl *D,
311 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
312 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
313 bool FromParent);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000314 /// \brief Checks if the specified variables has explicit data-sharing
315 /// attributes which match specified \a CPred predicate at the specified
316 /// OpenMP region.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000317 bool hasExplicitDSA(ValueDecl *D,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000318 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000319 unsigned Level, bool NotLastprivate = false);
Samuel Antao4be30e92015-10-02 17:14:03 +0000320
321 /// \brief Returns true if the directive at level \Level matches in the
322 /// specified \a DPred predicate.
323 bool hasExplicitDirective(
324 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
325 unsigned Level);
326
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000327 /// \brief Finds a directive which matches specified \a DPred predicate.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000328 bool hasDirective(const llvm::function_ref<bool(OpenMPDirectiveKind,
329 const DeclarationNameInfo &,
330 SourceLocation)> &DPred,
331 bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000332
Alexey Bataev758e55e2013-09-06 18:03:48 +0000333 /// \brief Returns currently analyzed directive.
334 OpenMPDirectiveKind getCurrentDirective() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000335 return isStackEmpty() ? OMPD_unknown : Stack.back().first.back().Directive;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000336 }
Alexey Bataevdfa430f2017-12-08 15:03:50 +0000337 /// \brief Returns directive kind at specified level.
338 OpenMPDirectiveKind getDirective(unsigned Level) const {
339 assert(!isStackEmpty() && "No directive at specified level.");
340 return Stack.back().first[Level].Directive;
341 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000342 /// \brief Returns parent directive.
343 OpenMPDirectiveKind getParentDirective() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000344 if (isStackEmpty() || Stack.back().first.size() == 1)
345 return OMPD_unknown;
346 return std::next(Stack.back().first.rbegin())->Directive;
Alexey Bataev549210e2014-06-24 04:39:47 +0000347 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000348
349 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000350 void setDefaultDSANone(SourceLocation Loc) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000351 assert(!isStackEmpty());
352 Stack.back().first.back().DefaultAttr = DSA_none;
353 Stack.back().first.back().DefaultAttrLoc = Loc;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000354 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000355 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000356 void setDefaultDSAShared(SourceLocation Loc) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000357 assert(!isStackEmpty());
358 Stack.back().first.back().DefaultAttr = DSA_shared;
359 Stack.back().first.back().DefaultAttrLoc = Loc;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000360 }
Alexey Bataev2fd0cb22017-10-05 17:51:39 +0000361 /// Set default data mapping attribute to 'tofrom:scalar'.
362 void setDefaultDMAToFromScalar(SourceLocation Loc) {
363 assert(!isStackEmpty());
364 Stack.back().first.back().DefaultMapAttr = DMA_tofrom_scalar;
365 Stack.back().first.back().DefaultMapAttrLoc = Loc;
366 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000367
368 DefaultDataSharingAttributes getDefaultDSA() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000369 return isStackEmpty() ? DSA_unspecified
370 : Stack.back().first.back().DefaultAttr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000371 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000372 SourceLocation getDefaultDSALocation() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000373 return isStackEmpty() ? SourceLocation()
374 : Stack.back().first.back().DefaultAttrLoc;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000375 }
Alexey Bataev2fd0cb22017-10-05 17:51:39 +0000376 DefaultMapAttributes getDefaultDMA() const {
377 return isStackEmpty() ? DMA_unspecified
378 : Stack.back().first.back().DefaultMapAttr;
379 }
380 DefaultMapAttributes getDefaultDMAAtLevel(unsigned Level) const {
381 return Stack.back().first[Level].DefaultMapAttr;
382 }
383 SourceLocation getDefaultDMALocation() const {
384 return isStackEmpty() ? SourceLocation()
385 : Stack.back().first.back().DefaultMapAttrLoc;
386 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000387
Alexey Bataevf29276e2014-06-18 04:14:57 +0000388 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000389 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000390 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000391 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000392 }
393
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000394 /// \brief Marks current region as ordered (it has an 'ordered' clause).
Alexey Bataev346265e2015-09-25 10:37:12 +0000395 void setOrderedRegion(bool IsOrdered, Expr *Param) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000396 assert(!isStackEmpty());
397 Stack.back().first.back().OrderedRegion.setInt(IsOrdered);
398 Stack.back().first.back().OrderedRegion.setPointer(Param);
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000399 }
400 /// \brief Returns true, if parent region is ordered (has associated
401 /// 'ordered' clause), false - otherwise.
402 bool isParentOrderedRegion() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000403 if (isStackEmpty() || Stack.back().first.size() == 1)
404 return false;
405 return std::next(Stack.back().first.rbegin())->OrderedRegion.getInt();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000406 }
Alexey Bataev346265e2015-09-25 10:37:12 +0000407 /// \brief Returns optional parameter for the ordered region.
408 Expr *getParentOrderedRegionParam() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000409 if (isStackEmpty() || Stack.back().first.size() == 1)
410 return nullptr;
411 return std::next(Stack.back().first.rbegin())->OrderedRegion.getPointer();
Alexey Bataev346265e2015-09-25 10:37:12 +0000412 }
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000413 /// \brief Marks current region as nowait (it has a 'nowait' clause).
414 void setNowaitRegion(bool IsNowait = true) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000415 assert(!isStackEmpty());
416 Stack.back().first.back().NowaitRegion = IsNowait;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000417 }
418 /// \brief Returns true, if parent region is nowait (has associated
419 /// 'nowait' clause), false - otherwise.
420 bool isParentNowaitRegion() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000421 if (isStackEmpty() || Stack.back().first.size() == 1)
422 return false;
423 return std::next(Stack.back().first.rbegin())->NowaitRegion;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000424 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000425 /// \brief Marks parent region as cancel region.
426 void setParentCancelRegion(bool Cancel = true) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000427 if (!isStackEmpty() && Stack.back().first.size() > 1) {
428 auto &StackElemRef = *std::next(Stack.back().first.rbegin());
429 StackElemRef.CancelRegion |= StackElemRef.CancelRegion || Cancel;
430 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000431 }
432 /// \brief Return true if current region has inner cancel construct.
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000433 bool isCancelRegion() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000434 return isStackEmpty() ? false : Stack.back().first.back().CancelRegion;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000435 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000436
Alexey Bataev9c821032015-04-30 04:23:23 +0000437 /// \brief Set collapse value for the region.
Alexey Bataev4b465392017-04-26 15:06:24 +0000438 void setAssociatedLoops(unsigned Val) {
439 assert(!isStackEmpty());
440 Stack.back().first.back().AssociatedLoops = Val;
441 }
Alexey Bataev9c821032015-04-30 04:23:23 +0000442 /// \brief Return collapse value for region.
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000443 unsigned getAssociatedLoops() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000444 return isStackEmpty() ? 0 : Stack.back().first.back().AssociatedLoops;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000445 }
Alexey Bataev9c821032015-04-30 04:23:23 +0000446
Alexey Bataev13314bf2014-10-09 04:18:56 +0000447 /// \brief Marks current target region as one with closely nested teams
448 /// region.
449 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000450 if (!isStackEmpty() && Stack.back().first.size() > 1) {
451 std::next(Stack.back().first.rbegin())->InnerTeamsRegionLoc =
452 TeamsRegionLoc;
453 }
Alexey Bataev13314bf2014-10-09 04:18:56 +0000454 }
455 /// \brief Returns true, if current region has closely nested teams region.
456 bool hasInnerTeamsRegion() const {
457 return getInnerTeamsRegionLoc().isValid();
458 }
459 /// \brief Returns location of the nested teams region (if any).
460 SourceLocation getInnerTeamsRegionLoc() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000461 return isStackEmpty() ? SourceLocation()
462 : Stack.back().first.back().InnerTeamsRegionLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000463 }
464
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000465 Scope *getCurScope() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000466 return isStackEmpty() ? nullptr : Stack.back().first.back().CurScope;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000467 }
468 Scope *getCurScope() {
Alexey Bataev4b465392017-04-26 15:06:24 +0000469 return isStackEmpty() ? nullptr : Stack.back().first.back().CurScope;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000470 }
471 SourceLocation getConstructLoc() {
Alexey Bataev4b465392017-04-26 15:06:24 +0000472 return isStackEmpty() ? SourceLocation()
473 : Stack.back().first.back().ConstructLoc;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000474 }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000475
Samuel Antao4c8035b2016-12-12 18:00:20 +0000476 /// Do the check specified in \a Check to all component lists and return true
477 /// if any issue is found.
Samuel Antao90927002016-04-26 14:54:23 +0000478 bool checkMappableExprComponentListsForDecl(
479 ValueDecl *VD, bool CurrentRegionOnly,
Samuel Antao6890b092016-07-28 14:25:09 +0000480 const llvm::function_ref<
481 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
482 OpenMPClauseKind)> &Check) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000483 if (isStackEmpty())
484 return false;
485 auto SI = Stack.back().first.rbegin();
486 auto SE = Stack.back().first.rend();
Samuel Antao5de996e2016-01-22 20:21:36 +0000487
488 if (SI == SE)
489 return false;
490
491 if (CurrentRegionOnly) {
492 SE = std::next(SI);
493 } else {
494 ++SI;
495 }
496
497 for (; SI != SE; ++SI) {
Samuel Antao90927002016-04-26 14:54:23 +0000498 auto MI = SI->MappedExprComponents.find(VD);
499 if (MI != SI->MappedExprComponents.end())
Samuel Antao6890b092016-07-28 14:25:09 +0000500 for (auto &L : MI->second.Components)
501 if (Check(L, MI->second.Kind))
Samuel Antao5de996e2016-01-22 20:21:36 +0000502 return true;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000503 }
Samuel Antao5de996e2016-01-22 20:21:36 +0000504 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000505 }
506
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000507 /// Do the check specified in \a Check to all component lists at a given level
508 /// and return true if any issue is found.
509 bool checkMappableExprComponentListsForDeclAtLevel(
510 ValueDecl *VD, unsigned Level,
511 const llvm::function_ref<
512 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
513 OpenMPClauseKind)> &Check) {
514 if (isStackEmpty())
515 return false;
516
517 auto StartI = Stack.back().first.begin();
518 auto EndI = Stack.back().first.end();
519 if (std::distance(StartI, EndI) <= (int)Level)
520 return false;
521 std::advance(StartI, Level);
522
523 auto MI = StartI->MappedExprComponents.find(VD);
524 if (MI != StartI->MappedExprComponents.end())
525 for (auto &L : MI->second.Components)
526 if (Check(L, MI->second.Kind))
527 return true;
528 return false;
529 }
530
Samuel Antao4c8035b2016-12-12 18:00:20 +0000531 /// Create a new mappable expression component list associated with a given
532 /// declaration and initialize it with the provided list of components.
Samuel Antao90927002016-04-26 14:54:23 +0000533 void addMappableExpressionComponents(
534 ValueDecl *VD,
Samuel Antao6890b092016-07-28 14:25:09 +0000535 OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
536 OpenMPClauseKind WhereFoundClauseKind) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000537 assert(!isStackEmpty() &&
Samuel Antao90927002016-04-26 14:54:23 +0000538 "Not expecting to retrieve components from a empty stack!");
Alexey Bataev4b465392017-04-26 15:06:24 +0000539 auto &MEC = Stack.back().first.back().MappedExprComponents[VD];
Samuel Antao90927002016-04-26 14:54:23 +0000540 // Create new entry and append the new components there.
Samuel Antao6890b092016-07-28 14:25:09 +0000541 MEC.Components.resize(MEC.Components.size() + 1);
542 MEC.Components.back().append(Components.begin(), Components.end());
543 MEC.Kind = WhereFoundClauseKind;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000544 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000545
546 unsigned getNestingLevel() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000547 assert(!isStackEmpty());
548 return Stack.back().first.size() - 1;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000549 }
Alexey Bataev8b427062016-05-25 12:36:08 +0000550 void addDoacrossDependClause(OMPDependClause *C, OperatorOffsetTy &OpsOffs) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000551 assert(!isStackEmpty() && Stack.back().first.size() > 1);
552 auto &StackElem = *std::next(Stack.back().first.rbegin());
553 assert(isOpenMPWorksharingDirective(StackElem.Directive));
554 StackElem.DoacrossDepends.insert({C, OpsOffs});
Alexey Bataev8b427062016-05-25 12:36:08 +0000555 }
556 llvm::iterator_range<DoacrossDependMapTy::const_iterator>
557 getDoacrossDependClauses() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000558 assert(!isStackEmpty());
559 auto &StackElem = Stack.back().first.back();
560 if (isOpenMPWorksharingDirective(StackElem.Directive)) {
561 auto &Ref = StackElem.DoacrossDepends;
Alexey Bataev8b427062016-05-25 12:36:08 +0000562 return llvm::make_range(Ref.begin(), Ref.end());
563 }
Alexey Bataev4b465392017-04-26 15:06:24 +0000564 return llvm::make_range(StackElem.DoacrossDepends.end(),
565 StackElem.DoacrossDepends.end());
Alexey Bataev8b427062016-05-25 12:36:08 +0000566 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000567};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000568bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
Alexey Bataev35aaee62016-04-13 13:36:48 +0000569 return isOpenMPParallelDirective(DKind) || isOpenMPTaskingDirective(DKind) ||
570 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000571}
Alexey Bataeved09d242014-05-28 05:53:51 +0000572} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000573
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000574static Expr *getExprAsWritten(Expr *E) {
575 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
576 E = ExprTemp->getSubExpr();
577
578 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
579 E = MTE->GetTemporaryExpr();
580
581 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
582 E = Binder->getSubExpr();
583
584 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
585 E = ICE->getSubExprAsWritten();
586 return E->IgnoreParens();
587}
588
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000589static ValueDecl *getCanonicalDecl(ValueDecl *D) {
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000590 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(D))
591 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
592 D = ME->getMemberDecl();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000593 auto *VD = dyn_cast<VarDecl>(D);
594 auto *FD = dyn_cast<FieldDecl>(D);
David Majnemer9d168222016-08-05 17:44:54 +0000595 if (VD != nullptr) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000596 VD = VD->getCanonicalDecl();
597 D = VD;
598 } else {
599 assert(FD);
600 FD = FD->getCanonicalDecl();
601 D = FD;
602 }
603 return D;
604}
605
David Majnemer9d168222016-08-05 17:44:54 +0000606DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator &Iter,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000607 ValueDecl *D) {
608 D = getCanonicalDecl(D);
609 auto *VD = dyn_cast<VarDecl>(D);
610 auto *FD = dyn_cast<FieldDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000611 DSAVarData DVar;
Alexey Bataev4b465392017-04-26 15:06:24 +0000612 if (isStackEmpty() || Iter == Stack.back().first.rend()) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000613 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
614 // in a region but not in construct]
615 // File-scope or namespace-scope variables referenced in called routines
616 // in the region are shared unless they appear in a threadprivate
617 // directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000618 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000619 DVar.CKind = OMPC_shared;
620
621 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
622 // in a region but not in construct]
623 // Variables with static storage duration that are declared in called
624 // routines in the region are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000625 if (VD && VD->hasGlobalStorage())
626 DVar.CKind = OMPC_shared;
627
628 // Non-static data members are shared by default.
629 if (FD)
Alexey Bataev750a58b2014-03-18 12:19:12 +0000630 DVar.CKind = OMPC_shared;
631
Alexey Bataev758e55e2013-09-06 18:03:48 +0000632 return DVar;
633 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000634
Alexey Bataevec3da872014-01-31 05:15:34 +0000635 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
636 // in a Construct, C/C++, predetermined, p.1]
637 // Variables with automatic storage duration that are declared in a scope
638 // inside the construct are private.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000639 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
640 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000641 DVar.CKind = OMPC_private;
642 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000643 }
644
Alexey Bataeveffbdf12017-07-21 17:24:30 +0000645 DVar.DKind = Iter->Directive;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000646 // Explicitly specified attributes and local variables with predetermined
647 // attributes.
648 if (Iter->SharingMap.count(D)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000649 DVar.RefExpr = Iter->SharingMap[D].RefExpr.getPointer();
Alexey Bataev90c228f2016-02-08 09:29:13 +0000650 DVar.PrivateCopy = Iter->SharingMap[D].PrivateCopy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000651 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000652 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000653 return DVar;
654 }
655
656 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
657 // in a Construct, C/C++, implicitly determined, p.1]
658 // In a parallel or task construct, the data-sharing attributes of these
659 // variables are determined by the default clause, if present.
660 switch (Iter->DefaultAttr) {
661 case DSA_shared:
662 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000663 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000664 return DVar;
665 case DSA_none:
666 return DVar;
667 case DSA_unspecified:
668 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
669 // in a Construct, implicitly determined, p.2]
670 // In a parallel construct, if no default clause is present, these
671 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000672 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000673 if (isOpenMPParallelDirective(DVar.DKind) ||
674 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000675 DVar.CKind = OMPC_shared;
676 return DVar;
677 }
678
679 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
680 // in a Construct, implicitly determined, p.4]
681 // In a task construct, if no default clause is present, a variable that in
682 // the enclosing context is determined to be shared by all implicit tasks
683 // bound to the current team is shared.
Alexey Bataev35aaee62016-04-13 13:36:48 +0000684 if (isOpenMPTaskingDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000685 DSAVarData DVarTemp;
Alexey Bataev4b465392017-04-26 15:06:24 +0000686 auto I = Iter, E = Stack.back().first.rend();
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000687 do {
688 ++I;
Alexey Bataeved09d242014-05-28 05:53:51 +0000689 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
Alexey Bataev35aaee62016-04-13 13:36:48 +0000690 // Referenced in a Construct, implicitly determined, p.6]
Alexey Bataev758e55e2013-09-06 18:03:48 +0000691 // In a task construct, if no default clause is present, a variable
692 // whose data-sharing attribute is not determined by the rules above is
693 // firstprivate.
694 DVarTemp = getDSA(I, D);
695 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000696 DVar.RefExpr = nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000697 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000698 return DVar;
699 }
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000700 } while (I != E && !isParallelOrTaskRegion(I->Directive));
Alexey Bataev758e55e2013-09-06 18:03:48 +0000701 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000702 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000703 return DVar;
704 }
705 }
706 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
707 // in a Construct, implicitly determined, p.3]
708 // For constructs other than task, if no default clause is present, these
709 // variables inherit their data-sharing attributes from the enclosing
710 // context.
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000711 return getDSA(++Iter, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000712}
713
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000714Expr *DSAStackTy::addUniqueAligned(ValueDecl *D, Expr *NewDE) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000715 assert(!isStackEmpty() && "Data sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000716 D = getCanonicalDecl(D);
Alexey Bataev4b465392017-04-26 15:06:24 +0000717 auto &StackElem = Stack.back().first.back();
718 auto It = StackElem.AlignedMap.find(D);
719 if (It == StackElem.AlignedMap.end()) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000720 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
Alexey Bataev4b465392017-04-26 15:06:24 +0000721 StackElem.AlignedMap[D] = NewDE;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000722 return nullptr;
723 } else {
724 assert(It->second && "Unexpected nullptr expr in the aligned map");
725 return It->second;
726 }
727 return nullptr;
728}
729
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000730void DSAStackTy::addLoopControlVariable(ValueDecl *D, VarDecl *Capture) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000731 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000732 D = getCanonicalDecl(D);
Alexey Bataev4b465392017-04-26 15:06:24 +0000733 auto &StackElem = Stack.back().first.back();
734 StackElem.LCVMap.insert(
735 {D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture)});
Alexey Bataev9c821032015-04-30 04:23:23 +0000736}
737
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000738DSAStackTy::LCDeclInfo DSAStackTy::isLoopControlVariable(ValueDecl *D) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000739 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000740 D = getCanonicalDecl(D);
Alexey Bataev4b465392017-04-26 15:06:24 +0000741 auto &StackElem = Stack.back().first.back();
742 auto It = StackElem.LCVMap.find(D);
743 if (It != StackElem.LCVMap.end())
744 return It->second;
745 return {0, nullptr};
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000746}
747
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000748DSAStackTy::LCDeclInfo DSAStackTy::isParentLoopControlVariable(ValueDecl *D) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000749 assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
750 "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000751 D = getCanonicalDecl(D);
Alexey Bataev4b465392017-04-26 15:06:24 +0000752 auto &StackElem = *std::next(Stack.back().first.rbegin());
753 auto It = StackElem.LCVMap.find(D);
754 if (It != StackElem.LCVMap.end())
755 return It->second;
756 return {0, nullptr};
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000757}
758
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000759ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000760 assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
761 "Data-sharing attributes stack is empty");
762 auto &StackElem = *std::next(Stack.back().first.rbegin());
763 if (StackElem.LCVMap.size() < I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000764 return nullptr;
Alexey Bataev4b465392017-04-26 15:06:24 +0000765 for (auto &Pair : StackElem.LCVMap)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000766 if (Pair.second.first == I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000767 return Pair.first;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000768 return nullptr;
Alexey Bataev9c821032015-04-30 04:23:23 +0000769}
770
Alexey Bataev90c228f2016-02-08 09:29:13 +0000771void DSAStackTy::addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
772 DeclRefExpr *PrivateCopy) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000773 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000774 if (A == OMPC_threadprivate) {
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000775 auto &Data = Threadprivates[D];
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000776 Data.Attributes = A;
777 Data.RefExpr.setPointer(E);
778 Data.PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000779 } else {
Alexey Bataev4b465392017-04-26 15:06:24 +0000780 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
781 auto &Data = Stack.back().first.back().SharingMap[D];
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000782 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
783 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
784 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
785 (isLoopControlVariable(D).first && A == OMPC_private));
786 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
787 Data.RefExpr.setInt(/*IntVal=*/true);
788 return;
789 }
790 const bool IsLastprivate =
791 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
792 Data.Attributes = A;
793 Data.RefExpr.setPointerAndInt(E, IsLastprivate);
794 Data.PrivateCopy = PrivateCopy;
795 if (PrivateCopy) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000796 auto &Data = Stack.back().first.back().SharingMap[PrivateCopy->getDecl()];
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000797 Data.Attributes = A;
798 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
799 Data.PrivateCopy = nullptr;
800 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000801 }
802}
803
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000804/// \brief Build a variable declaration for OpenMP loop iteration variable.
805static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
806 StringRef Name, const AttrVec *Attrs = nullptr) {
807 DeclContext *DC = SemaRef.CurContext;
808 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
809 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
810 VarDecl *Decl =
811 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
812 if (Attrs) {
813 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
814 I != E; ++I)
815 Decl->addAttr(*I);
816 }
817 Decl->setImplicit();
818 return Decl;
819}
820
821static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
822 SourceLocation Loc,
823 bool RefersToCapture = false) {
824 D->setReferenced();
825 D->markUsed(S.Context);
826 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
827 SourceLocation(), D, RefersToCapture, Loc, Ty,
828 VK_LValue);
829}
830
831void DSAStackTy::addTaskgroupReductionData(ValueDecl *D, SourceRange SR,
832 BinaryOperatorKind BOK) {
Alexey Bataevfa312f32017-07-21 18:48:21 +0000833 D = getCanonicalDecl(D);
834 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataevfa312f32017-07-21 18:48:21 +0000835 assert(
Richard Trieu09f14112017-07-21 21:29:35 +0000836 Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000837 "Additional reduction info may be specified only for reduction items.");
838 auto &ReductionData = Stack.back().first.back().ReductionMap[D];
839 assert(ReductionData.ReductionRange.isInvalid() &&
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000840 Stack.back().first.back().Directive == OMPD_taskgroup &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000841 "Additional reduction info may be specified only once for reduction "
842 "items.");
843 ReductionData.set(BOK, SR);
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000844 Expr *&TaskgroupReductionRef =
845 Stack.back().first.back().TaskgroupReductionRef;
846 if (!TaskgroupReductionRef) {
Alexey Bataevd070a582017-10-25 15:54:04 +0000847 auto *VD = buildVarDecl(SemaRef, SR.getBegin(),
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000848 SemaRef.Context.VoidPtrTy, ".task_red.");
Alexey Bataevd070a582017-10-25 15:54:04 +0000849 TaskgroupReductionRef =
850 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000851 }
Alexey Bataevfa312f32017-07-21 18:48:21 +0000852}
853
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000854void DSAStackTy::addTaskgroupReductionData(ValueDecl *D, SourceRange SR,
855 const Expr *ReductionRef) {
Alexey Bataevfa312f32017-07-21 18:48:21 +0000856 D = getCanonicalDecl(D);
857 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataevfa312f32017-07-21 18:48:21 +0000858 assert(
Richard Trieu09f14112017-07-21 21:29:35 +0000859 Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000860 "Additional reduction info may be specified only for reduction items.");
861 auto &ReductionData = Stack.back().first.back().ReductionMap[D];
862 assert(ReductionData.ReductionRange.isInvalid() &&
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000863 Stack.back().first.back().Directive == OMPD_taskgroup &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000864 "Additional reduction info may be specified only once for reduction "
865 "items.");
866 ReductionData.set(ReductionRef, SR);
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000867 Expr *&TaskgroupReductionRef =
868 Stack.back().first.back().TaskgroupReductionRef;
869 if (!TaskgroupReductionRef) {
Alexey Bataevd070a582017-10-25 15:54:04 +0000870 auto *VD = buildVarDecl(SemaRef, SR.getBegin(), SemaRef.Context.VoidPtrTy,
871 ".task_red.");
872 TaskgroupReductionRef =
873 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000874 }
Alexey Bataevfa312f32017-07-21 18:48:21 +0000875}
876
Alexey Bataevf189cb72017-07-24 14:52:13 +0000877DSAStackTy::DSAVarData
878DSAStackTy::getTopMostTaskgroupReductionData(ValueDecl *D, SourceRange &SR,
Alexey Bataev88202be2017-07-27 13:20:36 +0000879 BinaryOperatorKind &BOK,
880 Expr *&TaskgroupDescriptor) {
Alexey Bataevfa312f32017-07-21 18:48:21 +0000881 D = getCanonicalDecl(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +0000882 assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
883 if (Stack.back().first.empty())
884 return DSAVarData();
885 for (auto I = std::next(Stack.back().first.rbegin(), 1),
Alexey Bataevfa312f32017-07-21 18:48:21 +0000886 E = Stack.back().first.rend();
887 I != E; std::advance(I, 1)) {
888 auto &Data = I->SharingMap[D];
Alexey Bataevf189cb72017-07-24 14:52:13 +0000889 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
Alexey Bataevfa312f32017-07-21 18:48:21 +0000890 continue;
891 auto &ReductionData = I->ReductionMap[D];
892 if (!ReductionData.ReductionOp ||
893 ReductionData.ReductionOp.is<const Expr *>())
Alexey Bataevf189cb72017-07-24 14:52:13 +0000894 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +0000895 SR = ReductionData.ReductionRange;
Alexey Bataevf87fa882017-07-21 19:26:22 +0000896 BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>();
Alexey Bataev88202be2017-07-27 13:20:36 +0000897 assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
898 "expression for the descriptor is not "
899 "set.");
900 TaskgroupDescriptor = I->TaskgroupReductionRef;
Alexey Bataevf189cb72017-07-24 14:52:13 +0000901 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
902 Data.PrivateCopy, I->DefaultAttrLoc);
Alexey Bataevfa312f32017-07-21 18:48:21 +0000903 }
Alexey Bataevf189cb72017-07-24 14:52:13 +0000904 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +0000905}
906
Alexey Bataevf189cb72017-07-24 14:52:13 +0000907DSAStackTy::DSAVarData
908DSAStackTy::getTopMostTaskgroupReductionData(ValueDecl *D, SourceRange &SR,
Alexey Bataev88202be2017-07-27 13:20:36 +0000909 const Expr *&ReductionRef,
910 Expr *&TaskgroupDescriptor) {
Alexey Bataevfa312f32017-07-21 18:48:21 +0000911 D = getCanonicalDecl(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +0000912 assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
913 if (Stack.back().first.empty())
914 return DSAVarData();
915 for (auto I = std::next(Stack.back().first.rbegin(), 1),
Alexey Bataevfa312f32017-07-21 18:48:21 +0000916 E = Stack.back().first.rend();
917 I != E; std::advance(I, 1)) {
918 auto &Data = I->SharingMap[D];
Alexey Bataevf189cb72017-07-24 14:52:13 +0000919 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
Alexey Bataevfa312f32017-07-21 18:48:21 +0000920 continue;
921 auto &ReductionData = I->ReductionMap[D];
922 if (!ReductionData.ReductionOp ||
923 !ReductionData.ReductionOp.is<const Expr *>())
Alexey Bataevf189cb72017-07-24 14:52:13 +0000924 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +0000925 SR = ReductionData.ReductionRange;
926 ReductionRef = ReductionData.ReductionOp.get<const Expr *>();
Alexey Bataev88202be2017-07-27 13:20:36 +0000927 assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
928 "expression for the descriptor is not "
929 "set.");
930 TaskgroupDescriptor = I->TaskgroupReductionRef;
Alexey Bataevf189cb72017-07-24 14:52:13 +0000931 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
932 Data.PrivateCopy, I->DefaultAttrLoc);
Alexey Bataevfa312f32017-07-21 18:48:21 +0000933 }
Alexey Bataevf189cb72017-07-24 14:52:13 +0000934 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +0000935}
936
Alexey Bataeved09d242014-05-28 05:53:51 +0000937bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000938 D = D->getCanonicalDecl();
Alexey Bataev4b465392017-04-26 15:06:24 +0000939 if (!isStackEmpty() && Stack.back().first.size() > 1) {
940 reverse_iterator I = Iter, E = Stack.back().first.rend();
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000941 Scope *TopScope = nullptr;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000942 while (I != E && !isParallelOrTaskRegion(I->Directive))
Alexey Bataevec3da872014-01-31 05:15:34 +0000943 ++I;
Alexey Bataeved09d242014-05-28 05:53:51 +0000944 if (I == E)
945 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000946 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000947 Scope *CurScope = getCurScope();
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000948 while (CurScope != TopScope && !CurScope->isDeclScope(D))
Alexey Bataev758e55e2013-09-06 18:03:48 +0000949 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000950 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000951 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000952 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000953}
954
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000955DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, bool FromParent) {
956 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000957 DSAVarData DVar;
958
959 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
960 // in a Construct, C/C++, predetermined, p.1]
961 // Variables appearing in threadprivate directives are threadprivate.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000962 auto *VD = dyn_cast<VarDecl>(D);
963 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
964 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
Samuel Antaof8b50122015-07-13 22:54:53 +0000965 SemaRef.getLangOpts().OpenMPUseTLS &&
966 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000967 (VD && VD->getStorageClass() == SC_Register &&
968 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
969 addDSA(D, buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
Alexey Bataev39f915b82015-05-08 10:41:21 +0000970 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000971 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000972 }
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000973 auto TI = Threadprivates.find(D);
974 if (TI != Threadprivates.end()) {
975 DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000976 DVar.CKind = OMPC_threadprivate;
977 return DVar;
Alexey Bataev817d7f32017-11-14 21:01:01 +0000978 } else if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
979 DVar.RefExpr = buildDeclRefExpr(
980 SemaRef, VD, D->getType().getNonReferenceType(),
981 VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation());
982 DVar.CKind = OMPC_threadprivate;
983 addDSA(D, DVar.RefExpr, OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000984 }
985
Alexey Bataev4b465392017-04-26 15:06:24 +0000986 if (isStackEmpty())
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000987 // Not in OpenMP execution region and top scope was already checked.
988 return DVar;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000989
Alexey Bataev758e55e2013-09-06 18:03:48 +0000990 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000991 // in a Construct, C/C++, predetermined, p.4]
992 // Static data members are shared.
993 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
994 // in a Construct, C/C++, predetermined, p.7]
995 // Variables with static storage duration that are declared in a scope
996 // inside the construct are shared.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000997 auto &&MatchesAlways = [](OpenMPDirectiveKind) -> bool { return true; };
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000998 if (VD && VD->isStaticDataMember()) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000999 DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent);
Alexey Bataevdffa93a2015-12-10 08:20:58 +00001000 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +00001001 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001002
Alexey Bataevdffa93a2015-12-10 08:20:58 +00001003 DVar.CKind = OMPC_shared;
1004 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001005 }
1006
1007 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00001008 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
1009 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001010 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1011 // in a Construct, C/C++, predetermined, p.6]
1012 // Variables with const qualified type having no mutable member are
1013 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001014 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +00001015 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00001016 if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
1017 if (auto *CTD = CTSD->getSpecializedTemplate())
1018 RD = CTD->getTemplatedDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001019 if (IsConstant &&
Alexey Bataev4bcad7f2016-02-10 10:50:12 +00001020 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
1021 RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001022 // Variables with const-qualified type having no mutable member may be
1023 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001024 DSAVarData DVarTemp = hasDSA(
1025 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_firstprivate; },
1026 MatchesAlways, FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001027 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
1028 return DVar;
1029
Alexey Bataev758e55e2013-09-06 18:03:48 +00001030 DVar.CKind = OMPC_shared;
1031 return DVar;
1032 }
1033
Alexey Bataev758e55e2013-09-06 18:03:48 +00001034 // Explicitly specified attributes and local variables with predetermined
1035 // attributes.
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001036 auto I = Stack.back().first.rbegin();
Alexey Bataev4b465392017-04-26 15:06:24 +00001037 auto EndI = Stack.back().first.rend();
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001038 if (FromParent && I != EndI)
1039 std::advance(I, 1);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001040 if (I->SharingMap.count(D)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001041 DVar.RefExpr = I->SharingMap[D].RefExpr.getPointer();
Alexey Bataev90c228f2016-02-08 09:29:13 +00001042 DVar.PrivateCopy = I->SharingMap[D].PrivateCopy;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001043 DVar.CKind = I->SharingMap[D].Attributes;
1044 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev4d4624c2017-07-20 16:47:47 +00001045 DVar.DKind = I->Directive;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001046 }
1047
1048 return DVar;
1049}
1050
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001051DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1052 bool FromParent) {
Alexey Bataev4b465392017-04-26 15:06:24 +00001053 if (isStackEmpty()) {
1054 StackTy::reverse_iterator I;
1055 return getDSA(I, D);
1056 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001057 D = getCanonicalDecl(D);
Alexey Bataev4b465392017-04-26 15:06:24 +00001058 auto StartI = Stack.back().first.rbegin();
1059 auto EndI = Stack.back().first.rend();
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001060 if (FromParent && StartI != EndI)
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001061 std::advance(StartI, 1);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001062 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001063}
1064
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001065DSAStackTy::DSAVarData
1066DSAStackTy::hasDSA(ValueDecl *D,
1067 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
1068 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
1069 bool FromParent) {
Alexey Bataev4b465392017-04-26 15:06:24 +00001070 if (isStackEmpty())
1071 return {};
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001072 D = getCanonicalDecl(D);
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001073 auto I = Stack.back().first.rbegin();
Alexey Bataev4b465392017-04-26 15:06:24 +00001074 auto EndI = Stack.back().first.rend();
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001075 if (FromParent && I != EndI)
Alexey Bataev0e6fc1c2017-04-27 14:46:26 +00001076 std::advance(I, 1);
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001077 for (; I != EndI; std::advance(I, 1)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001078 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +00001079 continue;
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001080 auto NewI = I;
1081 DSAVarData DVar = getDSA(NewI, D);
1082 if (I == NewI && CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001083 return DVar;
Alexey Bataev60859c02017-04-27 15:10:33 +00001084 }
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001085 return {};
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001086}
1087
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001088DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
1089 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
1090 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
1091 bool FromParent) {
Alexey Bataev4b465392017-04-26 15:06:24 +00001092 if (isStackEmpty())
1093 return {};
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001094 D = getCanonicalDecl(D);
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001095 auto StartI = Stack.back().first.rbegin();
Alexey Bataev4b465392017-04-26 15:06:24 +00001096 auto EndI = Stack.back().first.rend();
Alexey Bataeve3978122016-07-19 05:06:39 +00001097 if (FromParent && StartI != EndI)
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001098 std::advance(StartI, 1);
Alexey Bataeve3978122016-07-19 05:06:39 +00001099 if (StartI == EndI || !DPred(StartI->Directive))
Alexey Bataev4b465392017-04-26 15:06:24 +00001100 return {};
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001101 auto NewI = StartI;
1102 DSAVarData DVar = getDSA(NewI, D);
1103 return (NewI == StartI && CPred(DVar.CKind)) ? DVar : DSAVarData();
Alexey Bataevc5e02582014-06-16 07:08:35 +00001104}
1105
Alexey Bataevaac108a2015-06-23 04:51:00 +00001106bool DSAStackTy::hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001107 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001108 unsigned Level, bool NotLastprivate) {
Alexey Bataev4b465392017-04-26 15:06:24 +00001109 if (isStackEmpty())
1110 return false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001111 D = getCanonicalDecl(D);
Alexey Bataev4b465392017-04-26 15:06:24 +00001112 auto StartI = Stack.back().first.begin();
1113 auto EndI = Stack.back().first.end();
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +00001114 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +00001115 return false;
1116 std::advance(StartI, Level);
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001117 return (StartI->SharingMap.count(D) > 0) &&
1118 StartI->SharingMap[D].RefExpr.getPointer() &&
1119 CPred(StartI->SharingMap[D].Attributes) &&
1120 (!NotLastprivate || !StartI->SharingMap[D].RefExpr.getInt());
Alexey Bataevaac108a2015-06-23 04:51:00 +00001121}
1122
Samuel Antao4be30e92015-10-02 17:14:03 +00001123bool DSAStackTy::hasExplicitDirective(
1124 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
1125 unsigned Level) {
Alexey Bataev4b465392017-04-26 15:06:24 +00001126 if (isStackEmpty())
1127 return false;
1128 auto StartI = Stack.back().first.begin();
1129 auto EndI = Stack.back().first.end();
Samuel Antao4be30e92015-10-02 17:14:03 +00001130 if (std::distance(StartI, EndI) <= (int)Level)
1131 return false;
1132 std::advance(StartI, Level);
1133 return DPred(StartI->Directive);
1134}
1135
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001136bool DSAStackTy::hasDirective(
1137 const llvm::function_ref<bool(OpenMPDirectiveKind,
1138 const DeclarationNameInfo &, SourceLocation)>
1139 &DPred,
1140 bool FromParent) {
Samuel Antaof0d79752016-05-27 15:21:27 +00001141 // We look only in the enclosing region.
Alexey Bataev4b465392017-04-26 15:06:24 +00001142 if (isStackEmpty())
Samuel Antaof0d79752016-05-27 15:21:27 +00001143 return false;
Alexey Bataev4b465392017-04-26 15:06:24 +00001144 auto StartI = std::next(Stack.back().first.rbegin());
1145 auto EndI = Stack.back().first.rend();
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001146 if (FromParent && StartI != EndI)
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001147 StartI = std::next(StartI);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001148 for (auto I = StartI, EE = EndI; I != EE; ++I) {
1149 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
1150 return true;
1151 }
1152 return false;
1153}
1154
Alexey Bataev758e55e2013-09-06 18:03:48 +00001155void Sema::InitDataSharingAttributesStack() {
1156 VarDataSharingAttributesStack = new DSAStackTy(*this);
1157}
1158
1159#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
1160
Alexey Bataev4b465392017-04-26 15:06:24 +00001161void Sema::pushOpenMPFunctionRegion() {
1162 DSAStack->pushFunction();
1163}
1164
1165void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) {
1166 DSAStack->popFunction(OldFSI);
1167}
1168
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001169bool Sema::IsOpenMPCapturedByRef(ValueDecl *D, unsigned Level) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001170 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1171
1172 auto &Ctx = getASTContext();
1173 bool IsByRef = true;
1174
1175 // Find the directive that is associated with the provided scope.
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00001176 D = cast<ValueDecl>(D->getCanonicalDecl());
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001177 auto Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001178
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001179 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001180 // This table summarizes how a given variable should be passed to the device
1181 // given its type and the clauses where it appears. This table is based on
1182 // the description in OpenMP 4.5 [2.10.4, target Construct] and
1183 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
1184 //
1185 // =========================================================================
1186 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
1187 // | |(tofrom:scalar)| | pvt | | | |
1188 // =========================================================================
1189 // | scl | | | | - | | bycopy|
1190 // | scl | | - | x | - | - | bycopy|
1191 // | scl | | x | - | - | - | null |
1192 // | scl | x | | | - | | byref |
1193 // | scl | x | - | x | - | - | bycopy|
1194 // | scl | x | x | - | - | - | null |
1195 // | scl | | - | - | - | x | byref |
1196 // | scl | x | - | - | - | x | byref |
1197 //
1198 // | agg | n.a. | | | - | | byref |
1199 // | agg | n.a. | - | x | - | - | byref |
1200 // | agg | n.a. | x | - | - | - | null |
1201 // | agg | n.a. | - | - | - | x | byref |
1202 // | agg | n.a. | - | - | - | x[] | byref |
1203 //
1204 // | ptr | n.a. | | | - | | bycopy|
1205 // | ptr | n.a. | - | x | - | - | bycopy|
1206 // | ptr | n.a. | x | - | - | - | null |
1207 // | ptr | n.a. | - | - | - | x | byref |
1208 // | ptr | n.a. | - | - | - | x[] | bycopy|
1209 // | ptr | n.a. | - | - | x | | bycopy|
1210 // | ptr | n.a. | - | - | x | x | bycopy|
1211 // | ptr | n.a. | - | - | x | x[] | bycopy|
1212 // =========================================================================
1213 // Legend:
1214 // scl - scalar
1215 // ptr - pointer
1216 // agg - aggregate
1217 // x - applies
1218 // - - invalid in this combination
1219 // [] - mapped with an array section
1220 // byref - should be mapped by reference
1221 // byval - should be mapped by value
1222 // null - initialize a local variable to null on the device
1223 //
1224 // Observations:
1225 // - All scalar declarations that show up in a map clause have to be passed
1226 // by reference, because they may have been mapped in the enclosing data
1227 // environment.
1228 // - If the scalar value does not fit the size of uintptr, it has to be
1229 // passed by reference, regardless the result in the table above.
1230 // - For pointers mapped by value that have either an implicit map or an
1231 // array section, the runtime library may pass the NULL value to the
1232 // device instead of the value passed to it by the compiler.
1233
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001234 if (Ty->isReferenceType())
1235 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
Samuel Antao86ace552016-04-27 22:40:57 +00001236
1237 // Locate map clauses and see if the variable being captured is referred to
1238 // in any of those clauses. Here we only care about variables, not fields,
1239 // because fields are part of aggregates.
1240 bool IsVariableUsedInMapClause = false;
1241 bool IsVariableAssociatedWithSection = false;
1242
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +00001243 DSAStack->checkMappableExprComponentListsForDeclAtLevel(
1244 D, Level, [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
Samuel Antao6890b092016-07-28 14:25:09 +00001245 MapExprComponents,
1246 OpenMPClauseKind WhereFoundClauseKind) {
1247 // Only the map clause information influences how a variable is
1248 // captured. E.g. is_device_ptr does not require changing the default
Samuel Antao4c8035b2016-12-12 18:00:20 +00001249 // behavior.
Samuel Antao6890b092016-07-28 14:25:09 +00001250 if (WhereFoundClauseKind != OMPC_map)
1251 return false;
Samuel Antao86ace552016-04-27 22:40:57 +00001252
1253 auto EI = MapExprComponents.rbegin();
1254 auto EE = MapExprComponents.rend();
1255
1256 assert(EI != EE && "Invalid map expression!");
1257
1258 if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
1259 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
1260
1261 ++EI;
1262 if (EI == EE)
1263 return false;
1264
1265 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
1266 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
1267 isa<MemberExpr>(EI->getAssociatedExpression())) {
1268 IsVariableAssociatedWithSection = true;
1269 // There is nothing more we need to know about this variable.
1270 return true;
1271 }
1272
1273 // Keep looking for more map info.
1274 return false;
1275 });
1276
1277 if (IsVariableUsedInMapClause) {
1278 // If variable is identified in a map clause it is always captured by
1279 // reference except if it is a pointer that is dereferenced somehow.
1280 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
1281 } else {
Alexey Bataev3f96fe62017-12-13 17:31:39 +00001282 // By default, all the data that has a scalar type is mapped by copy
1283 // (except for reduction variables).
1284 IsByRef =
1285 !Ty->isScalarType() ||
1286 DSAStack->getDefaultDMAAtLevel(Level) == DMA_tofrom_scalar ||
1287 DSAStack->hasExplicitDSA(
1288 D, [](OpenMPClauseKind K) { return K == OMPC_reduction; }, Level);
Samuel Antao86ace552016-04-27 22:40:57 +00001289 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001290 }
1291
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001292 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00001293 IsByRef =
1294 !DSAStack->hasExplicitDSA(
1295 D,
1296 [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
1297 Level, /*NotLastprivate=*/true) &&
1298 // If the variable is artificial and must be captured by value - try to
1299 // capture by value.
Alexey Bataevd2202ca2017-12-27 17:58:32 +00001300 !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() &&
1301 !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue());
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001302 }
1303
Samuel Antao86ace552016-04-27 22:40:57 +00001304 // When passing data by copy, we need to make sure it fits the uintptr size
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001305 // and alignment, because the runtime library only deals with uintptr types.
1306 // If it does not fit the uintptr size, we need to pass the data by reference
1307 // instead.
1308 if (!IsByRef &&
1309 (Ctx.getTypeSizeInChars(Ty) >
1310 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001311 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001312 IsByRef = true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001313 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001314
1315 return IsByRef;
1316}
1317
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001318unsigned Sema::getOpenMPNestingLevel() const {
1319 assert(getLangOpts().OpenMP);
1320 return DSAStack->getNestingLevel();
1321}
1322
Jonas Hahnfeld87d44262017-11-18 21:00:46 +00001323bool Sema::isInOpenMPTargetExecutionDirective() const {
1324 return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) &&
1325 !DSAStack->isClauseParsingMode()) ||
1326 DSAStack->hasDirective(
1327 [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
1328 SourceLocation) -> bool {
1329 return isOpenMPTargetExecutionDirective(K);
1330 },
1331 false);
1332}
1333
Alexey Bataev90c228f2016-02-08 09:29:13 +00001334VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +00001335 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001336 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +00001337
1338 // If we are attempting to capture a global variable in a directive with
1339 // 'target' we return true so that this global is also mapped to the device.
1340 //
1341 // FIXME: If the declaration is enclosed in a 'declare target' directive,
1342 // then it should not be captured. Therefore, an extra check has to be
1343 // inserted here once support for 'declare target' is added.
1344 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001345 auto *VD = dyn_cast<VarDecl>(D);
Jonas Hahnfeld87d44262017-11-18 21:00:46 +00001346 if (VD && !VD->hasLocalStorage() && isInOpenMPTargetExecutionDirective())
1347 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +00001348
Alexey Bataev48977c32015-08-04 08:10:48 +00001349 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
1350 (!DSAStack->isClauseParsingMode() ||
1351 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001352 auto &&Info = DSAStack->isLoopControlVariable(D);
1353 if (Info.first ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001354 (VD && VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001355 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001356 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001357 return VD ? VD : Info.second;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001358 auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001359 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +00001360 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001361 DVarPrivate = DSAStack->hasDSA(
1362 D, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
1363 DSAStack->isClauseParsingMode());
Alexey Bataev90c228f2016-02-08 09:29:13 +00001364 if (DVarPrivate.CKind != OMPC_unknown)
1365 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001366 }
Alexey Bataev90c228f2016-02-08 09:29:13 +00001367 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +00001368}
1369
Alexey Bataevdfa430f2017-12-08 15:03:50 +00001370void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex,
1371 unsigned Level) const {
1372 SmallVector<OpenMPDirectiveKind, 4> Regions;
1373 getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level));
1374 FunctionScopesIndex -= Regions.size();
1375}
1376
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001377bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) {
Alexey Bataevaac108a2015-06-23 04:51:00 +00001378 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1379 return DSAStack->hasExplicitDSA(
Alexey Bataev88202be2017-07-27 13:20:36 +00001380 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; },
1381 Level) ||
Alexey Bataev3f82cfc2017-12-13 15:28:44 +00001382 (DSAStack->isClauseParsingMode() &&
1383 DSAStack->getClauseParsingMode() == OMPC_private) ||
Alexey Bataev88202be2017-07-27 13:20:36 +00001384 // Consider taskgroup reduction descriptor variable a private to avoid
1385 // possible capture in the region.
1386 (DSAStack->hasExplicitDirective(
1387 [](OpenMPDirectiveKind K) { return K == OMPD_taskgroup; },
1388 Level) &&
1389 DSAStack->isTaskgroupReductionRef(D, Level));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001390}
1391
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001392void Sema::setOpenMPCaptureKind(FieldDecl *FD, ValueDecl *D, unsigned Level) {
1393 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1394 D = getCanonicalDecl(D);
1395 OpenMPClauseKind OMPC = OMPC_unknown;
1396 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) {
1397 const unsigned NewLevel = I - 1;
1398 if (DSAStack->hasExplicitDSA(D,
1399 [&OMPC](const OpenMPClauseKind K) {
1400 if (isOpenMPPrivate(K)) {
1401 OMPC = K;
1402 return true;
1403 }
1404 return false;
1405 },
1406 NewLevel))
1407 break;
1408 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel(
1409 D, NewLevel,
1410 [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
1411 OpenMPClauseKind) { return true; })) {
1412 OMPC = OMPC_map;
1413 break;
1414 }
1415 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1416 NewLevel)) {
1417 OMPC = OMPC_firstprivate;
1418 break;
1419 }
1420 }
1421 if (OMPC != OMPC_unknown)
1422 FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, OMPC));
1423}
1424
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001425bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) {
Samuel Antao4be30e92015-10-02 17:14:03 +00001426 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1427 // Return true if the current level is no longer enclosed in a target region.
1428
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001429 auto *VD = dyn_cast<VarDecl>(D);
1430 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00001431 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1432 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +00001433}
1434
Alexey Bataeved09d242014-05-28 05:53:51 +00001435void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001436
1437void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
1438 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001439 Scope *CurScope, SourceLocation Loc) {
1440 DSAStack->push(DKind, DirName, CurScope, Loc);
Faisal Valid143a0c2017-04-01 21:30:49 +00001441 PushExpressionEvaluationContext(
1442 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001443}
1444
Alexey Bataevaac108a2015-06-23 04:51:00 +00001445void Sema::StartOpenMPClause(OpenMPClauseKind K) {
1446 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001447}
1448
Alexey Bataevaac108a2015-06-23 04:51:00 +00001449void Sema::EndOpenMPClause() {
1450 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001451}
1452
Alexey Bataev758e55e2013-09-06 18:03:48 +00001453void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001454 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
1455 // A variable of class type (or array thereof) that appears in a lastprivate
1456 // clause requires an accessible, unambiguous default constructor for the
1457 // class type, unless the list item is also specified in a firstprivate
1458 // clause.
David Majnemer9d168222016-08-05 17:44:54 +00001459 if (auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001460 for (auto *C : D->clauses()) {
1461 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
1462 SmallVector<Expr *, 8> PrivateCopies;
1463 for (auto *DE : Clause->varlists()) {
1464 if (DE->isValueDependent() || DE->isTypeDependent()) {
1465 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001466 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +00001467 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00001468 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
Alexey Bataev005248a2016-02-25 05:25:57 +00001469 VarDecl *VD = cast<VarDecl>(DRE->getDecl());
1470 QualType Type = VD->getType().getNonReferenceType();
1471 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001472 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001473 // Generate helper private variable and initialize it with the
1474 // default value. The address of the original variable is replaced
1475 // by the address of the new private variable in CodeGen. This new
1476 // variable is not added to IdResolver, so the code in the OpenMP
1477 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001478 auto *VDPrivate = buildVarDecl(
1479 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev005248a2016-02-25 05:25:57 +00001480 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Richard Smith3beb7c62017-01-12 02:27:38 +00001481 ActOnUninitializedDecl(VDPrivate);
Alexey Bataev38e89532015-04-16 04:54:05 +00001482 if (VDPrivate->isInvalidDecl())
1483 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001484 PrivateCopies.push_back(buildDeclRefExpr(
1485 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001486 } else {
1487 // The variable is also a firstprivate, so initialization sequence
1488 // for private copy is generated already.
1489 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001490 }
1491 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001492 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001493 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001494 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001495 }
1496 }
1497 }
1498
Alexey Bataev758e55e2013-09-06 18:03:48 +00001499 DSAStack->pop();
1500 DiscardCleanupsInEvaluationContext();
1501 PopExpressionEvaluationContext();
1502}
1503
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001504static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1505 Expr *NumIterations, Sema &SemaRef,
1506 Scope *S, DSAStackTy *Stack);
Alexander Musman3276a272015-03-21 10:12:56 +00001507
Alexey Bataeva769e072013-03-22 06:34:35 +00001508namespace {
1509
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001510class VarDeclFilterCCC : public CorrectionCandidateCallback {
1511private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001512 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001513
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001514public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001515 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001516 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001517 NamedDecl *ND = Candidate.getCorrectionDecl();
David Majnemer9d168222016-08-05 17:44:54 +00001518 if (auto *VD = dyn_cast_or_null<VarDecl>(ND)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001519 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001520 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1521 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001522 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001523 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001524 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001525};
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001526
1527class VarOrFuncDeclFilterCCC : public CorrectionCandidateCallback {
1528private:
1529 Sema &SemaRef;
1530
1531public:
1532 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
1533 bool ValidateCandidate(const TypoCorrection &Candidate) override {
1534 NamedDecl *ND = Candidate.getCorrectionDecl();
Kelvin Li59e3d192017-11-30 18:52:06 +00001535 if (ND && (isa<VarDecl>(ND) || isa<FunctionDecl>(ND))) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001536 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1537 SemaRef.getCurScope());
1538 }
1539 return false;
1540 }
1541};
1542
Alexey Bataeved09d242014-05-28 05:53:51 +00001543} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001544
1545ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1546 CXXScopeSpec &ScopeSpec,
1547 const DeclarationNameInfo &Id) {
1548 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1549 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1550
1551 if (Lookup.isAmbiguous())
1552 return ExprError();
1553
1554 VarDecl *VD;
1555 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001556 if (TypoCorrection Corrected = CorrectTypo(
1557 Id, LookupOrdinaryName, CurScope, nullptr,
1558 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001559 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001560 PDiag(Lookup.empty()
1561 ? diag::err_undeclared_var_use_suggest
1562 : diag::err_omp_expected_var_arg_suggest)
1563 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001564 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001565 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001566 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1567 : diag::err_omp_expected_var_arg)
1568 << Id.getName();
1569 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001570 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001571 } else {
1572 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001573 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001574 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1575 return ExprError();
1576 }
1577 }
1578 Lookup.suppressDiagnostics();
1579
1580 // OpenMP [2.9.2, Syntax, C/C++]
1581 // Variables must be file-scope, namespace-scope, or static block-scope.
1582 if (!VD->hasGlobalStorage()) {
1583 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001584 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1585 bool IsDecl =
1586 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001587 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001588 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1589 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001590 return ExprError();
1591 }
1592
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001593 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1594 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001595 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1596 // A threadprivate directive for file-scope variables must appear outside
1597 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001598 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1599 !getCurLexicalContext()->isTranslationUnit()) {
1600 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001601 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1602 bool IsDecl =
1603 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1604 Diag(VD->getLocation(),
1605 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1606 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001607 return ExprError();
1608 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001609 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1610 // A threadprivate directive for static class member variables must appear
1611 // in the class definition, in the same scope in which the member
1612 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001613 if (CanonicalVD->isStaticDataMember() &&
1614 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1615 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001616 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1617 bool IsDecl =
1618 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1619 Diag(VD->getLocation(),
1620 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1621 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001622 return ExprError();
1623 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001624 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1625 // A threadprivate directive for namespace-scope variables must appear
1626 // outside any definition or declaration other than the namespace
1627 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001628 if (CanonicalVD->getDeclContext()->isNamespace() &&
1629 (!getCurLexicalContext()->isFileContext() ||
1630 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1631 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001632 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1633 bool IsDecl =
1634 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1635 Diag(VD->getLocation(),
1636 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1637 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001638 return ExprError();
1639 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001640 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1641 // A threadprivate directive for static block-scope variables must appear
1642 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001643 if (CanonicalVD->isStaticLocal() && CurScope &&
1644 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001645 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001646 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1647 bool IsDecl =
1648 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1649 Diag(VD->getLocation(),
1650 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1651 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001652 return ExprError();
1653 }
1654
1655 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1656 // A threadprivate directive must lexically precede all references to any
1657 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001658 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001659 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001660 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001661 return ExprError();
1662 }
1663
1664 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00001665 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1666 SourceLocation(), VD,
1667 /*RefersToEnclosingVariableOrCapture=*/false,
1668 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001669}
1670
Alexey Bataeved09d242014-05-28 05:53:51 +00001671Sema::DeclGroupPtrTy
1672Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1673 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001674 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001675 CurContext->addDecl(D);
1676 return DeclGroupPtrTy::make(DeclGroupRef(D));
1677 }
David Blaikie0403cb12016-01-15 23:43:25 +00001678 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001679}
1680
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001681namespace {
1682class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1683 Sema &SemaRef;
1684
1685public:
1686 bool VisitDeclRefExpr(const DeclRefExpr *E) {
David Majnemer9d168222016-08-05 17:44:54 +00001687 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001688 if (VD->hasLocalStorage()) {
1689 SemaRef.Diag(E->getLocStart(),
1690 diag::err_omp_local_var_in_threadprivate_init)
1691 << E->getSourceRange();
1692 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1693 << VD << VD->getSourceRange();
1694 return true;
1695 }
1696 }
1697 return false;
1698 }
1699 bool VisitStmt(const Stmt *S) {
1700 for (auto Child : S->children()) {
1701 if (Child && Visit(Child))
1702 return true;
1703 }
1704 return false;
1705 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001706 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001707};
1708} // namespace
1709
Alexey Bataeved09d242014-05-28 05:53:51 +00001710OMPThreadPrivateDecl *
1711Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001712 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001713 for (auto &RefExpr : VarList) {
1714 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001715 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1716 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001717
Alexey Bataev376b4a42016-02-09 09:41:09 +00001718 // Mark variable as used.
1719 VD->setReferenced();
1720 VD->markUsed(Context);
1721
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001722 QualType QType = VD->getType();
1723 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1724 // It will be analyzed later.
1725 Vars.push_back(DE);
1726 continue;
1727 }
1728
Alexey Bataeva769e072013-03-22 06:34:35 +00001729 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1730 // A threadprivate variable must not have an incomplete type.
1731 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001732 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001733 continue;
1734 }
1735
1736 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1737 // A threadprivate variable must not have a reference type.
1738 if (VD->getType()->isReferenceType()) {
1739 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001740 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1741 bool IsDecl =
1742 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1743 Diag(VD->getLocation(),
1744 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1745 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001746 continue;
1747 }
1748
Samuel Antaof8b50122015-07-13 22:54:53 +00001749 // Check if this is a TLS variable. If TLS is not being supported, produce
1750 // the corresponding diagnostic.
1751 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1752 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1753 getLangOpts().OpenMPUseTLS &&
1754 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001755 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1756 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001757 Diag(ILoc, diag::err_omp_var_thread_local)
1758 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001759 bool IsDecl =
1760 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1761 Diag(VD->getLocation(),
1762 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1763 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001764 continue;
1765 }
1766
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001767 // Check if initial value of threadprivate variable reference variable with
1768 // local storage (it is not supported by runtime).
1769 if (auto Init = VD->getAnyInitializer()) {
1770 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001771 if (Checker.Visit(Init))
1772 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001773 }
1774
Alexey Bataeved09d242014-05-28 05:53:51 +00001775 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001776 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001777 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1778 Context, SourceRange(Loc, Loc)));
1779 if (auto *ML = Context.getASTMutationListener())
1780 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001781 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001782 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001783 if (!Vars.empty()) {
1784 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1785 Vars);
1786 D->setAccess(AS_public);
1787 }
1788 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001789}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001790
Alexey Bataev7ff55242014-06-19 09:13:45 +00001791static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001792 const ValueDecl *D, DSAStackTy::DSAVarData DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00001793 bool IsLoopIterVar = false) {
1794 if (DVar.RefExpr) {
1795 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1796 << getOpenMPClauseName(DVar.CKind);
1797 return;
1798 }
1799 enum {
1800 PDSA_StaticMemberShared,
1801 PDSA_StaticLocalVarShared,
1802 PDSA_LoopIterVarPrivate,
1803 PDSA_LoopIterVarLinear,
1804 PDSA_LoopIterVarLastprivate,
1805 PDSA_ConstVarShared,
1806 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001807 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001808 PDSA_LocalVarPrivate,
1809 PDSA_Implicit
1810 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001811 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001812 auto ReportLoc = D->getLocation();
1813 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001814 if (IsLoopIterVar) {
1815 if (DVar.CKind == OMPC_private)
1816 Reason = PDSA_LoopIterVarPrivate;
1817 else if (DVar.CKind == OMPC_lastprivate)
1818 Reason = PDSA_LoopIterVarLastprivate;
1819 else
1820 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev35aaee62016-04-13 13:36:48 +00001821 } else if (isOpenMPTaskingDirective(DVar.DKind) &&
1822 DVar.CKind == OMPC_firstprivate) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001823 Reason = PDSA_TaskVarFirstprivate;
1824 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001825 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001826 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001827 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001828 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001829 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001830 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001831 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00001832 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001833 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001834 ReportHint = true;
1835 Reason = PDSA_LocalVarPrivate;
1836 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001837 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001838 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001839 << Reason << ReportHint
1840 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1841 } else if (DVar.ImplicitDSALoc.isValid()) {
1842 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1843 << getOpenMPClauseName(DVar.CKind);
1844 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001845}
1846
Alexey Bataev758e55e2013-09-06 18:03:48 +00001847namespace {
1848class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1849 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001850 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001851 bool ErrorFound;
1852 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001853 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00001854 llvm::SmallVector<Expr *, 8> ImplicitMap;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001855 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00001856 llvm::DenseSet<ValueDecl *> ImplicitDeclarations;
Alexey Bataeved09d242014-05-28 05:53:51 +00001857
Alexey Bataev758e55e2013-09-06 18:03:48 +00001858public:
1859 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00001860 if (E->isTypeDependent() || E->isValueDependent() ||
1861 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1862 return;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001863 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00001864 VD = VD->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001865 // Skip internally declared variables.
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00001866 if (VD->hasLocalStorage() && !CS->capturesVariable(VD))
Alexey Bataeved09d242014-05-28 05:53:51 +00001867 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001868
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001869 auto DVar = Stack->getTopDSA(VD, false);
1870 // Check if the variable has explicit DSA set and stop analysis if it so.
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00001871 if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second)
David Majnemer9d168222016-08-05 17:44:54 +00001872 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001873
Alexey Bataevafe50572017-10-06 17:00:28 +00001874 // Skip internally declared static variables.
1875 if (VD->hasGlobalStorage() && !CS->capturesVariable(VD))
1876 return;
1877
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001878 auto ELoc = E->getExprLoc();
1879 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001880 // The default(none) clause requires that each variable that is referenced
1881 // in the construct, and does not have a predetermined data-sharing
1882 // attribute, must have its data-sharing attribute explicitly determined
1883 // by being listed in a data-sharing attribute clause.
1884 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001885 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001886 VarsWithInheritedDSA.count(VD) == 0) {
1887 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001888 return;
1889 }
1890
Alexey Bataevf47c4b42017-09-26 13:47:31 +00001891 if (isOpenMPTargetExecutionDirective(DKind) &&
1892 !Stack->isLoopControlVariable(VD).first) {
1893 if (!Stack->checkMappableExprComponentListsForDecl(
1894 VD, /*CurrentRegionOnly=*/true,
1895 [](OMPClauseMappableExprCommon::MappableExprComponentListRef
1896 StackComponents,
1897 OpenMPClauseKind) {
1898 // Variable is used if it has been marked as an array, array
1899 // section or the variable iself.
1900 return StackComponents.size() == 1 ||
1901 std::all_of(
1902 std::next(StackComponents.rbegin()),
1903 StackComponents.rend(),
1904 [](const OMPClauseMappableExprCommon::
1905 MappableComponent &MC) {
1906 return MC.getAssociatedDeclaration() ==
1907 nullptr &&
1908 (isa<OMPArraySectionExpr>(
1909 MC.getAssociatedExpression()) ||
1910 isa<ArraySubscriptExpr>(
1911 MC.getAssociatedExpression()));
1912 });
1913 })) {
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00001914 bool IsFirstprivate = false;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00001915 // By default lambdas are captured as firstprivates.
1916 if (const auto *RD =
1917 VD->getType().getNonReferenceType()->getAsCXXRecordDecl())
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00001918 IsFirstprivate = RD->isLambda();
1919 IsFirstprivate =
1920 IsFirstprivate ||
1921 (VD->getType().getNonReferenceType()->isScalarType() &&
1922 Stack->getDefaultDMA() != DMA_tofrom_scalar);
1923 if (IsFirstprivate)
Alexey Bataevf47c4b42017-09-26 13:47:31 +00001924 ImplicitFirstprivate.emplace_back(E);
1925 else
1926 ImplicitMap.emplace_back(E);
1927 return;
1928 }
1929 }
1930
Alexey Bataev758e55e2013-09-06 18:03:48 +00001931 // OpenMP [2.9.3.6, Restrictions, p.2]
1932 // A list item that appears in a reduction clause of the innermost
1933 // enclosing worksharing or parallel construct may not be accessed in an
1934 // explicit task.
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001935 DVar = Stack->hasInnermostDSA(
1936 VD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
1937 [](OpenMPDirectiveKind K) -> bool {
1938 return isOpenMPParallelDirective(K) ||
1939 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
1940 },
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001941 /*FromParent=*/true);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001942 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00001943 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001944 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1945 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001946 return;
1947 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001948
1949 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001950 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001951 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1952 !Stack->isLoopControlVariable(VD).first)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001953 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001954 }
1955 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001956 void VisitMemberExpr(MemberExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00001957 if (E->isTypeDependent() || E->isValueDependent() ||
1958 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1959 return;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00001960 auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
Alexey Bataevf47c4b42017-09-26 13:47:31 +00001961 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001962 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +00001963 if (!FD)
1964 return;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00001965 auto DVar = Stack->getTopDSA(FD, false);
1966 // Check if the variable has explicit DSA set and stop analysis if it
1967 // so.
1968 if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second)
1969 return;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001970
Alexey Bataevf47c4b42017-09-26 13:47:31 +00001971 if (isOpenMPTargetExecutionDirective(DKind) &&
1972 !Stack->isLoopControlVariable(FD).first &&
1973 !Stack->checkMappableExprComponentListsForDecl(
1974 FD, /*CurrentRegionOnly=*/true,
1975 [](OMPClauseMappableExprCommon::MappableExprComponentListRef
1976 StackComponents,
1977 OpenMPClauseKind) {
1978 return isa<CXXThisExpr>(
1979 cast<MemberExpr>(
1980 StackComponents.back().getAssociatedExpression())
1981 ->getBase()
1982 ->IgnoreParens());
1983 })) {
1984 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
1985 // A bit-field cannot appear in a map clause.
1986 //
Alexey Bataevb7a9b742017-12-05 19:20:09 +00001987 if (FD->isBitField())
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001988 return;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00001989 ImplicitMap.emplace_back(E);
1990 return;
1991 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001992
Alexey Bataevf47c4b42017-09-26 13:47:31 +00001993 auto ELoc = E->getExprLoc();
1994 // OpenMP [2.9.3.6, Restrictions, p.2]
1995 // A list item that appears in a reduction clause of the innermost
1996 // enclosing worksharing or parallel construct may not be accessed in
1997 // an explicit task.
1998 DVar = Stack->hasInnermostDSA(
1999 FD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
2000 [](OpenMPDirectiveKind K) -> bool {
2001 return isOpenMPParallelDirective(K) ||
2002 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2003 },
2004 /*FromParent=*/true);
2005 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
2006 ErrorFound = true;
2007 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
2008 ReportOriginalDSA(SemaRef, Stack, FD, DVar);
2009 return;
2010 }
2011
2012 // Define implicit data-sharing attributes for task.
2013 DVar = Stack->getImplicitDSA(FD, false);
2014 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
2015 !Stack->isLoopControlVariable(FD).first)
2016 ImplicitFirstprivate.push_back(E);
2017 return;
2018 }
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002019 if (isOpenMPTargetExecutionDirective(DKind)) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002020 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002021 if (!CheckMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map,
2022 /*NoDiagnose=*/true))
Alexey Bataev27041fa2017-12-05 15:22:49 +00002023 return;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002024 auto *VD = cast<ValueDecl>(
2025 CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl());
2026 if (!Stack->checkMappableExprComponentListsForDecl(
2027 VD, /*CurrentRegionOnly=*/true,
2028 [&CurComponents](
2029 OMPClauseMappableExprCommon::MappableExprComponentListRef
2030 StackComponents,
2031 OpenMPClauseKind) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002032 auto CCI = CurComponents.rbegin();
Alexey Bataev5ec38932017-09-26 16:19:04 +00002033 auto CCE = CurComponents.rend();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002034 for (const auto &SC : llvm::reverse(StackComponents)) {
2035 // Do both expressions have the same kind?
2036 if (CCI->getAssociatedExpression()->getStmtClass() !=
2037 SC.getAssociatedExpression()->getStmtClass())
2038 if (!(isa<OMPArraySectionExpr>(
2039 SC.getAssociatedExpression()) &&
2040 isa<ArraySubscriptExpr>(
2041 CCI->getAssociatedExpression())))
2042 return false;
2043
2044 Decl *CCD = CCI->getAssociatedDeclaration();
2045 Decl *SCD = SC.getAssociatedDeclaration();
2046 CCD = CCD ? CCD->getCanonicalDecl() : nullptr;
2047 SCD = SCD ? SCD->getCanonicalDecl() : nullptr;
2048 if (SCD != CCD)
2049 return false;
2050 std::advance(CCI, 1);
Alexey Bataev5ec38932017-09-26 16:19:04 +00002051 if (CCI == CCE)
2052 break;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002053 }
2054 return true;
2055 })) {
2056 Visit(E->getBase());
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002057 }
Alexey Bataev7fcacd82016-11-28 15:55:15 +00002058 } else
2059 Visit(E->getBase());
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002060 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002061 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002062 for (auto *C : S->clauses()) {
2063 // Skip analysis of arguments of implicitly defined firstprivate clause
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002064 // for task|target directives.
2065 // Skip analysis of arguments of implicitly defined map clause for target
2066 // directives.
2067 if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) &&
2068 C->isImplicit())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002069 for (auto *CC : C->children()) {
2070 if (CC)
2071 Visit(CC);
2072 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002073 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002074 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002075 }
2076 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002077 for (auto *C : S->children()) {
2078 if (C && !isa<OMPExecutableDirective>(C))
2079 Visit(C);
2080 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002081 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002082
2083 bool isErrorFound() { return ErrorFound; }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002084 ArrayRef<Expr *> getImplicitFirstprivate() const {
2085 return ImplicitFirstprivate;
2086 }
2087 ArrayRef<Expr *> getImplicitMap() const { return ImplicitMap; }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002088 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002089 return VarsWithInheritedDSA;
2090 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002091
Alexey Bataev7ff55242014-06-19 09:13:45 +00002092 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
2093 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00002094};
Alexey Bataeved09d242014-05-28 05:53:51 +00002095} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00002096
Alexey Bataevbae9a792014-06-27 10:37:06 +00002097void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00002098 switch (DKind) {
Kelvin Li70a12c52016-07-13 21:51:49 +00002099 case OMPD_parallel:
2100 case OMPD_parallel_for:
2101 case OMPD_parallel_for_simd:
2102 case OMPD_parallel_sections:
Carlo Bertolliba1487b2017-10-04 14:12:09 +00002103 case OMPD_teams:
Alexey Bataev999277a2017-12-06 14:31:09 +00002104 case OMPD_teams_distribute:
2105 case OMPD_teams_distribute_simd: {
Alexey Bataev9959db52014-05-06 10:08:46 +00002106 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00002107 QualType KmpInt32PtrTy =
2108 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002109 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002110 std::make_pair(".global_tid.", KmpInt32PtrTy),
2111 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2112 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00002113 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00002114 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2115 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00002116 break;
2117 }
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002118 case OMPD_target_teams:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00002119 case OMPD_target_parallel:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00002120 case OMPD_target_parallel_for:
Alexey Bataevdfa430f2017-12-08 15:03:50 +00002121 case OMPD_target_parallel_for_simd:
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00002122 case OMPD_target_teams_distribute:
2123 case OMPD_target_teams_distribute_simd: {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002124 Sema::CapturedParamNameType ParamsTarget[] = {
2125 std::make_pair(StringRef(), QualType()) // __context with shared vars
2126 };
2127 // Start a captured region for 'target' with no implicit parameters.
2128 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2129 ParamsTarget);
2130 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
2131 QualType KmpInt32PtrTy =
2132 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002133 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002134 std::make_pair(".global_tid.", KmpInt32PtrTy),
2135 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2136 std::make_pair(StringRef(), QualType()) // __context with shared vars
2137 };
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002138 // Start a captured region for 'teams' or 'parallel'. Both regions have
2139 // the same implicit parameters.
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002140 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002141 ParamsTeamsOrParallel);
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002142 break;
2143 }
Kelvin Li70a12c52016-07-13 21:51:49 +00002144 case OMPD_simd:
2145 case OMPD_for:
2146 case OMPD_for_simd:
2147 case OMPD_sections:
2148 case OMPD_section:
2149 case OMPD_single:
2150 case OMPD_master:
2151 case OMPD_critical:
Kelvin Lia579b912016-07-14 02:54:56 +00002152 case OMPD_taskgroup:
2153 case OMPD_distribute:
Alexey Bataev46506272017-12-05 17:41:34 +00002154 case OMPD_distribute_simd:
Kelvin Li70a12c52016-07-13 21:51:49 +00002155 case OMPD_ordered:
2156 case OMPD_atomic:
2157 case OMPD_target_data:
2158 case OMPD_target:
Kelvin Li986330c2016-07-20 22:57:10 +00002159 case OMPD_target_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002160 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002161 std::make_pair(StringRef(), QualType()) // __context with shared vars
2162 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00002163 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2164 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002165 break;
2166 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002167 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00002168 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002169 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
2170 FunctionProtoType::ExtProtoInfo EPI;
2171 EPI.Variadic = true;
2172 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002173 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00002174 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataev48591dd2016-04-20 04:01:36 +00002175 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
2176 std::make_pair(".privates.", Context.VoidPtrTy.withConst()),
2177 std::make_pair(".copy_fn.",
2178 Context.getPointerType(CopyFnType).withConst()),
2179 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002180 std::make_pair(StringRef(), QualType()) // __context with shared vars
2181 };
2182 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2183 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00002184 // Mark this captured region as inlined, because we don't use outlined
2185 // function directly.
2186 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2187 AlwaysInlineAttr::CreateImplicit(
2188 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002189 break;
2190 }
Alexey Bataev1e73ef32016-04-28 12:14:51 +00002191 case OMPD_taskloop:
2192 case OMPD_taskloop_simd: {
Alexey Bataev7292c292016-04-25 12:22:29 +00002193 QualType KmpInt32Ty =
2194 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
2195 QualType KmpUInt64Ty =
2196 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
2197 QualType KmpInt64Ty =
2198 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
2199 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
2200 FunctionProtoType::ExtProtoInfo EPI;
2201 EPI.Variadic = true;
2202 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev49f6e782015-12-01 04:18:41 +00002203 Sema::CapturedParamNameType Params[] = {
Alexey Bataev7292c292016-04-25 12:22:29 +00002204 std::make_pair(".global_tid.", KmpInt32Ty),
2205 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
2206 std::make_pair(".privates.",
2207 Context.VoidPtrTy.withConst().withRestrict()),
2208 std::make_pair(
2209 ".copy_fn.",
2210 Context.getPointerType(CopyFnType).withConst().withRestrict()),
2211 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2212 std::make_pair(".lb.", KmpUInt64Ty),
2213 std::make_pair(".ub.", KmpUInt64Ty), std::make_pair(".st.", KmpInt64Ty),
2214 std::make_pair(".liter.", KmpInt32Ty),
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00002215 std::make_pair(".reductions.",
2216 Context.VoidPtrTy.withConst().withRestrict()),
Alexey Bataev49f6e782015-12-01 04:18:41 +00002217 std::make_pair(StringRef(), QualType()) // __context with shared vars
2218 };
2219 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2220 Params);
Alexey Bataev7292c292016-04-25 12:22:29 +00002221 // Mark this captured region as inlined, because we don't use outlined
2222 // function directly.
2223 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2224 AlwaysInlineAttr::CreateImplicit(
2225 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev49f6e782015-12-01 04:18:41 +00002226 break;
2227 }
Kelvin Li4a39add2016-07-05 05:00:15 +00002228 case OMPD_distribute_parallel_for_simd:
Kelvin Li02532872016-08-05 14:37:37 +00002229 case OMPD_distribute_parallel_for:
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00002230 case OMPD_target_teams_distribute_parallel_for_simd: {
Carlo Bertolli9925f152016-06-27 14:55:37 +00002231 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
2232 QualType KmpInt32PtrTy =
2233 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2234 Sema::CapturedParamNameType Params[] = {
2235 std::make_pair(".global_tid.", KmpInt32PtrTy),
2236 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2237 std::make_pair(".previous.lb.", Context.getSizeType()),
2238 std::make_pair(".previous.ub.", Context.getSizeType()),
2239 std::make_pair(StringRef(), QualType()) // __context with shared vars
2240 };
2241 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2242 Params);
2243 break;
2244 }
Carlo Bertolli52978c32018-01-03 21:12:44 +00002245 case OMPD_target_teams_distribute_parallel_for: {
2246 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
2247 QualType KmpInt32PtrTy =
2248 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2249
2250 Sema::CapturedParamNameType ParamsTarget[] = {
2251 std::make_pair(StringRef(), QualType()) // __context with shared vars
2252 };
2253 // Start a captured region for 'target' with no implicit parameters.
2254 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2255 ParamsTarget);
2256
2257 Sema::CapturedParamNameType ParamsTeams[] = {
2258 std::make_pair(".global_tid.", KmpInt32PtrTy),
2259 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2260 std::make_pair(StringRef(), QualType()) // __context with shared vars
2261 };
2262 // Start a captured region for 'target' with no implicit parameters.
2263 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2264 ParamsTeams);
2265
2266 Sema::CapturedParamNameType ParamsParallel[] = {
2267 std::make_pair(".global_tid.", KmpInt32PtrTy),
2268 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2269 std::make_pair(".previous.lb.", Context.getSizeType()),
2270 std::make_pair(".previous.ub.", Context.getSizeType()),
2271 std::make_pair(StringRef(), QualType()) // __context with shared vars
2272 };
2273 // Start a captured region for 'teams' or 'parallel'. Both regions have
2274 // the same implicit parameters.
2275 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2276 ParamsParallel);
2277 break;
2278 }
2279
Alexey Bataev46506272017-12-05 17:41:34 +00002280 case OMPD_teams_distribute_parallel_for:
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00002281 case OMPD_teams_distribute_parallel_for_simd: {
Carlo Bertolli62fae152017-11-20 20:46:39 +00002282 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
2283 QualType KmpInt32PtrTy =
2284 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2285
2286 Sema::CapturedParamNameType ParamsTeams[] = {
2287 std::make_pair(".global_tid.", KmpInt32PtrTy),
2288 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2289 std::make_pair(StringRef(), QualType()) // __context with shared vars
2290 };
2291 // Start a captured region for 'target' with no implicit parameters.
2292 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2293 ParamsTeams);
2294
2295 Sema::CapturedParamNameType ParamsParallel[] = {
2296 std::make_pair(".global_tid.", KmpInt32PtrTy),
2297 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2298 std::make_pair(".previous.lb.", Context.getSizeType()),
2299 std::make_pair(".previous.ub.", Context.getSizeType()),
2300 std::make_pair(StringRef(), QualType()) // __context with shared vars
2301 };
2302 // Start a captured region for 'teams' or 'parallel'. Both regions have
2303 // the same implicit parameters.
2304 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2305 ParamsParallel);
2306 break;
2307 }
Alexey Bataev7828b252017-11-21 17:08:48 +00002308 case OMPD_target_update:
2309 case OMPD_target_enter_data:
2310 case OMPD_target_exit_data: {
2311 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
2312 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
2313 FunctionProtoType::ExtProtoInfo EPI;
2314 EPI.Variadic = true;
2315 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2316 Sema::CapturedParamNameType Params[] = {
2317 std::make_pair(".global_tid.", KmpInt32Ty),
2318 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
2319 std::make_pair(".privates.", Context.VoidPtrTy.withConst()),
2320 std::make_pair(".copy_fn.",
2321 Context.getPointerType(CopyFnType).withConst()),
2322 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2323 std::make_pair(StringRef(), QualType()) // __context with shared vars
2324 };
2325 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2326 Params);
2327 // Mark this captured region as inlined, because we don't use outlined
2328 // function directly.
2329 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2330 AlwaysInlineAttr::CreateImplicit(
2331 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
2332 break;
2333 }
Alexey Bataev9959db52014-05-06 10:08:46 +00002334 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00002335 case OMPD_taskyield:
2336 case OMPD_barrier:
2337 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002338 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00002339 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00002340 case OMPD_flush:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002341 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00002342 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00002343 case OMPD_declare_target:
2344 case OMPD_end_declare_target:
Alexey Bataev9959db52014-05-06 10:08:46 +00002345 llvm_unreachable("OpenMP Directive is not allowed");
2346 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00002347 llvm_unreachable("Unknown OpenMP directive");
2348 }
2349}
2350
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002351int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) {
2352 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2353 getOpenMPCaptureRegions(CaptureRegions, DKind);
2354 return CaptureRegions.size();
2355}
2356
Alexey Bataev3392d762016-02-16 11:18:12 +00002357static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00002358 Expr *CaptureExpr, bool WithInit,
2359 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00002360 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00002361 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00002362 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00002363 QualType Ty = Init->getType();
2364 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002365 if (S.getLangOpts().CPlusPlus) {
Alexey Bataev4244be22016-02-11 05:35:55 +00002366 Ty = C.getLValueReferenceType(Ty);
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002367 } else {
Alexey Bataev4244be22016-02-11 05:35:55 +00002368 Ty = C.getPointerType(Ty);
2369 ExprResult Res =
2370 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
2371 if (!Res.isUsable())
2372 return nullptr;
2373 Init = Res.get();
2374 }
Alexey Bataev61205072016-03-02 04:57:40 +00002375 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00002376 }
Alexey Bataeva7206b92016-12-20 16:51:02 +00002377 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty,
2378 CaptureExpr->getLocStart());
Alexey Bataev2bbf7212016-03-03 03:52:24 +00002379 if (!WithInit)
2380 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
Alexey Bataev4244be22016-02-11 05:35:55 +00002381 S.CurContext->addHiddenDecl(CED);
Richard Smith3beb7c62017-01-12 02:27:38 +00002382 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00002383 return CED;
2384}
2385
Alexey Bataev61205072016-03-02 04:57:40 +00002386static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
2387 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00002388 OMPCapturedExprDecl *CD;
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002389 if (auto *VD = S.IsOpenMPCapturedDecl(D)) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00002390 CD = cast<OMPCapturedExprDecl>(VD);
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002391 } else {
Alexey Bataev5a3af132016-03-29 08:58:54 +00002392 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
2393 /*AsExpression=*/false);
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002394 }
Alexey Bataev3392d762016-02-16 11:18:12 +00002395 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00002396 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00002397}
2398
Alexey Bataev5a3af132016-03-29 08:58:54 +00002399static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002400 CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00002401 if (!Ref) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002402 OMPCapturedExprDecl *CD = buildCaptureDecl(
2403 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr,
2404 /*WithInit=*/true, /*AsExpression=*/true);
Alexey Bataev5a3af132016-03-29 08:58:54 +00002405 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
2406 CaptureExpr->getExprLoc());
2407 }
2408 ExprResult Res = Ref;
2409 if (!S.getLangOpts().CPlusPlus &&
2410 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002411 Ref->getType()->isPointerType()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00002412 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002413 if (!Res.isUsable())
2414 return ExprError();
2415 }
2416 return S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00002417}
2418
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002419namespace {
2420// OpenMP directives parsed in this section are represented as a
2421// CapturedStatement with an associated statement. If a syntax error
2422// is detected during the parsing of the associated statement, the
2423// compiler must abort processing and close the CapturedStatement.
2424//
2425// Combined directives such as 'target parallel' have more than one
2426// nested CapturedStatements. This RAII ensures that we unwind out
2427// of all the nested CapturedStatements when an error is found.
2428class CaptureRegionUnwinderRAII {
2429private:
2430 Sema &S;
2431 bool &ErrorFound;
2432 OpenMPDirectiveKind DKind;
2433
2434public:
2435 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
2436 OpenMPDirectiveKind DKind)
2437 : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
2438 ~CaptureRegionUnwinderRAII() {
2439 if (ErrorFound) {
2440 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind);
2441 while (--ThisCaptureLevel >= 0)
2442 S.ActOnCapturedRegionError();
2443 }
2444 }
2445};
2446} // namespace
2447
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002448StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
2449 ArrayRef<OMPClause *> Clauses) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002450 bool ErrorFound = false;
2451 CaptureRegionUnwinderRAII CaptureRegionUnwinder(
2452 *this, ErrorFound, DSAStack->getCurrentDirective());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002453 if (!S.isUsable()) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002454 ErrorFound = true;
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002455 return StmtError();
2456 }
Alexey Bataev993d2802015-12-28 06:23:08 +00002457
Alexey Bataev2ba67042017-11-28 21:11:44 +00002458 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2459 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
Alexey Bataev993d2802015-12-28 06:23:08 +00002460 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00002461 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00002462 SmallVector<OMPLinearClause *, 4> LCs;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002463 SmallVector<OMPClauseWithPreInit *, 8> PICs;
Alexey Bataev040d5402015-05-12 08:35:28 +00002464 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002465 for (auto *Clause : Clauses) {
Alexey Bataev88202be2017-07-27 13:20:36 +00002466 if (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) &&
2467 Clause->getClauseKind() == OMPC_in_reduction) {
2468 // Capture taskgroup task_reduction descriptors inside the tasking regions
2469 // with the corresponding in_reduction items.
2470 auto *IRC = cast<OMPInReductionClause>(Clause);
2471 for (auto *E : IRC->taskgroup_descriptors())
2472 if (E)
2473 MarkDeclarationsReferencedInExpr(E);
2474 }
Alexey Bataev16dc7b62015-05-20 03:46:04 +00002475 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00002476 Clause->getClauseKind() == OMPC_copyprivate ||
2477 (getLangOpts().OpenMPUseTLS &&
2478 getASTContext().getTargetInfo().isTLSSupported() &&
2479 Clause->getClauseKind() == OMPC_copyin)) {
2480 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00002481 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002482 for (auto *VarRef : Clause->children()) {
2483 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00002484 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002485 }
2486 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00002487 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev2ba67042017-11-28 21:11:44 +00002488 } else if (CaptureRegions.size() > 1 ||
2489 CaptureRegions.back() != OMPD_unknown) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002490 if (auto *C = OMPClauseWithPreInit::get(Clause))
2491 PICs.push_back(C);
Alexey Bataev005248a2016-02-25 05:25:57 +00002492 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
2493 if (auto *E = C->getPostUpdateExpr())
2494 MarkDeclarationsReferencedInExpr(E);
2495 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002496 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002497 if (Clause->getClauseKind() == OMPC_schedule)
2498 SC = cast<OMPScheduleClause>(Clause);
2499 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00002500 OC = cast<OMPOrderedClause>(Clause);
2501 else if (Clause->getClauseKind() == OMPC_linear)
2502 LCs.push_back(cast<OMPLinearClause>(Clause));
2503 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002504 // OpenMP, 2.7.1 Loop Construct, Restrictions
2505 // The nonmonotonic modifier cannot be specified if an ordered clause is
2506 // specified.
2507 if (SC &&
2508 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
2509 SC->getSecondScheduleModifier() ==
2510 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
2511 OC) {
2512 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
2513 ? SC->getFirstScheduleModifierLoc()
2514 : SC->getSecondScheduleModifierLoc(),
2515 diag::err_omp_schedule_nonmonotonic_ordered)
2516 << SourceRange(OC->getLocStart(), OC->getLocEnd());
2517 ErrorFound = true;
2518 }
Alexey Bataev993d2802015-12-28 06:23:08 +00002519 if (!LCs.empty() && OC && OC->getNumForLoops()) {
2520 for (auto *C : LCs) {
2521 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
2522 << SourceRange(OC->getLocStart(), OC->getLocEnd());
2523 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002524 ErrorFound = true;
2525 }
Alexey Bataev113438c2015-12-30 12:06:23 +00002526 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
2527 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
2528 OC->getNumForLoops()) {
2529 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
2530 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
2531 ErrorFound = true;
2532 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002533 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00002534 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002535 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002536 StmtResult SR = S;
Alexey Bataev2ba67042017-11-28 21:11:44 +00002537 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002538 // Mark all variables in private list clauses as used in inner region.
2539 // Required for proper codegen of combined directives.
2540 // TODO: add processing for other clauses.
Alexey Bataev2ba67042017-11-28 21:11:44 +00002541 if (ThisCaptureRegion != OMPD_unknown) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002542 for (auto *C : PICs) {
2543 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
2544 // Find the particular capture region for the clause if the
2545 // directive is a combined one with multiple capture regions.
2546 // If the directive is not a combined one, the capture region
2547 // associated with the clause is OMPD_unknown and is generated
2548 // only once.
2549 if (CaptureRegion == ThisCaptureRegion ||
2550 CaptureRegion == OMPD_unknown) {
2551 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
2552 for (auto *D : DS->decls())
2553 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
2554 }
2555 }
2556 }
2557 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002558 SR = ActOnCapturedRegionEnd(SR.get());
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002559 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002560 return SR;
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002561}
2562
Jonas Hahnfeld64a9e3c2017-02-22 06:49:10 +00002563static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
2564 OpenMPDirectiveKind CancelRegion,
2565 SourceLocation StartLoc) {
2566 // CancelRegion is only needed for cancel and cancellation_point.
2567 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point)
2568 return false;
2569
2570 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for ||
2571 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup)
2572 return false;
2573
2574 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region)
2575 << getOpenMPDirectiveName(CancelRegion);
2576 return true;
2577}
2578
2579static bool checkNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002580 OpenMPDirectiveKind CurrentRegion,
2581 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002582 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002583 SourceLocation StartLoc) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002584 if (Stack->getCurScope()) {
2585 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002586 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002587 bool NestingProhibited = false;
2588 bool CloseNesting = true;
David Majnemer9d168222016-08-05 17:44:54 +00002589 bool OrphanSeen = false;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002590 enum {
2591 NoRecommend,
2592 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002593 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002594 ShouldBeInTargetRegion,
2595 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002596 } Recommend = NoRecommend;
Kelvin Lifd8b5742016-07-01 14:30:25 +00002597 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002598 // OpenMP [2.16, Nesting of Regions]
2599 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002600 // OpenMP [2.8.1,simd Construct, Restrictions]
Kelvin Lifd8b5742016-07-01 14:30:25 +00002601 // An ordered construct with the simd clause is the only OpenMP
2602 // construct that can appear in the simd region.
David Majnemer9d168222016-08-05 17:44:54 +00002603 // Allowing a SIMD construct nested in another SIMD construct is an
Kelvin Lifd8b5742016-07-01 14:30:25 +00002604 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
2605 // message.
2606 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
2607 ? diag::err_omp_prohibited_region_simd
2608 : diag::warn_omp_nesting_simd);
2609 return CurrentRegion != OMPD_simd;
Alexey Bataev549210e2014-06-24 04:39:47 +00002610 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002611 if (ParentRegion == OMPD_atomic) {
2612 // OpenMP [2.16, Nesting of Regions]
2613 // OpenMP constructs may not be nested inside an atomic region.
2614 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2615 return true;
2616 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002617 if (CurrentRegion == OMPD_section) {
2618 // OpenMP [2.7.2, sections Construct, Restrictions]
2619 // Orphaned section directives are prohibited. That is, the section
2620 // directives must appear within the sections construct and must not be
2621 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002622 if (ParentRegion != OMPD_sections &&
2623 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002624 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2625 << (ParentRegion != OMPD_unknown)
2626 << getOpenMPDirectiveName(ParentRegion);
2627 return true;
2628 }
2629 return false;
2630 }
Kelvin Li2b51f722016-07-26 04:32:50 +00002631 // Allow some constructs (except teams) to be orphaned (they could be
David Majnemer9d168222016-08-05 17:44:54 +00002632 // used in functions, called from OpenMP regions with the required
Kelvin Li2b51f722016-07-26 04:32:50 +00002633 // preconditions).
Kelvin Libf594a52016-12-17 05:48:59 +00002634 if (ParentRegion == OMPD_unknown &&
2635 !isOpenMPNestingTeamsDirective(CurrentRegion))
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002636 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002637 if (CurrentRegion == OMPD_cancellation_point ||
2638 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002639 // OpenMP [2.16, Nesting of Regions]
2640 // A cancellation point construct for which construct-type-clause is
2641 // taskgroup must be nested inside a task construct. A cancellation
2642 // point construct for which construct-type-clause is not taskgroup must
2643 // be closely nested inside an OpenMP construct that matches the type
2644 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002645 // A cancel construct for which construct-type-clause is taskgroup must be
2646 // nested inside a task construct. A cancel construct for which
2647 // construct-type-clause is not taskgroup must be closely nested inside an
2648 // OpenMP construct that matches the type specified in
2649 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002650 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002651 !((CancelRegion == OMPD_parallel &&
2652 (ParentRegion == OMPD_parallel ||
2653 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002654 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002655 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00002656 ParentRegion == OMPD_target_parallel_for ||
2657 ParentRegion == OMPD_distribute_parallel_for ||
Alexey Bataev16e79882017-11-22 21:12:03 +00002658 ParentRegion == OMPD_teams_distribute_parallel_for ||
2659 ParentRegion == OMPD_target_teams_distribute_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002660 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2661 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002662 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2663 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002664 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002665 // OpenMP [2.16, Nesting of Regions]
2666 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002667 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002668 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00002669 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002670 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2671 // OpenMP [2.16, Nesting of Regions]
2672 // A critical region may not be nested (closely or otherwise) inside a
2673 // critical region with the same name. Note that this restriction is not
2674 // sufficient to prevent deadlock.
2675 SourceLocation PreviousCriticalLoc;
David Majnemer9d168222016-08-05 17:44:54 +00002676 bool DeadLock = Stack->hasDirective(
2677 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
2678 const DeclarationNameInfo &DNI,
2679 SourceLocation Loc) -> bool {
2680 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
2681 PreviousCriticalLoc = Loc;
2682 return true;
2683 } else
2684 return false;
2685 },
2686 false /* skip top directive */);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002687 if (DeadLock) {
2688 SemaRef.Diag(StartLoc,
2689 diag::err_omp_prohibited_region_critical_same_name)
2690 << CurrentName.getName();
2691 if (PreviousCriticalLoc.isValid())
2692 SemaRef.Diag(PreviousCriticalLoc,
2693 diag::note_omp_previous_critical_region);
2694 return true;
2695 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002696 } else if (CurrentRegion == OMPD_barrier) {
2697 // OpenMP [2.16, Nesting of Regions]
2698 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002699 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00002700 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2701 isOpenMPTaskingDirective(ParentRegion) ||
2702 ParentRegion == OMPD_master ||
2703 ParentRegion == OMPD_critical ||
2704 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00002705 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Kelvin Li579e41c2016-11-30 23:51:03 +00002706 !isOpenMPParallelDirective(CurrentRegion) &&
2707 !isOpenMPTeamsDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002708 // OpenMP [2.16, Nesting of Regions]
2709 // A worksharing region may not be closely nested inside a worksharing,
2710 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00002711 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2712 isOpenMPTaskingDirective(ParentRegion) ||
2713 ParentRegion == OMPD_master ||
2714 ParentRegion == OMPD_critical ||
2715 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002716 Recommend = ShouldBeInParallelRegion;
2717 } else if (CurrentRegion == OMPD_ordered) {
2718 // OpenMP [2.16, Nesting of Regions]
2719 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002720 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002721 // An ordered region must be closely nested inside a loop region (or
2722 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002723 // OpenMP [2.8.1,simd Construct, Restrictions]
2724 // An ordered construct with the simd clause is the only OpenMP construct
2725 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002726 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00002727 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002728 !(isOpenMPSimdDirective(ParentRegion) ||
2729 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002730 Recommend = ShouldBeInOrderedRegion;
Kelvin Libf594a52016-12-17 05:48:59 +00002731 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00002732 // OpenMP [2.16, Nesting of Regions]
2733 // If specified, a teams construct must be contained within a target
2734 // construct.
2735 NestingProhibited = ParentRegion != OMPD_target;
Kelvin Li2b51f722016-07-26 04:32:50 +00002736 OrphanSeen = ParentRegion == OMPD_unknown;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002737 Recommend = ShouldBeInTargetRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002738 }
Kelvin Libf594a52016-12-17 05:48:59 +00002739 if (!NestingProhibited &&
2740 !isOpenMPTargetExecutionDirective(CurrentRegion) &&
2741 !isOpenMPTargetDataManagementDirective(CurrentRegion) &&
2742 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00002743 // OpenMP [2.16, Nesting of Regions]
2744 // distribute, parallel, parallel sections, parallel workshare, and the
2745 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2746 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002747 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2748 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002749 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002750 }
David Majnemer9d168222016-08-05 17:44:54 +00002751 if (!NestingProhibited &&
Kelvin Li02532872016-08-05 14:37:37 +00002752 isOpenMPNestingDistributeDirective(CurrentRegion)) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002753 // OpenMP 4.5 [2.17 Nesting of Regions]
2754 // The region associated with the distribute construct must be strictly
2755 // nested inside a teams region
Kelvin Libf594a52016-12-17 05:48:59 +00002756 NestingProhibited =
2757 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002758 Recommend = ShouldBeInTeamsRegion;
2759 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002760 if (!NestingProhibited &&
2761 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2762 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2763 // OpenMP 4.5 [2.17 Nesting of Regions]
2764 // If a target, target update, target data, target enter data, or
2765 // target exit data construct is encountered during execution of a
2766 // target region, the behavior is unspecified.
2767 NestingProhibited = Stack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +00002768 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
2769 SourceLocation) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002770 if (isOpenMPTargetExecutionDirective(K)) {
2771 OffendingRegion = K;
2772 return true;
2773 } else
2774 return false;
2775 },
2776 false /* don't skip top directive */);
2777 CloseNesting = false;
2778 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002779 if (NestingProhibited) {
Kelvin Li2b51f722016-07-26 04:32:50 +00002780 if (OrphanSeen) {
2781 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
2782 << getOpenMPDirectiveName(CurrentRegion) << Recommend;
2783 } else {
2784 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
2785 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2786 << Recommend << getOpenMPDirectiveName(CurrentRegion);
2787 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002788 return true;
2789 }
2790 }
2791 return false;
2792}
2793
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002794static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2795 ArrayRef<OMPClause *> Clauses,
2796 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2797 bool ErrorFound = false;
2798 unsigned NamedModifiersNumber = 0;
2799 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2800 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002801 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002802 for (const auto *C : Clauses) {
2803 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2804 // At most one if clause without a directive-name-modifier can appear on
2805 // the directive.
2806 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2807 if (FoundNameModifiers[CurNM]) {
2808 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2809 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2810 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2811 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002812 } else if (CurNM != OMPD_unknown) {
2813 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002814 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002815 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002816 FoundNameModifiers[CurNM] = IC;
2817 if (CurNM == OMPD_unknown)
2818 continue;
2819 // Check if the specified name modifier is allowed for the current
2820 // directive.
2821 // At most one if clause with the particular directive-name-modifier can
2822 // appear on the directive.
2823 bool MatchFound = false;
2824 for (auto NM : AllowedNameModifiers) {
2825 if (CurNM == NM) {
2826 MatchFound = true;
2827 break;
2828 }
2829 }
2830 if (!MatchFound) {
2831 S.Diag(IC->getNameModifierLoc(),
2832 diag::err_omp_wrong_if_directive_name_modifier)
2833 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2834 ErrorFound = true;
2835 }
2836 }
2837 }
2838 // If any if clause on the directive includes a directive-name-modifier then
2839 // all if clauses on the directive must include a directive-name-modifier.
2840 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2841 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2842 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2843 diag::err_omp_no_more_if_clause);
2844 } else {
2845 std::string Values;
2846 std::string Sep(", ");
2847 unsigned AllowedCnt = 0;
2848 unsigned TotalAllowedNum =
2849 AllowedNameModifiers.size() - NamedModifiersNumber;
2850 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2851 ++Cnt) {
2852 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2853 if (!FoundNameModifiers[NM]) {
2854 Values += "'";
2855 Values += getOpenMPDirectiveName(NM);
2856 Values += "'";
2857 if (AllowedCnt + 2 == TotalAllowedNum)
2858 Values += " or ";
2859 else if (AllowedCnt + 1 != TotalAllowedNum)
2860 Values += Sep;
2861 ++AllowedCnt;
2862 }
2863 }
2864 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2865 diag::err_omp_unnamed_if_clause)
2866 << (TotalAllowedNum > 1) << Values;
2867 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002868 for (auto Loc : NameModifierLoc) {
2869 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2870 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002871 ErrorFound = true;
2872 }
2873 return ErrorFound;
2874}
2875
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002876StmtResult Sema::ActOnOpenMPExecutableDirective(
2877 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2878 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2879 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002880 StmtResult Res = StmtError();
Jonas Hahnfeld64a9e3c2017-02-22 06:49:10 +00002881 // First check CancelRegion which is then used in checkNestingOfRegions.
2882 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) ||
2883 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002884 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002885 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002886
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002887 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002888 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002889 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002890 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00002891 if (AStmt && !CurContext->isDependentContext()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00002892 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2893
2894 // Check default data sharing attributes for referenced variables.
2895 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
Arpith Chacko Jacob1f46b702017-01-23 15:38:49 +00002896 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind);
2897 Stmt *S = AStmt;
2898 while (--ThisCaptureLevel >= 0)
2899 S = cast<CapturedStmt>(S)->getCapturedStmt();
2900 DSAChecker.Visit(S);
Alexey Bataev68446b72014-07-18 07:47:19 +00002901 if (DSAChecker.isErrorFound())
2902 return StmtError();
2903 // Generate list of implicitly defined firstprivate variables.
2904 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002905
Alexey Bataev88202be2017-07-27 13:20:36 +00002906 SmallVector<Expr *, 4> ImplicitFirstprivates(
2907 DSAChecker.getImplicitFirstprivate().begin(),
2908 DSAChecker.getImplicitFirstprivate().end());
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002909 SmallVector<Expr *, 4> ImplicitMaps(DSAChecker.getImplicitMap().begin(),
2910 DSAChecker.getImplicitMap().end());
Alexey Bataev88202be2017-07-27 13:20:36 +00002911 // Mark taskgroup task_reduction descriptors as implicitly firstprivate.
2912 for (auto *C : Clauses) {
2913 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) {
2914 for (auto *E : IRC->taskgroup_descriptors())
2915 if (E)
2916 ImplicitFirstprivates.emplace_back(E);
2917 }
2918 }
2919 if (!ImplicitFirstprivates.empty()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00002920 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
Alexey Bataev88202be2017-07-27 13:20:36 +00002921 ImplicitFirstprivates, SourceLocation(), SourceLocation(),
2922 SourceLocation())) {
Alexey Bataev68446b72014-07-18 07:47:19 +00002923 ClausesWithImplicit.push_back(Implicit);
2924 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
Alexey Bataev88202be2017-07-27 13:20:36 +00002925 ImplicitFirstprivates.size();
Alexey Bataev68446b72014-07-18 07:47:19 +00002926 } else
2927 ErrorFound = true;
2928 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002929 if (!ImplicitMaps.empty()) {
2930 if (OMPClause *Implicit = ActOnOpenMPMapClause(
2931 OMPC_MAP_unknown, OMPC_MAP_tofrom, /*IsMapTypeImplicit=*/true,
2932 SourceLocation(), SourceLocation(), ImplicitMaps,
2933 SourceLocation(), SourceLocation(), SourceLocation())) {
2934 ClausesWithImplicit.emplace_back(Implicit);
2935 ErrorFound |=
2936 cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMaps.size();
2937 } else
2938 ErrorFound = true;
2939 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002940 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002941
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002942 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002943 switch (Kind) {
2944 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002945 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2946 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002947 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002948 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002949 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002950 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2951 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002952 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002953 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002954 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2955 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002956 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002957 case OMPD_for_simd:
2958 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2959 EndLoc, VarsWithInheritedDSA);
2960 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002961 case OMPD_sections:
2962 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2963 EndLoc);
2964 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002965 case OMPD_section:
2966 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002967 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002968 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2969 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002970 case OMPD_single:
2971 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2972 EndLoc);
2973 break;
Alexander Musman80c22892014-07-17 08:54:58 +00002974 case OMPD_master:
2975 assert(ClausesWithImplicit.empty() &&
2976 "No clauses are allowed for 'omp master' directive");
2977 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2978 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002979 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00002980 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
2981 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002982 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002983 case OMPD_parallel_for:
2984 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2985 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002986 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002987 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002988 case OMPD_parallel_for_simd:
2989 Res = ActOnOpenMPParallelForSimdDirective(
2990 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002991 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00002992 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002993 case OMPD_parallel_sections:
2994 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2995 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002996 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002997 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002998 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002999 Res =
3000 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003001 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003002 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003003 case OMPD_taskyield:
3004 assert(ClausesWithImplicit.empty() &&
3005 "No clauses are allowed for 'omp taskyield' directive");
3006 assert(AStmt == nullptr &&
3007 "No associated statement allowed for 'omp taskyield' directive");
3008 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3009 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003010 case OMPD_barrier:
3011 assert(ClausesWithImplicit.empty() &&
3012 "No clauses are allowed for 'omp barrier' directive");
3013 assert(AStmt == nullptr &&
3014 "No associated statement allowed for 'omp barrier' directive");
3015 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3016 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003017 case OMPD_taskwait:
3018 assert(ClausesWithImplicit.empty() &&
3019 "No clauses are allowed for 'omp taskwait' directive");
3020 assert(AStmt == nullptr &&
3021 "No associated statement allowed for 'omp taskwait' directive");
3022 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3023 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003024 case OMPD_taskgroup:
Alexey Bataev169d96a2017-07-18 20:17:46 +00003025 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc,
3026 EndLoc);
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003027 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003028 case OMPD_flush:
3029 assert(AStmt == nullptr &&
3030 "No associated statement allowed for 'omp flush' directive");
3031 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3032 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003033 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003034 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3035 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003036 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003037 case OMPD_atomic:
3038 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3039 EndLoc);
3040 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003041 case OMPD_teams:
3042 Res =
3043 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3044 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003045 case OMPD_target:
3046 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3047 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003048 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003049 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003050 case OMPD_target_parallel:
3051 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3052 StartLoc, EndLoc);
3053 AllowedNameModifiers.push_back(OMPD_target);
3054 AllowedNameModifiers.push_back(OMPD_parallel);
3055 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003056 case OMPD_target_parallel_for:
3057 Res = ActOnOpenMPTargetParallelForDirective(
3058 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3059 AllowedNameModifiers.push_back(OMPD_target);
3060 AllowedNameModifiers.push_back(OMPD_parallel);
3061 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003062 case OMPD_cancellation_point:
3063 assert(ClausesWithImplicit.empty() &&
3064 "No clauses are allowed for 'omp cancellation point' directive");
3065 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3066 "cancellation point' directive");
3067 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3068 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003069 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003070 assert(AStmt == nullptr &&
3071 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003072 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3073 CancelRegion);
3074 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003075 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003076 case OMPD_target_data:
3077 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3078 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003079 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003080 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003081 case OMPD_target_enter_data:
3082 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00003083 EndLoc, AStmt);
Samuel Antaodf67fc42016-01-19 19:15:56 +00003084 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3085 break;
Samuel Antao72590762016-01-19 20:04:50 +00003086 case OMPD_target_exit_data:
3087 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00003088 EndLoc, AStmt);
Samuel Antao72590762016-01-19 20:04:50 +00003089 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3090 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003091 case OMPD_taskloop:
3092 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3093 EndLoc, VarsWithInheritedDSA);
3094 AllowedNameModifiers.push_back(OMPD_taskloop);
3095 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003096 case OMPD_taskloop_simd:
3097 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3098 EndLoc, VarsWithInheritedDSA);
3099 AllowedNameModifiers.push_back(OMPD_taskloop);
3100 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003101 case OMPD_distribute:
3102 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3103 EndLoc, VarsWithInheritedDSA);
3104 break;
Samuel Antao686c70c2016-05-26 17:30:50 +00003105 case OMPD_target_update:
Alexey Bataev7828b252017-11-21 17:08:48 +00003106 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc,
3107 EndLoc, AStmt);
Samuel Antao686c70c2016-05-26 17:30:50 +00003108 AllowedNameModifiers.push_back(OMPD_target_update);
3109 break;
Carlo Bertolli9925f152016-06-27 14:55:37 +00003110 case OMPD_distribute_parallel_for:
3111 Res = ActOnOpenMPDistributeParallelForDirective(
3112 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3113 AllowedNameModifiers.push_back(OMPD_parallel);
3114 break;
Kelvin Li4a39add2016-07-05 05:00:15 +00003115 case OMPD_distribute_parallel_for_simd:
3116 Res = ActOnOpenMPDistributeParallelForSimdDirective(
3117 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3118 AllowedNameModifiers.push_back(OMPD_parallel);
3119 break;
Kelvin Li787f3fc2016-07-06 04:45:38 +00003120 case OMPD_distribute_simd:
3121 Res = ActOnOpenMPDistributeSimdDirective(
3122 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3123 break;
Kelvin Lia579b912016-07-14 02:54:56 +00003124 case OMPD_target_parallel_for_simd:
3125 Res = ActOnOpenMPTargetParallelForSimdDirective(
3126 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3127 AllowedNameModifiers.push_back(OMPD_target);
3128 AllowedNameModifiers.push_back(OMPD_parallel);
3129 break;
Kelvin Li986330c2016-07-20 22:57:10 +00003130 case OMPD_target_simd:
3131 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3132 EndLoc, VarsWithInheritedDSA);
3133 AllowedNameModifiers.push_back(OMPD_target);
3134 break;
Kelvin Li02532872016-08-05 14:37:37 +00003135 case OMPD_teams_distribute:
David Majnemer9d168222016-08-05 17:44:54 +00003136 Res = ActOnOpenMPTeamsDistributeDirective(
3137 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Kelvin Li02532872016-08-05 14:37:37 +00003138 break;
Kelvin Li4e325f72016-10-25 12:50:55 +00003139 case OMPD_teams_distribute_simd:
3140 Res = ActOnOpenMPTeamsDistributeSimdDirective(
3141 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3142 break;
Kelvin Li579e41c2016-11-30 23:51:03 +00003143 case OMPD_teams_distribute_parallel_for_simd:
3144 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
3145 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3146 AllowedNameModifiers.push_back(OMPD_parallel);
3147 break;
Kelvin Li7ade93f2016-12-09 03:24:30 +00003148 case OMPD_teams_distribute_parallel_for:
3149 Res = ActOnOpenMPTeamsDistributeParallelForDirective(
3150 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3151 AllowedNameModifiers.push_back(OMPD_parallel);
3152 break;
Kelvin Libf594a52016-12-17 05:48:59 +00003153 case OMPD_target_teams:
3154 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc,
3155 EndLoc);
3156 AllowedNameModifiers.push_back(OMPD_target);
3157 break;
Kelvin Li83c451e2016-12-25 04:52:54 +00003158 case OMPD_target_teams_distribute:
3159 Res = ActOnOpenMPTargetTeamsDistributeDirective(
3160 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3161 AllowedNameModifiers.push_back(OMPD_target);
3162 break;
Kelvin Li80e8f562016-12-29 22:16:30 +00003163 case OMPD_target_teams_distribute_parallel_for:
3164 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
3165 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3166 AllowedNameModifiers.push_back(OMPD_target);
3167 AllowedNameModifiers.push_back(OMPD_parallel);
3168 break;
Kelvin Li1851df52017-01-03 05:23:48 +00003169 case OMPD_target_teams_distribute_parallel_for_simd:
3170 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
3171 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3172 AllowedNameModifiers.push_back(OMPD_target);
3173 AllowedNameModifiers.push_back(OMPD_parallel);
3174 break;
Kelvin Lida681182017-01-10 18:08:18 +00003175 case OMPD_target_teams_distribute_simd:
3176 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
3177 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3178 AllowedNameModifiers.push_back(OMPD_target);
3179 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003180 case OMPD_declare_target:
3181 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003182 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003183 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003184 case OMPD_declare_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003185 llvm_unreachable("OpenMP Directive is not allowed");
3186 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003187 llvm_unreachable("Unknown OpenMP directive");
3188 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003189
Alexey Bataev4acb8592014-07-07 13:01:15 +00003190 for (auto P : VarsWithInheritedDSA) {
3191 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3192 << P.first << P.second->getSourceRange();
3193 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003194 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3195
3196 if (!AllowedNameModifiers.empty())
3197 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3198 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003199
Alexey Bataeved09d242014-05-28 05:53:51 +00003200 if (ErrorFound)
3201 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003202 return Res;
3203}
3204
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003205Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3206 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00003207 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00003208 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3209 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003210 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00003211 assert(Linears.size() == LinModifiers.size());
3212 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00003213 if (!DG || DG.get().isNull())
3214 return DeclGroupPtrTy();
3215
3216 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00003217 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003218 return DG;
3219 }
3220 auto *ADecl = DG.get().getSingleDecl();
3221 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3222 ADecl = FTD->getTemplatedDecl();
3223
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003224 auto *FD = dyn_cast<FunctionDecl>(ADecl);
3225 if (!FD) {
3226 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003227 return DeclGroupPtrTy();
3228 }
3229
Alexey Bataev2af33e32016-04-07 12:45:37 +00003230 // OpenMP [2.8.2, declare simd construct, Description]
3231 // The parameter of the simdlen clause must be a constant positive integer
3232 // expression.
3233 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003234 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00003235 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003236 // OpenMP [2.8.2, declare simd construct, Description]
3237 // The special this pointer can be used as if was one of the arguments to the
3238 // function in any of the linear, aligned, or uniform clauses.
3239 // The uniform clause declares one or more arguments to have an invariant
3240 // value for all concurrent invocations of the function in the execution of a
3241 // single SIMD loop.
Alexey Bataevecba70f2016-04-12 11:02:11 +00003242 llvm::DenseMap<Decl *, Expr *> UniformedArgs;
3243 Expr *UniformedLinearThis = nullptr;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003244 for (auto *E : Uniforms) {
3245 E = E->IgnoreParenImpCasts();
3246 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3247 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
3248 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3249 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00003250 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
3251 UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E));
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003252 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003253 }
3254 if (isa<CXXThisExpr>(E)) {
3255 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003256 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003257 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003258 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3259 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00003260 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00003261 // OpenMP [2.8.2, declare simd construct, Description]
3262 // The aligned clause declares that the object to which each list item points
3263 // is aligned to the number of bytes expressed in the optional parameter of
3264 // the aligned clause.
3265 // The special this pointer can be used as if was one of the arguments to the
3266 // function in any of the linear, aligned, or uniform clauses.
3267 // The type of list items appearing in the aligned clause must be array,
3268 // pointer, reference to array, or reference to pointer.
3269 llvm::DenseMap<Decl *, Expr *> AlignedArgs;
3270 Expr *AlignedThis = nullptr;
3271 for (auto *E : Aligneds) {
3272 E = E->IgnoreParenImpCasts();
3273 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3274 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3275 auto *CanonPVD = PVD->getCanonicalDecl();
3276 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3277 FD->getParamDecl(PVD->getFunctionScopeIndex())
3278 ->getCanonicalDecl() == CanonPVD) {
3279 // OpenMP [2.8.1, simd construct, Restrictions]
3280 // A list-item cannot appear in more than one aligned clause.
3281 if (AlignedArgs.count(CanonPVD) > 0) {
3282 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3283 << 1 << E->getSourceRange();
3284 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3285 diag::note_omp_explicit_dsa)
3286 << getOpenMPClauseName(OMPC_aligned);
3287 continue;
3288 }
3289 AlignedArgs[CanonPVD] = E;
3290 QualType QTy = PVD->getType()
3291 .getNonReferenceType()
3292 .getUnqualifiedType()
3293 .getCanonicalType();
3294 const Type *Ty = QTy.getTypePtrOrNull();
3295 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3296 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3297 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3298 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3299 }
3300 continue;
3301 }
3302 }
3303 if (isa<CXXThisExpr>(E)) {
3304 if (AlignedThis) {
3305 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3306 << 2 << E->getSourceRange();
3307 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3308 << getOpenMPClauseName(OMPC_aligned);
3309 }
3310 AlignedThis = E;
3311 continue;
3312 }
3313 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3314 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3315 }
3316 // The optional parameter of the aligned clause, alignment, must be a constant
3317 // positive integer expression. If no optional parameter is specified,
3318 // implementation-defined default alignments for SIMD instructions on the
3319 // target platforms are assumed.
3320 SmallVector<Expr *, 4> NewAligns;
3321 for (auto *E : Alignments) {
3322 ExprResult Align;
3323 if (E)
3324 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3325 NewAligns.push_back(Align.get());
3326 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00003327 // OpenMP [2.8.2, declare simd construct, Description]
3328 // The linear clause declares one or more list items to be private to a SIMD
3329 // lane and to have a linear relationship with respect to the iteration space
3330 // of a loop.
3331 // The special this pointer can be used as if was one of the arguments to the
3332 // function in any of the linear, aligned, or uniform clauses.
3333 // When a linear-step expression is specified in a linear clause it must be
3334 // either a constant integer expression or an integer-typed parameter that is
3335 // specified in a uniform clause on the directive.
3336 llvm::DenseMap<Decl *, Expr *> LinearArgs;
3337 const bool IsUniformedThis = UniformedLinearThis != nullptr;
3338 auto MI = LinModifiers.begin();
3339 for (auto *E : Linears) {
3340 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3341 ++MI;
3342 E = E->IgnoreParenImpCasts();
3343 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3344 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3345 auto *CanonPVD = PVD->getCanonicalDecl();
3346 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3347 FD->getParamDecl(PVD->getFunctionScopeIndex())
3348 ->getCanonicalDecl() == CanonPVD) {
3349 // OpenMP [2.15.3.7, linear Clause, Restrictions]
3350 // A list-item cannot appear in more than one linear clause.
3351 if (LinearArgs.count(CanonPVD) > 0) {
3352 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3353 << getOpenMPClauseName(OMPC_linear)
3354 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3355 Diag(LinearArgs[CanonPVD]->getExprLoc(),
3356 diag::note_omp_explicit_dsa)
3357 << getOpenMPClauseName(OMPC_linear);
3358 continue;
3359 }
3360 // Each argument can appear in at most one uniform or linear clause.
3361 if (UniformedArgs.count(CanonPVD) > 0) {
3362 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3363 << getOpenMPClauseName(OMPC_linear)
3364 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3365 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3366 diag::note_omp_explicit_dsa)
3367 << getOpenMPClauseName(OMPC_uniform);
3368 continue;
3369 }
3370 LinearArgs[CanonPVD] = E;
3371 if (E->isValueDependent() || E->isTypeDependent() ||
3372 E->isInstantiationDependent() ||
3373 E->containsUnexpandedParameterPack())
3374 continue;
3375 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3376 PVD->getOriginalType());
3377 continue;
3378 }
3379 }
3380 if (isa<CXXThisExpr>(E)) {
3381 if (UniformedLinearThis) {
3382 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3383 << getOpenMPClauseName(OMPC_linear)
3384 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3385 << E->getSourceRange();
3386 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3387 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3388 : OMPC_linear);
3389 continue;
3390 }
3391 UniformedLinearThis = E;
3392 if (E->isValueDependent() || E->isTypeDependent() ||
3393 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3394 continue;
3395 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3396 E->getType());
3397 continue;
3398 }
3399 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3400 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3401 }
3402 Expr *Step = nullptr;
3403 Expr *NewStep = nullptr;
3404 SmallVector<Expr *, 4> NewSteps;
3405 for (auto *E : Steps) {
3406 // Skip the same step expression, it was checked already.
3407 if (Step == E || !E) {
3408 NewSteps.push_back(E ? NewStep : nullptr);
3409 continue;
3410 }
3411 Step = E;
3412 if (auto *DRE = dyn_cast<DeclRefExpr>(Step))
3413 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3414 auto *CanonPVD = PVD->getCanonicalDecl();
3415 if (UniformedArgs.count(CanonPVD) == 0) {
3416 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3417 << Step->getSourceRange();
3418 } else if (E->isValueDependent() || E->isTypeDependent() ||
3419 E->isInstantiationDependent() ||
3420 E->containsUnexpandedParameterPack() ||
3421 CanonPVD->getType()->hasIntegerRepresentation())
3422 NewSteps.push_back(Step);
3423 else {
3424 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3425 << Step->getSourceRange();
3426 }
3427 continue;
3428 }
3429 NewStep = Step;
3430 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3431 !Step->isInstantiationDependent() &&
3432 !Step->containsUnexpandedParameterPack()) {
3433 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3434 .get();
3435 if (NewStep)
3436 NewStep = VerifyIntegerConstantExpression(NewStep).get();
3437 }
3438 NewSteps.push_back(NewStep);
3439 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003440 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3441 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00003442 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00003443 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3444 const_cast<Expr **>(Linears.data()), Linears.size(),
3445 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3446 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003447 ADecl->addAttr(NewAttr);
3448 return ConvertDeclToDeclGroup(ADecl);
3449}
3450
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003451StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3452 Stmt *AStmt,
3453 SourceLocation StartLoc,
3454 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003455 if (!AStmt)
3456 return StmtError();
3457
Alexey Bataev9959db52014-05-06 10:08:46 +00003458 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3459 // 1.2.2 OpenMP Language Terminology
3460 // Structured block - An executable statement with a single entry at the
3461 // top and a single exit at the bottom.
3462 // The point of exit cannot be a branch out of the structured block.
3463 // longjmp() and throw() must not violate the entry/exit criteria.
3464 CS->getCapturedDecl()->setNothrow();
3465
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003466 getCurFunction()->setHasBranchProtectedScope();
3467
Alexey Bataev25e5b442015-09-15 12:52:43 +00003468 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3469 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003470}
3471
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003472namespace {
3473/// \brief Helper class for checking canonical form of the OpenMP loops and
3474/// extracting iteration space of each loop in the loop nest, that will be used
3475/// for IR generation.
3476class OpenMPIterationSpaceChecker {
3477 /// \brief Reference to Sema.
3478 Sema &SemaRef;
3479 /// \brief A location for diagnostics (when there is no some better location).
3480 SourceLocation DefaultLoc;
3481 /// \brief A location for diagnostics (when increment is not compatible).
3482 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003483 /// \brief A source location for referring to loop init later.
3484 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003485 /// \brief A source location for referring to condition later.
3486 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003487 /// \brief A source location for referring to increment later.
3488 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003489 /// \brief Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003490 ValueDecl *LCDecl = nullptr;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003491 /// \brief Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003492 Expr *LCRef = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003493 /// \brief Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003494 Expr *LB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003495 /// \brief Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003496 Expr *UB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003497 /// \brief Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003498 Expr *Step = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003499 /// \brief This flag is true when condition is one of:
3500 /// Var < UB
3501 /// Var <= UB
3502 /// UB > Var
3503 /// UB >= Var
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003504 bool TestIsLessOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003505 /// \brief This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003506 bool TestIsStrictOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003507 /// \brief This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003508 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003509
3510public:
3511 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003512 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003513 /// \brief Check init-expr for canonical loop form and save loop counter
3514 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00003515 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003516 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3517 /// for less/greater and for strict/non-strict comparison.
3518 bool CheckCond(Expr *S);
3519 /// \brief Check incr-expr for canonical loop form and return true if it
3520 /// does not conform, otherwise save loop step (#Step).
3521 bool CheckInc(Expr *S);
3522 /// \brief Return the loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003523 ValueDecl *GetLoopDecl() const { return LCDecl; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003524 /// \brief Return the reference expression to loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003525 Expr *GetLoopDeclRefExpr() const { return LCRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003526 /// \brief Source range of the loop init.
3527 SourceRange GetInitSrcRange() const { return InitSrcRange; }
3528 /// \brief Source range of the loop condition.
3529 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3530 /// \brief Source range of the loop increment.
3531 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3532 /// \brief True if the step should be subtracted.
3533 bool ShouldSubtractStep() const { return SubtractStep; }
3534 /// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003535 Expr *
3536 BuildNumIterations(Scope *S, const bool LimitedType,
3537 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003538 /// \brief Build the precondition expression for the loops.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003539 Expr *BuildPreCond(Scope *S, Expr *Cond,
3540 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003541 /// \brief Build reference expression to the counter be used for codegen.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00003542 DeclRefExpr *BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures,
3543 DSAStackTy &DSA) const;
Alexey Bataeva8899172015-08-06 12:30:57 +00003544 /// \brief Build reference expression to the private counter be used for
3545 /// codegen.
3546 Expr *BuildPrivateCounterVar() const;
David Majnemer9d168222016-08-05 17:44:54 +00003547 /// \brief Build initialization of the counter be used for codegen.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003548 Expr *BuildCounterInit() const;
3549 /// \brief Build step of the counter be used for codegen.
3550 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003551 /// \brief Return true if any expression is dependent.
3552 bool Dependent() const;
3553
3554private:
3555 /// \brief Check the right-hand side of an assignment in the increment
3556 /// expression.
3557 bool CheckIncRHS(Expr *RHS);
3558 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003559 bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003560 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00003561 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003562 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003563 /// \brief Helper to set loop increment.
3564 bool SetStep(Expr *NewStep, bool Subtract);
3565};
3566
3567bool OpenMPIterationSpaceChecker::Dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003568 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003569 assert(!LB && !UB && !Step);
3570 return false;
3571 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003572 return LCDecl->getType()->isDependentType() ||
3573 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
3574 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003575}
3576
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003577bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl,
3578 Expr *NewLCRefExpr,
3579 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003580 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003581 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003582 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003583 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003584 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003585 LCDecl = getCanonicalDecl(NewLCDecl);
3586 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003587 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3588 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003589 if ((Ctor->isCopyOrMoveConstructor() ||
3590 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3591 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003592 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003593 LB = NewLB;
3594 return false;
3595}
3596
3597bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00003598 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003599 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003600 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
3601 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003602 if (!NewUB)
3603 return true;
3604 UB = NewUB;
3605 TestIsLessOp = LessOp;
3606 TestIsStrictOp = StrictOp;
3607 ConditionSrcRange = SR;
3608 ConditionLoc = SL;
3609 return false;
3610}
3611
3612bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3613 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003614 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003615 if (!NewStep)
3616 return true;
3617 if (!NewStep->isValueDependent()) {
3618 // Check that the step is integer expression.
3619 SourceLocation StepLoc = NewStep->getLocStart();
Alexey Bataev5372fb82017-08-31 23:06:52 +00003620 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion(
3621 StepLoc, getExprAsWritten(NewStep));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003622 if (Val.isInvalid())
3623 return true;
3624 NewStep = Val.get();
3625
3626 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3627 // If test-expr is of form var relational-op b and relational-op is < or
3628 // <= then incr-expr must cause var to increase on each iteration of the
3629 // loop. If test-expr is of form var relational-op b and relational-op is
3630 // > or >= then incr-expr must cause var to decrease on each iteration of
3631 // the loop.
3632 // If test-expr is of form b relational-op var and relational-op is < or
3633 // <= then incr-expr must cause var to decrease on each iteration of the
3634 // loop. If test-expr is of form b relational-op var and relational-op is
3635 // > or >= then incr-expr must cause var to increase on each iteration of
3636 // the loop.
3637 llvm::APSInt Result;
3638 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3639 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3640 bool IsConstNeg =
3641 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003642 bool IsConstPos =
3643 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003644 bool IsConstZero = IsConstant && !Result.getBoolValue();
3645 if (UB && (IsConstZero ||
3646 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00003647 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003648 SemaRef.Diag(NewStep->getExprLoc(),
3649 diag::err_omp_loop_incr_not_compatible)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003650 << LCDecl << TestIsLessOp << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003651 SemaRef.Diag(ConditionLoc,
3652 diag::note_omp_loop_cond_requres_compatible_incr)
3653 << TestIsLessOp << ConditionSrcRange;
3654 return true;
3655 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003656 if (TestIsLessOp == Subtract) {
David Majnemer9d168222016-08-05 17:44:54 +00003657 NewStep =
3658 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep)
3659 .get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003660 Subtract = !Subtract;
3661 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003662 }
3663
3664 Step = NewStep;
3665 SubtractStep = Subtract;
3666 return false;
3667}
3668
Alexey Bataev9c821032015-04-30 04:23:23 +00003669bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003670 // Check init-expr for canonical loop form and save loop counter
3671 // variable - #Var and its initialization value - #LB.
3672 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3673 // var = lb
3674 // integer-type var = lb
3675 // random-access-iterator-type var = lb
3676 // pointer-type var = lb
3677 //
3678 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003679 if (EmitDiags) {
3680 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3681 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003682 return true;
3683 }
Tim Shen4a05bb82016-06-21 20:29:17 +00003684 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
3685 if (!ExprTemp->cleanupsHaveSideEffects())
3686 S = ExprTemp->getSubExpr();
3687
Alexander Musmana5f070a2014-10-01 06:03:56 +00003688 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003689 if (Expr *E = dyn_cast<Expr>(S))
3690 S = E->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00003691 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003692 if (BO->getOpcode() == BO_Assign) {
3693 auto *LHS = BO->getLHS()->IgnoreParens();
3694 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
3695 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3696 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3697 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3698 return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
3699 }
3700 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3701 if (ME->isArrow() &&
3702 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3703 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3704 }
3705 }
David Majnemer9d168222016-08-05 17:44:54 +00003706 } else if (auto *DS = dyn_cast<DeclStmt>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003707 if (DS->isSingleDecl()) {
David Majnemer9d168222016-08-05 17:44:54 +00003708 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00003709 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003710 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00003711 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003712 SemaRef.Diag(S->getLocStart(),
3713 diag::ext_omp_loop_not_canonical_init)
3714 << S->getSourceRange();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003715 return SetLCDeclAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003716 }
3717 }
3718 }
David Majnemer9d168222016-08-05 17:44:54 +00003719 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003720 if (CE->getOperator() == OO_Equal) {
3721 auto *LHS = CE->getArg(0);
David Majnemer9d168222016-08-05 17:44:54 +00003722 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003723 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3724 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3725 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3726 return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
3727 }
3728 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3729 if (ME->isArrow() &&
3730 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3731 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3732 }
3733 }
3734 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003735
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003736 if (Dependent() || SemaRef.CurContext->isDependentContext())
3737 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00003738 if (EmitDiags) {
3739 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3740 << S->getSourceRange();
3741 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003742 return true;
3743}
3744
Alexey Bataev23b69422014-06-18 07:08:49 +00003745/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003746/// variable (which may be the loop variable) if possible.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003747static const ValueDecl *GetInitLCDecl(Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003748 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00003749 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003750 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003751 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3752 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003753 if ((Ctor->isCopyOrMoveConstructor() ||
3754 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3755 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003756 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003757 if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
Alexey Bataev4d4624c2017-07-20 16:47:47 +00003758 if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003759 return getCanonicalDecl(VD);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003760 }
3761 if (auto *ME = dyn_cast_or_null<MemberExpr>(E))
3762 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3763 return getCanonicalDecl(ME->getMemberDecl());
3764 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003765}
3766
3767bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3768 // Check test-expr for canonical form, save upper-bound UB, flags for
3769 // less/greater and for strict/non-strict comparison.
3770 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3771 // var relational-op b
3772 // b relational-op var
3773 //
3774 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003775 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003776 return true;
3777 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003778 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003779 SourceLocation CondLoc = S->getLocStart();
David Majnemer9d168222016-08-05 17:44:54 +00003780 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003781 if (BO->isRelationalOp()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003782 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003783 return SetUB(BO->getRHS(),
3784 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3785 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3786 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003787 if (GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003788 return SetUB(BO->getLHS(),
3789 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3790 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3791 BO->getSourceRange(), BO->getOperatorLoc());
3792 }
David Majnemer9d168222016-08-05 17:44:54 +00003793 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003794 if (CE->getNumArgs() == 2) {
3795 auto Op = CE->getOperator();
3796 switch (Op) {
3797 case OO_Greater:
3798 case OO_GreaterEqual:
3799 case OO_Less:
3800 case OO_LessEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003801 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003802 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3803 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3804 CE->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003805 if (GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003806 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3807 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3808 CE->getOperatorLoc());
3809 break;
3810 default:
3811 break;
3812 }
3813 }
3814 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003815 if (Dependent() || SemaRef.CurContext->isDependentContext())
3816 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003817 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003818 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003819 return true;
3820}
3821
3822bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3823 // RHS of canonical loop form increment can be:
3824 // var + incr
3825 // incr + var
3826 // var - incr
3827 //
3828 RHS = RHS->IgnoreParenImpCasts();
David Majnemer9d168222016-08-05 17:44:54 +00003829 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003830 if (BO->isAdditiveOp()) {
3831 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003832 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003833 return SetStep(BO->getRHS(), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003834 if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003835 return SetStep(BO->getLHS(), false);
3836 }
David Majnemer9d168222016-08-05 17:44:54 +00003837 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003838 bool IsAdd = CE->getOperator() == OO_Plus;
3839 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003840 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003841 return SetStep(CE->getArg(1), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003842 if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003843 return SetStep(CE->getArg(0), false);
3844 }
3845 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003846 if (Dependent() || SemaRef.CurContext->isDependentContext())
3847 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003848 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003849 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003850 return true;
3851}
3852
3853bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3854 // Check incr-expr for canonical loop form and return true if it
3855 // does not conform.
3856 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3857 // ++var
3858 // var++
3859 // --var
3860 // var--
3861 // var += incr
3862 // var -= incr
3863 // var = var + incr
3864 // var = incr + var
3865 // var = var - incr
3866 //
3867 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003868 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003869 return true;
3870 }
Tim Shen4a05bb82016-06-21 20:29:17 +00003871 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
3872 if (!ExprTemp->cleanupsHaveSideEffects())
3873 S = ExprTemp->getSubExpr();
3874
Alexander Musmana5f070a2014-10-01 06:03:56 +00003875 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003876 S = S->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00003877 if (auto *UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003878 if (UO->isIncrementDecrementOp() &&
3879 GetInitLCDecl(UO->getSubExpr()) == LCDecl)
David Majnemer9d168222016-08-05 17:44:54 +00003880 return SetStep(SemaRef
3881 .ActOnIntegerConstant(UO->getLocStart(),
3882 (UO->isDecrementOp() ? -1 : 1))
3883 .get(),
3884 false);
3885 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003886 switch (BO->getOpcode()) {
3887 case BO_AddAssign:
3888 case BO_SubAssign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003889 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003890 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3891 break;
3892 case BO_Assign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003893 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003894 return CheckIncRHS(BO->getRHS());
3895 break;
3896 default:
3897 break;
3898 }
David Majnemer9d168222016-08-05 17:44:54 +00003899 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003900 switch (CE->getOperator()) {
3901 case OO_PlusPlus:
3902 case OO_MinusMinus:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003903 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
David Majnemer9d168222016-08-05 17:44:54 +00003904 return SetStep(SemaRef
3905 .ActOnIntegerConstant(
3906 CE->getLocStart(),
3907 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
3908 .get(),
3909 false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003910 break;
3911 case OO_PlusEqual:
3912 case OO_MinusEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003913 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003914 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3915 break;
3916 case OO_Equal:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003917 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003918 return CheckIncRHS(CE->getArg(1));
3919 break;
3920 default:
3921 break;
3922 }
3923 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003924 if (Dependent() || SemaRef.CurContext->isDependentContext())
3925 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003926 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003927 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003928 return true;
3929}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003930
Alexey Bataev5a3af132016-03-29 08:58:54 +00003931static ExprResult
3932tryBuildCapture(Sema &SemaRef, Expr *Capture,
3933 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00003934 if (SemaRef.CurContext->isDependentContext())
3935 return ExprResult(Capture);
Alexey Bataev5a3af132016-03-29 08:58:54 +00003936 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
3937 return SemaRef.PerformImplicitConversion(
3938 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
3939 /*AllowExplicit=*/true);
3940 auto I = Captures.find(Capture);
3941 if (I != Captures.end())
3942 return buildCapture(SemaRef, Capture, I->second);
3943 DeclRefExpr *Ref = nullptr;
3944 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
3945 Captures[Capture] = Ref;
3946 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003947}
3948
Alexander Musmana5f070a2014-10-01 06:03:56 +00003949/// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003950Expr *OpenMPIterationSpaceChecker::BuildNumIterations(
3951 Scope *S, const bool LimitedType,
3952 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003953 ExprResult Diff;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003954 auto VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003955 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003956 SemaRef.getLangOpts().CPlusPlus) {
3957 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003958 auto *UBExpr = TestIsLessOp ? UB : LB;
3959 auto *LBExpr = TestIsLessOp ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00003960 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
3961 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003962 if (!Upper || !Lower)
3963 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003964
3965 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3966
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003967 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003968 // BuildBinOp already emitted error, this one is to point user to upper
3969 // and lower bound, and to tell what is passed to 'operator-'.
3970 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3971 << Upper->getSourceRange() << Lower->getSourceRange();
3972 return nullptr;
3973 }
3974 }
3975
3976 if (!Diff.isUsable())
3977 return nullptr;
3978
3979 // Upper - Lower [- 1]
3980 if (TestIsStrictOp)
3981 Diff = SemaRef.BuildBinOp(
3982 S, DefaultLoc, BO_Sub, Diff.get(),
3983 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3984 if (!Diff.isUsable())
3985 return nullptr;
3986
3987 // Upper - Lower [- 1] + Step
Alexey Bataev5a3af132016-03-29 08:58:54 +00003988 auto NewStep = tryBuildCapture(SemaRef, Step, Captures);
3989 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003990 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003991 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003992 if (!Diff.isUsable())
3993 return nullptr;
3994
3995 // Parentheses (for dumping/debugging purposes only).
3996 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3997 if (!Diff.isUsable())
3998 return nullptr;
3999
4000 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004001 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004002 if (!Diff.isUsable())
4003 return nullptr;
4004
Alexander Musman174b3ca2014-10-06 11:16:29 +00004005 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004006 QualType Type = Diff.get()->getType();
4007 auto &C = SemaRef.Context;
4008 bool UseVarType = VarType->hasIntegerRepresentation() &&
4009 C.getTypeSize(Type) > C.getTypeSize(VarType);
4010 if (!Type->isIntegerType() || UseVarType) {
4011 unsigned NewSize =
4012 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4013 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4014 : Type->hasSignedIntegerRepresentation();
4015 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00004016 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4017 Diff = SemaRef.PerformImplicitConversion(
4018 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4019 if (!Diff.isUsable())
4020 return nullptr;
4021 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004022 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004023 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00004024 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4025 if (NewSize != C.getTypeSize(Type)) {
4026 if (NewSize < C.getTypeSize(Type)) {
4027 assert(NewSize == 64 && "incorrect loop var size");
4028 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4029 << InitSrcRange << ConditionSrcRange;
4030 }
4031 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004032 NewSize, Type->hasSignedIntegerRepresentation() ||
4033 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00004034 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4035 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4036 Sema::AA_Converting, true);
4037 if (!Diff.isUsable())
4038 return nullptr;
4039 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004040 }
4041 }
4042
Alexander Musmana5f070a2014-10-01 06:03:56 +00004043 return Diff.get();
4044}
4045
Alexey Bataev5a3af132016-03-29 08:58:54 +00004046Expr *OpenMPIterationSpaceChecker::BuildPreCond(
4047 Scope *S, Expr *Cond,
4048 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004049 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4050 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4051 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004052
Alexey Bataev5a3af132016-03-29 08:58:54 +00004053 auto NewLB = tryBuildCapture(SemaRef, LB, Captures);
4054 auto NewUB = tryBuildCapture(SemaRef, UB, Captures);
4055 if (!NewLB.isUsable() || !NewUB.isUsable())
4056 return nullptr;
4057
Alexey Bataev62dbb972015-04-22 11:59:37 +00004058 auto CondExpr = SemaRef.BuildBinOp(
4059 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
4060 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004061 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004062 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004063 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4064 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00004065 CondExpr = SemaRef.PerformImplicitConversion(
4066 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4067 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004068 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00004069 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4070 // Otherwise use original loop conditon and evaluate it in runtime.
4071 return CondExpr.isUsable() ? CondExpr.get() : Cond;
4072}
4073
Alexander Musmana5f070a2014-10-01 06:03:56 +00004074/// \brief Build reference expression to the counter be used for codegen.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004075DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar(
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004076 llvm::MapVector<Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004077 auto *VD = dyn_cast<VarDecl>(LCDecl);
4078 if (!VD) {
4079 VD = SemaRef.IsOpenMPCapturedDecl(LCDecl);
4080 auto *Ref = buildDeclRefExpr(
4081 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004082 DSAStackTy::DSAVarData Data = DSA.getTopDSA(LCDecl, /*FromParent=*/false);
4083 // If the loop control decl is explicitly marked as private, do not mark it
4084 // as captured again.
4085 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4086 Captures.insert(std::make_pair(LCRef, Ref));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004087 return Ref;
4088 }
4089 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00004090 DefaultLoc);
4091}
4092
4093Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004094 if (LCDecl && !LCDecl->isInvalidDecl()) {
4095 auto Type = LCDecl->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00004096 auto *PrivateVar =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004097 buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(),
4098 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00004099 if (PrivateVar->isInvalidDecl())
4100 return nullptr;
4101 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4102 }
4103 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004104}
4105
Samuel Antao4c8035b2016-12-12 18:00:20 +00004106/// \brief Build initialization of the counter to be used for codegen.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004107Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
4108
4109/// \brief Build step of the counter be used for codegen.
4110Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
4111
4112/// \brief Iteration space of a single for loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004113struct LoopIterationSpace final {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004114 /// \brief Condition of the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004115 Expr *PreCond = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004116 /// \brief This expression calculates the number of iterations in the loop.
4117 /// It is always possible to calculate it before starting the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004118 Expr *NumIterations = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004119 /// \brief The loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004120 Expr *CounterVar = nullptr;
Alexey Bataeva8899172015-08-06 12:30:57 +00004121 /// \brief Private loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004122 Expr *PrivateCounterVar = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004123 /// \brief This is initializer for the initial value of #CounterVar.
Alexey Bataev8b427062016-05-25 12:36:08 +00004124 Expr *CounterInit = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004125 /// \brief This is step for the #CounterVar used to generate its update:
4126 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
Alexey Bataev8b427062016-05-25 12:36:08 +00004127 Expr *CounterStep = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004128 /// \brief Should step be subtracted?
Alexey Bataev8b427062016-05-25 12:36:08 +00004129 bool Subtract = false;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004130 /// \brief Source range of the loop init.
4131 SourceRange InitSrcRange;
4132 /// \brief Source range of the loop condition.
4133 SourceRange CondSrcRange;
4134 /// \brief Source range of the loop increment.
4135 SourceRange IncSrcRange;
4136};
4137
Alexey Bataev23b69422014-06-18 07:08:49 +00004138} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004139
Alexey Bataev9c821032015-04-30 04:23:23 +00004140void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4141 assert(getLangOpts().OpenMP && "OpenMP is not active.");
4142 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004143 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4144 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00004145 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4146 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004147 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
4148 if (auto *D = ISC.GetLoopDecl()) {
4149 auto *VD = dyn_cast<VarDecl>(D);
4150 if (!VD) {
4151 if (auto *Private = IsOpenMPCapturedDecl(D))
4152 VD = Private;
4153 else {
4154 auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(),
4155 /*WithInit=*/false);
4156 VD = cast<VarDecl>(Ref->getDecl());
4157 }
4158 }
4159 DSAStack->addLoopControlVariable(D, VD);
4160 }
4161 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004162 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00004163 }
4164}
4165
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004166/// \brief Called on a for stmt to check and extract its iteration space
4167/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00004168static bool CheckOpenMPIterationSpace(
4169 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4170 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004171 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004172 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004173 LoopIterationSpace &ResultIterSpace,
4174 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004175 // OpenMP [2.6, Canonical Loop Form]
4176 // for (init-expr; test-expr; incr-expr) structured-block
David Majnemer9d168222016-08-05 17:44:54 +00004177 auto *For = dyn_cast_or_null<ForStmt>(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004178 if (!For) {
4179 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004180 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
4181 << getOpenMPDirectiveName(DKind) << NestedLoopCount
4182 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
4183 if (NestedLoopCount > 1) {
4184 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4185 SemaRef.Diag(DSA.getConstructLoc(),
4186 diag::note_omp_collapse_ordered_expr)
4187 << 2 << CollapseLoopCountExpr->getSourceRange()
4188 << OrderedLoopCountExpr->getSourceRange();
4189 else if (CollapseLoopCountExpr)
4190 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4191 diag::note_omp_collapse_ordered_expr)
4192 << 0 << CollapseLoopCountExpr->getSourceRange();
4193 else
4194 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4195 diag::note_omp_collapse_ordered_expr)
4196 << 1 << OrderedLoopCountExpr->getSourceRange();
4197 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004198 return true;
4199 }
4200 assert(For->getBody());
4201
4202 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4203
4204 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00004205 auto Init = For->getInit();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004206 if (ISC.CheckInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004207 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004208
4209 bool HasErrors = false;
4210
4211 // Check loop variable's type.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004212 if (auto *LCDecl = ISC.GetLoopDecl()) {
4213 auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004214
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004215 // OpenMP [2.6, Canonical Loop Form]
4216 // Var is one of the following:
4217 // A variable of signed or unsigned integer type.
4218 // For C++, a variable of a random access iterator type.
4219 // For C, a variable of a pointer type.
4220 auto VarType = LCDecl->getType().getNonReferenceType();
4221 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4222 !VarType->isPointerType() &&
4223 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
4224 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
4225 << SemaRef.getLangOpts().CPlusPlus;
4226 HasErrors = true;
4227 }
4228
4229 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4230 // a Construct
4231 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4232 // parallel for construct is (are) private.
4233 // The loop iteration variable in the associated for-loop of a simd
4234 // construct with just one associated for-loop is linear with a
4235 // constant-linear-step that is the increment of the associated for-loop.
4236 // Exclude loop var from the list of variables with implicitly defined data
4237 // sharing attributes.
4238 VarsWithImplicitDSA.erase(LCDecl);
4239
4240 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4241 // in a Construct, C/C++].
4242 // The loop iteration variable in the associated for-loop of a simd
4243 // construct with just one associated for-loop may be listed in a linear
4244 // clause with a constant-linear-step that is the increment of the
4245 // associated for-loop.
4246 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4247 // parallel for construct may be listed in a private or lastprivate clause.
4248 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4249 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4250 // declared in the loop and it is predetermined as a private.
4251 auto PredeterminedCKind =
4252 isOpenMPSimdDirective(DKind)
4253 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4254 : OMPC_private;
4255 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4256 DVar.CKind != PredeterminedCKind) ||
4257 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4258 isOpenMPDistributeDirective(DKind)) &&
4259 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4260 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4261 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4262 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
4263 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4264 << getOpenMPClauseName(PredeterminedCKind);
4265 if (DVar.RefExpr == nullptr)
4266 DVar.CKind = PredeterminedCKind;
4267 ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
4268 HasErrors = true;
4269 } else if (LoopDeclRefExpr != nullptr) {
4270 // Make the loop iteration variable private (for worksharing constructs),
4271 // linear (for simd directives with the only one associated loop) or
4272 // lastprivate (for simd directives with several collapsed or ordered
4273 // loops).
4274 if (DVar.CKind == OMPC_unknown)
Alexey Bataev7ace49d2016-05-17 08:55:33 +00004275 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
4276 [](OpenMPDirectiveKind) -> bool { return true; },
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004277 /*FromParent=*/false);
4278 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4279 }
4280
4281 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4282
4283 // Check test-expr.
4284 HasErrors |= ISC.CheckCond(For->getCond());
4285
4286 // Check incr-expr.
4287 HasErrors |= ISC.CheckInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004288 }
4289
Alexander Musmana5f070a2014-10-01 06:03:56 +00004290 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004291 return HasErrors;
4292
Alexander Musmana5f070a2014-10-01 06:03:56 +00004293 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004294 ResultIterSpace.PreCond =
4295 ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures);
Alexander Musman174b3ca2014-10-06 11:16:29 +00004296 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004297 DSA.getCurScope(),
4298 (isOpenMPWorksharingDirective(DKind) ||
4299 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4300 Captures);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004301 ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures, DSA);
Alexey Bataeva8899172015-08-06 12:30:57 +00004302 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004303 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
4304 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
4305 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
4306 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4307 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4308 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4309
Alexey Bataev62dbb972015-04-22 11:59:37 +00004310 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4311 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004312 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004313 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004314 ResultIterSpace.CounterInit == nullptr ||
4315 ResultIterSpace.CounterStep == nullptr);
4316
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004317 return HasErrors;
4318}
4319
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004320/// \brief Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004321static ExprResult
4322BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4323 ExprResult Start,
4324 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004325 // Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004326 auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
4327 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004328 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004329 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00004330 VarRef.get()->getType())) {
4331 NewStart = SemaRef.PerformImplicitConversion(
4332 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4333 /*AllowExplicit=*/true);
4334 if (!NewStart.isUsable())
4335 return ExprError();
4336 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004337
4338 auto Init =
4339 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4340 return Init;
4341}
4342
Alexander Musmana5f070a2014-10-01 06:03:56 +00004343/// \brief Build 'VarRef = Start + Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004344static ExprResult
4345BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc,
4346 ExprResult VarRef, ExprResult Start, ExprResult Iter,
4347 ExprResult Step, bool Subtract,
4348 llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004349 // Add parentheses (for debugging purposes only).
4350 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4351 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4352 !Step.isUsable())
4353 return ExprError();
4354
Alexey Bataev5a3af132016-03-29 08:58:54 +00004355 ExprResult NewStep = Step;
4356 if (Captures)
4357 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004358 if (NewStep.isInvalid())
4359 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004360 ExprResult Update =
4361 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004362 if (!Update.isUsable())
4363 return ExprError();
4364
Alexey Bataevc0214e02016-02-16 12:13:49 +00004365 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4366 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004367 ExprResult NewStart = Start;
4368 if (Captures)
4369 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004370 if (NewStart.isInvalid())
4371 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004372
Alexey Bataevc0214e02016-02-16 12:13:49 +00004373 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4374 ExprResult SavedUpdate = Update;
4375 ExprResult UpdateVal;
4376 if (VarRef.get()->getType()->isOverloadableType() ||
4377 NewStart.get()->getType()->isOverloadableType() ||
4378 Update.get()->getType()->isOverloadableType()) {
4379 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4380 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4381 Update =
4382 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4383 if (Update.isUsable()) {
4384 UpdateVal =
4385 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4386 VarRef.get(), SavedUpdate.get());
4387 if (UpdateVal.isUsable()) {
4388 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4389 UpdateVal.get());
4390 }
4391 }
4392 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4393 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004394
Alexey Bataevc0214e02016-02-16 12:13:49 +00004395 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4396 if (!Update.isUsable() || !UpdateVal.isUsable()) {
4397 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4398 NewStart.get(), SavedUpdate.get());
4399 if (!Update.isUsable())
4400 return ExprError();
4401
Alexey Bataev11481f52016-02-17 10:29:05 +00004402 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4403 VarRef.get()->getType())) {
4404 Update = SemaRef.PerformImplicitConversion(
4405 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4406 if (!Update.isUsable())
4407 return ExprError();
4408 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00004409
4410 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4411 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004412 return Update;
4413}
4414
4415/// \brief Convert integer expression \a E to make it have at least \a Bits
4416/// bits.
David Majnemer9d168222016-08-05 17:44:54 +00004417static ExprResult WidenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004418 if (E == nullptr)
4419 return ExprError();
4420 auto &C = SemaRef.Context;
4421 QualType OldType = E->getType();
4422 unsigned HasBits = C.getTypeSize(OldType);
4423 if (HasBits >= Bits)
4424 return ExprResult(E);
4425 // OK to convert to signed, because new type has more bits than old.
4426 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4427 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4428 true);
4429}
4430
4431/// \brief Check if the given expression \a E is a constant integer that fits
4432/// into \a Bits bits.
4433static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4434 if (E == nullptr)
4435 return false;
4436 llvm::APSInt Result;
4437 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4438 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4439 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004440}
4441
Alexey Bataev5a3af132016-03-29 08:58:54 +00004442/// Build preinits statement for the given declarations.
4443static Stmt *buildPreInits(ASTContext &Context,
Alexey Bataevc5514062017-10-25 15:44:52 +00004444 MutableArrayRef<Decl *> PreInits) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004445 if (!PreInits.empty()) {
4446 return new (Context) DeclStmt(
4447 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
4448 SourceLocation(), SourceLocation());
4449 }
4450 return nullptr;
4451}
4452
4453/// Build preinits statement for the given declarations.
Alexey Bataevc5514062017-10-25 15:44:52 +00004454static Stmt *
4455buildPreInits(ASTContext &Context,
4456 const llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004457 if (!Captures.empty()) {
4458 SmallVector<Decl *, 16> PreInits;
4459 for (auto &Pair : Captures)
4460 PreInits.push_back(Pair.second->getDecl());
4461 return buildPreInits(Context, PreInits);
4462 }
4463 return nullptr;
4464}
4465
4466/// Build postupdate expression for the given list of postupdates expressions.
4467static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
4468 Expr *PostUpdate = nullptr;
4469 if (!PostUpdates.empty()) {
4470 for (auto *E : PostUpdates) {
4471 Expr *ConvE = S.BuildCStyleCastExpr(
4472 E->getExprLoc(),
4473 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
4474 E->getExprLoc(), E)
4475 .get();
4476 PostUpdate = PostUpdate
4477 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
4478 PostUpdate, ConvE)
4479 .get()
4480 : ConvE;
4481 }
4482 }
4483 return PostUpdate;
4484}
4485
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004486/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00004487/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4488/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004489static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00004490CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
4491 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4492 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004493 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00004494 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004495 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004496 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004497 // Found 'collapse' clause - calculate collapse number.
4498 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004499 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004500 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004501 }
4502 if (OrderedLoopCountExpr) {
4503 // Found 'ordered' clause - calculate collapse number.
4504 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004505 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4506 if (Result.getLimitedValue() < NestedLoopCount) {
4507 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4508 diag::err_omp_wrong_ordered_loop_count)
4509 << OrderedLoopCountExpr->getSourceRange();
4510 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4511 diag::note_collapse_loop_count)
4512 << CollapseLoopCountExpr->getSourceRange();
4513 }
4514 NestedLoopCount = Result.getLimitedValue();
4515 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004516 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004517 // This is helper routine for loop directives (e.g., 'for', 'simd',
4518 // 'for simd', etc.).
Alexey Bataev5a3af132016-03-29 08:58:54 +00004519 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004520 SmallVector<LoopIterationSpace, 4> IterSpaces;
4521 IterSpaces.resize(NestedLoopCount);
4522 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004523 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004524 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004525 NestedLoopCount, CollapseLoopCountExpr,
4526 OrderedLoopCountExpr, VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004527 IterSpaces[Cnt], Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004528 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004529 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004530 // OpenMP [2.8.1, simd construct, Restrictions]
4531 // All loops associated with the construct must be perfectly nested; that
4532 // is, there must be no intervening code nor any OpenMP directive between
4533 // any two loops.
4534 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004535 }
4536
Alexander Musmana5f070a2014-10-01 06:03:56 +00004537 Built.clear(/* size */ NestedLoopCount);
4538
4539 if (SemaRef.CurContext->isDependentContext())
4540 return NestedLoopCount;
4541
4542 // An example of what is generated for the following code:
4543 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004544 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004545 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004546 // for (k = 0; k < NK; ++k)
4547 // for (j = J0; j < NJ; j+=2) {
4548 // <loop body>
4549 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004550 //
4551 // We generate the code below.
4552 // Note: the loop body may be outlined in CodeGen.
4553 // Note: some counters may be C++ classes, operator- is used to find number of
4554 // iterations and operator+= to calculate counter value.
4555 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4556 // or i64 is currently supported).
4557 //
4558 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4559 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4560 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4561 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4562 // // similar updates for vars in clauses (e.g. 'linear')
4563 // <loop body (using local i and j)>
4564 // }
4565 // i = NI; // assign final values of counters
4566 // j = NJ;
4567 //
4568
4569 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4570 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004571 // Precondition tests if there is at least one iteration (all conditions are
4572 // true).
4573 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004574 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004575 ExprResult LastIteration32 = WidenIterationCount(
David Majnemer9d168222016-08-05 17:44:54 +00004576 32 /* Bits */, SemaRef
4577 .PerformImplicitConversion(
4578 N0->IgnoreImpCasts(), N0->getType(),
4579 Sema::AA_Converting, /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004580 .get(),
4581 SemaRef);
4582 ExprResult LastIteration64 = WidenIterationCount(
David Majnemer9d168222016-08-05 17:44:54 +00004583 64 /* Bits */, SemaRef
4584 .PerformImplicitConversion(
4585 N0->IgnoreImpCasts(), N0->getType(),
4586 Sema::AA_Converting, /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004587 .get(),
4588 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004589
4590 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4591 return NestedLoopCount;
4592
4593 auto &C = SemaRef.Context;
4594 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4595
4596 Scope *CurScope = DSA.getCurScope();
4597 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004598 if (PreCond.isUsable()) {
Alexey Bataeva7206b92016-12-20 16:51:02 +00004599 PreCond =
4600 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd,
4601 PreCond.get(), IterSpaces[Cnt].PreCond);
Alexey Bataev62dbb972015-04-22 11:59:37 +00004602 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004603 auto N = IterSpaces[Cnt].NumIterations;
Alexey Bataeva7206b92016-12-20 16:51:02 +00004604 SourceLocation Loc = N->getExprLoc();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004605 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4606 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004607 LastIteration32 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00004608 CurScope, Loc, BO_Mul, LastIteration32.get(),
David Majnemer9d168222016-08-05 17:44:54 +00004609 SemaRef
4610 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4611 Sema::AA_Converting,
4612 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004613 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004614 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004615 LastIteration64 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00004616 CurScope, Loc, BO_Mul, LastIteration64.get(),
David Majnemer9d168222016-08-05 17:44:54 +00004617 SemaRef
4618 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4619 Sema::AA_Converting,
4620 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004621 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004622 }
4623
4624 // Choose either the 32-bit or 64-bit version.
4625 ExprResult LastIteration = LastIteration64;
4626 if (LastIteration32.isUsable() &&
4627 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4628 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4629 FitsInto(
4630 32 /* Bits */,
4631 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4632 LastIteration64.get(), SemaRef)))
4633 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00004634 QualType VType = LastIteration.get()->getType();
4635 QualType RealVType = VType;
4636 QualType StrideVType = VType;
4637 if (isOpenMPTaskLoopDirective(DKind)) {
4638 VType =
4639 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
4640 StrideVType =
4641 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
4642 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004643
4644 if (!LastIteration.isUsable())
4645 return 0;
4646
4647 // Save the number of iterations.
4648 ExprResult NumIterations = LastIteration;
4649 {
4650 LastIteration = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00004651 CurScope, LastIteration.get()->getExprLoc(), BO_Sub,
4652 LastIteration.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00004653 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4654 if (!LastIteration.isUsable())
4655 return 0;
4656 }
4657
4658 // Calculate the last iteration number beforehand instead of doing this on
4659 // each iteration. Do not do this if the number of iterations may be kfold-ed.
4660 llvm::APSInt Result;
4661 bool IsConstant =
4662 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4663 ExprResult CalcLastIteration;
4664 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004665 ExprResult SaveRef =
4666 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004667 LastIteration = SaveRef;
4668
4669 // Prepare SaveRef + 1.
4670 NumIterations = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00004671 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00004672 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4673 if (!NumIterations.isUsable())
4674 return 0;
4675 }
4676
4677 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4678
David Majnemer9d168222016-08-05 17:44:54 +00004679 // Build variables passed into runtime, necessary for worksharing directives.
Carlo Bertolliffafe102017-04-20 00:39:39 +00004680 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004681 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4682 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004683 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004684 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4685 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00004686 SemaRef.AddInitializerToDecl(LBDecl,
4687 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4688 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00004689
4690 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004691 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4692 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004693 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
Richard Smith3beb7c62017-01-12 02:27:38 +00004694 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00004695
4696 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4697 // This will be used to implement clause 'lastprivate'.
4698 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00004699 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4700 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00004701 SemaRef.AddInitializerToDecl(ILDecl,
4702 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4703 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00004704
4705 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00004706 VarDecl *STDecl =
4707 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
4708 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00004709 SemaRef.AddInitializerToDecl(STDecl,
4710 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4711 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00004712
4713 // Build expression: UB = min(UB, LastIteration)
David Majnemer9d168222016-08-05 17:44:54 +00004714 // It is necessary for CodeGen of directives with static scheduling.
Alexander Musmanc6388682014-12-15 07:07:06 +00004715 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4716 UB.get(), LastIteration.get());
4717 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4718 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4719 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4720 CondOp.get());
4721 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
Carlo Bertolli9925f152016-06-27 14:55:37 +00004722
4723 // If we have a combined directive that combines 'distribute', 'for' or
4724 // 'simd' we need to be able to access the bounds of the schedule of the
4725 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
4726 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
4727 if (isOpenMPLoopBoundSharingDirective(DKind)) {
Carlo Bertolli9925f152016-06-27 14:55:37 +00004728
Carlo Bertolliffafe102017-04-20 00:39:39 +00004729 // Lower bound variable, initialized with zero.
4730 VarDecl *CombLBDecl =
4731 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb");
4732 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc);
4733 SemaRef.AddInitializerToDecl(
4734 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4735 /*DirectInit*/ false);
4736
4737 // Upper bound variable, initialized with last iteration number.
4738 VarDecl *CombUBDecl =
4739 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub");
4740 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc);
4741 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(),
4742 /*DirectInit*/ false);
4743
4744 ExprResult CombIsUBGreater = SemaRef.BuildBinOp(
4745 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get());
4746 ExprResult CombCondOp =
4747 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(),
4748 LastIteration.get(), CombUB.get());
4749 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(),
4750 CombCondOp.get());
4751 CombEUB = SemaRef.ActOnFinishFullExpr(CombEUB.get());
4752
4753 auto *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
Carlo Bertolli9925f152016-06-27 14:55:37 +00004754 // We expect to have at least 2 more parameters than the 'parallel'
4755 // directive does - the lower and upper bounds of the previous schedule.
4756 assert(CD->getNumParams() >= 4 &&
4757 "Unexpected number of parameters in loop combined directive");
4758
4759 // Set the proper type for the bounds given what we learned from the
4760 // enclosed loops.
4761 auto *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
4762 auto *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
4763
4764 // Previous lower and upper bounds are obtained from the region
4765 // parameters.
4766 PrevLB =
4767 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
4768 PrevUB =
4769 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
4770 }
Alexander Musmanc6388682014-12-15 07:07:06 +00004771 }
4772
4773 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004774 ExprResult IV;
Carlo Bertolliffafe102017-04-20 00:39:39 +00004775 ExprResult Init, CombInit;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004776 {
Alexey Bataev7292c292016-04-25 12:22:29 +00004777 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
4778 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
David Majnemer9d168222016-08-05 17:44:54 +00004779 Expr *RHS =
4780 (isOpenMPWorksharingDirective(DKind) ||
4781 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
4782 ? LB.get()
4783 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004784 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4785 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Carlo Bertolliffafe102017-04-20 00:39:39 +00004786
4787 if (isOpenMPLoopBoundSharingDirective(DKind)) {
4788 Expr *CombRHS =
4789 (isOpenMPWorksharingDirective(DKind) ||
4790 isOpenMPTaskLoopDirective(DKind) ||
4791 isOpenMPDistributeDirective(DKind))
4792 ? CombLB.get()
4793 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4794 CombInit =
4795 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS);
4796 CombInit = SemaRef.ActOnFinishFullExpr(CombInit.get());
4797 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004798 }
4799
Alexander Musmanc6388682014-12-15 07:07:06 +00004800 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004801 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00004802 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004803 (isOpenMPWorksharingDirective(DKind) ||
4804 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004805 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4806 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4807 NumIterations.get());
Carlo Bertolliffafe102017-04-20 00:39:39 +00004808 ExprResult CombCond;
4809 if (isOpenMPLoopBoundSharingDirective(DKind)) {
4810 CombCond =
4811 SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), CombUB.get());
4812 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004813 // Loop increment (IV = IV + 1)
4814 SourceLocation IncLoc;
4815 ExprResult Inc =
4816 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4817 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4818 if (!Inc.isUsable())
4819 return 0;
4820 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00004821 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4822 if (!Inc.isUsable())
4823 return 0;
4824
4825 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4826 // Used for directives with static scheduling.
Carlo Bertolliffafe102017-04-20 00:39:39 +00004827 // In combined construct, add combined version that use CombLB and CombUB
4828 // base variables for the update
4829 ExprResult NextLB, NextUB, CombNextLB, CombNextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004830 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4831 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004832 // LB + ST
4833 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4834 if (!NextLB.isUsable())
4835 return 0;
4836 // LB = LB + ST
4837 NextLB =
4838 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4839 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4840 if (!NextLB.isUsable())
4841 return 0;
4842 // UB + ST
4843 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4844 if (!NextUB.isUsable())
4845 return 0;
4846 // UB = UB + ST
4847 NextUB =
4848 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4849 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4850 if (!NextUB.isUsable())
4851 return 0;
Carlo Bertolliffafe102017-04-20 00:39:39 +00004852 if (isOpenMPLoopBoundSharingDirective(DKind)) {
4853 CombNextLB =
4854 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get());
4855 if (!NextLB.isUsable())
4856 return 0;
4857 // LB = LB + ST
4858 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(),
4859 CombNextLB.get());
4860 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get());
4861 if (!CombNextLB.isUsable())
4862 return 0;
4863 // UB + ST
4864 CombNextUB =
4865 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get());
4866 if (!CombNextUB.isUsable())
4867 return 0;
4868 // UB = UB + ST
4869 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(),
4870 CombNextUB.get());
4871 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get());
4872 if (!CombNextUB.isUsable())
4873 return 0;
4874 }
Alexander Musmanc6388682014-12-15 07:07:06 +00004875 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004876
Carlo Bertolliffafe102017-04-20 00:39:39 +00004877 // Create increment expression for distribute loop when combined in a same
Carlo Bertolli8429d812017-02-17 21:29:13 +00004878 // directive with for as IV = IV + ST; ensure upper bound expression based
4879 // on PrevUB instead of NumIterations - used to implement 'for' when found
4880 // in combination with 'distribute', like in 'distribute parallel for'
4881 SourceLocation DistIncLoc;
4882 ExprResult DistCond, DistInc, PrevEUB;
4883 if (isOpenMPLoopBoundSharingDirective(DKind)) {
4884 DistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get());
4885 assert(DistCond.isUsable() && "distribute cond expr was not built");
4886
4887 DistInc =
4888 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get());
4889 assert(DistInc.isUsable() && "distribute inc expr was not built");
4890 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(),
4891 DistInc.get());
4892 DistInc = SemaRef.ActOnFinishFullExpr(DistInc.get());
4893 assert(DistInc.isUsable() && "distribute inc expr was not built");
4894
4895 // Build expression: UB = min(UB, prevUB) for #for in composite or combined
4896 // construct
4897 SourceLocation DistEUBLoc;
4898 ExprResult IsUBGreater =
4899 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get());
4900 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4901 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get());
4902 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(),
4903 CondOp.get());
4904 PrevEUB = SemaRef.ActOnFinishFullExpr(PrevEUB.get());
4905 }
4906
Alexander Musmana5f070a2014-10-01 06:03:56 +00004907 // Build updates and final values of the loop counters.
4908 bool HasErrors = false;
4909 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004910 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004911 Built.Updates.resize(NestedLoopCount);
4912 Built.Finals.resize(NestedLoopCount);
Alexey Bataev8b427062016-05-25 12:36:08 +00004913 SmallVector<Expr *, 4> LoopMultipliers;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004914 {
4915 ExprResult Div;
4916 // Go from inner nested loop to outer.
4917 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4918 LoopIterationSpace &IS = IterSpaces[Cnt];
4919 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4920 // Build: Iter = (IV / Div) % IS.NumIters
4921 // where Div is product of previous iterations' IS.NumIters.
4922 ExprResult Iter;
4923 if (Div.isUsable()) {
4924 Iter =
4925 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4926 } else {
4927 Iter = IV;
4928 assert((Cnt == (int)NestedLoopCount - 1) &&
4929 "unusable div expected on first iteration only");
4930 }
4931
4932 if (Cnt != 0 && Iter.isUsable())
4933 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4934 IS.NumIterations);
4935 if (!Iter.isUsable()) {
4936 HasErrors = true;
4937 break;
4938 }
4939
Alexey Bataev39f915b82015-05-08 10:41:21 +00004940 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004941 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
4942 auto *CounterVar = buildDeclRefExpr(SemaRef, VD, IS.CounterVar->getType(),
4943 IS.CounterVar->getExprLoc(),
4944 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004945 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004946 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004947 if (!Init.isUsable()) {
4948 HasErrors = true;
4949 break;
4950 }
Alexey Bataev5a3af132016-03-29 08:58:54 +00004951 ExprResult Update = BuildCounterUpdate(
4952 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
4953 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004954 if (!Update.isUsable()) {
4955 HasErrors = true;
4956 break;
4957 }
4958
4959 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4960 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00004961 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004962 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004963 if (!Final.isUsable()) {
4964 HasErrors = true;
4965 break;
4966 }
4967
4968 // Build Div for the next iteration: Div <- Div * IS.NumIters
4969 if (Cnt != 0) {
4970 if (Div.isUnset())
4971 Div = IS.NumIterations;
4972 else
4973 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4974 IS.NumIterations);
4975
4976 // Add parentheses (for debugging purposes only).
4977 if (Div.isUsable())
Alexey Bataev8b427062016-05-25 12:36:08 +00004978 Div = tryBuildCapture(SemaRef, Div.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004979 if (!Div.isUsable()) {
4980 HasErrors = true;
4981 break;
4982 }
Alexey Bataev8b427062016-05-25 12:36:08 +00004983 LoopMultipliers.push_back(Div.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004984 }
4985 if (!Update.isUsable() || !Final.isUsable()) {
4986 HasErrors = true;
4987 break;
4988 }
4989 // Save results
4990 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004991 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004992 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004993 Built.Updates[Cnt] = Update.get();
4994 Built.Finals[Cnt] = Final.get();
4995 }
4996 }
4997
4998 if (HasErrors)
4999 return 0;
5000
5001 // Save results
5002 Built.IterationVarRef = IV.get();
5003 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00005004 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00005005 Built.CalcLastIteration =
5006 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005007 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00005008 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005009 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005010 Built.Init = Init.get();
5011 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005012 Built.LB = LB.get();
5013 Built.UB = UB.get();
5014 Built.IL = IL.get();
5015 Built.ST = ST.get();
5016 Built.EUB = EUB.get();
5017 Built.NLB = NextLB.get();
5018 Built.NUB = NextUB.get();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005019 Built.PrevLB = PrevLB.get();
5020 Built.PrevUB = PrevUB.get();
Carlo Bertolli8429d812017-02-17 21:29:13 +00005021 Built.DistInc = DistInc.get();
5022 Built.PrevEUB = PrevEUB.get();
Carlo Bertolliffafe102017-04-20 00:39:39 +00005023 Built.DistCombinedFields.LB = CombLB.get();
5024 Built.DistCombinedFields.UB = CombUB.get();
5025 Built.DistCombinedFields.EUB = CombEUB.get();
5026 Built.DistCombinedFields.Init = CombInit.get();
5027 Built.DistCombinedFields.Cond = CombCond.get();
5028 Built.DistCombinedFields.NLB = CombNextLB.get();
5029 Built.DistCombinedFields.NUB = CombNextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005030
Alexey Bataev8b427062016-05-25 12:36:08 +00005031 Expr *CounterVal = SemaRef.DefaultLvalueConversion(IV.get()).get();
5032 // Fill data for doacross depend clauses.
5033 for (auto Pair : DSA.getDoacrossDependClauses()) {
5034 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
5035 Pair.first->setCounterValue(CounterVal);
5036 else {
5037 if (NestedLoopCount != Pair.second.size() ||
5038 NestedLoopCount != LoopMultipliers.size() + 1) {
5039 // Erroneous case - clause has some problems.
5040 Pair.first->setCounterValue(CounterVal);
5041 continue;
5042 }
5043 assert(Pair.first->getDependencyKind() == OMPC_DEPEND_sink);
5044 auto I = Pair.second.rbegin();
5045 auto IS = IterSpaces.rbegin();
5046 auto ILM = LoopMultipliers.rbegin();
5047 Expr *UpCounterVal = CounterVal;
5048 Expr *Multiplier = nullptr;
5049 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5050 if (I->first) {
5051 assert(IS->CounterStep);
5052 Expr *NormalizedOffset =
5053 SemaRef
5054 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Div,
5055 I->first, IS->CounterStep)
5056 .get();
5057 if (Multiplier) {
5058 NormalizedOffset =
5059 SemaRef
5060 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Mul,
5061 NormalizedOffset, Multiplier)
5062 .get();
5063 }
5064 assert(I->second == OO_Plus || I->second == OO_Minus);
5065 BinaryOperatorKind BOK = (I->second == OO_Plus) ? BO_Add : BO_Sub;
David Majnemer9d168222016-08-05 17:44:54 +00005066 UpCounterVal = SemaRef
5067 .BuildBinOp(CurScope, I->first->getExprLoc(), BOK,
5068 UpCounterVal, NormalizedOffset)
5069 .get();
Alexey Bataev8b427062016-05-25 12:36:08 +00005070 }
5071 Multiplier = *ILM;
5072 ++I;
5073 ++IS;
5074 ++ILM;
5075 }
5076 Pair.first->setCounterValue(UpCounterVal);
5077 }
5078 }
5079
Alexey Bataevabfc0692014-06-25 06:52:00 +00005080 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005081}
5082
Alexey Bataev10e775f2015-07-30 11:36:16 +00005083static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005084 auto CollapseClauses =
5085 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
5086 if (CollapseClauses.begin() != CollapseClauses.end())
5087 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005088 return nullptr;
5089}
5090
Alexey Bataev10e775f2015-07-30 11:36:16 +00005091static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005092 auto OrderedClauses =
5093 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
5094 if (OrderedClauses.begin() != OrderedClauses.end())
5095 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005096 return nullptr;
5097}
5098
Kelvin Lic5609492016-07-15 04:39:07 +00005099static bool checkSimdlenSafelenSpecified(Sema &S,
5100 const ArrayRef<OMPClause *> Clauses) {
5101 OMPSafelenClause *Safelen = nullptr;
5102 OMPSimdlenClause *Simdlen = nullptr;
5103
5104 for (auto *Clause : Clauses) {
5105 if (Clause->getClauseKind() == OMPC_safelen)
5106 Safelen = cast<OMPSafelenClause>(Clause);
5107 else if (Clause->getClauseKind() == OMPC_simdlen)
5108 Simdlen = cast<OMPSimdlenClause>(Clause);
5109 if (Safelen && Simdlen)
5110 break;
5111 }
5112
5113 if (Simdlen && Safelen) {
5114 llvm::APSInt SimdlenRes, SafelenRes;
5115 auto SimdlenLength = Simdlen->getSimdlen();
5116 auto SafelenLength = Safelen->getSafelen();
5117 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
5118 SimdlenLength->isInstantiationDependent() ||
5119 SimdlenLength->containsUnexpandedParameterPack())
5120 return false;
5121 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
5122 SafelenLength->isInstantiationDependent() ||
5123 SafelenLength->containsUnexpandedParameterPack())
5124 return false;
5125 SimdlenLength->EvaluateAsInt(SimdlenRes, S.Context);
5126 SafelenLength->EvaluateAsInt(SafelenRes, S.Context);
5127 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
5128 // If both simdlen and safelen clauses are specified, the value of the
5129 // simdlen parameter must be less than or equal to the value of the safelen
5130 // parameter.
5131 if (SimdlenRes > SafelenRes) {
5132 S.Diag(SimdlenLength->getExprLoc(),
5133 diag::err_omp_wrong_simdlen_safelen_values)
5134 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
5135 return true;
5136 }
Alexey Bataev66b15b52015-08-21 11:14:16 +00005137 }
5138 return false;
5139}
5140
Alexey Bataev4acb8592014-07-07 13:01:15 +00005141StmtResult Sema::ActOnOpenMPSimdDirective(
5142 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5143 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005144 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005145 if (!AStmt)
5146 return StmtError();
5147
5148 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005149 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005150 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5151 // define the nested loops number.
5152 unsigned NestedLoopCount = CheckOpenMPLoop(
5153 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5154 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005155 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005156 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005157
Alexander Musmana5f070a2014-10-01 06:03:56 +00005158 assert((CurContext->isDependentContext() || B.builtAll()) &&
5159 "omp simd loop exprs were not built");
5160
Alexander Musman3276a272015-03-21 10:12:56 +00005161 if (!CurContext->isDependentContext()) {
5162 // Finalize the clauses that need pre-built expressions for CodeGen.
5163 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005164 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexander Musman3276a272015-03-21 10:12:56 +00005165 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005166 B.NumIterations, *this, CurScope,
5167 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00005168 return StmtError();
5169 }
5170 }
5171
Kelvin Lic5609492016-07-15 04:39:07 +00005172 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005173 return StmtError();
5174
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005175 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005176 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5177 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005178}
5179
Alexey Bataev4acb8592014-07-07 13:01:15 +00005180StmtResult Sema::ActOnOpenMPForDirective(
5181 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5182 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005183 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005184 if (!AStmt)
5185 return StmtError();
5186
5187 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005188 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005189 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5190 // define the nested loops number.
5191 unsigned NestedLoopCount = CheckOpenMPLoop(
5192 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5193 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005194 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005195 return StmtError();
5196
Alexander Musmana5f070a2014-10-01 06:03:56 +00005197 assert((CurContext->isDependentContext() || B.builtAll()) &&
5198 "omp for loop exprs were not built");
5199
Alexey Bataev54acd402015-08-04 11:18:19 +00005200 if (!CurContext->isDependentContext()) {
5201 // Finalize the clauses that need pre-built expressions for CodeGen.
5202 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005203 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00005204 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005205 B.NumIterations, *this, CurScope,
5206 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005207 return StmtError();
5208 }
5209 }
5210
Alexey Bataevf29276e2014-06-18 04:14:57 +00005211 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005212 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005213 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005214}
5215
Alexander Musmanf82886e2014-09-18 05:12:34 +00005216StmtResult Sema::ActOnOpenMPForSimdDirective(
5217 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5218 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005219 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005220 if (!AStmt)
5221 return StmtError();
5222
5223 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005224 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005225 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5226 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00005227 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005228 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
5229 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5230 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005231 if (NestedLoopCount == 0)
5232 return StmtError();
5233
Alexander Musmanc6388682014-12-15 07:07:06 +00005234 assert((CurContext->isDependentContext() || B.builtAll()) &&
5235 "omp for simd loop exprs were not built");
5236
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005237 if (!CurContext->isDependentContext()) {
5238 // Finalize the clauses that need pre-built expressions for CodeGen.
5239 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005240 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005241 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005242 B.NumIterations, *this, CurScope,
5243 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005244 return StmtError();
5245 }
5246 }
5247
Kelvin Lic5609492016-07-15 04:39:07 +00005248 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005249 return StmtError();
5250
Alexander Musmanf82886e2014-09-18 05:12:34 +00005251 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005252 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5253 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005254}
5255
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005256StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5257 Stmt *AStmt,
5258 SourceLocation StartLoc,
5259 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005260 if (!AStmt)
5261 return StmtError();
5262
5263 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005264 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00005265 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005266 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00005267 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005268 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005269 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005270 return StmtError();
5271 // All associated statements must be '#pragma omp section' except for
5272 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005273 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005274 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5275 if (SectionStmt)
5276 Diag(SectionStmt->getLocStart(),
5277 diag::err_omp_sections_substmt_not_section);
5278 return StmtError();
5279 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005280 cast<OMPSectionDirective>(SectionStmt)
5281 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005282 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005283 } else {
5284 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
5285 return StmtError();
5286 }
5287
5288 getCurFunction()->setHasBranchProtectedScope();
5289
Alexey Bataev25e5b442015-09-15 12:52:43 +00005290 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5291 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005292}
5293
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005294StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5295 SourceLocation StartLoc,
5296 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005297 if (!AStmt)
5298 return StmtError();
5299
5300 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005301
5302 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00005303 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005304
Alexey Bataev25e5b442015-09-15 12:52:43 +00005305 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5306 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005307}
5308
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005309StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5310 Stmt *AStmt,
5311 SourceLocation StartLoc,
5312 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005313 if (!AStmt)
5314 return StmtError();
5315
5316 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00005317
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005318 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00005319
Alexey Bataev3255bf32015-01-19 05:20:46 +00005320 // OpenMP [2.7.3, single Construct, Restrictions]
5321 // The copyprivate clause must not be used with the nowait clause.
5322 OMPClause *Nowait = nullptr;
5323 OMPClause *Copyprivate = nullptr;
5324 for (auto *Clause : Clauses) {
5325 if (Clause->getClauseKind() == OMPC_nowait)
5326 Nowait = Clause;
5327 else if (Clause->getClauseKind() == OMPC_copyprivate)
5328 Copyprivate = Clause;
5329 if (Copyprivate && Nowait) {
5330 Diag(Copyprivate->getLocStart(),
5331 diag::err_omp_single_copyprivate_with_nowait);
5332 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
5333 return StmtError();
5334 }
5335 }
5336
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005337 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5338}
5339
Alexander Musman80c22892014-07-17 08:54:58 +00005340StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5341 SourceLocation StartLoc,
5342 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005343 if (!AStmt)
5344 return StmtError();
5345
5346 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00005347
5348 getCurFunction()->setHasBranchProtectedScope();
5349
5350 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5351}
5352
Alexey Bataev28c75412015-12-15 08:19:24 +00005353StmtResult Sema::ActOnOpenMPCriticalDirective(
5354 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5355 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005356 if (!AStmt)
5357 return StmtError();
5358
5359 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005360
Alexey Bataev28c75412015-12-15 08:19:24 +00005361 bool ErrorFound = false;
5362 llvm::APSInt Hint;
5363 SourceLocation HintLoc;
5364 bool DependentHint = false;
5365 for (auto *C : Clauses) {
5366 if (C->getClauseKind() == OMPC_hint) {
5367 if (!DirName.getName()) {
5368 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
5369 ErrorFound = true;
5370 }
5371 Expr *E = cast<OMPHintClause>(C)->getHint();
5372 if (E->isTypeDependent() || E->isValueDependent() ||
5373 E->isInstantiationDependent())
5374 DependentHint = true;
5375 else {
5376 Hint = E->EvaluateKnownConstInt(Context);
5377 HintLoc = C->getLocStart();
5378 }
5379 }
5380 }
5381 if (ErrorFound)
5382 return StmtError();
5383 auto Pair = DSAStack->getCriticalWithHint(DirName);
5384 if (Pair.first && DirName.getName() && !DependentHint) {
5385 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5386 Diag(StartLoc, diag::err_omp_critical_with_hint);
5387 if (HintLoc.isValid()) {
5388 Diag(HintLoc, diag::note_omp_critical_hint_here)
5389 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
5390 } else
5391 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
5392 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
5393 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
5394 << 1
5395 << C->getHint()->EvaluateKnownConstInt(Context).toString(
5396 /*Radix=*/10, /*Signed=*/false);
5397 } else
5398 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
5399 }
5400 }
5401
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005402 getCurFunction()->setHasBranchProtectedScope();
5403
Alexey Bataev28c75412015-12-15 08:19:24 +00005404 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5405 Clauses, AStmt);
5406 if (!Pair.first && DirName.getName() && !DependentHint)
5407 DSAStack->addCriticalWithHint(Dir, Hint);
5408 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005409}
5410
Alexey Bataev4acb8592014-07-07 13:01:15 +00005411StmtResult Sema::ActOnOpenMPParallelForDirective(
5412 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5413 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005414 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005415 if (!AStmt)
5416 return StmtError();
5417
Alexey Bataev4acb8592014-07-07 13:01:15 +00005418 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5419 // 1.2.2 OpenMP Language Terminology
5420 // Structured block - An executable statement with a single entry at the
5421 // top and a single exit at the bottom.
5422 // The point of exit cannot be a branch out of the structured block.
5423 // longjmp() and throw() must not violate the entry/exit criteria.
5424 CS->getCapturedDecl()->setNothrow();
5425
Alexander Musmanc6388682014-12-15 07:07:06 +00005426 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005427 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5428 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005429 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005430 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
5431 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5432 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005433 if (NestedLoopCount == 0)
5434 return StmtError();
5435
Alexander Musmana5f070a2014-10-01 06:03:56 +00005436 assert((CurContext->isDependentContext() || B.builtAll()) &&
5437 "omp parallel for loop exprs were not built");
5438
Alexey Bataev54acd402015-08-04 11:18:19 +00005439 if (!CurContext->isDependentContext()) {
5440 // Finalize the clauses that need pre-built expressions for CodeGen.
5441 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005442 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00005443 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005444 B.NumIterations, *this, CurScope,
5445 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005446 return StmtError();
5447 }
5448 }
5449
Alexey Bataev4acb8592014-07-07 13:01:15 +00005450 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005451 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005452 NestedLoopCount, Clauses, AStmt, B,
5453 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00005454}
5455
Alexander Musmane4e893b2014-09-23 09:33:00 +00005456StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5457 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5458 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005459 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005460 if (!AStmt)
5461 return StmtError();
5462
Alexander Musmane4e893b2014-09-23 09:33:00 +00005463 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5464 // 1.2.2 OpenMP Language Terminology
5465 // Structured block - An executable statement with a single entry at the
5466 // top and a single exit at the bottom.
5467 // The point of exit cannot be a branch out of the structured block.
5468 // longjmp() and throw() must not violate the entry/exit criteria.
5469 CS->getCapturedDecl()->setNothrow();
5470
Alexander Musmanc6388682014-12-15 07:07:06 +00005471 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005472 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5473 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00005474 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005475 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
5476 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5477 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005478 if (NestedLoopCount == 0)
5479 return StmtError();
5480
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005481 if (!CurContext->isDependentContext()) {
5482 // Finalize the clauses that need pre-built expressions for CodeGen.
5483 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005484 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005485 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005486 B.NumIterations, *this, CurScope,
5487 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005488 return StmtError();
5489 }
5490 }
5491
Kelvin Lic5609492016-07-15 04:39:07 +00005492 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005493 return StmtError();
5494
Alexander Musmane4e893b2014-09-23 09:33:00 +00005495 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005496 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00005497 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005498}
5499
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005500StmtResult
5501Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
5502 Stmt *AStmt, SourceLocation StartLoc,
5503 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005504 if (!AStmt)
5505 return StmtError();
5506
5507 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005508 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00005509 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005510 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00005511 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005512 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005513 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005514 return StmtError();
5515 // All associated statements must be '#pragma omp section' except for
5516 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005517 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005518 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5519 if (SectionStmt)
5520 Diag(SectionStmt->getLocStart(),
5521 diag::err_omp_parallel_sections_substmt_not_section);
5522 return StmtError();
5523 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005524 cast<OMPSectionDirective>(SectionStmt)
5525 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005526 }
5527 } else {
5528 Diag(AStmt->getLocStart(),
5529 diag::err_omp_parallel_sections_not_compound_stmt);
5530 return StmtError();
5531 }
5532
5533 getCurFunction()->setHasBranchProtectedScope();
5534
Alexey Bataev25e5b442015-09-15 12:52:43 +00005535 return OMPParallelSectionsDirective::Create(
5536 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005537}
5538
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005539StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5540 Stmt *AStmt, SourceLocation StartLoc,
5541 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005542 if (!AStmt)
5543 return StmtError();
5544
David Majnemer9d168222016-08-05 17:44:54 +00005545 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005546 // 1.2.2 OpenMP Language Terminology
5547 // Structured block - An executable statement with a single entry at the
5548 // top and a single exit at the bottom.
5549 // The point of exit cannot be a branch out of the structured block.
5550 // longjmp() and throw() must not violate the entry/exit criteria.
5551 CS->getCapturedDecl()->setNothrow();
5552
5553 getCurFunction()->setHasBranchProtectedScope();
5554
Alexey Bataev25e5b442015-09-15 12:52:43 +00005555 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5556 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005557}
5558
Alexey Bataev68446b72014-07-18 07:47:19 +00005559StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5560 SourceLocation EndLoc) {
5561 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5562}
5563
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00005564StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5565 SourceLocation EndLoc) {
5566 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5567}
5568
Alexey Bataev2df347a2014-07-18 10:17:07 +00005569StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5570 SourceLocation EndLoc) {
5571 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5572}
5573
Alexey Bataev169d96a2017-07-18 20:17:46 +00005574StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses,
5575 Stmt *AStmt,
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005576 SourceLocation StartLoc,
5577 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005578 if (!AStmt)
5579 return StmtError();
5580
5581 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005582
5583 getCurFunction()->setHasBranchProtectedScope();
5584
Alexey Bataev169d96a2017-07-18 20:17:46 +00005585 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses,
Alexey Bataev3b1b8952017-07-25 15:53:26 +00005586 AStmt,
5587 DSAStack->getTaskgroupReductionRef());
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005588}
5589
Alexey Bataev6125da92014-07-21 11:26:11 +00005590StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5591 SourceLocation StartLoc,
5592 SourceLocation EndLoc) {
5593 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5594 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5595}
5596
Alexey Bataev346265e2015-09-25 10:37:12 +00005597StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5598 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005599 SourceLocation StartLoc,
5600 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00005601 OMPClause *DependFound = nullptr;
5602 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005603 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005604 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00005605 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005606 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005607 for (auto *C : Clauses) {
5608 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5609 DependFound = C;
5610 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5611 if (DependSourceClause) {
5612 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5613 << getOpenMPDirectiveName(OMPD_ordered)
5614 << getOpenMPClauseName(OMPC_depend) << 2;
5615 ErrorFound = true;
5616 } else
5617 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005618 if (DependSinkClause) {
5619 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5620 << 0;
5621 ErrorFound = true;
5622 }
5623 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5624 if (DependSourceClause) {
5625 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5626 << 1;
5627 ErrorFound = true;
5628 }
5629 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00005630 }
5631 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00005632 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005633 else if (C->getClauseKind() == OMPC_simd)
5634 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00005635 }
Alexey Bataeveb482352015-12-18 05:05:56 +00005636 if (!ErrorFound && !SC &&
5637 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005638 // OpenMP [2.8.1,simd Construct, Restrictions]
5639 // An ordered construct with the simd clause is the only OpenMP construct
5640 // that can appear in the simd region.
5641 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00005642 ErrorFound = true;
5643 } else if (DependFound && (TC || SC)) {
5644 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
5645 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
5646 ErrorFound = true;
5647 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
5648 Diag(DependFound->getLocStart(),
5649 diag::err_omp_ordered_directive_without_param);
5650 ErrorFound = true;
5651 } else if (TC || Clauses.empty()) {
5652 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
5653 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
5654 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
5655 << (TC != nullptr);
5656 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
5657 ErrorFound = true;
5658 }
5659 }
5660 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005661 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00005662
5663 if (AStmt) {
5664 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5665
5666 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005667 }
Alexey Bataev346265e2015-09-25 10:37:12 +00005668
5669 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005670}
5671
Alexey Bataev1d160b12015-03-13 12:27:31 +00005672namespace {
5673/// \brief Helper class for checking expression in 'omp atomic [update]'
5674/// construct.
5675class OpenMPAtomicUpdateChecker {
5676 /// \brief Error results for atomic update expressions.
5677 enum ExprAnalysisErrorCode {
5678 /// \brief A statement is not an expression statement.
5679 NotAnExpression,
5680 /// \brief Expression is not builtin binary or unary operation.
5681 NotABinaryOrUnaryExpression,
5682 /// \brief Unary operation is not post-/pre- increment/decrement operation.
5683 NotAnUnaryIncDecExpression,
5684 /// \brief An expression is not of scalar type.
5685 NotAScalarType,
5686 /// \brief A binary operation is not an assignment operation.
5687 NotAnAssignmentOp,
5688 /// \brief RHS part of the binary operation is not a binary expression.
5689 NotABinaryExpression,
5690 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
5691 /// expression.
5692 NotABinaryOperator,
5693 /// \brief RHS binary operation does not have reference to the updated LHS
5694 /// part.
5695 NotAnUpdateExpression,
5696 /// \brief No errors is found.
5697 NoError
5698 };
5699 /// \brief Reference to Sema.
5700 Sema &SemaRef;
5701 /// \brief A location for note diagnostics (when error is found).
5702 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005703 /// \brief 'x' lvalue part of the source atomic expression.
5704 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005705 /// \brief 'expr' rvalue part of the source atomic expression.
5706 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005707 /// \brief Helper expression of the form
5708 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5709 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5710 Expr *UpdateExpr;
5711 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
5712 /// important for non-associative operations.
5713 bool IsXLHSInRHSPart;
5714 BinaryOperatorKind Op;
5715 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005716 /// \brief true if the source expression is a postfix unary operation, false
5717 /// if it is a prefix unary operation.
5718 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005719
5720public:
5721 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00005722 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00005723 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00005724 /// \brief Check specified statement that it is suitable for 'atomic update'
5725 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00005726 /// expression. If DiagId and NoteId == 0, then only check is performed
5727 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005728 /// \param DiagId Diagnostic which should be emitted if error is found.
5729 /// \param NoteId Diagnostic note for the main error message.
5730 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00005731 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005732 /// \brief Return the 'x' lvalue part of the source atomic expression.
5733 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00005734 /// \brief Return the 'expr' rvalue part of the source atomic expression.
5735 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00005736 /// \brief Return the update expression used in calculation of the updated
5737 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5738 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5739 Expr *getUpdateExpr() const { return UpdateExpr; }
5740 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
5741 /// false otherwise.
5742 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5743
Alexey Bataevb78ca832015-04-01 03:33:17 +00005744 /// \brief true if the source expression is a postfix unary operation, false
5745 /// if it is a prefix unary operation.
5746 bool isPostfixUpdate() const { return IsPostfixUpdate; }
5747
Alexey Bataev1d160b12015-03-13 12:27:31 +00005748private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00005749 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5750 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005751};
5752} // namespace
5753
5754bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5755 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5756 ExprAnalysisErrorCode ErrorFound = NoError;
5757 SourceLocation ErrorLoc, NoteLoc;
5758 SourceRange ErrorRange, NoteRange;
5759 // Allowed constructs are:
5760 // x = x binop expr;
5761 // x = expr binop x;
5762 if (AtomicBinOp->getOpcode() == BO_Assign) {
5763 X = AtomicBinOp->getLHS();
5764 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
5765 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5766 if (AtomicInnerBinOp->isMultiplicativeOp() ||
5767 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5768 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005769 Op = AtomicInnerBinOp->getOpcode();
5770 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005771 auto *LHS = AtomicInnerBinOp->getLHS();
5772 auto *RHS = AtomicInnerBinOp->getRHS();
5773 llvm::FoldingSetNodeID XId, LHSId, RHSId;
5774 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
5775 /*Canonical=*/true);
5776 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
5777 /*Canonical=*/true);
5778 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
5779 /*Canonical=*/true);
5780 if (XId == LHSId) {
5781 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005782 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005783 } else if (XId == RHSId) {
5784 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005785 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005786 } else {
5787 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5788 ErrorRange = AtomicInnerBinOp->getSourceRange();
5789 NoteLoc = X->getExprLoc();
5790 NoteRange = X->getSourceRange();
5791 ErrorFound = NotAnUpdateExpression;
5792 }
5793 } else {
5794 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5795 ErrorRange = AtomicInnerBinOp->getSourceRange();
5796 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5797 NoteRange = SourceRange(NoteLoc, NoteLoc);
5798 ErrorFound = NotABinaryOperator;
5799 }
5800 } else {
5801 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5802 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5803 ErrorFound = NotABinaryExpression;
5804 }
5805 } else {
5806 ErrorLoc = AtomicBinOp->getExprLoc();
5807 ErrorRange = AtomicBinOp->getSourceRange();
5808 NoteLoc = AtomicBinOp->getOperatorLoc();
5809 NoteRange = SourceRange(NoteLoc, NoteLoc);
5810 ErrorFound = NotAnAssignmentOp;
5811 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005812 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005813 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5814 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5815 return true;
5816 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005817 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005818 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005819}
5820
5821bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5822 unsigned NoteId) {
5823 ExprAnalysisErrorCode ErrorFound = NoError;
5824 SourceLocation ErrorLoc, NoteLoc;
5825 SourceRange ErrorRange, NoteRange;
5826 // Allowed constructs are:
5827 // x++;
5828 // x--;
5829 // ++x;
5830 // --x;
5831 // x binop= expr;
5832 // x = x binop expr;
5833 // x = expr binop x;
5834 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5835 AtomicBody = AtomicBody->IgnoreParenImpCasts();
5836 if (AtomicBody->getType()->isScalarType() ||
5837 AtomicBody->isInstantiationDependent()) {
5838 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5839 AtomicBody->IgnoreParenImpCasts())) {
5840 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005841 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00005842 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00005843 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005844 E = AtomicCompAssignOp->getRHS();
Kelvin Li4f161cf2016-07-20 19:41:17 +00005845 X = AtomicCompAssignOp->getLHS()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005846 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005847 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5848 AtomicBody->IgnoreParenImpCasts())) {
5849 // Check for Binary Operation
David Majnemer9d168222016-08-05 17:44:54 +00005850 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
Alexey Bataevb4505a72015-03-30 05:20:59 +00005851 return true;
David Majnemer9d168222016-08-05 17:44:54 +00005852 } else if (auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
5853 AtomicBody->IgnoreParenImpCasts())) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005854 // Check for Unary Operation
5855 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005856 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005857 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5858 OpLoc = AtomicUnaryOp->getOperatorLoc();
Kelvin Li4f161cf2016-07-20 19:41:17 +00005859 X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005860 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5861 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005862 } else {
5863 ErrorFound = NotAnUnaryIncDecExpression;
5864 ErrorLoc = AtomicUnaryOp->getExprLoc();
5865 ErrorRange = AtomicUnaryOp->getSourceRange();
5866 NoteLoc = AtomicUnaryOp->getOperatorLoc();
5867 NoteRange = SourceRange(NoteLoc, NoteLoc);
5868 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005869 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005870 ErrorFound = NotABinaryOrUnaryExpression;
5871 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5872 NoteRange = ErrorRange = AtomicBody->getSourceRange();
5873 }
5874 } else {
5875 ErrorFound = NotAScalarType;
5876 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
5877 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5878 }
5879 } else {
5880 ErrorFound = NotAnExpression;
5881 NoteLoc = ErrorLoc = S->getLocStart();
5882 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5883 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005884 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005885 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5886 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5887 return true;
5888 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005889 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005890 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005891 // Build an update expression of form 'OpaqueValueExpr(x) binop
5892 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
5893 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
5894 auto *OVEX = new (SemaRef.getASTContext())
5895 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
5896 auto *OVEExpr = new (SemaRef.getASTContext())
5897 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
5898 auto Update =
5899 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
5900 IsXLHSInRHSPart ? OVEExpr : OVEX);
5901 if (Update.isInvalid())
5902 return true;
5903 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
5904 Sema::AA_Casting);
5905 if (Update.isInvalid())
5906 return true;
5907 UpdateExpr = Update.get();
5908 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00005909 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005910}
5911
Alexey Bataev0162e452014-07-22 10:10:35 +00005912StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5913 Stmt *AStmt,
5914 SourceLocation StartLoc,
5915 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005916 if (!AStmt)
5917 return StmtError();
5918
David Majnemer9d168222016-08-05 17:44:54 +00005919 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00005920 // 1.2.2 OpenMP Language Terminology
5921 // Structured block - An executable statement with a single entry at the
5922 // top and a single exit at the bottom.
5923 // The point of exit cannot be a branch out of the structured block.
5924 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00005925 OpenMPClauseKind AtomicKind = OMPC_unknown;
5926 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005927 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00005928 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00005929 C->getClauseKind() == OMPC_update ||
5930 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00005931 if (AtomicKind != OMPC_unknown) {
5932 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5933 << SourceRange(C->getLocStart(), C->getLocEnd());
5934 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5935 << getOpenMPClauseName(AtomicKind);
5936 } else {
5937 AtomicKind = C->getClauseKind();
5938 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005939 }
5940 }
5941 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005942
Alexey Bataev459dec02014-07-24 06:46:57 +00005943 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00005944 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5945 Body = EWC->getSubExpr();
5946
Alexey Bataev62cec442014-11-18 10:14:22 +00005947 Expr *X = nullptr;
5948 Expr *V = nullptr;
5949 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005950 Expr *UE = nullptr;
5951 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005952 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00005953 // OpenMP [2.12.6, atomic Construct]
5954 // In the next expressions:
5955 // * x and v (as applicable) are both l-value expressions with scalar type.
5956 // * During the execution of an atomic region, multiple syntactic
5957 // occurrences of x must designate the same storage location.
5958 // * Neither of v and expr (as applicable) may access the storage location
5959 // designated by x.
5960 // * Neither of x and expr (as applicable) may access the storage location
5961 // designated by v.
5962 // * expr is an expression with scalar type.
5963 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5964 // * binop, binop=, ++, and -- are not overloaded operators.
5965 // * The expression x binop expr must be numerically equivalent to x binop
5966 // (expr). This requirement is satisfied if the operators in expr have
5967 // precedence greater than binop, or by using parentheses around expr or
5968 // subexpressions of expr.
5969 // * The expression expr binop x must be numerically equivalent to (expr)
5970 // binop x. This requirement is satisfied if the operators in expr have
5971 // precedence equal to or greater than binop, or by using parentheses around
5972 // expr or subexpressions of expr.
5973 // * For forms that allow multiple occurrences of x, the number of times
5974 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00005975 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005976 enum {
5977 NotAnExpression,
5978 NotAnAssignmentOp,
5979 NotAScalarType,
5980 NotAnLValue,
5981 NoError
5982 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00005983 SourceLocation ErrorLoc, NoteLoc;
5984 SourceRange ErrorRange, NoteRange;
5985 // If clause is read:
5986 // v = x;
David Majnemer9d168222016-08-05 17:44:54 +00005987 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5988 auto *AtomicBinOp =
Alexey Bataev62cec442014-11-18 10:14:22 +00005989 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5990 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5991 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5992 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5993 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5994 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5995 if (!X->isLValue() || !V->isLValue()) {
5996 auto NotLValueExpr = X->isLValue() ? V : X;
5997 ErrorFound = NotAnLValue;
5998 ErrorLoc = AtomicBinOp->getExprLoc();
5999 ErrorRange = AtomicBinOp->getSourceRange();
6000 NoteLoc = NotLValueExpr->getExprLoc();
6001 NoteRange = NotLValueExpr->getSourceRange();
6002 }
6003 } else if (!X->isInstantiationDependent() ||
6004 !V->isInstantiationDependent()) {
6005 auto NotScalarExpr =
6006 (X->isInstantiationDependent() || X->getType()->isScalarType())
6007 ? V
6008 : X;
6009 ErrorFound = NotAScalarType;
6010 ErrorLoc = AtomicBinOp->getExprLoc();
6011 ErrorRange = AtomicBinOp->getSourceRange();
6012 NoteLoc = NotScalarExpr->getExprLoc();
6013 NoteRange = NotScalarExpr->getSourceRange();
6014 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006015 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00006016 ErrorFound = NotAnAssignmentOp;
6017 ErrorLoc = AtomicBody->getExprLoc();
6018 ErrorRange = AtomicBody->getSourceRange();
6019 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6020 : AtomicBody->getExprLoc();
6021 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6022 : AtomicBody->getSourceRange();
6023 }
6024 } else {
6025 ErrorFound = NotAnExpression;
6026 NoteLoc = ErrorLoc = Body->getLocStart();
6027 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006028 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006029 if (ErrorFound != NoError) {
6030 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
6031 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006032 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6033 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00006034 return StmtError();
6035 } else if (CurContext->isDependentContext())
6036 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00006037 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006038 enum {
6039 NotAnExpression,
6040 NotAnAssignmentOp,
6041 NotAScalarType,
6042 NotAnLValue,
6043 NoError
6044 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006045 SourceLocation ErrorLoc, NoteLoc;
6046 SourceRange ErrorRange, NoteRange;
6047 // If clause is write:
6048 // x = expr;
David Majnemer9d168222016-08-05 17:44:54 +00006049 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6050 auto *AtomicBinOp =
Alexey Bataevf33eba62014-11-28 07:21:40 +00006051 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6052 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00006053 X = AtomicBinOp->getLHS();
6054 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006055 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6056 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
6057 if (!X->isLValue()) {
6058 ErrorFound = NotAnLValue;
6059 ErrorLoc = AtomicBinOp->getExprLoc();
6060 ErrorRange = AtomicBinOp->getSourceRange();
6061 NoteLoc = X->getExprLoc();
6062 NoteRange = X->getSourceRange();
6063 }
6064 } else if (!X->isInstantiationDependent() ||
6065 !E->isInstantiationDependent()) {
6066 auto NotScalarExpr =
6067 (X->isInstantiationDependent() || X->getType()->isScalarType())
6068 ? E
6069 : X;
6070 ErrorFound = NotAScalarType;
6071 ErrorLoc = AtomicBinOp->getExprLoc();
6072 ErrorRange = AtomicBinOp->getSourceRange();
6073 NoteLoc = NotScalarExpr->getExprLoc();
6074 NoteRange = NotScalarExpr->getSourceRange();
6075 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006076 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00006077 ErrorFound = NotAnAssignmentOp;
6078 ErrorLoc = AtomicBody->getExprLoc();
6079 ErrorRange = AtomicBody->getSourceRange();
6080 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6081 : AtomicBody->getExprLoc();
6082 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6083 : AtomicBody->getSourceRange();
6084 }
6085 } else {
6086 ErrorFound = NotAnExpression;
6087 NoteLoc = ErrorLoc = Body->getLocStart();
6088 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006089 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00006090 if (ErrorFound != NoError) {
6091 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
6092 << ErrorRange;
6093 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6094 << NoteRange;
6095 return StmtError();
6096 } else if (CurContext->isDependentContext())
6097 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006098 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006099 // If clause is update:
6100 // x++;
6101 // x--;
6102 // ++x;
6103 // --x;
6104 // x binop= expr;
6105 // x = x binop expr;
6106 // x = expr binop x;
6107 OpenMPAtomicUpdateChecker Checker(*this);
6108 if (Checker.checkStatement(
6109 Body, (AtomicKind == OMPC_update)
6110 ? diag::err_omp_atomic_update_not_expression_statement
6111 : diag::err_omp_atomic_not_expression_statement,
6112 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00006113 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006114 if (!CurContext->isDependentContext()) {
6115 E = Checker.getExpr();
6116 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006117 UE = Checker.getUpdateExpr();
6118 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00006119 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006120 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006121 enum {
6122 NotAnAssignmentOp,
6123 NotACompoundStatement,
6124 NotTwoSubstatements,
6125 NotASpecificExpression,
6126 NoError
6127 } ErrorFound = NoError;
6128 SourceLocation ErrorLoc, NoteLoc;
6129 SourceRange ErrorRange, NoteRange;
6130 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6131 // If clause is a capture:
6132 // v = x++;
6133 // v = x--;
6134 // v = ++x;
6135 // v = --x;
6136 // v = x binop= expr;
6137 // v = x = x binop expr;
6138 // v = x = expr binop x;
6139 auto *AtomicBinOp =
6140 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6141 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6142 V = AtomicBinOp->getLHS();
6143 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6144 OpenMPAtomicUpdateChecker Checker(*this);
6145 if (Checker.checkStatement(
6146 Body, diag::err_omp_atomic_capture_not_expression_statement,
6147 diag::note_omp_atomic_update))
6148 return StmtError();
6149 E = Checker.getExpr();
6150 X = Checker.getX();
6151 UE = Checker.getUpdateExpr();
6152 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6153 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00006154 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006155 ErrorLoc = AtomicBody->getExprLoc();
6156 ErrorRange = AtomicBody->getSourceRange();
6157 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6158 : AtomicBody->getExprLoc();
6159 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6160 : AtomicBody->getSourceRange();
6161 ErrorFound = NotAnAssignmentOp;
6162 }
6163 if (ErrorFound != NoError) {
6164 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6165 << ErrorRange;
6166 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6167 return StmtError();
6168 } else if (CurContext->isDependentContext()) {
6169 UE = V = E = X = nullptr;
6170 }
6171 } else {
6172 // If clause is a capture:
6173 // { v = x; x = expr; }
6174 // { v = x; x++; }
6175 // { v = x; x--; }
6176 // { v = x; ++x; }
6177 // { v = x; --x; }
6178 // { v = x; x binop= expr; }
6179 // { v = x; x = x binop expr; }
6180 // { v = x; x = expr binop x; }
6181 // { x++; v = x; }
6182 // { x--; v = x; }
6183 // { ++x; v = x; }
6184 // { --x; v = x; }
6185 // { x binop= expr; v = x; }
6186 // { x = x binop expr; v = x; }
6187 // { x = expr binop x; v = x; }
6188 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6189 // Check that this is { expr1; expr2; }
6190 if (CS->size() == 2) {
6191 auto *First = CS->body_front();
6192 auto *Second = CS->body_back();
6193 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6194 First = EWC->getSubExpr()->IgnoreParenImpCasts();
6195 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6196 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6197 // Need to find what subexpression is 'v' and what is 'x'.
6198 OpenMPAtomicUpdateChecker Checker(*this);
6199 bool IsUpdateExprFound = !Checker.checkStatement(Second);
6200 BinaryOperator *BinOp = nullptr;
6201 if (IsUpdateExprFound) {
6202 BinOp = dyn_cast<BinaryOperator>(First);
6203 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6204 }
6205 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6206 // { v = x; x++; }
6207 // { v = x; x--; }
6208 // { v = x; ++x; }
6209 // { v = x; --x; }
6210 // { v = x; x binop= expr; }
6211 // { v = x; x = x binop expr; }
6212 // { v = x; x = expr binop x; }
6213 // Check that the first expression has form v = x.
6214 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6215 llvm::FoldingSetNodeID XId, PossibleXId;
6216 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6217 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6218 IsUpdateExprFound = XId == PossibleXId;
6219 if (IsUpdateExprFound) {
6220 V = BinOp->getLHS();
6221 X = Checker.getX();
6222 E = Checker.getExpr();
6223 UE = Checker.getUpdateExpr();
6224 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006225 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006226 }
6227 }
6228 if (!IsUpdateExprFound) {
6229 IsUpdateExprFound = !Checker.checkStatement(First);
6230 BinOp = nullptr;
6231 if (IsUpdateExprFound) {
6232 BinOp = dyn_cast<BinaryOperator>(Second);
6233 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6234 }
6235 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6236 // { x++; v = x; }
6237 // { x--; v = x; }
6238 // { ++x; v = x; }
6239 // { --x; v = x; }
6240 // { x binop= expr; v = x; }
6241 // { x = x binop expr; v = x; }
6242 // { x = expr binop x; v = x; }
6243 // Check that the second expression has form v = x.
6244 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6245 llvm::FoldingSetNodeID XId, PossibleXId;
6246 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6247 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6248 IsUpdateExprFound = XId == PossibleXId;
6249 if (IsUpdateExprFound) {
6250 V = BinOp->getLHS();
6251 X = Checker.getX();
6252 E = Checker.getExpr();
6253 UE = Checker.getUpdateExpr();
6254 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006255 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006256 }
6257 }
6258 }
6259 if (!IsUpdateExprFound) {
6260 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00006261 auto *FirstExpr = dyn_cast<Expr>(First);
6262 auto *SecondExpr = dyn_cast<Expr>(Second);
6263 if (!FirstExpr || !SecondExpr ||
6264 !(FirstExpr->isInstantiationDependent() ||
6265 SecondExpr->isInstantiationDependent())) {
6266 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6267 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006268 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00006269 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
6270 : First->getLocStart();
6271 NoteRange = ErrorRange = FirstBinOp
6272 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00006273 : SourceRange(ErrorLoc, ErrorLoc);
6274 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006275 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6276 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6277 ErrorFound = NotAnAssignmentOp;
6278 NoteLoc = ErrorLoc = SecondBinOp
6279 ? SecondBinOp->getOperatorLoc()
6280 : Second->getLocStart();
6281 NoteRange = ErrorRange =
6282 SecondBinOp ? SecondBinOp->getSourceRange()
6283 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00006284 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006285 auto *PossibleXRHSInFirst =
6286 FirstBinOp->getRHS()->IgnoreParenImpCasts();
6287 auto *PossibleXLHSInSecond =
6288 SecondBinOp->getLHS()->IgnoreParenImpCasts();
6289 llvm::FoldingSetNodeID X1Id, X2Id;
6290 PossibleXRHSInFirst->Profile(X1Id, Context,
6291 /*Canonical=*/true);
6292 PossibleXLHSInSecond->Profile(X2Id, Context,
6293 /*Canonical=*/true);
6294 IsUpdateExprFound = X1Id == X2Id;
6295 if (IsUpdateExprFound) {
6296 V = FirstBinOp->getLHS();
6297 X = SecondBinOp->getLHS();
6298 E = SecondBinOp->getRHS();
6299 UE = nullptr;
6300 IsXLHSInRHSPart = false;
6301 IsPostfixUpdate = true;
6302 } else {
6303 ErrorFound = NotASpecificExpression;
6304 ErrorLoc = FirstBinOp->getExprLoc();
6305 ErrorRange = FirstBinOp->getSourceRange();
6306 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6307 NoteRange = SecondBinOp->getRHS()->getSourceRange();
6308 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006309 }
6310 }
6311 }
6312 }
6313 } else {
6314 NoteLoc = ErrorLoc = Body->getLocStart();
6315 NoteRange = ErrorRange =
6316 SourceRange(Body->getLocStart(), Body->getLocStart());
6317 ErrorFound = NotTwoSubstatements;
6318 }
6319 } else {
6320 NoteLoc = ErrorLoc = Body->getLocStart();
6321 NoteRange = ErrorRange =
6322 SourceRange(Body->getLocStart(), Body->getLocStart());
6323 ErrorFound = NotACompoundStatement;
6324 }
6325 if (ErrorFound != NoError) {
6326 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6327 << ErrorRange;
6328 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6329 return StmtError();
6330 } else if (CurContext->isDependentContext()) {
6331 UE = V = E = X = nullptr;
6332 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006333 }
Alexey Bataevdea47612014-07-23 07:46:59 +00006334 }
Alexey Bataev0162e452014-07-22 10:10:35 +00006335
6336 getCurFunction()->setHasBranchProtectedScope();
6337
Alexey Bataev62cec442014-11-18 10:14:22 +00006338 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00006339 X, V, E, UE, IsXLHSInRHSPart,
6340 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00006341}
6342
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006343StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6344 Stmt *AStmt,
6345 SourceLocation StartLoc,
6346 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006347 if (!AStmt)
6348 return StmtError();
6349
Samuel Antao4af1b7b2015-12-02 17:44:43 +00006350 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6351 // 1.2.2 OpenMP Language Terminology
6352 // Structured block - An executable statement with a single entry at the
6353 // top and a single exit at the bottom.
6354 // The point of exit cannot be a branch out of the structured block.
6355 // longjmp() and throw() must not violate the entry/exit criteria.
6356 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006357
Alexey Bataev13314bf2014-10-09 04:18:56 +00006358 // OpenMP [2.16, Nesting of Regions]
6359 // If specified, a teams construct must be contained within a target
6360 // construct. That target construct must contain no statements or directives
6361 // outside of the teams construct.
6362 if (DSAStack->hasInnerTeamsRegion()) {
6363 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
6364 bool OMPTeamsFound = true;
6365 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
6366 auto I = CS->body_begin();
6367 while (I != CS->body_end()) {
David Majnemer9d168222016-08-05 17:44:54 +00006368 auto *OED = dyn_cast<OMPExecutableDirective>(*I);
Alexey Bataev13314bf2014-10-09 04:18:56 +00006369 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6370 OMPTeamsFound = false;
6371 break;
6372 }
6373 ++I;
6374 }
6375 assert(I != CS->body_end() && "Not found statement");
6376 S = *I;
Kelvin Li3834dce2016-06-27 19:15:43 +00006377 } else {
6378 auto *OED = dyn_cast<OMPExecutableDirective>(S);
6379 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
Alexey Bataev13314bf2014-10-09 04:18:56 +00006380 }
6381 if (!OMPTeamsFound) {
6382 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6383 Diag(DSAStack->getInnerTeamsRegionLoc(),
6384 diag::note_omp_nested_teams_construct_here);
6385 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
6386 << isa<OMPExecutableDirective>(S);
6387 return StmtError();
6388 }
6389 }
6390
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006391 getCurFunction()->setHasBranchProtectedScope();
6392
6393 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6394}
6395
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006396StmtResult
6397Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6398 Stmt *AStmt, SourceLocation StartLoc,
6399 SourceLocation EndLoc) {
6400 if (!AStmt)
6401 return StmtError();
6402
6403 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6404 // 1.2.2 OpenMP Language Terminology
6405 // Structured block - An executable statement with a single entry at the
6406 // top and a single exit at the bottom.
6407 // The point of exit cannot be a branch out of the structured block.
6408 // longjmp() and throw() must not violate the entry/exit criteria.
6409 CS->getCapturedDecl()->setNothrow();
6410
6411 getCurFunction()->setHasBranchProtectedScope();
6412
6413 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6414 AStmt);
6415}
6416
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006417StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6418 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6419 SourceLocation EndLoc,
6420 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6421 if (!AStmt)
6422 return StmtError();
6423
6424 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6425 // 1.2.2 OpenMP Language Terminology
6426 // Structured block - An executable statement with a single entry at the
6427 // top and a single exit at the bottom.
6428 // The point of exit cannot be a branch out of the structured block.
6429 // longjmp() and throw() must not violate the entry/exit criteria.
6430 CS->getCapturedDecl()->setNothrow();
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00006431 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
6432 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6433 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6434 // 1.2.2 OpenMP Language Terminology
6435 // Structured block - An executable statement with a single entry at the
6436 // top and a single exit at the bottom.
6437 // The point of exit cannot be a branch out of the structured block.
6438 // longjmp() and throw() must not violate the entry/exit criteria.
6439 CS->getCapturedDecl()->setNothrow();
6440 }
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006441
6442 OMPLoopDirective::HelperExprs B;
6443 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6444 // define the nested loops number.
6445 unsigned NestedLoopCount =
6446 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00006447 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006448 VarsWithImplicitDSA, B);
6449 if (NestedLoopCount == 0)
6450 return StmtError();
6451
6452 assert((CurContext->isDependentContext() || B.builtAll()) &&
6453 "omp target parallel for loop exprs were not built");
6454
6455 if (!CurContext->isDependentContext()) {
6456 // Finalize the clauses that need pre-built expressions for CodeGen.
6457 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00006458 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006459 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006460 B.NumIterations, *this, CurScope,
6461 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006462 return StmtError();
6463 }
6464 }
6465
6466 getCurFunction()->setHasBranchProtectedScope();
6467 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
6468 NestedLoopCount, Clauses, AStmt,
6469 B, DSAStack->isCancelRegion());
6470}
6471
Alexey Bataev95b64a92017-05-30 16:00:04 +00006472/// Check for existence of a map clause in the list of clauses.
6473static bool hasClauses(ArrayRef<OMPClause *> Clauses,
6474 const OpenMPClauseKind K) {
6475 return llvm::any_of(
6476 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; });
6477}
Samuel Antaodf67fc42016-01-19 19:15:56 +00006478
Alexey Bataev95b64a92017-05-30 16:00:04 +00006479template <typename... Params>
6480static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K,
6481 const Params... ClauseTypes) {
6482 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...);
Samuel Antaodf67fc42016-01-19 19:15:56 +00006483}
6484
Michael Wong65f367f2015-07-21 13:44:28 +00006485StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6486 Stmt *AStmt,
6487 SourceLocation StartLoc,
6488 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006489 if (!AStmt)
6490 return StmtError();
6491
6492 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6493
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006494 // OpenMP [2.10.1, Restrictions, p. 97]
6495 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00006496 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) {
6497 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
6498 << "'map' or 'use_device_ptr'"
David Majnemer9d168222016-08-05 17:44:54 +00006499 << getOpenMPDirectiveName(OMPD_target_data);
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006500 return StmtError();
6501 }
6502
Michael Wong65f367f2015-07-21 13:44:28 +00006503 getCurFunction()->setHasBranchProtectedScope();
6504
6505 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6506 AStmt);
6507}
6508
Samuel Antaodf67fc42016-01-19 19:15:56 +00006509StmtResult
6510Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
6511 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00006512 SourceLocation EndLoc, Stmt *AStmt) {
6513 if (!AStmt)
6514 return StmtError();
6515
6516 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6517 // 1.2.2 OpenMP Language Terminology
6518 // Structured block - An executable statement with a single entry at the
6519 // top and a single exit at the bottom.
6520 // The point of exit cannot be a branch out of the structured block.
6521 // longjmp() and throw() must not violate the entry/exit criteria.
6522 CS->getCapturedDecl()->setNothrow();
6523 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data);
6524 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6525 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6526 // 1.2.2 OpenMP Language Terminology
6527 // Structured block - An executable statement with a single entry at the
6528 // top and a single exit at the bottom.
6529 // The point of exit cannot be a branch out of the structured block.
6530 // longjmp() and throw() must not violate the entry/exit criteria.
6531 CS->getCapturedDecl()->setNothrow();
6532 }
6533
Samuel Antaodf67fc42016-01-19 19:15:56 +00006534 // OpenMP [2.10.2, Restrictions, p. 99]
6535 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00006536 if (!hasClauses(Clauses, OMPC_map)) {
6537 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
6538 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data);
Samuel Antaodf67fc42016-01-19 19:15:56 +00006539 return StmtError();
6540 }
6541
Alexey Bataev7828b252017-11-21 17:08:48 +00006542 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6543 AStmt);
Samuel Antaodf67fc42016-01-19 19:15:56 +00006544}
6545
Samuel Antao72590762016-01-19 20:04:50 +00006546StmtResult
6547Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6548 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00006549 SourceLocation EndLoc, Stmt *AStmt) {
6550 if (!AStmt)
6551 return StmtError();
6552
6553 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6554 // 1.2.2 OpenMP Language Terminology
6555 // Structured block - An executable statement with a single entry at the
6556 // top and a single exit at the bottom.
6557 // The point of exit cannot be a branch out of the structured block.
6558 // longjmp() and throw() must not violate the entry/exit criteria.
6559 CS->getCapturedDecl()->setNothrow();
6560 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data);
6561 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6562 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6563 // 1.2.2 OpenMP Language Terminology
6564 // Structured block - An executable statement with a single entry at the
6565 // top and a single exit at the bottom.
6566 // The point of exit cannot be a branch out of the structured block.
6567 // longjmp() and throw() must not violate the entry/exit criteria.
6568 CS->getCapturedDecl()->setNothrow();
6569 }
6570
Samuel Antao72590762016-01-19 20:04:50 +00006571 // OpenMP [2.10.3, Restrictions, p. 102]
6572 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00006573 if (!hasClauses(Clauses, OMPC_map)) {
6574 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
6575 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data);
Samuel Antao72590762016-01-19 20:04:50 +00006576 return StmtError();
6577 }
6578
Alexey Bataev7828b252017-11-21 17:08:48 +00006579 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6580 AStmt);
Samuel Antao72590762016-01-19 20:04:50 +00006581}
6582
Samuel Antao686c70c2016-05-26 17:30:50 +00006583StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
6584 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00006585 SourceLocation EndLoc,
6586 Stmt *AStmt) {
6587 if (!AStmt)
6588 return StmtError();
6589
6590 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6591 // 1.2.2 OpenMP Language Terminology
6592 // Structured block - An executable statement with a single entry at the
6593 // top and a single exit at the bottom.
6594 // The point of exit cannot be a branch out of the structured block.
6595 // longjmp() and throw() must not violate the entry/exit criteria.
6596 CS->getCapturedDecl()->setNothrow();
6597 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update);
6598 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6599 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6600 // 1.2.2 OpenMP Language Terminology
6601 // Structured block - An executable statement with a single entry at the
6602 // top and a single exit at the bottom.
6603 // The point of exit cannot be a branch out of the structured block.
6604 // longjmp() and throw() must not violate the entry/exit criteria.
6605 CS->getCapturedDecl()->setNothrow();
6606 }
6607
Alexey Bataev95b64a92017-05-30 16:00:04 +00006608 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) {
Samuel Antao686c70c2016-05-26 17:30:50 +00006609 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
6610 return StmtError();
6611 }
Alexey Bataev7828b252017-11-21 17:08:48 +00006612 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses,
6613 AStmt);
Samuel Antao686c70c2016-05-26 17:30:50 +00006614}
6615
Alexey Bataev13314bf2014-10-09 04:18:56 +00006616StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6617 Stmt *AStmt, SourceLocation StartLoc,
6618 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006619 if (!AStmt)
6620 return StmtError();
6621
Alexey Bataev13314bf2014-10-09 04:18:56 +00006622 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6623 // 1.2.2 OpenMP Language Terminology
6624 // Structured block - An executable statement with a single entry at the
6625 // top and a single exit at the bottom.
6626 // The point of exit cannot be a branch out of the structured block.
6627 // longjmp() and throw() must not violate the entry/exit criteria.
6628 CS->getCapturedDecl()->setNothrow();
6629
6630 getCurFunction()->setHasBranchProtectedScope();
6631
Alexey Bataevceabd412017-11-30 18:01:54 +00006632 DSAStack->setParentTeamsRegionLoc(StartLoc);
6633
Alexey Bataev13314bf2014-10-09 04:18:56 +00006634 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6635}
6636
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006637StmtResult
6638Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6639 SourceLocation EndLoc,
6640 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006641 if (DSAStack->isParentNowaitRegion()) {
6642 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6643 return StmtError();
6644 }
6645 if (DSAStack->isParentOrderedRegion()) {
6646 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
6647 return StmtError();
6648 }
6649 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
6650 CancelRegion);
6651}
6652
Alexey Bataev87933c72015-09-18 08:07:34 +00006653StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
6654 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00006655 SourceLocation EndLoc,
6656 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev80909872015-07-02 11:25:17 +00006657 if (DSAStack->isParentNowaitRegion()) {
6658 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6659 return StmtError();
6660 }
6661 if (DSAStack->isParentOrderedRegion()) {
6662 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6663 return StmtError();
6664 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006665 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00006666 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6667 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00006668}
6669
Alexey Bataev382967a2015-12-08 12:06:20 +00006670static bool checkGrainsizeNumTasksClauses(Sema &S,
6671 ArrayRef<OMPClause *> Clauses) {
6672 OMPClause *PrevClause = nullptr;
6673 bool ErrorFound = false;
6674 for (auto *C : Clauses) {
6675 if (C->getClauseKind() == OMPC_grainsize ||
6676 C->getClauseKind() == OMPC_num_tasks) {
6677 if (!PrevClause)
6678 PrevClause = C;
6679 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6680 S.Diag(C->getLocStart(),
6681 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6682 << getOpenMPClauseName(C->getClauseKind())
6683 << getOpenMPClauseName(PrevClause->getClauseKind());
6684 S.Diag(PrevClause->getLocStart(),
6685 diag::note_omp_previous_grainsize_num_tasks)
6686 << getOpenMPClauseName(PrevClause->getClauseKind());
6687 ErrorFound = true;
6688 }
6689 }
6690 }
6691 return ErrorFound;
6692}
6693
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00006694static bool checkReductionClauseWithNogroup(Sema &S,
6695 ArrayRef<OMPClause *> Clauses) {
6696 OMPClause *ReductionClause = nullptr;
6697 OMPClause *NogroupClause = nullptr;
6698 for (auto *C : Clauses) {
6699 if (C->getClauseKind() == OMPC_reduction) {
6700 ReductionClause = C;
6701 if (NogroupClause)
6702 break;
6703 continue;
6704 }
6705 if (C->getClauseKind() == OMPC_nogroup) {
6706 NogroupClause = C;
6707 if (ReductionClause)
6708 break;
6709 continue;
6710 }
6711 }
6712 if (ReductionClause && NogroupClause) {
6713 S.Diag(ReductionClause->getLocStart(), diag::err_omp_reduction_with_nogroup)
6714 << SourceRange(NogroupClause->getLocStart(),
6715 NogroupClause->getLocEnd());
6716 return true;
6717 }
6718 return false;
6719}
6720
Alexey Bataev49f6e782015-12-01 04:18:41 +00006721StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6722 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6723 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006724 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00006725 if (!AStmt)
6726 return StmtError();
6727
6728 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6729 OMPLoopDirective::HelperExprs B;
6730 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6731 // define the nested loops number.
6732 unsigned NestedLoopCount =
6733 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006734 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00006735 VarsWithImplicitDSA, B);
6736 if (NestedLoopCount == 0)
6737 return StmtError();
6738
6739 assert((CurContext->isDependentContext() || B.builtAll()) &&
6740 "omp for loop exprs were not built");
6741
Alexey Bataev382967a2015-12-08 12:06:20 +00006742 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6743 // The grainsize clause and num_tasks clause are mutually exclusive and may
6744 // not appear on the same taskloop directive.
6745 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6746 return StmtError();
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00006747 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6748 // If a reduction clause is present on the taskloop directive, the nogroup
6749 // clause must not be specified.
6750 if (checkReductionClauseWithNogroup(*this, Clauses))
6751 return StmtError();
Alexey Bataev382967a2015-12-08 12:06:20 +00006752
Alexey Bataev49f6e782015-12-01 04:18:41 +00006753 getCurFunction()->setHasBranchProtectedScope();
6754 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
6755 NestedLoopCount, Clauses, AStmt, B);
6756}
6757
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006758StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
6759 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6760 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006761 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006762 if (!AStmt)
6763 return StmtError();
6764
6765 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6766 OMPLoopDirective::HelperExprs B;
6767 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6768 // define the nested loops number.
6769 unsigned NestedLoopCount =
6770 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
6771 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6772 VarsWithImplicitDSA, B);
6773 if (NestedLoopCount == 0)
6774 return StmtError();
6775
6776 assert((CurContext->isDependentContext() || B.builtAll()) &&
6777 "omp for loop exprs were not built");
6778
Alexey Bataev5a3af132016-03-29 08:58:54 +00006779 if (!CurContext->isDependentContext()) {
6780 // Finalize the clauses that need pre-built expressions for CodeGen.
6781 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00006782 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev5a3af132016-03-29 08:58:54 +00006783 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006784 B.NumIterations, *this, CurScope,
6785 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00006786 return StmtError();
6787 }
6788 }
6789
Alexey Bataev382967a2015-12-08 12:06:20 +00006790 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6791 // The grainsize clause and num_tasks clause are mutually exclusive and may
6792 // not appear on the same taskloop directive.
6793 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6794 return StmtError();
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00006795 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6796 // If a reduction clause is present on the taskloop directive, the nogroup
6797 // clause must not be specified.
6798 if (checkReductionClauseWithNogroup(*this, Clauses))
6799 return StmtError();
Alexey Bataev438388c2017-11-22 18:34:02 +00006800 if (checkSimdlenSafelenSpecified(*this, Clauses))
6801 return StmtError();
Alexey Bataev382967a2015-12-08 12:06:20 +00006802
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006803 getCurFunction()->setHasBranchProtectedScope();
6804 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
6805 NestedLoopCount, Clauses, AStmt, B);
6806}
6807
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006808StmtResult Sema::ActOnOpenMPDistributeDirective(
6809 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6810 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006811 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006812 if (!AStmt)
6813 return StmtError();
6814
6815 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6816 OMPLoopDirective::HelperExprs B;
6817 // In presence of clause 'collapse' with number of loops, it will
6818 // define the nested loops number.
6819 unsigned NestedLoopCount =
6820 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
6821 nullptr /*ordered not a clause on distribute*/, AStmt,
6822 *this, *DSAStack, VarsWithImplicitDSA, B);
6823 if (NestedLoopCount == 0)
6824 return StmtError();
6825
6826 assert((CurContext->isDependentContext() || B.builtAll()) &&
6827 "omp for loop exprs were not built");
6828
6829 getCurFunction()->setHasBranchProtectedScope();
6830 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
6831 NestedLoopCount, Clauses, AStmt, B);
6832}
6833
Carlo Bertolli9925f152016-06-27 14:55:37 +00006834StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
6835 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6836 SourceLocation EndLoc,
6837 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6838 if (!AStmt)
6839 return StmtError();
6840
6841 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6842 // 1.2.2 OpenMP Language Terminology
6843 // Structured block - An executable statement with a single entry at the
6844 // top and a single exit at the bottom.
6845 // The point of exit cannot be a branch out of the structured block.
6846 // longjmp() and throw() must not violate the entry/exit criteria.
6847 CS->getCapturedDecl()->setNothrow();
Alexey Bataev7f96c372017-11-22 17:19:31 +00006848 for (int ThisCaptureLevel =
6849 getOpenMPCaptureLevels(OMPD_distribute_parallel_for);
6850 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6851 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6852 // 1.2.2 OpenMP Language Terminology
6853 // Structured block - An executable statement with a single entry at the
6854 // top and a single exit at the bottom.
6855 // The point of exit cannot be a branch out of the structured block.
6856 // longjmp() and throw() must not violate the entry/exit criteria.
6857 CS->getCapturedDecl()->setNothrow();
6858 }
Carlo Bertolli9925f152016-06-27 14:55:37 +00006859
6860 OMPLoopDirective::HelperExprs B;
6861 // In presence of clause 'collapse' with number of loops, it will
6862 // define the nested loops number.
6863 unsigned NestedLoopCount = CheckOpenMPLoop(
6864 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataev7f96c372017-11-22 17:19:31 +00006865 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Carlo Bertolli9925f152016-06-27 14:55:37 +00006866 VarsWithImplicitDSA, B);
6867 if (NestedLoopCount == 0)
6868 return StmtError();
6869
6870 assert((CurContext->isDependentContext() || B.builtAll()) &&
6871 "omp for loop exprs were not built");
6872
6873 getCurFunction()->setHasBranchProtectedScope();
6874 return OMPDistributeParallelForDirective::Create(
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00006875 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
6876 DSAStack->isCancelRegion());
Carlo Bertolli9925f152016-06-27 14:55:37 +00006877}
6878
Kelvin Li4a39add2016-07-05 05:00:15 +00006879StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
6880 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6881 SourceLocation EndLoc,
6882 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6883 if (!AStmt)
6884 return StmtError();
6885
6886 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6887 // 1.2.2 OpenMP Language Terminology
6888 // Structured block - An executable statement with a single entry at the
6889 // top and a single exit at the bottom.
6890 // The point of exit cannot be a branch out of the structured block.
6891 // longjmp() and throw() must not violate the entry/exit criteria.
6892 CS->getCapturedDecl()->setNothrow();
Alexey Bataev974acd62017-11-27 19:38:52 +00006893 for (int ThisCaptureLevel =
6894 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd);
6895 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6896 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6897 // 1.2.2 OpenMP Language Terminology
6898 // Structured block - An executable statement with a single entry at the
6899 // top and a single exit at the bottom.
6900 // The point of exit cannot be a branch out of the structured block.
6901 // longjmp() and throw() must not violate the entry/exit criteria.
6902 CS->getCapturedDecl()->setNothrow();
6903 }
Kelvin Li4a39add2016-07-05 05:00:15 +00006904
6905 OMPLoopDirective::HelperExprs B;
6906 // In presence of clause 'collapse' with number of loops, it will
6907 // define the nested loops number.
6908 unsigned NestedLoopCount = CheckOpenMPLoop(
6909 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev974acd62017-11-27 19:38:52 +00006910 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li4a39add2016-07-05 05:00:15 +00006911 VarsWithImplicitDSA, B);
6912 if (NestedLoopCount == 0)
6913 return StmtError();
6914
6915 assert((CurContext->isDependentContext() || B.builtAll()) &&
6916 "omp for loop exprs were not built");
6917
Alexey Bataev438388c2017-11-22 18:34:02 +00006918 if (!CurContext->isDependentContext()) {
6919 // Finalize the clauses that need pre-built expressions for CodeGen.
6920 for (auto C : Clauses) {
6921 if (auto *LC = dyn_cast<OMPLinearClause>(C))
6922 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6923 B.NumIterations, *this, CurScope,
6924 DSAStack))
6925 return StmtError();
6926 }
6927 }
6928
Kelvin Lic5609492016-07-15 04:39:07 +00006929 if (checkSimdlenSafelenSpecified(*this, Clauses))
6930 return StmtError();
6931
Kelvin Li4a39add2016-07-05 05:00:15 +00006932 getCurFunction()->setHasBranchProtectedScope();
6933 return OMPDistributeParallelForSimdDirective::Create(
6934 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
6935}
6936
Kelvin Li787f3fc2016-07-06 04:45:38 +00006937StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
6938 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6939 SourceLocation EndLoc,
6940 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6941 if (!AStmt)
6942 return StmtError();
6943
6944 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6945 // 1.2.2 OpenMP Language Terminology
6946 // Structured block - An executable statement with a single entry at the
6947 // top and a single exit at the bottom.
6948 // The point of exit cannot be a branch out of the structured block.
6949 // longjmp() and throw() must not violate the entry/exit criteria.
6950 CS->getCapturedDecl()->setNothrow();
Alexey Bataev617db5f2017-12-04 15:38:33 +00006951 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd);
6952 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6953 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6954 // 1.2.2 OpenMP Language Terminology
6955 // Structured block - An executable statement with a single entry at the
6956 // top and a single exit at the bottom.
6957 // The point of exit cannot be a branch out of the structured block.
6958 // longjmp() and throw() must not violate the entry/exit criteria.
6959 CS->getCapturedDecl()->setNothrow();
6960 }
Kelvin Li787f3fc2016-07-06 04:45:38 +00006961
6962 OMPLoopDirective::HelperExprs B;
6963 // In presence of clause 'collapse' with number of loops, it will
6964 // define the nested loops number.
6965 unsigned NestedLoopCount =
6966 CheckOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev617db5f2017-12-04 15:38:33 +00006967 nullptr /*ordered not a clause on distribute*/, CS, *this,
6968 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li787f3fc2016-07-06 04:45:38 +00006969 if (NestedLoopCount == 0)
6970 return StmtError();
6971
6972 assert((CurContext->isDependentContext() || B.builtAll()) &&
6973 "omp for loop exprs were not built");
6974
Alexey Bataev438388c2017-11-22 18:34:02 +00006975 if (!CurContext->isDependentContext()) {
6976 // Finalize the clauses that need pre-built expressions for CodeGen.
6977 for (auto C : Clauses) {
6978 if (auto *LC = dyn_cast<OMPLinearClause>(C))
6979 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6980 B.NumIterations, *this, CurScope,
6981 DSAStack))
6982 return StmtError();
6983 }
6984 }
6985
Kelvin Lic5609492016-07-15 04:39:07 +00006986 if (checkSimdlenSafelenSpecified(*this, Clauses))
6987 return StmtError();
6988
Kelvin Li787f3fc2016-07-06 04:45:38 +00006989 getCurFunction()->setHasBranchProtectedScope();
6990 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
6991 NestedLoopCount, Clauses, AStmt, B);
6992}
6993
Kelvin Lia579b912016-07-14 02:54:56 +00006994StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
6995 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6996 SourceLocation EndLoc,
6997 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6998 if (!AStmt)
6999 return StmtError();
7000
7001 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7002 // 1.2.2 OpenMP Language Terminology
7003 // Structured block - An executable statement with a single entry at the
7004 // top and a single exit at the bottom.
7005 // The point of exit cannot be a branch out of the structured block.
7006 // longjmp() and throw() must not violate the entry/exit criteria.
7007 CS->getCapturedDecl()->setNothrow();
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007008 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
7009 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7010 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7011 // 1.2.2 OpenMP Language Terminology
7012 // Structured block - An executable statement with a single entry at the
7013 // top and a single exit at the bottom.
7014 // The point of exit cannot be a branch out of the structured block.
7015 // longjmp() and throw() must not violate the entry/exit criteria.
7016 CS->getCapturedDecl()->setNothrow();
7017 }
Kelvin Lia579b912016-07-14 02:54:56 +00007018
7019 OMPLoopDirective::HelperExprs B;
7020 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7021 // define the nested loops number.
7022 unsigned NestedLoopCount = CheckOpenMPLoop(
7023 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007024 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Kelvin Lia579b912016-07-14 02:54:56 +00007025 VarsWithImplicitDSA, B);
7026 if (NestedLoopCount == 0)
7027 return StmtError();
7028
7029 assert((CurContext->isDependentContext() || B.builtAll()) &&
7030 "omp target parallel for simd loop exprs were not built");
7031
7032 if (!CurContext->isDependentContext()) {
7033 // Finalize the clauses that need pre-built expressions for CodeGen.
7034 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007035 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Lia579b912016-07-14 02:54:56 +00007036 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7037 B.NumIterations, *this, CurScope,
7038 DSAStack))
7039 return StmtError();
7040 }
7041 }
Kelvin Lic5609492016-07-15 04:39:07 +00007042 if (checkSimdlenSafelenSpecified(*this, Clauses))
Kelvin Lia579b912016-07-14 02:54:56 +00007043 return StmtError();
7044
7045 getCurFunction()->setHasBranchProtectedScope();
7046 return OMPTargetParallelForSimdDirective::Create(
7047 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7048}
7049
Kelvin Li986330c2016-07-20 22:57:10 +00007050StmtResult Sema::ActOnOpenMPTargetSimdDirective(
7051 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7052 SourceLocation EndLoc,
7053 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7054 if (!AStmt)
7055 return StmtError();
7056
7057 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7058 // 1.2.2 OpenMP Language Terminology
7059 // Structured block - An executable statement with a single entry at the
7060 // top and a single exit at the bottom.
7061 // The point of exit cannot be a branch out of the structured block.
7062 // longjmp() and throw() must not violate the entry/exit criteria.
7063 CS->getCapturedDecl()->setNothrow();
Alexey Bataevf8365372017-11-17 17:57:25 +00007064 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd);
7065 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7066 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7067 // 1.2.2 OpenMP Language Terminology
7068 // Structured block - An executable statement with a single entry at the
7069 // top and a single exit at the bottom.
7070 // The point of exit cannot be a branch out of the structured block.
7071 // longjmp() and throw() must not violate the entry/exit criteria.
7072 CS->getCapturedDecl()->setNothrow();
7073 }
7074
Kelvin Li986330c2016-07-20 22:57:10 +00007075 OMPLoopDirective::HelperExprs B;
7076 // In presence of clause 'collapse' with number of loops, it will define the
7077 // nested loops number.
David Majnemer9d168222016-08-05 17:44:54 +00007078 unsigned NestedLoopCount =
Kelvin Li986330c2016-07-20 22:57:10 +00007079 CheckOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
Alexey Bataevf8365372017-11-17 17:57:25 +00007080 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Kelvin Li986330c2016-07-20 22:57:10 +00007081 VarsWithImplicitDSA, B);
7082 if (NestedLoopCount == 0)
7083 return StmtError();
7084
7085 assert((CurContext->isDependentContext() || B.builtAll()) &&
7086 "omp target simd loop exprs were not built");
7087
7088 if (!CurContext->isDependentContext()) {
7089 // Finalize the clauses that need pre-built expressions for CodeGen.
7090 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007091 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Li986330c2016-07-20 22:57:10 +00007092 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7093 B.NumIterations, *this, CurScope,
7094 DSAStack))
7095 return StmtError();
7096 }
7097 }
7098
7099 if (checkSimdlenSafelenSpecified(*this, Clauses))
7100 return StmtError();
7101
7102 getCurFunction()->setHasBranchProtectedScope();
7103 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
7104 NestedLoopCount, Clauses, AStmt, B);
7105}
7106
Kelvin Li02532872016-08-05 14:37:37 +00007107StmtResult Sema::ActOnOpenMPTeamsDistributeDirective(
7108 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7109 SourceLocation EndLoc,
7110 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7111 if (!AStmt)
7112 return StmtError();
7113
7114 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7115 // 1.2.2 OpenMP Language Terminology
7116 // Structured block - An executable statement with a single entry at the
7117 // top and a single exit at the bottom.
7118 // The point of exit cannot be a branch out of the structured block.
7119 // longjmp() and throw() must not violate the entry/exit criteria.
7120 CS->getCapturedDecl()->setNothrow();
Alexey Bataev95c6dd42017-11-29 15:14:16 +00007121 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute);
7122 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7123 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7124 // 1.2.2 OpenMP Language Terminology
7125 // Structured block - An executable statement with a single entry at the
7126 // top and a single exit at the bottom.
7127 // The point of exit cannot be a branch out of the structured block.
7128 // longjmp() and throw() must not violate the entry/exit criteria.
7129 CS->getCapturedDecl()->setNothrow();
7130 }
Kelvin Li02532872016-08-05 14:37:37 +00007131
7132 OMPLoopDirective::HelperExprs B;
7133 // In presence of clause 'collapse' with number of loops, it will
7134 // define the nested loops number.
7135 unsigned NestedLoopCount =
7136 CheckOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses),
Alexey Bataev95c6dd42017-11-29 15:14:16 +00007137 nullptr /*ordered not a clause on distribute*/, CS, *this,
7138 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li02532872016-08-05 14:37:37 +00007139 if (NestedLoopCount == 0)
7140 return StmtError();
7141
7142 assert((CurContext->isDependentContext() || B.builtAll()) &&
7143 "omp teams distribute loop exprs were not built");
7144
7145 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007146
7147 DSAStack->setParentTeamsRegionLoc(StartLoc);
7148
David Majnemer9d168222016-08-05 17:44:54 +00007149 return OMPTeamsDistributeDirective::Create(
7150 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Kelvin Li02532872016-08-05 14:37:37 +00007151}
7152
Kelvin Li4e325f72016-10-25 12:50:55 +00007153StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective(
7154 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7155 SourceLocation EndLoc,
7156 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7157 if (!AStmt)
7158 return StmtError();
7159
7160 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7161 // 1.2.2 OpenMP Language Terminology
7162 // Structured block - An executable statement with a single entry at the
7163 // top and a single exit at the bottom.
7164 // The point of exit cannot be a branch out of the structured block.
7165 // longjmp() and throw() must not violate the entry/exit criteria.
7166 CS->getCapturedDecl()->setNothrow();
Alexey Bataev999277a2017-12-06 14:31:09 +00007167 for (int ThisCaptureLevel =
7168 getOpenMPCaptureLevels(OMPD_teams_distribute_simd);
7169 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7170 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7171 // 1.2.2 OpenMP Language Terminology
7172 // Structured block - An executable statement with a single entry at the
7173 // top and a single exit at the bottom.
7174 // The point of exit cannot be a branch out of the structured block.
7175 // longjmp() and throw() must not violate the entry/exit criteria.
7176 CS->getCapturedDecl()->setNothrow();
7177 }
7178
Kelvin Li4e325f72016-10-25 12:50:55 +00007179
7180 OMPLoopDirective::HelperExprs B;
7181 // In presence of clause 'collapse' with number of loops, it will
7182 // define the nested loops number.
Samuel Antao4c8035b2016-12-12 18:00:20 +00007183 unsigned NestedLoopCount = CheckOpenMPLoop(
7184 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev999277a2017-12-06 14:31:09 +00007185 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Samuel Antao4c8035b2016-12-12 18:00:20 +00007186 VarsWithImplicitDSA, B);
Kelvin Li4e325f72016-10-25 12:50:55 +00007187
7188 if (NestedLoopCount == 0)
7189 return StmtError();
7190
7191 assert((CurContext->isDependentContext() || B.builtAll()) &&
7192 "omp teams distribute simd loop exprs were not built");
7193
7194 if (!CurContext->isDependentContext()) {
7195 // Finalize the clauses that need pre-built expressions for CodeGen.
7196 for (auto C : Clauses) {
7197 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7198 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7199 B.NumIterations, *this, CurScope,
7200 DSAStack))
7201 return StmtError();
7202 }
7203 }
7204
7205 if (checkSimdlenSafelenSpecified(*this, Clauses))
7206 return StmtError();
7207
7208 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007209
7210 DSAStack->setParentTeamsRegionLoc(StartLoc);
7211
Kelvin Li4e325f72016-10-25 12:50:55 +00007212 return OMPTeamsDistributeSimdDirective::Create(
7213 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7214}
7215
Kelvin Li579e41c2016-11-30 23:51:03 +00007216StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective(
7217 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7218 SourceLocation EndLoc,
7219 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7220 if (!AStmt)
7221 return StmtError();
7222
7223 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7224 // 1.2.2 OpenMP Language Terminology
7225 // Structured block - An executable statement with a single entry at the
7226 // top and a single exit at the bottom.
7227 // The point of exit cannot be a branch out of the structured block.
7228 // longjmp() and throw() must not violate the entry/exit criteria.
7229 CS->getCapturedDecl()->setNothrow();
7230
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00007231 for (int ThisCaptureLevel =
7232 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd);
7233 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7234 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7235 // 1.2.2 OpenMP Language Terminology
7236 // Structured block - An executable statement with a single entry at the
7237 // top and a single exit at the bottom.
7238 // The point of exit cannot be a branch out of the structured block.
7239 // longjmp() and throw() must not violate the entry/exit criteria.
7240 CS->getCapturedDecl()->setNothrow();
7241 }
7242
Kelvin Li579e41c2016-11-30 23:51:03 +00007243 OMPLoopDirective::HelperExprs B;
7244 // In presence of clause 'collapse' with number of loops, it will
7245 // define the nested loops number.
7246 auto NestedLoopCount = CheckOpenMPLoop(
7247 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00007248 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li579e41c2016-11-30 23:51:03 +00007249 VarsWithImplicitDSA, B);
7250
7251 if (NestedLoopCount == 0)
7252 return StmtError();
7253
7254 assert((CurContext->isDependentContext() || B.builtAll()) &&
7255 "omp for loop exprs were not built");
7256
7257 if (!CurContext->isDependentContext()) {
7258 // Finalize the clauses that need pre-built expressions for CodeGen.
7259 for (auto C : Clauses) {
7260 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7261 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7262 B.NumIterations, *this, CurScope,
7263 DSAStack))
7264 return StmtError();
7265 }
7266 }
7267
7268 if (checkSimdlenSafelenSpecified(*this, Clauses))
7269 return StmtError();
7270
7271 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007272
7273 DSAStack->setParentTeamsRegionLoc(StartLoc);
7274
Kelvin Li579e41c2016-11-30 23:51:03 +00007275 return OMPTeamsDistributeParallelForSimdDirective::Create(
7276 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7277}
7278
Kelvin Li7ade93f2016-12-09 03:24:30 +00007279StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective(
7280 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7281 SourceLocation EndLoc,
7282 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7283 if (!AStmt)
7284 return StmtError();
7285
7286 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7287 // 1.2.2 OpenMP Language Terminology
7288 // Structured block - An executable statement with a single entry at the
7289 // top and a single exit at the bottom.
7290 // The point of exit cannot be a branch out of the structured block.
7291 // longjmp() and throw() must not violate the entry/exit criteria.
7292 CS->getCapturedDecl()->setNothrow();
7293
Carlo Bertolli62fae152017-11-20 20:46:39 +00007294 for (int ThisCaptureLevel =
7295 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for);
7296 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7297 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7298 // 1.2.2 OpenMP Language Terminology
7299 // Structured block - An executable statement with a single entry at the
7300 // top and a single exit at the bottom.
7301 // The point of exit cannot be a branch out of the structured block.
7302 // longjmp() and throw() must not violate the entry/exit criteria.
7303 CS->getCapturedDecl()->setNothrow();
7304 }
7305
Kelvin Li7ade93f2016-12-09 03:24:30 +00007306 OMPLoopDirective::HelperExprs B;
7307 // In presence of clause 'collapse' with number of loops, it will
7308 // define the nested loops number.
7309 unsigned NestedLoopCount = CheckOpenMPLoop(
7310 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
Carlo Bertolli62fae152017-11-20 20:46:39 +00007311 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li7ade93f2016-12-09 03:24:30 +00007312 VarsWithImplicitDSA, B);
7313
7314 if (NestedLoopCount == 0)
7315 return StmtError();
7316
7317 assert((CurContext->isDependentContext() || B.builtAll()) &&
7318 "omp for loop exprs were not built");
7319
Kelvin Li7ade93f2016-12-09 03:24:30 +00007320 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007321
7322 DSAStack->setParentTeamsRegionLoc(StartLoc);
7323
Kelvin Li7ade93f2016-12-09 03:24:30 +00007324 return OMPTeamsDistributeParallelForDirective::Create(
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00007325 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7326 DSAStack->isCancelRegion());
Kelvin Li7ade93f2016-12-09 03:24:30 +00007327}
7328
Kelvin Libf594a52016-12-17 05:48:59 +00007329StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses,
7330 Stmt *AStmt,
7331 SourceLocation StartLoc,
7332 SourceLocation EndLoc) {
7333 if (!AStmt)
7334 return StmtError();
7335
7336 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7337 // 1.2.2 OpenMP Language Terminology
7338 // Structured block - An executable statement with a single entry at the
7339 // top and a single exit at the bottom.
7340 // The point of exit cannot be a branch out of the structured block.
7341 // longjmp() and throw() must not violate the entry/exit criteria.
7342 CS->getCapturedDecl()->setNothrow();
7343
Alexey Bataevf9fc42e2017-11-22 14:25:55 +00007344 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams);
7345 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7346 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7347 // 1.2.2 OpenMP Language Terminology
7348 // Structured block - An executable statement with a single entry at the
7349 // top and a single exit at the bottom.
7350 // The point of exit cannot be a branch out of the structured block.
7351 // longjmp() and throw() must not violate the entry/exit criteria.
7352 CS->getCapturedDecl()->setNothrow();
7353 }
Kelvin Libf594a52016-12-17 05:48:59 +00007354 getCurFunction()->setHasBranchProtectedScope();
7355
7356 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses,
7357 AStmt);
7358}
7359
Kelvin Li83c451e2016-12-25 04:52:54 +00007360StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective(
7361 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7362 SourceLocation EndLoc,
7363 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7364 if (!AStmt)
7365 return StmtError();
7366
7367 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7368 // 1.2.2 OpenMP Language Terminology
7369 // Structured block - An executable statement with a single entry at the
7370 // top and a single exit at the bottom.
7371 // The point of exit cannot be a branch out of the structured block.
7372 // longjmp() and throw() must not violate the entry/exit criteria.
7373 CS->getCapturedDecl()->setNothrow();
Alexey Bataevdfa430f2017-12-08 15:03:50 +00007374 for (int ThisCaptureLevel =
7375 getOpenMPCaptureLevels(OMPD_target_teams_distribute);
7376 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7377 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7378 // 1.2.2 OpenMP Language Terminology
7379 // Structured block - An executable statement with a single entry at the
7380 // top and a single exit at the bottom.
7381 // The point of exit cannot be a branch out of the structured block.
7382 // longjmp() and throw() must not violate the entry/exit criteria.
7383 CS->getCapturedDecl()->setNothrow();
7384 }
Kelvin Li83c451e2016-12-25 04:52:54 +00007385
7386 OMPLoopDirective::HelperExprs B;
7387 // In presence of clause 'collapse' with number of loops, it will
7388 // define the nested loops number.
7389 auto NestedLoopCount = CheckOpenMPLoop(
Alexey Bataevdfa430f2017-12-08 15:03:50 +00007390 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses),
7391 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li83c451e2016-12-25 04:52:54 +00007392 VarsWithImplicitDSA, B);
7393 if (NestedLoopCount == 0)
7394 return StmtError();
7395
7396 assert((CurContext->isDependentContext() || B.builtAll()) &&
7397 "omp target teams distribute loop exprs were not built");
7398
7399 getCurFunction()->setHasBranchProtectedScope();
7400 return OMPTargetTeamsDistributeDirective::Create(
7401 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7402}
7403
Kelvin Li80e8f562016-12-29 22:16:30 +00007404StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective(
7405 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7406 SourceLocation EndLoc,
7407 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7408 if (!AStmt)
7409 return StmtError();
7410
7411 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7412 // 1.2.2 OpenMP Language Terminology
7413 // Structured block - An executable statement with a single entry at the
7414 // top and a single exit at the bottom.
7415 // The point of exit cannot be a branch out of the structured block.
7416 // longjmp() and throw() must not violate the entry/exit criteria.
7417 CS->getCapturedDecl()->setNothrow();
7418
Carlo Bertolli52978c32018-01-03 21:12:44 +00007419 for (int ThisCaptureLevel =
7420 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for);
7421 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7422 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7423 // 1.2.2 OpenMP Language Terminology
7424 // Structured block - An executable statement with a single entry at the
7425 // top and a single exit at the bottom.
7426 // The point of exit cannot be a branch out of the structured block.
7427 // longjmp() and throw() must not violate the entry/exit criteria.
7428 CS->getCapturedDecl()->setNothrow();
7429 }
7430
Kelvin Li80e8f562016-12-29 22:16:30 +00007431 OMPLoopDirective::HelperExprs B;
7432 // In presence of clause 'collapse' with number of loops, it will
7433 // define the nested loops number.
7434 auto NestedLoopCount = CheckOpenMPLoop(
Carlo Bertolli52978c32018-01-03 21:12:44 +00007435 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
7436 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li80e8f562016-12-29 22:16:30 +00007437 VarsWithImplicitDSA, B);
7438 if (NestedLoopCount == 0)
7439 return StmtError();
7440
7441 assert((CurContext->isDependentContext() || B.builtAll()) &&
7442 "omp target teams distribute parallel for loop exprs were not built");
7443
Kelvin Li80e8f562016-12-29 22:16:30 +00007444 getCurFunction()->setHasBranchProtectedScope();
7445 return OMPTargetTeamsDistributeParallelForDirective::Create(
Alexey Bataev16e79882017-11-22 21:12:03 +00007446 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7447 DSAStack->isCancelRegion());
Kelvin Li80e8f562016-12-29 22:16:30 +00007448}
7449
Kelvin Li1851df52017-01-03 05:23:48 +00007450StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
7451 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7452 SourceLocation EndLoc,
7453 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7454 if (!AStmt)
7455 return StmtError();
7456
7457 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7458 // 1.2.2 OpenMP Language Terminology
7459 // Structured block - An executable statement with a single entry at the
7460 // top and a single exit at the bottom.
7461 // The point of exit cannot be a branch out of the structured block.
7462 // longjmp() and throw() must not violate the entry/exit criteria.
7463 CS->getCapturedDecl()->setNothrow();
7464
7465 OMPLoopDirective::HelperExprs B;
7466 // In presence of clause 'collapse' with number of loops, it will
7467 // define the nested loops number.
7468 auto NestedLoopCount = CheckOpenMPLoop(
7469 OMPD_target_teams_distribute_parallel_for_simd,
7470 getCollapseNumberExpr(Clauses),
7471 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
7472 VarsWithImplicitDSA, B);
7473 if (NestedLoopCount == 0)
7474 return StmtError();
7475
7476 assert((CurContext->isDependentContext() || B.builtAll()) &&
7477 "omp target teams distribute parallel for simd loop exprs were not "
7478 "built");
7479
7480 if (!CurContext->isDependentContext()) {
7481 // Finalize the clauses that need pre-built expressions for CodeGen.
7482 for (auto C : Clauses) {
7483 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7484 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7485 B.NumIterations, *this, CurScope,
7486 DSAStack))
7487 return StmtError();
7488 }
7489 }
7490
Alexey Bataev438388c2017-11-22 18:34:02 +00007491 if (checkSimdlenSafelenSpecified(*this, Clauses))
7492 return StmtError();
7493
Kelvin Li1851df52017-01-03 05:23:48 +00007494 getCurFunction()->setHasBranchProtectedScope();
7495 return OMPTargetTeamsDistributeParallelForSimdDirective::Create(
7496 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7497}
7498
Kelvin Lida681182017-01-10 18:08:18 +00007499StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective(
7500 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7501 SourceLocation EndLoc,
7502 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7503 if (!AStmt)
7504 return StmtError();
7505
7506 auto *CS = cast<CapturedStmt>(AStmt);
7507 // 1.2.2 OpenMP Language Terminology
7508 // Structured block - An executable statement with a single entry at the
7509 // top and a single exit at the bottom.
7510 // The point of exit cannot be a branch out of the structured block.
7511 // longjmp() and throw() must not violate the entry/exit criteria.
7512 CS->getCapturedDecl()->setNothrow();
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00007513 for (int ThisCaptureLevel =
7514 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd);
7515 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7516 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7517 // 1.2.2 OpenMP Language Terminology
7518 // Structured block - An executable statement with a single entry at the
7519 // top and a single exit at the bottom.
7520 // The point of exit cannot be a branch out of the structured block.
7521 // longjmp() and throw() must not violate the entry/exit criteria.
7522 CS->getCapturedDecl()->setNothrow();
7523 }
Kelvin Lida681182017-01-10 18:08:18 +00007524
7525 OMPLoopDirective::HelperExprs B;
7526 // In presence of clause 'collapse' with number of loops, it will
7527 // define the nested loops number.
7528 auto NestedLoopCount = CheckOpenMPLoop(
7529 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00007530 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Lida681182017-01-10 18:08:18 +00007531 VarsWithImplicitDSA, B);
7532 if (NestedLoopCount == 0)
7533 return StmtError();
7534
7535 assert((CurContext->isDependentContext() || B.builtAll()) &&
7536 "omp target teams distribute simd loop exprs were not built");
7537
Alexey Bataev438388c2017-11-22 18:34:02 +00007538 if (!CurContext->isDependentContext()) {
7539 // Finalize the clauses that need pre-built expressions for CodeGen.
7540 for (auto C : Clauses) {
7541 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7542 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7543 B.NumIterations, *this, CurScope,
7544 DSAStack))
7545 return StmtError();
7546 }
7547 }
7548
7549 if (checkSimdlenSafelenSpecified(*this, Clauses))
7550 return StmtError();
7551
Kelvin Lida681182017-01-10 18:08:18 +00007552 getCurFunction()->setHasBranchProtectedScope();
7553 return OMPTargetTeamsDistributeSimdDirective::Create(
7554 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7555}
7556
Alexey Bataeved09d242014-05-28 05:53:51 +00007557OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007558 SourceLocation StartLoc,
7559 SourceLocation LParenLoc,
7560 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007561 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007562 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00007563 case OMPC_final:
7564 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
7565 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00007566 case OMPC_num_threads:
7567 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
7568 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007569 case OMPC_safelen:
7570 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
7571 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00007572 case OMPC_simdlen:
7573 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
7574 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00007575 case OMPC_collapse:
7576 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
7577 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00007578 case OMPC_ordered:
7579 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
7580 break;
Michael Wonge710d542015-08-07 16:16:36 +00007581 case OMPC_device:
7582 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
7583 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00007584 case OMPC_num_teams:
7585 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
7586 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007587 case OMPC_thread_limit:
7588 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
7589 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00007590 case OMPC_priority:
7591 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
7592 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007593 case OMPC_grainsize:
7594 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
7595 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00007596 case OMPC_num_tasks:
7597 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
7598 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00007599 case OMPC_hint:
7600 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
7601 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007602 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007603 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007604 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007605 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007606 case OMPC_private:
7607 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00007608 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007609 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007610 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00007611 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00007612 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00007613 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007614 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007615 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007616 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00007617 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007618 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007619 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007620 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007621 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007622 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007623 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007624 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007625 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007626 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007627 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00007628 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007629 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007630 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00007631 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007632 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007633 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007634 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007635 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007636 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007637 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007638 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007639 case OMPC_is_device_ptr:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007640 llvm_unreachable("Clause is not allowed.");
7641 }
7642 return Res;
7643}
7644
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00007645// An OpenMP directive such as 'target parallel' has two captured regions:
7646// for the 'target' and 'parallel' respectively. This function returns
7647// the region in which to capture expressions associated with a clause.
7648// A return value of OMPD_unknown signifies that the expression should not
7649// be captured.
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00007650static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
7651 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
7652 OpenMPDirectiveKind NameModifier = OMPD_unknown) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00007653 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00007654 switch (CKind) {
7655 case OMPC_if:
7656 switch (DKind) {
7657 case OMPD_target_parallel:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00007658 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007659 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00007660 // If this clause applies to the nested 'parallel' region, capture within
7661 // the 'target' region, otherwise do not capture.
7662 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
7663 CaptureRegion = OMPD_target;
7664 break;
Carlo Bertolli52978c32018-01-03 21:12:44 +00007665 case OMPD_target_teams_distribute_parallel_for:
7666 case OMPD_target_teams_distribute_parallel_for_simd:
7667 // If this clause applies to the nested 'parallel' region, capture within
7668 // the 'teams' region, otherwise do not capture.
7669 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
7670 CaptureRegion = OMPD_teams;
7671 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +00007672 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00007673 case OMPD_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00007674 CaptureRegion = OMPD_teams;
7675 break;
Alexey Bataevd2202ca2017-12-27 17:58:32 +00007676 case OMPD_target_update:
Alexey Bataevfab20e42017-12-27 18:49:38 +00007677 case OMPD_target_enter_data:
7678 case OMPD_target_exit_data:
Alexey Bataevd2202ca2017-12-27 17:58:32 +00007679 CaptureRegion = OMPD_task;
7680 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00007681 case OMPD_cancel:
7682 case OMPD_parallel:
7683 case OMPD_parallel_sections:
7684 case OMPD_parallel_for:
7685 case OMPD_parallel_for_simd:
7686 case OMPD_target:
7687 case OMPD_target_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00007688 case OMPD_target_teams:
7689 case OMPD_target_teams_distribute:
7690 case OMPD_target_teams_distribute_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00007691 case OMPD_distribute_parallel_for:
7692 case OMPD_distribute_parallel_for_simd:
7693 case OMPD_task:
7694 case OMPD_taskloop:
7695 case OMPD_taskloop_simd:
7696 case OMPD_target_data:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00007697 // Do not capture if-clause expressions.
7698 break;
7699 case OMPD_threadprivate:
7700 case OMPD_taskyield:
7701 case OMPD_barrier:
7702 case OMPD_taskwait:
7703 case OMPD_cancellation_point:
7704 case OMPD_flush:
7705 case OMPD_declare_reduction:
7706 case OMPD_declare_simd:
7707 case OMPD_declare_target:
7708 case OMPD_end_declare_target:
7709 case OMPD_teams:
7710 case OMPD_simd:
7711 case OMPD_for:
7712 case OMPD_for_simd:
7713 case OMPD_sections:
7714 case OMPD_section:
7715 case OMPD_single:
7716 case OMPD_master:
7717 case OMPD_critical:
7718 case OMPD_taskgroup:
7719 case OMPD_distribute:
7720 case OMPD_ordered:
7721 case OMPD_atomic:
7722 case OMPD_distribute_simd:
7723 case OMPD_teams_distribute:
7724 case OMPD_teams_distribute_simd:
7725 llvm_unreachable("Unexpected OpenMP directive with if-clause");
7726 case OMPD_unknown:
7727 llvm_unreachable("Unknown OpenMP directive");
7728 }
7729 break;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00007730 case OMPC_num_threads:
7731 switch (DKind) {
7732 case OMPD_target_parallel:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00007733 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007734 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00007735 CaptureRegion = OMPD_target;
7736 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +00007737 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00007738 case OMPD_teams_distribute_parallel_for_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00007739 case OMPD_target_teams_distribute_parallel_for:
7740 case OMPD_target_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00007741 CaptureRegion = OMPD_teams;
7742 break;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00007743 case OMPD_parallel:
7744 case OMPD_parallel_sections:
7745 case OMPD_parallel_for:
7746 case OMPD_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00007747 case OMPD_distribute_parallel_for:
7748 case OMPD_distribute_parallel_for_simd:
7749 // Do not capture num_threads-clause expressions.
7750 break;
7751 case OMPD_target_data:
7752 case OMPD_target_enter_data:
7753 case OMPD_target_exit_data:
7754 case OMPD_target_update:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00007755 case OMPD_target:
7756 case OMPD_target_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00007757 case OMPD_target_teams:
7758 case OMPD_target_teams_distribute:
7759 case OMPD_target_teams_distribute_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00007760 case OMPD_cancel:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00007761 case OMPD_task:
7762 case OMPD_taskloop:
7763 case OMPD_taskloop_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00007764 case OMPD_threadprivate:
7765 case OMPD_taskyield:
7766 case OMPD_barrier:
7767 case OMPD_taskwait:
7768 case OMPD_cancellation_point:
7769 case OMPD_flush:
7770 case OMPD_declare_reduction:
7771 case OMPD_declare_simd:
7772 case OMPD_declare_target:
7773 case OMPD_end_declare_target:
7774 case OMPD_teams:
7775 case OMPD_simd:
7776 case OMPD_for:
7777 case OMPD_for_simd:
7778 case OMPD_sections:
7779 case OMPD_section:
7780 case OMPD_single:
7781 case OMPD_master:
7782 case OMPD_critical:
7783 case OMPD_taskgroup:
7784 case OMPD_distribute:
7785 case OMPD_ordered:
7786 case OMPD_atomic:
7787 case OMPD_distribute_simd:
7788 case OMPD_teams_distribute:
7789 case OMPD_teams_distribute_simd:
7790 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause");
7791 case OMPD_unknown:
7792 llvm_unreachable("Unknown OpenMP directive");
7793 }
7794 break;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00007795 case OMPC_num_teams:
7796 switch (DKind) {
7797 case OMPD_target_teams:
Alexey Bataev2ba67042017-11-28 21:11:44 +00007798 case OMPD_target_teams_distribute:
7799 case OMPD_target_teams_distribute_simd:
7800 case OMPD_target_teams_distribute_parallel_for:
7801 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00007802 CaptureRegion = OMPD_target;
7803 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00007804 case OMPD_teams_distribute_parallel_for:
7805 case OMPD_teams_distribute_parallel_for_simd:
7806 case OMPD_teams:
7807 case OMPD_teams_distribute:
7808 case OMPD_teams_distribute_simd:
7809 // Do not capture num_teams-clause expressions.
7810 break;
7811 case OMPD_distribute_parallel_for:
7812 case OMPD_distribute_parallel_for_simd:
7813 case OMPD_task:
7814 case OMPD_taskloop:
7815 case OMPD_taskloop_simd:
7816 case OMPD_target_data:
7817 case OMPD_target_enter_data:
7818 case OMPD_target_exit_data:
7819 case OMPD_target_update:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00007820 case OMPD_cancel:
7821 case OMPD_parallel:
7822 case OMPD_parallel_sections:
7823 case OMPD_parallel_for:
7824 case OMPD_parallel_for_simd:
7825 case OMPD_target:
7826 case OMPD_target_simd:
7827 case OMPD_target_parallel:
7828 case OMPD_target_parallel_for:
7829 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00007830 case OMPD_threadprivate:
7831 case OMPD_taskyield:
7832 case OMPD_barrier:
7833 case OMPD_taskwait:
7834 case OMPD_cancellation_point:
7835 case OMPD_flush:
7836 case OMPD_declare_reduction:
7837 case OMPD_declare_simd:
7838 case OMPD_declare_target:
7839 case OMPD_end_declare_target:
7840 case OMPD_simd:
7841 case OMPD_for:
7842 case OMPD_for_simd:
7843 case OMPD_sections:
7844 case OMPD_section:
7845 case OMPD_single:
7846 case OMPD_master:
7847 case OMPD_critical:
7848 case OMPD_taskgroup:
7849 case OMPD_distribute:
7850 case OMPD_ordered:
7851 case OMPD_atomic:
7852 case OMPD_distribute_simd:
7853 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
7854 case OMPD_unknown:
7855 llvm_unreachable("Unknown OpenMP directive");
7856 }
7857 break;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00007858 case OMPC_thread_limit:
7859 switch (DKind) {
7860 case OMPD_target_teams:
Alexey Bataev2ba67042017-11-28 21:11:44 +00007861 case OMPD_target_teams_distribute:
7862 case OMPD_target_teams_distribute_simd:
7863 case OMPD_target_teams_distribute_parallel_for:
7864 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00007865 CaptureRegion = OMPD_target;
7866 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00007867 case OMPD_teams_distribute_parallel_for:
7868 case OMPD_teams_distribute_parallel_for_simd:
7869 case OMPD_teams:
7870 case OMPD_teams_distribute:
7871 case OMPD_teams_distribute_simd:
7872 // Do not capture thread_limit-clause expressions.
7873 break;
7874 case OMPD_distribute_parallel_for:
7875 case OMPD_distribute_parallel_for_simd:
7876 case OMPD_task:
7877 case OMPD_taskloop:
7878 case OMPD_taskloop_simd:
7879 case OMPD_target_data:
7880 case OMPD_target_enter_data:
7881 case OMPD_target_exit_data:
7882 case OMPD_target_update:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00007883 case OMPD_cancel:
7884 case OMPD_parallel:
7885 case OMPD_parallel_sections:
7886 case OMPD_parallel_for:
7887 case OMPD_parallel_for_simd:
7888 case OMPD_target:
7889 case OMPD_target_simd:
7890 case OMPD_target_parallel:
7891 case OMPD_target_parallel_for:
7892 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00007893 case OMPD_threadprivate:
7894 case OMPD_taskyield:
7895 case OMPD_barrier:
7896 case OMPD_taskwait:
7897 case OMPD_cancellation_point:
7898 case OMPD_flush:
7899 case OMPD_declare_reduction:
7900 case OMPD_declare_simd:
7901 case OMPD_declare_target:
7902 case OMPD_end_declare_target:
7903 case OMPD_simd:
7904 case OMPD_for:
7905 case OMPD_for_simd:
7906 case OMPD_sections:
7907 case OMPD_section:
7908 case OMPD_single:
7909 case OMPD_master:
7910 case OMPD_critical:
7911 case OMPD_taskgroup:
7912 case OMPD_distribute:
7913 case OMPD_ordered:
7914 case OMPD_atomic:
7915 case OMPD_distribute_simd:
7916 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause");
7917 case OMPD_unknown:
7918 llvm_unreachable("Unknown OpenMP directive");
7919 }
7920 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00007921 case OMPC_schedule:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00007922 switch (DKind) {
Alexey Bataev2ba67042017-11-28 21:11:44 +00007923 case OMPD_parallel_for:
7924 case OMPD_parallel_for_simd:
Alexey Bataev7f96c372017-11-22 17:19:31 +00007925 case OMPD_distribute_parallel_for:
Alexey Bataev974acd62017-11-27 19:38:52 +00007926 case OMPD_distribute_parallel_for_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00007927 case OMPD_teams_distribute_parallel_for:
7928 case OMPD_teams_distribute_parallel_for_simd:
7929 case OMPD_target_parallel_for:
7930 case OMPD_target_parallel_for_simd:
7931 case OMPD_target_teams_distribute_parallel_for:
7932 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataev7f96c372017-11-22 17:19:31 +00007933 CaptureRegion = OMPD_parallel;
7934 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00007935 case OMPD_for:
7936 case OMPD_for_simd:
7937 // Do not capture schedule-clause expressions.
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00007938 break;
7939 case OMPD_task:
7940 case OMPD_taskloop:
7941 case OMPD_taskloop_simd:
7942 case OMPD_target_data:
7943 case OMPD_target_enter_data:
7944 case OMPD_target_exit_data:
7945 case OMPD_target_update:
7946 case OMPD_teams:
7947 case OMPD_teams_distribute:
7948 case OMPD_teams_distribute_simd:
7949 case OMPD_target_teams_distribute:
7950 case OMPD_target_teams_distribute_simd:
7951 case OMPD_target:
7952 case OMPD_target_simd:
7953 case OMPD_target_parallel:
7954 case OMPD_cancel:
7955 case OMPD_parallel:
7956 case OMPD_parallel_sections:
7957 case OMPD_threadprivate:
7958 case OMPD_taskyield:
7959 case OMPD_barrier:
7960 case OMPD_taskwait:
7961 case OMPD_cancellation_point:
7962 case OMPD_flush:
7963 case OMPD_declare_reduction:
7964 case OMPD_declare_simd:
7965 case OMPD_declare_target:
7966 case OMPD_end_declare_target:
7967 case OMPD_simd:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00007968 case OMPD_sections:
7969 case OMPD_section:
7970 case OMPD_single:
7971 case OMPD_master:
7972 case OMPD_critical:
7973 case OMPD_taskgroup:
7974 case OMPD_distribute:
7975 case OMPD_ordered:
7976 case OMPD_atomic:
7977 case OMPD_distribute_simd:
7978 case OMPD_target_teams:
7979 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
7980 case OMPD_unknown:
7981 llvm_unreachable("Unknown OpenMP directive");
7982 }
7983 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00007984 case OMPC_dist_schedule:
Carlo Bertolli62fae152017-11-20 20:46:39 +00007985 switch (DKind) {
7986 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00007987 case OMPD_teams_distribute_parallel_for_simd:
7988 case OMPD_teams_distribute:
7989 case OMPD_teams_distribute_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00007990 case OMPD_target_teams_distribute_parallel_for:
7991 case OMPD_target_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00007992 case OMPD_target_teams_distribute:
7993 case OMPD_target_teams_distribute_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00007994 CaptureRegion = OMPD_teams;
Alexey Bataev2ba67042017-11-28 21:11:44 +00007995 break;
7996 case OMPD_distribute_parallel_for:
7997 case OMPD_distribute_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00007998 case OMPD_distribute:
Carlo Bertolli62fae152017-11-20 20:46:39 +00007999 case OMPD_distribute_simd:
8000 // Do not capture thread_limit-clause expressions.
8001 break;
8002 case OMPD_parallel_for:
8003 case OMPD_parallel_for_simd:
8004 case OMPD_target_parallel_for_simd:
8005 case OMPD_target_parallel_for:
8006 case OMPD_task:
8007 case OMPD_taskloop:
8008 case OMPD_taskloop_simd:
8009 case OMPD_target_data:
8010 case OMPD_target_enter_data:
8011 case OMPD_target_exit_data:
8012 case OMPD_target_update:
8013 case OMPD_teams:
8014 case OMPD_target:
8015 case OMPD_target_simd:
8016 case OMPD_target_parallel:
8017 case OMPD_cancel:
8018 case OMPD_parallel:
8019 case OMPD_parallel_sections:
8020 case OMPD_threadprivate:
8021 case OMPD_taskyield:
8022 case OMPD_barrier:
8023 case OMPD_taskwait:
8024 case OMPD_cancellation_point:
8025 case OMPD_flush:
8026 case OMPD_declare_reduction:
8027 case OMPD_declare_simd:
8028 case OMPD_declare_target:
8029 case OMPD_end_declare_target:
8030 case OMPD_simd:
8031 case OMPD_for:
8032 case OMPD_for_simd:
8033 case OMPD_sections:
8034 case OMPD_section:
8035 case OMPD_single:
8036 case OMPD_master:
8037 case OMPD_critical:
8038 case OMPD_taskgroup:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008039 case OMPD_ordered:
8040 case OMPD_atomic:
8041 case OMPD_target_teams:
8042 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
8043 case OMPD_unknown:
8044 llvm_unreachable("Unknown OpenMP directive");
8045 }
8046 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008047 case OMPC_device:
8048 switch (DKind) {
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008049 case OMPD_target_update:
Alexey Bataevfab20e42017-12-27 18:49:38 +00008050 case OMPD_target_enter_data:
8051 case OMPD_target_exit_data:
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008052 CaptureRegion = OMPD_task;
8053 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008054 case OMPD_target_teams:
8055 case OMPD_target_teams_distribute:
8056 case OMPD_target_teams_distribute_simd:
8057 case OMPD_target_teams_distribute_parallel_for:
8058 case OMPD_target_teams_distribute_parallel_for_simd:
8059 case OMPD_target_data:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008060 case OMPD_target:
8061 case OMPD_target_simd:
8062 case OMPD_target_parallel:
8063 case OMPD_target_parallel_for:
8064 case OMPD_target_parallel_for_simd:
8065 // Do not capture device-clause expressions.
8066 break;
8067 case OMPD_teams_distribute_parallel_for:
8068 case OMPD_teams_distribute_parallel_for_simd:
8069 case OMPD_teams:
8070 case OMPD_teams_distribute:
8071 case OMPD_teams_distribute_simd:
8072 case OMPD_distribute_parallel_for:
8073 case OMPD_distribute_parallel_for_simd:
8074 case OMPD_task:
8075 case OMPD_taskloop:
8076 case OMPD_taskloop_simd:
8077 case OMPD_cancel:
8078 case OMPD_parallel:
8079 case OMPD_parallel_sections:
8080 case OMPD_parallel_for:
8081 case OMPD_parallel_for_simd:
8082 case OMPD_threadprivate:
8083 case OMPD_taskyield:
8084 case OMPD_barrier:
8085 case OMPD_taskwait:
8086 case OMPD_cancellation_point:
8087 case OMPD_flush:
8088 case OMPD_declare_reduction:
8089 case OMPD_declare_simd:
8090 case OMPD_declare_target:
8091 case OMPD_end_declare_target:
8092 case OMPD_simd:
8093 case OMPD_for:
8094 case OMPD_for_simd:
8095 case OMPD_sections:
8096 case OMPD_section:
8097 case OMPD_single:
8098 case OMPD_master:
8099 case OMPD_critical:
8100 case OMPD_taskgroup:
8101 case OMPD_distribute:
8102 case OMPD_ordered:
8103 case OMPD_atomic:
8104 case OMPD_distribute_simd:
8105 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
8106 case OMPD_unknown:
8107 llvm_unreachable("Unknown OpenMP directive");
8108 }
8109 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008110 case OMPC_firstprivate:
8111 case OMPC_lastprivate:
8112 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008113 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008114 case OMPC_in_reduction:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008115 case OMPC_linear:
8116 case OMPC_default:
8117 case OMPC_proc_bind:
8118 case OMPC_final:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008119 case OMPC_safelen:
8120 case OMPC_simdlen:
8121 case OMPC_collapse:
8122 case OMPC_private:
8123 case OMPC_shared:
8124 case OMPC_aligned:
8125 case OMPC_copyin:
8126 case OMPC_copyprivate:
8127 case OMPC_ordered:
8128 case OMPC_nowait:
8129 case OMPC_untied:
8130 case OMPC_mergeable:
8131 case OMPC_threadprivate:
8132 case OMPC_flush:
8133 case OMPC_read:
8134 case OMPC_write:
8135 case OMPC_update:
8136 case OMPC_capture:
8137 case OMPC_seq_cst:
8138 case OMPC_depend:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008139 case OMPC_threads:
8140 case OMPC_simd:
8141 case OMPC_map:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008142 case OMPC_priority:
8143 case OMPC_grainsize:
8144 case OMPC_nogroup:
8145 case OMPC_num_tasks:
8146 case OMPC_hint:
8147 case OMPC_defaultmap:
8148 case OMPC_unknown:
8149 case OMPC_uniform:
8150 case OMPC_to:
8151 case OMPC_from:
8152 case OMPC_use_device_ptr:
8153 case OMPC_is_device_ptr:
8154 llvm_unreachable("Unexpected OpenMP clause.");
8155 }
8156 return CaptureRegion;
8157}
8158
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008159OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
8160 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008161 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008162 SourceLocation NameModifierLoc,
8163 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008164 SourceLocation EndLoc) {
8165 Expr *ValExpr = Condition;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008166 Stmt *HelperValStmt = nullptr;
8167 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008168 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
8169 !Condition->isInstantiationDependent() &&
8170 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00008171 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008172 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008173 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008174
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008175 ValExpr = Val.get();
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008176
8177 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
8178 CaptureRegion =
8179 getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier);
Alexey Bataev2ba67042017-11-28 21:11:44 +00008180 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008181 ValExpr = MakeFullExpr(ValExpr).get();
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008182 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
8183 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8184 HelperValStmt = buildPreInits(Context, Captures);
8185 }
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008186 }
8187
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008188 return new (Context)
8189 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
8190 LParenLoc, NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008191}
8192
Alexey Bataev3778b602014-07-17 07:32:53 +00008193OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
8194 SourceLocation StartLoc,
8195 SourceLocation LParenLoc,
8196 SourceLocation EndLoc) {
8197 Expr *ValExpr = Condition;
8198 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
8199 !Condition->isInstantiationDependent() &&
8200 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00008201 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataev3778b602014-07-17 07:32:53 +00008202 if (Val.isInvalid())
8203 return nullptr;
8204
Richard Smith03a4aa32016-06-23 19:02:52 +00008205 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataev3778b602014-07-17 07:32:53 +00008206 }
8207
8208 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8209}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00008210ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
8211 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00008212 if (!Op)
8213 return ExprError();
8214
8215 class IntConvertDiagnoser : public ICEConvertDiagnoser {
8216 public:
8217 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00008218 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00008219 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
8220 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008221 return S.Diag(Loc, diag::err_omp_not_integral) << T;
8222 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008223 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
8224 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008225 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
8226 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008227 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
8228 QualType T,
8229 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008230 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
8231 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008232 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
8233 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008234 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00008235 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00008236 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008237 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
8238 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008239 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
8240 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008241 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
8242 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008243 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00008244 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00008245 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008246 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
8247 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008248 llvm_unreachable("conversion functions are permitted");
8249 }
8250 } ConvertDiagnoser;
8251 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
8252}
8253
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008254static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00008255 OpenMPClauseKind CKind,
8256 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008257 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
8258 !ValExpr->isInstantiationDependent()) {
8259 SourceLocation Loc = ValExpr->getExprLoc();
8260 ExprResult Value =
8261 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
8262 if (Value.isInvalid())
8263 return false;
8264
8265 ValExpr = Value.get();
8266 // The expression must evaluate to a non-negative integer value.
8267 llvm::APSInt Result;
8268 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00008269 Result.isSigned() &&
8270 !((!StrictlyPositive && Result.isNonNegative()) ||
8271 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008272 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00008273 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
8274 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008275 return false;
8276 }
8277 }
8278 return true;
8279}
8280
Alexey Bataev568a8332014-03-06 06:15:19 +00008281OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
8282 SourceLocation StartLoc,
8283 SourceLocation LParenLoc,
8284 SourceLocation EndLoc) {
8285 Expr *ValExpr = NumThreads;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008286 Stmt *HelperValStmt = nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00008287
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008288 // OpenMP [2.5, Restrictions]
8289 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008290 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
8291 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008292 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00008293
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008294 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +00008295 OpenMPDirectiveKind CaptureRegion =
8296 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads);
8297 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008298 ValExpr = MakeFullExpr(ValExpr).get();
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008299 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
8300 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8301 HelperValStmt = buildPreInits(Context, Captures);
8302 }
8303
8304 return new (Context) OMPNumThreadsClause(
8305 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00008306}
8307
Alexey Bataev62c87d22014-03-21 04:51:18 +00008308ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008309 OpenMPClauseKind CKind,
8310 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00008311 if (!E)
8312 return ExprError();
8313 if (E->isValueDependent() || E->isTypeDependent() ||
8314 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00008315 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00008316 llvm::APSInt Result;
8317 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
8318 if (ICE.isInvalid())
8319 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008320 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
8321 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00008322 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008323 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
8324 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00008325 return ExprError();
8326 }
Alexander Musman09184fe2014-09-30 05:29:28 +00008327 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
8328 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
8329 << E->getSourceRange();
8330 return ExprError();
8331 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008332 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
8333 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00008334 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008335 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00008336 return ICE;
8337}
8338
8339OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
8340 SourceLocation LParenLoc,
8341 SourceLocation EndLoc) {
8342 // OpenMP [2.8.1, simd construct, Description]
8343 // The parameter of the safelen clause must be a constant
8344 // positive integer expression.
8345 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
8346 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008347 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00008348 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00008349 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00008350}
8351
Alexey Bataev66b15b52015-08-21 11:14:16 +00008352OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
8353 SourceLocation LParenLoc,
8354 SourceLocation EndLoc) {
8355 // OpenMP [2.8.1, simd construct, Description]
8356 // The parameter of the simdlen clause must be a constant
8357 // positive integer expression.
8358 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
8359 if (Simdlen.isInvalid())
8360 return nullptr;
8361 return new (Context)
8362 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
8363}
8364
Alexander Musman64d33f12014-06-04 07:53:32 +00008365OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
8366 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00008367 SourceLocation LParenLoc,
8368 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00008369 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00008370 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00008371 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00008372 // The parameter of the collapse clause must be a constant
8373 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00008374 ExprResult NumForLoopsResult =
8375 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
8376 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00008377 return nullptr;
8378 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00008379 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00008380}
8381
Alexey Bataev10e775f2015-07-30 11:36:16 +00008382OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
8383 SourceLocation EndLoc,
8384 SourceLocation LParenLoc,
8385 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00008386 // OpenMP [2.7.1, loop construct, Description]
8387 // OpenMP [2.8.1, simd construct, Description]
8388 // OpenMP [2.9.6, distribute construct, Description]
8389 // The parameter of the ordered clause must be a constant
8390 // positive integer expression if any.
8391 if (NumForLoops && LParenLoc.isValid()) {
8392 ExprResult NumForLoopsResult =
8393 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
8394 if (NumForLoopsResult.isInvalid())
8395 return nullptr;
8396 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00008397 } else
8398 NumForLoops = nullptr;
8399 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00008400 return new (Context)
8401 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
8402}
8403
Alexey Bataeved09d242014-05-28 05:53:51 +00008404OMPClause *Sema::ActOnOpenMPSimpleClause(
8405 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
8406 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008407 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008408 switch (Kind) {
8409 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00008410 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00008411 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
8412 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008413 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008414 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00008415 Res = ActOnOpenMPProcBindClause(
8416 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
8417 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008418 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008419 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00008420 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00008421 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00008422 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008423 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00008424 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008425 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008426 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008427 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00008428 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00008429 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00008430 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008431 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008432 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00008433 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008434 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008435 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008436 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008437 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00008438 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008439 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008440 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008441 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00008442 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008443 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008444 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008445 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008446 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008447 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008448 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00008449 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00008450 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008451 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008452 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00008453 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008454 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00008455 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008456 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00008457 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00008458 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00008459 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008460 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008461 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008462 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008463 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00008464 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00008465 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00008466 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00008467 case OMPC_is_device_ptr:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008468 llvm_unreachable("Clause is not allowed.");
8469 }
8470 return Res;
8471}
8472
Alexey Bataev6402bca2015-12-28 07:25:51 +00008473static std::string
8474getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
8475 ArrayRef<unsigned> Exclude = llvm::None) {
8476 std::string Values;
8477 unsigned Bound = Last >= 2 ? Last - 2 : 0;
8478 unsigned Skipped = Exclude.size();
8479 auto S = Exclude.begin(), E = Exclude.end();
8480 for (unsigned i = First; i < Last; ++i) {
8481 if (std::find(S, E, i) != E) {
8482 --Skipped;
8483 continue;
8484 }
8485 Values += "'";
8486 Values += getOpenMPSimpleClauseTypeName(K, i);
8487 Values += "'";
8488 if (i == Bound - Skipped)
8489 Values += " or ";
8490 else if (i != Bound + 1 - Skipped)
8491 Values += ", ";
8492 }
8493 return Values;
8494}
8495
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008496OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
8497 SourceLocation KindKwLoc,
8498 SourceLocation StartLoc,
8499 SourceLocation LParenLoc,
8500 SourceLocation EndLoc) {
8501 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00008502 static_assert(OMPC_DEFAULT_unknown > 0,
8503 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008504 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008505 << getListOfPossibleValues(OMPC_default, /*First=*/0,
8506 /*Last=*/OMPC_DEFAULT_unknown)
8507 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008508 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008509 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00008510 switch (Kind) {
8511 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008512 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008513 break;
8514 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008515 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008516 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008517 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008518 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00008519 break;
8520 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008521 return new (Context)
8522 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008523}
8524
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008525OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
8526 SourceLocation KindKwLoc,
8527 SourceLocation StartLoc,
8528 SourceLocation LParenLoc,
8529 SourceLocation EndLoc) {
8530 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008531 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008532 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
8533 /*Last=*/OMPC_PROC_BIND_unknown)
8534 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008535 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008536 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008537 return new (Context)
8538 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008539}
8540
Alexey Bataev56dafe82014-06-20 07:16:17 +00008541OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00008542 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00008543 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00008544 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00008545 SourceLocation EndLoc) {
8546 OMPClause *Res = nullptr;
8547 switch (Kind) {
8548 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00008549 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
8550 assert(Argument.size() == NumberOfElements &&
8551 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00008552 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00008553 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
8554 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
8555 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
8556 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
8557 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00008558 break;
8559 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00008560 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
8561 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
8562 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
8563 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008564 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00008565 case OMPC_dist_schedule:
8566 Res = ActOnOpenMPDistScheduleClause(
8567 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
8568 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
8569 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008570 case OMPC_defaultmap:
8571 enum { Modifier, DefaultmapKind };
8572 Res = ActOnOpenMPDefaultmapClause(
8573 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
8574 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
David Majnemer9d168222016-08-05 17:44:54 +00008575 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind],
8576 EndLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008577 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00008578 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008579 case OMPC_num_threads:
8580 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008581 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008582 case OMPC_collapse:
8583 case OMPC_default:
8584 case OMPC_proc_bind:
8585 case OMPC_private:
8586 case OMPC_firstprivate:
8587 case OMPC_lastprivate:
8588 case OMPC_shared:
8589 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008590 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008591 case OMPC_in_reduction:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008592 case OMPC_linear:
8593 case OMPC_aligned:
8594 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008595 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008596 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00008597 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008598 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008599 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008600 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00008601 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008602 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008603 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008604 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008605 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008606 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008607 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00008608 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00008609 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008610 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008611 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00008612 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008613 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00008614 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008615 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00008616 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00008617 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00008618 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008619 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008620 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00008621 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00008622 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00008623 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00008624 case OMPC_is_device_ptr:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008625 llvm_unreachable("Clause is not allowed.");
8626 }
8627 return Res;
8628}
8629
Alexey Bataev6402bca2015-12-28 07:25:51 +00008630static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
8631 OpenMPScheduleClauseModifier M2,
8632 SourceLocation M1Loc, SourceLocation M2Loc) {
8633 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
8634 SmallVector<unsigned, 2> Excluded;
8635 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
8636 Excluded.push_back(M2);
8637 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
8638 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
8639 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
8640 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
8641 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
8642 << getListOfPossibleValues(OMPC_schedule,
8643 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
8644 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
8645 Excluded)
8646 << getOpenMPClauseName(OMPC_schedule);
8647 return true;
8648 }
8649 return false;
8650}
8651
Alexey Bataev56dafe82014-06-20 07:16:17 +00008652OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00008653 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00008654 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00008655 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
8656 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
8657 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
8658 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
8659 return nullptr;
8660 // OpenMP, 2.7.1, Loop Construct, Restrictions
8661 // Either the monotonic modifier or the nonmonotonic modifier can be specified
8662 // but not both.
8663 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
8664 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
8665 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
8666 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
8667 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
8668 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
8669 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
8670 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
8671 return nullptr;
8672 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00008673 if (Kind == OMPC_SCHEDULE_unknown) {
8674 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00008675 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
8676 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
8677 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
8678 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
8679 Exclude);
8680 } else {
8681 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
8682 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00008683 }
8684 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
8685 << Values << getOpenMPClauseName(OMPC_schedule);
8686 return nullptr;
8687 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00008688 // OpenMP, 2.7.1, Loop Construct, Restrictions
8689 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
8690 // schedule(guided).
8691 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
8692 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
8693 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
8694 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
8695 diag::err_omp_schedule_nonmonotonic_static);
8696 return nullptr;
8697 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00008698 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00008699 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00008700 if (ChunkSize) {
8701 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
8702 !ChunkSize->isInstantiationDependent() &&
8703 !ChunkSize->containsUnexpandedParameterPack()) {
8704 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
8705 ExprResult Val =
8706 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
8707 if (Val.isInvalid())
8708 return nullptr;
8709
8710 ValExpr = Val.get();
8711
8712 // OpenMP [2.7.1, Restrictions]
8713 // chunk_size must be a loop invariant integer expression with a positive
8714 // value.
8715 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00008716 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
8717 if (Result.isSigned() && !Result.isStrictlyPositive()) {
8718 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00008719 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00008720 return nullptr;
8721 }
Alexey Bataev2ba67042017-11-28 21:11:44 +00008722 } else if (getOpenMPCaptureRegionForClause(
8723 DSAStack->getCurrentDirective(), OMPC_schedule) !=
8724 OMPD_unknown &&
Alexey Bataevb46cdea2016-06-15 11:20:48 +00008725 !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008726 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00008727 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
8728 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8729 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00008730 }
8731 }
8732 }
8733
Alexey Bataev6402bca2015-12-28 07:25:51 +00008734 return new (Context)
8735 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00008736 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00008737}
8738
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008739OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
8740 SourceLocation StartLoc,
8741 SourceLocation EndLoc) {
8742 OMPClause *Res = nullptr;
8743 switch (Kind) {
8744 case OMPC_ordered:
8745 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
8746 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00008747 case OMPC_nowait:
8748 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
8749 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008750 case OMPC_untied:
8751 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
8752 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008753 case OMPC_mergeable:
8754 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
8755 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008756 case OMPC_read:
8757 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
8758 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00008759 case OMPC_write:
8760 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
8761 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00008762 case OMPC_update:
8763 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
8764 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00008765 case OMPC_capture:
8766 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
8767 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008768 case OMPC_seq_cst:
8769 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
8770 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00008771 case OMPC_threads:
8772 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
8773 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008774 case OMPC_simd:
8775 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
8776 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00008777 case OMPC_nogroup:
8778 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
8779 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008780 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00008781 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008782 case OMPC_num_threads:
8783 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008784 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008785 case OMPC_collapse:
8786 case OMPC_schedule:
8787 case OMPC_private:
8788 case OMPC_firstprivate:
8789 case OMPC_lastprivate:
8790 case OMPC_shared:
8791 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008792 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008793 case OMPC_in_reduction:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008794 case OMPC_linear:
8795 case OMPC_aligned:
8796 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008797 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008798 case OMPC_default:
8799 case OMPC_proc_bind:
8800 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00008801 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008802 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00008803 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008804 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00008805 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008806 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00008807 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008808 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00008809 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00008810 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008811 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008812 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008813 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008814 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00008815 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00008816 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00008817 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00008818 case OMPC_is_device_ptr:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008819 llvm_unreachable("Clause is not allowed.");
8820 }
8821 return Res;
8822}
8823
Alexey Bataev236070f2014-06-20 11:19:47 +00008824OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
8825 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00008826 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00008827 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
8828}
8829
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008830OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
8831 SourceLocation EndLoc) {
8832 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
8833}
8834
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008835OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
8836 SourceLocation EndLoc) {
8837 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
8838}
8839
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008840OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
8841 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008842 return new (Context) OMPReadClause(StartLoc, EndLoc);
8843}
8844
Alexey Bataevdea47612014-07-23 07:46:59 +00008845OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
8846 SourceLocation EndLoc) {
8847 return new (Context) OMPWriteClause(StartLoc, EndLoc);
8848}
8849
Alexey Bataev67a4f222014-07-23 10:25:33 +00008850OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
8851 SourceLocation EndLoc) {
8852 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
8853}
8854
Alexey Bataev459dec02014-07-24 06:46:57 +00008855OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
8856 SourceLocation EndLoc) {
8857 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
8858}
8859
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008860OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
8861 SourceLocation EndLoc) {
8862 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
8863}
8864
Alexey Bataev346265e2015-09-25 10:37:12 +00008865OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
8866 SourceLocation EndLoc) {
8867 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
8868}
8869
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008870OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
8871 SourceLocation EndLoc) {
8872 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
8873}
8874
Alexey Bataevb825de12015-12-07 10:51:44 +00008875OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
8876 SourceLocation EndLoc) {
8877 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
8878}
8879
Alexey Bataevc5e02582014-06-16 07:08:35 +00008880OMPClause *Sema::ActOnOpenMPVarListClause(
8881 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
8882 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
8883 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008884 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00008885 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
8886 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
8887 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008888 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008889 switch (Kind) {
8890 case OMPC_private:
8891 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8892 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008893 case OMPC_firstprivate:
8894 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8895 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008896 case OMPC_lastprivate:
8897 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8898 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008899 case OMPC_shared:
8900 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
8901 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008902 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00008903 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
8904 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008905 break;
Alexey Bataev169d96a2017-07-18 20:17:46 +00008906 case OMPC_task_reduction:
8907 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
8908 EndLoc, ReductionIdScopeSpec,
8909 ReductionId);
8910 break;
Alexey Bataevfa312f32017-07-21 18:48:21 +00008911 case OMPC_in_reduction:
8912 Res =
8913 ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
8914 EndLoc, ReductionIdScopeSpec, ReductionId);
8915 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00008916 case OMPC_linear:
8917 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00008918 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00008919 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008920 case OMPC_aligned:
8921 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
8922 ColonLoc, EndLoc);
8923 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008924 case OMPC_copyin:
8925 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
8926 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008927 case OMPC_copyprivate:
8928 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8929 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00008930 case OMPC_flush:
8931 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
8932 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008933 case OMPC_depend:
David Majnemer9d168222016-08-05 17:44:54 +00008934 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
Kelvin Li0bff7af2015-11-23 05:32:03 +00008935 StartLoc, LParenLoc, EndLoc);
8936 break;
8937 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00008938 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
8939 DepLinMapLoc, ColonLoc, VarList, StartLoc,
8940 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008941 break;
Samuel Antao661c0902016-05-26 17:39:58 +00008942 case OMPC_to:
8943 Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc);
8944 break;
Samuel Antaoec172c62016-05-26 17:49:04 +00008945 case OMPC_from:
8946 Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc);
8947 break;
Carlo Bertolli2404b172016-07-13 15:37:16 +00008948 case OMPC_use_device_ptr:
8949 Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
8950 break;
Carlo Bertolli70594e92016-07-13 17:16:49 +00008951 case OMPC_is_device_ptr:
8952 Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
8953 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008954 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00008955 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00008956 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00008957 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008958 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00008959 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008960 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008961 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008962 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008963 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00008964 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008965 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008966 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008967 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008968 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008969 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008970 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008971 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008972 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00008973 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00008974 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008975 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00008976 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008977 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00008978 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008979 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00008980 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00008981 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00008982 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008983 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008984 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008985 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008986 case OMPC_uniform:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008987 llvm_unreachable("Clause is not allowed.");
8988 }
8989 return Res;
8990}
8991
Alexey Bataev90c228f2016-02-08 09:29:13 +00008992ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00008993 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00008994 ExprResult Res = BuildDeclRefExpr(
8995 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
8996 if (!Res.isUsable())
8997 return ExprError();
8998 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
8999 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
9000 if (!Res.isUsable())
9001 return ExprError();
9002 }
9003 if (VK != VK_LValue && Res.get()->isGLValue()) {
9004 Res = DefaultLvalueConversion(Res.get());
9005 if (!Res.isUsable())
9006 return ExprError();
9007 }
9008 return Res;
9009}
9010
Alexey Bataev60da77e2016-02-29 05:54:20 +00009011static std::pair<ValueDecl *, bool>
9012getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
9013 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00009014 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
9015 RefExpr->containsUnexpandedParameterPack())
9016 return std::make_pair(nullptr, true);
9017
Alexey Bataevd985eda2016-02-10 11:29:16 +00009018 // OpenMP [3.1, C/C++]
9019 // A list item is a variable name.
9020 // OpenMP [2.9.3.3, Restrictions, p.1]
9021 // A variable that is part of another variable (as an array or
9022 // structure element) cannot appear in a private clause.
9023 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009024 enum {
9025 NoArrayExpr = -1,
9026 ArraySubscript = 0,
9027 OMPArraySection = 1
9028 } IsArrayExpr = NoArrayExpr;
9029 if (AllowArraySection) {
9030 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
9031 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
9032 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
9033 Base = TempASE->getBase()->IgnoreParenImpCasts();
9034 RefExpr = Base;
9035 IsArrayExpr = ArraySubscript;
9036 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
9037 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
9038 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
9039 Base = TempOASE->getBase()->IgnoreParenImpCasts();
9040 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
9041 Base = TempASE->getBase()->IgnoreParenImpCasts();
9042 RefExpr = Base;
9043 IsArrayExpr = OMPArraySection;
9044 }
9045 }
9046 ELoc = RefExpr->getExprLoc();
9047 ERange = RefExpr->getSourceRange();
9048 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00009049 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
9050 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
9051 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
9052 (S.getCurrentThisType().isNull() || !ME ||
9053 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
9054 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009055 if (IsArrayExpr != NoArrayExpr)
9056 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
9057 << ERange;
9058 else {
9059 S.Diag(ELoc,
9060 AllowArraySection
9061 ? diag::err_omp_expected_var_name_member_expr_or_array_item
9062 : diag::err_omp_expected_var_name_member_expr)
9063 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
9064 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009065 return std::make_pair(nullptr, false);
9066 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +00009067 return std::make_pair(
9068 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009069}
9070
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009071OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
9072 SourceLocation StartLoc,
9073 SourceLocation LParenLoc,
9074 SourceLocation EndLoc) {
9075 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00009076 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00009077 for (auto &RefExpr : VarList) {
9078 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009079 SourceLocation ELoc;
9080 SourceRange ERange;
9081 Expr *SimpleRefExpr = RefExpr;
9082 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009083 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009084 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009085 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009086 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009087 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009088 ValueDecl *D = Res.first;
9089 if (!D)
9090 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009091
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009092 QualType Type = D->getType();
9093 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009094
9095 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9096 // A variable that appears in a private clause must not have an incomplete
9097 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009098 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009099 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009100 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009101
Alexey Bataev758e55e2013-09-06 18:03:48 +00009102 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9103 // in a Construct]
9104 // Variables with the predetermined data-sharing attributes may not be
9105 // listed in data-sharing attributes clauses, except for the cases
9106 // listed below. For these exceptions only, listing a predetermined
9107 // variable in a data-sharing attribute clause is allowed and overrides
9108 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009109 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009110 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009111 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9112 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009113 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009114 continue;
9115 }
9116
Kelvin Libf594a52016-12-17 05:48:59 +00009117 auto CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009118 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009119 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Kelvin Libf594a52016-12-17 05:48:59 +00009120 isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009121 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
9122 << getOpenMPClauseName(OMPC_private) << Type
Kelvin Libf594a52016-12-17 05:48:59 +00009123 << getOpenMPDirectiveName(CurrDir);
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009124 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009125 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009126 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009127 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009128 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009129 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009130 continue;
9131 }
9132
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009133 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
9134 // A list item cannot appear in both a map clause and a data-sharing
9135 // attribute clause on the same construct
Kelvin Libf594a52016-12-17 05:48:59 +00009136 if (CurrDir == OMPD_target || CurrDir == OMPD_target_parallel ||
Kelvin Lida681182017-01-10 18:08:18 +00009137 CurrDir == OMPD_target_teams ||
Kelvin Li80e8f562016-12-29 22:16:30 +00009138 CurrDir == OMPD_target_teams_distribute ||
Kelvin Li1851df52017-01-03 05:23:48 +00009139 CurrDir == OMPD_target_teams_distribute_parallel_for ||
Kelvin Lic4bfc6f2017-01-10 04:26:44 +00009140 CurrDir == OMPD_target_teams_distribute_parallel_for_simd ||
Kelvin Lida681182017-01-10 18:08:18 +00009141 CurrDir == OMPD_target_teams_distribute_simd ||
Kelvin Li41010322017-01-10 05:15:35 +00009142 CurrDir == OMPD_target_parallel_for_simd ||
9143 CurrDir == OMPD_target_parallel_for) {
Samuel Antao6890b092016-07-28 14:25:09 +00009144 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +00009145 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +00009146 VD, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +00009147 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
9148 OpenMPClauseKind WhereFoundClauseKind) -> bool {
9149 ConflictKind = WhereFoundClauseKind;
9150 return true;
9151 })) {
9152 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009153 << getOpenMPClauseName(OMPC_private)
Samuel Antao6890b092016-07-28 14:25:09 +00009154 << getOpenMPClauseName(ConflictKind)
Kelvin Libf594a52016-12-17 05:48:59 +00009155 << getOpenMPDirectiveName(CurrDir);
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009156 ReportOriginalDSA(*this, DSAStack, D, DVar);
9157 continue;
9158 }
9159 }
9160
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009161 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
9162 // A variable of class type (or array thereof) that appears in a private
9163 // clause requires an accessible, unambiguous default constructor for the
9164 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00009165 // Generate helper private variable and initialize it with the default
9166 // value. The address of the original variable is replaced by the address of
9167 // the new private variable in CodeGen. This new variable is not added to
9168 // IdResolver, so the code in the OpenMP region uses original variable for
9169 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009170 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009171 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
9172 D->hasAttrs() ? &D->getAttrs() : nullptr);
Richard Smith3beb7c62017-01-12 02:27:38 +00009173 ActOnUninitializedDecl(VDPrivate);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009174 if (VDPrivate->isInvalidDecl())
9175 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009176 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009177 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009178
Alexey Bataev90c228f2016-02-08 09:29:13 +00009179 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009180 if (!VD && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00009181 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00009182 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009183 Vars.push_back((VD || CurContext->isDependentContext())
9184 ? RefExpr->IgnoreParens()
9185 : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009186 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009187 }
9188
Alexey Bataeved09d242014-05-28 05:53:51 +00009189 if (Vars.empty())
9190 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009191
Alexey Bataev03b340a2014-10-21 03:16:40 +00009192 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9193 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009194}
9195
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009196namespace {
9197class DiagsUninitializedSeveretyRAII {
9198private:
9199 DiagnosticsEngine &Diags;
9200 SourceLocation SavedLoc;
9201 bool IsIgnored;
9202
9203public:
9204 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
9205 bool IsIgnored)
9206 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
9207 if (!IsIgnored) {
9208 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
9209 /*Map*/ diag::Severity::Ignored, Loc);
9210 }
9211 }
9212 ~DiagsUninitializedSeveretyRAII() {
9213 if (!IsIgnored)
9214 Diags.popMappings(SavedLoc);
9215 }
9216};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00009217}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009218
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009219OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
9220 SourceLocation StartLoc,
9221 SourceLocation LParenLoc,
9222 SourceLocation EndLoc) {
9223 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009224 SmallVector<Expr *, 8> PrivateCopies;
9225 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00009226 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009227 bool IsImplicitClause =
9228 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
9229 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
9230
Alexey Bataeved09d242014-05-28 05:53:51 +00009231 for (auto &RefExpr : VarList) {
9232 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009233 SourceLocation ELoc;
9234 SourceRange ERange;
9235 Expr *SimpleRefExpr = RefExpr;
9236 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009237 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009238 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009239 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009240 PrivateCopies.push_back(nullptr);
9241 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009242 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009243 ValueDecl *D = Res.first;
9244 if (!D)
9245 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009246
Alexey Bataev60da77e2016-02-29 05:54:20 +00009247 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00009248 QualType Type = D->getType();
9249 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009250
9251 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9252 // A variable that appears in a private clause must not have an incomplete
9253 // type or a reference type.
9254 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00009255 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009256 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009257 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009258
9259 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
9260 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00009261 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009262 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009263 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009264
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009265 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00009266 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009267 if (!IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00009268 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev005248a2016-02-25 05:25:57 +00009269 TopDVar = DVar;
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009270 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009271 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009272 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
9273 // A list item that specifies a given variable may not appear in more
9274 // than one clause on the same directive, except that a variable may be
9275 // specified in both firstprivate and lastprivate clauses.
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009276 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
9277 // A list item may appear in a firstprivate or lastprivate clause but not
9278 // both.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009279 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevb358f992017-12-01 17:40:15 +00009280 (isOpenMPDistributeDirective(CurrDir) ||
9281 DVar.CKind != OMPC_lastprivate) &&
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009282 DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009283 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00009284 << getOpenMPClauseName(DVar.CKind)
9285 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009286 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009287 continue;
9288 }
9289
9290 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9291 // in a Construct]
9292 // Variables with the predetermined data-sharing attributes may not be
9293 // listed in data-sharing attributes clauses, except for the cases
9294 // listed below. For these exceptions only, listing a predetermined
9295 // variable in a data-sharing attribute clause is allowed and overrides
9296 // the variable's predetermined data-sharing attributes.
9297 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9298 // in a Construct, C/C++, p.2]
9299 // Variables with const-qualified type having no mutable member may be
9300 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00009301 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009302 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
9303 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00009304 << getOpenMPClauseName(DVar.CKind)
9305 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009306 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009307 continue;
9308 }
9309
9310 // OpenMP [2.9.3.4, Restrictions, p.2]
9311 // A list item that is private within a parallel region must not appear
9312 // in a firstprivate clause on a worksharing construct if any of the
9313 // worksharing regions arising from the worksharing construct ever bind
9314 // to any of the parallel regions arising from the parallel construct.
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009315 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
9316 // A list item that is private within a teams region must not appear in a
9317 // firstprivate clause on a distribute construct if any of the distribute
9318 // regions arising from the distribute construct ever bind to any of the
9319 // teams regions arising from the teams construct.
9320 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
9321 // A list item that appears in a reduction clause of a teams construct
9322 // must not appear in a firstprivate clause on a distribute construct if
9323 // any of the distribute regions arising from the distribute construct
9324 // ever bind to any of the teams regions arising from the teams construct.
9325 if ((isOpenMPWorksharingDirective(CurrDir) ||
9326 isOpenMPDistributeDirective(CurrDir)) &&
Kelvin Li579e41c2016-11-30 23:51:03 +00009327 !isOpenMPParallelDirective(CurrDir) &&
9328 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00009329 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009330 if (DVar.CKind != OMPC_shared &&
9331 (isOpenMPParallelDirective(DVar.DKind) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009332 isOpenMPTeamsDirective(DVar.DKind) ||
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009333 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00009334 Diag(ELoc, diag::err_omp_required_access)
9335 << getOpenMPClauseName(OMPC_firstprivate)
9336 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009337 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00009338 continue;
9339 }
9340 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009341 // OpenMP [2.9.3.4, Restrictions, p.3]
9342 // A list item that appears in a reduction clause of a parallel construct
9343 // must not appear in a firstprivate clause on a worksharing or task
9344 // construct if any of the worksharing or task regions arising from the
9345 // worksharing or task construct ever bind to any of the parallel regions
9346 // arising from the parallel construct.
9347 // OpenMP [2.9.3.4, Restrictions, p.4]
9348 // A list item that appears in a reduction clause in worksharing
9349 // construct must not appear in a firstprivate clause in a task construct
9350 // encountered during execution of any of the worksharing regions arising
9351 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +00009352 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00009353 DVar = DSAStack->hasInnermostDSA(
9354 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
9355 [](OpenMPDirectiveKind K) -> bool {
9356 return isOpenMPParallelDirective(K) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009357 isOpenMPWorksharingDirective(K) ||
9358 isOpenMPTeamsDirective(K);
Alexey Bataev7ace49d2016-05-17 08:55:33 +00009359 },
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009360 /*FromParent=*/true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009361 if (DVar.CKind == OMPC_reduction &&
9362 (isOpenMPParallelDirective(DVar.DKind) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009363 isOpenMPWorksharingDirective(DVar.DKind) ||
9364 isOpenMPTeamsDirective(DVar.DKind))) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009365 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
9366 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009367 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009368 continue;
9369 }
9370 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00009371
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009372 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
9373 // A list item cannot appear in both a map clause and a data-sharing
9374 // attribute clause on the same construct
Alexey Bataevb358f992017-12-01 17:40:15 +00009375 if (isOpenMPTargetExecutionDirective(CurrDir)) {
Samuel Antao6890b092016-07-28 14:25:09 +00009376 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +00009377 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +00009378 VD, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +00009379 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
9380 OpenMPClauseKind WhereFoundClauseKind) -> bool {
9381 ConflictKind = WhereFoundClauseKind;
9382 return true;
9383 })) {
9384 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009385 << getOpenMPClauseName(OMPC_firstprivate)
Samuel Antao6890b092016-07-28 14:25:09 +00009386 << getOpenMPClauseName(ConflictKind)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009387 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
9388 ReportOriginalDSA(*this, DSAStack, D, DVar);
9389 continue;
9390 }
9391 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009392 }
9393
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009394 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009395 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00009396 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009397 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
9398 << getOpenMPClauseName(OMPC_firstprivate) << Type
9399 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
9400 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00009401 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009402 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00009403 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009404 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00009405 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009406 continue;
9407 }
9408
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009409 Type = Type.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00009410 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
9411 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009412 // Generate helper private variable and initialize it with the value of the
9413 // original variable. The address of the original variable is replaced by
9414 // the address of the new private variable in the CodeGen. This new variable
9415 // is not added to IdResolver, so the code in the OpenMP region uses
9416 // original variable for proper diagnostics and variable capturing.
9417 Expr *VDInitRefExpr = nullptr;
9418 // For arrays generate initializer for single element and replace it by the
9419 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009420 if (Type->isArrayType()) {
9421 auto VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00009422 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009423 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009424 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009425 ElemType = ElemType.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00009426 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009427 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00009428 InitializedEntity Entity =
9429 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009430 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
9431
9432 InitializationSequence InitSeq(*this, Entity, Kind, Init);
9433 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
9434 if (Result.isInvalid())
9435 VDPrivate->setInvalidDecl();
9436 else
9437 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009438 // Remove temp variable declaration.
9439 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009440 } else {
Alexey Bataevd985eda2016-02-10 11:29:16 +00009441 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
9442 ".firstprivate.temp");
9443 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
9444 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00009445 AddInitializerToDecl(VDPrivate,
9446 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +00009447 /*DirectInit=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009448 }
9449 if (VDPrivate->isInvalidDecl()) {
9450 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00009451 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009452 diag::note_omp_task_predetermined_firstprivate_here);
9453 }
9454 continue;
9455 }
9456 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00009457 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00009458 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
9459 RefExpr->getExprLoc());
9460 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009461 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev005248a2016-02-25 05:25:57 +00009462 if (TopDVar.CKind == OMPC_lastprivate)
9463 Ref = TopDVar.PrivateCopy;
9464 else {
Alexey Bataev61205072016-03-02 04:57:40 +00009465 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataev005248a2016-02-25 05:25:57 +00009466 if (!IsOpenMPCapturedDecl(D))
9467 ExprCaptures.push_back(Ref->getDecl());
9468 }
Alexey Bataev417089f2016-02-17 13:19:37 +00009469 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009470 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009471 Vars.push_back((VD || CurContext->isDependentContext())
9472 ? RefExpr->IgnoreParens()
9473 : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009474 PrivateCopies.push_back(VDPrivateRefExpr);
9475 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009476 }
9477
Alexey Bataeved09d242014-05-28 05:53:51 +00009478 if (Vars.empty())
9479 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009480
9481 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009482 Vars, PrivateCopies, Inits,
9483 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009484}
9485
Alexander Musman1bb328c2014-06-04 13:06:39 +00009486OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
9487 SourceLocation StartLoc,
9488 SourceLocation LParenLoc,
9489 SourceLocation EndLoc) {
9490 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00009491 SmallVector<Expr *, 8> SrcExprs;
9492 SmallVector<Expr *, 8> DstExprs;
9493 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +00009494 SmallVector<Decl *, 4> ExprCaptures;
9495 SmallVector<Expr *, 4> ExprPostUpdates;
Alexander Musman1bb328c2014-06-04 13:06:39 +00009496 for (auto &RefExpr : VarList) {
9497 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009498 SourceLocation ELoc;
9499 SourceRange ERange;
9500 Expr *SimpleRefExpr = RefExpr;
9501 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +00009502 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00009503 // It will be analyzed later.
9504 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00009505 SrcExprs.push_back(nullptr);
9506 DstExprs.push_back(nullptr);
9507 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00009508 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00009509 ValueDecl *D = Res.first;
9510 if (!D)
9511 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00009512
Alexey Bataev74caaf22016-02-20 04:09:36 +00009513 QualType Type = D->getType();
9514 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +00009515
9516 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
9517 // A variable that appears in a lastprivate clause must not have an
9518 // incomplete type or a reference type.
9519 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +00009520 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +00009521 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009522 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00009523
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009524 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexander Musman1bb328c2014-06-04 13:06:39 +00009525 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
9526 // in a Construct]
9527 // Variables with the predetermined data-sharing attributes may not be
9528 // listed in data-sharing attributes clauses, except for the cases
9529 // listed below.
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009530 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
9531 // A list item may appear in a firstprivate or lastprivate clause but not
9532 // both.
Alexey Bataev74caaf22016-02-20 04:09:36 +00009533 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00009534 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
Alexey Bataevb358f992017-12-01 17:40:15 +00009535 (isOpenMPDistributeDirective(CurrDir) ||
9536 DVar.CKind != OMPC_firstprivate) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +00009537 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
9538 Diag(ELoc, diag::err_omp_wrong_dsa)
9539 << getOpenMPClauseName(DVar.CKind)
9540 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev74caaf22016-02-20 04:09:36 +00009541 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00009542 continue;
9543 }
9544
Alexey Bataevf29276e2014-06-18 04:14:57 +00009545 // OpenMP [2.14.3.5, Restrictions, p.2]
9546 // A list item that is private within a parallel region, or that appears in
9547 // the reduction clause of a parallel construct, must not appear in a
9548 // lastprivate clause on a worksharing construct if any of the corresponding
9549 // worksharing regions ever binds to any of the corresponding parallel
9550 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00009551 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00009552 if (isOpenMPWorksharingDirective(CurrDir) &&
Kelvin Li579e41c2016-11-30 23:51:03 +00009553 !isOpenMPParallelDirective(CurrDir) &&
9554 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +00009555 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00009556 if (DVar.CKind != OMPC_shared) {
9557 Diag(ELoc, diag::err_omp_required_access)
9558 << getOpenMPClauseName(OMPC_lastprivate)
9559 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev74caaf22016-02-20 04:09:36 +00009560 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00009561 continue;
9562 }
9563 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00009564
Alexander Musman1bb328c2014-06-04 13:06:39 +00009565 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00009566 // A variable of class type (or array thereof) that appears in a
9567 // lastprivate clause requires an accessible, unambiguous default
9568 // constructor for the class type, unless the list item is also specified
9569 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00009570 // A variable of class type (or array thereof) that appears in a
9571 // lastprivate clause requires an accessible, unambiguous copy assignment
9572 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00009573 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009574 auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009575 Type.getUnqualifiedType(), ".lastprivate.src",
Alexey Bataev74caaf22016-02-20 04:09:36 +00009576 D->hasAttrs() ? &D->getAttrs() : nullptr);
9577 auto *PseudoSrcExpr =
9578 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00009579 auto *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +00009580 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +00009581 D->hasAttrs() ? &D->getAttrs() : nullptr);
9582 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00009583 // For arrays generate assignment operation for single element and replace
9584 // it by the original array element in CodeGen.
Alexey Bataev74caaf22016-02-20 04:09:36 +00009585 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
Alexey Bataev38e89532015-04-16 04:54:05 +00009586 PseudoDstExpr, PseudoSrcExpr);
9587 if (AssignmentOp.isInvalid())
9588 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +00009589 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00009590 /*DiscardedValue=*/true);
9591 if (AssignmentOp.isInvalid())
9592 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00009593
Alexey Bataev74caaf22016-02-20 04:09:36 +00009594 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009595 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev005248a2016-02-25 05:25:57 +00009596 if (TopDVar.CKind == OMPC_firstprivate)
9597 Ref = TopDVar.PrivateCopy;
9598 else {
Alexey Bataev61205072016-03-02 04:57:40 +00009599 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00009600 if (!IsOpenMPCapturedDecl(D))
9601 ExprCaptures.push_back(Ref->getDecl());
9602 }
9603 if (TopDVar.CKind == OMPC_firstprivate ||
9604 (!IsOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009605 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +00009606 ExprResult RefRes = DefaultLvalueConversion(Ref);
9607 if (!RefRes.isUsable())
9608 continue;
9609 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +00009610 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
9611 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +00009612 if (!PostUpdateRes.isUsable())
9613 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +00009614 ExprPostUpdates.push_back(
9615 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +00009616 }
9617 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00009618 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009619 Vars.push_back((VD || CurContext->isDependentContext())
9620 ? RefExpr->IgnoreParens()
9621 : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +00009622 SrcExprs.push_back(PseudoSrcExpr);
9623 DstExprs.push_back(PseudoDstExpr);
9624 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00009625 }
9626
9627 if (Vars.empty())
9628 return nullptr;
9629
9630 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +00009631 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009632 buildPreInits(Context, ExprCaptures),
9633 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +00009634}
9635
Alexey Bataev758e55e2013-09-06 18:03:48 +00009636OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
9637 SourceLocation StartLoc,
9638 SourceLocation LParenLoc,
9639 SourceLocation EndLoc) {
9640 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00009641 for (auto &RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009642 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009643 SourceLocation ELoc;
9644 SourceRange ERange;
9645 Expr *SimpleRefExpr = RefExpr;
9646 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009647 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00009648 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009649 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009650 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009651 ValueDecl *D = Res.first;
9652 if (!D)
9653 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +00009654
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009655 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009656 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9657 // in a Construct]
9658 // Variables with the predetermined data-sharing attributes may not be
9659 // listed in data-sharing attributes clauses, except for the cases
9660 // listed below. For these exceptions only, listing a predetermined
9661 // variable in a data-sharing attribute clause is allowed and overrides
9662 // the variable's predetermined data-sharing attributes.
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009663 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00009664 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
9665 DVar.RefExpr) {
9666 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9667 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009668 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009669 continue;
9670 }
9671
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009672 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009673 if (!VD && IsOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00009674 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009675 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009676 Vars.push_back((VD || !Ref || CurContext->isDependentContext())
9677 ? RefExpr->IgnoreParens()
9678 : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009679 }
9680
Alexey Bataeved09d242014-05-28 05:53:51 +00009681 if (Vars.empty())
9682 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00009683
9684 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
9685}
9686
Alexey Bataevc5e02582014-06-16 07:08:35 +00009687namespace {
9688class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
9689 DSAStackTy *Stack;
9690
9691public:
9692 bool VisitDeclRefExpr(DeclRefExpr *E) {
9693 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009694 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00009695 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
9696 return false;
9697 if (DVar.CKind != OMPC_unknown)
9698 return true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +00009699 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
9700 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009701 /*FromParent=*/true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00009702 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00009703 return true;
9704 return false;
9705 }
9706 return false;
9707 }
9708 bool VisitStmt(Stmt *S) {
9709 for (auto Child : S->children()) {
9710 if (Child && Visit(Child))
9711 return true;
9712 }
9713 return false;
9714 }
Alexey Bataev23b69422014-06-18 07:08:49 +00009715 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00009716};
Alexey Bataev23b69422014-06-18 07:08:49 +00009717} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00009718
Alexey Bataev60da77e2016-02-29 05:54:20 +00009719namespace {
9720// Transform MemberExpression for specified FieldDecl of current class to
9721// DeclRefExpr to specified OMPCapturedExprDecl.
9722class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
9723 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
9724 ValueDecl *Field;
9725 DeclRefExpr *CapturedExpr;
9726
9727public:
9728 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
9729 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
9730
9731 ExprResult TransformMemberExpr(MemberExpr *E) {
9732 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
9733 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +00009734 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009735 return CapturedExpr;
9736 }
9737 return BaseTransform::TransformMemberExpr(E);
9738 }
9739 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
9740};
9741} // namespace
9742
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009743template <typename T>
9744static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups,
9745 const llvm::function_ref<T(ValueDecl *)> &Gen) {
9746 for (auto &Set : Lookups) {
9747 for (auto *D : Set) {
9748 if (auto Res = Gen(cast<ValueDecl>(D)))
9749 return Res;
9750 }
9751 }
9752 return T();
9753}
9754
9755static ExprResult
9756buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
9757 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
9758 const DeclarationNameInfo &ReductionId, QualType Ty,
9759 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
9760 if (ReductionIdScopeSpec.isInvalid())
9761 return ExprError();
9762 SmallVector<UnresolvedSet<8>, 4> Lookups;
9763 if (S) {
9764 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
9765 Lookup.suppressDiagnostics();
9766 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
9767 auto *D = Lookup.getRepresentativeDecl();
9768 do {
9769 S = S->getParent();
9770 } while (S && !S->isDeclScope(D));
9771 if (S)
9772 S = S->getParent();
9773 Lookups.push_back(UnresolvedSet<8>());
9774 Lookups.back().append(Lookup.begin(), Lookup.end());
9775 Lookup.clear();
9776 }
9777 } else if (auto *ULE =
9778 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
9779 Lookups.push_back(UnresolvedSet<8>());
9780 Decl *PrevD = nullptr;
David Majnemer9d168222016-08-05 17:44:54 +00009781 for (auto *D : ULE->decls()) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009782 if (D == PrevD)
9783 Lookups.push_back(UnresolvedSet<8>());
9784 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
9785 Lookups.back().addDecl(DRD);
9786 PrevD = D;
9787 }
9788 }
Alexey Bataevfdc20352017-08-25 15:43:55 +00009789 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() ||
9790 Ty->isInstantiationDependentType() ||
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009791 Ty->containsUnexpandedParameterPack() ||
9792 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool {
9793 return !D->isInvalidDecl() &&
9794 (D->getType()->isDependentType() ||
9795 D->getType()->isInstantiationDependentType() ||
9796 D->getType()->containsUnexpandedParameterPack());
9797 })) {
9798 UnresolvedSet<8> ResSet;
9799 for (auto &Set : Lookups) {
9800 ResSet.append(Set.begin(), Set.end());
9801 // The last item marks the end of all declarations at the specified scope.
9802 ResSet.addDecl(Set[Set.size() - 1]);
9803 }
9804 return UnresolvedLookupExpr::Create(
9805 SemaRef.Context, /*NamingClass=*/nullptr,
9806 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
9807 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
9808 }
9809 if (auto *VD = filterLookupForUDR<ValueDecl *>(
9810 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
9811 if (!D->isInvalidDecl() &&
9812 SemaRef.Context.hasSameType(D->getType(), Ty))
9813 return D;
9814 return nullptr;
9815 }))
9816 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
9817 if (auto *VD = filterLookupForUDR<ValueDecl *>(
9818 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
9819 if (!D->isInvalidDecl() &&
9820 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
9821 !Ty.isMoreQualifiedThan(D->getType()))
9822 return D;
9823 return nullptr;
9824 })) {
9825 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
9826 /*DetectVirtual=*/false);
9827 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
9828 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
9829 VD->getType().getUnqualifiedType()))) {
9830 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
9831 /*DiagID=*/0) !=
9832 Sema::AR_inaccessible) {
9833 SemaRef.BuildBasePathArray(Paths, BasePath);
9834 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
9835 }
9836 }
9837 }
9838 }
9839 if (ReductionIdScopeSpec.isSet()) {
9840 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
9841 return ExprError();
9842 }
9843 return ExprEmpty();
9844}
9845
Alexey Bataevfad872fc2017-07-18 15:32:58 +00009846namespace {
9847/// Data for the reduction-based clauses.
9848struct ReductionData {
9849 /// List of original reduction items.
9850 SmallVector<Expr *, 8> Vars;
9851 /// List of private copies of the reduction items.
9852 SmallVector<Expr *, 8> Privates;
9853 /// LHS expressions for the reduction_op expressions.
9854 SmallVector<Expr *, 8> LHSs;
9855 /// RHS expressions for the reduction_op expressions.
9856 SmallVector<Expr *, 8> RHSs;
9857 /// Reduction operation expression.
9858 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev88202be2017-07-27 13:20:36 +00009859 /// Taskgroup descriptors for the corresponding reduction items in
9860 /// in_reduction clauses.
9861 SmallVector<Expr *, 8> TaskgroupDescriptors;
Alexey Bataevfad872fc2017-07-18 15:32:58 +00009862 /// List of captures for clause.
9863 SmallVector<Decl *, 4> ExprCaptures;
9864 /// List of postupdate expressions.
9865 SmallVector<Expr *, 4> ExprPostUpdates;
9866 ReductionData() = delete;
9867 /// Reserves required memory for the reduction data.
9868 ReductionData(unsigned Size) {
9869 Vars.reserve(Size);
9870 Privates.reserve(Size);
9871 LHSs.reserve(Size);
9872 RHSs.reserve(Size);
9873 ReductionOps.reserve(Size);
Alexey Bataev88202be2017-07-27 13:20:36 +00009874 TaskgroupDescriptors.reserve(Size);
Alexey Bataevfad872fc2017-07-18 15:32:58 +00009875 ExprCaptures.reserve(Size);
9876 ExprPostUpdates.reserve(Size);
9877 }
9878 /// Stores reduction item and reduction operation only (required for dependent
9879 /// reduction item).
9880 void push(Expr *Item, Expr *ReductionOp) {
9881 Vars.emplace_back(Item);
9882 Privates.emplace_back(nullptr);
9883 LHSs.emplace_back(nullptr);
9884 RHSs.emplace_back(nullptr);
9885 ReductionOps.emplace_back(ReductionOp);
Alexey Bataev88202be2017-07-27 13:20:36 +00009886 TaskgroupDescriptors.emplace_back(nullptr);
Alexey Bataevfad872fc2017-07-18 15:32:58 +00009887 }
9888 /// Stores reduction data.
Alexey Bataev88202be2017-07-27 13:20:36 +00009889 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp,
9890 Expr *TaskgroupDescriptor) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +00009891 Vars.emplace_back(Item);
9892 Privates.emplace_back(Private);
9893 LHSs.emplace_back(LHS);
9894 RHSs.emplace_back(RHS);
9895 ReductionOps.emplace_back(ReductionOp);
Alexey Bataev88202be2017-07-27 13:20:36 +00009896 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor);
Alexey Bataevfad872fc2017-07-18 15:32:58 +00009897 }
9898};
9899} // namespace
9900
Jonas Hahnfeld4525c822017-10-23 19:01:35 +00009901static bool CheckOMPArraySectionConstantForReduction(
9902 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement,
9903 SmallVectorImpl<llvm::APSInt> &ArraySizes) {
9904 const Expr *Length = OASE->getLength();
9905 if (Length == nullptr) {
9906 // For array sections of the form [1:] or [:], we would need to analyze
9907 // the lower bound...
9908 if (OASE->getColonLoc().isValid())
9909 return false;
9910
9911 // This is an array subscript which has implicit length 1!
9912 SingleElement = true;
9913 ArraySizes.push_back(llvm::APSInt::get(1));
9914 } else {
9915 llvm::APSInt ConstantLengthValue;
9916 if (!Length->EvaluateAsInt(ConstantLengthValue, Context))
9917 return false;
9918
9919 SingleElement = (ConstantLengthValue.getSExtValue() == 1);
9920 ArraySizes.push_back(ConstantLengthValue);
9921 }
9922
9923 // Get the base of this array section and walk up from there.
9924 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
9925
9926 // We require length = 1 for all array sections except the right-most to
9927 // guarantee that the memory region is contiguous and has no holes in it.
9928 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) {
9929 Length = TempOASE->getLength();
9930 if (Length == nullptr) {
9931 // For array sections of the form [1:] or [:], we would need to analyze
9932 // the lower bound...
9933 if (OASE->getColonLoc().isValid())
9934 return false;
9935
9936 // This is an array subscript which has implicit length 1!
9937 ArraySizes.push_back(llvm::APSInt::get(1));
9938 } else {
9939 llvm::APSInt ConstantLengthValue;
9940 if (!Length->EvaluateAsInt(ConstantLengthValue, Context) ||
9941 ConstantLengthValue.getSExtValue() != 1)
9942 return false;
9943
9944 ArraySizes.push_back(ConstantLengthValue);
9945 }
9946 Base = TempOASE->getBase()->IgnoreParenImpCasts();
9947 }
9948
9949 // If we have a single element, we don't need to add the implicit lengths.
9950 if (!SingleElement) {
9951 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) {
9952 // Has implicit length 1!
9953 ArraySizes.push_back(llvm::APSInt::get(1));
9954 Base = TempASE->getBase()->IgnoreParenImpCasts();
9955 }
9956 }
9957
9958 // This array section can be privatized as a single value or as a constant
9959 // sized array.
9960 return true;
9961}
9962
Alexey Bataevfad872fc2017-07-18 15:32:58 +00009963static bool ActOnOMPReductionKindClause(
Alexey Bataev169d96a2017-07-18 20:17:46 +00009964 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind,
9965 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
9966 SourceLocation ColonLoc, SourceLocation EndLoc,
9967 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
Alexey Bataevfad872fc2017-07-18 15:32:58 +00009968 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00009969 auto DN = ReductionId.getName();
9970 auto OOK = DN.getCXXOverloadedOperator();
9971 BinaryOperatorKind BOK = BO_Comma;
9972
Alexey Bataevfad872fc2017-07-18 15:32:58 +00009973 ASTContext &Context = S.Context;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009974 // OpenMP [2.14.3.6, reduction clause]
9975 // C
9976 // reduction-identifier is either an identifier or one of the following
9977 // operators: +, -, *, &, |, ^, && and ||
9978 // C++
9979 // reduction-identifier is either an id-expression or one of the following
9980 // operators: +, -, *, &, |, ^, && and ||
Alexey Bataevc5e02582014-06-16 07:08:35 +00009981 switch (OOK) {
9982 case OO_Plus:
9983 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009984 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009985 break;
9986 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009987 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009988 break;
9989 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009990 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009991 break;
9992 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009993 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009994 break;
9995 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009996 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009997 break;
9998 case OO_AmpAmp:
9999 BOK = BO_LAnd;
10000 break;
10001 case OO_PipePipe:
10002 BOK = BO_LOr;
10003 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010004 case OO_New:
10005 case OO_Delete:
10006 case OO_Array_New:
10007 case OO_Array_Delete:
10008 case OO_Slash:
10009 case OO_Percent:
10010 case OO_Tilde:
10011 case OO_Exclaim:
10012 case OO_Equal:
10013 case OO_Less:
10014 case OO_Greater:
10015 case OO_LessEqual:
10016 case OO_GreaterEqual:
10017 case OO_PlusEqual:
10018 case OO_MinusEqual:
10019 case OO_StarEqual:
10020 case OO_SlashEqual:
10021 case OO_PercentEqual:
10022 case OO_CaretEqual:
10023 case OO_AmpEqual:
10024 case OO_PipeEqual:
10025 case OO_LessLess:
10026 case OO_GreaterGreater:
10027 case OO_LessLessEqual:
10028 case OO_GreaterGreaterEqual:
10029 case OO_EqualEqual:
10030 case OO_ExclaimEqual:
Richard Smithd30b23d2017-12-01 02:13:10 +000010031 case OO_Spaceship:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010032 case OO_PlusPlus:
10033 case OO_MinusMinus:
10034 case OO_Comma:
10035 case OO_ArrowStar:
10036 case OO_Arrow:
10037 case OO_Call:
10038 case OO_Subscript:
10039 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +000010040 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010041 case NUM_OVERLOADED_OPERATORS:
10042 llvm_unreachable("Unexpected reduction identifier");
10043 case OO_None:
Alexey Bataev4d4624c2017-07-20 16:47:47 +000010044 if (auto *II = DN.getAsIdentifierInfo()) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010045 if (II->isStr("max"))
10046 BOK = BO_GT;
10047 else if (II->isStr("min"))
10048 BOK = BO_LT;
10049 }
10050 break;
10051 }
10052 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010053 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +000010054 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataev4d4624c2017-07-20 16:47:47 +000010055 else
10056 ReductionIdRange.setBegin(ReductionId.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +000010057 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +000010058
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010059 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
10060 bool FirstIter = true;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010061 for (auto RefExpr : VarList) {
10062 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +000010063 // OpenMP [2.1, C/C++]
10064 // A list item is a variable or array section, subject to the restrictions
10065 // specified in Section 2.4 on page 42 and in each of the sections
10066 // describing clauses and directives for which a list appears.
10067 // OpenMP [2.14.3.3, Restrictions, p.1]
10068 // A variable that is part of another variable (as an array or
10069 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010070 if (!FirstIter && IR != ER)
10071 ++IR;
10072 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010073 SourceLocation ELoc;
10074 SourceRange ERange;
10075 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010076 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
Alexey Bataev60da77e2016-02-29 05:54:20 +000010077 /*AllowArraySection=*/true);
10078 if (Res.second) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010079 // Try to find 'declare reduction' corresponding construct before using
10080 // builtin/overloaded operators.
10081 QualType Type = Context.DependentTy;
10082 CXXCastPath BasePath;
10083 ExprResult DeclareReductionRef = buildDeclareReductionRef(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010084 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010085 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010086 Expr *ReductionOp = nullptr;
10087 if (S.CurContext->isDependentContext() &&
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010088 (DeclareReductionRef.isUnset() ||
10089 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010090 ReductionOp = DeclareReductionRef.get();
10091 // It will be analyzed later.
10092 RD.push(RefExpr, ReductionOp);
Alexey Bataevc5e02582014-06-16 07:08:35 +000010093 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000010094 ValueDecl *D = Res.first;
10095 if (!D)
10096 continue;
10097
Alexey Bataev88202be2017-07-27 13:20:36 +000010098 Expr *TaskgroupDescriptor = nullptr;
Alexey Bataeva1764212015-09-30 09:22:36 +000010099 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010100 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
10101 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
10102 if (ASE)
Alexey Bataev31300ed2016-02-04 11:27:03 +000010103 Type = ASE->getType().getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +000010104 else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +000010105 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
10106 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
10107 Type = ATy->getElementType();
10108 else
10109 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +000010110 Type = Type.getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +000010111 } else
10112 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
10113 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +000010114
Alexey Bataevc5e02582014-06-16 07:08:35 +000010115 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
10116 // A variable that appears in a private clause must not have an incomplete
10117 // type or a reference type.
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010118 if (S.RequireCompleteType(ELoc, Type,
10119 diag::err_omp_reduction_incomplete_type))
Alexey Bataevc5e02582014-06-16 07:08:35 +000010120 continue;
10121 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +000010122 // A list item that appears in a reduction clause must not be
10123 // const-qualified.
10124 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataev169d96a2017-07-18 20:17:46 +000010125 S.Diag(ELoc, diag::err_omp_const_reduction_list_item) << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010126 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010127 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10128 VarDecl::DeclarationOnly;
10129 S.Diag(D->getLocation(),
10130 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +000010131 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +000010132 }
Alexey Bataevc5e02582014-06-16 07:08:35 +000010133 continue;
10134 }
10135 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
10136 // If a list-item is a reference type then it must bind to the same object
10137 // for all threads of the team.
Alexey Bataev60da77e2016-02-29 05:54:20 +000010138 if (!ASE && !OASE && VD) {
Alexey Bataeva1764212015-09-30 09:22:36 +000010139 VarDecl *VDDef = VD->getDefinition();
David Sheinkman92589992016-10-04 14:41:36 +000010140 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010141 DSARefChecker Check(Stack);
Alexey Bataeva1764212015-09-30 09:22:36 +000010142 if (Check.Visit(VDDef->getInit())) {
Alexey Bataev169d96a2017-07-18 20:17:46 +000010143 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg)
10144 << getOpenMPClauseName(ClauseKind) << ERange;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010145 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
Alexey Bataeva1764212015-09-30 09:22:36 +000010146 continue;
10147 }
Alexey Bataevc5e02582014-06-16 07:08:35 +000010148 }
10149 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010150
Alexey Bataevc5e02582014-06-16 07:08:35 +000010151 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
10152 // in a Construct]
10153 // Variables with the predetermined data-sharing attributes may not be
10154 // listed in data-sharing attributes clauses, except for the cases
10155 // listed below. For these exceptions only, listing a predetermined
10156 // variable in a data-sharing attribute clause is allowed and overrides
10157 // the variable's predetermined data-sharing attributes.
10158 // OpenMP [2.14.3.6, Restrictions, p.3]
10159 // Any number of reduction clauses can be specified on the directive,
10160 // but a list item can appear only once in the reduction clauses for that
10161 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +000010162 DSAStackTy::DSAVarData DVar;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010163 DVar = Stack->getTopDSA(D, false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010164 if (DVar.CKind == OMPC_reduction) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010165 S.Diag(ELoc, diag::err_omp_once_referenced)
Alexey Bataev169d96a2017-07-18 20:17:46 +000010166 << getOpenMPClauseName(ClauseKind);
Alexey Bataev60da77e2016-02-29 05:54:20 +000010167 if (DVar.RefExpr)
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010168 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataev4d4624c2017-07-20 16:47:47 +000010169 continue;
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010170 } else if (DVar.CKind != OMPC_unknown) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010171 S.Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010172 << getOpenMPClauseName(DVar.CKind)
10173 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010174 ReportOriginalDSA(S, Stack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010175 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010176 }
10177
10178 // OpenMP [2.14.3.6, Restrictions, p.1]
10179 // A list item that appears in a reduction clause of a worksharing
10180 // construct must be shared in the parallel regions to which any of the
10181 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010182 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective();
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010183 if (isOpenMPWorksharingDirective(CurrDir) &&
Kelvin Li579e41c2016-11-30 23:51:03 +000010184 !isOpenMPParallelDirective(CurrDir) &&
10185 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010186 DVar = Stack->getImplicitDSA(D, true);
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010187 if (DVar.CKind != OMPC_shared) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010188 S.Diag(ELoc, diag::err_omp_required_access)
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010189 << getOpenMPClauseName(OMPC_reduction)
10190 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010191 ReportOriginalDSA(S, Stack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010192 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +000010193 }
10194 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010195
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010196 // Try to find 'declare reduction' corresponding construct before using
10197 // builtin/overloaded operators.
10198 CXXCastPath BasePath;
10199 ExprResult DeclareReductionRef = buildDeclareReductionRef(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010200 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010201 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
10202 if (DeclareReductionRef.isInvalid())
10203 continue;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010204 if (S.CurContext->isDependentContext() &&
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010205 (DeclareReductionRef.isUnset() ||
10206 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010207 RD.push(RefExpr, DeclareReductionRef.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010208 continue;
10209 }
10210 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
10211 // Not allowed reduction identifier is found.
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010212 S.Diag(ReductionId.getLocStart(),
10213 diag::err_omp_unknown_reduction_identifier)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010214 << Type << ReductionIdRange;
10215 continue;
10216 }
10217
10218 // OpenMP [2.14.3.6, reduction clause, Restrictions]
10219 // The type of a list item that appears in a reduction clause must be valid
10220 // for the reduction-identifier. For a max or min reduction in C, the type
10221 // of the list item must be an allowed arithmetic data type: char, int,
10222 // float, double, or _Bool, possibly modified with long, short, signed, or
10223 // unsigned. For a max or min reduction in C++, the type of the list item
10224 // must be an allowed arithmetic data type: char, wchar_t, int, float,
10225 // double, or bool, possibly modified with long, short, signed, or unsigned.
10226 if (DeclareReductionRef.isUnset()) {
10227 if ((BOK == BO_GT || BOK == BO_LT) &&
10228 !(Type->isScalarType() ||
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010229 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
10230 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
Alexey Bataev169d96a2017-07-18 20:17:46 +000010231 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010232 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010233 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10234 VarDecl::DeclarationOnly;
10235 S.Diag(D->getLocation(),
10236 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010237 << D;
10238 }
10239 continue;
10240 }
10241 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010242 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) {
Alexey Bataev169d96a2017-07-18 20:17:46 +000010243 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg)
10244 << getOpenMPClauseName(ClauseKind);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010245 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010246 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10247 VarDecl::DeclarationOnly;
10248 S.Diag(D->getLocation(),
10249 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010250 << D;
10251 }
10252 continue;
10253 }
10254 }
10255
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010256 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010257 auto *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs",
Alexey Bataev60da77e2016-02-29 05:54:20 +000010258 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010259 auto *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(),
Alexey Bataev60da77e2016-02-29 05:54:20 +000010260 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010261 auto PrivateTy = Type;
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010262
10263 // Try if we can determine constant lengths for all array sections and avoid
10264 // the VLA.
10265 bool ConstantLengthOASE = false;
10266 if (OASE) {
10267 bool SingleElement;
10268 llvm::SmallVector<llvm::APSInt, 4> ArraySizes;
10269 ConstantLengthOASE = CheckOMPArraySectionConstantForReduction(
10270 Context, OASE, SingleElement, ArraySizes);
10271
10272 // If we don't have a single element, we must emit a constant array type.
10273 if (ConstantLengthOASE && !SingleElement) {
10274 for (auto &Size : ArraySizes) {
10275 PrivateTy = Context.getConstantArrayType(
10276 PrivateTy, Size, ArrayType::Normal, /*IndexTypeQuals=*/0);
10277 }
10278 }
10279 }
10280
10281 if ((OASE && !ConstantLengthOASE) ||
Jonas Hahnfeld96087f32017-11-02 13:30:42 +000010282 (!OASE && !ASE &&
Alexey Bataev60da77e2016-02-29 05:54:20 +000010283 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Jonas Hahnfeld87d44262017-11-18 21:00:46 +000010284 if (!Context.getTargetInfo().isVLASupported() &&
10285 S.shouldDiagnoseTargetSupportFromOpenMP()) {
10286 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
10287 S.Diag(ELoc, diag::note_vla_unsupported);
10288 continue;
10289 }
David Majnemer9d168222016-08-05 17:44:54 +000010290 // For arrays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010291 // Create pseudo array type for private copy. The size for this array will
10292 // be generated during codegen.
10293 // For array subscripts or single variables Private Ty is the same as Type
10294 // (type of the variable or single array element).
10295 PrivateTy = Context.getVariableArrayType(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010296 Type,
Alexey Bataevd070a582017-10-25 15:54:04 +000010297 new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue),
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010298 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +000010299 } else if (!ASE && !OASE &&
10300 Context.getAsArrayType(D->getType().getNonReferenceType()))
10301 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010302 // Private copy.
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010303 auto *PrivateVD = buildVarDecl(S, ELoc, PrivateTy, D->getName(),
Alexey Bataev60da77e2016-02-29 05:54:20 +000010304 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010305 // Add initializer for private variable.
10306 Expr *Init = nullptr;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010307 auto *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc);
10308 auto *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010309 if (DeclareReductionRef.isUsable()) {
10310 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
10311 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
10312 if (DRD->getInitializer()) {
10313 Init = DRDRef;
10314 RHSVD->setInit(DRDRef);
10315 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010316 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010317 } else {
10318 switch (BOK) {
10319 case BO_Add:
10320 case BO_Xor:
10321 case BO_Or:
10322 case BO_LOr:
10323 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
10324 if (Type->isScalarType() || Type->isAnyComplexType())
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010325 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010326 break;
10327 case BO_Mul:
10328 case BO_LAnd:
10329 if (Type->isScalarType() || Type->isAnyComplexType()) {
10330 // '*' and '&&' reduction ops - initializer is '1'.
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010331 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +000010332 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010333 break;
10334 case BO_And: {
10335 // '&' reduction op - initializer is '~0'.
10336 QualType OrigType = Type;
10337 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
10338 Type = ComplexTy->getElementType();
10339 if (Type->isRealFloatingType()) {
10340 llvm::APFloat InitValue =
10341 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
10342 /*isIEEE=*/true);
10343 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
10344 Type, ELoc);
10345 } else if (Type->isScalarType()) {
10346 auto Size = Context.getTypeSize(Type);
10347 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
10348 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
10349 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
10350 }
10351 if (Init && OrigType->isAnyComplexType()) {
10352 // Init = 0xFFFF + 0xFFFFi;
10353 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010354 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010355 }
10356 Type = OrigType;
10357 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010358 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010359 case BO_LT:
10360 case BO_GT: {
10361 // 'min' reduction op - initializer is 'Largest representable number in
10362 // the reduction list item type'.
10363 // 'max' reduction op - initializer is 'Least representable number in
10364 // the reduction list item type'.
10365 if (Type->isIntegerType() || Type->isPointerType()) {
10366 bool IsSigned = Type->hasSignedIntegerRepresentation();
10367 auto Size = Context.getTypeSize(Type);
10368 QualType IntTy =
10369 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
10370 llvm::APInt InitValue =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010371 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
10372 : llvm::APInt::getMinValue(Size)
10373 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
10374 : llvm::APInt::getMaxValue(Size);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010375 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
10376 if (Type->isPointerType()) {
10377 // Cast to pointer type.
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010378 auto CastExpr = S.BuildCStyleCastExpr(
Alexey Bataevd070a582017-10-25 15:54:04 +000010379 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010380 if (CastExpr.isInvalid())
10381 continue;
10382 Init = CastExpr.get();
10383 }
10384 } else if (Type->isRealFloatingType()) {
10385 llvm::APFloat InitValue = llvm::APFloat::getLargest(
10386 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
10387 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
10388 Type, ELoc);
10389 }
10390 break;
10391 }
10392 case BO_PtrMemD:
10393 case BO_PtrMemI:
10394 case BO_MulAssign:
10395 case BO_Div:
10396 case BO_Rem:
10397 case BO_Sub:
10398 case BO_Shl:
10399 case BO_Shr:
10400 case BO_LE:
10401 case BO_GE:
10402 case BO_EQ:
10403 case BO_NE:
Richard Smithc70f1d62017-12-14 15:16:18 +000010404 case BO_Cmp:
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010405 case BO_AndAssign:
10406 case BO_XorAssign:
10407 case BO_OrAssign:
10408 case BO_Assign:
10409 case BO_AddAssign:
10410 case BO_SubAssign:
10411 case BO_DivAssign:
10412 case BO_RemAssign:
10413 case BO_ShlAssign:
10414 case BO_ShrAssign:
10415 case BO_Comma:
10416 llvm_unreachable("Unexpected reduction operation");
10417 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010418 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010419 if (Init && DeclareReductionRef.isUnset())
10420 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false);
10421 else if (!Init)
10422 S.ActOnUninitializedDecl(RHSVD);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010423 if (RHSVD->isInvalidDecl())
10424 continue;
10425 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010426 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible)
10427 << Type << ReductionIdRange;
10428 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10429 VarDecl::DeclarationOnly;
10430 S.Diag(D->getLocation(),
10431 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +000010432 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010433 continue;
10434 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010435 // Store initializer for single element in private copy. Will be used during
10436 // codegen.
10437 PrivateVD->setInit(RHSVD->getInit());
10438 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010439 auto *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010440 ExprResult ReductionOp;
10441 if (DeclareReductionRef.isUsable()) {
10442 QualType RedTy = DeclareReductionRef.get()->getType();
10443 QualType PtrRedTy = Context.getPointerType(RedTy);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010444 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
10445 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010446 if (!BasePath.empty()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010447 LHS = S.DefaultLvalueConversion(LHS.get());
10448 RHS = S.DefaultLvalueConversion(RHS.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010449 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
10450 CK_UncheckedDerivedToBase, LHS.get(),
10451 &BasePath, LHS.get()->getValueKind());
10452 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
10453 CK_UncheckedDerivedToBase, RHS.get(),
10454 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010455 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010456 FunctionProtoType::ExtProtoInfo EPI;
10457 QualType Params[] = {PtrRedTy, PtrRedTy};
10458 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
10459 auto *OVE = new (Context) OpaqueValueExpr(
10460 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010461 S.DefaultLvalueConversion(DeclareReductionRef.get()).get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010462 Expr *Args[] = {LHS.get(), RHS.get()};
10463 ReductionOp = new (Context)
10464 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
10465 } else {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010466 ReductionOp = S.BuildBinOp(
10467 Stack->getCurScope(), ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010468 if (ReductionOp.isUsable()) {
10469 if (BOK != BO_LT && BOK != BO_GT) {
10470 ReductionOp =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010471 S.BuildBinOp(Stack->getCurScope(), ReductionId.getLocStart(),
10472 BO_Assign, LHSDRE, ReductionOp.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010473 } else {
Alexey Bataevd070a582017-10-25 15:54:04 +000010474 auto *ConditionalOp = new (Context)
10475 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE,
10476 Type, VK_LValue, OK_Ordinary);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010477 ReductionOp =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010478 S.BuildBinOp(Stack->getCurScope(), ReductionId.getLocStart(),
10479 BO_Assign, LHSDRE, ConditionalOp);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010480 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000010481 if (ReductionOp.isUsable())
10482 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010483 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000010484 if (!ReductionOp.isUsable())
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010485 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010486 }
10487
Alexey Bataevfa312f32017-07-21 18:48:21 +000010488 // OpenMP [2.15.4.6, Restrictions, p.2]
10489 // A list item that appears in an in_reduction clause of a task construct
10490 // must appear in a task_reduction clause of a construct associated with a
10491 // taskgroup region that includes the participating task in its taskgroup
10492 // set. The construct associated with the innermost region that meets this
10493 // condition must specify the same reduction-identifier as the in_reduction
10494 // clause.
10495 if (ClauseKind == OMPC_in_reduction) {
Alexey Bataevfa312f32017-07-21 18:48:21 +000010496 SourceRange ParentSR;
10497 BinaryOperatorKind ParentBOK;
10498 const Expr *ParentReductionOp;
Alexey Bataev88202be2017-07-27 13:20:36 +000010499 Expr *ParentBOKTD, *ParentReductionOpTD;
Alexey Bataevf189cb72017-07-24 14:52:13 +000010500 DSAStackTy::DSAVarData ParentBOKDSA =
Alexey Bataev88202be2017-07-27 13:20:36 +000010501 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK,
10502 ParentBOKTD);
Alexey Bataevf189cb72017-07-24 14:52:13 +000010503 DSAStackTy::DSAVarData ParentReductionOpDSA =
Alexey Bataev88202be2017-07-27 13:20:36 +000010504 Stack->getTopMostTaskgroupReductionData(
10505 D, ParentSR, ParentReductionOp, ParentReductionOpTD);
Alexey Bataevf189cb72017-07-24 14:52:13 +000010506 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown;
10507 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown;
10508 if (!IsParentBOK && !IsParentReductionOp) {
10509 S.Diag(ELoc, diag::err_omp_in_reduction_not_task_reduction);
10510 continue;
10511 }
Alexey Bataevfa312f32017-07-21 18:48:21 +000010512 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) ||
10513 (DeclareReductionRef.isUsable() && IsParentBOK) || BOK != ParentBOK ||
10514 IsParentReductionOp) {
10515 bool EmitError = true;
10516 if (IsParentReductionOp && DeclareReductionRef.isUsable()) {
10517 llvm::FoldingSetNodeID RedId, ParentRedId;
10518 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true);
10519 DeclareReductionRef.get()->Profile(RedId, Context,
10520 /*Canonical=*/true);
10521 EmitError = RedId != ParentRedId;
10522 }
10523 if (EmitError) {
10524 S.Diag(ReductionId.getLocStart(),
10525 diag::err_omp_reduction_identifier_mismatch)
10526 << ReductionIdRange << RefExpr->getSourceRange();
10527 S.Diag(ParentSR.getBegin(),
10528 diag::note_omp_previous_reduction_identifier)
Alexey Bataevf189cb72017-07-24 14:52:13 +000010529 << ParentSR
10530 << (IsParentBOK ? ParentBOKDSA.RefExpr
10531 : ParentReductionOpDSA.RefExpr)
10532 ->getSourceRange();
Alexey Bataevfa312f32017-07-21 18:48:21 +000010533 continue;
10534 }
10535 }
Alexey Bataev88202be2017-07-27 13:20:36 +000010536 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD;
10537 assert(TaskgroupDescriptor && "Taskgroup descriptor must be defined.");
Alexey Bataevfa312f32017-07-21 18:48:21 +000010538 }
10539
Alexey Bataev60da77e2016-02-29 05:54:20 +000010540 DeclRefExpr *Ref = nullptr;
10541 Expr *VarsExpr = RefExpr->IgnoreParens();
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010542 if (!VD && !S.CurContext->isDependentContext()) {
Alexey Bataev60da77e2016-02-29 05:54:20 +000010543 if (ASE || OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010544 TransformExprToCaptures RebuildToCapture(S, D);
Alexey Bataev60da77e2016-02-29 05:54:20 +000010545 VarsExpr =
10546 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
10547 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +000010548 } else {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010549 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +000010550 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010551 if (!S.IsOpenMPCapturedDecl(D)) {
10552 RD.ExprCaptures.emplace_back(Ref->getDecl());
Alexey Bataev5a3af132016-03-29 08:58:54 +000010553 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010554 ExprResult RefRes = S.DefaultLvalueConversion(Ref);
Alexey Bataev5a3af132016-03-29 08:58:54 +000010555 if (!RefRes.isUsable())
10556 continue;
10557 ExprResult PostUpdateRes =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010558 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
10559 RefRes.get());
Alexey Bataev5a3af132016-03-29 08:58:54 +000010560 if (!PostUpdateRes.isUsable())
10561 continue;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010562 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
10563 Stack->getCurrentDirective() == OMPD_taskgroup) {
10564 S.Diag(RefExpr->getExprLoc(),
10565 diag::err_omp_reduction_non_addressable_expression)
Alexey Bataevbcd0ae02017-07-11 19:16:44 +000010566 << RefExpr->getSourceRange();
10567 continue;
10568 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010569 RD.ExprPostUpdates.emplace_back(
10570 S.IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +000010571 }
10572 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000010573 }
Alexey Bataev169d96a2017-07-18 20:17:46 +000010574 // All reduction items are still marked as reduction (to do not increase
10575 // code base size).
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010576 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
Alexey Bataevf189cb72017-07-24 14:52:13 +000010577 if (CurrDir == OMPD_taskgroup) {
10578 if (DeclareReductionRef.isUsable())
Alexey Bataev3b1b8952017-07-25 15:53:26 +000010579 Stack->addTaskgroupReductionData(D, ReductionIdRange,
10580 DeclareReductionRef.get());
Alexey Bataevf189cb72017-07-24 14:52:13 +000010581 else
Alexey Bataev3b1b8952017-07-25 15:53:26 +000010582 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK);
Alexey Bataevf189cb72017-07-24 14:52:13 +000010583 }
Alexey Bataev88202be2017-07-27 13:20:36 +000010584 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(),
10585 TaskgroupDescriptor);
Alexey Bataevc5e02582014-06-16 07:08:35 +000010586 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010587 return RD.Vars.empty();
10588}
Alexey Bataevc5e02582014-06-16 07:08:35 +000010589
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010590OMPClause *Sema::ActOnOpenMPReductionClause(
10591 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
10592 SourceLocation ColonLoc, SourceLocation EndLoc,
10593 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
10594 ArrayRef<Expr *> UnresolvedReductions) {
10595 ReductionData RD(VarList.size());
10596
Alexey Bataev169d96a2017-07-18 20:17:46 +000010597 if (ActOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList,
10598 StartLoc, LParenLoc, ColonLoc, EndLoc,
10599 ReductionIdScopeSpec, ReductionId,
10600 UnresolvedReductions, RD))
Alexey Bataevc5e02582014-06-16 07:08:35 +000010601 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +000010602
Alexey Bataevc5e02582014-06-16 07:08:35 +000010603 return OMPReductionClause::Create(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010604 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
10605 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
10606 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
10607 buildPreInits(Context, RD.ExprCaptures),
10608 buildPostUpdate(*this, RD.ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +000010609}
10610
Alexey Bataev169d96a2017-07-18 20:17:46 +000010611OMPClause *Sema::ActOnOpenMPTaskReductionClause(
10612 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
10613 SourceLocation ColonLoc, SourceLocation EndLoc,
10614 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
10615 ArrayRef<Expr *> UnresolvedReductions) {
10616 ReductionData RD(VarList.size());
10617
10618 if (ActOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction,
10619 VarList, StartLoc, LParenLoc, ColonLoc,
10620 EndLoc, ReductionIdScopeSpec, ReductionId,
10621 UnresolvedReductions, RD))
10622 return nullptr;
10623
10624 return OMPTaskReductionClause::Create(
10625 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
10626 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
10627 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
10628 buildPreInits(Context, RD.ExprCaptures),
10629 buildPostUpdate(*this, RD.ExprPostUpdates));
10630}
10631
Alexey Bataevfa312f32017-07-21 18:48:21 +000010632OMPClause *Sema::ActOnOpenMPInReductionClause(
10633 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
10634 SourceLocation ColonLoc, SourceLocation EndLoc,
10635 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
10636 ArrayRef<Expr *> UnresolvedReductions) {
10637 ReductionData RD(VarList.size());
10638
10639 if (ActOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList,
10640 StartLoc, LParenLoc, ColonLoc, EndLoc,
10641 ReductionIdScopeSpec, ReductionId,
10642 UnresolvedReductions, RD))
10643 return nullptr;
10644
10645 return OMPInReductionClause::Create(
10646 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
10647 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
Alexey Bataev88202be2017-07-27 13:20:36 +000010648 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors,
Alexey Bataevfa312f32017-07-21 18:48:21 +000010649 buildPreInits(Context, RD.ExprCaptures),
10650 buildPostUpdate(*this, RD.ExprPostUpdates));
10651}
10652
Alexey Bataevecba70f2016-04-12 11:02:11 +000010653bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
10654 SourceLocation LinLoc) {
10655 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
10656 LinKind == OMPC_LINEAR_unknown) {
10657 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
10658 return true;
10659 }
10660 return false;
10661}
10662
10663bool Sema::CheckOpenMPLinearDecl(ValueDecl *D, SourceLocation ELoc,
10664 OpenMPLinearClauseKind LinKind,
10665 QualType Type) {
10666 auto *VD = dyn_cast_or_null<VarDecl>(D);
10667 // A variable must not have an incomplete type or a reference type.
10668 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
10669 return true;
10670 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
10671 !Type->isReferenceType()) {
10672 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
10673 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
10674 return true;
10675 }
10676 Type = Type.getNonReferenceType();
10677
10678 // A list item must not be const-qualified.
10679 if (Type.isConstant(Context)) {
10680 Diag(ELoc, diag::err_omp_const_variable)
10681 << getOpenMPClauseName(OMPC_linear);
10682 if (D) {
10683 bool IsDecl =
10684 !VD ||
10685 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
10686 Diag(D->getLocation(),
10687 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
10688 << D;
10689 }
10690 return true;
10691 }
10692
10693 // A list item must be of integral or pointer type.
10694 Type = Type.getUnqualifiedType().getCanonicalType();
10695 const auto *Ty = Type.getTypePtrOrNull();
10696 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
10697 !Ty->isPointerType())) {
10698 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
10699 if (D) {
10700 bool IsDecl =
10701 !VD ||
10702 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
10703 Diag(D->getLocation(),
10704 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
10705 << D;
10706 }
10707 return true;
10708 }
10709 return false;
10710}
10711
Alexey Bataev182227b2015-08-20 10:54:39 +000010712OMPClause *Sema::ActOnOpenMPLinearClause(
10713 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
10714 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
10715 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +000010716 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010717 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +000010718 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +000010719 SmallVector<Decl *, 4> ExprCaptures;
10720 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +000010721 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +000010722 LinKind = OMPC_LINEAR_val;
Alexey Bataeved09d242014-05-28 05:53:51 +000010723 for (auto &RefExpr : VarList) {
10724 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010725 SourceLocation ELoc;
10726 SourceRange ERange;
10727 Expr *SimpleRefExpr = RefExpr;
10728 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
10729 /*AllowArraySection=*/false);
10730 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +000010731 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000010732 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010733 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +000010734 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +000010735 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010736 ValueDecl *D = Res.first;
10737 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +000010738 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +000010739
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010740 QualType Type = D->getType();
10741 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +000010742
10743 // OpenMP [2.14.3.7, linear clause]
10744 // A list-item cannot appear in more than one linear clause.
10745 // A list-item that appears in a linear clause cannot appear in any
10746 // other data-sharing attribute clause.
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010747 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman8dba6642014-04-22 13:09:42 +000010748 if (DVar.RefExpr) {
10749 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
10750 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010751 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +000010752 continue;
10753 }
10754
Alexey Bataevecba70f2016-04-12 11:02:11 +000010755 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +000010756 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +000010757 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +000010758
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010759 // Build private copy of original var.
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010760 auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(),
10761 D->hasAttrs() ? &D->getAttrs() : nullptr);
10762 auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +000010763 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010764 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000010765 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010766 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010767 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev78849fb2016-03-09 09:49:00 +000010768 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
10769 if (!IsOpenMPCapturedDecl(D)) {
10770 ExprCaptures.push_back(Ref->getDecl());
10771 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
10772 ExprResult RefRes = DefaultLvalueConversion(Ref);
10773 if (!RefRes.isUsable())
10774 continue;
10775 ExprResult PostUpdateRes =
10776 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
10777 SimpleRefExpr, RefRes.get());
10778 if (!PostUpdateRes.isUsable())
10779 continue;
10780 ExprPostUpdates.push_back(
10781 IgnoredValueConversions(PostUpdateRes.get()).get());
10782 }
10783 }
10784 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000010785 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010786 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000010787 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010788 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000010789 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000010790 /*DirectInit=*/false);
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010791 auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
10792
10793 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010794 Vars.push_back((VD || CurContext->isDependentContext())
10795 ? RefExpr->IgnoreParens()
10796 : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010797 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +000010798 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +000010799 }
10800
10801 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000010802 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000010803
10804 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +000010805 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000010806 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
10807 !Step->isInstantiationDependent() &&
10808 !Step->containsUnexpandedParameterPack()) {
10809 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +000010810 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +000010811 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000010812 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +000010813 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +000010814
Alexander Musman3276a272015-03-21 10:12:56 +000010815 // Build var to save the step value.
10816 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +000010817 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +000010818 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +000010819 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +000010820 ExprResult CalcStep =
10821 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +000010822 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +000010823
Alexander Musman8dba6642014-04-22 13:09:42 +000010824 // Warn about zero linear step (it would be probably better specified as
10825 // making corresponding variables 'const').
10826 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +000010827 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
10828 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +000010829 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
10830 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +000010831 if (!IsConstant && CalcStep.isUsable()) {
10832 // Calculate the step beforehand instead of doing this on each iteration.
10833 // (This is not used if the number of iterations may be kfold-ed).
10834 CalcStepExpr = CalcStep.get();
10835 }
Alexander Musman8dba6642014-04-22 13:09:42 +000010836 }
10837
Alexey Bataev182227b2015-08-20 10:54:39 +000010838 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
10839 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +000010840 StepExpr, CalcStepExpr,
10841 buildPreInits(Context, ExprCaptures),
10842 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +000010843}
10844
Alexey Bataev5dff95c2016-04-22 03:56:56 +000010845static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
10846 Expr *NumIterations, Sema &SemaRef,
10847 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +000010848 // Walk the vars and build update/final expressions for the CodeGen.
10849 SmallVector<Expr *, 8> Updates;
10850 SmallVector<Expr *, 8> Finals;
10851 Expr *Step = Clause.getStep();
10852 Expr *CalcStep = Clause.getCalcStep();
10853 // OpenMP [2.14.3.7, linear clause]
10854 // If linear-step is not specified it is assumed to be 1.
10855 if (Step == nullptr)
10856 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +000010857 else if (CalcStep) {
Alexander Musman3276a272015-03-21 10:12:56 +000010858 Step = cast<BinaryOperator>(CalcStep)->getLHS();
Alexey Bataev5a3af132016-03-29 08:58:54 +000010859 }
Alexander Musman3276a272015-03-21 10:12:56 +000010860 bool HasErrors = false;
10861 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010862 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000010863 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +000010864 for (auto &RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +000010865 SourceLocation ELoc;
10866 SourceRange ERange;
10867 Expr *SimpleRefExpr = RefExpr;
10868 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange,
10869 /*AllowArraySection=*/false);
10870 ValueDecl *D = Res.first;
10871 if (Res.second || !D) {
10872 Updates.push_back(nullptr);
10873 Finals.push_back(nullptr);
10874 HasErrors = true;
10875 continue;
10876 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +000010877 auto &&Info = Stack->isLoopControlVariable(D);
Alexey Bataev2b86f212017-11-29 21:31:48 +000010878 // OpenMP [2.15.11, distribute simd Construct]
10879 // A list item may not appear in a linear clause, unless it is the loop
10880 // iteration variable.
10881 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) &&
10882 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) {
10883 SemaRef.Diag(ELoc,
10884 diag::err_omp_linear_distribute_var_non_loop_iteration);
10885 Updates.push_back(nullptr);
10886 Finals.push_back(nullptr);
10887 HasErrors = true;
10888 continue;
10889 }
Alexander Musman3276a272015-03-21 10:12:56 +000010890 Expr *InitExpr = *CurInit;
10891
10892 // Build privatized reference to the current linear var.
David Majnemer9d168222016-08-05 17:44:54 +000010893 auto *DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000010894 Expr *CapturedRef;
10895 if (LinKind == OMPC_LINEAR_uval)
10896 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
10897 else
10898 CapturedRef =
10899 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
10900 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
10901 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +000010902
10903 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000010904 ExprResult Update;
10905 if (!Info.first) {
10906 Update =
10907 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
10908 InitExpr, IV, Step, /* Subtract */ false);
10909 } else
10910 Update = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +000010911 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
10912 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +000010913
10914 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000010915 ExprResult Final;
10916 if (!Info.first) {
10917 Final = BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
10918 InitExpr, NumIterations, Step,
10919 /* Subtract */ false);
10920 } else
10921 Final = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +000010922 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
10923 /*DiscardedValue=*/true);
Alexey Bataev5dff95c2016-04-22 03:56:56 +000010924
Alexander Musman3276a272015-03-21 10:12:56 +000010925 if (!Update.isUsable() || !Final.isUsable()) {
10926 Updates.push_back(nullptr);
10927 Finals.push_back(nullptr);
10928 HasErrors = true;
10929 } else {
10930 Updates.push_back(Update.get());
10931 Finals.push_back(Final.get());
10932 }
Richard Trieucc3949d2016-02-18 22:34:54 +000010933 ++CurInit;
10934 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +000010935 }
10936 Clause.setUpdates(Updates);
10937 Clause.setFinals(Finals);
10938 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +000010939}
10940
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010941OMPClause *Sema::ActOnOpenMPAlignedClause(
10942 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
10943 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
10944
10945 SmallVector<Expr *, 8> Vars;
10946 for (auto &RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +000010947 assert(RefExpr && "NULL expr in OpenMP linear clause.");
10948 SourceLocation ELoc;
10949 SourceRange ERange;
10950 Expr *SimpleRefExpr = RefExpr;
10951 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
10952 /*AllowArraySection=*/false);
10953 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010954 // It will be analyzed later.
10955 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010956 }
Alexey Bataev1efd1662016-03-29 10:59:56 +000010957 ValueDecl *D = Res.first;
10958 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010959 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010960
Alexey Bataev1efd1662016-03-29 10:59:56 +000010961 QualType QType = D->getType();
10962 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010963
10964 // OpenMP [2.8.1, simd construct, Restrictions]
10965 // The type of list items appearing in the aligned clause must be
10966 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010967 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010968 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +000010969 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010970 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +000010971 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010972 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +000010973 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010974 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +000010975 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010976 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +000010977 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010978 continue;
10979 }
10980
10981 // OpenMP [2.8.1, simd construct, Restrictions]
10982 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev1efd1662016-03-29 10:59:56 +000010983 if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +000010984 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010985 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
10986 << getOpenMPClauseName(OMPC_aligned);
10987 continue;
10988 }
10989
Alexey Bataev1efd1662016-03-29 10:59:56 +000010990 DeclRefExpr *Ref = nullptr;
10991 if (!VD && IsOpenMPCapturedDecl(D))
10992 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
10993 Vars.push_back(DefaultFunctionArrayConversion(
10994 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
10995 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010996 }
10997
10998 // OpenMP [2.8.1, simd construct, Description]
10999 // The parameter of the aligned clause, alignment, must be a constant
11000 // positive integer expression.
11001 // If no optional parameter is specified, implementation-defined default
11002 // alignments for SIMD instructions on the target platforms are assumed.
11003 if (Alignment != nullptr) {
11004 ExprResult AlignResult =
11005 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
11006 if (AlignResult.isInvalid())
11007 return nullptr;
11008 Alignment = AlignResult.get();
11009 }
11010 if (Vars.empty())
11011 return nullptr;
11012
11013 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
11014 EndLoc, Vars, Alignment);
11015}
11016
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011017OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
11018 SourceLocation StartLoc,
11019 SourceLocation LParenLoc,
11020 SourceLocation EndLoc) {
11021 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011022 SmallVector<Expr *, 8> SrcExprs;
11023 SmallVector<Expr *, 8> DstExprs;
11024 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +000011025 for (auto &RefExpr : VarList) {
11026 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
11027 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011028 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000011029 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011030 SrcExprs.push_back(nullptr);
11031 DstExprs.push_back(nullptr);
11032 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011033 continue;
11034 }
11035
Alexey Bataeved09d242014-05-28 05:53:51 +000011036 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011037 // OpenMP [2.1, C/C++]
11038 // A list item is a variable name.
11039 // OpenMP [2.14.4.1, Restrictions, p.1]
11040 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +000011041 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011042 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000011043 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
11044 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011045 continue;
11046 }
11047
11048 Decl *D = DE->getDecl();
11049 VarDecl *VD = cast<VarDecl>(D);
11050
11051 QualType Type = VD->getType();
11052 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
11053 // It will be analyzed later.
11054 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011055 SrcExprs.push_back(nullptr);
11056 DstExprs.push_back(nullptr);
11057 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011058 continue;
11059 }
11060
11061 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
11062 // A list item that appears in a copyin clause must be threadprivate.
11063 if (!DSAStack->isThreadPrivate(VD)) {
11064 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +000011065 << getOpenMPClauseName(OMPC_copyin)
11066 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011067 continue;
11068 }
11069
11070 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
11071 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +000011072 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011073 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011074 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000011075 auto *SrcVD =
11076 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
11077 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +000011078 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011079 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
11080 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000011081 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
11082 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011083 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011084 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011085 // For arrays generate assignment operation for single element and replace
11086 // it by the original array element in CodeGen.
11087 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
11088 PseudoDstExpr, PseudoSrcExpr);
11089 if (AssignmentOp.isInvalid())
11090 continue;
11091 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
11092 /*DiscardedValue=*/true);
11093 if (AssignmentOp.isInvalid())
11094 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011095
11096 DSAStack->addDSA(VD, DE, OMPC_copyin);
11097 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011098 SrcExprs.push_back(PseudoSrcExpr);
11099 DstExprs.push_back(PseudoDstExpr);
11100 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011101 }
11102
Alexey Bataeved09d242014-05-28 05:53:51 +000011103 if (Vars.empty())
11104 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011105
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011106 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
11107 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011108}
11109
Alexey Bataevbae9a792014-06-27 10:37:06 +000011110OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
11111 SourceLocation StartLoc,
11112 SourceLocation LParenLoc,
11113 SourceLocation EndLoc) {
11114 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +000011115 SmallVector<Expr *, 8> SrcExprs;
11116 SmallVector<Expr *, 8> DstExprs;
11117 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +000011118 for (auto &RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +000011119 assert(RefExpr && "NULL expr in OpenMP linear clause.");
11120 SourceLocation ELoc;
11121 SourceRange ERange;
11122 Expr *SimpleRefExpr = RefExpr;
11123 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
11124 /*AllowArraySection=*/false);
11125 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000011126 // It will be analyzed later.
11127 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000011128 SrcExprs.push_back(nullptr);
11129 DstExprs.push_back(nullptr);
11130 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011131 }
Alexey Bataeve122da12016-03-17 10:50:17 +000011132 ValueDecl *D = Res.first;
11133 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +000011134 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000011135
Alexey Bataeve122da12016-03-17 10:50:17 +000011136 QualType Type = D->getType();
11137 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011138
11139 // OpenMP [2.14.4.2, Restrictions, p.2]
11140 // A list item that appears in a copyprivate clause may not appear in a
11141 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +000011142 if (!VD || !DSAStack->isThreadPrivate(VD)) {
11143 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +000011144 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
11145 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000011146 Diag(ELoc, diag::err_omp_wrong_dsa)
11147 << getOpenMPClauseName(DVar.CKind)
11148 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve122da12016-03-17 10:50:17 +000011149 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011150 continue;
11151 }
11152
11153 // OpenMP [2.11.4.2, Restrictions, p.1]
11154 // All list items that appear in a copyprivate clause must be either
11155 // threadprivate or private in the enclosing context.
11156 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +000011157 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011158 if (DVar.CKind == OMPC_shared) {
11159 Diag(ELoc, diag::err_omp_required_access)
11160 << getOpenMPClauseName(OMPC_copyprivate)
11161 << "threadprivate or private in the enclosing context";
Alexey Bataeve122da12016-03-17 10:50:17 +000011162 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011163 continue;
11164 }
11165 }
11166 }
11167
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011168 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000011169 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011170 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +000011171 << getOpenMPClauseName(OMPC_copyprivate) << Type
11172 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011173 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +000011174 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011175 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +000011176 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011177 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +000011178 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011179 continue;
11180 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +000011181
Alexey Bataevbae9a792014-06-27 10:37:06 +000011182 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
11183 // A variable of class type (or array thereof) that appears in a
11184 // copyin clause requires an accessible, unambiguous copy assignment
11185 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011186 Type = Context.getBaseElementType(Type.getNonReferenceType())
11187 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +000011188 auto *SrcVD =
Alexey Bataeve122da12016-03-17 10:50:17 +000011189 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src",
11190 D->hasAttrs() ? &D->getAttrs() : nullptr);
11191 auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
Alexey Bataev420d45b2015-04-14 05:11:24 +000011192 auto *DstVD =
Alexey Bataeve122da12016-03-17 10:50:17 +000011193 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst",
11194 D->hasAttrs() ? &D->getAttrs() : nullptr);
David Majnemer9d168222016-08-05 17:44:54 +000011195 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataeve122da12016-03-17 10:50:17 +000011196 auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
Alexey Bataeva63048e2015-03-23 06:18:07 +000011197 PseudoDstExpr, PseudoSrcExpr);
11198 if (AssignmentOp.isInvalid())
11199 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +000011200 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +000011201 /*DiscardedValue=*/true);
11202 if (AssignmentOp.isInvalid())
11203 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000011204
11205 // No need to mark vars as copyprivate, they are already threadprivate or
11206 // implicitly private.
Alexey Bataeve122da12016-03-17 10:50:17 +000011207 assert(VD || IsOpenMPCapturedDecl(D));
11208 Vars.push_back(
11209 VD ? RefExpr->IgnoreParens()
11210 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +000011211 SrcExprs.push_back(PseudoSrcExpr);
11212 DstExprs.push_back(PseudoDstExpr);
11213 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +000011214 }
11215
11216 if (Vars.empty())
11217 return nullptr;
11218
Alexey Bataeva63048e2015-03-23 06:18:07 +000011219 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11220 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011221}
11222
Alexey Bataev6125da92014-07-21 11:26:11 +000011223OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
11224 SourceLocation StartLoc,
11225 SourceLocation LParenLoc,
11226 SourceLocation EndLoc) {
11227 if (VarList.empty())
11228 return nullptr;
11229
11230 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
11231}
Alexey Bataevdea47612014-07-23 07:46:59 +000011232
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011233OMPClause *
11234Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
11235 SourceLocation DepLoc, SourceLocation ColonLoc,
11236 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
11237 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +000011238 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011239 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +000011240 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000011241 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +000011242 return nullptr;
11243 }
11244 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011245 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
11246 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +000011247 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011248 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000011249 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
11250 /*Last=*/OMPC_DEPEND_unknown, Except)
11251 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011252 return nullptr;
11253 }
11254 SmallVector<Expr *, 8> Vars;
Alexey Bataev8b427062016-05-25 12:36:08 +000011255 DSAStackTy::OperatorOffsetTy OpsOffs;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011256 llvm::APSInt DepCounter(/*BitWidth=*/32);
11257 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
11258 if (DepKind == OMPC_DEPEND_sink) {
11259 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
11260 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
11261 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011262 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011263 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011264 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
11265 DSAStack->getParentOrderedRegionParam()) {
11266 for (auto &RefExpr : VarList) {
11267 assert(RefExpr && "NULL expr in OpenMP shared clause.");
Alexey Bataev8b427062016-05-25 12:36:08 +000011268 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011269 // It will be analyzed later.
11270 Vars.push_back(RefExpr);
11271 continue;
11272 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011273
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011274 SourceLocation ELoc = RefExpr->getExprLoc();
11275 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
11276 if (DepKind == OMPC_DEPEND_sink) {
11277 if (DepCounter >= TotalDepCount) {
11278 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
11279 continue;
11280 }
11281 ++DepCounter;
11282 // OpenMP [2.13.9, Summary]
11283 // depend(dependence-type : vec), where dependence-type is:
11284 // 'sink' and where vec is the iteration vector, which has the form:
11285 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
11286 // where n is the value specified by the ordered clause in the loop
11287 // directive, xi denotes the loop iteration variable of the i-th nested
11288 // loop associated with the loop directive, and di is a constant
11289 // non-negative integer.
Alexey Bataev8b427062016-05-25 12:36:08 +000011290 if (CurContext->isDependentContext()) {
11291 // It will be analyzed later.
11292 Vars.push_back(RefExpr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011293 continue;
11294 }
Alexey Bataev8b427062016-05-25 12:36:08 +000011295 SimpleExpr = SimpleExpr->IgnoreImplicit();
11296 OverloadedOperatorKind OOK = OO_None;
11297 SourceLocation OOLoc;
11298 Expr *LHS = SimpleExpr;
11299 Expr *RHS = nullptr;
11300 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
11301 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
11302 OOLoc = BO->getOperatorLoc();
11303 LHS = BO->getLHS()->IgnoreParenImpCasts();
11304 RHS = BO->getRHS()->IgnoreParenImpCasts();
11305 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
11306 OOK = OCE->getOperator();
11307 OOLoc = OCE->getOperatorLoc();
11308 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
11309 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
11310 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
11311 OOK = MCE->getMethodDecl()
11312 ->getNameInfo()
11313 .getName()
11314 .getCXXOverloadedOperator();
11315 OOLoc = MCE->getCallee()->getExprLoc();
11316 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
11317 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
11318 }
11319 SourceLocation ELoc;
11320 SourceRange ERange;
11321 auto Res = getPrivateItem(*this, LHS, ELoc, ERange,
11322 /*AllowArraySection=*/false);
11323 if (Res.second) {
11324 // It will be analyzed later.
11325 Vars.push_back(RefExpr);
11326 }
11327 ValueDecl *D = Res.first;
11328 if (!D)
11329 continue;
11330
11331 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
11332 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
11333 continue;
11334 }
11335 if (RHS) {
11336 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
11337 RHS, OMPC_depend, /*StrictlyPositive=*/false);
11338 if (RHSRes.isInvalid())
11339 continue;
11340 }
11341 if (!CurContext->isDependentContext() &&
11342 DSAStack->getParentOrderedRegionParam() &&
11343 DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
Rachel Craik1cf49e42017-09-19 21:04:23 +000011344 ValueDecl* VD = DSAStack->getParentLoopControlVariable(
11345 DepCounter.getZExtValue());
11346 if (VD) {
11347 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
11348 << 1 << VD;
11349 } else {
11350 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0;
11351 }
Alexey Bataev8b427062016-05-25 12:36:08 +000011352 continue;
11353 }
11354 OpsOffs.push_back({RHS, OOK});
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011355 } else {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011356 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011357 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
Alexey Bataev31300ed2016-02-04 11:27:03 +000011358 (ASE &&
11359 !ASE->getBase()
11360 ->getType()
11361 .getNonReferenceType()
11362 ->isPointerType() &&
11363 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev463a9fe2017-07-27 19:15:30 +000011364 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
11365 << RefExpr->getSourceRange();
11366 continue;
11367 }
11368 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
11369 getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevd070a582017-10-25 15:54:04 +000011370 ExprResult Res = CreateBuiltinUnaryOp(ELoc, UO_AddrOf,
Alexey Bataev463a9fe2017-07-27 19:15:30 +000011371 RefExpr->IgnoreParenImpCasts());
11372 getDiagnostics().setSuppressAllDiagnostics(Suppress);
11373 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr)) {
11374 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
11375 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011376 continue;
11377 }
11378 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011379 Vars.push_back(RefExpr->IgnoreParenImpCasts());
11380 }
11381
11382 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
11383 TotalDepCount > VarList.size() &&
Rachel Craik1cf49e42017-09-19 21:04:23 +000011384 DSAStack->getParentOrderedRegionParam() &&
11385 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) {
11386 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration) << 1
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011387 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
11388 }
11389 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
11390 Vars.empty())
11391 return nullptr;
11392 }
Alexey Bataev8b427062016-05-25 12:36:08 +000011393 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11394 DepKind, DepLoc, ColonLoc, Vars);
11395 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source)
11396 DSAStack->addDoacrossDependClause(C, OpsOffs);
11397 return C;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011398}
Michael Wonge710d542015-08-07 16:16:36 +000011399
11400OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
11401 SourceLocation LParenLoc,
11402 SourceLocation EndLoc) {
11403 Expr *ValExpr = Device;
Alexey Bataev931e19b2017-10-02 16:32:39 +000011404 Stmt *HelperValStmt = nullptr;
Michael Wonge710d542015-08-07 16:16:36 +000011405
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011406 // OpenMP [2.9.1, Restrictions]
11407 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000011408 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
11409 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011410 return nullptr;
11411
Alexey Bataev931e19b2017-10-02 16:32:39 +000011412 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000011413 OpenMPDirectiveKind CaptureRegion =
11414 getOpenMPCaptureRegionForClause(DKind, OMPC_device);
11415 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000011416 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataev931e19b2017-10-02 16:32:39 +000011417 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
11418 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
11419 HelperValStmt = buildPreInits(Context, Captures);
11420 }
11421
11422 return new (Context)
11423 OMPDeviceClause(ValExpr, HelperValStmt, StartLoc, LParenLoc, EndLoc);
Michael Wonge710d542015-08-07 16:16:36 +000011424}
Kelvin Li0bff7af2015-11-23 05:32:03 +000011425
Kelvin Li0bff7af2015-11-23 05:32:03 +000011426static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
11427 DSAStackTy *Stack, QualType QTy) {
11428 NamedDecl *ND;
11429 if (QTy->isIncompleteType(&ND)) {
11430 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
11431 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +000011432 }
11433 return true;
11434}
11435
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011436/// \brief Return true if it can be proven that the provided array expression
11437/// (array section or array subscript) does NOT specify the whole size of the
11438/// array whose base type is \a BaseQTy.
11439static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
11440 const Expr *E,
11441 QualType BaseQTy) {
11442 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
11443
11444 // If this is an array subscript, it refers to the whole size if the size of
11445 // the dimension is constant and equals 1. Also, an array section assumes the
11446 // format of an array subscript if no colon is used.
11447 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
11448 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
11449 return ATy->getSize().getSExtValue() != 1;
11450 // Size can't be evaluated statically.
11451 return false;
11452 }
11453
11454 assert(OASE && "Expecting array section if not an array subscript.");
11455 auto *LowerBound = OASE->getLowerBound();
11456 auto *Length = OASE->getLength();
11457
11458 // If there is a lower bound that does not evaluates to zero, we are not
David Majnemer9d168222016-08-05 17:44:54 +000011459 // covering the whole dimension.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011460 if (LowerBound) {
11461 llvm::APSInt ConstLowerBound;
11462 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
11463 return false; // Can't get the integer value as a constant.
11464 if (ConstLowerBound.getSExtValue())
11465 return true;
11466 }
11467
11468 // If we don't have a length we covering the whole dimension.
11469 if (!Length)
11470 return false;
11471
11472 // If the base is a pointer, we don't have a way to get the size of the
11473 // pointee.
11474 if (BaseQTy->isPointerType())
11475 return false;
11476
11477 // We can only check if the length is the same as the size of the dimension
11478 // if we have a constant array.
11479 auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
11480 if (!CATy)
11481 return false;
11482
11483 llvm::APSInt ConstLength;
11484 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
11485 return false; // Can't get the integer value as a constant.
11486
11487 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
11488}
11489
11490// Return true if it can be proven that the provided array expression (array
11491// section or array subscript) does NOT specify a single element of the array
11492// whose base type is \a BaseQTy.
11493static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
David Majnemer9d168222016-08-05 17:44:54 +000011494 const Expr *E,
11495 QualType BaseQTy) {
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011496 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
11497
11498 // An array subscript always refer to a single element. Also, an array section
11499 // assumes the format of an array subscript if no colon is used.
11500 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
11501 return false;
11502
11503 assert(OASE && "Expecting array section if not an array subscript.");
11504 auto *Length = OASE->getLength();
11505
11506 // If we don't have a length we have to check if the array has unitary size
11507 // for this dimension. Also, we should always expect a length if the base type
11508 // is pointer.
11509 if (!Length) {
11510 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
11511 return ATy->getSize().getSExtValue() != 1;
11512 // We cannot assume anything.
11513 return false;
11514 }
11515
11516 // Check if the length evaluates to 1.
11517 llvm::APSInt ConstLength;
11518 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
11519 return false; // Can't get the integer value as a constant.
11520
11521 return ConstLength.getSExtValue() != 1;
11522}
11523
Samuel Antao661c0902016-05-26 17:39:58 +000011524// Return the expression of the base of the mappable expression or null if it
11525// cannot be determined and do all the necessary checks to see if the expression
11526// is valid as a standalone mappable expression. In the process, record all the
Samuel Antao90927002016-04-26 14:54:23 +000011527// components of the expression.
11528static Expr *CheckMapClauseExpressionBase(
11529 Sema &SemaRef, Expr *E,
Samuel Antao661c0902016-05-26 17:39:58 +000011530 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
Alexey Bataevb7a9b742017-12-05 19:20:09 +000011531 OpenMPClauseKind CKind, bool NoDiagnose) {
Samuel Antao5de996e2016-01-22 20:21:36 +000011532 SourceLocation ELoc = E->getExprLoc();
11533 SourceRange ERange = E->getSourceRange();
11534
11535 // The base of elements of list in a map clause have to be either:
11536 // - a reference to variable or field.
11537 // - a member expression.
11538 // - an array expression.
11539 //
11540 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
11541 // reference to 'r'.
11542 //
11543 // If we have:
11544 //
11545 // struct SS {
11546 // Bla S;
11547 // foo() {
11548 // #pragma omp target map (S.Arr[:12]);
11549 // }
11550 // }
11551 //
11552 // We want to retrieve the member expression 'this->S';
11553
11554 Expr *RelevantExpr = nullptr;
11555
Samuel Antao5de996e2016-01-22 20:21:36 +000011556 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
11557 // If a list item is an array section, it must specify contiguous storage.
11558 //
11559 // For this restriction it is sufficient that we make sure only references
11560 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011561 // exist except in the rightmost expression (unless they cover the whole
11562 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +000011563 //
11564 // r.ArrS[3:5].Arr[6:7]
11565 //
11566 // r.ArrS[3:5].x
11567 //
11568 // but these would be valid:
11569 // r.ArrS[3].Arr[6:7]
11570 //
11571 // r.ArrS[3].x
11572
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011573 bool AllowUnitySizeArraySection = true;
11574 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +000011575
Dmitry Polukhin644a9252016-03-11 07:58:34 +000011576 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +000011577 E = E->IgnoreParenImpCasts();
11578
11579 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
11580 if (!isa<VarDecl>(CurE->getDecl()))
Alexey Bataev27041fa2017-12-05 15:22:49 +000011581 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000011582
11583 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011584
11585 // If we got a reference to a declaration, we should not expect any array
11586 // section before that.
11587 AllowUnitySizeArraySection = false;
11588 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000011589
11590 // Record the component.
Alexey Bataev27041fa2017-12-05 15:22:49 +000011591 CurComponents.emplace_back(CurE, CurE->getDecl());
11592 } else if (auto *CurE = dyn_cast<MemberExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +000011593 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
11594
11595 if (isa<CXXThisExpr>(BaseE))
11596 // We found a base expression: this->Val.
11597 RelevantExpr = CurE;
11598 else
11599 E = BaseE;
11600
11601 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000011602 if (!NoDiagnose) {
11603 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
11604 << CurE->getSourceRange();
11605 return nullptr;
11606 }
11607 if (RelevantExpr)
11608 return nullptr;
11609 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000011610 }
11611
11612 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
11613
11614 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
11615 // A bit-field cannot appear in a map clause.
11616 //
11617 if (FD->isBitField()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000011618 if (!NoDiagnose) {
11619 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
11620 << CurE->getSourceRange() << getOpenMPClauseName(CKind);
11621 return nullptr;
11622 }
11623 if (RelevantExpr)
11624 return nullptr;
11625 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000011626 }
11627
11628 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
11629 // If the type of a list item is a reference to a type T then the type
11630 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011631 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000011632
11633 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
11634 // A list item cannot be a variable that is a member of a structure with
11635 // a union type.
11636 //
Alexey Bataevb7a9b742017-12-05 19:20:09 +000011637 if (auto *RT = CurType->getAs<RecordType>()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000011638 if (RT->isUnionType()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000011639 if (!NoDiagnose) {
11640 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
11641 << CurE->getSourceRange();
11642 return nullptr;
11643 }
11644 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000011645 }
Alexey Bataevb7a9b742017-12-05 19:20:09 +000011646 }
Samuel Antao5de996e2016-01-22 20:21:36 +000011647
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011648 // If we got a member expression, we should not expect any array section
11649 // before that:
11650 //
11651 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
11652 // If a list item is an element of a structure, only the rightmost symbol
11653 // of the variable reference can be an array section.
11654 //
11655 AllowUnitySizeArraySection = false;
11656 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000011657
11658 // Record the component.
Alexey Bataev27041fa2017-12-05 15:22:49 +000011659 CurComponents.emplace_back(CurE, FD);
11660 } else if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +000011661 E = CurE->getBase()->IgnoreParenImpCasts();
11662
11663 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000011664 if (!NoDiagnose) {
11665 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
11666 << 0 << CurE->getSourceRange();
11667 return nullptr;
11668 }
11669 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000011670 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011671
11672 // If we got an array subscript that express the whole dimension we
11673 // can have any array expressions before. If it only expressing part of
11674 // the dimension, we can only have unitary-size array expressions.
11675 if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
11676 E->getType()))
11677 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000011678
11679 // Record the component - we don't have any declaration associated.
Alexey Bataev27041fa2017-12-05 15:22:49 +000011680 CurComponents.emplace_back(CurE, nullptr);
11681 } else if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000011682 assert(!NoDiagnose && "Array sections cannot be implicitly mapped.");
Samuel Antao5de996e2016-01-22 20:21:36 +000011683 E = CurE->getBase()->IgnoreParenImpCasts();
11684
Alexey Bataev27041fa2017-12-05 15:22:49 +000011685 QualType CurType =
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011686 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
11687
Samuel Antao5de996e2016-01-22 20:21:36 +000011688 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
11689 // If the type of a list item is a reference to a type T then the type
11690 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +000011691 if (CurType->isReferenceType())
11692 CurType = CurType->getPointeeType();
11693
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011694 bool IsPointer = CurType->isAnyPointerType();
11695
11696 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000011697 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
11698 << 0 << CurE->getSourceRange();
Alexey Bataev27041fa2017-12-05 15:22:49 +000011699 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000011700 }
11701
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011702 bool NotWhole =
11703 CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
11704 bool NotUnity =
11705 CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
11706
Samuel Antaodab51bb2016-07-18 23:22:11 +000011707 if (AllowWholeSizeArraySection) {
11708 // Any array section is currently allowed. Allowing a whole size array
11709 // section implies allowing a unity array section as well.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011710 //
11711 // If this array section refers to the whole dimension we can still
11712 // accept other array sections before this one, except if the base is a
11713 // pointer. Otherwise, only unitary sections are accepted.
11714 if (NotWhole || IsPointer)
11715 AllowWholeSizeArraySection = false;
Samuel Antaodab51bb2016-07-18 23:22:11 +000011716 } else if (AllowUnitySizeArraySection && NotUnity) {
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011717 // A unity or whole array section is not allowed and that is not
11718 // compatible with the properties of the current array section.
11719 SemaRef.Diag(
11720 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
11721 << CurE->getSourceRange();
Alexey Bataev27041fa2017-12-05 15:22:49 +000011722 return nullptr;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011723 }
Samuel Antao90927002016-04-26 14:54:23 +000011724
11725 // Record the component - we don't have any declaration associated.
Alexey Bataev27041fa2017-12-05 15:22:49 +000011726 CurComponents.emplace_back(CurE, nullptr);
11727 } else {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000011728 if (!NoDiagnose) {
11729 // If nothing else worked, this is not a valid map clause expression.
11730 SemaRef.Diag(
11731 ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
11732 << ERange;
11733 }
Alexey Bataev27041fa2017-12-05 15:22:49 +000011734 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000011735 }
Samuel Antao5de996e2016-01-22 20:21:36 +000011736 }
11737
11738 return RelevantExpr;
11739}
11740
11741// Return true if expression E associated with value VD has conflicts with other
11742// map information.
Samuel Antao90927002016-04-26 14:54:23 +000011743static bool CheckMapConflicts(
11744 Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD, Expr *E,
11745 bool CurrentRegionOnly,
Samuel Antao661c0902016-05-26 17:39:58 +000011746 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
11747 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000011748 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +000011749 SourceLocation ELoc = E->getExprLoc();
11750 SourceRange ERange = E->getSourceRange();
11751
11752 // In order to easily check the conflicts we need to match each component of
11753 // the expression under test with the components of the expressions that are
11754 // already in the stack.
11755
Samuel Antao5de996e2016-01-22 20:21:36 +000011756 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000011757 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000011758 "Map clause expression with unexpected base!");
11759
11760 // Variables to help detecting enclosing problems in data environment nests.
11761 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +000011762 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000011763
Samuel Antao90927002016-04-26 14:54:23 +000011764 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
11765 VD, CurrentRegionOnly,
11766 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
Samuel Antao6890b092016-07-28 14:25:09 +000011767 StackComponents,
11768 OpenMPClauseKind) -> bool {
Samuel Antao90927002016-04-26 14:54:23 +000011769
Samuel Antao5de996e2016-01-22 20:21:36 +000011770 assert(!StackComponents.empty() &&
11771 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000011772 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000011773 "Map clause expression with unexpected base!");
11774
Samuel Antao90927002016-04-26 14:54:23 +000011775 // The whole expression in the stack.
11776 auto *RE = StackComponents.front().getAssociatedExpression();
11777
Samuel Antao5de996e2016-01-22 20:21:36 +000011778 // Expressions must start from the same base. Here we detect at which
11779 // point both expressions diverge from each other and see if we can
11780 // detect if the memory referred to both expressions is contiguous and
11781 // do not overlap.
11782 auto CI = CurComponents.rbegin();
11783 auto CE = CurComponents.rend();
11784 auto SI = StackComponents.rbegin();
11785 auto SE = StackComponents.rend();
11786 for (; CI != CE && SI != SE; ++CI, ++SI) {
11787
11788 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
11789 // At most one list item can be an array item derived from a given
11790 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +000011791 if (CurrentRegionOnly &&
11792 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
11793 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
11794 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
11795 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
11796 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +000011797 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +000011798 << CI->getAssociatedExpression()->getSourceRange();
11799 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
11800 diag::note_used_here)
11801 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +000011802 return true;
11803 }
11804
11805 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +000011806 if (CI->getAssociatedExpression()->getStmtClass() !=
11807 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +000011808 break;
11809
11810 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +000011811 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +000011812 break;
11813 }
Kelvin Li9f645ae2016-07-18 22:49:16 +000011814 // Check if the extra components of the expressions in the enclosing
11815 // data environment are redundant for the current base declaration.
11816 // If they are, the maps completely overlap, which is legal.
11817 for (; SI != SE; ++SI) {
11818 QualType Type;
11819 if (auto *ASE =
David Majnemer9d168222016-08-05 17:44:54 +000011820 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
Kelvin Li9f645ae2016-07-18 22:49:16 +000011821 Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
David Majnemer9d168222016-08-05 17:44:54 +000011822 } else if (auto *OASE = dyn_cast<OMPArraySectionExpr>(
11823 SI->getAssociatedExpression())) {
Kelvin Li9f645ae2016-07-18 22:49:16 +000011824 auto *E = OASE->getBase()->IgnoreParenImpCasts();
11825 Type =
11826 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
11827 }
11828 if (Type.isNull() || Type->isAnyPointerType() ||
11829 CheckArrayExpressionDoesNotReferToWholeSize(
11830 SemaRef, SI->getAssociatedExpression(), Type))
11831 break;
11832 }
Samuel Antao5de996e2016-01-22 20:21:36 +000011833
11834 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
11835 // List items of map clauses in the same construct must not share
11836 // original storage.
11837 //
11838 // If the expressions are exactly the same or one is a subset of the
11839 // other, it means they are sharing storage.
11840 if (CI == CE && SI == SE) {
11841 if (CurrentRegionOnly) {
Samuel Antao661c0902016-05-26 17:39:58 +000011842 if (CKind == OMPC_map)
11843 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
11844 else {
Samuel Antaoec172c62016-05-26 17:49:04 +000011845 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000011846 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
11847 << ERange;
11848 }
Samuel Antao5de996e2016-01-22 20:21:36 +000011849 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
11850 << RE->getSourceRange();
11851 return true;
11852 } else {
11853 // If we find the same expression in the enclosing data environment,
11854 // that is legal.
11855 IsEnclosedByDataEnvironmentExpr = true;
11856 return false;
11857 }
11858 }
11859
Samuel Antao90927002016-04-26 14:54:23 +000011860 QualType DerivedType =
11861 std::prev(CI)->getAssociatedDeclaration()->getType();
11862 SourceLocation DerivedLoc =
11863 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000011864
11865 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
11866 // If the type of a list item is a reference to a type T then the type
11867 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000011868 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000011869
11870 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
11871 // A variable for which the type is pointer and an array section
11872 // derived from that variable must not appear as list items of map
11873 // clauses of the same construct.
11874 //
11875 // Also, cover one of the cases in:
11876 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
11877 // If any part of the original storage of a list item has corresponding
11878 // storage in the device data environment, all of the original storage
11879 // must have corresponding storage in the device data environment.
11880 //
11881 if (DerivedType->isAnyPointerType()) {
11882 if (CI == CE || SI == SE) {
11883 SemaRef.Diag(
11884 DerivedLoc,
11885 diag::err_omp_pointer_mapped_along_with_derived_section)
11886 << DerivedLoc;
11887 } else {
11888 assert(CI != CE && SI != SE);
11889 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
11890 << DerivedLoc;
11891 }
11892 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
11893 << RE->getSourceRange();
11894 return true;
11895 }
11896
11897 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
11898 // List items of map clauses in the same construct must not share
11899 // original storage.
11900 //
11901 // An expression is a subset of the other.
11902 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
Samuel Antao661c0902016-05-26 17:39:58 +000011903 if (CKind == OMPC_map)
11904 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
11905 else {
Samuel Antaoec172c62016-05-26 17:49:04 +000011906 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000011907 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
11908 << ERange;
11909 }
Samuel Antao5de996e2016-01-22 20:21:36 +000011910 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
11911 << RE->getSourceRange();
11912 return true;
11913 }
11914
11915 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000011916 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000011917 if (!CurrentRegionOnly && SI != SE)
11918 EnclosingExpr = RE;
11919
11920 // The current expression is a subset of the expression in the data
11921 // environment.
11922 IsEnclosedByDataEnvironmentExpr |=
11923 (!CurrentRegionOnly && CI != CE && SI == SE);
11924
11925 return false;
11926 });
11927
11928 if (CurrentRegionOnly)
11929 return FoundError;
11930
11931 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
11932 // If any part of the original storage of a list item has corresponding
11933 // storage in the device data environment, all of the original storage must
11934 // have corresponding storage in the device data environment.
11935 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
11936 // If a list item is an element of a structure, and a different element of
11937 // the structure has a corresponding list item in the device data environment
11938 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000011939 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000011940 // data environment prior to the task encountering the construct.
11941 //
11942 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
11943 SemaRef.Diag(ELoc,
11944 diag::err_omp_original_storage_is_shared_and_does_not_contain)
11945 << ERange;
11946 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
11947 << EnclosingExpr->getSourceRange();
11948 return true;
11949 }
11950
11951 return FoundError;
11952}
11953
Samuel Antao661c0902016-05-26 17:39:58 +000011954namespace {
11955// Utility struct that gathers all the related lists associated with a mappable
11956// expression.
11957struct MappableVarListInfo final {
11958 // The list of expressions.
11959 ArrayRef<Expr *> VarList;
11960 // The list of processed expressions.
11961 SmallVector<Expr *, 16> ProcessedVarList;
11962 // The mappble components for each expression.
11963 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
11964 // The base declaration of the variable.
11965 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
11966
11967 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
11968 // We have a list of components and base declarations for each entry in the
11969 // variable list.
11970 VarComponents.reserve(VarList.size());
11971 VarBaseDeclarations.reserve(VarList.size());
11972 }
11973};
11974}
11975
11976// Check the validity of the provided variable list for the provided clause kind
11977// \a CKind. In the check process the valid expressions, and mappable expression
11978// components and variables are extracted and used to fill \a Vars,
11979// \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and
11980// \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'.
11981static void
11982checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS,
11983 OpenMPClauseKind CKind, MappableVarListInfo &MVLI,
11984 SourceLocation StartLoc,
11985 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
11986 bool IsMapTypeImplicit = false) {
Samuel Antaoec172c62016-05-26 17:49:04 +000011987 // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
11988 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
Samuel Antao661c0902016-05-26 17:39:58 +000011989 "Unexpected clause kind with mappable expressions!");
Kelvin Li0bff7af2015-11-23 05:32:03 +000011990
Samuel Antao90927002016-04-26 14:54:23 +000011991 // Keep track of the mappable components and base declarations in this clause.
11992 // Each entry in the list is going to have a list of components associated. We
11993 // record each set of the components so that we can build the clause later on.
11994 // In the end we should have the same amount of declarations and component
11995 // lists.
Samuel Antao90927002016-04-26 14:54:23 +000011996
Samuel Antao661c0902016-05-26 17:39:58 +000011997 for (auto &RE : MVLI.VarList) {
Samuel Antaoec172c62016-05-26 17:49:04 +000011998 assert(RE && "Null expr in omp to/from/map clause");
Kelvin Li0bff7af2015-11-23 05:32:03 +000011999 SourceLocation ELoc = RE->getExprLoc();
12000
Kelvin Li0bff7af2015-11-23 05:32:03 +000012001 auto *VE = RE->IgnoreParenLValueCasts();
12002
12003 if (VE->isValueDependent() || VE->isTypeDependent() ||
12004 VE->isInstantiationDependent() ||
12005 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012006 // We can only analyze this information once the missing information is
12007 // resolved.
Samuel Antao661c0902016-05-26 17:39:58 +000012008 MVLI.ProcessedVarList.push_back(RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012009 continue;
12010 }
12011
12012 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012013
Samuel Antao5de996e2016-01-22 20:21:36 +000012014 if (!RE->IgnoreParenImpCasts()->isLValue()) {
Samuel Antao661c0902016-05-26 17:39:58 +000012015 SemaRef.Diag(ELoc,
12016 diag::err_omp_expected_named_var_member_or_array_expression)
Samuel Antao5de996e2016-01-22 20:21:36 +000012017 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012018 continue;
12019 }
12020
Samuel Antao90927002016-04-26 14:54:23 +000012021 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
12022 ValueDecl *CurDeclaration = nullptr;
12023
12024 // Obtain the array or member expression bases if required. Also, fill the
12025 // components array with all the components identified in the process.
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012026 auto *BE = CheckMapClauseExpressionBase(SemaRef, SimpleExpr, CurComponents,
12027 CKind, /*NoDiagnose=*/false);
Samuel Antao5de996e2016-01-22 20:21:36 +000012028 if (!BE)
12029 continue;
12030
Samuel Antao90927002016-04-26 14:54:23 +000012031 assert(!CurComponents.empty() &&
12032 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000012033
Samuel Antao90927002016-04-26 14:54:23 +000012034 // For the following checks, we rely on the base declaration which is
12035 // expected to be associated with the last component. The declaration is
12036 // expected to be a variable or a field (if 'this' is being mapped).
12037 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
12038 assert(CurDeclaration && "Null decl on map clause.");
12039 assert(
12040 CurDeclaration->isCanonicalDecl() &&
12041 "Expecting components to have associated only canonical declarations.");
12042
12043 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
12044 auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000012045
12046 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000012047 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000012048
12049 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
Samuel Antao661c0902016-05-26 17:39:58 +000012050 // threadprivate variables cannot appear in a map clause.
12051 // OpenMP 4.5 [2.10.5, target update Construct]
12052 // threadprivate variables cannot appear in a from clause.
12053 if (VD && DSAS->isThreadPrivate(VD)) {
12054 auto DVar = DSAS->getTopDSA(VD, false);
12055 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
12056 << getOpenMPClauseName(CKind);
12057 ReportOriginalDSA(SemaRef, DSAS, VD, DVar);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012058 continue;
12059 }
12060
Samuel Antao5de996e2016-01-22 20:21:36 +000012061 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
12062 // A list item cannot appear in both a map clause and a data-sharing
12063 // attribute clause on the same construct.
Kelvin Li0bff7af2015-11-23 05:32:03 +000012064
Samuel Antao5de996e2016-01-22 20:21:36 +000012065 // Check conflicts with other map clause expressions. We check the conflicts
12066 // with the current construct separately from the enclosing data
Samuel Antao661c0902016-05-26 17:39:58 +000012067 // environment, because the restrictions are different. We only have to
12068 // check conflicts across regions for the map clauses.
12069 if (CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
12070 /*CurrentRegionOnly=*/true, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000012071 break;
Samuel Antao661c0902016-05-26 17:39:58 +000012072 if (CKind == OMPC_map &&
12073 CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
12074 /*CurrentRegionOnly=*/false, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000012075 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000012076
Samuel Antao661c0902016-05-26 17:39:58 +000012077 // OpenMP 4.5 [2.10.5, target update Construct]
Samuel Antao5de996e2016-01-22 20:21:36 +000012078 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12079 // If the type of a list item is a reference to a type T then the type will
12080 // be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000012081 QualType Type = CurDeclaration->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000012082
Samuel Antao661c0902016-05-26 17:39:58 +000012083 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
12084 // A list item in a to or from clause must have a mappable type.
Samuel Antao5de996e2016-01-22 20:21:36 +000012085 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000012086 // A list item must have a mappable type.
Samuel Antao661c0902016-05-26 17:39:58 +000012087 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
12088 DSAS, Type))
Kelvin Li0bff7af2015-11-23 05:32:03 +000012089 continue;
12090
Samuel Antao661c0902016-05-26 17:39:58 +000012091 if (CKind == OMPC_map) {
12092 // target enter data
12093 // OpenMP [2.10.2, Restrictions, p. 99]
12094 // A map-type must be specified in all map clauses and must be either
12095 // to or alloc.
12096 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
12097 if (DKind == OMPD_target_enter_data &&
12098 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
12099 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
12100 << (IsMapTypeImplicit ? 1 : 0)
12101 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
12102 << getOpenMPDirectiveName(DKind);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000012103 continue;
12104 }
Samuel Antao661c0902016-05-26 17:39:58 +000012105
12106 // target exit_data
12107 // OpenMP [2.10.3, Restrictions, p. 102]
12108 // A map-type must be specified in all map clauses and must be either
12109 // from, release, or delete.
12110 if (DKind == OMPD_target_exit_data &&
12111 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
12112 MapType == OMPC_MAP_delete)) {
12113 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
12114 << (IsMapTypeImplicit ? 1 : 0)
12115 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
12116 << getOpenMPDirectiveName(DKind);
12117 continue;
12118 }
12119
12120 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
12121 // A list item cannot appear in both a map clause and a data-sharing
12122 // attribute clause on the same construct
Kelvin Li83c451e2016-12-25 04:52:54 +000012123 if ((DKind == OMPD_target || DKind == OMPD_target_teams ||
Kelvin Li80e8f562016-12-29 22:16:30 +000012124 DKind == OMPD_target_teams_distribute ||
Kelvin Li1851df52017-01-03 05:23:48 +000012125 DKind == OMPD_target_teams_distribute_parallel_for ||
Kelvin Lida681182017-01-10 18:08:18 +000012126 DKind == OMPD_target_teams_distribute_parallel_for_simd ||
12127 DKind == OMPD_target_teams_distribute_simd) && VD) {
Samuel Antao661c0902016-05-26 17:39:58 +000012128 auto DVar = DSAS->getTopDSA(VD, false);
12129 if (isOpenMPPrivate(DVar.CKind)) {
Samuel Antao6890b092016-07-28 14:25:09 +000012130 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Samuel Antao661c0902016-05-26 17:39:58 +000012131 << getOpenMPClauseName(DVar.CKind)
Samuel Antao6890b092016-07-28 14:25:09 +000012132 << getOpenMPClauseName(OMPC_map)
Samuel Antao661c0902016-05-26 17:39:58 +000012133 << getOpenMPDirectiveName(DSAS->getCurrentDirective());
12134 ReportOriginalDSA(SemaRef, DSAS, CurDeclaration, DVar);
12135 continue;
12136 }
12137 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +000012138 }
12139
Samuel Antao90927002016-04-26 14:54:23 +000012140 // Save the current expression.
Samuel Antao661c0902016-05-26 17:39:58 +000012141 MVLI.ProcessedVarList.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000012142
12143 // Store the components in the stack so that they can be used to check
12144 // against other clauses later on.
Samuel Antao6890b092016-07-28 14:25:09 +000012145 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
12146 /*WhereFoundClauseKind=*/OMPC_map);
Samuel Antao90927002016-04-26 14:54:23 +000012147
12148 // Save the components and declaration to create the clause. For purposes of
12149 // the clause creation, any component list that has has base 'this' uses
Samuel Antao686c70c2016-05-26 17:30:50 +000012150 // null as base declaration.
Samuel Antao661c0902016-05-26 17:39:58 +000012151 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
12152 MVLI.VarComponents.back().append(CurComponents.begin(),
12153 CurComponents.end());
12154 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
12155 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012156 }
Samuel Antao661c0902016-05-26 17:39:58 +000012157}
12158
12159OMPClause *
12160Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
12161 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
12162 SourceLocation MapLoc, SourceLocation ColonLoc,
12163 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
12164 SourceLocation LParenLoc, SourceLocation EndLoc) {
12165 MappableVarListInfo MVLI(VarList);
12166 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc,
12167 MapType, IsMapTypeImplicit);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012168
Samuel Antao5de996e2016-01-22 20:21:36 +000012169 // We need to produce a map clause even if we don't have variables so that
12170 // other diagnostics related with non-existing map clauses are accurate.
Samuel Antao661c0902016-05-26 17:39:58 +000012171 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc,
12172 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
12173 MVLI.VarComponents, MapTypeModifier, MapType,
12174 IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012175}
Kelvin Li099bb8c2015-11-24 20:50:12 +000012176
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012177QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
12178 TypeResult ParsedType) {
12179 assert(ParsedType.isUsable());
12180
12181 QualType ReductionType = GetTypeFromParser(ParsedType.get());
12182 if (ReductionType.isNull())
12183 return QualType();
12184
12185 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
12186 // A type name in a declare reduction directive cannot be a function type, an
12187 // array type, a reference type, or a type qualified with const, volatile or
12188 // restrict.
12189 if (ReductionType.hasQualifiers()) {
12190 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
12191 return QualType();
12192 }
12193
12194 if (ReductionType->isFunctionType()) {
12195 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
12196 return QualType();
12197 }
12198 if (ReductionType->isReferenceType()) {
12199 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
12200 return QualType();
12201 }
12202 if (ReductionType->isArrayType()) {
12203 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
12204 return QualType();
12205 }
12206 return ReductionType;
12207}
12208
12209Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
12210 Scope *S, DeclContext *DC, DeclarationName Name,
12211 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
12212 AccessSpecifier AS, Decl *PrevDeclInScope) {
12213 SmallVector<Decl *, 8> Decls;
12214 Decls.reserve(ReductionTypes.size());
12215
12216 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
Richard Smithbecb92d2017-10-10 22:33:17 +000012217 forRedeclarationInCurContext());
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012218 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
12219 // A reduction-identifier may not be re-declared in the current scope for the
12220 // same type or for a type that is compatible according to the base language
12221 // rules.
12222 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
12223 OMPDeclareReductionDecl *PrevDRD = nullptr;
12224 bool InCompoundScope = true;
12225 if (S != nullptr) {
12226 // Find previous declaration with the same name not referenced in other
12227 // declarations.
12228 FunctionScopeInfo *ParentFn = getEnclosingFunction();
12229 InCompoundScope =
12230 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
12231 LookupName(Lookup, S);
12232 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
12233 /*AllowInlineNamespace=*/false);
12234 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
12235 auto Filter = Lookup.makeFilter();
12236 while (Filter.hasNext()) {
12237 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
12238 if (InCompoundScope) {
12239 auto I = UsedAsPrevious.find(PrevDecl);
12240 if (I == UsedAsPrevious.end())
12241 UsedAsPrevious[PrevDecl] = false;
12242 if (auto *D = PrevDecl->getPrevDeclInScope())
12243 UsedAsPrevious[D] = true;
12244 }
12245 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
12246 PrevDecl->getLocation();
12247 }
12248 Filter.done();
12249 if (InCompoundScope) {
12250 for (auto &PrevData : UsedAsPrevious) {
12251 if (!PrevData.second) {
12252 PrevDRD = PrevData.first;
12253 break;
12254 }
12255 }
12256 }
12257 } else if (PrevDeclInScope != nullptr) {
12258 auto *PrevDRDInScope = PrevDRD =
12259 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
12260 do {
12261 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
12262 PrevDRDInScope->getLocation();
12263 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
12264 } while (PrevDRDInScope != nullptr);
12265 }
12266 for (auto &TyData : ReductionTypes) {
12267 auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
12268 bool Invalid = false;
12269 if (I != PreviousRedeclTypes.end()) {
12270 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
12271 << TyData.first;
12272 Diag(I->second, diag::note_previous_definition);
12273 Invalid = true;
12274 }
12275 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
12276 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
12277 Name, TyData.first, PrevDRD);
12278 DC->addDecl(DRD);
12279 DRD->setAccess(AS);
12280 Decls.push_back(DRD);
12281 if (Invalid)
12282 DRD->setInvalidDecl();
12283 else
12284 PrevDRD = DRD;
12285 }
12286
12287 return DeclGroupPtrTy::make(
12288 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
12289}
12290
12291void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
12292 auto *DRD = cast<OMPDeclareReductionDecl>(D);
12293
12294 // Enter new function scope.
12295 PushFunctionScope();
12296 getCurFunction()->setHasBranchProtectedScope();
12297 getCurFunction()->setHasOMPDeclareReductionCombiner();
12298
12299 if (S != nullptr)
12300 PushDeclContext(S, DRD);
12301 else
12302 CurContext = DRD;
12303
Faisal Valid143a0c2017-04-01 21:30:49 +000012304 PushExpressionEvaluationContext(
12305 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012306
12307 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012308 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
12309 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
12310 // uses semantics of argument handles by value, but it should be passed by
12311 // reference. C lang does not support references, so pass all parameters as
12312 // pointers.
12313 // Create 'T omp_in;' variable.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012314 auto *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012315 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012316 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
12317 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
12318 // uses semantics of argument handles by value, but it should be passed by
12319 // reference. C lang does not support references, so pass all parameters as
12320 // pointers.
12321 // Create 'T omp_out;' variable.
12322 auto *OmpOutParm =
12323 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
12324 if (S != nullptr) {
12325 PushOnScopeChains(OmpInParm, S);
12326 PushOnScopeChains(OmpOutParm, S);
12327 } else {
12328 DRD->addDecl(OmpInParm);
12329 DRD->addDecl(OmpOutParm);
12330 }
12331}
12332
12333void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
12334 auto *DRD = cast<OMPDeclareReductionDecl>(D);
12335 DiscardCleanupsInEvaluationContext();
12336 PopExpressionEvaluationContext();
12337
12338 PopDeclContext();
12339 PopFunctionScopeInfo();
12340
12341 if (Combiner != nullptr)
12342 DRD->setCombiner(Combiner);
12343 else
12344 DRD->setInvalidDecl();
12345}
12346
Alexey Bataev070f43a2017-09-06 14:49:58 +000012347VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012348 auto *DRD = cast<OMPDeclareReductionDecl>(D);
12349
12350 // Enter new function scope.
12351 PushFunctionScope();
12352 getCurFunction()->setHasBranchProtectedScope();
12353
12354 if (S != nullptr)
12355 PushDeclContext(S, DRD);
12356 else
12357 CurContext = DRD;
12358
Faisal Valid143a0c2017-04-01 21:30:49 +000012359 PushExpressionEvaluationContext(
12360 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012361
12362 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012363 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
12364 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
12365 // uses semantics of argument handles by value, but it should be passed by
12366 // reference. C lang does not support references, so pass all parameters as
12367 // pointers.
12368 // Create 'T omp_priv;' variable.
12369 auto *OmpPrivParm =
12370 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012371 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
12372 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
12373 // uses semantics of argument handles by value, but it should be passed by
12374 // reference. C lang does not support references, so pass all parameters as
12375 // pointers.
12376 // Create 'T omp_orig;' variable.
12377 auto *OmpOrigParm =
12378 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012379 if (S != nullptr) {
12380 PushOnScopeChains(OmpPrivParm, S);
12381 PushOnScopeChains(OmpOrigParm, S);
12382 } else {
12383 DRD->addDecl(OmpPrivParm);
12384 DRD->addDecl(OmpOrigParm);
12385 }
Alexey Bataev070f43a2017-09-06 14:49:58 +000012386 return OmpPrivParm;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012387}
12388
Alexey Bataev070f43a2017-09-06 14:49:58 +000012389void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer,
12390 VarDecl *OmpPrivParm) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012391 auto *DRD = cast<OMPDeclareReductionDecl>(D);
12392 DiscardCleanupsInEvaluationContext();
12393 PopExpressionEvaluationContext();
12394
12395 PopDeclContext();
12396 PopFunctionScopeInfo();
12397
Alexey Bataev070f43a2017-09-06 14:49:58 +000012398 if (Initializer != nullptr) {
12399 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit);
12400 } else if (OmpPrivParm->hasInit()) {
12401 DRD->setInitializer(OmpPrivParm->getInit(),
12402 OmpPrivParm->isDirectInit()
12403 ? OMPDeclareReductionDecl::DirectInit
12404 : OMPDeclareReductionDecl::CopyInit);
12405 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012406 DRD->setInvalidDecl();
Alexey Bataev070f43a2017-09-06 14:49:58 +000012407 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012408}
12409
12410Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
12411 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
12412 for (auto *D : DeclReductions.get()) {
12413 if (IsValid) {
12414 auto *DRD = cast<OMPDeclareReductionDecl>(D);
12415 if (S != nullptr)
12416 PushOnScopeChains(DRD, S, /*AddToContext=*/false);
12417 } else
12418 D->setInvalidDecl();
12419 }
12420 return DeclReductions;
12421}
12422
David Majnemer9d168222016-08-05 17:44:54 +000012423OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
Kelvin Li099bb8c2015-11-24 20:50:12 +000012424 SourceLocation StartLoc,
12425 SourceLocation LParenLoc,
12426 SourceLocation EndLoc) {
12427 Expr *ValExpr = NumTeams;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000012428 Stmt *HelperValStmt = nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000012429
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012430 // OpenMP [teams Constrcut, Restrictions]
12431 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000012432 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
12433 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012434 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000012435
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000012436 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000012437 OpenMPDirectiveKind CaptureRegion =
12438 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams);
12439 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000012440 ValExpr = MakeFullExpr(ValExpr).get();
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000012441 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
12442 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
12443 HelperValStmt = buildPreInits(Context, Captures);
12444 }
12445
12446 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion,
12447 StartLoc, LParenLoc, EndLoc);
Kelvin Li099bb8c2015-11-24 20:50:12 +000012448}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012449
12450OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
12451 SourceLocation StartLoc,
12452 SourceLocation LParenLoc,
12453 SourceLocation EndLoc) {
12454 Expr *ValExpr = ThreadLimit;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000012455 Stmt *HelperValStmt = nullptr;
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012456
12457 // OpenMP [teams Constrcut, Restrictions]
12458 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000012459 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
12460 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012461 return nullptr;
12462
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000012463 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000012464 OpenMPDirectiveKind CaptureRegion =
12465 getOpenMPCaptureRegionForClause(DKind, OMPC_thread_limit);
12466 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000012467 ValExpr = MakeFullExpr(ValExpr).get();
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000012468 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
12469 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
12470 HelperValStmt = buildPreInits(Context, Captures);
12471 }
12472
12473 return new (Context) OMPThreadLimitClause(
12474 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012475}
Alexey Bataeva0569352015-12-01 10:17:31 +000012476
12477OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
12478 SourceLocation StartLoc,
12479 SourceLocation LParenLoc,
12480 SourceLocation EndLoc) {
12481 Expr *ValExpr = Priority;
12482
12483 // OpenMP [2.9.1, task Constrcut]
12484 // The priority-value is a non-negative numerical scalar expression.
12485 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
12486 /*StrictlyPositive=*/false))
12487 return nullptr;
12488
12489 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
12490}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000012491
12492OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
12493 SourceLocation StartLoc,
12494 SourceLocation LParenLoc,
12495 SourceLocation EndLoc) {
12496 Expr *ValExpr = Grainsize;
12497
12498 // OpenMP [2.9.2, taskloop Constrcut]
12499 // The parameter of the grainsize clause must be a positive integer
12500 // expression.
12501 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
12502 /*StrictlyPositive=*/true))
12503 return nullptr;
12504
12505 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
12506}
Alexey Bataev382967a2015-12-08 12:06:20 +000012507
12508OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
12509 SourceLocation StartLoc,
12510 SourceLocation LParenLoc,
12511 SourceLocation EndLoc) {
12512 Expr *ValExpr = NumTasks;
12513
12514 // OpenMP [2.9.2, taskloop Constrcut]
12515 // The parameter of the num_tasks clause must be a positive integer
12516 // expression.
12517 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
12518 /*StrictlyPositive=*/true))
12519 return nullptr;
12520
12521 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
12522}
12523
Alexey Bataev28c75412015-12-15 08:19:24 +000012524OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
12525 SourceLocation LParenLoc,
12526 SourceLocation EndLoc) {
12527 // OpenMP [2.13.2, critical construct, Description]
12528 // ... where hint-expression is an integer constant expression that evaluates
12529 // to a valid lock hint.
12530 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
12531 if (HintExpr.isInvalid())
12532 return nullptr;
12533 return new (Context)
12534 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
12535}
12536
Carlo Bertollib4adf552016-01-15 18:50:31 +000012537OMPClause *Sema::ActOnOpenMPDistScheduleClause(
12538 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
12539 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
12540 SourceLocation EndLoc) {
12541 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
12542 std::string Values;
12543 Values += "'";
12544 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
12545 Values += "'";
12546 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
12547 << Values << getOpenMPClauseName(OMPC_dist_schedule);
12548 return nullptr;
12549 }
12550 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000012551 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000012552 if (ChunkSize) {
12553 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
12554 !ChunkSize->isInstantiationDependent() &&
12555 !ChunkSize->containsUnexpandedParameterPack()) {
12556 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
12557 ExprResult Val =
12558 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
12559 if (Val.isInvalid())
12560 return nullptr;
12561
12562 ValExpr = Val.get();
12563
12564 // OpenMP [2.7.1, Restrictions]
12565 // chunk_size must be a loop invariant integer expression with a positive
12566 // value.
12567 llvm::APSInt Result;
12568 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
12569 if (Result.isSigned() && !Result.isStrictlyPositive()) {
12570 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
12571 << "dist_schedule" << ChunkSize->getSourceRange();
12572 return nullptr;
12573 }
Alexey Bataev2ba67042017-11-28 21:11:44 +000012574 } else if (getOpenMPCaptureRegionForClause(
12575 DSAStack->getCurrentDirective(), OMPC_dist_schedule) !=
12576 OMPD_unknown &&
Alexey Bataevb46cdea2016-06-15 11:20:48 +000012577 !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000012578 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +000012579 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
12580 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
12581 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000012582 }
12583 }
12584 }
12585
12586 return new (Context)
12587 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000012588 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000012589}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000012590
12591OMPClause *Sema::ActOnOpenMPDefaultmapClause(
12592 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
12593 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
12594 SourceLocation KindLoc, SourceLocation EndLoc) {
12595 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
David Majnemer9d168222016-08-05 17:44:54 +000012596 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) {
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000012597 std::string Value;
12598 SourceLocation Loc;
12599 Value += "'";
12600 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
12601 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000012602 OMPC_DEFAULTMAP_MODIFIER_tofrom);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000012603 Loc = MLoc;
12604 } else {
12605 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000012606 OMPC_DEFAULTMAP_scalar);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000012607 Loc = KindLoc;
12608 }
12609 Value += "'";
12610 Diag(Loc, diag::err_omp_unexpected_clause_value)
12611 << Value << getOpenMPClauseName(OMPC_defaultmap);
12612 return nullptr;
12613 }
Alexey Bataev2fd0cb22017-10-05 17:51:39 +000012614 DSAStack->setDefaultDMAToFromScalar(StartLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000012615
12616 return new (Context)
12617 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
12618}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012619
12620bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
12621 DeclContext *CurLexicalContext = getCurLexicalContext();
12622 if (!CurLexicalContext->isFileContext() &&
12623 !CurLexicalContext->isExternCContext() &&
Alexey Bataev502ec492017-10-03 20:00:00 +000012624 !CurLexicalContext->isExternCXXContext() &&
12625 !isa<CXXRecordDecl>(CurLexicalContext) &&
12626 !isa<ClassTemplateDecl>(CurLexicalContext) &&
12627 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) &&
12628 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012629 Diag(Loc, diag::err_omp_region_not_file_context);
12630 return false;
12631 }
12632 if (IsInOpenMPDeclareTargetContext) {
12633 Diag(Loc, diag::err_omp_enclosed_declare_target);
12634 return false;
12635 }
12636
12637 IsInOpenMPDeclareTargetContext = true;
12638 return true;
12639}
12640
12641void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
12642 assert(IsInOpenMPDeclareTargetContext &&
12643 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
12644
12645 IsInOpenMPDeclareTargetContext = false;
12646}
12647
David Majnemer9d168222016-08-05 17:44:54 +000012648void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope,
12649 CXXScopeSpec &ScopeSpec,
12650 const DeclarationNameInfo &Id,
12651 OMPDeclareTargetDeclAttr::MapTypeTy MT,
12652 NamedDeclSetType &SameDirectiveDecls) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012653 LookupResult Lookup(*this, Id, LookupOrdinaryName);
12654 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
12655
12656 if (Lookup.isAmbiguous())
12657 return;
12658 Lookup.suppressDiagnostics();
12659
12660 if (!Lookup.isSingleResult()) {
12661 if (TypoCorrection Corrected =
12662 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
12663 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
12664 CTK_ErrorRecovery)) {
12665 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
12666 << Id.getName());
12667 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
12668 return;
12669 }
12670
12671 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
12672 return;
12673 }
12674
12675 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
12676 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
12677 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
12678 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
12679
12680 if (!ND->hasAttr<OMPDeclareTargetDeclAttr>()) {
12681 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
12682 ND->addAttr(A);
12683 if (ASTMutationListener *ML = Context.getASTMutationListener())
12684 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
Kelvin Li1ce87c72017-12-12 20:08:12 +000012685 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Id.getLoc());
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012686 } else if (ND->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() != MT) {
12687 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
12688 << Id.getName();
12689 }
12690 } else
12691 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
12692}
12693
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012694static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
12695 Sema &SemaRef, Decl *D) {
12696 if (!D)
12697 return;
12698 Decl *LD = nullptr;
12699 if (isa<TagDecl>(D)) {
12700 LD = cast<TagDecl>(D)->getDefinition();
12701 } else if (isa<VarDecl>(D)) {
12702 LD = cast<VarDecl>(D)->getDefinition();
12703
12704 // If this is an implicit variable that is legal and we do not need to do
12705 // anything.
12706 if (cast<VarDecl>(D)->isImplicit()) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012707 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
12708 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
12709 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012710 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012711 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012712 return;
12713 }
12714
12715 } else if (isa<FunctionDecl>(D)) {
12716 const FunctionDecl *FD = nullptr;
12717 if (cast<FunctionDecl>(D)->hasBody(FD))
12718 LD = const_cast<FunctionDecl *>(FD);
12719
12720 // If the definition is associated with the current declaration in the
12721 // target region (it can be e.g. a lambda) that is legal and we do not need
12722 // to do anything else.
12723 if (LD == D) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012724 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
12725 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
12726 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012727 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012728 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012729 return;
12730 }
12731 }
12732 if (!LD)
12733 LD = D;
12734 if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() &&
12735 (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) {
12736 // Outlined declaration is not declared target.
12737 if (LD->isOutOfLine()) {
12738 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
12739 SemaRef.Diag(SL, diag::note_used_here) << SR;
12740 } else {
12741 DeclContext *DC = LD->getDeclContext();
12742 while (DC) {
12743 if (isa<FunctionDecl>(DC) &&
12744 cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>())
12745 break;
12746 DC = DC->getParent();
12747 }
12748 if (DC)
12749 return;
12750
12751 // Is not declared in target context.
12752 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
12753 SemaRef.Diag(SL, diag::note_used_here) << SR;
12754 }
12755 // Mark decl as declared target to prevent further diagnostic.
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012756 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
12757 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
12758 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012759 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012760 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012761 }
12762}
12763
12764static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
12765 Sema &SemaRef, DSAStackTy *Stack,
12766 ValueDecl *VD) {
12767 if (VD->hasAttr<OMPDeclareTargetDeclAttr>())
12768 return true;
12769 if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType()))
12770 return false;
12771 return true;
12772}
12773
Kelvin Li1ce87c72017-12-12 20:08:12 +000012774void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D,
12775 SourceLocation IdLoc) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012776 if (!D || D->isInvalidDecl())
12777 return;
12778 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
12779 SourceLocation SL = E ? E->getLocStart() : D->getLocation();
12780 // 2.10.6: threadprivate variable cannot appear in a declare target directive.
12781 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
12782 if (DSAStack->isThreadPrivate(VD)) {
12783 Diag(SL, diag::err_omp_threadprivate_in_target);
12784 ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
12785 return;
12786 }
12787 }
12788 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
12789 // Problem if any with var declared with incomplete type will be reported
12790 // as normal, so no need to check it here.
12791 if ((E || !VD->getType()->isIncompleteType()) &&
12792 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) {
12793 // Mark decl as declared target to prevent further diagnostic.
12794 if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012795 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
12796 Context, OMPDeclareTargetDeclAttr::MT_To);
12797 VD->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012798 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012799 ML->DeclarationMarkedOpenMPDeclareTarget(VD, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012800 }
12801 return;
12802 }
12803 }
Kelvin Li1ce87c72017-12-12 20:08:12 +000012804 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
12805 if (FD->hasAttr<OMPDeclareTargetDeclAttr>() &&
12806 (FD->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() ==
12807 OMPDeclareTargetDeclAttr::MT_Link)) {
12808 assert(IdLoc.isValid() && "Source location is expected");
12809 Diag(IdLoc, diag::err_omp_function_in_link_clause);
12810 Diag(FD->getLocation(), diag::note_defined_here) << FD;
12811 return;
12812 }
12813 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012814 if (!E) {
12815 // Checking declaration inside declare target region.
12816 if (!D->hasAttr<OMPDeclareTargetDeclAttr>() &&
12817 (isa<VarDecl>(D) || isa<FunctionDecl>(D))) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012818 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
12819 Context, OMPDeclareTargetDeclAttr::MT_To);
12820 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012821 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012822 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012823 }
12824 return;
12825 }
12826 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
12827}
Samuel Antao661c0902016-05-26 17:39:58 +000012828
12829OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
12830 SourceLocation StartLoc,
12831 SourceLocation LParenLoc,
12832 SourceLocation EndLoc) {
12833 MappableVarListInfo MVLI(VarList);
12834 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc);
12835 if (MVLI.ProcessedVarList.empty())
12836 return nullptr;
12837
12838 return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc,
12839 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
12840 MVLI.VarComponents);
12841}
Samuel Antaoec172c62016-05-26 17:49:04 +000012842
12843OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
12844 SourceLocation StartLoc,
12845 SourceLocation LParenLoc,
12846 SourceLocation EndLoc) {
12847 MappableVarListInfo MVLI(VarList);
12848 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc);
12849 if (MVLI.ProcessedVarList.empty())
12850 return nullptr;
12851
12852 return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc,
12853 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
12854 MVLI.VarComponents);
12855}
Carlo Bertolli2404b172016-07-13 15:37:16 +000012856
12857OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
12858 SourceLocation StartLoc,
12859 SourceLocation LParenLoc,
12860 SourceLocation EndLoc) {
Samuel Antaocc10b852016-07-28 14:23:26 +000012861 MappableVarListInfo MVLI(VarList);
12862 SmallVector<Expr *, 8> PrivateCopies;
12863 SmallVector<Expr *, 8> Inits;
12864
Carlo Bertolli2404b172016-07-13 15:37:16 +000012865 for (auto &RefExpr : VarList) {
12866 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
12867 SourceLocation ELoc;
12868 SourceRange ERange;
12869 Expr *SimpleRefExpr = RefExpr;
12870 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
12871 if (Res.second) {
12872 // It will be analyzed later.
Samuel Antaocc10b852016-07-28 14:23:26 +000012873 MVLI.ProcessedVarList.push_back(RefExpr);
12874 PrivateCopies.push_back(nullptr);
12875 Inits.push_back(nullptr);
Carlo Bertolli2404b172016-07-13 15:37:16 +000012876 }
12877 ValueDecl *D = Res.first;
12878 if (!D)
12879 continue;
12880
12881 QualType Type = D->getType();
Samuel Antaocc10b852016-07-28 14:23:26 +000012882 Type = Type.getNonReferenceType().getUnqualifiedType();
12883
12884 auto *VD = dyn_cast<VarDecl>(D);
12885
12886 // Item should be a pointer or reference to pointer.
12887 if (!Type->isPointerType()) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000012888 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
12889 << 0 << RefExpr->getSourceRange();
12890 continue;
12891 }
Samuel Antaocc10b852016-07-28 14:23:26 +000012892
12893 // Build the private variable and the expression that refers to it.
12894 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
12895 D->hasAttrs() ? &D->getAttrs() : nullptr);
12896 if (VDPrivate->isInvalidDecl())
12897 continue;
12898
12899 CurContext->addDecl(VDPrivate);
12900 auto VDPrivateRefExpr = buildDeclRefExpr(
12901 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
12902
12903 // Add temporary variable to initialize the private copy of the pointer.
12904 auto *VDInit =
12905 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
12906 auto *VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
12907 RefExpr->getExprLoc());
12908 AddInitializerToDecl(VDPrivate,
12909 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000012910 /*DirectInit=*/false);
Samuel Antaocc10b852016-07-28 14:23:26 +000012911
12912 // If required, build a capture to implement the privatization initialized
12913 // with the current list item value.
12914 DeclRefExpr *Ref = nullptr;
12915 if (!VD)
12916 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
12917 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
12918 PrivateCopies.push_back(VDPrivateRefExpr);
12919 Inits.push_back(VDInitRefExpr);
12920
12921 // We need to add a data sharing attribute for this variable to make sure it
12922 // is correctly captured. A variable that shows up in a use_device_ptr has
12923 // similar properties of a first private variable.
12924 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
12925
12926 // Create a mappable component for the list item. List items in this clause
12927 // only need a component.
12928 MVLI.VarBaseDeclarations.push_back(D);
12929 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
12930 MVLI.VarComponents.back().push_back(
12931 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D));
Carlo Bertolli2404b172016-07-13 15:37:16 +000012932 }
12933
Samuel Antaocc10b852016-07-28 14:23:26 +000012934 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli2404b172016-07-13 15:37:16 +000012935 return nullptr;
12936
Samuel Antaocc10b852016-07-28 14:23:26 +000012937 return OMPUseDevicePtrClause::Create(
12938 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
12939 PrivateCopies, Inits, MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli2404b172016-07-13 15:37:16 +000012940}
Carlo Bertolli70594e92016-07-13 17:16:49 +000012941
12942OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
12943 SourceLocation StartLoc,
12944 SourceLocation LParenLoc,
12945 SourceLocation EndLoc) {
Samuel Antao6890b092016-07-28 14:25:09 +000012946 MappableVarListInfo MVLI(VarList);
Carlo Bertolli70594e92016-07-13 17:16:49 +000012947 for (auto &RefExpr : VarList) {
Kelvin Li84376252016-12-14 15:39:58 +000012948 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause.");
Carlo Bertolli70594e92016-07-13 17:16:49 +000012949 SourceLocation ELoc;
12950 SourceRange ERange;
12951 Expr *SimpleRefExpr = RefExpr;
12952 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
12953 if (Res.second) {
12954 // It will be analyzed later.
Samuel Antao6890b092016-07-28 14:25:09 +000012955 MVLI.ProcessedVarList.push_back(RefExpr);
Carlo Bertolli70594e92016-07-13 17:16:49 +000012956 }
12957 ValueDecl *D = Res.first;
12958 if (!D)
12959 continue;
12960
12961 QualType Type = D->getType();
12962 // item should be a pointer or array or reference to pointer or array
12963 if (!Type.getNonReferenceType()->isPointerType() &&
12964 !Type.getNonReferenceType()->isArrayType()) {
12965 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
12966 << 0 << RefExpr->getSourceRange();
12967 continue;
12968 }
Samuel Antao6890b092016-07-28 14:25:09 +000012969
12970 // Check if the declaration in the clause does not show up in any data
12971 // sharing attribute.
12972 auto DVar = DSAStack->getTopDSA(D, false);
12973 if (isOpenMPPrivate(DVar.CKind)) {
12974 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
12975 << getOpenMPClauseName(DVar.CKind)
12976 << getOpenMPClauseName(OMPC_is_device_ptr)
12977 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
12978 ReportOriginalDSA(*this, DSAStack, D, DVar);
12979 continue;
12980 }
12981
12982 Expr *ConflictExpr;
12983 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +000012984 D, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +000012985 [&ConflictExpr](
12986 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
12987 OpenMPClauseKind) -> bool {
12988 ConflictExpr = R.front().getAssociatedExpression();
12989 return true;
12990 })) {
12991 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
12992 Diag(ConflictExpr->getExprLoc(), diag::note_used_here)
12993 << ConflictExpr->getSourceRange();
12994 continue;
12995 }
12996
12997 // Store the components in the stack so that they can be used to check
12998 // against other clauses later on.
12999 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D);
13000 DSAStack->addMappableExpressionComponents(
13001 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
13002
13003 // Record the expression we've just processed.
13004 MVLI.ProcessedVarList.push_back(SimpleRefExpr);
13005
13006 // Create a mappable component for the list item. List items in this clause
13007 // only need a component. We use a null declaration to signal fields in
13008 // 'this'.
13009 assert((isa<DeclRefExpr>(SimpleRefExpr) ||
13010 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
13011 "Unexpected device pointer expression!");
13012 MVLI.VarBaseDeclarations.push_back(
13013 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr);
13014 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13015 MVLI.VarComponents.back().push_back(MC);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013016 }
13017
Samuel Antao6890b092016-07-28 14:25:09 +000013018 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli70594e92016-07-13 17:16:49 +000013019 return nullptr;
13020
Samuel Antao6890b092016-07-28 14:25:09 +000013021 return OMPIsDevicePtrClause::Create(
13022 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
13023 MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013024}