blob: 24b58e8fd12b0318f8ada6945b62bf7e04224118 [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:
Kelvin Li1851df52017-01-03 05:23:48 +00002230 case OMPD_target_teams_distribute_parallel_for:
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00002231 case OMPD_target_teams_distribute_parallel_for_simd: {
Carlo Bertolli9925f152016-06-27 14:55:37 +00002232 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
2233 QualType KmpInt32PtrTy =
2234 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2235 Sema::CapturedParamNameType Params[] = {
2236 std::make_pair(".global_tid.", KmpInt32PtrTy),
2237 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2238 std::make_pair(".previous.lb.", Context.getSizeType()),
2239 std::make_pair(".previous.ub.", Context.getSizeType()),
2240 std::make_pair(StringRef(), QualType()) // __context with shared vars
2241 };
2242 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2243 Params);
2244 break;
2245 }
Alexey Bataev46506272017-12-05 17:41:34 +00002246 case OMPD_teams_distribute_parallel_for:
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00002247 case OMPD_teams_distribute_parallel_for_simd: {
Carlo Bertolli62fae152017-11-20 20:46:39 +00002248 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
2249 QualType KmpInt32PtrTy =
2250 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2251
2252 Sema::CapturedParamNameType ParamsTeams[] = {
2253 std::make_pair(".global_tid.", KmpInt32PtrTy),
2254 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2255 std::make_pair(StringRef(), QualType()) // __context with shared vars
2256 };
2257 // Start a captured region for 'target' with no implicit parameters.
2258 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2259 ParamsTeams);
2260
2261 Sema::CapturedParamNameType ParamsParallel[] = {
2262 std::make_pair(".global_tid.", KmpInt32PtrTy),
2263 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2264 std::make_pair(".previous.lb.", Context.getSizeType()),
2265 std::make_pair(".previous.ub.", Context.getSizeType()),
2266 std::make_pair(StringRef(), QualType()) // __context with shared vars
2267 };
2268 // Start a captured region for 'teams' or 'parallel'. Both regions have
2269 // the same implicit parameters.
2270 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2271 ParamsParallel);
2272 break;
2273 }
Alexey Bataev7828b252017-11-21 17:08:48 +00002274 case OMPD_target_update:
2275 case OMPD_target_enter_data:
2276 case OMPD_target_exit_data: {
2277 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
2278 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
2279 FunctionProtoType::ExtProtoInfo EPI;
2280 EPI.Variadic = true;
2281 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2282 Sema::CapturedParamNameType Params[] = {
2283 std::make_pair(".global_tid.", KmpInt32Ty),
2284 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
2285 std::make_pair(".privates.", Context.VoidPtrTy.withConst()),
2286 std::make_pair(".copy_fn.",
2287 Context.getPointerType(CopyFnType).withConst()),
2288 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2289 std::make_pair(StringRef(), QualType()) // __context with shared vars
2290 };
2291 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2292 Params);
2293 // Mark this captured region as inlined, because we don't use outlined
2294 // function directly.
2295 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2296 AlwaysInlineAttr::CreateImplicit(
2297 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
2298 break;
2299 }
Alexey Bataev9959db52014-05-06 10:08:46 +00002300 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00002301 case OMPD_taskyield:
2302 case OMPD_barrier:
2303 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002304 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00002305 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00002306 case OMPD_flush:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002307 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00002308 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00002309 case OMPD_declare_target:
2310 case OMPD_end_declare_target:
Alexey Bataev9959db52014-05-06 10:08:46 +00002311 llvm_unreachable("OpenMP Directive is not allowed");
2312 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00002313 llvm_unreachable("Unknown OpenMP directive");
2314 }
2315}
2316
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002317int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) {
2318 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2319 getOpenMPCaptureRegions(CaptureRegions, DKind);
2320 return CaptureRegions.size();
2321}
2322
Alexey Bataev3392d762016-02-16 11:18:12 +00002323static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00002324 Expr *CaptureExpr, bool WithInit,
2325 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00002326 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00002327 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00002328 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00002329 QualType Ty = Init->getType();
2330 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002331 if (S.getLangOpts().CPlusPlus) {
Alexey Bataev4244be22016-02-11 05:35:55 +00002332 Ty = C.getLValueReferenceType(Ty);
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002333 } else {
Alexey Bataev4244be22016-02-11 05:35:55 +00002334 Ty = C.getPointerType(Ty);
2335 ExprResult Res =
2336 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
2337 if (!Res.isUsable())
2338 return nullptr;
2339 Init = Res.get();
2340 }
Alexey Bataev61205072016-03-02 04:57:40 +00002341 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00002342 }
Alexey Bataeva7206b92016-12-20 16:51:02 +00002343 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty,
2344 CaptureExpr->getLocStart());
Alexey Bataev2bbf7212016-03-03 03:52:24 +00002345 if (!WithInit)
2346 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
Alexey Bataev4244be22016-02-11 05:35:55 +00002347 S.CurContext->addHiddenDecl(CED);
Richard Smith3beb7c62017-01-12 02:27:38 +00002348 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00002349 return CED;
2350}
2351
Alexey Bataev61205072016-03-02 04:57:40 +00002352static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
2353 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00002354 OMPCapturedExprDecl *CD;
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002355 if (auto *VD = S.IsOpenMPCapturedDecl(D)) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00002356 CD = cast<OMPCapturedExprDecl>(VD);
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002357 } else {
Alexey Bataev5a3af132016-03-29 08:58:54 +00002358 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
2359 /*AsExpression=*/false);
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002360 }
Alexey Bataev3392d762016-02-16 11:18:12 +00002361 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00002362 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00002363}
2364
Alexey Bataev5a3af132016-03-29 08:58:54 +00002365static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002366 CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00002367 if (!Ref) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002368 OMPCapturedExprDecl *CD = buildCaptureDecl(
2369 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr,
2370 /*WithInit=*/true, /*AsExpression=*/true);
Alexey Bataev5a3af132016-03-29 08:58:54 +00002371 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
2372 CaptureExpr->getExprLoc());
2373 }
2374 ExprResult Res = Ref;
2375 if (!S.getLangOpts().CPlusPlus &&
2376 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002377 Ref->getType()->isPointerType()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00002378 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002379 if (!Res.isUsable())
2380 return ExprError();
2381 }
2382 return S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00002383}
2384
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002385namespace {
2386// OpenMP directives parsed in this section are represented as a
2387// CapturedStatement with an associated statement. If a syntax error
2388// is detected during the parsing of the associated statement, the
2389// compiler must abort processing and close the CapturedStatement.
2390//
2391// Combined directives such as 'target parallel' have more than one
2392// nested CapturedStatements. This RAII ensures that we unwind out
2393// of all the nested CapturedStatements when an error is found.
2394class CaptureRegionUnwinderRAII {
2395private:
2396 Sema &S;
2397 bool &ErrorFound;
2398 OpenMPDirectiveKind DKind;
2399
2400public:
2401 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
2402 OpenMPDirectiveKind DKind)
2403 : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
2404 ~CaptureRegionUnwinderRAII() {
2405 if (ErrorFound) {
2406 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind);
2407 while (--ThisCaptureLevel >= 0)
2408 S.ActOnCapturedRegionError();
2409 }
2410 }
2411};
2412} // namespace
2413
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002414StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
2415 ArrayRef<OMPClause *> Clauses) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002416 bool ErrorFound = false;
2417 CaptureRegionUnwinderRAII CaptureRegionUnwinder(
2418 *this, ErrorFound, DSAStack->getCurrentDirective());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002419 if (!S.isUsable()) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002420 ErrorFound = true;
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002421 return StmtError();
2422 }
Alexey Bataev993d2802015-12-28 06:23:08 +00002423
Alexey Bataev2ba67042017-11-28 21:11:44 +00002424 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2425 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
Alexey Bataev993d2802015-12-28 06:23:08 +00002426 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00002427 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00002428 SmallVector<OMPLinearClause *, 4> LCs;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002429 SmallVector<OMPClauseWithPreInit *, 8> PICs;
Alexey Bataev040d5402015-05-12 08:35:28 +00002430 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002431 for (auto *Clause : Clauses) {
Alexey Bataev88202be2017-07-27 13:20:36 +00002432 if (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) &&
2433 Clause->getClauseKind() == OMPC_in_reduction) {
2434 // Capture taskgroup task_reduction descriptors inside the tasking regions
2435 // with the corresponding in_reduction items.
2436 auto *IRC = cast<OMPInReductionClause>(Clause);
2437 for (auto *E : IRC->taskgroup_descriptors())
2438 if (E)
2439 MarkDeclarationsReferencedInExpr(E);
2440 }
Alexey Bataev16dc7b62015-05-20 03:46:04 +00002441 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00002442 Clause->getClauseKind() == OMPC_copyprivate ||
2443 (getLangOpts().OpenMPUseTLS &&
2444 getASTContext().getTargetInfo().isTLSSupported() &&
2445 Clause->getClauseKind() == OMPC_copyin)) {
2446 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00002447 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002448 for (auto *VarRef : Clause->children()) {
2449 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00002450 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002451 }
2452 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00002453 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev2ba67042017-11-28 21:11:44 +00002454 } else if (CaptureRegions.size() > 1 ||
2455 CaptureRegions.back() != OMPD_unknown) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002456 if (auto *C = OMPClauseWithPreInit::get(Clause))
2457 PICs.push_back(C);
Alexey Bataev005248a2016-02-25 05:25:57 +00002458 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
2459 if (auto *E = C->getPostUpdateExpr())
2460 MarkDeclarationsReferencedInExpr(E);
2461 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002462 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002463 if (Clause->getClauseKind() == OMPC_schedule)
2464 SC = cast<OMPScheduleClause>(Clause);
2465 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00002466 OC = cast<OMPOrderedClause>(Clause);
2467 else if (Clause->getClauseKind() == OMPC_linear)
2468 LCs.push_back(cast<OMPLinearClause>(Clause));
2469 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002470 // OpenMP, 2.7.1 Loop Construct, Restrictions
2471 // The nonmonotonic modifier cannot be specified if an ordered clause is
2472 // specified.
2473 if (SC &&
2474 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
2475 SC->getSecondScheduleModifier() ==
2476 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
2477 OC) {
2478 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
2479 ? SC->getFirstScheduleModifierLoc()
2480 : SC->getSecondScheduleModifierLoc(),
2481 diag::err_omp_schedule_nonmonotonic_ordered)
2482 << SourceRange(OC->getLocStart(), OC->getLocEnd());
2483 ErrorFound = true;
2484 }
Alexey Bataev993d2802015-12-28 06:23:08 +00002485 if (!LCs.empty() && OC && OC->getNumForLoops()) {
2486 for (auto *C : LCs) {
2487 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
2488 << SourceRange(OC->getLocStart(), OC->getLocEnd());
2489 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002490 ErrorFound = true;
2491 }
Alexey Bataev113438c2015-12-30 12:06:23 +00002492 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
2493 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
2494 OC->getNumForLoops()) {
2495 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
2496 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
2497 ErrorFound = true;
2498 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002499 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00002500 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002501 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002502 StmtResult SR = S;
Alexey Bataev2ba67042017-11-28 21:11:44 +00002503 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002504 // Mark all variables in private list clauses as used in inner region.
2505 // Required for proper codegen of combined directives.
2506 // TODO: add processing for other clauses.
Alexey Bataev2ba67042017-11-28 21:11:44 +00002507 if (ThisCaptureRegion != OMPD_unknown) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002508 for (auto *C : PICs) {
2509 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
2510 // Find the particular capture region for the clause if the
2511 // directive is a combined one with multiple capture regions.
2512 // If the directive is not a combined one, the capture region
2513 // associated with the clause is OMPD_unknown and is generated
2514 // only once.
2515 if (CaptureRegion == ThisCaptureRegion ||
2516 CaptureRegion == OMPD_unknown) {
2517 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
2518 for (auto *D : DS->decls())
2519 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
2520 }
2521 }
2522 }
2523 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002524 SR = ActOnCapturedRegionEnd(SR.get());
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002525 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002526 return SR;
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002527}
2528
Jonas Hahnfeld64a9e3c2017-02-22 06:49:10 +00002529static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
2530 OpenMPDirectiveKind CancelRegion,
2531 SourceLocation StartLoc) {
2532 // CancelRegion is only needed for cancel and cancellation_point.
2533 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point)
2534 return false;
2535
2536 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for ||
2537 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup)
2538 return false;
2539
2540 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region)
2541 << getOpenMPDirectiveName(CancelRegion);
2542 return true;
2543}
2544
2545static bool checkNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002546 OpenMPDirectiveKind CurrentRegion,
2547 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002548 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002549 SourceLocation StartLoc) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002550 if (Stack->getCurScope()) {
2551 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002552 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002553 bool NestingProhibited = false;
2554 bool CloseNesting = true;
David Majnemer9d168222016-08-05 17:44:54 +00002555 bool OrphanSeen = false;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002556 enum {
2557 NoRecommend,
2558 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002559 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002560 ShouldBeInTargetRegion,
2561 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002562 } Recommend = NoRecommend;
Kelvin Lifd8b5742016-07-01 14:30:25 +00002563 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002564 // OpenMP [2.16, Nesting of Regions]
2565 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002566 // OpenMP [2.8.1,simd Construct, Restrictions]
Kelvin Lifd8b5742016-07-01 14:30:25 +00002567 // An ordered construct with the simd clause is the only OpenMP
2568 // construct that can appear in the simd region.
David Majnemer9d168222016-08-05 17:44:54 +00002569 // Allowing a SIMD construct nested in another SIMD construct is an
Kelvin Lifd8b5742016-07-01 14:30:25 +00002570 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
2571 // message.
2572 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
2573 ? diag::err_omp_prohibited_region_simd
2574 : diag::warn_omp_nesting_simd);
2575 return CurrentRegion != OMPD_simd;
Alexey Bataev549210e2014-06-24 04:39:47 +00002576 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002577 if (ParentRegion == OMPD_atomic) {
2578 // OpenMP [2.16, Nesting of Regions]
2579 // OpenMP constructs may not be nested inside an atomic region.
2580 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2581 return true;
2582 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002583 if (CurrentRegion == OMPD_section) {
2584 // OpenMP [2.7.2, sections Construct, Restrictions]
2585 // Orphaned section directives are prohibited. That is, the section
2586 // directives must appear within the sections construct and must not be
2587 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002588 if (ParentRegion != OMPD_sections &&
2589 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002590 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2591 << (ParentRegion != OMPD_unknown)
2592 << getOpenMPDirectiveName(ParentRegion);
2593 return true;
2594 }
2595 return false;
2596 }
Kelvin Li2b51f722016-07-26 04:32:50 +00002597 // Allow some constructs (except teams) to be orphaned (they could be
David Majnemer9d168222016-08-05 17:44:54 +00002598 // used in functions, called from OpenMP regions with the required
Kelvin Li2b51f722016-07-26 04:32:50 +00002599 // preconditions).
Kelvin Libf594a52016-12-17 05:48:59 +00002600 if (ParentRegion == OMPD_unknown &&
2601 !isOpenMPNestingTeamsDirective(CurrentRegion))
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002602 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002603 if (CurrentRegion == OMPD_cancellation_point ||
2604 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002605 // OpenMP [2.16, Nesting of Regions]
2606 // A cancellation point construct for which construct-type-clause is
2607 // taskgroup must be nested inside a task construct. A cancellation
2608 // point construct for which construct-type-clause is not taskgroup must
2609 // be closely nested inside an OpenMP construct that matches the type
2610 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002611 // A cancel construct for which construct-type-clause is taskgroup must be
2612 // nested inside a task construct. A cancel construct for which
2613 // construct-type-clause is not taskgroup must be closely nested inside an
2614 // OpenMP construct that matches the type specified in
2615 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002616 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002617 !((CancelRegion == OMPD_parallel &&
2618 (ParentRegion == OMPD_parallel ||
2619 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002620 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002621 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00002622 ParentRegion == OMPD_target_parallel_for ||
2623 ParentRegion == OMPD_distribute_parallel_for ||
Alexey Bataev16e79882017-11-22 21:12:03 +00002624 ParentRegion == OMPD_teams_distribute_parallel_for ||
2625 ParentRegion == OMPD_target_teams_distribute_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002626 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2627 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002628 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2629 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002630 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002631 // OpenMP [2.16, Nesting of Regions]
2632 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002633 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002634 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00002635 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002636 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2637 // OpenMP [2.16, Nesting of Regions]
2638 // A critical region may not be nested (closely or otherwise) inside a
2639 // critical region with the same name. Note that this restriction is not
2640 // sufficient to prevent deadlock.
2641 SourceLocation PreviousCriticalLoc;
David Majnemer9d168222016-08-05 17:44:54 +00002642 bool DeadLock = Stack->hasDirective(
2643 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
2644 const DeclarationNameInfo &DNI,
2645 SourceLocation Loc) -> bool {
2646 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
2647 PreviousCriticalLoc = Loc;
2648 return true;
2649 } else
2650 return false;
2651 },
2652 false /* skip top directive */);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002653 if (DeadLock) {
2654 SemaRef.Diag(StartLoc,
2655 diag::err_omp_prohibited_region_critical_same_name)
2656 << CurrentName.getName();
2657 if (PreviousCriticalLoc.isValid())
2658 SemaRef.Diag(PreviousCriticalLoc,
2659 diag::note_omp_previous_critical_region);
2660 return true;
2661 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002662 } else if (CurrentRegion == OMPD_barrier) {
2663 // OpenMP [2.16, Nesting of Regions]
2664 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002665 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00002666 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2667 isOpenMPTaskingDirective(ParentRegion) ||
2668 ParentRegion == OMPD_master ||
2669 ParentRegion == OMPD_critical ||
2670 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00002671 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Kelvin Li579e41c2016-11-30 23:51:03 +00002672 !isOpenMPParallelDirective(CurrentRegion) &&
2673 !isOpenMPTeamsDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002674 // OpenMP [2.16, Nesting of Regions]
2675 // A worksharing region may not be closely nested inside a worksharing,
2676 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00002677 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2678 isOpenMPTaskingDirective(ParentRegion) ||
2679 ParentRegion == OMPD_master ||
2680 ParentRegion == OMPD_critical ||
2681 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002682 Recommend = ShouldBeInParallelRegion;
2683 } else if (CurrentRegion == OMPD_ordered) {
2684 // OpenMP [2.16, Nesting of Regions]
2685 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002686 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002687 // An ordered region must be closely nested inside a loop region (or
2688 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002689 // OpenMP [2.8.1,simd Construct, Restrictions]
2690 // An ordered construct with the simd clause is the only OpenMP construct
2691 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002692 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00002693 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002694 !(isOpenMPSimdDirective(ParentRegion) ||
2695 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002696 Recommend = ShouldBeInOrderedRegion;
Kelvin Libf594a52016-12-17 05:48:59 +00002697 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00002698 // OpenMP [2.16, Nesting of Regions]
2699 // If specified, a teams construct must be contained within a target
2700 // construct.
2701 NestingProhibited = ParentRegion != OMPD_target;
Kelvin Li2b51f722016-07-26 04:32:50 +00002702 OrphanSeen = ParentRegion == OMPD_unknown;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002703 Recommend = ShouldBeInTargetRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002704 }
Kelvin Libf594a52016-12-17 05:48:59 +00002705 if (!NestingProhibited &&
2706 !isOpenMPTargetExecutionDirective(CurrentRegion) &&
2707 !isOpenMPTargetDataManagementDirective(CurrentRegion) &&
2708 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00002709 // OpenMP [2.16, Nesting of Regions]
2710 // distribute, parallel, parallel sections, parallel workshare, and the
2711 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2712 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002713 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2714 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002715 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002716 }
David Majnemer9d168222016-08-05 17:44:54 +00002717 if (!NestingProhibited &&
Kelvin Li02532872016-08-05 14:37:37 +00002718 isOpenMPNestingDistributeDirective(CurrentRegion)) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002719 // OpenMP 4.5 [2.17 Nesting of Regions]
2720 // The region associated with the distribute construct must be strictly
2721 // nested inside a teams region
Kelvin Libf594a52016-12-17 05:48:59 +00002722 NestingProhibited =
2723 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002724 Recommend = ShouldBeInTeamsRegion;
2725 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002726 if (!NestingProhibited &&
2727 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2728 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2729 // OpenMP 4.5 [2.17 Nesting of Regions]
2730 // If a target, target update, target data, target enter data, or
2731 // target exit data construct is encountered during execution of a
2732 // target region, the behavior is unspecified.
2733 NestingProhibited = Stack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +00002734 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
2735 SourceLocation) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002736 if (isOpenMPTargetExecutionDirective(K)) {
2737 OffendingRegion = K;
2738 return true;
2739 } else
2740 return false;
2741 },
2742 false /* don't skip top directive */);
2743 CloseNesting = false;
2744 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002745 if (NestingProhibited) {
Kelvin Li2b51f722016-07-26 04:32:50 +00002746 if (OrphanSeen) {
2747 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
2748 << getOpenMPDirectiveName(CurrentRegion) << Recommend;
2749 } else {
2750 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
2751 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2752 << Recommend << getOpenMPDirectiveName(CurrentRegion);
2753 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002754 return true;
2755 }
2756 }
2757 return false;
2758}
2759
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002760static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2761 ArrayRef<OMPClause *> Clauses,
2762 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2763 bool ErrorFound = false;
2764 unsigned NamedModifiersNumber = 0;
2765 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2766 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002767 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002768 for (const auto *C : Clauses) {
2769 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2770 // At most one if clause without a directive-name-modifier can appear on
2771 // the directive.
2772 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2773 if (FoundNameModifiers[CurNM]) {
2774 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2775 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2776 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2777 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002778 } else if (CurNM != OMPD_unknown) {
2779 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002780 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002781 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002782 FoundNameModifiers[CurNM] = IC;
2783 if (CurNM == OMPD_unknown)
2784 continue;
2785 // Check if the specified name modifier is allowed for the current
2786 // directive.
2787 // At most one if clause with the particular directive-name-modifier can
2788 // appear on the directive.
2789 bool MatchFound = false;
2790 for (auto NM : AllowedNameModifiers) {
2791 if (CurNM == NM) {
2792 MatchFound = true;
2793 break;
2794 }
2795 }
2796 if (!MatchFound) {
2797 S.Diag(IC->getNameModifierLoc(),
2798 diag::err_omp_wrong_if_directive_name_modifier)
2799 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2800 ErrorFound = true;
2801 }
2802 }
2803 }
2804 // If any if clause on the directive includes a directive-name-modifier then
2805 // all if clauses on the directive must include a directive-name-modifier.
2806 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2807 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2808 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2809 diag::err_omp_no_more_if_clause);
2810 } else {
2811 std::string Values;
2812 std::string Sep(", ");
2813 unsigned AllowedCnt = 0;
2814 unsigned TotalAllowedNum =
2815 AllowedNameModifiers.size() - NamedModifiersNumber;
2816 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2817 ++Cnt) {
2818 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2819 if (!FoundNameModifiers[NM]) {
2820 Values += "'";
2821 Values += getOpenMPDirectiveName(NM);
2822 Values += "'";
2823 if (AllowedCnt + 2 == TotalAllowedNum)
2824 Values += " or ";
2825 else if (AllowedCnt + 1 != TotalAllowedNum)
2826 Values += Sep;
2827 ++AllowedCnt;
2828 }
2829 }
2830 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2831 diag::err_omp_unnamed_if_clause)
2832 << (TotalAllowedNum > 1) << Values;
2833 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002834 for (auto Loc : NameModifierLoc) {
2835 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2836 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002837 ErrorFound = true;
2838 }
2839 return ErrorFound;
2840}
2841
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002842StmtResult Sema::ActOnOpenMPExecutableDirective(
2843 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2844 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2845 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002846 StmtResult Res = StmtError();
Jonas Hahnfeld64a9e3c2017-02-22 06:49:10 +00002847 // First check CancelRegion which is then used in checkNestingOfRegions.
2848 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) ||
2849 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002850 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002851 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002852
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002853 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002854 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002855 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002856 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00002857 if (AStmt && !CurContext->isDependentContext()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00002858 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2859
2860 // Check default data sharing attributes for referenced variables.
2861 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
Arpith Chacko Jacob1f46b702017-01-23 15:38:49 +00002862 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind);
2863 Stmt *S = AStmt;
2864 while (--ThisCaptureLevel >= 0)
2865 S = cast<CapturedStmt>(S)->getCapturedStmt();
2866 DSAChecker.Visit(S);
Alexey Bataev68446b72014-07-18 07:47:19 +00002867 if (DSAChecker.isErrorFound())
2868 return StmtError();
2869 // Generate list of implicitly defined firstprivate variables.
2870 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002871
Alexey Bataev88202be2017-07-27 13:20:36 +00002872 SmallVector<Expr *, 4> ImplicitFirstprivates(
2873 DSAChecker.getImplicitFirstprivate().begin(),
2874 DSAChecker.getImplicitFirstprivate().end());
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002875 SmallVector<Expr *, 4> ImplicitMaps(DSAChecker.getImplicitMap().begin(),
2876 DSAChecker.getImplicitMap().end());
Alexey Bataev88202be2017-07-27 13:20:36 +00002877 // Mark taskgroup task_reduction descriptors as implicitly firstprivate.
2878 for (auto *C : Clauses) {
2879 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) {
2880 for (auto *E : IRC->taskgroup_descriptors())
2881 if (E)
2882 ImplicitFirstprivates.emplace_back(E);
2883 }
2884 }
2885 if (!ImplicitFirstprivates.empty()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00002886 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
Alexey Bataev88202be2017-07-27 13:20:36 +00002887 ImplicitFirstprivates, SourceLocation(), SourceLocation(),
2888 SourceLocation())) {
Alexey Bataev68446b72014-07-18 07:47:19 +00002889 ClausesWithImplicit.push_back(Implicit);
2890 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
Alexey Bataev88202be2017-07-27 13:20:36 +00002891 ImplicitFirstprivates.size();
Alexey Bataev68446b72014-07-18 07:47:19 +00002892 } else
2893 ErrorFound = true;
2894 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002895 if (!ImplicitMaps.empty()) {
2896 if (OMPClause *Implicit = ActOnOpenMPMapClause(
2897 OMPC_MAP_unknown, OMPC_MAP_tofrom, /*IsMapTypeImplicit=*/true,
2898 SourceLocation(), SourceLocation(), ImplicitMaps,
2899 SourceLocation(), SourceLocation(), SourceLocation())) {
2900 ClausesWithImplicit.emplace_back(Implicit);
2901 ErrorFound |=
2902 cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMaps.size();
2903 } else
2904 ErrorFound = true;
2905 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002906 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002907
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002908 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002909 switch (Kind) {
2910 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002911 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2912 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002913 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002914 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002915 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002916 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2917 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002918 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002919 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002920 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2921 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002922 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002923 case OMPD_for_simd:
2924 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2925 EndLoc, VarsWithInheritedDSA);
2926 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002927 case OMPD_sections:
2928 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2929 EndLoc);
2930 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002931 case OMPD_section:
2932 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002933 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002934 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2935 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002936 case OMPD_single:
2937 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2938 EndLoc);
2939 break;
Alexander Musman80c22892014-07-17 08:54:58 +00002940 case OMPD_master:
2941 assert(ClausesWithImplicit.empty() &&
2942 "No clauses are allowed for 'omp master' directive");
2943 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2944 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002945 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00002946 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
2947 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002948 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002949 case OMPD_parallel_for:
2950 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2951 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002952 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002953 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002954 case OMPD_parallel_for_simd:
2955 Res = ActOnOpenMPParallelForSimdDirective(
2956 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002957 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00002958 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002959 case OMPD_parallel_sections:
2960 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2961 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002962 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002963 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002964 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002965 Res =
2966 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002967 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002968 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00002969 case OMPD_taskyield:
2970 assert(ClausesWithImplicit.empty() &&
2971 "No clauses are allowed for 'omp taskyield' directive");
2972 assert(AStmt == nullptr &&
2973 "No associated statement allowed for 'omp taskyield' directive");
2974 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2975 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002976 case OMPD_barrier:
2977 assert(ClausesWithImplicit.empty() &&
2978 "No clauses are allowed for 'omp barrier' directive");
2979 assert(AStmt == nullptr &&
2980 "No associated statement allowed for 'omp barrier' directive");
2981 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2982 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00002983 case OMPD_taskwait:
2984 assert(ClausesWithImplicit.empty() &&
2985 "No clauses are allowed for 'omp taskwait' directive");
2986 assert(AStmt == nullptr &&
2987 "No associated statement allowed for 'omp taskwait' directive");
2988 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
2989 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002990 case OMPD_taskgroup:
Alexey Bataev169d96a2017-07-18 20:17:46 +00002991 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc,
2992 EndLoc);
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002993 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00002994 case OMPD_flush:
2995 assert(AStmt == nullptr &&
2996 "No associated statement allowed for 'omp flush' directive");
2997 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
2998 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002999 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003000 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3001 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003002 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003003 case OMPD_atomic:
3004 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3005 EndLoc);
3006 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003007 case OMPD_teams:
3008 Res =
3009 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3010 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003011 case OMPD_target:
3012 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3013 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003014 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003015 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003016 case OMPD_target_parallel:
3017 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3018 StartLoc, EndLoc);
3019 AllowedNameModifiers.push_back(OMPD_target);
3020 AllowedNameModifiers.push_back(OMPD_parallel);
3021 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003022 case OMPD_target_parallel_for:
3023 Res = ActOnOpenMPTargetParallelForDirective(
3024 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3025 AllowedNameModifiers.push_back(OMPD_target);
3026 AllowedNameModifiers.push_back(OMPD_parallel);
3027 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003028 case OMPD_cancellation_point:
3029 assert(ClausesWithImplicit.empty() &&
3030 "No clauses are allowed for 'omp cancellation point' directive");
3031 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3032 "cancellation point' directive");
3033 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3034 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003035 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003036 assert(AStmt == nullptr &&
3037 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003038 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3039 CancelRegion);
3040 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003041 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003042 case OMPD_target_data:
3043 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3044 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003045 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003046 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003047 case OMPD_target_enter_data:
3048 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00003049 EndLoc, AStmt);
Samuel Antaodf67fc42016-01-19 19:15:56 +00003050 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3051 break;
Samuel Antao72590762016-01-19 20:04:50 +00003052 case OMPD_target_exit_data:
3053 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00003054 EndLoc, AStmt);
Samuel Antao72590762016-01-19 20:04:50 +00003055 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3056 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003057 case OMPD_taskloop:
3058 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3059 EndLoc, VarsWithInheritedDSA);
3060 AllowedNameModifiers.push_back(OMPD_taskloop);
3061 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003062 case OMPD_taskloop_simd:
3063 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3064 EndLoc, VarsWithInheritedDSA);
3065 AllowedNameModifiers.push_back(OMPD_taskloop);
3066 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003067 case OMPD_distribute:
3068 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3069 EndLoc, VarsWithInheritedDSA);
3070 break;
Samuel Antao686c70c2016-05-26 17:30:50 +00003071 case OMPD_target_update:
Alexey Bataev7828b252017-11-21 17:08:48 +00003072 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc,
3073 EndLoc, AStmt);
Samuel Antao686c70c2016-05-26 17:30:50 +00003074 AllowedNameModifiers.push_back(OMPD_target_update);
3075 break;
Carlo Bertolli9925f152016-06-27 14:55:37 +00003076 case OMPD_distribute_parallel_for:
3077 Res = ActOnOpenMPDistributeParallelForDirective(
3078 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3079 AllowedNameModifiers.push_back(OMPD_parallel);
3080 break;
Kelvin Li4a39add2016-07-05 05:00:15 +00003081 case OMPD_distribute_parallel_for_simd:
3082 Res = ActOnOpenMPDistributeParallelForSimdDirective(
3083 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3084 AllowedNameModifiers.push_back(OMPD_parallel);
3085 break;
Kelvin Li787f3fc2016-07-06 04:45:38 +00003086 case OMPD_distribute_simd:
3087 Res = ActOnOpenMPDistributeSimdDirective(
3088 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3089 break;
Kelvin Lia579b912016-07-14 02:54:56 +00003090 case OMPD_target_parallel_for_simd:
3091 Res = ActOnOpenMPTargetParallelForSimdDirective(
3092 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3093 AllowedNameModifiers.push_back(OMPD_target);
3094 AllowedNameModifiers.push_back(OMPD_parallel);
3095 break;
Kelvin Li986330c2016-07-20 22:57:10 +00003096 case OMPD_target_simd:
3097 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3098 EndLoc, VarsWithInheritedDSA);
3099 AllowedNameModifiers.push_back(OMPD_target);
3100 break;
Kelvin Li02532872016-08-05 14:37:37 +00003101 case OMPD_teams_distribute:
David Majnemer9d168222016-08-05 17:44:54 +00003102 Res = ActOnOpenMPTeamsDistributeDirective(
3103 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Kelvin Li02532872016-08-05 14:37:37 +00003104 break;
Kelvin Li4e325f72016-10-25 12:50:55 +00003105 case OMPD_teams_distribute_simd:
3106 Res = ActOnOpenMPTeamsDistributeSimdDirective(
3107 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3108 break;
Kelvin Li579e41c2016-11-30 23:51:03 +00003109 case OMPD_teams_distribute_parallel_for_simd:
3110 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
3111 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3112 AllowedNameModifiers.push_back(OMPD_parallel);
3113 break;
Kelvin Li7ade93f2016-12-09 03:24:30 +00003114 case OMPD_teams_distribute_parallel_for:
3115 Res = ActOnOpenMPTeamsDistributeParallelForDirective(
3116 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3117 AllowedNameModifiers.push_back(OMPD_parallel);
3118 break;
Kelvin Libf594a52016-12-17 05:48:59 +00003119 case OMPD_target_teams:
3120 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc,
3121 EndLoc);
3122 AllowedNameModifiers.push_back(OMPD_target);
3123 break;
Kelvin Li83c451e2016-12-25 04:52:54 +00003124 case OMPD_target_teams_distribute:
3125 Res = ActOnOpenMPTargetTeamsDistributeDirective(
3126 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3127 AllowedNameModifiers.push_back(OMPD_target);
3128 break;
Kelvin Li80e8f562016-12-29 22:16:30 +00003129 case OMPD_target_teams_distribute_parallel_for:
3130 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
3131 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3132 AllowedNameModifiers.push_back(OMPD_target);
3133 AllowedNameModifiers.push_back(OMPD_parallel);
3134 break;
Kelvin Li1851df52017-01-03 05:23:48 +00003135 case OMPD_target_teams_distribute_parallel_for_simd:
3136 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
3137 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3138 AllowedNameModifiers.push_back(OMPD_target);
3139 AllowedNameModifiers.push_back(OMPD_parallel);
3140 break;
Kelvin Lida681182017-01-10 18:08:18 +00003141 case OMPD_target_teams_distribute_simd:
3142 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
3143 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3144 AllowedNameModifiers.push_back(OMPD_target);
3145 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003146 case OMPD_declare_target:
3147 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003148 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003149 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003150 case OMPD_declare_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003151 llvm_unreachable("OpenMP Directive is not allowed");
3152 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003153 llvm_unreachable("Unknown OpenMP directive");
3154 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003155
Alexey Bataev4acb8592014-07-07 13:01:15 +00003156 for (auto P : VarsWithInheritedDSA) {
3157 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3158 << P.first << P.second->getSourceRange();
3159 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003160 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3161
3162 if (!AllowedNameModifiers.empty())
3163 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3164 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003165
Alexey Bataeved09d242014-05-28 05:53:51 +00003166 if (ErrorFound)
3167 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003168 return Res;
3169}
3170
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003171Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3172 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00003173 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00003174 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3175 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003176 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00003177 assert(Linears.size() == LinModifiers.size());
3178 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00003179 if (!DG || DG.get().isNull())
3180 return DeclGroupPtrTy();
3181
3182 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00003183 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003184 return DG;
3185 }
3186 auto *ADecl = DG.get().getSingleDecl();
3187 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3188 ADecl = FTD->getTemplatedDecl();
3189
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003190 auto *FD = dyn_cast<FunctionDecl>(ADecl);
3191 if (!FD) {
3192 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003193 return DeclGroupPtrTy();
3194 }
3195
Alexey Bataev2af33e32016-04-07 12:45:37 +00003196 // OpenMP [2.8.2, declare simd construct, Description]
3197 // The parameter of the simdlen clause must be a constant positive integer
3198 // expression.
3199 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003200 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00003201 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003202 // OpenMP [2.8.2, declare simd construct, Description]
3203 // The special this pointer can be used as if was one of the arguments to the
3204 // function in any of the linear, aligned, or uniform clauses.
3205 // The uniform clause declares one or more arguments to have an invariant
3206 // value for all concurrent invocations of the function in the execution of a
3207 // single SIMD loop.
Alexey Bataevecba70f2016-04-12 11:02:11 +00003208 llvm::DenseMap<Decl *, Expr *> UniformedArgs;
3209 Expr *UniformedLinearThis = nullptr;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003210 for (auto *E : Uniforms) {
3211 E = E->IgnoreParenImpCasts();
3212 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3213 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
3214 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3215 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00003216 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
3217 UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E));
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003218 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003219 }
3220 if (isa<CXXThisExpr>(E)) {
3221 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003222 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003223 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003224 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3225 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00003226 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00003227 // OpenMP [2.8.2, declare simd construct, Description]
3228 // The aligned clause declares that the object to which each list item points
3229 // is aligned to the number of bytes expressed in the optional parameter of
3230 // the aligned clause.
3231 // The special this pointer can be used as if was one of the arguments to the
3232 // function in any of the linear, aligned, or uniform clauses.
3233 // The type of list items appearing in the aligned clause must be array,
3234 // pointer, reference to array, or reference to pointer.
3235 llvm::DenseMap<Decl *, Expr *> AlignedArgs;
3236 Expr *AlignedThis = nullptr;
3237 for (auto *E : Aligneds) {
3238 E = E->IgnoreParenImpCasts();
3239 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3240 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3241 auto *CanonPVD = PVD->getCanonicalDecl();
3242 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3243 FD->getParamDecl(PVD->getFunctionScopeIndex())
3244 ->getCanonicalDecl() == CanonPVD) {
3245 // OpenMP [2.8.1, simd construct, Restrictions]
3246 // A list-item cannot appear in more than one aligned clause.
3247 if (AlignedArgs.count(CanonPVD) > 0) {
3248 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3249 << 1 << E->getSourceRange();
3250 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3251 diag::note_omp_explicit_dsa)
3252 << getOpenMPClauseName(OMPC_aligned);
3253 continue;
3254 }
3255 AlignedArgs[CanonPVD] = E;
3256 QualType QTy = PVD->getType()
3257 .getNonReferenceType()
3258 .getUnqualifiedType()
3259 .getCanonicalType();
3260 const Type *Ty = QTy.getTypePtrOrNull();
3261 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3262 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3263 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3264 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3265 }
3266 continue;
3267 }
3268 }
3269 if (isa<CXXThisExpr>(E)) {
3270 if (AlignedThis) {
3271 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3272 << 2 << E->getSourceRange();
3273 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3274 << getOpenMPClauseName(OMPC_aligned);
3275 }
3276 AlignedThis = E;
3277 continue;
3278 }
3279 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3280 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3281 }
3282 // The optional parameter of the aligned clause, alignment, must be a constant
3283 // positive integer expression. If no optional parameter is specified,
3284 // implementation-defined default alignments for SIMD instructions on the
3285 // target platforms are assumed.
3286 SmallVector<Expr *, 4> NewAligns;
3287 for (auto *E : Alignments) {
3288 ExprResult Align;
3289 if (E)
3290 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3291 NewAligns.push_back(Align.get());
3292 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00003293 // OpenMP [2.8.2, declare simd construct, Description]
3294 // The linear clause declares one or more list items to be private to a SIMD
3295 // lane and to have a linear relationship with respect to the iteration space
3296 // of a loop.
3297 // The special this pointer can be used as if was one of the arguments to the
3298 // function in any of the linear, aligned, or uniform clauses.
3299 // When a linear-step expression is specified in a linear clause it must be
3300 // either a constant integer expression or an integer-typed parameter that is
3301 // specified in a uniform clause on the directive.
3302 llvm::DenseMap<Decl *, Expr *> LinearArgs;
3303 const bool IsUniformedThis = UniformedLinearThis != nullptr;
3304 auto MI = LinModifiers.begin();
3305 for (auto *E : Linears) {
3306 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3307 ++MI;
3308 E = E->IgnoreParenImpCasts();
3309 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3310 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3311 auto *CanonPVD = PVD->getCanonicalDecl();
3312 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3313 FD->getParamDecl(PVD->getFunctionScopeIndex())
3314 ->getCanonicalDecl() == CanonPVD) {
3315 // OpenMP [2.15.3.7, linear Clause, Restrictions]
3316 // A list-item cannot appear in more than one linear clause.
3317 if (LinearArgs.count(CanonPVD) > 0) {
3318 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3319 << getOpenMPClauseName(OMPC_linear)
3320 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3321 Diag(LinearArgs[CanonPVD]->getExprLoc(),
3322 diag::note_omp_explicit_dsa)
3323 << getOpenMPClauseName(OMPC_linear);
3324 continue;
3325 }
3326 // Each argument can appear in at most one uniform or linear clause.
3327 if (UniformedArgs.count(CanonPVD) > 0) {
3328 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3329 << getOpenMPClauseName(OMPC_linear)
3330 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3331 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3332 diag::note_omp_explicit_dsa)
3333 << getOpenMPClauseName(OMPC_uniform);
3334 continue;
3335 }
3336 LinearArgs[CanonPVD] = E;
3337 if (E->isValueDependent() || E->isTypeDependent() ||
3338 E->isInstantiationDependent() ||
3339 E->containsUnexpandedParameterPack())
3340 continue;
3341 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3342 PVD->getOriginalType());
3343 continue;
3344 }
3345 }
3346 if (isa<CXXThisExpr>(E)) {
3347 if (UniformedLinearThis) {
3348 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3349 << getOpenMPClauseName(OMPC_linear)
3350 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3351 << E->getSourceRange();
3352 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3353 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3354 : OMPC_linear);
3355 continue;
3356 }
3357 UniformedLinearThis = E;
3358 if (E->isValueDependent() || E->isTypeDependent() ||
3359 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3360 continue;
3361 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3362 E->getType());
3363 continue;
3364 }
3365 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3366 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3367 }
3368 Expr *Step = nullptr;
3369 Expr *NewStep = nullptr;
3370 SmallVector<Expr *, 4> NewSteps;
3371 for (auto *E : Steps) {
3372 // Skip the same step expression, it was checked already.
3373 if (Step == E || !E) {
3374 NewSteps.push_back(E ? NewStep : nullptr);
3375 continue;
3376 }
3377 Step = E;
3378 if (auto *DRE = dyn_cast<DeclRefExpr>(Step))
3379 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3380 auto *CanonPVD = PVD->getCanonicalDecl();
3381 if (UniformedArgs.count(CanonPVD) == 0) {
3382 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3383 << Step->getSourceRange();
3384 } else if (E->isValueDependent() || E->isTypeDependent() ||
3385 E->isInstantiationDependent() ||
3386 E->containsUnexpandedParameterPack() ||
3387 CanonPVD->getType()->hasIntegerRepresentation())
3388 NewSteps.push_back(Step);
3389 else {
3390 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3391 << Step->getSourceRange();
3392 }
3393 continue;
3394 }
3395 NewStep = Step;
3396 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3397 !Step->isInstantiationDependent() &&
3398 !Step->containsUnexpandedParameterPack()) {
3399 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3400 .get();
3401 if (NewStep)
3402 NewStep = VerifyIntegerConstantExpression(NewStep).get();
3403 }
3404 NewSteps.push_back(NewStep);
3405 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003406 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3407 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00003408 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00003409 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3410 const_cast<Expr **>(Linears.data()), Linears.size(),
3411 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3412 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003413 ADecl->addAttr(NewAttr);
3414 return ConvertDeclToDeclGroup(ADecl);
3415}
3416
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003417StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3418 Stmt *AStmt,
3419 SourceLocation StartLoc,
3420 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003421 if (!AStmt)
3422 return StmtError();
3423
Alexey Bataev9959db52014-05-06 10:08:46 +00003424 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3425 // 1.2.2 OpenMP Language Terminology
3426 // Structured block - An executable statement with a single entry at the
3427 // top and a single exit at the bottom.
3428 // The point of exit cannot be a branch out of the structured block.
3429 // longjmp() and throw() must not violate the entry/exit criteria.
3430 CS->getCapturedDecl()->setNothrow();
3431
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003432 getCurFunction()->setHasBranchProtectedScope();
3433
Alexey Bataev25e5b442015-09-15 12:52:43 +00003434 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3435 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003436}
3437
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003438namespace {
3439/// \brief Helper class for checking canonical form of the OpenMP loops and
3440/// extracting iteration space of each loop in the loop nest, that will be used
3441/// for IR generation.
3442class OpenMPIterationSpaceChecker {
3443 /// \brief Reference to Sema.
3444 Sema &SemaRef;
3445 /// \brief A location for diagnostics (when there is no some better location).
3446 SourceLocation DefaultLoc;
3447 /// \brief A location for diagnostics (when increment is not compatible).
3448 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003449 /// \brief A source location for referring to loop init later.
3450 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003451 /// \brief A source location for referring to condition later.
3452 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003453 /// \brief A source location for referring to increment later.
3454 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003455 /// \brief Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003456 ValueDecl *LCDecl = nullptr;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003457 /// \brief Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003458 Expr *LCRef = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003459 /// \brief Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003460 Expr *LB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003461 /// \brief Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003462 Expr *UB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003463 /// \brief Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003464 Expr *Step = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003465 /// \brief This flag is true when condition is one of:
3466 /// Var < UB
3467 /// Var <= UB
3468 /// UB > Var
3469 /// UB >= Var
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003470 bool TestIsLessOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003471 /// \brief This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003472 bool TestIsStrictOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003473 /// \brief This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003474 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003475
3476public:
3477 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003478 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003479 /// \brief Check init-expr for canonical loop form and save loop counter
3480 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00003481 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003482 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3483 /// for less/greater and for strict/non-strict comparison.
3484 bool CheckCond(Expr *S);
3485 /// \brief Check incr-expr for canonical loop form and return true if it
3486 /// does not conform, otherwise save loop step (#Step).
3487 bool CheckInc(Expr *S);
3488 /// \brief Return the loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003489 ValueDecl *GetLoopDecl() const { return LCDecl; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003490 /// \brief Return the reference expression to loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003491 Expr *GetLoopDeclRefExpr() const { return LCRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003492 /// \brief Source range of the loop init.
3493 SourceRange GetInitSrcRange() const { return InitSrcRange; }
3494 /// \brief Source range of the loop condition.
3495 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3496 /// \brief Source range of the loop increment.
3497 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3498 /// \brief True if the step should be subtracted.
3499 bool ShouldSubtractStep() const { return SubtractStep; }
3500 /// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003501 Expr *
3502 BuildNumIterations(Scope *S, const bool LimitedType,
3503 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003504 /// \brief Build the precondition expression for the loops.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003505 Expr *BuildPreCond(Scope *S, Expr *Cond,
3506 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003507 /// \brief Build reference expression to the counter be used for codegen.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00003508 DeclRefExpr *BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures,
3509 DSAStackTy &DSA) const;
Alexey Bataeva8899172015-08-06 12:30:57 +00003510 /// \brief Build reference expression to the private counter be used for
3511 /// codegen.
3512 Expr *BuildPrivateCounterVar() const;
David Majnemer9d168222016-08-05 17:44:54 +00003513 /// \brief Build initialization of the counter be used for codegen.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003514 Expr *BuildCounterInit() const;
3515 /// \brief Build step of the counter be used for codegen.
3516 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003517 /// \brief Return true if any expression is dependent.
3518 bool Dependent() const;
3519
3520private:
3521 /// \brief Check the right-hand side of an assignment in the increment
3522 /// expression.
3523 bool CheckIncRHS(Expr *RHS);
3524 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003525 bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003526 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00003527 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003528 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003529 /// \brief Helper to set loop increment.
3530 bool SetStep(Expr *NewStep, bool Subtract);
3531};
3532
3533bool OpenMPIterationSpaceChecker::Dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003534 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003535 assert(!LB && !UB && !Step);
3536 return false;
3537 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003538 return LCDecl->getType()->isDependentType() ||
3539 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
3540 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003541}
3542
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003543bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl,
3544 Expr *NewLCRefExpr,
3545 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003546 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003547 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003548 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003549 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003550 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003551 LCDecl = getCanonicalDecl(NewLCDecl);
3552 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003553 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3554 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003555 if ((Ctor->isCopyOrMoveConstructor() ||
3556 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3557 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003558 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003559 LB = NewLB;
3560 return false;
3561}
3562
3563bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00003564 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003565 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003566 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
3567 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003568 if (!NewUB)
3569 return true;
3570 UB = NewUB;
3571 TestIsLessOp = LessOp;
3572 TestIsStrictOp = StrictOp;
3573 ConditionSrcRange = SR;
3574 ConditionLoc = SL;
3575 return false;
3576}
3577
3578bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3579 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003580 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003581 if (!NewStep)
3582 return true;
3583 if (!NewStep->isValueDependent()) {
3584 // Check that the step is integer expression.
3585 SourceLocation StepLoc = NewStep->getLocStart();
Alexey Bataev5372fb82017-08-31 23:06:52 +00003586 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion(
3587 StepLoc, getExprAsWritten(NewStep));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003588 if (Val.isInvalid())
3589 return true;
3590 NewStep = Val.get();
3591
3592 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3593 // If test-expr is of form var relational-op b and relational-op is < or
3594 // <= then incr-expr must cause var to increase on each iteration of the
3595 // loop. If test-expr is of form var relational-op b and relational-op is
3596 // > or >= then incr-expr must cause var to decrease on each iteration of
3597 // the loop.
3598 // If test-expr is of form b relational-op var and relational-op is < or
3599 // <= then incr-expr must cause var to decrease on each iteration of the
3600 // loop. If test-expr is of form b relational-op var and relational-op is
3601 // > or >= then incr-expr must cause var to increase on each iteration of
3602 // the loop.
3603 llvm::APSInt Result;
3604 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3605 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3606 bool IsConstNeg =
3607 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003608 bool IsConstPos =
3609 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003610 bool IsConstZero = IsConstant && !Result.getBoolValue();
3611 if (UB && (IsConstZero ||
3612 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00003613 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003614 SemaRef.Diag(NewStep->getExprLoc(),
3615 diag::err_omp_loop_incr_not_compatible)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003616 << LCDecl << TestIsLessOp << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003617 SemaRef.Diag(ConditionLoc,
3618 diag::note_omp_loop_cond_requres_compatible_incr)
3619 << TestIsLessOp << ConditionSrcRange;
3620 return true;
3621 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003622 if (TestIsLessOp == Subtract) {
David Majnemer9d168222016-08-05 17:44:54 +00003623 NewStep =
3624 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep)
3625 .get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003626 Subtract = !Subtract;
3627 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003628 }
3629
3630 Step = NewStep;
3631 SubtractStep = Subtract;
3632 return false;
3633}
3634
Alexey Bataev9c821032015-04-30 04:23:23 +00003635bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003636 // Check init-expr for canonical loop form and save loop counter
3637 // variable - #Var and its initialization value - #LB.
3638 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3639 // var = lb
3640 // integer-type var = lb
3641 // random-access-iterator-type var = lb
3642 // pointer-type var = lb
3643 //
3644 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003645 if (EmitDiags) {
3646 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3647 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003648 return true;
3649 }
Tim Shen4a05bb82016-06-21 20:29:17 +00003650 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
3651 if (!ExprTemp->cleanupsHaveSideEffects())
3652 S = ExprTemp->getSubExpr();
3653
Alexander Musmana5f070a2014-10-01 06:03:56 +00003654 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003655 if (Expr *E = dyn_cast<Expr>(S))
3656 S = E->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00003657 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003658 if (BO->getOpcode() == BO_Assign) {
3659 auto *LHS = BO->getLHS()->IgnoreParens();
3660 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
3661 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3662 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3663 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3664 return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
3665 }
3666 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3667 if (ME->isArrow() &&
3668 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3669 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3670 }
3671 }
David Majnemer9d168222016-08-05 17:44:54 +00003672 } else if (auto *DS = dyn_cast<DeclStmt>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003673 if (DS->isSingleDecl()) {
David Majnemer9d168222016-08-05 17:44:54 +00003674 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00003675 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003676 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00003677 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003678 SemaRef.Diag(S->getLocStart(),
3679 diag::ext_omp_loop_not_canonical_init)
3680 << S->getSourceRange();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003681 return SetLCDeclAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003682 }
3683 }
3684 }
David Majnemer9d168222016-08-05 17:44:54 +00003685 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003686 if (CE->getOperator() == OO_Equal) {
3687 auto *LHS = CE->getArg(0);
David Majnemer9d168222016-08-05 17:44:54 +00003688 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003689 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3690 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3691 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3692 return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
3693 }
3694 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3695 if (ME->isArrow() &&
3696 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3697 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3698 }
3699 }
3700 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003701
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003702 if (Dependent() || SemaRef.CurContext->isDependentContext())
3703 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00003704 if (EmitDiags) {
3705 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3706 << S->getSourceRange();
3707 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003708 return true;
3709}
3710
Alexey Bataev23b69422014-06-18 07:08:49 +00003711/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003712/// variable (which may be the loop variable) if possible.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003713static const ValueDecl *GetInitLCDecl(Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003714 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00003715 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003716 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003717 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3718 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003719 if ((Ctor->isCopyOrMoveConstructor() ||
3720 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3721 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003722 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003723 if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
Alexey Bataev4d4624c2017-07-20 16:47:47 +00003724 if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003725 return getCanonicalDecl(VD);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003726 }
3727 if (auto *ME = dyn_cast_or_null<MemberExpr>(E))
3728 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3729 return getCanonicalDecl(ME->getMemberDecl());
3730 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003731}
3732
3733bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3734 // Check test-expr for canonical form, save upper-bound UB, flags for
3735 // less/greater and for strict/non-strict comparison.
3736 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3737 // var relational-op b
3738 // b relational-op var
3739 //
3740 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003741 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003742 return true;
3743 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003744 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003745 SourceLocation CondLoc = S->getLocStart();
David Majnemer9d168222016-08-05 17:44:54 +00003746 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003747 if (BO->isRelationalOp()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003748 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003749 return SetUB(BO->getRHS(),
3750 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3751 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3752 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003753 if (GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003754 return SetUB(BO->getLHS(),
3755 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3756 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3757 BO->getSourceRange(), BO->getOperatorLoc());
3758 }
David Majnemer9d168222016-08-05 17:44:54 +00003759 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003760 if (CE->getNumArgs() == 2) {
3761 auto Op = CE->getOperator();
3762 switch (Op) {
3763 case OO_Greater:
3764 case OO_GreaterEqual:
3765 case OO_Less:
3766 case OO_LessEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003767 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003768 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3769 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3770 CE->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003771 if (GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003772 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3773 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3774 CE->getOperatorLoc());
3775 break;
3776 default:
3777 break;
3778 }
3779 }
3780 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003781 if (Dependent() || SemaRef.CurContext->isDependentContext())
3782 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003783 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003784 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003785 return true;
3786}
3787
3788bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3789 // RHS of canonical loop form increment can be:
3790 // var + incr
3791 // incr + var
3792 // var - incr
3793 //
3794 RHS = RHS->IgnoreParenImpCasts();
David Majnemer9d168222016-08-05 17:44:54 +00003795 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003796 if (BO->isAdditiveOp()) {
3797 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003798 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003799 return SetStep(BO->getRHS(), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003800 if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003801 return SetStep(BO->getLHS(), false);
3802 }
David Majnemer9d168222016-08-05 17:44:54 +00003803 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003804 bool IsAdd = CE->getOperator() == OO_Plus;
3805 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003806 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003807 return SetStep(CE->getArg(1), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003808 if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003809 return SetStep(CE->getArg(0), false);
3810 }
3811 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003812 if (Dependent() || SemaRef.CurContext->isDependentContext())
3813 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003814 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003815 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003816 return true;
3817}
3818
3819bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3820 // Check incr-expr for canonical loop form and return true if it
3821 // does not conform.
3822 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3823 // ++var
3824 // var++
3825 // --var
3826 // var--
3827 // var += incr
3828 // var -= incr
3829 // var = var + incr
3830 // var = incr + var
3831 // var = var - incr
3832 //
3833 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003834 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003835 return true;
3836 }
Tim Shen4a05bb82016-06-21 20:29:17 +00003837 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
3838 if (!ExprTemp->cleanupsHaveSideEffects())
3839 S = ExprTemp->getSubExpr();
3840
Alexander Musmana5f070a2014-10-01 06:03:56 +00003841 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003842 S = S->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00003843 if (auto *UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003844 if (UO->isIncrementDecrementOp() &&
3845 GetInitLCDecl(UO->getSubExpr()) == LCDecl)
David Majnemer9d168222016-08-05 17:44:54 +00003846 return SetStep(SemaRef
3847 .ActOnIntegerConstant(UO->getLocStart(),
3848 (UO->isDecrementOp() ? -1 : 1))
3849 .get(),
3850 false);
3851 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003852 switch (BO->getOpcode()) {
3853 case BO_AddAssign:
3854 case BO_SubAssign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003855 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003856 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3857 break;
3858 case BO_Assign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003859 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003860 return CheckIncRHS(BO->getRHS());
3861 break;
3862 default:
3863 break;
3864 }
David Majnemer9d168222016-08-05 17:44:54 +00003865 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003866 switch (CE->getOperator()) {
3867 case OO_PlusPlus:
3868 case OO_MinusMinus:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003869 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
David Majnemer9d168222016-08-05 17:44:54 +00003870 return SetStep(SemaRef
3871 .ActOnIntegerConstant(
3872 CE->getLocStart(),
3873 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
3874 .get(),
3875 false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003876 break;
3877 case OO_PlusEqual:
3878 case OO_MinusEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003879 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003880 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3881 break;
3882 case OO_Equal:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003883 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003884 return CheckIncRHS(CE->getArg(1));
3885 break;
3886 default:
3887 break;
3888 }
3889 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003890 if (Dependent() || SemaRef.CurContext->isDependentContext())
3891 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003892 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003893 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003894 return true;
3895}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003896
Alexey Bataev5a3af132016-03-29 08:58:54 +00003897static ExprResult
3898tryBuildCapture(Sema &SemaRef, Expr *Capture,
3899 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00003900 if (SemaRef.CurContext->isDependentContext())
3901 return ExprResult(Capture);
Alexey Bataev5a3af132016-03-29 08:58:54 +00003902 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
3903 return SemaRef.PerformImplicitConversion(
3904 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
3905 /*AllowExplicit=*/true);
3906 auto I = Captures.find(Capture);
3907 if (I != Captures.end())
3908 return buildCapture(SemaRef, Capture, I->second);
3909 DeclRefExpr *Ref = nullptr;
3910 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
3911 Captures[Capture] = Ref;
3912 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003913}
3914
Alexander Musmana5f070a2014-10-01 06:03:56 +00003915/// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003916Expr *OpenMPIterationSpaceChecker::BuildNumIterations(
3917 Scope *S, const bool LimitedType,
3918 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003919 ExprResult Diff;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003920 auto VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003921 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003922 SemaRef.getLangOpts().CPlusPlus) {
3923 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003924 auto *UBExpr = TestIsLessOp ? UB : LB;
3925 auto *LBExpr = TestIsLessOp ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00003926 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
3927 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003928 if (!Upper || !Lower)
3929 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003930
3931 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3932
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003933 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003934 // BuildBinOp already emitted error, this one is to point user to upper
3935 // and lower bound, and to tell what is passed to 'operator-'.
3936 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3937 << Upper->getSourceRange() << Lower->getSourceRange();
3938 return nullptr;
3939 }
3940 }
3941
3942 if (!Diff.isUsable())
3943 return nullptr;
3944
3945 // Upper - Lower [- 1]
3946 if (TestIsStrictOp)
3947 Diff = SemaRef.BuildBinOp(
3948 S, DefaultLoc, BO_Sub, Diff.get(),
3949 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3950 if (!Diff.isUsable())
3951 return nullptr;
3952
3953 // Upper - Lower [- 1] + Step
Alexey Bataev5a3af132016-03-29 08:58:54 +00003954 auto NewStep = tryBuildCapture(SemaRef, Step, Captures);
3955 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003956 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003957 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003958 if (!Diff.isUsable())
3959 return nullptr;
3960
3961 // Parentheses (for dumping/debugging purposes only).
3962 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3963 if (!Diff.isUsable())
3964 return nullptr;
3965
3966 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003967 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003968 if (!Diff.isUsable())
3969 return nullptr;
3970
Alexander Musman174b3ca2014-10-06 11:16:29 +00003971 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003972 QualType Type = Diff.get()->getType();
3973 auto &C = SemaRef.Context;
3974 bool UseVarType = VarType->hasIntegerRepresentation() &&
3975 C.getTypeSize(Type) > C.getTypeSize(VarType);
3976 if (!Type->isIntegerType() || UseVarType) {
3977 unsigned NewSize =
3978 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
3979 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
3980 : Type->hasSignedIntegerRepresentation();
3981 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00003982 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
3983 Diff = SemaRef.PerformImplicitConversion(
3984 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
3985 if (!Diff.isUsable())
3986 return nullptr;
3987 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003988 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003989 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00003990 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
3991 if (NewSize != C.getTypeSize(Type)) {
3992 if (NewSize < C.getTypeSize(Type)) {
3993 assert(NewSize == 64 && "incorrect loop var size");
3994 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
3995 << InitSrcRange << ConditionSrcRange;
3996 }
3997 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003998 NewSize, Type->hasSignedIntegerRepresentation() ||
3999 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00004000 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4001 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4002 Sema::AA_Converting, true);
4003 if (!Diff.isUsable())
4004 return nullptr;
4005 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004006 }
4007 }
4008
Alexander Musmana5f070a2014-10-01 06:03:56 +00004009 return Diff.get();
4010}
4011
Alexey Bataev5a3af132016-03-29 08:58:54 +00004012Expr *OpenMPIterationSpaceChecker::BuildPreCond(
4013 Scope *S, Expr *Cond,
4014 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004015 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4016 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4017 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004018
Alexey Bataev5a3af132016-03-29 08:58:54 +00004019 auto NewLB = tryBuildCapture(SemaRef, LB, Captures);
4020 auto NewUB = tryBuildCapture(SemaRef, UB, Captures);
4021 if (!NewLB.isUsable() || !NewUB.isUsable())
4022 return nullptr;
4023
Alexey Bataev62dbb972015-04-22 11:59:37 +00004024 auto CondExpr = SemaRef.BuildBinOp(
4025 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
4026 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004027 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004028 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004029 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4030 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00004031 CondExpr = SemaRef.PerformImplicitConversion(
4032 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4033 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004034 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00004035 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4036 // Otherwise use original loop conditon and evaluate it in runtime.
4037 return CondExpr.isUsable() ? CondExpr.get() : Cond;
4038}
4039
Alexander Musmana5f070a2014-10-01 06:03:56 +00004040/// \brief Build reference expression to the counter be used for codegen.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004041DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar(
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004042 llvm::MapVector<Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004043 auto *VD = dyn_cast<VarDecl>(LCDecl);
4044 if (!VD) {
4045 VD = SemaRef.IsOpenMPCapturedDecl(LCDecl);
4046 auto *Ref = buildDeclRefExpr(
4047 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004048 DSAStackTy::DSAVarData Data = DSA.getTopDSA(LCDecl, /*FromParent=*/false);
4049 // If the loop control decl is explicitly marked as private, do not mark it
4050 // as captured again.
4051 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4052 Captures.insert(std::make_pair(LCRef, Ref));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004053 return Ref;
4054 }
4055 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00004056 DefaultLoc);
4057}
4058
4059Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004060 if (LCDecl && !LCDecl->isInvalidDecl()) {
4061 auto Type = LCDecl->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00004062 auto *PrivateVar =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004063 buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(),
4064 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00004065 if (PrivateVar->isInvalidDecl())
4066 return nullptr;
4067 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4068 }
4069 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004070}
4071
Samuel Antao4c8035b2016-12-12 18:00:20 +00004072/// \brief Build initialization of the counter to be used for codegen.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004073Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
4074
4075/// \brief Build step of the counter be used for codegen.
4076Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
4077
4078/// \brief Iteration space of a single for loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004079struct LoopIterationSpace final {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004080 /// \brief Condition of the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004081 Expr *PreCond = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004082 /// \brief This expression calculates the number of iterations in the loop.
4083 /// It is always possible to calculate it before starting the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004084 Expr *NumIterations = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004085 /// \brief The loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004086 Expr *CounterVar = nullptr;
Alexey Bataeva8899172015-08-06 12:30:57 +00004087 /// \brief Private loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004088 Expr *PrivateCounterVar = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004089 /// \brief This is initializer for the initial value of #CounterVar.
Alexey Bataev8b427062016-05-25 12:36:08 +00004090 Expr *CounterInit = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004091 /// \brief This is step for the #CounterVar used to generate its update:
4092 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
Alexey Bataev8b427062016-05-25 12:36:08 +00004093 Expr *CounterStep = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004094 /// \brief Should step be subtracted?
Alexey Bataev8b427062016-05-25 12:36:08 +00004095 bool Subtract = false;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004096 /// \brief Source range of the loop init.
4097 SourceRange InitSrcRange;
4098 /// \brief Source range of the loop condition.
4099 SourceRange CondSrcRange;
4100 /// \brief Source range of the loop increment.
4101 SourceRange IncSrcRange;
4102};
4103
Alexey Bataev23b69422014-06-18 07:08:49 +00004104} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004105
Alexey Bataev9c821032015-04-30 04:23:23 +00004106void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4107 assert(getLangOpts().OpenMP && "OpenMP is not active.");
4108 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004109 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4110 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00004111 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4112 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004113 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
4114 if (auto *D = ISC.GetLoopDecl()) {
4115 auto *VD = dyn_cast<VarDecl>(D);
4116 if (!VD) {
4117 if (auto *Private = IsOpenMPCapturedDecl(D))
4118 VD = Private;
4119 else {
4120 auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(),
4121 /*WithInit=*/false);
4122 VD = cast<VarDecl>(Ref->getDecl());
4123 }
4124 }
4125 DSAStack->addLoopControlVariable(D, VD);
4126 }
4127 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004128 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00004129 }
4130}
4131
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004132/// \brief Called on a for stmt to check and extract its iteration space
4133/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00004134static bool CheckOpenMPIterationSpace(
4135 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4136 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004137 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004138 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004139 LoopIterationSpace &ResultIterSpace,
4140 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004141 // OpenMP [2.6, Canonical Loop Form]
4142 // for (init-expr; test-expr; incr-expr) structured-block
David Majnemer9d168222016-08-05 17:44:54 +00004143 auto *For = dyn_cast_or_null<ForStmt>(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004144 if (!For) {
4145 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004146 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
4147 << getOpenMPDirectiveName(DKind) << NestedLoopCount
4148 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
4149 if (NestedLoopCount > 1) {
4150 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4151 SemaRef.Diag(DSA.getConstructLoc(),
4152 diag::note_omp_collapse_ordered_expr)
4153 << 2 << CollapseLoopCountExpr->getSourceRange()
4154 << OrderedLoopCountExpr->getSourceRange();
4155 else if (CollapseLoopCountExpr)
4156 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4157 diag::note_omp_collapse_ordered_expr)
4158 << 0 << CollapseLoopCountExpr->getSourceRange();
4159 else
4160 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4161 diag::note_omp_collapse_ordered_expr)
4162 << 1 << OrderedLoopCountExpr->getSourceRange();
4163 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004164 return true;
4165 }
4166 assert(For->getBody());
4167
4168 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4169
4170 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00004171 auto Init = For->getInit();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004172 if (ISC.CheckInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004173 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004174
4175 bool HasErrors = false;
4176
4177 // Check loop variable's type.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004178 if (auto *LCDecl = ISC.GetLoopDecl()) {
4179 auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004180
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004181 // OpenMP [2.6, Canonical Loop Form]
4182 // Var is one of the following:
4183 // A variable of signed or unsigned integer type.
4184 // For C++, a variable of a random access iterator type.
4185 // For C, a variable of a pointer type.
4186 auto VarType = LCDecl->getType().getNonReferenceType();
4187 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4188 !VarType->isPointerType() &&
4189 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
4190 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
4191 << SemaRef.getLangOpts().CPlusPlus;
4192 HasErrors = true;
4193 }
4194
4195 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4196 // a Construct
4197 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4198 // parallel for construct is (are) private.
4199 // The loop iteration variable in the associated for-loop of a simd
4200 // construct with just one associated for-loop is linear with a
4201 // constant-linear-step that is the increment of the associated for-loop.
4202 // Exclude loop var from the list of variables with implicitly defined data
4203 // sharing attributes.
4204 VarsWithImplicitDSA.erase(LCDecl);
4205
4206 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4207 // in a Construct, C/C++].
4208 // The loop iteration variable in the associated for-loop of a simd
4209 // construct with just one associated for-loop may be listed in a linear
4210 // clause with a constant-linear-step that is the increment of the
4211 // associated for-loop.
4212 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4213 // parallel for construct may be listed in a private or lastprivate clause.
4214 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4215 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4216 // declared in the loop and it is predetermined as a private.
4217 auto PredeterminedCKind =
4218 isOpenMPSimdDirective(DKind)
4219 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4220 : OMPC_private;
4221 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4222 DVar.CKind != PredeterminedCKind) ||
4223 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4224 isOpenMPDistributeDirective(DKind)) &&
4225 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4226 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4227 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4228 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
4229 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4230 << getOpenMPClauseName(PredeterminedCKind);
4231 if (DVar.RefExpr == nullptr)
4232 DVar.CKind = PredeterminedCKind;
4233 ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
4234 HasErrors = true;
4235 } else if (LoopDeclRefExpr != nullptr) {
4236 // Make the loop iteration variable private (for worksharing constructs),
4237 // linear (for simd directives with the only one associated loop) or
4238 // lastprivate (for simd directives with several collapsed or ordered
4239 // loops).
4240 if (DVar.CKind == OMPC_unknown)
Alexey Bataev7ace49d2016-05-17 08:55:33 +00004241 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
4242 [](OpenMPDirectiveKind) -> bool { return true; },
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004243 /*FromParent=*/false);
4244 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4245 }
4246
4247 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4248
4249 // Check test-expr.
4250 HasErrors |= ISC.CheckCond(For->getCond());
4251
4252 // Check incr-expr.
4253 HasErrors |= ISC.CheckInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004254 }
4255
Alexander Musmana5f070a2014-10-01 06:03:56 +00004256 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004257 return HasErrors;
4258
Alexander Musmana5f070a2014-10-01 06:03:56 +00004259 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004260 ResultIterSpace.PreCond =
4261 ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures);
Alexander Musman174b3ca2014-10-06 11:16:29 +00004262 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004263 DSA.getCurScope(),
4264 (isOpenMPWorksharingDirective(DKind) ||
4265 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4266 Captures);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004267 ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures, DSA);
Alexey Bataeva8899172015-08-06 12:30:57 +00004268 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004269 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
4270 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
4271 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
4272 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4273 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4274 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4275
Alexey Bataev62dbb972015-04-22 11:59:37 +00004276 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4277 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004278 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004279 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004280 ResultIterSpace.CounterInit == nullptr ||
4281 ResultIterSpace.CounterStep == nullptr);
4282
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004283 return HasErrors;
4284}
4285
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004286/// \brief Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004287static ExprResult
4288BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4289 ExprResult Start,
4290 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004291 // Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004292 auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
4293 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004294 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004295 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00004296 VarRef.get()->getType())) {
4297 NewStart = SemaRef.PerformImplicitConversion(
4298 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4299 /*AllowExplicit=*/true);
4300 if (!NewStart.isUsable())
4301 return ExprError();
4302 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004303
4304 auto Init =
4305 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4306 return Init;
4307}
4308
Alexander Musmana5f070a2014-10-01 06:03:56 +00004309/// \brief Build 'VarRef = Start + Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004310static ExprResult
4311BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc,
4312 ExprResult VarRef, ExprResult Start, ExprResult Iter,
4313 ExprResult Step, bool Subtract,
4314 llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004315 // Add parentheses (for debugging purposes only).
4316 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4317 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4318 !Step.isUsable())
4319 return ExprError();
4320
Alexey Bataev5a3af132016-03-29 08:58:54 +00004321 ExprResult NewStep = Step;
4322 if (Captures)
4323 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004324 if (NewStep.isInvalid())
4325 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004326 ExprResult Update =
4327 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004328 if (!Update.isUsable())
4329 return ExprError();
4330
Alexey Bataevc0214e02016-02-16 12:13:49 +00004331 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4332 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004333 ExprResult NewStart = Start;
4334 if (Captures)
4335 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004336 if (NewStart.isInvalid())
4337 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004338
Alexey Bataevc0214e02016-02-16 12:13:49 +00004339 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4340 ExprResult SavedUpdate = Update;
4341 ExprResult UpdateVal;
4342 if (VarRef.get()->getType()->isOverloadableType() ||
4343 NewStart.get()->getType()->isOverloadableType() ||
4344 Update.get()->getType()->isOverloadableType()) {
4345 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4346 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4347 Update =
4348 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4349 if (Update.isUsable()) {
4350 UpdateVal =
4351 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4352 VarRef.get(), SavedUpdate.get());
4353 if (UpdateVal.isUsable()) {
4354 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4355 UpdateVal.get());
4356 }
4357 }
4358 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4359 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004360
Alexey Bataevc0214e02016-02-16 12:13:49 +00004361 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4362 if (!Update.isUsable() || !UpdateVal.isUsable()) {
4363 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4364 NewStart.get(), SavedUpdate.get());
4365 if (!Update.isUsable())
4366 return ExprError();
4367
Alexey Bataev11481f52016-02-17 10:29:05 +00004368 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4369 VarRef.get()->getType())) {
4370 Update = SemaRef.PerformImplicitConversion(
4371 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4372 if (!Update.isUsable())
4373 return ExprError();
4374 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00004375
4376 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4377 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004378 return Update;
4379}
4380
4381/// \brief Convert integer expression \a E to make it have at least \a Bits
4382/// bits.
David Majnemer9d168222016-08-05 17:44:54 +00004383static ExprResult WidenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004384 if (E == nullptr)
4385 return ExprError();
4386 auto &C = SemaRef.Context;
4387 QualType OldType = E->getType();
4388 unsigned HasBits = C.getTypeSize(OldType);
4389 if (HasBits >= Bits)
4390 return ExprResult(E);
4391 // OK to convert to signed, because new type has more bits than old.
4392 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4393 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4394 true);
4395}
4396
4397/// \brief Check if the given expression \a E is a constant integer that fits
4398/// into \a Bits bits.
4399static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4400 if (E == nullptr)
4401 return false;
4402 llvm::APSInt Result;
4403 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4404 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4405 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004406}
4407
Alexey Bataev5a3af132016-03-29 08:58:54 +00004408/// Build preinits statement for the given declarations.
4409static Stmt *buildPreInits(ASTContext &Context,
Alexey Bataevc5514062017-10-25 15:44:52 +00004410 MutableArrayRef<Decl *> PreInits) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004411 if (!PreInits.empty()) {
4412 return new (Context) DeclStmt(
4413 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
4414 SourceLocation(), SourceLocation());
4415 }
4416 return nullptr;
4417}
4418
4419/// Build preinits statement for the given declarations.
Alexey Bataevc5514062017-10-25 15:44:52 +00004420static Stmt *
4421buildPreInits(ASTContext &Context,
4422 const llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004423 if (!Captures.empty()) {
4424 SmallVector<Decl *, 16> PreInits;
4425 for (auto &Pair : Captures)
4426 PreInits.push_back(Pair.second->getDecl());
4427 return buildPreInits(Context, PreInits);
4428 }
4429 return nullptr;
4430}
4431
4432/// Build postupdate expression for the given list of postupdates expressions.
4433static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
4434 Expr *PostUpdate = nullptr;
4435 if (!PostUpdates.empty()) {
4436 for (auto *E : PostUpdates) {
4437 Expr *ConvE = S.BuildCStyleCastExpr(
4438 E->getExprLoc(),
4439 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
4440 E->getExprLoc(), E)
4441 .get();
4442 PostUpdate = PostUpdate
4443 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
4444 PostUpdate, ConvE)
4445 .get()
4446 : ConvE;
4447 }
4448 }
4449 return PostUpdate;
4450}
4451
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004452/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00004453/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4454/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004455static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00004456CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
4457 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4458 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004459 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00004460 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004461 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004462 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004463 // Found 'collapse' clause - calculate collapse number.
4464 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004465 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004466 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004467 }
4468 if (OrderedLoopCountExpr) {
4469 // Found 'ordered' clause - calculate collapse number.
4470 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004471 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4472 if (Result.getLimitedValue() < NestedLoopCount) {
4473 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4474 diag::err_omp_wrong_ordered_loop_count)
4475 << OrderedLoopCountExpr->getSourceRange();
4476 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4477 diag::note_collapse_loop_count)
4478 << CollapseLoopCountExpr->getSourceRange();
4479 }
4480 NestedLoopCount = Result.getLimitedValue();
4481 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004482 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004483 // This is helper routine for loop directives (e.g., 'for', 'simd',
4484 // 'for simd', etc.).
Alexey Bataev5a3af132016-03-29 08:58:54 +00004485 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004486 SmallVector<LoopIterationSpace, 4> IterSpaces;
4487 IterSpaces.resize(NestedLoopCount);
4488 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004489 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004490 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004491 NestedLoopCount, CollapseLoopCountExpr,
4492 OrderedLoopCountExpr, VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004493 IterSpaces[Cnt], Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004494 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004495 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004496 // OpenMP [2.8.1, simd construct, Restrictions]
4497 // All loops associated with the construct must be perfectly nested; that
4498 // is, there must be no intervening code nor any OpenMP directive between
4499 // any two loops.
4500 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004501 }
4502
Alexander Musmana5f070a2014-10-01 06:03:56 +00004503 Built.clear(/* size */ NestedLoopCount);
4504
4505 if (SemaRef.CurContext->isDependentContext())
4506 return NestedLoopCount;
4507
4508 // An example of what is generated for the following code:
4509 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004510 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004511 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004512 // for (k = 0; k < NK; ++k)
4513 // for (j = J0; j < NJ; j+=2) {
4514 // <loop body>
4515 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004516 //
4517 // We generate the code below.
4518 // Note: the loop body may be outlined in CodeGen.
4519 // Note: some counters may be C++ classes, operator- is used to find number of
4520 // iterations and operator+= to calculate counter value.
4521 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4522 // or i64 is currently supported).
4523 //
4524 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4525 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4526 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4527 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4528 // // similar updates for vars in clauses (e.g. 'linear')
4529 // <loop body (using local i and j)>
4530 // }
4531 // i = NI; // assign final values of counters
4532 // j = NJ;
4533 //
4534
4535 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4536 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004537 // Precondition tests if there is at least one iteration (all conditions are
4538 // true).
4539 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004540 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004541 ExprResult LastIteration32 = WidenIterationCount(
David Majnemer9d168222016-08-05 17:44:54 +00004542 32 /* Bits */, SemaRef
4543 .PerformImplicitConversion(
4544 N0->IgnoreImpCasts(), N0->getType(),
4545 Sema::AA_Converting, /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004546 .get(),
4547 SemaRef);
4548 ExprResult LastIteration64 = WidenIterationCount(
David Majnemer9d168222016-08-05 17:44:54 +00004549 64 /* Bits */, SemaRef
4550 .PerformImplicitConversion(
4551 N0->IgnoreImpCasts(), N0->getType(),
4552 Sema::AA_Converting, /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004553 .get(),
4554 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004555
4556 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4557 return NestedLoopCount;
4558
4559 auto &C = SemaRef.Context;
4560 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4561
4562 Scope *CurScope = DSA.getCurScope();
4563 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004564 if (PreCond.isUsable()) {
Alexey Bataeva7206b92016-12-20 16:51:02 +00004565 PreCond =
4566 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd,
4567 PreCond.get(), IterSpaces[Cnt].PreCond);
Alexey Bataev62dbb972015-04-22 11:59:37 +00004568 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004569 auto N = IterSpaces[Cnt].NumIterations;
Alexey Bataeva7206b92016-12-20 16:51:02 +00004570 SourceLocation Loc = N->getExprLoc();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004571 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4572 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004573 LastIteration32 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00004574 CurScope, Loc, BO_Mul, LastIteration32.get(),
David Majnemer9d168222016-08-05 17:44:54 +00004575 SemaRef
4576 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4577 Sema::AA_Converting,
4578 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004579 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004580 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004581 LastIteration64 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00004582 CurScope, Loc, BO_Mul, LastIteration64.get(),
David Majnemer9d168222016-08-05 17:44:54 +00004583 SemaRef
4584 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4585 Sema::AA_Converting,
4586 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004587 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004588 }
4589
4590 // Choose either the 32-bit or 64-bit version.
4591 ExprResult LastIteration = LastIteration64;
4592 if (LastIteration32.isUsable() &&
4593 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4594 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4595 FitsInto(
4596 32 /* Bits */,
4597 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4598 LastIteration64.get(), SemaRef)))
4599 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00004600 QualType VType = LastIteration.get()->getType();
4601 QualType RealVType = VType;
4602 QualType StrideVType = VType;
4603 if (isOpenMPTaskLoopDirective(DKind)) {
4604 VType =
4605 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
4606 StrideVType =
4607 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
4608 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004609
4610 if (!LastIteration.isUsable())
4611 return 0;
4612
4613 // Save the number of iterations.
4614 ExprResult NumIterations = LastIteration;
4615 {
4616 LastIteration = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00004617 CurScope, LastIteration.get()->getExprLoc(), BO_Sub,
4618 LastIteration.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00004619 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4620 if (!LastIteration.isUsable())
4621 return 0;
4622 }
4623
4624 // Calculate the last iteration number beforehand instead of doing this on
4625 // each iteration. Do not do this if the number of iterations may be kfold-ed.
4626 llvm::APSInt Result;
4627 bool IsConstant =
4628 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4629 ExprResult CalcLastIteration;
4630 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004631 ExprResult SaveRef =
4632 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004633 LastIteration = SaveRef;
4634
4635 // Prepare SaveRef + 1.
4636 NumIterations = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00004637 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00004638 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4639 if (!NumIterations.isUsable())
4640 return 0;
4641 }
4642
4643 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4644
David Majnemer9d168222016-08-05 17:44:54 +00004645 // Build variables passed into runtime, necessary for worksharing directives.
Carlo Bertolliffafe102017-04-20 00:39:39 +00004646 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004647 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4648 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004649 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004650 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4651 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00004652 SemaRef.AddInitializerToDecl(LBDecl,
4653 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4654 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00004655
4656 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004657 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4658 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004659 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
Richard Smith3beb7c62017-01-12 02:27:38 +00004660 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00004661
4662 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4663 // This will be used to implement clause 'lastprivate'.
4664 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00004665 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4666 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00004667 SemaRef.AddInitializerToDecl(ILDecl,
4668 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4669 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00004670
4671 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00004672 VarDecl *STDecl =
4673 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
4674 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00004675 SemaRef.AddInitializerToDecl(STDecl,
4676 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4677 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00004678
4679 // Build expression: UB = min(UB, LastIteration)
David Majnemer9d168222016-08-05 17:44:54 +00004680 // It is necessary for CodeGen of directives with static scheduling.
Alexander Musmanc6388682014-12-15 07:07:06 +00004681 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4682 UB.get(), LastIteration.get());
4683 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4684 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4685 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4686 CondOp.get());
4687 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
Carlo Bertolli9925f152016-06-27 14:55:37 +00004688
4689 // If we have a combined directive that combines 'distribute', 'for' or
4690 // 'simd' we need to be able to access the bounds of the schedule of the
4691 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
4692 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
4693 if (isOpenMPLoopBoundSharingDirective(DKind)) {
Carlo Bertolli9925f152016-06-27 14:55:37 +00004694
Carlo Bertolliffafe102017-04-20 00:39:39 +00004695 // Lower bound variable, initialized with zero.
4696 VarDecl *CombLBDecl =
4697 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb");
4698 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc);
4699 SemaRef.AddInitializerToDecl(
4700 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4701 /*DirectInit*/ false);
4702
4703 // Upper bound variable, initialized with last iteration number.
4704 VarDecl *CombUBDecl =
4705 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub");
4706 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc);
4707 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(),
4708 /*DirectInit*/ false);
4709
4710 ExprResult CombIsUBGreater = SemaRef.BuildBinOp(
4711 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get());
4712 ExprResult CombCondOp =
4713 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(),
4714 LastIteration.get(), CombUB.get());
4715 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(),
4716 CombCondOp.get());
4717 CombEUB = SemaRef.ActOnFinishFullExpr(CombEUB.get());
4718
4719 auto *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
Carlo Bertolli9925f152016-06-27 14:55:37 +00004720 // We expect to have at least 2 more parameters than the 'parallel'
4721 // directive does - the lower and upper bounds of the previous schedule.
4722 assert(CD->getNumParams() >= 4 &&
4723 "Unexpected number of parameters in loop combined directive");
4724
4725 // Set the proper type for the bounds given what we learned from the
4726 // enclosed loops.
4727 auto *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
4728 auto *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
4729
4730 // Previous lower and upper bounds are obtained from the region
4731 // parameters.
4732 PrevLB =
4733 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
4734 PrevUB =
4735 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
4736 }
Alexander Musmanc6388682014-12-15 07:07:06 +00004737 }
4738
4739 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004740 ExprResult IV;
Carlo Bertolliffafe102017-04-20 00:39:39 +00004741 ExprResult Init, CombInit;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004742 {
Alexey Bataev7292c292016-04-25 12:22:29 +00004743 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
4744 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
David Majnemer9d168222016-08-05 17:44:54 +00004745 Expr *RHS =
4746 (isOpenMPWorksharingDirective(DKind) ||
4747 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
4748 ? LB.get()
4749 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004750 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4751 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Carlo Bertolliffafe102017-04-20 00:39:39 +00004752
4753 if (isOpenMPLoopBoundSharingDirective(DKind)) {
4754 Expr *CombRHS =
4755 (isOpenMPWorksharingDirective(DKind) ||
4756 isOpenMPTaskLoopDirective(DKind) ||
4757 isOpenMPDistributeDirective(DKind))
4758 ? CombLB.get()
4759 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4760 CombInit =
4761 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS);
4762 CombInit = SemaRef.ActOnFinishFullExpr(CombInit.get());
4763 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004764 }
4765
Alexander Musmanc6388682014-12-15 07:07:06 +00004766 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004767 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00004768 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004769 (isOpenMPWorksharingDirective(DKind) ||
4770 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004771 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4772 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4773 NumIterations.get());
Carlo Bertolliffafe102017-04-20 00:39:39 +00004774 ExprResult CombCond;
4775 if (isOpenMPLoopBoundSharingDirective(DKind)) {
4776 CombCond =
4777 SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), CombUB.get());
4778 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004779 // Loop increment (IV = IV + 1)
4780 SourceLocation IncLoc;
4781 ExprResult Inc =
4782 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4783 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4784 if (!Inc.isUsable())
4785 return 0;
4786 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00004787 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4788 if (!Inc.isUsable())
4789 return 0;
4790
4791 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4792 // Used for directives with static scheduling.
Carlo Bertolliffafe102017-04-20 00:39:39 +00004793 // In combined construct, add combined version that use CombLB and CombUB
4794 // base variables for the update
4795 ExprResult NextLB, NextUB, CombNextLB, CombNextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004796 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4797 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004798 // LB + ST
4799 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4800 if (!NextLB.isUsable())
4801 return 0;
4802 // LB = LB + ST
4803 NextLB =
4804 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4805 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4806 if (!NextLB.isUsable())
4807 return 0;
4808 // UB + ST
4809 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4810 if (!NextUB.isUsable())
4811 return 0;
4812 // UB = UB + ST
4813 NextUB =
4814 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4815 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4816 if (!NextUB.isUsable())
4817 return 0;
Carlo Bertolliffafe102017-04-20 00:39:39 +00004818 if (isOpenMPLoopBoundSharingDirective(DKind)) {
4819 CombNextLB =
4820 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get());
4821 if (!NextLB.isUsable())
4822 return 0;
4823 // LB = LB + ST
4824 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(),
4825 CombNextLB.get());
4826 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get());
4827 if (!CombNextLB.isUsable())
4828 return 0;
4829 // UB + ST
4830 CombNextUB =
4831 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get());
4832 if (!CombNextUB.isUsable())
4833 return 0;
4834 // UB = UB + ST
4835 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(),
4836 CombNextUB.get());
4837 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get());
4838 if (!CombNextUB.isUsable())
4839 return 0;
4840 }
Alexander Musmanc6388682014-12-15 07:07:06 +00004841 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004842
Carlo Bertolliffafe102017-04-20 00:39:39 +00004843 // Create increment expression for distribute loop when combined in a same
Carlo Bertolli8429d812017-02-17 21:29:13 +00004844 // directive with for as IV = IV + ST; ensure upper bound expression based
4845 // on PrevUB instead of NumIterations - used to implement 'for' when found
4846 // in combination with 'distribute', like in 'distribute parallel for'
4847 SourceLocation DistIncLoc;
4848 ExprResult DistCond, DistInc, PrevEUB;
4849 if (isOpenMPLoopBoundSharingDirective(DKind)) {
4850 DistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get());
4851 assert(DistCond.isUsable() && "distribute cond expr was not built");
4852
4853 DistInc =
4854 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get());
4855 assert(DistInc.isUsable() && "distribute inc expr was not built");
4856 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(),
4857 DistInc.get());
4858 DistInc = SemaRef.ActOnFinishFullExpr(DistInc.get());
4859 assert(DistInc.isUsable() && "distribute inc expr was not built");
4860
4861 // Build expression: UB = min(UB, prevUB) for #for in composite or combined
4862 // construct
4863 SourceLocation DistEUBLoc;
4864 ExprResult IsUBGreater =
4865 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get());
4866 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4867 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get());
4868 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(),
4869 CondOp.get());
4870 PrevEUB = SemaRef.ActOnFinishFullExpr(PrevEUB.get());
4871 }
4872
Alexander Musmana5f070a2014-10-01 06:03:56 +00004873 // Build updates and final values of the loop counters.
4874 bool HasErrors = false;
4875 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004876 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004877 Built.Updates.resize(NestedLoopCount);
4878 Built.Finals.resize(NestedLoopCount);
Alexey Bataev8b427062016-05-25 12:36:08 +00004879 SmallVector<Expr *, 4> LoopMultipliers;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004880 {
4881 ExprResult Div;
4882 // Go from inner nested loop to outer.
4883 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4884 LoopIterationSpace &IS = IterSpaces[Cnt];
4885 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4886 // Build: Iter = (IV / Div) % IS.NumIters
4887 // where Div is product of previous iterations' IS.NumIters.
4888 ExprResult Iter;
4889 if (Div.isUsable()) {
4890 Iter =
4891 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4892 } else {
4893 Iter = IV;
4894 assert((Cnt == (int)NestedLoopCount - 1) &&
4895 "unusable div expected on first iteration only");
4896 }
4897
4898 if (Cnt != 0 && Iter.isUsable())
4899 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4900 IS.NumIterations);
4901 if (!Iter.isUsable()) {
4902 HasErrors = true;
4903 break;
4904 }
4905
Alexey Bataev39f915b82015-05-08 10:41:21 +00004906 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004907 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
4908 auto *CounterVar = buildDeclRefExpr(SemaRef, VD, IS.CounterVar->getType(),
4909 IS.CounterVar->getExprLoc(),
4910 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004911 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004912 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004913 if (!Init.isUsable()) {
4914 HasErrors = true;
4915 break;
4916 }
Alexey Bataev5a3af132016-03-29 08:58:54 +00004917 ExprResult Update = BuildCounterUpdate(
4918 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
4919 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004920 if (!Update.isUsable()) {
4921 HasErrors = true;
4922 break;
4923 }
4924
4925 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4926 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00004927 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004928 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004929 if (!Final.isUsable()) {
4930 HasErrors = true;
4931 break;
4932 }
4933
4934 // Build Div for the next iteration: Div <- Div * IS.NumIters
4935 if (Cnt != 0) {
4936 if (Div.isUnset())
4937 Div = IS.NumIterations;
4938 else
4939 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4940 IS.NumIterations);
4941
4942 // Add parentheses (for debugging purposes only).
4943 if (Div.isUsable())
Alexey Bataev8b427062016-05-25 12:36:08 +00004944 Div = tryBuildCapture(SemaRef, Div.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004945 if (!Div.isUsable()) {
4946 HasErrors = true;
4947 break;
4948 }
Alexey Bataev8b427062016-05-25 12:36:08 +00004949 LoopMultipliers.push_back(Div.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004950 }
4951 if (!Update.isUsable() || !Final.isUsable()) {
4952 HasErrors = true;
4953 break;
4954 }
4955 // Save results
4956 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004957 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004958 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004959 Built.Updates[Cnt] = Update.get();
4960 Built.Finals[Cnt] = Final.get();
4961 }
4962 }
4963
4964 if (HasErrors)
4965 return 0;
4966
4967 // Save results
4968 Built.IterationVarRef = IV.get();
4969 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004970 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004971 Built.CalcLastIteration =
4972 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004973 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00004974 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004975 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004976 Built.Init = Init.get();
4977 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004978 Built.LB = LB.get();
4979 Built.UB = UB.get();
4980 Built.IL = IL.get();
4981 Built.ST = ST.get();
4982 Built.EUB = EUB.get();
4983 Built.NLB = NextLB.get();
4984 Built.NUB = NextUB.get();
Carlo Bertolli9925f152016-06-27 14:55:37 +00004985 Built.PrevLB = PrevLB.get();
4986 Built.PrevUB = PrevUB.get();
Carlo Bertolli8429d812017-02-17 21:29:13 +00004987 Built.DistInc = DistInc.get();
4988 Built.PrevEUB = PrevEUB.get();
Carlo Bertolliffafe102017-04-20 00:39:39 +00004989 Built.DistCombinedFields.LB = CombLB.get();
4990 Built.DistCombinedFields.UB = CombUB.get();
4991 Built.DistCombinedFields.EUB = CombEUB.get();
4992 Built.DistCombinedFields.Init = CombInit.get();
4993 Built.DistCombinedFields.Cond = CombCond.get();
4994 Built.DistCombinedFields.NLB = CombNextLB.get();
4995 Built.DistCombinedFields.NUB = CombNextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004996
Alexey Bataev8b427062016-05-25 12:36:08 +00004997 Expr *CounterVal = SemaRef.DefaultLvalueConversion(IV.get()).get();
4998 // Fill data for doacross depend clauses.
4999 for (auto Pair : DSA.getDoacrossDependClauses()) {
5000 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
5001 Pair.first->setCounterValue(CounterVal);
5002 else {
5003 if (NestedLoopCount != Pair.second.size() ||
5004 NestedLoopCount != LoopMultipliers.size() + 1) {
5005 // Erroneous case - clause has some problems.
5006 Pair.first->setCounterValue(CounterVal);
5007 continue;
5008 }
5009 assert(Pair.first->getDependencyKind() == OMPC_DEPEND_sink);
5010 auto I = Pair.second.rbegin();
5011 auto IS = IterSpaces.rbegin();
5012 auto ILM = LoopMultipliers.rbegin();
5013 Expr *UpCounterVal = CounterVal;
5014 Expr *Multiplier = nullptr;
5015 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5016 if (I->first) {
5017 assert(IS->CounterStep);
5018 Expr *NormalizedOffset =
5019 SemaRef
5020 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Div,
5021 I->first, IS->CounterStep)
5022 .get();
5023 if (Multiplier) {
5024 NormalizedOffset =
5025 SemaRef
5026 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Mul,
5027 NormalizedOffset, Multiplier)
5028 .get();
5029 }
5030 assert(I->second == OO_Plus || I->second == OO_Minus);
5031 BinaryOperatorKind BOK = (I->second == OO_Plus) ? BO_Add : BO_Sub;
David Majnemer9d168222016-08-05 17:44:54 +00005032 UpCounterVal = SemaRef
5033 .BuildBinOp(CurScope, I->first->getExprLoc(), BOK,
5034 UpCounterVal, NormalizedOffset)
5035 .get();
Alexey Bataev8b427062016-05-25 12:36:08 +00005036 }
5037 Multiplier = *ILM;
5038 ++I;
5039 ++IS;
5040 ++ILM;
5041 }
5042 Pair.first->setCounterValue(UpCounterVal);
5043 }
5044 }
5045
Alexey Bataevabfc0692014-06-25 06:52:00 +00005046 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005047}
5048
Alexey Bataev10e775f2015-07-30 11:36:16 +00005049static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005050 auto CollapseClauses =
5051 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
5052 if (CollapseClauses.begin() != CollapseClauses.end())
5053 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005054 return nullptr;
5055}
5056
Alexey Bataev10e775f2015-07-30 11:36:16 +00005057static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005058 auto OrderedClauses =
5059 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
5060 if (OrderedClauses.begin() != OrderedClauses.end())
5061 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005062 return nullptr;
5063}
5064
Kelvin Lic5609492016-07-15 04:39:07 +00005065static bool checkSimdlenSafelenSpecified(Sema &S,
5066 const ArrayRef<OMPClause *> Clauses) {
5067 OMPSafelenClause *Safelen = nullptr;
5068 OMPSimdlenClause *Simdlen = nullptr;
5069
5070 for (auto *Clause : Clauses) {
5071 if (Clause->getClauseKind() == OMPC_safelen)
5072 Safelen = cast<OMPSafelenClause>(Clause);
5073 else if (Clause->getClauseKind() == OMPC_simdlen)
5074 Simdlen = cast<OMPSimdlenClause>(Clause);
5075 if (Safelen && Simdlen)
5076 break;
5077 }
5078
5079 if (Simdlen && Safelen) {
5080 llvm::APSInt SimdlenRes, SafelenRes;
5081 auto SimdlenLength = Simdlen->getSimdlen();
5082 auto SafelenLength = Safelen->getSafelen();
5083 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
5084 SimdlenLength->isInstantiationDependent() ||
5085 SimdlenLength->containsUnexpandedParameterPack())
5086 return false;
5087 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
5088 SafelenLength->isInstantiationDependent() ||
5089 SafelenLength->containsUnexpandedParameterPack())
5090 return false;
5091 SimdlenLength->EvaluateAsInt(SimdlenRes, S.Context);
5092 SafelenLength->EvaluateAsInt(SafelenRes, S.Context);
5093 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
5094 // If both simdlen and safelen clauses are specified, the value of the
5095 // simdlen parameter must be less than or equal to the value of the safelen
5096 // parameter.
5097 if (SimdlenRes > SafelenRes) {
5098 S.Diag(SimdlenLength->getExprLoc(),
5099 diag::err_omp_wrong_simdlen_safelen_values)
5100 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
5101 return true;
5102 }
Alexey Bataev66b15b52015-08-21 11:14:16 +00005103 }
5104 return false;
5105}
5106
Alexey Bataev4acb8592014-07-07 13:01:15 +00005107StmtResult Sema::ActOnOpenMPSimdDirective(
5108 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5109 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005110 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005111 if (!AStmt)
5112 return StmtError();
5113
5114 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005115 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005116 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5117 // define the nested loops number.
5118 unsigned NestedLoopCount = CheckOpenMPLoop(
5119 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5120 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005121 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005122 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005123
Alexander Musmana5f070a2014-10-01 06:03:56 +00005124 assert((CurContext->isDependentContext() || B.builtAll()) &&
5125 "omp simd loop exprs were not built");
5126
Alexander Musman3276a272015-03-21 10:12:56 +00005127 if (!CurContext->isDependentContext()) {
5128 // Finalize the clauses that need pre-built expressions for CodeGen.
5129 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005130 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexander Musman3276a272015-03-21 10:12:56 +00005131 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005132 B.NumIterations, *this, CurScope,
5133 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00005134 return StmtError();
5135 }
5136 }
5137
Kelvin Lic5609492016-07-15 04:39:07 +00005138 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005139 return StmtError();
5140
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005141 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005142 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5143 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005144}
5145
Alexey Bataev4acb8592014-07-07 13:01:15 +00005146StmtResult Sema::ActOnOpenMPForDirective(
5147 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5148 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005149 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005150 if (!AStmt)
5151 return StmtError();
5152
5153 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005154 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005155 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5156 // define the nested loops number.
5157 unsigned NestedLoopCount = CheckOpenMPLoop(
5158 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5159 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005160 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005161 return StmtError();
5162
Alexander Musmana5f070a2014-10-01 06:03:56 +00005163 assert((CurContext->isDependentContext() || B.builtAll()) &&
5164 "omp for loop exprs were not built");
5165
Alexey Bataev54acd402015-08-04 11:18:19 +00005166 if (!CurContext->isDependentContext()) {
5167 // Finalize the clauses that need pre-built expressions for CodeGen.
5168 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005169 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00005170 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005171 B.NumIterations, *this, CurScope,
5172 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005173 return StmtError();
5174 }
5175 }
5176
Alexey Bataevf29276e2014-06-18 04:14:57 +00005177 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005178 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005179 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005180}
5181
Alexander Musmanf82886e2014-09-18 05:12:34 +00005182StmtResult Sema::ActOnOpenMPForSimdDirective(
5183 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5184 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005185 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005186 if (!AStmt)
5187 return StmtError();
5188
5189 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005190 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005191 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5192 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00005193 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005194 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
5195 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5196 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005197 if (NestedLoopCount == 0)
5198 return StmtError();
5199
Alexander Musmanc6388682014-12-15 07:07:06 +00005200 assert((CurContext->isDependentContext() || B.builtAll()) &&
5201 "omp for simd loop exprs were not built");
5202
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005203 if (!CurContext->isDependentContext()) {
5204 // Finalize the clauses that need pre-built expressions for CodeGen.
5205 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005206 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005207 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005208 B.NumIterations, *this, CurScope,
5209 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005210 return StmtError();
5211 }
5212 }
5213
Kelvin Lic5609492016-07-15 04:39:07 +00005214 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005215 return StmtError();
5216
Alexander Musmanf82886e2014-09-18 05:12:34 +00005217 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005218 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5219 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005220}
5221
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005222StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5223 Stmt *AStmt,
5224 SourceLocation StartLoc,
5225 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005226 if (!AStmt)
5227 return StmtError();
5228
5229 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005230 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00005231 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005232 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00005233 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005234 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005235 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005236 return StmtError();
5237 // All associated statements must be '#pragma omp section' except for
5238 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005239 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005240 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5241 if (SectionStmt)
5242 Diag(SectionStmt->getLocStart(),
5243 diag::err_omp_sections_substmt_not_section);
5244 return StmtError();
5245 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005246 cast<OMPSectionDirective>(SectionStmt)
5247 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005248 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005249 } else {
5250 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
5251 return StmtError();
5252 }
5253
5254 getCurFunction()->setHasBranchProtectedScope();
5255
Alexey Bataev25e5b442015-09-15 12:52:43 +00005256 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5257 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005258}
5259
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005260StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5261 SourceLocation StartLoc,
5262 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005263 if (!AStmt)
5264 return StmtError();
5265
5266 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005267
5268 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00005269 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005270
Alexey Bataev25e5b442015-09-15 12:52:43 +00005271 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5272 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005273}
5274
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005275StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5276 Stmt *AStmt,
5277 SourceLocation StartLoc,
5278 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005279 if (!AStmt)
5280 return StmtError();
5281
5282 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00005283
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005284 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00005285
Alexey Bataev3255bf32015-01-19 05:20:46 +00005286 // OpenMP [2.7.3, single Construct, Restrictions]
5287 // The copyprivate clause must not be used with the nowait clause.
5288 OMPClause *Nowait = nullptr;
5289 OMPClause *Copyprivate = nullptr;
5290 for (auto *Clause : Clauses) {
5291 if (Clause->getClauseKind() == OMPC_nowait)
5292 Nowait = Clause;
5293 else if (Clause->getClauseKind() == OMPC_copyprivate)
5294 Copyprivate = Clause;
5295 if (Copyprivate && Nowait) {
5296 Diag(Copyprivate->getLocStart(),
5297 diag::err_omp_single_copyprivate_with_nowait);
5298 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
5299 return StmtError();
5300 }
5301 }
5302
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005303 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5304}
5305
Alexander Musman80c22892014-07-17 08:54:58 +00005306StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5307 SourceLocation StartLoc,
5308 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005309 if (!AStmt)
5310 return StmtError();
5311
5312 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00005313
5314 getCurFunction()->setHasBranchProtectedScope();
5315
5316 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5317}
5318
Alexey Bataev28c75412015-12-15 08:19:24 +00005319StmtResult Sema::ActOnOpenMPCriticalDirective(
5320 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5321 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005322 if (!AStmt)
5323 return StmtError();
5324
5325 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005326
Alexey Bataev28c75412015-12-15 08:19:24 +00005327 bool ErrorFound = false;
5328 llvm::APSInt Hint;
5329 SourceLocation HintLoc;
5330 bool DependentHint = false;
5331 for (auto *C : Clauses) {
5332 if (C->getClauseKind() == OMPC_hint) {
5333 if (!DirName.getName()) {
5334 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
5335 ErrorFound = true;
5336 }
5337 Expr *E = cast<OMPHintClause>(C)->getHint();
5338 if (E->isTypeDependent() || E->isValueDependent() ||
5339 E->isInstantiationDependent())
5340 DependentHint = true;
5341 else {
5342 Hint = E->EvaluateKnownConstInt(Context);
5343 HintLoc = C->getLocStart();
5344 }
5345 }
5346 }
5347 if (ErrorFound)
5348 return StmtError();
5349 auto Pair = DSAStack->getCriticalWithHint(DirName);
5350 if (Pair.first && DirName.getName() && !DependentHint) {
5351 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5352 Diag(StartLoc, diag::err_omp_critical_with_hint);
5353 if (HintLoc.isValid()) {
5354 Diag(HintLoc, diag::note_omp_critical_hint_here)
5355 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
5356 } else
5357 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
5358 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
5359 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
5360 << 1
5361 << C->getHint()->EvaluateKnownConstInt(Context).toString(
5362 /*Radix=*/10, /*Signed=*/false);
5363 } else
5364 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
5365 }
5366 }
5367
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005368 getCurFunction()->setHasBranchProtectedScope();
5369
Alexey Bataev28c75412015-12-15 08:19:24 +00005370 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5371 Clauses, AStmt);
5372 if (!Pair.first && DirName.getName() && !DependentHint)
5373 DSAStack->addCriticalWithHint(Dir, Hint);
5374 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005375}
5376
Alexey Bataev4acb8592014-07-07 13:01:15 +00005377StmtResult Sema::ActOnOpenMPParallelForDirective(
5378 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5379 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005380 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005381 if (!AStmt)
5382 return StmtError();
5383
Alexey Bataev4acb8592014-07-07 13:01:15 +00005384 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5385 // 1.2.2 OpenMP Language Terminology
5386 // Structured block - An executable statement with a single entry at the
5387 // top and a single exit at the bottom.
5388 // The point of exit cannot be a branch out of the structured block.
5389 // longjmp() and throw() must not violate the entry/exit criteria.
5390 CS->getCapturedDecl()->setNothrow();
5391
Alexander Musmanc6388682014-12-15 07:07:06 +00005392 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005393 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5394 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005395 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005396 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
5397 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5398 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005399 if (NestedLoopCount == 0)
5400 return StmtError();
5401
Alexander Musmana5f070a2014-10-01 06:03:56 +00005402 assert((CurContext->isDependentContext() || B.builtAll()) &&
5403 "omp parallel for loop exprs were not built");
5404
Alexey Bataev54acd402015-08-04 11:18:19 +00005405 if (!CurContext->isDependentContext()) {
5406 // Finalize the clauses that need pre-built expressions for CodeGen.
5407 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005408 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00005409 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005410 B.NumIterations, *this, CurScope,
5411 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005412 return StmtError();
5413 }
5414 }
5415
Alexey Bataev4acb8592014-07-07 13:01:15 +00005416 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005417 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005418 NestedLoopCount, Clauses, AStmt, B,
5419 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00005420}
5421
Alexander Musmane4e893b2014-09-23 09:33:00 +00005422StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5423 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5424 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005425 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005426 if (!AStmt)
5427 return StmtError();
5428
Alexander Musmane4e893b2014-09-23 09:33:00 +00005429 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5430 // 1.2.2 OpenMP Language Terminology
5431 // Structured block - An executable statement with a single entry at the
5432 // top and a single exit at the bottom.
5433 // The point of exit cannot be a branch out of the structured block.
5434 // longjmp() and throw() must not violate the entry/exit criteria.
5435 CS->getCapturedDecl()->setNothrow();
5436
Alexander Musmanc6388682014-12-15 07:07:06 +00005437 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005438 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5439 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00005440 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005441 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
5442 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5443 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005444 if (NestedLoopCount == 0)
5445 return StmtError();
5446
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005447 if (!CurContext->isDependentContext()) {
5448 // Finalize the clauses that need pre-built expressions for CodeGen.
5449 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005450 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005451 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005452 B.NumIterations, *this, CurScope,
5453 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005454 return StmtError();
5455 }
5456 }
5457
Kelvin Lic5609492016-07-15 04:39:07 +00005458 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005459 return StmtError();
5460
Alexander Musmane4e893b2014-09-23 09:33:00 +00005461 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005462 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00005463 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005464}
5465
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005466StmtResult
5467Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
5468 Stmt *AStmt, SourceLocation StartLoc,
5469 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005470 if (!AStmt)
5471 return StmtError();
5472
5473 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005474 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00005475 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005476 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00005477 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005478 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005479 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005480 return StmtError();
5481 // All associated statements must be '#pragma omp section' except for
5482 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005483 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005484 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5485 if (SectionStmt)
5486 Diag(SectionStmt->getLocStart(),
5487 diag::err_omp_parallel_sections_substmt_not_section);
5488 return StmtError();
5489 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005490 cast<OMPSectionDirective>(SectionStmt)
5491 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005492 }
5493 } else {
5494 Diag(AStmt->getLocStart(),
5495 diag::err_omp_parallel_sections_not_compound_stmt);
5496 return StmtError();
5497 }
5498
5499 getCurFunction()->setHasBranchProtectedScope();
5500
Alexey Bataev25e5b442015-09-15 12:52:43 +00005501 return OMPParallelSectionsDirective::Create(
5502 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005503}
5504
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005505StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5506 Stmt *AStmt, SourceLocation StartLoc,
5507 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005508 if (!AStmt)
5509 return StmtError();
5510
David Majnemer9d168222016-08-05 17:44:54 +00005511 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005512 // 1.2.2 OpenMP Language Terminology
5513 // Structured block - An executable statement with a single entry at the
5514 // top and a single exit at the bottom.
5515 // The point of exit cannot be a branch out of the structured block.
5516 // longjmp() and throw() must not violate the entry/exit criteria.
5517 CS->getCapturedDecl()->setNothrow();
5518
5519 getCurFunction()->setHasBranchProtectedScope();
5520
Alexey Bataev25e5b442015-09-15 12:52:43 +00005521 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5522 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005523}
5524
Alexey Bataev68446b72014-07-18 07:47:19 +00005525StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5526 SourceLocation EndLoc) {
5527 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5528}
5529
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00005530StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5531 SourceLocation EndLoc) {
5532 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5533}
5534
Alexey Bataev2df347a2014-07-18 10:17:07 +00005535StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5536 SourceLocation EndLoc) {
5537 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5538}
5539
Alexey Bataev169d96a2017-07-18 20:17:46 +00005540StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses,
5541 Stmt *AStmt,
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005542 SourceLocation StartLoc,
5543 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005544 if (!AStmt)
5545 return StmtError();
5546
5547 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005548
5549 getCurFunction()->setHasBranchProtectedScope();
5550
Alexey Bataev169d96a2017-07-18 20:17:46 +00005551 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses,
Alexey Bataev3b1b8952017-07-25 15:53:26 +00005552 AStmt,
5553 DSAStack->getTaskgroupReductionRef());
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005554}
5555
Alexey Bataev6125da92014-07-21 11:26:11 +00005556StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5557 SourceLocation StartLoc,
5558 SourceLocation EndLoc) {
5559 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5560 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5561}
5562
Alexey Bataev346265e2015-09-25 10:37:12 +00005563StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5564 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005565 SourceLocation StartLoc,
5566 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00005567 OMPClause *DependFound = nullptr;
5568 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005569 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005570 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00005571 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005572 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005573 for (auto *C : Clauses) {
5574 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5575 DependFound = C;
5576 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5577 if (DependSourceClause) {
5578 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5579 << getOpenMPDirectiveName(OMPD_ordered)
5580 << getOpenMPClauseName(OMPC_depend) << 2;
5581 ErrorFound = true;
5582 } else
5583 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005584 if (DependSinkClause) {
5585 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5586 << 0;
5587 ErrorFound = true;
5588 }
5589 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5590 if (DependSourceClause) {
5591 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5592 << 1;
5593 ErrorFound = true;
5594 }
5595 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00005596 }
5597 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00005598 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005599 else if (C->getClauseKind() == OMPC_simd)
5600 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00005601 }
Alexey Bataeveb482352015-12-18 05:05:56 +00005602 if (!ErrorFound && !SC &&
5603 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005604 // OpenMP [2.8.1,simd Construct, Restrictions]
5605 // An ordered construct with the simd clause is the only OpenMP construct
5606 // that can appear in the simd region.
5607 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00005608 ErrorFound = true;
5609 } else if (DependFound && (TC || SC)) {
5610 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
5611 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
5612 ErrorFound = true;
5613 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
5614 Diag(DependFound->getLocStart(),
5615 diag::err_omp_ordered_directive_without_param);
5616 ErrorFound = true;
5617 } else if (TC || Clauses.empty()) {
5618 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
5619 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
5620 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
5621 << (TC != nullptr);
5622 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
5623 ErrorFound = true;
5624 }
5625 }
5626 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005627 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00005628
5629 if (AStmt) {
5630 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5631
5632 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005633 }
Alexey Bataev346265e2015-09-25 10:37:12 +00005634
5635 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005636}
5637
Alexey Bataev1d160b12015-03-13 12:27:31 +00005638namespace {
5639/// \brief Helper class for checking expression in 'omp atomic [update]'
5640/// construct.
5641class OpenMPAtomicUpdateChecker {
5642 /// \brief Error results for atomic update expressions.
5643 enum ExprAnalysisErrorCode {
5644 /// \brief A statement is not an expression statement.
5645 NotAnExpression,
5646 /// \brief Expression is not builtin binary or unary operation.
5647 NotABinaryOrUnaryExpression,
5648 /// \brief Unary operation is not post-/pre- increment/decrement operation.
5649 NotAnUnaryIncDecExpression,
5650 /// \brief An expression is not of scalar type.
5651 NotAScalarType,
5652 /// \brief A binary operation is not an assignment operation.
5653 NotAnAssignmentOp,
5654 /// \brief RHS part of the binary operation is not a binary expression.
5655 NotABinaryExpression,
5656 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
5657 /// expression.
5658 NotABinaryOperator,
5659 /// \brief RHS binary operation does not have reference to the updated LHS
5660 /// part.
5661 NotAnUpdateExpression,
5662 /// \brief No errors is found.
5663 NoError
5664 };
5665 /// \brief Reference to Sema.
5666 Sema &SemaRef;
5667 /// \brief A location for note diagnostics (when error is found).
5668 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005669 /// \brief 'x' lvalue part of the source atomic expression.
5670 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005671 /// \brief 'expr' rvalue part of the source atomic expression.
5672 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005673 /// \brief Helper expression of the form
5674 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5675 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5676 Expr *UpdateExpr;
5677 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
5678 /// important for non-associative operations.
5679 bool IsXLHSInRHSPart;
5680 BinaryOperatorKind Op;
5681 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005682 /// \brief true if the source expression is a postfix unary operation, false
5683 /// if it is a prefix unary operation.
5684 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005685
5686public:
5687 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00005688 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00005689 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00005690 /// \brief Check specified statement that it is suitable for 'atomic update'
5691 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00005692 /// expression. If DiagId and NoteId == 0, then only check is performed
5693 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005694 /// \param DiagId Diagnostic which should be emitted if error is found.
5695 /// \param NoteId Diagnostic note for the main error message.
5696 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00005697 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005698 /// \brief Return the 'x' lvalue part of the source atomic expression.
5699 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00005700 /// \brief Return the 'expr' rvalue part of the source atomic expression.
5701 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00005702 /// \brief Return the update expression used in calculation of the updated
5703 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5704 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5705 Expr *getUpdateExpr() const { return UpdateExpr; }
5706 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
5707 /// false otherwise.
5708 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5709
Alexey Bataevb78ca832015-04-01 03:33:17 +00005710 /// \brief true if the source expression is a postfix unary operation, false
5711 /// if it is a prefix unary operation.
5712 bool isPostfixUpdate() const { return IsPostfixUpdate; }
5713
Alexey Bataev1d160b12015-03-13 12:27:31 +00005714private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00005715 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5716 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005717};
5718} // namespace
5719
5720bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5721 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5722 ExprAnalysisErrorCode ErrorFound = NoError;
5723 SourceLocation ErrorLoc, NoteLoc;
5724 SourceRange ErrorRange, NoteRange;
5725 // Allowed constructs are:
5726 // x = x binop expr;
5727 // x = expr binop x;
5728 if (AtomicBinOp->getOpcode() == BO_Assign) {
5729 X = AtomicBinOp->getLHS();
5730 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
5731 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5732 if (AtomicInnerBinOp->isMultiplicativeOp() ||
5733 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5734 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005735 Op = AtomicInnerBinOp->getOpcode();
5736 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005737 auto *LHS = AtomicInnerBinOp->getLHS();
5738 auto *RHS = AtomicInnerBinOp->getRHS();
5739 llvm::FoldingSetNodeID XId, LHSId, RHSId;
5740 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
5741 /*Canonical=*/true);
5742 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
5743 /*Canonical=*/true);
5744 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
5745 /*Canonical=*/true);
5746 if (XId == LHSId) {
5747 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005748 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005749 } else if (XId == RHSId) {
5750 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005751 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005752 } else {
5753 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5754 ErrorRange = AtomicInnerBinOp->getSourceRange();
5755 NoteLoc = X->getExprLoc();
5756 NoteRange = X->getSourceRange();
5757 ErrorFound = NotAnUpdateExpression;
5758 }
5759 } else {
5760 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5761 ErrorRange = AtomicInnerBinOp->getSourceRange();
5762 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5763 NoteRange = SourceRange(NoteLoc, NoteLoc);
5764 ErrorFound = NotABinaryOperator;
5765 }
5766 } else {
5767 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5768 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5769 ErrorFound = NotABinaryExpression;
5770 }
5771 } else {
5772 ErrorLoc = AtomicBinOp->getExprLoc();
5773 ErrorRange = AtomicBinOp->getSourceRange();
5774 NoteLoc = AtomicBinOp->getOperatorLoc();
5775 NoteRange = SourceRange(NoteLoc, NoteLoc);
5776 ErrorFound = NotAnAssignmentOp;
5777 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005778 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005779 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5780 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5781 return true;
5782 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005783 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005784 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005785}
5786
5787bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5788 unsigned NoteId) {
5789 ExprAnalysisErrorCode ErrorFound = NoError;
5790 SourceLocation ErrorLoc, NoteLoc;
5791 SourceRange ErrorRange, NoteRange;
5792 // Allowed constructs are:
5793 // x++;
5794 // x--;
5795 // ++x;
5796 // --x;
5797 // x binop= expr;
5798 // x = x binop expr;
5799 // x = expr binop x;
5800 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5801 AtomicBody = AtomicBody->IgnoreParenImpCasts();
5802 if (AtomicBody->getType()->isScalarType() ||
5803 AtomicBody->isInstantiationDependent()) {
5804 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5805 AtomicBody->IgnoreParenImpCasts())) {
5806 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005807 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00005808 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00005809 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005810 E = AtomicCompAssignOp->getRHS();
Kelvin Li4f161cf2016-07-20 19:41:17 +00005811 X = AtomicCompAssignOp->getLHS()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005812 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005813 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5814 AtomicBody->IgnoreParenImpCasts())) {
5815 // Check for Binary Operation
David Majnemer9d168222016-08-05 17:44:54 +00005816 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
Alexey Bataevb4505a72015-03-30 05:20:59 +00005817 return true;
David Majnemer9d168222016-08-05 17:44:54 +00005818 } else if (auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
5819 AtomicBody->IgnoreParenImpCasts())) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005820 // Check for Unary Operation
5821 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005822 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005823 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5824 OpLoc = AtomicUnaryOp->getOperatorLoc();
Kelvin Li4f161cf2016-07-20 19:41:17 +00005825 X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005826 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5827 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005828 } else {
5829 ErrorFound = NotAnUnaryIncDecExpression;
5830 ErrorLoc = AtomicUnaryOp->getExprLoc();
5831 ErrorRange = AtomicUnaryOp->getSourceRange();
5832 NoteLoc = AtomicUnaryOp->getOperatorLoc();
5833 NoteRange = SourceRange(NoteLoc, NoteLoc);
5834 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005835 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005836 ErrorFound = NotABinaryOrUnaryExpression;
5837 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5838 NoteRange = ErrorRange = AtomicBody->getSourceRange();
5839 }
5840 } else {
5841 ErrorFound = NotAScalarType;
5842 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
5843 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5844 }
5845 } else {
5846 ErrorFound = NotAnExpression;
5847 NoteLoc = ErrorLoc = S->getLocStart();
5848 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5849 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005850 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005851 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5852 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5853 return true;
5854 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005855 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005856 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005857 // Build an update expression of form 'OpaqueValueExpr(x) binop
5858 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
5859 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
5860 auto *OVEX = new (SemaRef.getASTContext())
5861 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
5862 auto *OVEExpr = new (SemaRef.getASTContext())
5863 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
5864 auto Update =
5865 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
5866 IsXLHSInRHSPart ? OVEExpr : OVEX);
5867 if (Update.isInvalid())
5868 return true;
5869 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
5870 Sema::AA_Casting);
5871 if (Update.isInvalid())
5872 return true;
5873 UpdateExpr = Update.get();
5874 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00005875 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005876}
5877
Alexey Bataev0162e452014-07-22 10:10:35 +00005878StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5879 Stmt *AStmt,
5880 SourceLocation StartLoc,
5881 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005882 if (!AStmt)
5883 return StmtError();
5884
David Majnemer9d168222016-08-05 17:44:54 +00005885 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00005886 // 1.2.2 OpenMP Language Terminology
5887 // Structured block - An executable statement with a single entry at the
5888 // top and a single exit at the bottom.
5889 // The point of exit cannot be a branch out of the structured block.
5890 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00005891 OpenMPClauseKind AtomicKind = OMPC_unknown;
5892 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005893 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00005894 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00005895 C->getClauseKind() == OMPC_update ||
5896 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00005897 if (AtomicKind != OMPC_unknown) {
5898 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5899 << SourceRange(C->getLocStart(), C->getLocEnd());
5900 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5901 << getOpenMPClauseName(AtomicKind);
5902 } else {
5903 AtomicKind = C->getClauseKind();
5904 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005905 }
5906 }
5907 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005908
Alexey Bataev459dec02014-07-24 06:46:57 +00005909 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00005910 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5911 Body = EWC->getSubExpr();
5912
Alexey Bataev62cec442014-11-18 10:14:22 +00005913 Expr *X = nullptr;
5914 Expr *V = nullptr;
5915 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005916 Expr *UE = nullptr;
5917 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005918 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00005919 // OpenMP [2.12.6, atomic Construct]
5920 // In the next expressions:
5921 // * x and v (as applicable) are both l-value expressions with scalar type.
5922 // * During the execution of an atomic region, multiple syntactic
5923 // occurrences of x must designate the same storage location.
5924 // * Neither of v and expr (as applicable) may access the storage location
5925 // designated by x.
5926 // * Neither of x and expr (as applicable) may access the storage location
5927 // designated by v.
5928 // * expr is an expression with scalar type.
5929 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5930 // * binop, binop=, ++, and -- are not overloaded operators.
5931 // * The expression x binop expr must be numerically equivalent to x binop
5932 // (expr). This requirement is satisfied if the operators in expr have
5933 // precedence greater than binop, or by using parentheses around expr or
5934 // subexpressions of expr.
5935 // * The expression expr binop x must be numerically equivalent to (expr)
5936 // binop x. This requirement is satisfied if the operators in expr have
5937 // precedence equal to or greater than binop, or by using parentheses around
5938 // expr or subexpressions of expr.
5939 // * For forms that allow multiple occurrences of x, the number of times
5940 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00005941 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005942 enum {
5943 NotAnExpression,
5944 NotAnAssignmentOp,
5945 NotAScalarType,
5946 NotAnLValue,
5947 NoError
5948 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00005949 SourceLocation ErrorLoc, NoteLoc;
5950 SourceRange ErrorRange, NoteRange;
5951 // If clause is read:
5952 // v = x;
David Majnemer9d168222016-08-05 17:44:54 +00005953 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5954 auto *AtomicBinOp =
Alexey Bataev62cec442014-11-18 10:14:22 +00005955 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5956 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5957 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5958 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5959 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5960 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5961 if (!X->isLValue() || !V->isLValue()) {
5962 auto NotLValueExpr = X->isLValue() ? V : X;
5963 ErrorFound = NotAnLValue;
5964 ErrorLoc = AtomicBinOp->getExprLoc();
5965 ErrorRange = AtomicBinOp->getSourceRange();
5966 NoteLoc = NotLValueExpr->getExprLoc();
5967 NoteRange = NotLValueExpr->getSourceRange();
5968 }
5969 } else if (!X->isInstantiationDependent() ||
5970 !V->isInstantiationDependent()) {
5971 auto NotScalarExpr =
5972 (X->isInstantiationDependent() || X->getType()->isScalarType())
5973 ? V
5974 : X;
5975 ErrorFound = NotAScalarType;
5976 ErrorLoc = AtomicBinOp->getExprLoc();
5977 ErrorRange = AtomicBinOp->getSourceRange();
5978 NoteLoc = NotScalarExpr->getExprLoc();
5979 NoteRange = NotScalarExpr->getSourceRange();
5980 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005981 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00005982 ErrorFound = NotAnAssignmentOp;
5983 ErrorLoc = AtomicBody->getExprLoc();
5984 ErrorRange = AtomicBody->getSourceRange();
5985 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5986 : AtomicBody->getExprLoc();
5987 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5988 : AtomicBody->getSourceRange();
5989 }
5990 } else {
5991 ErrorFound = NotAnExpression;
5992 NoteLoc = ErrorLoc = Body->getLocStart();
5993 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005994 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005995 if (ErrorFound != NoError) {
5996 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
5997 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005998 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5999 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00006000 return StmtError();
6001 } else if (CurContext->isDependentContext())
6002 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00006003 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006004 enum {
6005 NotAnExpression,
6006 NotAnAssignmentOp,
6007 NotAScalarType,
6008 NotAnLValue,
6009 NoError
6010 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006011 SourceLocation ErrorLoc, NoteLoc;
6012 SourceRange ErrorRange, NoteRange;
6013 // If clause is write:
6014 // x = expr;
David Majnemer9d168222016-08-05 17:44:54 +00006015 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6016 auto *AtomicBinOp =
Alexey Bataevf33eba62014-11-28 07:21:40 +00006017 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6018 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00006019 X = AtomicBinOp->getLHS();
6020 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006021 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6022 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
6023 if (!X->isLValue()) {
6024 ErrorFound = NotAnLValue;
6025 ErrorLoc = AtomicBinOp->getExprLoc();
6026 ErrorRange = AtomicBinOp->getSourceRange();
6027 NoteLoc = X->getExprLoc();
6028 NoteRange = X->getSourceRange();
6029 }
6030 } else if (!X->isInstantiationDependent() ||
6031 !E->isInstantiationDependent()) {
6032 auto NotScalarExpr =
6033 (X->isInstantiationDependent() || X->getType()->isScalarType())
6034 ? E
6035 : X;
6036 ErrorFound = NotAScalarType;
6037 ErrorLoc = AtomicBinOp->getExprLoc();
6038 ErrorRange = AtomicBinOp->getSourceRange();
6039 NoteLoc = NotScalarExpr->getExprLoc();
6040 NoteRange = NotScalarExpr->getSourceRange();
6041 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006042 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00006043 ErrorFound = NotAnAssignmentOp;
6044 ErrorLoc = AtomicBody->getExprLoc();
6045 ErrorRange = AtomicBody->getSourceRange();
6046 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6047 : AtomicBody->getExprLoc();
6048 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6049 : AtomicBody->getSourceRange();
6050 }
6051 } else {
6052 ErrorFound = NotAnExpression;
6053 NoteLoc = ErrorLoc = Body->getLocStart();
6054 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006055 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00006056 if (ErrorFound != NoError) {
6057 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
6058 << ErrorRange;
6059 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6060 << NoteRange;
6061 return StmtError();
6062 } else if (CurContext->isDependentContext())
6063 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006064 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006065 // If clause is update:
6066 // x++;
6067 // x--;
6068 // ++x;
6069 // --x;
6070 // x binop= expr;
6071 // x = x binop expr;
6072 // x = expr binop x;
6073 OpenMPAtomicUpdateChecker Checker(*this);
6074 if (Checker.checkStatement(
6075 Body, (AtomicKind == OMPC_update)
6076 ? diag::err_omp_atomic_update_not_expression_statement
6077 : diag::err_omp_atomic_not_expression_statement,
6078 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00006079 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006080 if (!CurContext->isDependentContext()) {
6081 E = Checker.getExpr();
6082 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006083 UE = Checker.getUpdateExpr();
6084 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00006085 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006086 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006087 enum {
6088 NotAnAssignmentOp,
6089 NotACompoundStatement,
6090 NotTwoSubstatements,
6091 NotASpecificExpression,
6092 NoError
6093 } ErrorFound = NoError;
6094 SourceLocation ErrorLoc, NoteLoc;
6095 SourceRange ErrorRange, NoteRange;
6096 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6097 // If clause is a capture:
6098 // v = x++;
6099 // v = x--;
6100 // v = ++x;
6101 // v = --x;
6102 // v = x binop= expr;
6103 // v = x = x binop expr;
6104 // v = x = expr binop x;
6105 auto *AtomicBinOp =
6106 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6107 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6108 V = AtomicBinOp->getLHS();
6109 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6110 OpenMPAtomicUpdateChecker Checker(*this);
6111 if (Checker.checkStatement(
6112 Body, diag::err_omp_atomic_capture_not_expression_statement,
6113 diag::note_omp_atomic_update))
6114 return StmtError();
6115 E = Checker.getExpr();
6116 X = Checker.getX();
6117 UE = Checker.getUpdateExpr();
6118 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6119 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00006120 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006121 ErrorLoc = AtomicBody->getExprLoc();
6122 ErrorRange = AtomicBody->getSourceRange();
6123 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6124 : AtomicBody->getExprLoc();
6125 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6126 : AtomicBody->getSourceRange();
6127 ErrorFound = NotAnAssignmentOp;
6128 }
6129 if (ErrorFound != NoError) {
6130 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6131 << ErrorRange;
6132 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6133 return StmtError();
6134 } else if (CurContext->isDependentContext()) {
6135 UE = V = E = X = nullptr;
6136 }
6137 } else {
6138 // If clause is a capture:
6139 // { v = x; x = expr; }
6140 // { v = x; x++; }
6141 // { v = x; x--; }
6142 // { v = x; ++x; }
6143 // { v = x; --x; }
6144 // { v = x; x binop= expr; }
6145 // { v = x; x = x binop expr; }
6146 // { v = x; x = expr binop x; }
6147 // { x++; v = x; }
6148 // { x--; v = x; }
6149 // { ++x; v = x; }
6150 // { --x; v = x; }
6151 // { x binop= expr; v = x; }
6152 // { x = x binop expr; v = x; }
6153 // { x = expr binop x; v = x; }
6154 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6155 // Check that this is { expr1; expr2; }
6156 if (CS->size() == 2) {
6157 auto *First = CS->body_front();
6158 auto *Second = CS->body_back();
6159 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6160 First = EWC->getSubExpr()->IgnoreParenImpCasts();
6161 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6162 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6163 // Need to find what subexpression is 'v' and what is 'x'.
6164 OpenMPAtomicUpdateChecker Checker(*this);
6165 bool IsUpdateExprFound = !Checker.checkStatement(Second);
6166 BinaryOperator *BinOp = nullptr;
6167 if (IsUpdateExprFound) {
6168 BinOp = dyn_cast<BinaryOperator>(First);
6169 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6170 }
6171 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6172 // { v = x; x++; }
6173 // { v = x; x--; }
6174 // { v = x; ++x; }
6175 // { v = x; --x; }
6176 // { v = x; x binop= expr; }
6177 // { v = x; x = x binop expr; }
6178 // { v = x; x = expr binop x; }
6179 // Check that the first expression has form v = x.
6180 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6181 llvm::FoldingSetNodeID XId, PossibleXId;
6182 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6183 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6184 IsUpdateExprFound = XId == PossibleXId;
6185 if (IsUpdateExprFound) {
6186 V = BinOp->getLHS();
6187 X = Checker.getX();
6188 E = Checker.getExpr();
6189 UE = Checker.getUpdateExpr();
6190 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006191 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006192 }
6193 }
6194 if (!IsUpdateExprFound) {
6195 IsUpdateExprFound = !Checker.checkStatement(First);
6196 BinOp = nullptr;
6197 if (IsUpdateExprFound) {
6198 BinOp = dyn_cast<BinaryOperator>(Second);
6199 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6200 }
6201 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6202 // { x++; v = x; }
6203 // { x--; v = x; }
6204 // { ++x; v = x; }
6205 // { --x; v = x; }
6206 // { x binop= expr; v = x; }
6207 // { x = x binop expr; v = x; }
6208 // { x = expr binop x; v = x; }
6209 // Check that the second expression has form v = x.
6210 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6211 llvm::FoldingSetNodeID XId, PossibleXId;
6212 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6213 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6214 IsUpdateExprFound = XId == PossibleXId;
6215 if (IsUpdateExprFound) {
6216 V = BinOp->getLHS();
6217 X = Checker.getX();
6218 E = Checker.getExpr();
6219 UE = Checker.getUpdateExpr();
6220 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006221 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006222 }
6223 }
6224 }
6225 if (!IsUpdateExprFound) {
6226 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00006227 auto *FirstExpr = dyn_cast<Expr>(First);
6228 auto *SecondExpr = dyn_cast<Expr>(Second);
6229 if (!FirstExpr || !SecondExpr ||
6230 !(FirstExpr->isInstantiationDependent() ||
6231 SecondExpr->isInstantiationDependent())) {
6232 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6233 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006234 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00006235 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
6236 : First->getLocStart();
6237 NoteRange = ErrorRange = FirstBinOp
6238 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00006239 : SourceRange(ErrorLoc, ErrorLoc);
6240 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006241 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6242 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6243 ErrorFound = NotAnAssignmentOp;
6244 NoteLoc = ErrorLoc = SecondBinOp
6245 ? SecondBinOp->getOperatorLoc()
6246 : Second->getLocStart();
6247 NoteRange = ErrorRange =
6248 SecondBinOp ? SecondBinOp->getSourceRange()
6249 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00006250 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006251 auto *PossibleXRHSInFirst =
6252 FirstBinOp->getRHS()->IgnoreParenImpCasts();
6253 auto *PossibleXLHSInSecond =
6254 SecondBinOp->getLHS()->IgnoreParenImpCasts();
6255 llvm::FoldingSetNodeID X1Id, X2Id;
6256 PossibleXRHSInFirst->Profile(X1Id, Context,
6257 /*Canonical=*/true);
6258 PossibleXLHSInSecond->Profile(X2Id, Context,
6259 /*Canonical=*/true);
6260 IsUpdateExprFound = X1Id == X2Id;
6261 if (IsUpdateExprFound) {
6262 V = FirstBinOp->getLHS();
6263 X = SecondBinOp->getLHS();
6264 E = SecondBinOp->getRHS();
6265 UE = nullptr;
6266 IsXLHSInRHSPart = false;
6267 IsPostfixUpdate = true;
6268 } else {
6269 ErrorFound = NotASpecificExpression;
6270 ErrorLoc = FirstBinOp->getExprLoc();
6271 ErrorRange = FirstBinOp->getSourceRange();
6272 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6273 NoteRange = SecondBinOp->getRHS()->getSourceRange();
6274 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006275 }
6276 }
6277 }
6278 }
6279 } else {
6280 NoteLoc = ErrorLoc = Body->getLocStart();
6281 NoteRange = ErrorRange =
6282 SourceRange(Body->getLocStart(), Body->getLocStart());
6283 ErrorFound = NotTwoSubstatements;
6284 }
6285 } else {
6286 NoteLoc = ErrorLoc = Body->getLocStart();
6287 NoteRange = ErrorRange =
6288 SourceRange(Body->getLocStart(), Body->getLocStart());
6289 ErrorFound = NotACompoundStatement;
6290 }
6291 if (ErrorFound != NoError) {
6292 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6293 << ErrorRange;
6294 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6295 return StmtError();
6296 } else if (CurContext->isDependentContext()) {
6297 UE = V = E = X = nullptr;
6298 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006299 }
Alexey Bataevdea47612014-07-23 07:46:59 +00006300 }
Alexey Bataev0162e452014-07-22 10:10:35 +00006301
6302 getCurFunction()->setHasBranchProtectedScope();
6303
Alexey Bataev62cec442014-11-18 10:14:22 +00006304 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00006305 X, V, E, UE, IsXLHSInRHSPart,
6306 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00006307}
6308
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006309StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6310 Stmt *AStmt,
6311 SourceLocation StartLoc,
6312 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006313 if (!AStmt)
6314 return StmtError();
6315
Samuel Antao4af1b7b2015-12-02 17:44:43 +00006316 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6317 // 1.2.2 OpenMP Language Terminology
6318 // Structured block - An executable statement with a single entry at the
6319 // top and a single exit at the bottom.
6320 // The point of exit cannot be a branch out of the structured block.
6321 // longjmp() and throw() must not violate the entry/exit criteria.
6322 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006323
Alexey Bataev13314bf2014-10-09 04:18:56 +00006324 // OpenMP [2.16, Nesting of Regions]
6325 // If specified, a teams construct must be contained within a target
6326 // construct. That target construct must contain no statements or directives
6327 // outside of the teams construct.
6328 if (DSAStack->hasInnerTeamsRegion()) {
6329 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
6330 bool OMPTeamsFound = true;
6331 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
6332 auto I = CS->body_begin();
6333 while (I != CS->body_end()) {
David Majnemer9d168222016-08-05 17:44:54 +00006334 auto *OED = dyn_cast<OMPExecutableDirective>(*I);
Alexey Bataev13314bf2014-10-09 04:18:56 +00006335 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6336 OMPTeamsFound = false;
6337 break;
6338 }
6339 ++I;
6340 }
6341 assert(I != CS->body_end() && "Not found statement");
6342 S = *I;
Kelvin Li3834dce2016-06-27 19:15:43 +00006343 } else {
6344 auto *OED = dyn_cast<OMPExecutableDirective>(S);
6345 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
Alexey Bataev13314bf2014-10-09 04:18:56 +00006346 }
6347 if (!OMPTeamsFound) {
6348 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6349 Diag(DSAStack->getInnerTeamsRegionLoc(),
6350 diag::note_omp_nested_teams_construct_here);
6351 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
6352 << isa<OMPExecutableDirective>(S);
6353 return StmtError();
6354 }
6355 }
6356
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006357 getCurFunction()->setHasBranchProtectedScope();
6358
6359 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6360}
6361
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006362StmtResult
6363Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6364 Stmt *AStmt, SourceLocation StartLoc,
6365 SourceLocation EndLoc) {
6366 if (!AStmt)
6367 return StmtError();
6368
6369 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6370 // 1.2.2 OpenMP Language Terminology
6371 // Structured block - An executable statement with a single entry at the
6372 // top and a single exit at the bottom.
6373 // The point of exit cannot be a branch out of the structured block.
6374 // longjmp() and throw() must not violate the entry/exit criteria.
6375 CS->getCapturedDecl()->setNothrow();
6376
6377 getCurFunction()->setHasBranchProtectedScope();
6378
6379 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6380 AStmt);
6381}
6382
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006383StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6384 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6385 SourceLocation EndLoc,
6386 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6387 if (!AStmt)
6388 return StmtError();
6389
6390 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6391 // 1.2.2 OpenMP Language Terminology
6392 // Structured block - An executable statement with a single entry at the
6393 // top and a single exit at the bottom.
6394 // The point of exit cannot be a branch out of the structured block.
6395 // longjmp() and throw() must not violate the entry/exit criteria.
6396 CS->getCapturedDecl()->setNothrow();
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00006397 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
6398 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6399 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6400 // 1.2.2 OpenMP Language Terminology
6401 // Structured block - An executable statement with a single entry at the
6402 // top and a single exit at the bottom.
6403 // The point of exit cannot be a branch out of the structured block.
6404 // longjmp() and throw() must not violate the entry/exit criteria.
6405 CS->getCapturedDecl()->setNothrow();
6406 }
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006407
6408 OMPLoopDirective::HelperExprs B;
6409 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6410 // define the nested loops number.
6411 unsigned NestedLoopCount =
6412 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00006413 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006414 VarsWithImplicitDSA, B);
6415 if (NestedLoopCount == 0)
6416 return StmtError();
6417
6418 assert((CurContext->isDependentContext() || B.builtAll()) &&
6419 "omp target parallel for loop exprs were not built");
6420
6421 if (!CurContext->isDependentContext()) {
6422 // Finalize the clauses that need pre-built expressions for CodeGen.
6423 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00006424 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006425 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006426 B.NumIterations, *this, CurScope,
6427 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006428 return StmtError();
6429 }
6430 }
6431
6432 getCurFunction()->setHasBranchProtectedScope();
6433 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
6434 NestedLoopCount, Clauses, AStmt,
6435 B, DSAStack->isCancelRegion());
6436}
6437
Alexey Bataev95b64a92017-05-30 16:00:04 +00006438/// Check for existence of a map clause in the list of clauses.
6439static bool hasClauses(ArrayRef<OMPClause *> Clauses,
6440 const OpenMPClauseKind K) {
6441 return llvm::any_of(
6442 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; });
6443}
Samuel Antaodf67fc42016-01-19 19:15:56 +00006444
Alexey Bataev95b64a92017-05-30 16:00:04 +00006445template <typename... Params>
6446static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K,
6447 const Params... ClauseTypes) {
6448 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...);
Samuel Antaodf67fc42016-01-19 19:15:56 +00006449}
6450
Michael Wong65f367f2015-07-21 13:44:28 +00006451StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6452 Stmt *AStmt,
6453 SourceLocation StartLoc,
6454 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006455 if (!AStmt)
6456 return StmtError();
6457
6458 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6459
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006460 // OpenMP [2.10.1, Restrictions, p. 97]
6461 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00006462 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) {
6463 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
6464 << "'map' or 'use_device_ptr'"
David Majnemer9d168222016-08-05 17:44:54 +00006465 << getOpenMPDirectiveName(OMPD_target_data);
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006466 return StmtError();
6467 }
6468
Michael Wong65f367f2015-07-21 13:44:28 +00006469 getCurFunction()->setHasBranchProtectedScope();
6470
6471 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6472 AStmt);
6473}
6474
Samuel Antaodf67fc42016-01-19 19:15:56 +00006475StmtResult
6476Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
6477 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00006478 SourceLocation EndLoc, Stmt *AStmt) {
6479 if (!AStmt)
6480 return StmtError();
6481
6482 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6483 // 1.2.2 OpenMP Language Terminology
6484 // Structured block - An executable statement with a single entry at the
6485 // top and a single exit at the bottom.
6486 // The point of exit cannot be a branch out of the structured block.
6487 // longjmp() and throw() must not violate the entry/exit criteria.
6488 CS->getCapturedDecl()->setNothrow();
6489 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data);
6490 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6491 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6492 // 1.2.2 OpenMP Language Terminology
6493 // Structured block - An executable statement with a single entry at the
6494 // top and a single exit at the bottom.
6495 // The point of exit cannot be a branch out of the structured block.
6496 // longjmp() and throw() must not violate the entry/exit criteria.
6497 CS->getCapturedDecl()->setNothrow();
6498 }
6499
Samuel Antaodf67fc42016-01-19 19:15:56 +00006500 // OpenMP [2.10.2, Restrictions, p. 99]
6501 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00006502 if (!hasClauses(Clauses, OMPC_map)) {
6503 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
6504 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data);
Samuel Antaodf67fc42016-01-19 19:15:56 +00006505 return StmtError();
6506 }
6507
Alexey Bataev7828b252017-11-21 17:08:48 +00006508 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6509 AStmt);
Samuel Antaodf67fc42016-01-19 19:15:56 +00006510}
6511
Samuel Antao72590762016-01-19 20:04:50 +00006512StmtResult
6513Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6514 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00006515 SourceLocation EndLoc, Stmt *AStmt) {
6516 if (!AStmt)
6517 return StmtError();
6518
6519 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6520 // 1.2.2 OpenMP Language Terminology
6521 // Structured block - An executable statement with a single entry at the
6522 // top and a single exit at the bottom.
6523 // The point of exit cannot be a branch out of the structured block.
6524 // longjmp() and throw() must not violate the entry/exit criteria.
6525 CS->getCapturedDecl()->setNothrow();
6526 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data);
6527 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6528 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6529 // 1.2.2 OpenMP Language Terminology
6530 // Structured block - An executable statement with a single entry at the
6531 // top and a single exit at the bottom.
6532 // The point of exit cannot be a branch out of the structured block.
6533 // longjmp() and throw() must not violate the entry/exit criteria.
6534 CS->getCapturedDecl()->setNothrow();
6535 }
6536
Samuel Antao72590762016-01-19 20:04:50 +00006537 // OpenMP [2.10.3, Restrictions, p. 102]
6538 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00006539 if (!hasClauses(Clauses, OMPC_map)) {
6540 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
6541 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data);
Samuel Antao72590762016-01-19 20:04:50 +00006542 return StmtError();
6543 }
6544
Alexey Bataev7828b252017-11-21 17:08:48 +00006545 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6546 AStmt);
Samuel Antao72590762016-01-19 20:04:50 +00006547}
6548
Samuel Antao686c70c2016-05-26 17:30:50 +00006549StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
6550 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00006551 SourceLocation EndLoc,
6552 Stmt *AStmt) {
6553 if (!AStmt)
6554 return StmtError();
6555
6556 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6557 // 1.2.2 OpenMP Language Terminology
6558 // Structured block - An executable statement with a single entry at the
6559 // top and a single exit at the bottom.
6560 // The point of exit cannot be a branch out of the structured block.
6561 // longjmp() and throw() must not violate the entry/exit criteria.
6562 CS->getCapturedDecl()->setNothrow();
6563 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update);
6564 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6565 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6566 // 1.2.2 OpenMP Language Terminology
6567 // Structured block - An executable statement with a single entry at the
6568 // top and a single exit at the bottom.
6569 // The point of exit cannot be a branch out of the structured block.
6570 // longjmp() and throw() must not violate the entry/exit criteria.
6571 CS->getCapturedDecl()->setNothrow();
6572 }
6573
Alexey Bataev95b64a92017-05-30 16:00:04 +00006574 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) {
Samuel Antao686c70c2016-05-26 17:30:50 +00006575 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
6576 return StmtError();
6577 }
Alexey Bataev7828b252017-11-21 17:08:48 +00006578 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses,
6579 AStmt);
Samuel Antao686c70c2016-05-26 17:30:50 +00006580}
6581
Alexey Bataev13314bf2014-10-09 04:18:56 +00006582StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6583 Stmt *AStmt, SourceLocation StartLoc,
6584 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006585 if (!AStmt)
6586 return StmtError();
6587
Alexey Bataev13314bf2014-10-09 04:18:56 +00006588 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6589 // 1.2.2 OpenMP Language Terminology
6590 // Structured block - An executable statement with a single entry at the
6591 // top and a single exit at the bottom.
6592 // The point of exit cannot be a branch out of the structured block.
6593 // longjmp() and throw() must not violate the entry/exit criteria.
6594 CS->getCapturedDecl()->setNothrow();
6595
6596 getCurFunction()->setHasBranchProtectedScope();
6597
Alexey Bataevceabd412017-11-30 18:01:54 +00006598 DSAStack->setParentTeamsRegionLoc(StartLoc);
6599
Alexey Bataev13314bf2014-10-09 04:18:56 +00006600 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6601}
6602
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006603StmtResult
6604Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6605 SourceLocation EndLoc,
6606 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006607 if (DSAStack->isParentNowaitRegion()) {
6608 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6609 return StmtError();
6610 }
6611 if (DSAStack->isParentOrderedRegion()) {
6612 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
6613 return StmtError();
6614 }
6615 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
6616 CancelRegion);
6617}
6618
Alexey Bataev87933c72015-09-18 08:07:34 +00006619StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
6620 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00006621 SourceLocation EndLoc,
6622 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev80909872015-07-02 11:25:17 +00006623 if (DSAStack->isParentNowaitRegion()) {
6624 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6625 return StmtError();
6626 }
6627 if (DSAStack->isParentOrderedRegion()) {
6628 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6629 return StmtError();
6630 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006631 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00006632 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6633 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00006634}
6635
Alexey Bataev382967a2015-12-08 12:06:20 +00006636static bool checkGrainsizeNumTasksClauses(Sema &S,
6637 ArrayRef<OMPClause *> Clauses) {
6638 OMPClause *PrevClause = nullptr;
6639 bool ErrorFound = false;
6640 for (auto *C : Clauses) {
6641 if (C->getClauseKind() == OMPC_grainsize ||
6642 C->getClauseKind() == OMPC_num_tasks) {
6643 if (!PrevClause)
6644 PrevClause = C;
6645 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6646 S.Diag(C->getLocStart(),
6647 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6648 << getOpenMPClauseName(C->getClauseKind())
6649 << getOpenMPClauseName(PrevClause->getClauseKind());
6650 S.Diag(PrevClause->getLocStart(),
6651 diag::note_omp_previous_grainsize_num_tasks)
6652 << getOpenMPClauseName(PrevClause->getClauseKind());
6653 ErrorFound = true;
6654 }
6655 }
6656 }
6657 return ErrorFound;
6658}
6659
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00006660static bool checkReductionClauseWithNogroup(Sema &S,
6661 ArrayRef<OMPClause *> Clauses) {
6662 OMPClause *ReductionClause = nullptr;
6663 OMPClause *NogroupClause = nullptr;
6664 for (auto *C : Clauses) {
6665 if (C->getClauseKind() == OMPC_reduction) {
6666 ReductionClause = C;
6667 if (NogroupClause)
6668 break;
6669 continue;
6670 }
6671 if (C->getClauseKind() == OMPC_nogroup) {
6672 NogroupClause = C;
6673 if (ReductionClause)
6674 break;
6675 continue;
6676 }
6677 }
6678 if (ReductionClause && NogroupClause) {
6679 S.Diag(ReductionClause->getLocStart(), diag::err_omp_reduction_with_nogroup)
6680 << SourceRange(NogroupClause->getLocStart(),
6681 NogroupClause->getLocEnd());
6682 return true;
6683 }
6684 return false;
6685}
6686
Alexey Bataev49f6e782015-12-01 04:18:41 +00006687StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6688 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6689 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006690 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00006691 if (!AStmt)
6692 return StmtError();
6693
6694 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6695 OMPLoopDirective::HelperExprs B;
6696 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6697 // define the nested loops number.
6698 unsigned NestedLoopCount =
6699 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006700 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00006701 VarsWithImplicitDSA, B);
6702 if (NestedLoopCount == 0)
6703 return StmtError();
6704
6705 assert((CurContext->isDependentContext() || B.builtAll()) &&
6706 "omp for loop exprs were not built");
6707
Alexey Bataev382967a2015-12-08 12:06:20 +00006708 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6709 // The grainsize clause and num_tasks clause are mutually exclusive and may
6710 // not appear on the same taskloop directive.
6711 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6712 return StmtError();
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00006713 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6714 // If a reduction clause is present on the taskloop directive, the nogroup
6715 // clause must not be specified.
6716 if (checkReductionClauseWithNogroup(*this, Clauses))
6717 return StmtError();
Alexey Bataev382967a2015-12-08 12:06:20 +00006718
Alexey Bataev49f6e782015-12-01 04:18:41 +00006719 getCurFunction()->setHasBranchProtectedScope();
6720 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
6721 NestedLoopCount, Clauses, AStmt, B);
6722}
6723
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006724StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
6725 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6726 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006727 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006728 if (!AStmt)
6729 return StmtError();
6730
6731 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6732 OMPLoopDirective::HelperExprs B;
6733 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6734 // define the nested loops number.
6735 unsigned NestedLoopCount =
6736 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
6737 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6738 VarsWithImplicitDSA, B);
6739 if (NestedLoopCount == 0)
6740 return StmtError();
6741
6742 assert((CurContext->isDependentContext() || B.builtAll()) &&
6743 "omp for loop exprs were not built");
6744
Alexey Bataev5a3af132016-03-29 08:58:54 +00006745 if (!CurContext->isDependentContext()) {
6746 // Finalize the clauses that need pre-built expressions for CodeGen.
6747 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00006748 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev5a3af132016-03-29 08:58:54 +00006749 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006750 B.NumIterations, *this, CurScope,
6751 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00006752 return StmtError();
6753 }
6754 }
6755
Alexey Bataev382967a2015-12-08 12:06:20 +00006756 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6757 // The grainsize clause and num_tasks clause are mutually exclusive and may
6758 // not appear on the same taskloop directive.
6759 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6760 return StmtError();
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00006761 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6762 // If a reduction clause is present on the taskloop directive, the nogroup
6763 // clause must not be specified.
6764 if (checkReductionClauseWithNogroup(*this, Clauses))
6765 return StmtError();
Alexey Bataev438388c2017-11-22 18:34:02 +00006766 if (checkSimdlenSafelenSpecified(*this, Clauses))
6767 return StmtError();
Alexey Bataev382967a2015-12-08 12:06:20 +00006768
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006769 getCurFunction()->setHasBranchProtectedScope();
6770 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
6771 NestedLoopCount, Clauses, AStmt, B);
6772}
6773
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006774StmtResult Sema::ActOnOpenMPDistributeDirective(
6775 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6776 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006777 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006778 if (!AStmt)
6779 return StmtError();
6780
6781 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6782 OMPLoopDirective::HelperExprs B;
6783 // In presence of clause 'collapse' with number of loops, it will
6784 // define the nested loops number.
6785 unsigned NestedLoopCount =
6786 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
6787 nullptr /*ordered not a clause on distribute*/, AStmt,
6788 *this, *DSAStack, VarsWithImplicitDSA, B);
6789 if (NestedLoopCount == 0)
6790 return StmtError();
6791
6792 assert((CurContext->isDependentContext() || B.builtAll()) &&
6793 "omp for loop exprs were not built");
6794
6795 getCurFunction()->setHasBranchProtectedScope();
6796 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
6797 NestedLoopCount, Clauses, AStmt, B);
6798}
6799
Carlo Bertolli9925f152016-06-27 14:55:37 +00006800StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
6801 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6802 SourceLocation EndLoc,
6803 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6804 if (!AStmt)
6805 return StmtError();
6806
6807 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6808 // 1.2.2 OpenMP Language Terminology
6809 // Structured block - An executable statement with a single entry at the
6810 // top and a single exit at the bottom.
6811 // The point of exit cannot be a branch out of the structured block.
6812 // longjmp() and throw() must not violate the entry/exit criteria.
6813 CS->getCapturedDecl()->setNothrow();
Alexey Bataev7f96c372017-11-22 17:19:31 +00006814 for (int ThisCaptureLevel =
6815 getOpenMPCaptureLevels(OMPD_distribute_parallel_for);
6816 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6817 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6818 // 1.2.2 OpenMP Language Terminology
6819 // Structured block - An executable statement with a single entry at the
6820 // top and a single exit at the bottom.
6821 // The point of exit cannot be a branch out of the structured block.
6822 // longjmp() and throw() must not violate the entry/exit criteria.
6823 CS->getCapturedDecl()->setNothrow();
6824 }
Carlo Bertolli9925f152016-06-27 14:55:37 +00006825
6826 OMPLoopDirective::HelperExprs B;
6827 // In presence of clause 'collapse' with number of loops, it will
6828 // define the nested loops number.
6829 unsigned NestedLoopCount = CheckOpenMPLoop(
6830 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataev7f96c372017-11-22 17:19:31 +00006831 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Carlo Bertolli9925f152016-06-27 14:55:37 +00006832 VarsWithImplicitDSA, B);
6833 if (NestedLoopCount == 0)
6834 return StmtError();
6835
6836 assert((CurContext->isDependentContext() || B.builtAll()) &&
6837 "omp for loop exprs were not built");
6838
6839 getCurFunction()->setHasBranchProtectedScope();
6840 return OMPDistributeParallelForDirective::Create(
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00006841 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
6842 DSAStack->isCancelRegion());
Carlo Bertolli9925f152016-06-27 14:55:37 +00006843}
6844
Kelvin Li4a39add2016-07-05 05:00:15 +00006845StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
6846 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6847 SourceLocation EndLoc,
6848 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6849 if (!AStmt)
6850 return StmtError();
6851
6852 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6853 // 1.2.2 OpenMP Language Terminology
6854 // Structured block - An executable statement with a single entry at the
6855 // top and a single exit at the bottom.
6856 // The point of exit cannot be a branch out of the structured block.
6857 // longjmp() and throw() must not violate the entry/exit criteria.
6858 CS->getCapturedDecl()->setNothrow();
Alexey Bataev974acd62017-11-27 19:38:52 +00006859 for (int ThisCaptureLevel =
6860 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd);
6861 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6862 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6863 // 1.2.2 OpenMP Language Terminology
6864 // Structured block - An executable statement with a single entry at the
6865 // top and a single exit at the bottom.
6866 // The point of exit cannot be a branch out of the structured block.
6867 // longjmp() and throw() must not violate the entry/exit criteria.
6868 CS->getCapturedDecl()->setNothrow();
6869 }
Kelvin Li4a39add2016-07-05 05:00:15 +00006870
6871 OMPLoopDirective::HelperExprs B;
6872 // In presence of clause 'collapse' with number of loops, it will
6873 // define the nested loops number.
6874 unsigned NestedLoopCount = CheckOpenMPLoop(
6875 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev974acd62017-11-27 19:38:52 +00006876 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li4a39add2016-07-05 05:00:15 +00006877 VarsWithImplicitDSA, B);
6878 if (NestedLoopCount == 0)
6879 return StmtError();
6880
6881 assert((CurContext->isDependentContext() || B.builtAll()) &&
6882 "omp for loop exprs were not built");
6883
Alexey Bataev438388c2017-11-22 18:34:02 +00006884 if (!CurContext->isDependentContext()) {
6885 // Finalize the clauses that need pre-built expressions for CodeGen.
6886 for (auto C : Clauses) {
6887 if (auto *LC = dyn_cast<OMPLinearClause>(C))
6888 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6889 B.NumIterations, *this, CurScope,
6890 DSAStack))
6891 return StmtError();
6892 }
6893 }
6894
Kelvin Lic5609492016-07-15 04:39:07 +00006895 if (checkSimdlenSafelenSpecified(*this, Clauses))
6896 return StmtError();
6897
Kelvin Li4a39add2016-07-05 05:00:15 +00006898 getCurFunction()->setHasBranchProtectedScope();
6899 return OMPDistributeParallelForSimdDirective::Create(
6900 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
6901}
6902
Kelvin Li787f3fc2016-07-06 04:45:38 +00006903StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
6904 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6905 SourceLocation EndLoc,
6906 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6907 if (!AStmt)
6908 return StmtError();
6909
6910 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6911 // 1.2.2 OpenMP Language Terminology
6912 // Structured block - An executable statement with a single entry at the
6913 // top and a single exit at the bottom.
6914 // The point of exit cannot be a branch out of the structured block.
6915 // longjmp() and throw() must not violate the entry/exit criteria.
6916 CS->getCapturedDecl()->setNothrow();
Alexey Bataev617db5f2017-12-04 15:38:33 +00006917 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd);
6918 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6919 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6920 // 1.2.2 OpenMP Language Terminology
6921 // Structured block - An executable statement with a single entry at the
6922 // top and a single exit at the bottom.
6923 // The point of exit cannot be a branch out of the structured block.
6924 // longjmp() and throw() must not violate the entry/exit criteria.
6925 CS->getCapturedDecl()->setNothrow();
6926 }
Kelvin Li787f3fc2016-07-06 04:45:38 +00006927
6928 OMPLoopDirective::HelperExprs B;
6929 // In presence of clause 'collapse' with number of loops, it will
6930 // define the nested loops number.
6931 unsigned NestedLoopCount =
6932 CheckOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev617db5f2017-12-04 15:38:33 +00006933 nullptr /*ordered not a clause on distribute*/, CS, *this,
6934 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li787f3fc2016-07-06 04:45:38 +00006935 if (NestedLoopCount == 0)
6936 return StmtError();
6937
6938 assert((CurContext->isDependentContext() || B.builtAll()) &&
6939 "omp for loop exprs were not built");
6940
Alexey Bataev438388c2017-11-22 18:34:02 +00006941 if (!CurContext->isDependentContext()) {
6942 // Finalize the clauses that need pre-built expressions for CodeGen.
6943 for (auto C : Clauses) {
6944 if (auto *LC = dyn_cast<OMPLinearClause>(C))
6945 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6946 B.NumIterations, *this, CurScope,
6947 DSAStack))
6948 return StmtError();
6949 }
6950 }
6951
Kelvin Lic5609492016-07-15 04:39:07 +00006952 if (checkSimdlenSafelenSpecified(*this, Clauses))
6953 return StmtError();
6954
Kelvin Li787f3fc2016-07-06 04:45:38 +00006955 getCurFunction()->setHasBranchProtectedScope();
6956 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
6957 NestedLoopCount, Clauses, AStmt, B);
6958}
6959
Kelvin Lia579b912016-07-14 02:54:56 +00006960StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
6961 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6962 SourceLocation EndLoc,
6963 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6964 if (!AStmt)
6965 return StmtError();
6966
6967 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6968 // 1.2.2 OpenMP Language Terminology
6969 // Structured block - An executable statement with a single entry at the
6970 // top and a single exit at the bottom.
6971 // The point of exit cannot be a branch out of the structured block.
6972 // longjmp() and throw() must not violate the entry/exit criteria.
6973 CS->getCapturedDecl()->setNothrow();
Alexey Bataev5d7edca2017-11-09 17:32:15 +00006974 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
6975 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6976 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6977 // 1.2.2 OpenMP Language Terminology
6978 // Structured block - An executable statement with a single entry at the
6979 // top and a single exit at the bottom.
6980 // The point of exit cannot be a branch out of the structured block.
6981 // longjmp() and throw() must not violate the entry/exit criteria.
6982 CS->getCapturedDecl()->setNothrow();
6983 }
Kelvin Lia579b912016-07-14 02:54:56 +00006984
6985 OMPLoopDirective::HelperExprs B;
6986 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6987 // define the nested loops number.
6988 unsigned NestedLoopCount = CheckOpenMPLoop(
6989 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev5d7edca2017-11-09 17:32:15 +00006990 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Kelvin Lia579b912016-07-14 02:54:56 +00006991 VarsWithImplicitDSA, B);
6992 if (NestedLoopCount == 0)
6993 return StmtError();
6994
6995 assert((CurContext->isDependentContext() || B.builtAll()) &&
6996 "omp target parallel for simd loop exprs were not built");
6997
6998 if (!CurContext->isDependentContext()) {
6999 // Finalize the clauses that need pre-built expressions for CodeGen.
7000 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007001 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Lia579b912016-07-14 02:54:56 +00007002 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7003 B.NumIterations, *this, CurScope,
7004 DSAStack))
7005 return StmtError();
7006 }
7007 }
Kelvin Lic5609492016-07-15 04:39:07 +00007008 if (checkSimdlenSafelenSpecified(*this, Clauses))
Kelvin Lia579b912016-07-14 02:54:56 +00007009 return StmtError();
7010
7011 getCurFunction()->setHasBranchProtectedScope();
7012 return OMPTargetParallelForSimdDirective::Create(
7013 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7014}
7015
Kelvin Li986330c2016-07-20 22:57:10 +00007016StmtResult Sema::ActOnOpenMPTargetSimdDirective(
7017 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7018 SourceLocation EndLoc,
7019 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7020 if (!AStmt)
7021 return StmtError();
7022
7023 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7024 // 1.2.2 OpenMP Language Terminology
7025 // Structured block - An executable statement with a single entry at the
7026 // top and a single exit at the bottom.
7027 // The point of exit cannot be a branch out of the structured block.
7028 // longjmp() and throw() must not violate the entry/exit criteria.
7029 CS->getCapturedDecl()->setNothrow();
Alexey Bataevf8365372017-11-17 17:57:25 +00007030 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd);
7031 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7032 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7033 // 1.2.2 OpenMP Language Terminology
7034 // Structured block - An executable statement with a single entry at the
7035 // top and a single exit at the bottom.
7036 // The point of exit cannot be a branch out of the structured block.
7037 // longjmp() and throw() must not violate the entry/exit criteria.
7038 CS->getCapturedDecl()->setNothrow();
7039 }
7040
Kelvin Li986330c2016-07-20 22:57:10 +00007041 OMPLoopDirective::HelperExprs B;
7042 // In presence of clause 'collapse' with number of loops, it will define the
7043 // nested loops number.
David Majnemer9d168222016-08-05 17:44:54 +00007044 unsigned NestedLoopCount =
Kelvin Li986330c2016-07-20 22:57:10 +00007045 CheckOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
Alexey Bataevf8365372017-11-17 17:57:25 +00007046 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Kelvin Li986330c2016-07-20 22:57:10 +00007047 VarsWithImplicitDSA, B);
7048 if (NestedLoopCount == 0)
7049 return StmtError();
7050
7051 assert((CurContext->isDependentContext() || B.builtAll()) &&
7052 "omp target simd loop exprs were not built");
7053
7054 if (!CurContext->isDependentContext()) {
7055 // Finalize the clauses that need pre-built expressions for CodeGen.
7056 for (auto C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007057 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Li986330c2016-07-20 22:57:10 +00007058 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7059 B.NumIterations, *this, CurScope,
7060 DSAStack))
7061 return StmtError();
7062 }
7063 }
7064
7065 if (checkSimdlenSafelenSpecified(*this, Clauses))
7066 return StmtError();
7067
7068 getCurFunction()->setHasBranchProtectedScope();
7069 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
7070 NestedLoopCount, Clauses, AStmt, B);
7071}
7072
Kelvin Li02532872016-08-05 14:37:37 +00007073StmtResult Sema::ActOnOpenMPTeamsDistributeDirective(
7074 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7075 SourceLocation EndLoc,
7076 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7077 if (!AStmt)
7078 return StmtError();
7079
7080 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7081 // 1.2.2 OpenMP Language Terminology
7082 // Structured block - An executable statement with a single entry at the
7083 // top and a single exit at the bottom.
7084 // The point of exit cannot be a branch out of the structured block.
7085 // longjmp() and throw() must not violate the entry/exit criteria.
7086 CS->getCapturedDecl()->setNothrow();
Alexey Bataev95c6dd42017-11-29 15:14:16 +00007087 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute);
7088 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7089 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7090 // 1.2.2 OpenMP Language Terminology
7091 // Structured block - An executable statement with a single entry at the
7092 // top and a single exit at the bottom.
7093 // The point of exit cannot be a branch out of the structured block.
7094 // longjmp() and throw() must not violate the entry/exit criteria.
7095 CS->getCapturedDecl()->setNothrow();
7096 }
Kelvin Li02532872016-08-05 14:37:37 +00007097
7098 OMPLoopDirective::HelperExprs B;
7099 // In presence of clause 'collapse' with number of loops, it will
7100 // define the nested loops number.
7101 unsigned NestedLoopCount =
7102 CheckOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses),
Alexey Bataev95c6dd42017-11-29 15:14:16 +00007103 nullptr /*ordered not a clause on distribute*/, CS, *this,
7104 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li02532872016-08-05 14:37:37 +00007105 if (NestedLoopCount == 0)
7106 return StmtError();
7107
7108 assert((CurContext->isDependentContext() || B.builtAll()) &&
7109 "omp teams distribute loop exprs were not built");
7110
7111 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007112
7113 DSAStack->setParentTeamsRegionLoc(StartLoc);
7114
David Majnemer9d168222016-08-05 17:44:54 +00007115 return OMPTeamsDistributeDirective::Create(
7116 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Kelvin Li02532872016-08-05 14:37:37 +00007117}
7118
Kelvin Li4e325f72016-10-25 12:50:55 +00007119StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective(
7120 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7121 SourceLocation EndLoc,
7122 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7123 if (!AStmt)
7124 return StmtError();
7125
7126 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7127 // 1.2.2 OpenMP Language Terminology
7128 // Structured block - An executable statement with a single entry at the
7129 // top and a single exit at the bottom.
7130 // The point of exit cannot be a branch out of the structured block.
7131 // longjmp() and throw() must not violate the entry/exit criteria.
7132 CS->getCapturedDecl()->setNothrow();
Alexey Bataev999277a2017-12-06 14:31:09 +00007133 for (int ThisCaptureLevel =
7134 getOpenMPCaptureLevels(OMPD_teams_distribute_simd);
7135 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7136 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7137 // 1.2.2 OpenMP Language Terminology
7138 // Structured block - An executable statement with a single entry at the
7139 // top and a single exit at the bottom.
7140 // The point of exit cannot be a branch out of the structured block.
7141 // longjmp() and throw() must not violate the entry/exit criteria.
7142 CS->getCapturedDecl()->setNothrow();
7143 }
7144
Kelvin Li4e325f72016-10-25 12:50:55 +00007145
7146 OMPLoopDirective::HelperExprs B;
7147 // In presence of clause 'collapse' with number of loops, it will
7148 // define the nested loops number.
Samuel Antao4c8035b2016-12-12 18:00:20 +00007149 unsigned NestedLoopCount = CheckOpenMPLoop(
7150 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev999277a2017-12-06 14:31:09 +00007151 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Samuel Antao4c8035b2016-12-12 18:00:20 +00007152 VarsWithImplicitDSA, B);
Kelvin Li4e325f72016-10-25 12:50:55 +00007153
7154 if (NestedLoopCount == 0)
7155 return StmtError();
7156
7157 assert((CurContext->isDependentContext() || B.builtAll()) &&
7158 "omp teams distribute simd loop exprs were not built");
7159
7160 if (!CurContext->isDependentContext()) {
7161 // Finalize the clauses that need pre-built expressions for CodeGen.
7162 for (auto C : Clauses) {
7163 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7164 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7165 B.NumIterations, *this, CurScope,
7166 DSAStack))
7167 return StmtError();
7168 }
7169 }
7170
7171 if (checkSimdlenSafelenSpecified(*this, Clauses))
7172 return StmtError();
7173
7174 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007175
7176 DSAStack->setParentTeamsRegionLoc(StartLoc);
7177
Kelvin Li4e325f72016-10-25 12:50:55 +00007178 return OMPTeamsDistributeSimdDirective::Create(
7179 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7180}
7181
Kelvin Li579e41c2016-11-30 23:51:03 +00007182StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective(
7183 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7184 SourceLocation EndLoc,
7185 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7186 if (!AStmt)
7187 return StmtError();
7188
7189 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7190 // 1.2.2 OpenMP Language Terminology
7191 // Structured block - An executable statement with a single entry at the
7192 // top and a single exit at the bottom.
7193 // The point of exit cannot be a branch out of the structured block.
7194 // longjmp() and throw() must not violate the entry/exit criteria.
7195 CS->getCapturedDecl()->setNothrow();
7196
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00007197 for (int ThisCaptureLevel =
7198 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd);
7199 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7200 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7201 // 1.2.2 OpenMP Language Terminology
7202 // Structured block - An executable statement with a single entry at the
7203 // top and a single exit at the bottom.
7204 // The point of exit cannot be a branch out of the structured block.
7205 // longjmp() and throw() must not violate the entry/exit criteria.
7206 CS->getCapturedDecl()->setNothrow();
7207 }
7208
Kelvin Li579e41c2016-11-30 23:51:03 +00007209 OMPLoopDirective::HelperExprs B;
7210 // In presence of clause 'collapse' with number of loops, it will
7211 // define the nested loops number.
7212 auto NestedLoopCount = CheckOpenMPLoop(
7213 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00007214 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li579e41c2016-11-30 23:51:03 +00007215 VarsWithImplicitDSA, B);
7216
7217 if (NestedLoopCount == 0)
7218 return StmtError();
7219
7220 assert((CurContext->isDependentContext() || B.builtAll()) &&
7221 "omp for loop exprs were not built");
7222
7223 if (!CurContext->isDependentContext()) {
7224 // Finalize the clauses that need pre-built expressions for CodeGen.
7225 for (auto C : Clauses) {
7226 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7227 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7228 B.NumIterations, *this, CurScope,
7229 DSAStack))
7230 return StmtError();
7231 }
7232 }
7233
7234 if (checkSimdlenSafelenSpecified(*this, Clauses))
7235 return StmtError();
7236
7237 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007238
7239 DSAStack->setParentTeamsRegionLoc(StartLoc);
7240
Kelvin Li579e41c2016-11-30 23:51:03 +00007241 return OMPTeamsDistributeParallelForSimdDirective::Create(
7242 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7243}
7244
Kelvin Li7ade93f2016-12-09 03:24:30 +00007245StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective(
7246 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7247 SourceLocation EndLoc,
7248 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7249 if (!AStmt)
7250 return StmtError();
7251
7252 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7253 // 1.2.2 OpenMP Language Terminology
7254 // Structured block - An executable statement with a single entry at the
7255 // top and a single exit at the bottom.
7256 // The point of exit cannot be a branch out of the structured block.
7257 // longjmp() and throw() must not violate the entry/exit criteria.
7258 CS->getCapturedDecl()->setNothrow();
7259
Carlo Bertolli62fae152017-11-20 20:46:39 +00007260 for (int ThisCaptureLevel =
7261 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for);
7262 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7263 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7264 // 1.2.2 OpenMP Language Terminology
7265 // Structured block - An executable statement with a single entry at the
7266 // top and a single exit at the bottom.
7267 // The point of exit cannot be a branch out of the structured block.
7268 // longjmp() and throw() must not violate the entry/exit criteria.
7269 CS->getCapturedDecl()->setNothrow();
7270 }
7271
Kelvin Li7ade93f2016-12-09 03:24:30 +00007272 OMPLoopDirective::HelperExprs B;
7273 // In presence of clause 'collapse' with number of loops, it will
7274 // define the nested loops number.
7275 unsigned NestedLoopCount = CheckOpenMPLoop(
7276 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
Carlo Bertolli62fae152017-11-20 20:46:39 +00007277 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li7ade93f2016-12-09 03:24:30 +00007278 VarsWithImplicitDSA, B);
7279
7280 if (NestedLoopCount == 0)
7281 return StmtError();
7282
7283 assert((CurContext->isDependentContext() || B.builtAll()) &&
7284 "omp for loop exprs were not built");
7285
Kelvin Li7ade93f2016-12-09 03:24:30 +00007286 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007287
7288 DSAStack->setParentTeamsRegionLoc(StartLoc);
7289
Kelvin Li7ade93f2016-12-09 03:24:30 +00007290 return OMPTeamsDistributeParallelForDirective::Create(
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00007291 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7292 DSAStack->isCancelRegion());
Kelvin Li7ade93f2016-12-09 03:24:30 +00007293}
7294
Kelvin Libf594a52016-12-17 05:48:59 +00007295StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses,
7296 Stmt *AStmt,
7297 SourceLocation StartLoc,
7298 SourceLocation EndLoc) {
7299 if (!AStmt)
7300 return StmtError();
7301
7302 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7303 // 1.2.2 OpenMP Language Terminology
7304 // Structured block - An executable statement with a single entry at the
7305 // top and a single exit at the bottom.
7306 // The point of exit cannot be a branch out of the structured block.
7307 // longjmp() and throw() must not violate the entry/exit criteria.
7308 CS->getCapturedDecl()->setNothrow();
7309
Alexey Bataevf9fc42e2017-11-22 14:25:55 +00007310 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams);
7311 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7312 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7313 // 1.2.2 OpenMP Language Terminology
7314 // Structured block - An executable statement with a single entry at the
7315 // top and a single exit at the bottom.
7316 // The point of exit cannot be a branch out of the structured block.
7317 // longjmp() and throw() must not violate the entry/exit criteria.
7318 CS->getCapturedDecl()->setNothrow();
7319 }
Kelvin Libf594a52016-12-17 05:48:59 +00007320 getCurFunction()->setHasBranchProtectedScope();
7321
7322 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses,
7323 AStmt);
7324}
7325
Kelvin Li83c451e2016-12-25 04:52:54 +00007326StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective(
7327 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7328 SourceLocation EndLoc,
7329 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7330 if (!AStmt)
7331 return StmtError();
7332
7333 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7334 // 1.2.2 OpenMP Language Terminology
7335 // Structured block - An executable statement with a single entry at the
7336 // top and a single exit at the bottom.
7337 // The point of exit cannot be a branch out of the structured block.
7338 // longjmp() and throw() must not violate the entry/exit criteria.
7339 CS->getCapturedDecl()->setNothrow();
Alexey Bataevdfa430f2017-12-08 15:03:50 +00007340 for (int ThisCaptureLevel =
7341 getOpenMPCaptureLevels(OMPD_target_teams_distribute);
7342 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7343 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7344 // 1.2.2 OpenMP Language Terminology
7345 // Structured block - An executable statement with a single entry at the
7346 // top and a single exit at the bottom.
7347 // The point of exit cannot be a branch out of the structured block.
7348 // longjmp() and throw() must not violate the entry/exit criteria.
7349 CS->getCapturedDecl()->setNothrow();
7350 }
Kelvin Li83c451e2016-12-25 04:52:54 +00007351
7352 OMPLoopDirective::HelperExprs B;
7353 // In presence of clause 'collapse' with number of loops, it will
7354 // define the nested loops number.
7355 auto NestedLoopCount = CheckOpenMPLoop(
Alexey Bataevdfa430f2017-12-08 15:03:50 +00007356 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses),
7357 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li83c451e2016-12-25 04:52:54 +00007358 VarsWithImplicitDSA, B);
7359 if (NestedLoopCount == 0)
7360 return StmtError();
7361
7362 assert((CurContext->isDependentContext() || B.builtAll()) &&
7363 "omp target teams distribute loop exprs were not built");
7364
7365 getCurFunction()->setHasBranchProtectedScope();
7366 return OMPTargetTeamsDistributeDirective::Create(
7367 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7368}
7369
Kelvin Li80e8f562016-12-29 22:16:30 +00007370StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective(
7371 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7372 SourceLocation EndLoc,
7373 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7374 if (!AStmt)
7375 return StmtError();
7376
7377 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
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
7385 OMPLoopDirective::HelperExprs B;
7386 // In presence of clause 'collapse' with number of loops, it will
7387 // define the nested loops number.
7388 auto NestedLoopCount = CheckOpenMPLoop(
7389 OMPD_target_teams_distribute_parallel_for,
7390 getCollapseNumberExpr(Clauses),
7391 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
7392 VarsWithImplicitDSA, B);
7393 if (NestedLoopCount == 0)
7394 return StmtError();
7395
7396 assert((CurContext->isDependentContext() || B.builtAll()) &&
7397 "omp target teams distribute parallel for loop exprs were not built");
7398
Kelvin Li80e8f562016-12-29 22:16:30 +00007399 getCurFunction()->setHasBranchProtectedScope();
7400 return OMPTargetTeamsDistributeParallelForDirective::Create(
Alexey Bataev16e79882017-11-22 21:12:03 +00007401 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7402 DSAStack->isCancelRegion());
Kelvin Li80e8f562016-12-29 22:16:30 +00007403}
7404
Kelvin Li1851df52017-01-03 05:23:48 +00007405StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
7406 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7407 SourceLocation EndLoc,
7408 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7409 if (!AStmt)
7410 return StmtError();
7411
7412 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7413 // 1.2.2 OpenMP Language Terminology
7414 // Structured block - An executable statement with a single entry at the
7415 // top and a single exit at the bottom.
7416 // The point of exit cannot be a branch out of the structured block.
7417 // longjmp() and throw() must not violate the entry/exit criteria.
7418 CS->getCapturedDecl()->setNothrow();
7419
7420 OMPLoopDirective::HelperExprs B;
7421 // In presence of clause 'collapse' with number of loops, it will
7422 // define the nested loops number.
7423 auto NestedLoopCount = CheckOpenMPLoop(
7424 OMPD_target_teams_distribute_parallel_for_simd,
7425 getCollapseNumberExpr(Clauses),
7426 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
7427 VarsWithImplicitDSA, B);
7428 if (NestedLoopCount == 0)
7429 return StmtError();
7430
7431 assert((CurContext->isDependentContext() || B.builtAll()) &&
7432 "omp target teams distribute parallel for simd loop exprs were not "
7433 "built");
7434
7435 if (!CurContext->isDependentContext()) {
7436 // Finalize the clauses that need pre-built expressions for CodeGen.
7437 for (auto C : Clauses) {
7438 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7439 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7440 B.NumIterations, *this, CurScope,
7441 DSAStack))
7442 return StmtError();
7443 }
7444 }
7445
Alexey Bataev438388c2017-11-22 18:34:02 +00007446 if (checkSimdlenSafelenSpecified(*this, Clauses))
7447 return StmtError();
7448
Kelvin Li1851df52017-01-03 05:23:48 +00007449 getCurFunction()->setHasBranchProtectedScope();
7450 return OMPTargetTeamsDistributeParallelForSimdDirective::Create(
7451 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7452}
7453
Kelvin Lida681182017-01-10 18:08:18 +00007454StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective(
7455 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7456 SourceLocation EndLoc,
7457 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7458 if (!AStmt)
7459 return StmtError();
7460
7461 auto *CS = cast<CapturedStmt>(AStmt);
7462 // 1.2.2 OpenMP Language Terminology
7463 // Structured block - An executable statement with a single entry at the
7464 // top and a single exit at the bottom.
7465 // The point of exit cannot be a branch out of the structured block.
7466 // longjmp() and throw() must not violate the entry/exit criteria.
7467 CS->getCapturedDecl()->setNothrow();
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00007468 for (int ThisCaptureLevel =
7469 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd);
7470 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7471 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7472 // 1.2.2 OpenMP Language Terminology
7473 // Structured block - An executable statement with a single entry at the
7474 // top and a single exit at the bottom.
7475 // The point of exit cannot be a branch out of the structured block.
7476 // longjmp() and throw() must not violate the entry/exit criteria.
7477 CS->getCapturedDecl()->setNothrow();
7478 }
Kelvin Lida681182017-01-10 18:08:18 +00007479
7480 OMPLoopDirective::HelperExprs B;
7481 // In presence of clause 'collapse' with number of loops, it will
7482 // define the nested loops number.
7483 auto NestedLoopCount = CheckOpenMPLoop(
7484 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00007485 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Lida681182017-01-10 18:08:18 +00007486 VarsWithImplicitDSA, B);
7487 if (NestedLoopCount == 0)
7488 return StmtError();
7489
7490 assert((CurContext->isDependentContext() || B.builtAll()) &&
7491 "omp target teams distribute simd loop exprs were not built");
7492
Alexey Bataev438388c2017-11-22 18:34:02 +00007493 if (!CurContext->isDependentContext()) {
7494 // Finalize the clauses that need pre-built expressions for CodeGen.
7495 for (auto C : Clauses) {
7496 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7497 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7498 B.NumIterations, *this, CurScope,
7499 DSAStack))
7500 return StmtError();
7501 }
7502 }
7503
7504 if (checkSimdlenSafelenSpecified(*this, Clauses))
7505 return StmtError();
7506
Kelvin Lida681182017-01-10 18:08:18 +00007507 getCurFunction()->setHasBranchProtectedScope();
7508 return OMPTargetTeamsDistributeSimdDirective::Create(
7509 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7510}
7511
Alexey Bataeved09d242014-05-28 05:53:51 +00007512OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007513 SourceLocation StartLoc,
7514 SourceLocation LParenLoc,
7515 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007516 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007517 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00007518 case OMPC_final:
7519 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
7520 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00007521 case OMPC_num_threads:
7522 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
7523 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007524 case OMPC_safelen:
7525 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
7526 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00007527 case OMPC_simdlen:
7528 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
7529 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00007530 case OMPC_collapse:
7531 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
7532 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00007533 case OMPC_ordered:
7534 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
7535 break;
Michael Wonge710d542015-08-07 16:16:36 +00007536 case OMPC_device:
7537 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
7538 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00007539 case OMPC_num_teams:
7540 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
7541 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007542 case OMPC_thread_limit:
7543 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
7544 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00007545 case OMPC_priority:
7546 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
7547 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007548 case OMPC_grainsize:
7549 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
7550 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00007551 case OMPC_num_tasks:
7552 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
7553 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00007554 case OMPC_hint:
7555 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
7556 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007557 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007558 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007559 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007560 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007561 case OMPC_private:
7562 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00007563 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007564 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007565 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00007566 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00007567 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00007568 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007569 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007570 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007571 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00007572 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007573 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007574 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007575 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007576 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007577 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007578 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007579 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007580 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007581 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007582 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00007583 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007584 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007585 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00007586 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007587 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007588 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007589 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007590 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007591 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007592 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007593 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007594 case OMPC_is_device_ptr:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007595 llvm_unreachable("Clause is not allowed.");
7596 }
7597 return Res;
7598}
7599
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00007600// An OpenMP directive such as 'target parallel' has two captured regions:
7601// for the 'target' and 'parallel' respectively. This function returns
7602// the region in which to capture expressions associated with a clause.
7603// A return value of OMPD_unknown signifies that the expression should not
7604// be captured.
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00007605static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
7606 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
7607 OpenMPDirectiveKind NameModifier = OMPD_unknown) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00007608 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00007609 switch (CKind) {
7610 case OMPC_if:
7611 switch (DKind) {
7612 case OMPD_target_parallel:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00007613 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007614 case OMPD_target_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00007615 case OMPD_target_teams_distribute_parallel_for:
7616 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00007617 // If this clause applies to the nested 'parallel' region, capture within
7618 // the 'target' region, otherwise do not capture.
7619 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
7620 CaptureRegion = OMPD_target;
7621 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +00007622 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00007623 case OMPD_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00007624 CaptureRegion = OMPD_teams;
7625 break;
Alexey Bataevd2202ca2017-12-27 17:58:32 +00007626 case OMPD_target_update:
Alexey Bataevfab20e42017-12-27 18:49:38 +00007627 case OMPD_target_enter_data:
7628 case OMPD_target_exit_data:
Alexey Bataevd2202ca2017-12-27 17:58:32 +00007629 CaptureRegion = OMPD_task;
7630 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00007631 case OMPD_cancel:
7632 case OMPD_parallel:
7633 case OMPD_parallel_sections:
7634 case OMPD_parallel_for:
7635 case OMPD_parallel_for_simd:
7636 case OMPD_target:
7637 case OMPD_target_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00007638 case OMPD_target_teams:
7639 case OMPD_target_teams_distribute:
7640 case OMPD_target_teams_distribute_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00007641 case OMPD_distribute_parallel_for:
7642 case OMPD_distribute_parallel_for_simd:
7643 case OMPD_task:
7644 case OMPD_taskloop:
7645 case OMPD_taskloop_simd:
7646 case OMPD_target_data:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00007647 // Do not capture if-clause expressions.
7648 break;
7649 case OMPD_threadprivate:
7650 case OMPD_taskyield:
7651 case OMPD_barrier:
7652 case OMPD_taskwait:
7653 case OMPD_cancellation_point:
7654 case OMPD_flush:
7655 case OMPD_declare_reduction:
7656 case OMPD_declare_simd:
7657 case OMPD_declare_target:
7658 case OMPD_end_declare_target:
7659 case OMPD_teams:
7660 case OMPD_simd:
7661 case OMPD_for:
7662 case OMPD_for_simd:
7663 case OMPD_sections:
7664 case OMPD_section:
7665 case OMPD_single:
7666 case OMPD_master:
7667 case OMPD_critical:
7668 case OMPD_taskgroup:
7669 case OMPD_distribute:
7670 case OMPD_ordered:
7671 case OMPD_atomic:
7672 case OMPD_distribute_simd:
7673 case OMPD_teams_distribute:
7674 case OMPD_teams_distribute_simd:
7675 llvm_unreachable("Unexpected OpenMP directive with if-clause");
7676 case OMPD_unknown:
7677 llvm_unreachable("Unknown OpenMP directive");
7678 }
7679 break;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00007680 case OMPC_num_threads:
7681 switch (DKind) {
7682 case OMPD_target_parallel:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00007683 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007684 case OMPD_target_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00007685 case OMPD_target_teams_distribute_parallel_for:
7686 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00007687 CaptureRegion = OMPD_target;
7688 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +00007689 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00007690 case OMPD_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00007691 CaptureRegion = OMPD_teams;
7692 break;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00007693 case OMPD_parallel:
7694 case OMPD_parallel_sections:
7695 case OMPD_parallel_for:
7696 case OMPD_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00007697 case OMPD_distribute_parallel_for:
7698 case OMPD_distribute_parallel_for_simd:
7699 // Do not capture num_threads-clause expressions.
7700 break;
7701 case OMPD_target_data:
7702 case OMPD_target_enter_data:
7703 case OMPD_target_exit_data:
7704 case OMPD_target_update:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00007705 case OMPD_target:
7706 case OMPD_target_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00007707 case OMPD_target_teams:
7708 case OMPD_target_teams_distribute:
7709 case OMPD_target_teams_distribute_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00007710 case OMPD_cancel:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00007711 case OMPD_task:
7712 case OMPD_taskloop:
7713 case OMPD_taskloop_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00007714 case OMPD_threadprivate:
7715 case OMPD_taskyield:
7716 case OMPD_barrier:
7717 case OMPD_taskwait:
7718 case OMPD_cancellation_point:
7719 case OMPD_flush:
7720 case OMPD_declare_reduction:
7721 case OMPD_declare_simd:
7722 case OMPD_declare_target:
7723 case OMPD_end_declare_target:
7724 case OMPD_teams:
7725 case OMPD_simd:
7726 case OMPD_for:
7727 case OMPD_for_simd:
7728 case OMPD_sections:
7729 case OMPD_section:
7730 case OMPD_single:
7731 case OMPD_master:
7732 case OMPD_critical:
7733 case OMPD_taskgroup:
7734 case OMPD_distribute:
7735 case OMPD_ordered:
7736 case OMPD_atomic:
7737 case OMPD_distribute_simd:
7738 case OMPD_teams_distribute:
7739 case OMPD_teams_distribute_simd:
7740 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause");
7741 case OMPD_unknown:
7742 llvm_unreachable("Unknown OpenMP directive");
7743 }
7744 break;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00007745 case OMPC_num_teams:
7746 switch (DKind) {
7747 case OMPD_target_teams:
Alexey Bataev2ba67042017-11-28 21:11:44 +00007748 case OMPD_target_teams_distribute:
7749 case OMPD_target_teams_distribute_simd:
7750 case OMPD_target_teams_distribute_parallel_for:
7751 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00007752 CaptureRegion = OMPD_target;
7753 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00007754 case OMPD_teams_distribute_parallel_for:
7755 case OMPD_teams_distribute_parallel_for_simd:
7756 case OMPD_teams:
7757 case OMPD_teams_distribute:
7758 case OMPD_teams_distribute_simd:
7759 // Do not capture num_teams-clause expressions.
7760 break;
7761 case OMPD_distribute_parallel_for:
7762 case OMPD_distribute_parallel_for_simd:
7763 case OMPD_task:
7764 case OMPD_taskloop:
7765 case OMPD_taskloop_simd:
7766 case OMPD_target_data:
7767 case OMPD_target_enter_data:
7768 case OMPD_target_exit_data:
7769 case OMPD_target_update:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00007770 case OMPD_cancel:
7771 case OMPD_parallel:
7772 case OMPD_parallel_sections:
7773 case OMPD_parallel_for:
7774 case OMPD_parallel_for_simd:
7775 case OMPD_target:
7776 case OMPD_target_simd:
7777 case OMPD_target_parallel:
7778 case OMPD_target_parallel_for:
7779 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00007780 case OMPD_threadprivate:
7781 case OMPD_taskyield:
7782 case OMPD_barrier:
7783 case OMPD_taskwait:
7784 case OMPD_cancellation_point:
7785 case OMPD_flush:
7786 case OMPD_declare_reduction:
7787 case OMPD_declare_simd:
7788 case OMPD_declare_target:
7789 case OMPD_end_declare_target:
7790 case OMPD_simd:
7791 case OMPD_for:
7792 case OMPD_for_simd:
7793 case OMPD_sections:
7794 case OMPD_section:
7795 case OMPD_single:
7796 case OMPD_master:
7797 case OMPD_critical:
7798 case OMPD_taskgroup:
7799 case OMPD_distribute:
7800 case OMPD_ordered:
7801 case OMPD_atomic:
7802 case OMPD_distribute_simd:
7803 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
7804 case OMPD_unknown:
7805 llvm_unreachable("Unknown OpenMP directive");
7806 }
7807 break;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00007808 case OMPC_thread_limit:
7809 switch (DKind) {
7810 case OMPD_target_teams:
Alexey Bataev2ba67042017-11-28 21:11:44 +00007811 case OMPD_target_teams_distribute:
7812 case OMPD_target_teams_distribute_simd:
7813 case OMPD_target_teams_distribute_parallel_for:
7814 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00007815 CaptureRegion = OMPD_target;
7816 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00007817 case OMPD_teams_distribute_parallel_for:
7818 case OMPD_teams_distribute_parallel_for_simd:
7819 case OMPD_teams:
7820 case OMPD_teams_distribute:
7821 case OMPD_teams_distribute_simd:
7822 // Do not capture thread_limit-clause expressions.
7823 break;
7824 case OMPD_distribute_parallel_for:
7825 case OMPD_distribute_parallel_for_simd:
7826 case OMPD_task:
7827 case OMPD_taskloop:
7828 case OMPD_taskloop_simd:
7829 case OMPD_target_data:
7830 case OMPD_target_enter_data:
7831 case OMPD_target_exit_data:
7832 case OMPD_target_update:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00007833 case OMPD_cancel:
7834 case OMPD_parallel:
7835 case OMPD_parallel_sections:
7836 case OMPD_parallel_for:
7837 case OMPD_parallel_for_simd:
7838 case OMPD_target:
7839 case OMPD_target_simd:
7840 case OMPD_target_parallel:
7841 case OMPD_target_parallel_for:
7842 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00007843 case OMPD_threadprivate:
7844 case OMPD_taskyield:
7845 case OMPD_barrier:
7846 case OMPD_taskwait:
7847 case OMPD_cancellation_point:
7848 case OMPD_flush:
7849 case OMPD_declare_reduction:
7850 case OMPD_declare_simd:
7851 case OMPD_declare_target:
7852 case OMPD_end_declare_target:
7853 case OMPD_simd:
7854 case OMPD_for:
7855 case OMPD_for_simd:
7856 case OMPD_sections:
7857 case OMPD_section:
7858 case OMPD_single:
7859 case OMPD_master:
7860 case OMPD_critical:
7861 case OMPD_taskgroup:
7862 case OMPD_distribute:
7863 case OMPD_ordered:
7864 case OMPD_atomic:
7865 case OMPD_distribute_simd:
7866 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause");
7867 case OMPD_unknown:
7868 llvm_unreachable("Unknown OpenMP directive");
7869 }
7870 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00007871 case OMPC_schedule:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00007872 switch (DKind) {
7873 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007874 case OMPD_target_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00007875 case OMPD_target_teams_distribute_parallel_for:
7876 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00007877 CaptureRegion = OMPD_target;
7878 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +00007879 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00007880 case OMPD_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00007881 CaptureRegion = OMPD_teams;
7882 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00007883 case OMPD_parallel_for:
7884 case OMPD_parallel_for_simd:
Alexey Bataev7f96c372017-11-22 17:19:31 +00007885 case OMPD_distribute_parallel_for:
Alexey Bataev974acd62017-11-27 19:38:52 +00007886 case OMPD_distribute_parallel_for_simd:
Alexey Bataev7f96c372017-11-22 17:19:31 +00007887 CaptureRegion = OMPD_parallel;
7888 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00007889 case OMPD_for:
7890 case OMPD_for_simd:
7891 // Do not capture schedule-clause expressions.
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00007892 break;
7893 case OMPD_task:
7894 case OMPD_taskloop:
7895 case OMPD_taskloop_simd:
7896 case OMPD_target_data:
7897 case OMPD_target_enter_data:
7898 case OMPD_target_exit_data:
7899 case OMPD_target_update:
7900 case OMPD_teams:
7901 case OMPD_teams_distribute:
7902 case OMPD_teams_distribute_simd:
7903 case OMPD_target_teams_distribute:
7904 case OMPD_target_teams_distribute_simd:
7905 case OMPD_target:
7906 case OMPD_target_simd:
7907 case OMPD_target_parallel:
7908 case OMPD_cancel:
7909 case OMPD_parallel:
7910 case OMPD_parallel_sections:
7911 case OMPD_threadprivate:
7912 case OMPD_taskyield:
7913 case OMPD_barrier:
7914 case OMPD_taskwait:
7915 case OMPD_cancellation_point:
7916 case OMPD_flush:
7917 case OMPD_declare_reduction:
7918 case OMPD_declare_simd:
7919 case OMPD_declare_target:
7920 case OMPD_end_declare_target:
7921 case OMPD_simd:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00007922 case OMPD_sections:
7923 case OMPD_section:
7924 case OMPD_single:
7925 case OMPD_master:
7926 case OMPD_critical:
7927 case OMPD_taskgroup:
7928 case OMPD_distribute:
7929 case OMPD_ordered:
7930 case OMPD_atomic:
7931 case OMPD_distribute_simd:
7932 case OMPD_target_teams:
7933 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
7934 case OMPD_unknown:
7935 llvm_unreachable("Unknown OpenMP directive");
7936 }
7937 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00007938 case OMPC_dist_schedule:
Carlo Bertolli62fae152017-11-20 20:46:39 +00007939 switch (DKind) {
7940 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00007941 case OMPD_teams_distribute_parallel_for_simd:
7942 case OMPD_teams_distribute:
7943 case OMPD_teams_distribute_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00007944 CaptureRegion = OMPD_teams;
7945 break;
7946 case OMPD_target_teams_distribute_parallel_for:
7947 case OMPD_target_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00007948 case OMPD_target_teams_distribute:
7949 case OMPD_target_teams_distribute_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00007950 CaptureRegion = OMPD_target;
7951 break;
7952 case OMPD_distribute_parallel_for:
7953 case OMPD_distribute_parallel_for_simd:
7954 CaptureRegion = OMPD_parallel;
7955 break;
7956 case OMPD_distribute:
Carlo Bertolli62fae152017-11-20 20:46:39 +00007957 case OMPD_distribute_simd:
7958 // Do not capture thread_limit-clause expressions.
7959 break;
7960 case OMPD_parallel_for:
7961 case OMPD_parallel_for_simd:
7962 case OMPD_target_parallel_for_simd:
7963 case OMPD_target_parallel_for:
7964 case OMPD_task:
7965 case OMPD_taskloop:
7966 case OMPD_taskloop_simd:
7967 case OMPD_target_data:
7968 case OMPD_target_enter_data:
7969 case OMPD_target_exit_data:
7970 case OMPD_target_update:
7971 case OMPD_teams:
7972 case OMPD_target:
7973 case OMPD_target_simd:
7974 case OMPD_target_parallel:
7975 case OMPD_cancel:
7976 case OMPD_parallel:
7977 case OMPD_parallel_sections:
7978 case OMPD_threadprivate:
7979 case OMPD_taskyield:
7980 case OMPD_barrier:
7981 case OMPD_taskwait:
7982 case OMPD_cancellation_point:
7983 case OMPD_flush:
7984 case OMPD_declare_reduction:
7985 case OMPD_declare_simd:
7986 case OMPD_declare_target:
7987 case OMPD_end_declare_target:
7988 case OMPD_simd:
7989 case OMPD_for:
7990 case OMPD_for_simd:
7991 case OMPD_sections:
7992 case OMPD_section:
7993 case OMPD_single:
7994 case OMPD_master:
7995 case OMPD_critical:
7996 case OMPD_taskgroup:
Carlo Bertolli62fae152017-11-20 20:46:39 +00007997 case OMPD_ordered:
7998 case OMPD_atomic:
7999 case OMPD_target_teams:
8000 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
8001 case OMPD_unknown:
8002 llvm_unreachable("Unknown OpenMP directive");
8003 }
8004 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008005 case OMPC_device:
8006 switch (DKind) {
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008007 case OMPD_target_update:
Alexey Bataevfab20e42017-12-27 18:49:38 +00008008 case OMPD_target_enter_data:
8009 case OMPD_target_exit_data:
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008010 CaptureRegion = OMPD_task;
8011 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008012 case OMPD_target_teams:
8013 case OMPD_target_teams_distribute:
8014 case OMPD_target_teams_distribute_simd:
8015 case OMPD_target_teams_distribute_parallel_for:
8016 case OMPD_target_teams_distribute_parallel_for_simd:
8017 case OMPD_target_data:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008018 case OMPD_target:
8019 case OMPD_target_simd:
8020 case OMPD_target_parallel:
8021 case OMPD_target_parallel_for:
8022 case OMPD_target_parallel_for_simd:
8023 // Do not capture device-clause expressions.
8024 break;
8025 case OMPD_teams_distribute_parallel_for:
8026 case OMPD_teams_distribute_parallel_for_simd:
8027 case OMPD_teams:
8028 case OMPD_teams_distribute:
8029 case OMPD_teams_distribute_simd:
8030 case OMPD_distribute_parallel_for:
8031 case OMPD_distribute_parallel_for_simd:
8032 case OMPD_task:
8033 case OMPD_taskloop:
8034 case OMPD_taskloop_simd:
8035 case OMPD_cancel:
8036 case OMPD_parallel:
8037 case OMPD_parallel_sections:
8038 case OMPD_parallel_for:
8039 case OMPD_parallel_for_simd:
8040 case OMPD_threadprivate:
8041 case OMPD_taskyield:
8042 case OMPD_barrier:
8043 case OMPD_taskwait:
8044 case OMPD_cancellation_point:
8045 case OMPD_flush:
8046 case OMPD_declare_reduction:
8047 case OMPD_declare_simd:
8048 case OMPD_declare_target:
8049 case OMPD_end_declare_target:
8050 case OMPD_simd:
8051 case OMPD_for:
8052 case OMPD_for_simd:
8053 case OMPD_sections:
8054 case OMPD_section:
8055 case OMPD_single:
8056 case OMPD_master:
8057 case OMPD_critical:
8058 case OMPD_taskgroup:
8059 case OMPD_distribute:
8060 case OMPD_ordered:
8061 case OMPD_atomic:
8062 case OMPD_distribute_simd:
8063 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
8064 case OMPD_unknown:
8065 llvm_unreachable("Unknown OpenMP directive");
8066 }
8067 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008068 case OMPC_firstprivate:
8069 case OMPC_lastprivate:
8070 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008071 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008072 case OMPC_in_reduction:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008073 case OMPC_linear:
8074 case OMPC_default:
8075 case OMPC_proc_bind:
8076 case OMPC_final:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008077 case OMPC_safelen:
8078 case OMPC_simdlen:
8079 case OMPC_collapse:
8080 case OMPC_private:
8081 case OMPC_shared:
8082 case OMPC_aligned:
8083 case OMPC_copyin:
8084 case OMPC_copyprivate:
8085 case OMPC_ordered:
8086 case OMPC_nowait:
8087 case OMPC_untied:
8088 case OMPC_mergeable:
8089 case OMPC_threadprivate:
8090 case OMPC_flush:
8091 case OMPC_read:
8092 case OMPC_write:
8093 case OMPC_update:
8094 case OMPC_capture:
8095 case OMPC_seq_cst:
8096 case OMPC_depend:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008097 case OMPC_threads:
8098 case OMPC_simd:
8099 case OMPC_map:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008100 case OMPC_priority:
8101 case OMPC_grainsize:
8102 case OMPC_nogroup:
8103 case OMPC_num_tasks:
8104 case OMPC_hint:
8105 case OMPC_defaultmap:
8106 case OMPC_unknown:
8107 case OMPC_uniform:
8108 case OMPC_to:
8109 case OMPC_from:
8110 case OMPC_use_device_ptr:
8111 case OMPC_is_device_ptr:
8112 llvm_unreachable("Unexpected OpenMP clause.");
8113 }
8114 return CaptureRegion;
8115}
8116
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008117OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
8118 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008119 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008120 SourceLocation NameModifierLoc,
8121 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008122 SourceLocation EndLoc) {
8123 Expr *ValExpr = Condition;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008124 Stmt *HelperValStmt = nullptr;
8125 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008126 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
8127 !Condition->isInstantiationDependent() &&
8128 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00008129 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008130 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008131 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008132
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008133 ValExpr = Val.get();
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008134
8135 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
8136 CaptureRegion =
8137 getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier);
Alexey Bataev2ba67042017-11-28 21:11:44 +00008138 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008139 ValExpr = MakeFullExpr(ValExpr).get();
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008140 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
8141 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8142 HelperValStmt = buildPreInits(Context, Captures);
8143 }
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008144 }
8145
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008146 return new (Context)
8147 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
8148 LParenLoc, NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008149}
8150
Alexey Bataev3778b602014-07-17 07:32:53 +00008151OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
8152 SourceLocation StartLoc,
8153 SourceLocation LParenLoc,
8154 SourceLocation EndLoc) {
8155 Expr *ValExpr = Condition;
8156 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
8157 !Condition->isInstantiationDependent() &&
8158 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00008159 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataev3778b602014-07-17 07:32:53 +00008160 if (Val.isInvalid())
8161 return nullptr;
8162
Richard Smith03a4aa32016-06-23 19:02:52 +00008163 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataev3778b602014-07-17 07:32:53 +00008164 }
8165
8166 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8167}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00008168ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
8169 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00008170 if (!Op)
8171 return ExprError();
8172
8173 class IntConvertDiagnoser : public ICEConvertDiagnoser {
8174 public:
8175 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00008176 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00008177 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
8178 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008179 return S.Diag(Loc, diag::err_omp_not_integral) << T;
8180 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008181 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
8182 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008183 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
8184 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008185 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
8186 QualType T,
8187 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008188 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
8189 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008190 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
8191 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008192 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00008193 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00008194 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008195 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
8196 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008197 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
8198 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008199 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
8200 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008201 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00008202 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00008203 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008204 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
8205 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008206 llvm_unreachable("conversion functions are permitted");
8207 }
8208 } ConvertDiagnoser;
8209 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
8210}
8211
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008212static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00008213 OpenMPClauseKind CKind,
8214 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008215 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
8216 !ValExpr->isInstantiationDependent()) {
8217 SourceLocation Loc = ValExpr->getExprLoc();
8218 ExprResult Value =
8219 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
8220 if (Value.isInvalid())
8221 return false;
8222
8223 ValExpr = Value.get();
8224 // The expression must evaluate to a non-negative integer value.
8225 llvm::APSInt Result;
8226 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00008227 Result.isSigned() &&
8228 !((!StrictlyPositive && Result.isNonNegative()) ||
8229 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008230 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00008231 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
8232 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008233 return false;
8234 }
8235 }
8236 return true;
8237}
8238
Alexey Bataev568a8332014-03-06 06:15:19 +00008239OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
8240 SourceLocation StartLoc,
8241 SourceLocation LParenLoc,
8242 SourceLocation EndLoc) {
8243 Expr *ValExpr = NumThreads;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008244 Stmt *HelperValStmt = nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00008245
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008246 // OpenMP [2.5, Restrictions]
8247 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008248 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
8249 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008250 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00008251
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008252 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +00008253 OpenMPDirectiveKind CaptureRegion =
8254 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads);
8255 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008256 ValExpr = MakeFullExpr(ValExpr).get();
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008257 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
8258 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8259 HelperValStmt = buildPreInits(Context, Captures);
8260 }
8261
8262 return new (Context) OMPNumThreadsClause(
8263 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00008264}
8265
Alexey Bataev62c87d22014-03-21 04:51:18 +00008266ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008267 OpenMPClauseKind CKind,
8268 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00008269 if (!E)
8270 return ExprError();
8271 if (E->isValueDependent() || E->isTypeDependent() ||
8272 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00008273 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00008274 llvm::APSInt Result;
8275 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
8276 if (ICE.isInvalid())
8277 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008278 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
8279 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00008280 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008281 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
8282 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00008283 return ExprError();
8284 }
Alexander Musman09184fe2014-09-30 05:29:28 +00008285 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
8286 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
8287 << E->getSourceRange();
8288 return ExprError();
8289 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008290 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
8291 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00008292 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008293 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00008294 return ICE;
8295}
8296
8297OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
8298 SourceLocation LParenLoc,
8299 SourceLocation EndLoc) {
8300 // OpenMP [2.8.1, simd construct, Description]
8301 // The parameter of the safelen clause must be a constant
8302 // positive integer expression.
8303 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
8304 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008305 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00008306 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00008307 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00008308}
8309
Alexey Bataev66b15b52015-08-21 11:14:16 +00008310OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
8311 SourceLocation LParenLoc,
8312 SourceLocation EndLoc) {
8313 // OpenMP [2.8.1, simd construct, Description]
8314 // The parameter of the simdlen clause must be a constant
8315 // positive integer expression.
8316 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
8317 if (Simdlen.isInvalid())
8318 return nullptr;
8319 return new (Context)
8320 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
8321}
8322
Alexander Musman64d33f12014-06-04 07:53:32 +00008323OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
8324 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00008325 SourceLocation LParenLoc,
8326 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00008327 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00008328 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00008329 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00008330 // The parameter of the collapse clause must be a constant
8331 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00008332 ExprResult NumForLoopsResult =
8333 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
8334 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00008335 return nullptr;
8336 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00008337 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00008338}
8339
Alexey Bataev10e775f2015-07-30 11:36:16 +00008340OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
8341 SourceLocation EndLoc,
8342 SourceLocation LParenLoc,
8343 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00008344 // OpenMP [2.7.1, loop construct, Description]
8345 // OpenMP [2.8.1, simd construct, Description]
8346 // OpenMP [2.9.6, distribute construct, Description]
8347 // The parameter of the ordered clause must be a constant
8348 // positive integer expression if any.
8349 if (NumForLoops && LParenLoc.isValid()) {
8350 ExprResult NumForLoopsResult =
8351 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
8352 if (NumForLoopsResult.isInvalid())
8353 return nullptr;
8354 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00008355 } else
8356 NumForLoops = nullptr;
8357 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00008358 return new (Context)
8359 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
8360}
8361
Alexey Bataeved09d242014-05-28 05:53:51 +00008362OMPClause *Sema::ActOnOpenMPSimpleClause(
8363 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
8364 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008365 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008366 switch (Kind) {
8367 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00008368 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00008369 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
8370 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008371 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008372 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00008373 Res = ActOnOpenMPProcBindClause(
8374 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
8375 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008376 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008377 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00008378 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00008379 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00008380 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008381 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00008382 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008383 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008384 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008385 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00008386 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00008387 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00008388 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008389 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008390 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00008391 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008392 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008393 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008394 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008395 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00008396 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008397 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008398 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008399 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00008400 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008401 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008402 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008403 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008404 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008405 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008406 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00008407 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00008408 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008409 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008410 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00008411 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008412 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00008413 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008414 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00008415 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00008416 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00008417 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008418 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008419 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008420 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008421 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00008422 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00008423 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00008424 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00008425 case OMPC_is_device_ptr:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008426 llvm_unreachable("Clause is not allowed.");
8427 }
8428 return Res;
8429}
8430
Alexey Bataev6402bca2015-12-28 07:25:51 +00008431static std::string
8432getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
8433 ArrayRef<unsigned> Exclude = llvm::None) {
8434 std::string Values;
8435 unsigned Bound = Last >= 2 ? Last - 2 : 0;
8436 unsigned Skipped = Exclude.size();
8437 auto S = Exclude.begin(), E = Exclude.end();
8438 for (unsigned i = First; i < Last; ++i) {
8439 if (std::find(S, E, i) != E) {
8440 --Skipped;
8441 continue;
8442 }
8443 Values += "'";
8444 Values += getOpenMPSimpleClauseTypeName(K, i);
8445 Values += "'";
8446 if (i == Bound - Skipped)
8447 Values += " or ";
8448 else if (i != Bound + 1 - Skipped)
8449 Values += ", ";
8450 }
8451 return Values;
8452}
8453
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008454OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
8455 SourceLocation KindKwLoc,
8456 SourceLocation StartLoc,
8457 SourceLocation LParenLoc,
8458 SourceLocation EndLoc) {
8459 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00008460 static_assert(OMPC_DEFAULT_unknown > 0,
8461 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008462 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008463 << getListOfPossibleValues(OMPC_default, /*First=*/0,
8464 /*Last=*/OMPC_DEFAULT_unknown)
8465 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008466 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008467 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00008468 switch (Kind) {
8469 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008470 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008471 break;
8472 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008473 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008474 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008475 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008476 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00008477 break;
8478 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008479 return new (Context)
8480 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008481}
8482
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008483OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
8484 SourceLocation KindKwLoc,
8485 SourceLocation StartLoc,
8486 SourceLocation LParenLoc,
8487 SourceLocation EndLoc) {
8488 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008489 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008490 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
8491 /*Last=*/OMPC_PROC_BIND_unknown)
8492 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008493 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008494 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008495 return new (Context)
8496 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008497}
8498
Alexey Bataev56dafe82014-06-20 07:16:17 +00008499OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00008500 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00008501 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00008502 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00008503 SourceLocation EndLoc) {
8504 OMPClause *Res = nullptr;
8505 switch (Kind) {
8506 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00008507 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
8508 assert(Argument.size() == NumberOfElements &&
8509 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00008510 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00008511 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
8512 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
8513 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
8514 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
8515 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00008516 break;
8517 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00008518 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
8519 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
8520 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
8521 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008522 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00008523 case OMPC_dist_schedule:
8524 Res = ActOnOpenMPDistScheduleClause(
8525 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
8526 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
8527 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008528 case OMPC_defaultmap:
8529 enum { Modifier, DefaultmapKind };
8530 Res = ActOnOpenMPDefaultmapClause(
8531 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
8532 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
David Majnemer9d168222016-08-05 17:44:54 +00008533 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind],
8534 EndLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008535 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00008536 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008537 case OMPC_num_threads:
8538 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008539 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008540 case OMPC_collapse:
8541 case OMPC_default:
8542 case OMPC_proc_bind:
8543 case OMPC_private:
8544 case OMPC_firstprivate:
8545 case OMPC_lastprivate:
8546 case OMPC_shared:
8547 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008548 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008549 case OMPC_in_reduction:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008550 case OMPC_linear:
8551 case OMPC_aligned:
8552 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008553 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008554 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00008555 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008556 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008557 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008558 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00008559 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008560 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008561 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008562 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008563 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008564 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008565 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00008566 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00008567 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008568 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008569 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00008570 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008571 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00008572 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008573 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00008574 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00008575 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00008576 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008577 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008578 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00008579 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00008580 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00008581 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00008582 case OMPC_is_device_ptr:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008583 llvm_unreachable("Clause is not allowed.");
8584 }
8585 return Res;
8586}
8587
Alexey Bataev6402bca2015-12-28 07:25:51 +00008588static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
8589 OpenMPScheduleClauseModifier M2,
8590 SourceLocation M1Loc, SourceLocation M2Loc) {
8591 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
8592 SmallVector<unsigned, 2> Excluded;
8593 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
8594 Excluded.push_back(M2);
8595 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
8596 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
8597 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
8598 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
8599 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
8600 << getListOfPossibleValues(OMPC_schedule,
8601 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
8602 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
8603 Excluded)
8604 << getOpenMPClauseName(OMPC_schedule);
8605 return true;
8606 }
8607 return false;
8608}
8609
Alexey Bataev56dafe82014-06-20 07:16:17 +00008610OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00008611 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00008612 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00008613 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
8614 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
8615 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
8616 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
8617 return nullptr;
8618 // OpenMP, 2.7.1, Loop Construct, Restrictions
8619 // Either the monotonic modifier or the nonmonotonic modifier can be specified
8620 // but not both.
8621 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
8622 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
8623 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
8624 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
8625 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
8626 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
8627 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
8628 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
8629 return nullptr;
8630 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00008631 if (Kind == OMPC_SCHEDULE_unknown) {
8632 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00008633 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
8634 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
8635 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
8636 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
8637 Exclude);
8638 } else {
8639 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
8640 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00008641 }
8642 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
8643 << Values << getOpenMPClauseName(OMPC_schedule);
8644 return nullptr;
8645 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00008646 // OpenMP, 2.7.1, Loop Construct, Restrictions
8647 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
8648 // schedule(guided).
8649 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
8650 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
8651 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
8652 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
8653 diag::err_omp_schedule_nonmonotonic_static);
8654 return nullptr;
8655 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00008656 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00008657 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00008658 if (ChunkSize) {
8659 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
8660 !ChunkSize->isInstantiationDependent() &&
8661 !ChunkSize->containsUnexpandedParameterPack()) {
8662 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
8663 ExprResult Val =
8664 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
8665 if (Val.isInvalid())
8666 return nullptr;
8667
8668 ValExpr = Val.get();
8669
8670 // OpenMP [2.7.1, Restrictions]
8671 // chunk_size must be a loop invariant integer expression with a positive
8672 // value.
8673 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00008674 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
8675 if (Result.isSigned() && !Result.isStrictlyPositive()) {
8676 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00008677 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00008678 return nullptr;
8679 }
Alexey Bataev2ba67042017-11-28 21:11:44 +00008680 } else if (getOpenMPCaptureRegionForClause(
8681 DSAStack->getCurrentDirective(), OMPC_schedule) !=
8682 OMPD_unknown &&
Alexey Bataevb46cdea2016-06-15 11:20:48 +00008683 !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008684 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00008685 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
8686 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8687 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00008688 }
8689 }
8690 }
8691
Alexey Bataev6402bca2015-12-28 07:25:51 +00008692 return new (Context)
8693 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00008694 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00008695}
8696
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008697OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
8698 SourceLocation StartLoc,
8699 SourceLocation EndLoc) {
8700 OMPClause *Res = nullptr;
8701 switch (Kind) {
8702 case OMPC_ordered:
8703 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
8704 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00008705 case OMPC_nowait:
8706 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
8707 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008708 case OMPC_untied:
8709 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
8710 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008711 case OMPC_mergeable:
8712 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
8713 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008714 case OMPC_read:
8715 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
8716 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00008717 case OMPC_write:
8718 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
8719 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00008720 case OMPC_update:
8721 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
8722 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00008723 case OMPC_capture:
8724 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
8725 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008726 case OMPC_seq_cst:
8727 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
8728 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00008729 case OMPC_threads:
8730 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
8731 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008732 case OMPC_simd:
8733 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
8734 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00008735 case OMPC_nogroup:
8736 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
8737 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008738 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00008739 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008740 case OMPC_num_threads:
8741 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008742 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008743 case OMPC_collapse:
8744 case OMPC_schedule:
8745 case OMPC_private:
8746 case OMPC_firstprivate:
8747 case OMPC_lastprivate:
8748 case OMPC_shared:
8749 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008750 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008751 case OMPC_in_reduction:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008752 case OMPC_linear:
8753 case OMPC_aligned:
8754 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008755 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008756 case OMPC_default:
8757 case OMPC_proc_bind:
8758 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00008759 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008760 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00008761 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008762 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00008763 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008764 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00008765 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008766 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00008767 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00008768 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008769 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008770 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008771 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008772 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00008773 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00008774 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00008775 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00008776 case OMPC_is_device_ptr:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008777 llvm_unreachable("Clause is not allowed.");
8778 }
8779 return Res;
8780}
8781
Alexey Bataev236070f2014-06-20 11:19:47 +00008782OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
8783 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00008784 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00008785 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
8786}
8787
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008788OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
8789 SourceLocation EndLoc) {
8790 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
8791}
8792
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008793OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
8794 SourceLocation EndLoc) {
8795 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
8796}
8797
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008798OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
8799 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008800 return new (Context) OMPReadClause(StartLoc, EndLoc);
8801}
8802
Alexey Bataevdea47612014-07-23 07:46:59 +00008803OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
8804 SourceLocation EndLoc) {
8805 return new (Context) OMPWriteClause(StartLoc, EndLoc);
8806}
8807
Alexey Bataev67a4f222014-07-23 10:25:33 +00008808OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
8809 SourceLocation EndLoc) {
8810 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
8811}
8812
Alexey Bataev459dec02014-07-24 06:46:57 +00008813OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
8814 SourceLocation EndLoc) {
8815 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
8816}
8817
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008818OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
8819 SourceLocation EndLoc) {
8820 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
8821}
8822
Alexey Bataev346265e2015-09-25 10:37:12 +00008823OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
8824 SourceLocation EndLoc) {
8825 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
8826}
8827
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008828OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
8829 SourceLocation EndLoc) {
8830 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
8831}
8832
Alexey Bataevb825de12015-12-07 10:51:44 +00008833OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
8834 SourceLocation EndLoc) {
8835 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
8836}
8837
Alexey Bataevc5e02582014-06-16 07:08:35 +00008838OMPClause *Sema::ActOnOpenMPVarListClause(
8839 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
8840 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
8841 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008842 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00008843 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
8844 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
8845 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008846 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008847 switch (Kind) {
8848 case OMPC_private:
8849 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8850 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008851 case OMPC_firstprivate:
8852 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8853 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008854 case OMPC_lastprivate:
8855 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8856 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008857 case OMPC_shared:
8858 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
8859 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008860 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00008861 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
8862 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008863 break;
Alexey Bataev169d96a2017-07-18 20:17:46 +00008864 case OMPC_task_reduction:
8865 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
8866 EndLoc, ReductionIdScopeSpec,
8867 ReductionId);
8868 break;
Alexey Bataevfa312f32017-07-21 18:48:21 +00008869 case OMPC_in_reduction:
8870 Res =
8871 ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
8872 EndLoc, ReductionIdScopeSpec, ReductionId);
8873 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00008874 case OMPC_linear:
8875 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00008876 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00008877 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008878 case OMPC_aligned:
8879 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
8880 ColonLoc, EndLoc);
8881 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008882 case OMPC_copyin:
8883 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
8884 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008885 case OMPC_copyprivate:
8886 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8887 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00008888 case OMPC_flush:
8889 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
8890 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008891 case OMPC_depend:
David Majnemer9d168222016-08-05 17:44:54 +00008892 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
Kelvin Li0bff7af2015-11-23 05:32:03 +00008893 StartLoc, LParenLoc, EndLoc);
8894 break;
8895 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00008896 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
8897 DepLinMapLoc, ColonLoc, VarList, StartLoc,
8898 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008899 break;
Samuel Antao661c0902016-05-26 17:39:58 +00008900 case OMPC_to:
8901 Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc);
8902 break;
Samuel Antaoec172c62016-05-26 17:49:04 +00008903 case OMPC_from:
8904 Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc);
8905 break;
Carlo Bertolli2404b172016-07-13 15:37:16 +00008906 case OMPC_use_device_ptr:
8907 Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
8908 break;
Carlo Bertolli70594e92016-07-13 17:16:49 +00008909 case OMPC_is_device_ptr:
8910 Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
8911 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008912 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00008913 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00008914 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00008915 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008916 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00008917 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008918 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008919 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008920 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008921 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00008922 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008923 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008924 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008925 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008926 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008927 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008928 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008929 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008930 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00008931 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00008932 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008933 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00008934 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008935 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00008936 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008937 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00008938 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00008939 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00008940 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008941 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008942 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008943 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008944 case OMPC_uniform:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008945 llvm_unreachable("Clause is not allowed.");
8946 }
8947 return Res;
8948}
8949
Alexey Bataev90c228f2016-02-08 09:29:13 +00008950ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00008951 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00008952 ExprResult Res = BuildDeclRefExpr(
8953 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
8954 if (!Res.isUsable())
8955 return ExprError();
8956 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
8957 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
8958 if (!Res.isUsable())
8959 return ExprError();
8960 }
8961 if (VK != VK_LValue && Res.get()->isGLValue()) {
8962 Res = DefaultLvalueConversion(Res.get());
8963 if (!Res.isUsable())
8964 return ExprError();
8965 }
8966 return Res;
8967}
8968
Alexey Bataev60da77e2016-02-29 05:54:20 +00008969static std::pair<ValueDecl *, bool>
8970getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
8971 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008972 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
8973 RefExpr->containsUnexpandedParameterPack())
8974 return std::make_pair(nullptr, true);
8975
Alexey Bataevd985eda2016-02-10 11:29:16 +00008976 // OpenMP [3.1, C/C++]
8977 // A list item is a variable name.
8978 // OpenMP [2.9.3.3, Restrictions, p.1]
8979 // A variable that is part of another variable (as an array or
8980 // structure element) cannot appear in a private clause.
8981 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008982 enum {
8983 NoArrayExpr = -1,
8984 ArraySubscript = 0,
8985 OMPArraySection = 1
8986 } IsArrayExpr = NoArrayExpr;
8987 if (AllowArraySection) {
8988 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
8989 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
8990 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
8991 Base = TempASE->getBase()->IgnoreParenImpCasts();
8992 RefExpr = Base;
8993 IsArrayExpr = ArraySubscript;
8994 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
8995 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
8996 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
8997 Base = TempOASE->getBase()->IgnoreParenImpCasts();
8998 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
8999 Base = TempASE->getBase()->IgnoreParenImpCasts();
9000 RefExpr = Base;
9001 IsArrayExpr = OMPArraySection;
9002 }
9003 }
9004 ELoc = RefExpr->getExprLoc();
9005 ERange = RefExpr->getSourceRange();
9006 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00009007 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
9008 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
9009 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
9010 (S.getCurrentThisType().isNull() || !ME ||
9011 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
9012 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009013 if (IsArrayExpr != NoArrayExpr)
9014 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
9015 << ERange;
9016 else {
9017 S.Diag(ELoc,
9018 AllowArraySection
9019 ? diag::err_omp_expected_var_name_member_expr_or_array_item
9020 : diag::err_omp_expected_var_name_member_expr)
9021 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
9022 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009023 return std::make_pair(nullptr, false);
9024 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +00009025 return std::make_pair(
9026 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009027}
9028
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009029OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
9030 SourceLocation StartLoc,
9031 SourceLocation LParenLoc,
9032 SourceLocation EndLoc) {
9033 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00009034 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00009035 for (auto &RefExpr : VarList) {
9036 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009037 SourceLocation ELoc;
9038 SourceRange ERange;
9039 Expr *SimpleRefExpr = RefExpr;
9040 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009041 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009042 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009043 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009044 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009045 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009046 ValueDecl *D = Res.first;
9047 if (!D)
9048 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009049
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009050 QualType Type = D->getType();
9051 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009052
9053 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9054 // A variable that appears in a private clause must not have an incomplete
9055 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009056 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009057 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009058 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009059
Alexey Bataev758e55e2013-09-06 18:03:48 +00009060 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9061 // in a Construct]
9062 // Variables with the predetermined data-sharing attributes may not be
9063 // listed in data-sharing attributes clauses, except for the cases
9064 // listed below. For these exceptions only, listing a predetermined
9065 // variable in a data-sharing attribute clause is allowed and overrides
9066 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009067 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009068 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009069 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9070 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009071 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009072 continue;
9073 }
9074
Kelvin Libf594a52016-12-17 05:48:59 +00009075 auto CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009076 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009077 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Kelvin Libf594a52016-12-17 05:48:59 +00009078 isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009079 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
9080 << getOpenMPClauseName(OMPC_private) << Type
Kelvin Libf594a52016-12-17 05:48:59 +00009081 << getOpenMPDirectiveName(CurrDir);
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009082 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009083 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009084 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009085 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009086 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009087 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009088 continue;
9089 }
9090
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009091 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
9092 // A list item cannot appear in both a map clause and a data-sharing
9093 // attribute clause on the same construct
Kelvin Libf594a52016-12-17 05:48:59 +00009094 if (CurrDir == OMPD_target || CurrDir == OMPD_target_parallel ||
Kelvin Lida681182017-01-10 18:08:18 +00009095 CurrDir == OMPD_target_teams ||
Kelvin Li80e8f562016-12-29 22:16:30 +00009096 CurrDir == OMPD_target_teams_distribute ||
Kelvin Li1851df52017-01-03 05:23:48 +00009097 CurrDir == OMPD_target_teams_distribute_parallel_for ||
Kelvin Lic4bfc6f2017-01-10 04:26:44 +00009098 CurrDir == OMPD_target_teams_distribute_parallel_for_simd ||
Kelvin Lida681182017-01-10 18:08:18 +00009099 CurrDir == OMPD_target_teams_distribute_simd ||
Kelvin Li41010322017-01-10 05:15:35 +00009100 CurrDir == OMPD_target_parallel_for_simd ||
9101 CurrDir == OMPD_target_parallel_for) {
Samuel Antao6890b092016-07-28 14:25:09 +00009102 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +00009103 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +00009104 VD, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +00009105 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
9106 OpenMPClauseKind WhereFoundClauseKind) -> bool {
9107 ConflictKind = WhereFoundClauseKind;
9108 return true;
9109 })) {
9110 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009111 << getOpenMPClauseName(OMPC_private)
Samuel Antao6890b092016-07-28 14:25:09 +00009112 << getOpenMPClauseName(ConflictKind)
Kelvin Libf594a52016-12-17 05:48:59 +00009113 << getOpenMPDirectiveName(CurrDir);
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009114 ReportOriginalDSA(*this, DSAStack, D, DVar);
9115 continue;
9116 }
9117 }
9118
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009119 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
9120 // A variable of class type (or array thereof) that appears in a private
9121 // clause requires an accessible, unambiguous default constructor for the
9122 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00009123 // Generate helper private variable and initialize it with the default
9124 // value. The address of the original variable is replaced by the address of
9125 // the new private variable in CodeGen. This new variable is not added to
9126 // IdResolver, so the code in the OpenMP region uses original variable for
9127 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009128 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009129 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
9130 D->hasAttrs() ? &D->getAttrs() : nullptr);
Richard Smith3beb7c62017-01-12 02:27:38 +00009131 ActOnUninitializedDecl(VDPrivate);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009132 if (VDPrivate->isInvalidDecl())
9133 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009134 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009135 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009136
Alexey Bataev90c228f2016-02-08 09:29:13 +00009137 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009138 if (!VD && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00009139 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00009140 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009141 Vars.push_back((VD || CurContext->isDependentContext())
9142 ? RefExpr->IgnoreParens()
9143 : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009144 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009145 }
9146
Alexey Bataeved09d242014-05-28 05:53:51 +00009147 if (Vars.empty())
9148 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009149
Alexey Bataev03b340a2014-10-21 03:16:40 +00009150 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9151 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009152}
9153
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009154namespace {
9155class DiagsUninitializedSeveretyRAII {
9156private:
9157 DiagnosticsEngine &Diags;
9158 SourceLocation SavedLoc;
9159 bool IsIgnored;
9160
9161public:
9162 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
9163 bool IsIgnored)
9164 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
9165 if (!IsIgnored) {
9166 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
9167 /*Map*/ diag::Severity::Ignored, Loc);
9168 }
9169 }
9170 ~DiagsUninitializedSeveretyRAII() {
9171 if (!IsIgnored)
9172 Diags.popMappings(SavedLoc);
9173 }
9174};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00009175}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009176
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009177OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
9178 SourceLocation StartLoc,
9179 SourceLocation LParenLoc,
9180 SourceLocation EndLoc) {
9181 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009182 SmallVector<Expr *, 8> PrivateCopies;
9183 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00009184 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009185 bool IsImplicitClause =
9186 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
9187 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
9188
Alexey Bataeved09d242014-05-28 05:53:51 +00009189 for (auto &RefExpr : VarList) {
9190 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009191 SourceLocation ELoc;
9192 SourceRange ERange;
9193 Expr *SimpleRefExpr = RefExpr;
9194 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009195 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009196 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009197 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009198 PrivateCopies.push_back(nullptr);
9199 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009200 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009201 ValueDecl *D = Res.first;
9202 if (!D)
9203 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009204
Alexey Bataev60da77e2016-02-29 05:54:20 +00009205 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00009206 QualType Type = D->getType();
9207 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009208
9209 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9210 // A variable that appears in a private clause must not have an incomplete
9211 // type or a reference type.
9212 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00009213 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009214 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009215 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009216
9217 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
9218 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00009219 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009220 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009221 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009222
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009223 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00009224 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009225 if (!IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00009226 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev005248a2016-02-25 05:25:57 +00009227 TopDVar = DVar;
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009228 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009229 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009230 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
9231 // A list item that specifies a given variable may not appear in more
9232 // than one clause on the same directive, except that a variable may be
9233 // specified in both firstprivate and lastprivate clauses.
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009234 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
9235 // A list item may appear in a firstprivate or lastprivate clause but not
9236 // both.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009237 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevb358f992017-12-01 17:40:15 +00009238 (isOpenMPDistributeDirective(CurrDir) ||
9239 DVar.CKind != OMPC_lastprivate) &&
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009240 DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009241 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00009242 << getOpenMPClauseName(DVar.CKind)
9243 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009244 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009245 continue;
9246 }
9247
9248 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9249 // in a Construct]
9250 // Variables with the predetermined data-sharing attributes may not be
9251 // listed in data-sharing attributes clauses, except for the cases
9252 // listed below. For these exceptions only, listing a predetermined
9253 // variable in a data-sharing attribute clause is allowed and overrides
9254 // the variable's predetermined data-sharing attributes.
9255 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9256 // in a Construct, C/C++, p.2]
9257 // Variables with const-qualified type having no mutable member may be
9258 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00009259 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009260 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
9261 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00009262 << getOpenMPClauseName(DVar.CKind)
9263 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009264 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009265 continue;
9266 }
9267
9268 // OpenMP [2.9.3.4, Restrictions, p.2]
9269 // A list item that is private within a parallel region must not appear
9270 // in a firstprivate clause on a worksharing construct if any of the
9271 // worksharing regions arising from the worksharing construct ever bind
9272 // to any of the parallel regions arising from the parallel construct.
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009273 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
9274 // A list item that is private within a teams region must not appear in a
9275 // firstprivate clause on a distribute construct if any of the distribute
9276 // regions arising from the distribute construct ever bind to any of the
9277 // teams regions arising from the teams construct.
9278 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
9279 // A list item that appears in a reduction clause of a teams construct
9280 // must not appear in a firstprivate clause on a distribute construct if
9281 // any of the distribute regions arising from the distribute construct
9282 // ever bind to any of the teams regions arising from the teams construct.
9283 if ((isOpenMPWorksharingDirective(CurrDir) ||
9284 isOpenMPDistributeDirective(CurrDir)) &&
Kelvin Li579e41c2016-11-30 23:51:03 +00009285 !isOpenMPParallelDirective(CurrDir) &&
9286 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00009287 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009288 if (DVar.CKind != OMPC_shared &&
9289 (isOpenMPParallelDirective(DVar.DKind) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009290 isOpenMPTeamsDirective(DVar.DKind) ||
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009291 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00009292 Diag(ELoc, diag::err_omp_required_access)
9293 << getOpenMPClauseName(OMPC_firstprivate)
9294 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009295 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00009296 continue;
9297 }
9298 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009299 // OpenMP [2.9.3.4, Restrictions, p.3]
9300 // A list item that appears in a reduction clause of a parallel construct
9301 // must not appear in a firstprivate clause on a worksharing or task
9302 // construct if any of the worksharing or task regions arising from the
9303 // worksharing or task construct ever bind to any of the parallel regions
9304 // arising from the parallel construct.
9305 // OpenMP [2.9.3.4, Restrictions, p.4]
9306 // A list item that appears in a reduction clause in worksharing
9307 // construct must not appear in a firstprivate clause in a task construct
9308 // encountered during execution of any of the worksharing regions arising
9309 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +00009310 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00009311 DVar = DSAStack->hasInnermostDSA(
9312 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
9313 [](OpenMPDirectiveKind K) -> bool {
9314 return isOpenMPParallelDirective(K) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009315 isOpenMPWorksharingDirective(K) ||
9316 isOpenMPTeamsDirective(K);
Alexey Bataev7ace49d2016-05-17 08:55:33 +00009317 },
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009318 /*FromParent=*/true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009319 if (DVar.CKind == OMPC_reduction &&
9320 (isOpenMPParallelDirective(DVar.DKind) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009321 isOpenMPWorksharingDirective(DVar.DKind) ||
9322 isOpenMPTeamsDirective(DVar.DKind))) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009323 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
9324 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009325 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009326 continue;
9327 }
9328 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00009329
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009330 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
9331 // A list item cannot appear in both a map clause and a data-sharing
9332 // attribute clause on the same construct
Alexey Bataevb358f992017-12-01 17:40:15 +00009333 if (isOpenMPTargetExecutionDirective(CurrDir)) {
Samuel Antao6890b092016-07-28 14:25:09 +00009334 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +00009335 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +00009336 VD, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +00009337 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
9338 OpenMPClauseKind WhereFoundClauseKind) -> bool {
9339 ConflictKind = WhereFoundClauseKind;
9340 return true;
9341 })) {
9342 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009343 << getOpenMPClauseName(OMPC_firstprivate)
Samuel Antao6890b092016-07-28 14:25:09 +00009344 << getOpenMPClauseName(ConflictKind)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009345 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
9346 ReportOriginalDSA(*this, DSAStack, D, DVar);
9347 continue;
9348 }
9349 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009350 }
9351
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009352 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009353 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00009354 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009355 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
9356 << getOpenMPClauseName(OMPC_firstprivate) << Type
9357 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
9358 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00009359 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009360 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00009361 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009362 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00009363 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009364 continue;
9365 }
9366
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009367 Type = Type.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00009368 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
9369 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009370 // Generate helper private variable and initialize it with the value of the
9371 // original variable. The address of the original variable is replaced by
9372 // the address of the new private variable in the CodeGen. This new variable
9373 // is not added to IdResolver, so the code in the OpenMP region uses
9374 // original variable for proper diagnostics and variable capturing.
9375 Expr *VDInitRefExpr = nullptr;
9376 // For arrays generate initializer for single element and replace it by the
9377 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009378 if (Type->isArrayType()) {
9379 auto VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00009380 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009381 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009382 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009383 ElemType = ElemType.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00009384 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009385 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00009386 InitializedEntity Entity =
9387 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009388 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
9389
9390 InitializationSequence InitSeq(*this, Entity, Kind, Init);
9391 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
9392 if (Result.isInvalid())
9393 VDPrivate->setInvalidDecl();
9394 else
9395 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009396 // Remove temp variable declaration.
9397 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009398 } else {
Alexey Bataevd985eda2016-02-10 11:29:16 +00009399 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
9400 ".firstprivate.temp");
9401 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
9402 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00009403 AddInitializerToDecl(VDPrivate,
9404 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +00009405 /*DirectInit=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009406 }
9407 if (VDPrivate->isInvalidDecl()) {
9408 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00009409 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009410 diag::note_omp_task_predetermined_firstprivate_here);
9411 }
9412 continue;
9413 }
9414 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00009415 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00009416 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
9417 RefExpr->getExprLoc());
9418 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009419 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev005248a2016-02-25 05:25:57 +00009420 if (TopDVar.CKind == OMPC_lastprivate)
9421 Ref = TopDVar.PrivateCopy;
9422 else {
Alexey Bataev61205072016-03-02 04:57:40 +00009423 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataev005248a2016-02-25 05:25:57 +00009424 if (!IsOpenMPCapturedDecl(D))
9425 ExprCaptures.push_back(Ref->getDecl());
9426 }
Alexey Bataev417089f2016-02-17 13:19:37 +00009427 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009428 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009429 Vars.push_back((VD || CurContext->isDependentContext())
9430 ? RefExpr->IgnoreParens()
9431 : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009432 PrivateCopies.push_back(VDPrivateRefExpr);
9433 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009434 }
9435
Alexey Bataeved09d242014-05-28 05:53:51 +00009436 if (Vars.empty())
9437 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009438
9439 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009440 Vars, PrivateCopies, Inits,
9441 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009442}
9443
Alexander Musman1bb328c2014-06-04 13:06:39 +00009444OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
9445 SourceLocation StartLoc,
9446 SourceLocation LParenLoc,
9447 SourceLocation EndLoc) {
9448 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00009449 SmallVector<Expr *, 8> SrcExprs;
9450 SmallVector<Expr *, 8> DstExprs;
9451 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +00009452 SmallVector<Decl *, 4> ExprCaptures;
9453 SmallVector<Expr *, 4> ExprPostUpdates;
Alexander Musman1bb328c2014-06-04 13:06:39 +00009454 for (auto &RefExpr : VarList) {
9455 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009456 SourceLocation ELoc;
9457 SourceRange ERange;
9458 Expr *SimpleRefExpr = RefExpr;
9459 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +00009460 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00009461 // It will be analyzed later.
9462 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00009463 SrcExprs.push_back(nullptr);
9464 DstExprs.push_back(nullptr);
9465 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00009466 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00009467 ValueDecl *D = Res.first;
9468 if (!D)
9469 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00009470
Alexey Bataev74caaf22016-02-20 04:09:36 +00009471 QualType Type = D->getType();
9472 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +00009473
9474 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
9475 // A variable that appears in a lastprivate clause must not have an
9476 // incomplete type or a reference type.
9477 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +00009478 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +00009479 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009480 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00009481
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009482 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexander Musman1bb328c2014-06-04 13:06:39 +00009483 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
9484 // in a Construct]
9485 // Variables with the predetermined data-sharing attributes may not be
9486 // listed in data-sharing attributes clauses, except for the cases
9487 // listed below.
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009488 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
9489 // A list item may appear in a firstprivate or lastprivate clause but not
9490 // both.
Alexey Bataev74caaf22016-02-20 04:09:36 +00009491 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00009492 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
Alexey Bataevb358f992017-12-01 17:40:15 +00009493 (isOpenMPDistributeDirective(CurrDir) ||
9494 DVar.CKind != OMPC_firstprivate) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +00009495 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
9496 Diag(ELoc, diag::err_omp_wrong_dsa)
9497 << getOpenMPClauseName(DVar.CKind)
9498 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev74caaf22016-02-20 04:09:36 +00009499 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00009500 continue;
9501 }
9502
Alexey Bataevf29276e2014-06-18 04:14:57 +00009503 // OpenMP [2.14.3.5, Restrictions, p.2]
9504 // A list item that is private within a parallel region, or that appears in
9505 // the reduction clause of a parallel construct, must not appear in a
9506 // lastprivate clause on a worksharing construct if any of the corresponding
9507 // worksharing regions ever binds to any of the corresponding parallel
9508 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00009509 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00009510 if (isOpenMPWorksharingDirective(CurrDir) &&
Kelvin Li579e41c2016-11-30 23:51:03 +00009511 !isOpenMPParallelDirective(CurrDir) &&
9512 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +00009513 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00009514 if (DVar.CKind != OMPC_shared) {
9515 Diag(ELoc, diag::err_omp_required_access)
9516 << getOpenMPClauseName(OMPC_lastprivate)
9517 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev74caaf22016-02-20 04:09:36 +00009518 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00009519 continue;
9520 }
9521 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00009522
Alexander Musman1bb328c2014-06-04 13:06:39 +00009523 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00009524 // A variable of class type (or array thereof) that appears in a
9525 // lastprivate clause requires an accessible, unambiguous default
9526 // constructor for the class type, unless the list item is also specified
9527 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00009528 // A variable of class type (or array thereof) that appears in a
9529 // lastprivate clause requires an accessible, unambiguous copy assignment
9530 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00009531 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009532 auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009533 Type.getUnqualifiedType(), ".lastprivate.src",
Alexey Bataev74caaf22016-02-20 04:09:36 +00009534 D->hasAttrs() ? &D->getAttrs() : nullptr);
9535 auto *PseudoSrcExpr =
9536 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00009537 auto *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +00009538 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +00009539 D->hasAttrs() ? &D->getAttrs() : nullptr);
9540 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00009541 // For arrays generate assignment operation for single element and replace
9542 // it by the original array element in CodeGen.
Alexey Bataev74caaf22016-02-20 04:09:36 +00009543 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
Alexey Bataev38e89532015-04-16 04:54:05 +00009544 PseudoDstExpr, PseudoSrcExpr);
9545 if (AssignmentOp.isInvalid())
9546 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +00009547 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00009548 /*DiscardedValue=*/true);
9549 if (AssignmentOp.isInvalid())
9550 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00009551
Alexey Bataev74caaf22016-02-20 04:09:36 +00009552 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009553 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev005248a2016-02-25 05:25:57 +00009554 if (TopDVar.CKind == OMPC_firstprivate)
9555 Ref = TopDVar.PrivateCopy;
9556 else {
Alexey Bataev61205072016-03-02 04:57:40 +00009557 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00009558 if (!IsOpenMPCapturedDecl(D))
9559 ExprCaptures.push_back(Ref->getDecl());
9560 }
9561 if (TopDVar.CKind == OMPC_firstprivate ||
9562 (!IsOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009563 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +00009564 ExprResult RefRes = DefaultLvalueConversion(Ref);
9565 if (!RefRes.isUsable())
9566 continue;
9567 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +00009568 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
9569 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +00009570 if (!PostUpdateRes.isUsable())
9571 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +00009572 ExprPostUpdates.push_back(
9573 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +00009574 }
9575 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00009576 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009577 Vars.push_back((VD || CurContext->isDependentContext())
9578 ? RefExpr->IgnoreParens()
9579 : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +00009580 SrcExprs.push_back(PseudoSrcExpr);
9581 DstExprs.push_back(PseudoDstExpr);
9582 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00009583 }
9584
9585 if (Vars.empty())
9586 return nullptr;
9587
9588 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +00009589 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009590 buildPreInits(Context, ExprCaptures),
9591 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +00009592}
9593
Alexey Bataev758e55e2013-09-06 18:03:48 +00009594OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
9595 SourceLocation StartLoc,
9596 SourceLocation LParenLoc,
9597 SourceLocation EndLoc) {
9598 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00009599 for (auto &RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009600 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009601 SourceLocation ELoc;
9602 SourceRange ERange;
9603 Expr *SimpleRefExpr = RefExpr;
9604 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009605 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00009606 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009607 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009608 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009609 ValueDecl *D = Res.first;
9610 if (!D)
9611 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +00009612
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009613 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009614 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9615 // in a Construct]
9616 // Variables with the predetermined data-sharing attributes may not be
9617 // listed in data-sharing attributes clauses, except for the cases
9618 // listed below. For these exceptions only, listing a predetermined
9619 // variable in a data-sharing attribute clause is allowed and overrides
9620 // the variable's predetermined data-sharing attributes.
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009621 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00009622 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
9623 DVar.RefExpr) {
9624 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9625 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009626 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009627 continue;
9628 }
9629
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009630 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009631 if (!VD && IsOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00009632 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009633 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009634 Vars.push_back((VD || !Ref || CurContext->isDependentContext())
9635 ? RefExpr->IgnoreParens()
9636 : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009637 }
9638
Alexey Bataeved09d242014-05-28 05:53:51 +00009639 if (Vars.empty())
9640 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00009641
9642 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
9643}
9644
Alexey Bataevc5e02582014-06-16 07:08:35 +00009645namespace {
9646class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
9647 DSAStackTy *Stack;
9648
9649public:
9650 bool VisitDeclRefExpr(DeclRefExpr *E) {
9651 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009652 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00009653 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
9654 return false;
9655 if (DVar.CKind != OMPC_unknown)
9656 return true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +00009657 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
9658 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009659 /*FromParent=*/true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00009660 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00009661 return true;
9662 return false;
9663 }
9664 return false;
9665 }
9666 bool VisitStmt(Stmt *S) {
9667 for (auto Child : S->children()) {
9668 if (Child && Visit(Child))
9669 return true;
9670 }
9671 return false;
9672 }
Alexey Bataev23b69422014-06-18 07:08:49 +00009673 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00009674};
Alexey Bataev23b69422014-06-18 07:08:49 +00009675} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00009676
Alexey Bataev60da77e2016-02-29 05:54:20 +00009677namespace {
9678// Transform MemberExpression for specified FieldDecl of current class to
9679// DeclRefExpr to specified OMPCapturedExprDecl.
9680class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
9681 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
9682 ValueDecl *Field;
9683 DeclRefExpr *CapturedExpr;
9684
9685public:
9686 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
9687 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
9688
9689 ExprResult TransformMemberExpr(MemberExpr *E) {
9690 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
9691 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +00009692 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009693 return CapturedExpr;
9694 }
9695 return BaseTransform::TransformMemberExpr(E);
9696 }
9697 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
9698};
9699} // namespace
9700
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009701template <typename T>
9702static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups,
9703 const llvm::function_ref<T(ValueDecl *)> &Gen) {
9704 for (auto &Set : Lookups) {
9705 for (auto *D : Set) {
9706 if (auto Res = Gen(cast<ValueDecl>(D)))
9707 return Res;
9708 }
9709 }
9710 return T();
9711}
9712
9713static ExprResult
9714buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
9715 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
9716 const DeclarationNameInfo &ReductionId, QualType Ty,
9717 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
9718 if (ReductionIdScopeSpec.isInvalid())
9719 return ExprError();
9720 SmallVector<UnresolvedSet<8>, 4> Lookups;
9721 if (S) {
9722 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
9723 Lookup.suppressDiagnostics();
9724 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
9725 auto *D = Lookup.getRepresentativeDecl();
9726 do {
9727 S = S->getParent();
9728 } while (S && !S->isDeclScope(D));
9729 if (S)
9730 S = S->getParent();
9731 Lookups.push_back(UnresolvedSet<8>());
9732 Lookups.back().append(Lookup.begin(), Lookup.end());
9733 Lookup.clear();
9734 }
9735 } else if (auto *ULE =
9736 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
9737 Lookups.push_back(UnresolvedSet<8>());
9738 Decl *PrevD = nullptr;
David Majnemer9d168222016-08-05 17:44:54 +00009739 for (auto *D : ULE->decls()) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009740 if (D == PrevD)
9741 Lookups.push_back(UnresolvedSet<8>());
9742 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
9743 Lookups.back().addDecl(DRD);
9744 PrevD = D;
9745 }
9746 }
Alexey Bataevfdc20352017-08-25 15:43:55 +00009747 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() ||
9748 Ty->isInstantiationDependentType() ||
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009749 Ty->containsUnexpandedParameterPack() ||
9750 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool {
9751 return !D->isInvalidDecl() &&
9752 (D->getType()->isDependentType() ||
9753 D->getType()->isInstantiationDependentType() ||
9754 D->getType()->containsUnexpandedParameterPack());
9755 })) {
9756 UnresolvedSet<8> ResSet;
9757 for (auto &Set : Lookups) {
9758 ResSet.append(Set.begin(), Set.end());
9759 // The last item marks the end of all declarations at the specified scope.
9760 ResSet.addDecl(Set[Set.size() - 1]);
9761 }
9762 return UnresolvedLookupExpr::Create(
9763 SemaRef.Context, /*NamingClass=*/nullptr,
9764 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
9765 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
9766 }
9767 if (auto *VD = filterLookupForUDR<ValueDecl *>(
9768 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
9769 if (!D->isInvalidDecl() &&
9770 SemaRef.Context.hasSameType(D->getType(), Ty))
9771 return D;
9772 return nullptr;
9773 }))
9774 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
9775 if (auto *VD = filterLookupForUDR<ValueDecl *>(
9776 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
9777 if (!D->isInvalidDecl() &&
9778 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
9779 !Ty.isMoreQualifiedThan(D->getType()))
9780 return D;
9781 return nullptr;
9782 })) {
9783 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
9784 /*DetectVirtual=*/false);
9785 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
9786 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
9787 VD->getType().getUnqualifiedType()))) {
9788 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
9789 /*DiagID=*/0) !=
9790 Sema::AR_inaccessible) {
9791 SemaRef.BuildBasePathArray(Paths, BasePath);
9792 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
9793 }
9794 }
9795 }
9796 }
9797 if (ReductionIdScopeSpec.isSet()) {
9798 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
9799 return ExprError();
9800 }
9801 return ExprEmpty();
9802}
9803
Alexey Bataevfad872fc2017-07-18 15:32:58 +00009804namespace {
9805/// Data for the reduction-based clauses.
9806struct ReductionData {
9807 /// List of original reduction items.
9808 SmallVector<Expr *, 8> Vars;
9809 /// List of private copies of the reduction items.
9810 SmallVector<Expr *, 8> Privates;
9811 /// LHS expressions for the reduction_op expressions.
9812 SmallVector<Expr *, 8> LHSs;
9813 /// RHS expressions for the reduction_op expressions.
9814 SmallVector<Expr *, 8> RHSs;
9815 /// Reduction operation expression.
9816 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev88202be2017-07-27 13:20:36 +00009817 /// Taskgroup descriptors for the corresponding reduction items in
9818 /// in_reduction clauses.
9819 SmallVector<Expr *, 8> TaskgroupDescriptors;
Alexey Bataevfad872fc2017-07-18 15:32:58 +00009820 /// List of captures for clause.
9821 SmallVector<Decl *, 4> ExprCaptures;
9822 /// List of postupdate expressions.
9823 SmallVector<Expr *, 4> ExprPostUpdates;
9824 ReductionData() = delete;
9825 /// Reserves required memory for the reduction data.
9826 ReductionData(unsigned Size) {
9827 Vars.reserve(Size);
9828 Privates.reserve(Size);
9829 LHSs.reserve(Size);
9830 RHSs.reserve(Size);
9831 ReductionOps.reserve(Size);
Alexey Bataev88202be2017-07-27 13:20:36 +00009832 TaskgroupDescriptors.reserve(Size);
Alexey Bataevfad872fc2017-07-18 15:32:58 +00009833 ExprCaptures.reserve(Size);
9834 ExprPostUpdates.reserve(Size);
9835 }
9836 /// Stores reduction item and reduction operation only (required for dependent
9837 /// reduction item).
9838 void push(Expr *Item, Expr *ReductionOp) {
9839 Vars.emplace_back(Item);
9840 Privates.emplace_back(nullptr);
9841 LHSs.emplace_back(nullptr);
9842 RHSs.emplace_back(nullptr);
9843 ReductionOps.emplace_back(ReductionOp);
Alexey Bataev88202be2017-07-27 13:20:36 +00009844 TaskgroupDescriptors.emplace_back(nullptr);
Alexey Bataevfad872fc2017-07-18 15:32:58 +00009845 }
9846 /// Stores reduction data.
Alexey Bataev88202be2017-07-27 13:20:36 +00009847 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp,
9848 Expr *TaskgroupDescriptor) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +00009849 Vars.emplace_back(Item);
9850 Privates.emplace_back(Private);
9851 LHSs.emplace_back(LHS);
9852 RHSs.emplace_back(RHS);
9853 ReductionOps.emplace_back(ReductionOp);
Alexey Bataev88202be2017-07-27 13:20:36 +00009854 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor);
Alexey Bataevfad872fc2017-07-18 15:32:58 +00009855 }
9856};
9857} // namespace
9858
Jonas Hahnfeld4525c822017-10-23 19:01:35 +00009859static bool CheckOMPArraySectionConstantForReduction(
9860 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement,
9861 SmallVectorImpl<llvm::APSInt> &ArraySizes) {
9862 const Expr *Length = OASE->getLength();
9863 if (Length == nullptr) {
9864 // For array sections of the form [1:] or [:], we would need to analyze
9865 // the lower bound...
9866 if (OASE->getColonLoc().isValid())
9867 return false;
9868
9869 // This is an array subscript which has implicit length 1!
9870 SingleElement = true;
9871 ArraySizes.push_back(llvm::APSInt::get(1));
9872 } else {
9873 llvm::APSInt ConstantLengthValue;
9874 if (!Length->EvaluateAsInt(ConstantLengthValue, Context))
9875 return false;
9876
9877 SingleElement = (ConstantLengthValue.getSExtValue() == 1);
9878 ArraySizes.push_back(ConstantLengthValue);
9879 }
9880
9881 // Get the base of this array section and walk up from there.
9882 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
9883
9884 // We require length = 1 for all array sections except the right-most to
9885 // guarantee that the memory region is contiguous and has no holes in it.
9886 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) {
9887 Length = TempOASE->getLength();
9888 if (Length == nullptr) {
9889 // For array sections of the form [1:] or [:], we would need to analyze
9890 // the lower bound...
9891 if (OASE->getColonLoc().isValid())
9892 return false;
9893
9894 // This is an array subscript which has implicit length 1!
9895 ArraySizes.push_back(llvm::APSInt::get(1));
9896 } else {
9897 llvm::APSInt ConstantLengthValue;
9898 if (!Length->EvaluateAsInt(ConstantLengthValue, Context) ||
9899 ConstantLengthValue.getSExtValue() != 1)
9900 return false;
9901
9902 ArraySizes.push_back(ConstantLengthValue);
9903 }
9904 Base = TempOASE->getBase()->IgnoreParenImpCasts();
9905 }
9906
9907 // If we have a single element, we don't need to add the implicit lengths.
9908 if (!SingleElement) {
9909 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) {
9910 // Has implicit length 1!
9911 ArraySizes.push_back(llvm::APSInt::get(1));
9912 Base = TempASE->getBase()->IgnoreParenImpCasts();
9913 }
9914 }
9915
9916 // This array section can be privatized as a single value or as a constant
9917 // sized array.
9918 return true;
9919}
9920
Alexey Bataevfad872fc2017-07-18 15:32:58 +00009921static bool ActOnOMPReductionKindClause(
Alexey Bataev169d96a2017-07-18 20:17:46 +00009922 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind,
9923 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
9924 SourceLocation ColonLoc, SourceLocation EndLoc,
9925 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
Alexey Bataevfad872fc2017-07-18 15:32:58 +00009926 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00009927 auto DN = ReductionId.getName();
9928 auto OOK = DN.getCXXOverloadedOperator();
9929 BinaryOperatorKind BOK = BO_Comma;
9930
Alexey Bataevfad872fc2017-07-18 15:32:58 +00009931 ASTContext &Context = S.Context;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009932 // OpenMP [2.14.3.6, reduction clause]
9933 // C
9934 // reduction-identifier is either an identifier or one of the following
9935 // operators: +, -, *, &, |, ^, && and ||
9936 // C++
9937 // reduction-identifier is either an id-expression or one of the following
9938 // operators: +, -, *, &, |, ^, && and ||
Alexey Bataevc5e02582014-06-16 07:08:35 +00009939 switch (OOK) {
9940 case OO_Plus:
9941 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009942 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009943 break;
9944 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009945 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009946 break;
9947 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009948 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009949 break;
9950 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009951 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009952 break;
9953 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009954 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009955 break;
9956 case OO_AmpAmp:
9957 BOK = BO_LAnd;
9958 break;
9959 case OO_PipePipe:
9960 BOK = BO_LOr;
9961 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009962 case OO_New:
9963 case OO_Delete:
9964 case OO_Array_New:
9965 case OO_Array_Delete:
9966 case OO_Slash:
9967 case OO_Percent:
9968 case OO_Tilde:
9969 case OO_Exclaim:
9970 case OO_Equal:
9971 case OO_Less:
9972 case OO_Greater:
9973 case OO_LessEqual:
9974 case OO_GreaterEqual:
9975 case OO_PlusEqual:
9976 case OO_MinusEqual:
9977 case OO_StarEqual:
9978 case OO_SlashEqual:
9979 case OO_PercentEqual:
9980 case OO_CaretEqual:
9981 case OO_AmpEqual:
9982 case OO_PipeEqual:
9983 case OO_LessLess:
9984 case OO_GreaterGreater:
9985 case OO_LessLessEqual:
9986 case OO_GreaterGreaterEqual:
9987 case OO_EqualEqual:
9988 case OO_ExclaimEqual:
Richard Smithd30b23d2017-12-01 02:13:10 +00009989 case OO_Spaceship:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009990 case OO_PlusPlus:
9991 case OO_MinusMinus:
9992 case OO_Comma:
9993 case OO_ArrowStar:
9994 case OO_Arrow:
9995 case OO_Call:
9996 case OO_Subscript:
9997 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00009998 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009999 case NUM_OVERLOADED_OPERATORS:
10000 llvm_unreachable("Unexpected reduction identifier");
10001 case OO_None:
Alexey Bataev4d4624c2017-07-20 16:47:47 +000010002 if (auto *II = DN.getAsIdentifierInfo()) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010003 if (II->isStr("max"))
10004 BOK = BO_GT;
10005 else if (II->isStr("min"))
10006 BOK = BO_LT;
10007 }
10008 break;
10009 }
10010 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010011 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +000010012 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataev4d4624c2017-07-20 16:47:47 +000010013 else
10014 ReductionIdRange.setBegin(ReductionId.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +000010015 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +000010016
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010017 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
10018 bool FirstIter = true;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010019 for (auto RefExpr : VarList) {
10020 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +000010021 // OpenMP [2.1, C/C++]
10022 // A list item is a variable or array section, subject to the restrictions
10023 // specified in Section 2.4 on page 42 and in each of the sections
10024 // describing clauses and directives for which a list appears.
10025 // OpenMP [2.14.3.3, Restrictions, p.1]
10026 // A variable that is part of another variable (as an array or
10027 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010028 if (!FirstIter && IR != ER)
10029 ++IR;
10030 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010031 SourceLocation ELoc;
10032 SourceRange ERange;
10033 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010034 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
Alexey Bataev60da77e2016-02-29 05:54:20 +000010035 /*AllowArraySection=*/true);
10036 if (Res.second) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010037 // Try to find 'declare reduction' corresponding construct before using
10038 // builtin/overloaded operators.
10039 QualType Type = Context.DependentTy;
10040 CXXCastPath BasePath;
10041 ExprResult DeclareReductionRef = buildDeclareReductionRef(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010042 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010043 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010044 Expr *ReductionOp = nullptr;
10045 if (S.CurContext->isDependentContext() &&
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010046 (DeclareReductionRef.isUnset() ||
10047 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010048 ReductionOp = DeclareReductionRef.get();
10049 // It will be analyzed later.
10050 RD.push(RefExpr, ReductionOp);
Alexey Bataevc5e02582014-06-16 07:08:35 +000010051 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000010052 ValueDecl *D = Res.first;
10053 if (!D)
10054 continue;
10055
Alexey Bataev88202be2017-07-27 13:20:36 +000010056 Expr *TaskgroupDescriptor = nullptr;
Alexey Bataeva1764212015-09-30 09:22:36 +000010057 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010058 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
10059 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
10060 if (ASE)
Alexey Bataev31300ed2016-02-04 11:27:03 +000010061 Type = ASE->getType().getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +000010062 else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +000010063 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
10064 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
10065 Type = ATy->getElementType();
10066 else
10067 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +000010068 Type = Type.getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +000010069 } else
10070 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
10071 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +000010072
Alexey Bataevc5e02582014-06-16 07:08:35 +000010073 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
10074 // A variable that appears in a private clause must not have an incomplete
10075 // type or a reference type.
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010076 if (S.RequireCompleteType(ELoc, Type,
10077 diag::err_omp_reduction_incomplete_type))
Alexey Bataevc5e02582014-06-16 07:08:35 +000010078 continue;
10079 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +000010080 // A list item that appears in a reduction clause must not be
10081 // const-qualified.
10082 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataev169d96a2017-07-18 20:17:46 +000010083 S.Diag(ELoc, diag::err_omp_const_reduction_list_item) << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010084 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010085 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10086 VarDecl::DeclarationOnly;
10087 S.Diag(D->getLocation(),
10088 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +000010089 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +000010090 }
Alexey Bataevc5e02582014-06-16 07:08:35 +000010091 continue;
10092 }
10093 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
10094 // If a list-item is a reference type then it must bind to the same object
10095 // for all threads of the team.
Alexey Bataev60da77e2016-02-29 05:54:20 +000010096 if (!ASE && !OASE && VD) {
Alexey Bataeva1764212015-09-30 09:22:36 +000010097 VarDecl *VDDef = VD->getDefinition();
David Sheinkman92589992016-10-04 14:41:36 +000010098 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010099 DSARefChecker Check(Stack);
Alexey Bataeva1764212015-09-30 09:22:36 +000010100 if (Check.Visit(VDDef->getInit())) {
Alexey Bataev169d96a2017-07-18 20:17:46 +000010101 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg)
10102 << getOpenMPClauseName(ClauseKind) << ERange;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010103 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
Alexey Bataeva1764212015-09-30 09:22:36 +000010104 continue;
10105 }
Alexey Bataevc5e02582014-06-16 07:08:35 +000010106 }
10107 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010108
Alexey Bataevc5e02582014-06-16 07:08:35 +000010109 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
10110 // in a Construct]
10111 // Variables with the predetermined data-sharing attributes may not be
10112 // listed in data-sharing attributes clauses, except for the cases
10113 // listed below. For these exceptions only, listing a predetermined
10114 // variable in a data-sharing attribute clause is allowed and overrides
10115 // the variable's predetermined data-sharing attributes.
10116 // OpenMP [2.14.3.6, Restrictions, p.3]
10117 // Any number of reduction clauses can be specified on the directive,
10118 // but a list item can appear only once in the reduction clauses for that
10119 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +000010120 DSAStackTy::DSAVarData DVar;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010121 DVar = Stack->getTopDSA(D, false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010122 if (DVar.CKind == OMPC_reduction) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010123 S.Diag(ELoc, diag::err_omp_once_referenced)
Alexey Bataev169d96a2017-07-18 20:17:46 +000010124 << getOpenMPClauseName(ClauseKind);
Alexey Bataev60da77e2016-02-29 05:54:20 +000010125 if (DVar.RefExpr)
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010126 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataev4d4624c2017-07-20 16:47:47 +000010127 continue;
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010128 } else if (DVar.CKind != OMPC_unknown) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010129 S.Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010130 << getOpenMPClauseName(DVar.CKind)
10131 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010132 ReportOriginalDSA(S, Stack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010133 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010134 }
10135
10136 // OpenMP [2.14.3.6, Restrictions, p.1]
10137 // A list item that appears in a reduction clause of a worksharing
10138 // construct must be shared in the parallel regions to which any of the
10139 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010140 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective();
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010141 if (isOpenMPWorksharingDirective(CurrDir) &&
Kelvin Li579e41c2016-11-30 23:51:03 +000010142 !isOpenMPParallelDirective(CurrDir) &&
10143 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010144 DVar = Stack->getImplicitDSA(D, true);
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010145 if (DVar.CKind != OMPC_shared) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010146 S.Diag(ELoc, diag::err_omp_required_access)
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010147 << getOpenMPClauseName(OMPC_reduction)
10148 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010149 ReportOriginalDSA(S, Stack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010150 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +000010151 }
10152 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010153
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010154 // Try to find 'declare reduction' corresponding construct before using
10155 // builtin/overloaded operators.
10156 CXXCastPath BasePath;
10157 ExprResult DeclareReductionRef = buildDeclareReductionRef(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010158 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010159 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
10160 if (DeclareReductionRef.isInvalid())
10161 continue;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010162 if (S.CurContext->isDependentContext() &&
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010163 (DeclareReductionRef.isUnset() ||
10164 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010165 RD.push(RefExpr, DeclareReductionRef.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010166 continue;
10167 }
10168 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
10169 // Not allowed reduction identifier is found.
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010170 S.Diag(ReductionId.getLocStart(),
10171 diag::err_omp_unknown_reduction_identifier)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010172 << Type << ReductionIdRange;
10173 continue;
10174 }
10175
10176 // OpenMP [2.14.3.6, reduction clause, Restrictions]
10177 // The type of a list item that appears in a reduction clause must be valid
10178 // for the reduction-identifier. For a max or min reduction in C, the type
10179 // of the list item must be an allowed arithmetic data type: char, int,
10180 // float, double, or _Bool, possibly modified with long, short, signed, or
10181 // unsigned. For a max or min reduction in C++, the type of the list item
10182 // must be an allowed arithmetic data type: char, wchar_t, int, float,
10183 // double, or bool, possibly modified with long, short, signed, or unsigned.
10184 if (DeclareReductionRef.isUnset()) {
10185 if ((BOK == BO_GT || BOK == BO_LT) &&
10186 !(Type->isScalarType() ||
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010187 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
10188 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
Alexey Bataev169d96a2017-07-18 20:17:46 +000010189 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010190 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010191 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10192 VarDecl::DeclarationOnly;
10193 S.Diag(D->getLocation(),
10194 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010195 << D;
10196 }
10197 continue;
10198 }
10199 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010200 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) {
Alexey Bataev169d96a2017-07-18 20:17:46 +000010201 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg)
10202 << getOpenMPClauseName(ClauseKind);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010203 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010204 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10205 VarDecl::DeclarationOnly;
10206 S.Diag(D->getLocation(),
10207 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010208 << D;
10209 }
10210 continue;
10211 }
10212 }
10213
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010214 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010215 auto *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs",
Alexey Bataev60da77e2016-02-29 05:54:20 +000010216 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010217 auto *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(),
Alexey Bataev60da77e2016-02-29 05:54:20 +000010218 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010219 auto PrivateTy = Type;
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010220
10221 // Try if we can determine constant lengths for all array sections and avoid
10222 // the VLA.
10223 bool ConstantLengthOASE = false;
10224 if (OASE) {
10225 bool SingleElement;
10226 llvm::SmallVector<llvm::APSInt, 4> ArraySizes;
10227 ConstantLengthOASE = CheckOMPArraySectionConstantForReduction(
10228 Context, OASE, SingleElement, ArraySizes);
10229
10230 // If we don't have a single element, we must emit a constant array type.
10231 if (ConstantLengthOASE && !SingleElement) {
10232 for (auto &Size : ArraySizes) {
10233 PrivateTy = Context.getConstantArrayType(
10234 PrivateTy, Size, ArrayType::Normal, /*IndexTypeQuals=*/0);
10235 }
10236 }
10237 }
10238
10239 if ((OASE && !ConstantLengthOASE) ||
Jonas Hahnfeld96087f32017-11-02 13:30:42 +000010240 (!OASE && !ASE &&
Alexey Bataev60da77e2016-02-29 05:54:20 +000010241 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Jonas Hahnfeld87d44262017-11-18 21:00:46 +000010242 if (!Context.getTargetInfo().isVLASupported() &&
10243 S.shouldDiagnoseTargetSupportFromOpenMP()) {
10244 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
10245 S.Diag(ELoc, diag::note_vla_unsupported);
10246 continue;
10247 }
David Majnemer9d168222016-08-05 17:44:54 +000010248 // For arrays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010249 // Create pseudo array type for private copy. The size for this array will
10250 // be generated during codegen.
10251 // For array subscripts or single variables Private Ty is the same as Type
10252 // (type of the variable or single array element).
10253 PrivateTy = Context.getVariableArrayType(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010254 Type,
Alexey Bataevd070a582017-10-25 15:54:04 +000010255 new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue),
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010256 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +000010257 } else if (!ASE && !OASE &&
10258 Context.getAsArrayType(D->getType().getNonReferenceType()))
10259 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010260 // Private copy.
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010261 auto *PrivateVD = buildVarDecl(S, ELoc, PrivateTy, D->getName(),
Alexey Bataev60da77e2016-02-29 05:54:20 +000010262 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010263 // Add initializer for private variable.
10264 Expr *Init = nullptr;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010265 auto *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc);
10266 auto *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010267 if (DeclareReductionRef.isUsable()) {
10268 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
10269 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
10270 if (DRD->getInitializer()) {
10271 Init = DRDRef;
10272 RHSVD->setInit(DRDRef);
10273 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010274 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010275 } else {
10276 switch (BOK) {
10277 case BO_Add:
10278 case BO_Xor:
10279 case BO_Or:
10280 case BO_LOr:
10281 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
10282 if (Type->isScalarType() || Type->isAnyComplexType())
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010283 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010284 break;
10285 case BO_Mul:
10286 case BO_LAnd:
10287 if (Type->isScalarType() || Type->isAnyComplexType()) {
10288 // '*' and '&&' reduction ops - initializer is '1'.
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010289 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +000010290 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010291 break;
10292 case BO_And: {
10293 // '&' reduction op - initializer is '~0'.
10294 QualType OrigType = Type;
10295 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
10296 Type = ComplexTy->getElementType();
10297 if (Type->isRealFloatingType()) {
10298 llvm::APFloat InitValue =
10299 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
10300 /*isIEEE=*/true);
10301 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
10302 Type, ELoc);
10303 } else if (Type->isScalarType()) {
10304 auto Size = Context.getTypeSize(Type);
10305 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
10306 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
10307 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
10308 }
10309 if (Init && OrigType->isAnyComplexType()) {
10310 // Init = 0xFFFF + 0xFFFFi;
10311 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010312 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010313 }
10314 Type = OrigType;
10315 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010316 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010317 case BO_LT:
10318 case BO_GT: {
10319 // 'min' reduction op - initializer is 'Largest representable number in
10320 // the reduction list item type'.
10321 // 'max' reduction op - initializer is 'Least representable number in
10322 // the reduction list item type'.
10323 if (Type->isIntegerType() || Type->isPointerType()) {
10324 bool IsSigned = Type->hasSignedIntegerRepresentation();
10325 auto Size = Context.getTypeSize(Type);
10326 QualType IntTy =
10327 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
10328 llvm::APInt InitValue =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010329 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
10330 : llvm::APInt::getMinValue(Size)
10331 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
10332 : llvm::APInt::getMaxValue(Size);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010333 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
10334 if (Type->isPointerType()) {
10335 // Cast to pointer type.
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010336 auto CastExpr = S.BuildCStyleCastExpr(
Alexey Bataevd070a582017-10-25 15:54:04 +000010337 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010338 if (CastExpr.isInvalid())
10339 continue;
10340 Init = CastExpr.get();
10341 }
10342 } else if (Type->isRealFloatingType()) {
10343 llvm::APFloat InitValue = llvm::APFloat::getLargest(
10344 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
10345 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
10346 Type, ELoc);
10347 }
10348 break;
10349 }
10350 case BO_PtrMemD:
10351 case BO_PtrMemI:
10352 case BO_MulAssign:
10353 case BO_Div:
10354 case BO_Rem:
10355 case BO_Sub:
10356 case BO_Shl:
10357 case BO_Shr:
10358 case BO_LE:
10359 case BO_GE:
10360 case BO_EQ:
10361 case BO_NE:
Richard Smithc70f1d62017-12-14 15:16:18 +000010362 case BO_Cmp:
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010363 case BO_AndAssign:
10364 case BO_XorAssign:
10365 case BO_OrAssign:
10366 case BO_Assign:
10367 case BO_AddAssign:
10368 case BO_SubAssign:
10369 case BO_DivAssign:
10370 case BO_RemAssign:
10371 case BO_ShlAssign:
10372 case BO_ShrAssign:
10373 case BO_Comma:
10374 llvm_unreachable("Unexpected reduction operation");
10375 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010376 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010377 if (Init && DeclareReductionRef.isUnset())
10378 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false);
10379 else if (!Init)
10380 S.ActOnUninitializedDecl(RHSVD);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010381 if (RHSVD->isInvalidDecl())
10382 continue;
10383 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010384 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible)
10385 << Type << ReductionIdRange;
10386 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10387 VarDecl::DeclarationOnly;
10388 S.Diag(D->getLocation(),
10389 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +000010390 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010391 continue;
10392 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010393 // Store initializer for single element in private copy. Will be used during
10394 // codegen.
10395 PrivateVD->setInit(RHSVD->getInit());
10396 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010397 auto *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010398 ExprResult ReductionOp;
10399 if (DeclareReductionRef.isUsable()) {
10400 QualType RedTy = DeclareReductionRef.get()->getType();
10401 QualType PtrRedTy = Context.getPointerType(RedTy);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010402 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
10403 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010404 if (!BasePath.empty()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010405 LHS = S.DefaultLvalueConversion(LHS.get());
10406 RHS = S.DefaultLvalueConversion(RHS.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010407 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
10408 CK_UncheckedDerivedToBase, LHS.get(),
10409 &BasePath, LHS.get()->getValueKind());
10410 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
10411 CK_UncheckedDerivedToBase, RHS.get(),
10412 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010413 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010414 FunctionProtoType::ExtProtoInfo EPI;
10415 QualType Params[] = {PtrRedTy, PtrRedTy};
10416 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
10417 auto *OVE = new (Context) OpaqueValueExpr(
10418 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010419 S.DefaultLvalueConversion(DeclareReductionRef.get()).get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010420 Expr *Args[] = {LHS.get(), RHS.get()};
10421 ReductionOp = new (Context)
10422 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
10423 } else {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010424 ReductionOp = S.BuildBinOp(
10425 Stack->getCurScope(), ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010426 if (ReductionOp.isUsable()) {
10427 if (BOK != BO_LT && BOK != BO_GT) {
10428 ReductionOp =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010429 S.BuildBinOp(Stack->getCurScope(), ReductionId.getLocStart(),
10430 BO_Assign, LHSDRE, ReductionOp.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010431 } else {
Alexey Bataevd070a582017-10-25 15:54:04 +000010432 auto *ConditionalOp = new (Context)
10433 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE,
10434 Type, VK_LValue, OK_Ordinary);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010435 ReductionOp =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010436 S.BuildBinOp(Stack->getCurScope(), ReductionId.getLocStart(),
10437 BO_Assign, LHSDRE, ConditionalOp);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010438 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000010439 if (ReductionOp.isUsable())
10440 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010441 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000010442 if (!ReductionOp.isUsable())
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010443 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010444 }
10445
Alexey Bataevfa312f32017-07-21 18:48:21 +000010446 // OpenMP [2.15.4.6, Restrictions, p.2]
10447 // A list item that appears in an in_reduction clause of a task construct
10448 // must appear in a task_reduction clause of a construct associated with a
10449 // taskgroup region that includes the participating task in its taskgroup
10450 // set. The construct associated with the innermost region that meets this
10451 // condition must specify the same reduction-identifier as the in_reduction
10452 // clause.
10453 if (ClauseKind == OMPC_in_reduction) {
Alexey Bataevfa312f32017-07-21 18:48:21 +000010454 SourceRange ParentSR;
10455 BinaryOperatorKind ParentBOK;
10456 const Expr *ParentReductionOp;
Alexey Bataev88202be2017-07-27 13:20:36 +000010457 Expr *ParentBOKTD, *ParentReductionOpTD;
Alexey Bataevf189cb72017-07-24 14:52:13 +000010458 DSAStackTy::DSAVarData ParentBOKDSA =
Alexey Bataev88202be2017-07-27 13:20:36 +000010459 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK,
10460 ParentBOKTD);
Alexey Bataevf189cb72017-07-24 14:52:13 +000010461 DSAStackTy::DSAVarData ParentReductionOpDSA =
Alexey Bataev88202be2017-07-27 13:20:36 +000010462 Stack->getTopMostTaskgroupReductionData(
10463 D, ParentSR, ParentReductionOp, ParentReductionOpTD);
Alexey Bataevf189cb72017-07-24 14:52:13 +000010464 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown;
10465 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown;
10466 if (!IsParentBOK && !IsParentReductionOp) {
10467 S.Diag(ELoc, diag::err_omp_in_reduction_not_task_reduction);
10468 continue;
10469 }
Alexey Bataevfa312f32017-07-21 18:48:21 +000010470 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) ||
10471 (DeclareReductionRef.isUsable() && IsParentBOK) || BOK != ParentBOK ||
10472 IsParentReductionOp) {
10473 bool EmitError = true;
10474 if (IsParentReductionOp && DeclareReductionRef.isUsable()) {
10475 llvm::FoldingSetNodeID RedId, ParentRedId;
10476 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true);
10477 DeclareReductionRef.get()->Profile(RedId, Context,
10478 /*Canonical=*/true);
10479 EmitError = RedId != ParentRedId;
10480 }
10481 if (EmitError) {
10482 S.Diag(ReductionId.getLocStart(),
10483 diag::err_omp_reduction_identifier_mismatch)
10484 << ReductionIdRange << RefExpr->getSourceRange();
10485 S.Diag(ParentSR.getBegin(),
10486 diag::note_omp_previous_reduction_identifier)
Alexey Bataevf189cb72017-07-24 14:52:13 +000010487 << ParentSR
10488 << (IsParentBOK ? ParentBOKDSA.RefExpr
10489 : ParentReductionOpDSA.RefExpr)
10490 ->getSourceRange();
Alexey Bataevfa312f32017-07-21 18:48:21 +000010491 continue;
10492 }
10493 }
Alexey Bataev88202be2017-07-27 13:20:36 +000010494 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD;
10495 assert(TaskgroupDescriptor && "Taskgroup descriptor must be defined.");
Alexey Bataevfa312f32017-07-21 18:48:21 +000010496 }
10497
Alexey Bataev60da77e2016-02-29 05:54:20 +000010498 DeclRefExpr *Ref = nullptr;
10499 Expr *VarsExpr = RefExpr->IgnoreParens();
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010500 if (!VD && !S.CurContext->isDependentContext()) {
Alexey Bataev60da77e2016-02-29 05:54:20 +000010501 if (ASE || OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010502 TransformExprToCaptures RebuildToCapture(S, D);
Alexey Bataev60da77e2016-02-29 05:54:20 +000010503 VarsExpr =
10504 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
10505 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +000010506 } else {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010507 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +000010508 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010509 if (!S.IsOpenMPCapturedDecl(D)) {
10510 RD.ExprCaptures.emplace_back(Ref->getDecl());
Alexey Bataev5a3af132016-03-29 08:58:54 +000010511 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010512 ExprResult RefRes = S.DefaultLvalueConversion(Ref);
Alexey Bataev5a3af132016-03-29 08:58:54 +000010513 if (!RefRes.isUsable())
10514 continue;
10515 ExprResult PostUpdateRes =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010516 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
10517 RefRes.get());
Alexey Bataev5a3af132016-03-29 08:58:54 +000010518 if (!PostUpdateRes.isUsable())
10519 continue;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010520 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
10521 Stack->getCurrentDirective() == OMPD_taskgroup) {
10522 S.Diag(RefExpr->getExprLoc(),
10523 diag::err_omp_reduction_non_addressable_expression)
Alexey Bataevbcd0ae02017-07-11 19:16:44 +000010524 << RefExpr->getSourceRange();
10525 continue;
10526 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010527 RD.ExprPostUpdates.emplace_back(
10528 S.IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +000010529 }
10530 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000010531 }
Alexey Bataev169d96a2017-07-18 20:17:46 +000010532 // All reduction items are still marked as reduction (to do not increase
10533 // code base size).
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010534 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
Alexey Bataevf189cb72017-07-24 14:52:13 +000010535 if (CurrDir == OMPD_taskgroup) {
10536 if (DeclareReductionRef.isUsable())
Alexey Bataev3b1b8952017-07-25 15:53:26 +000010537 Stack->addTaskgroupReductionData(D, ReductionIdRange,
10538 DeclareReductionRef.get());
Alexey Bataevf189cb72017-07-24 14:52:13 +000010539 else
Alexey Bataev3b1b8952017-07-25 15:53:26 +000010540 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK);
Alexey Bataevf189cb72017-07-24 14:52:13 +000010541 }
Alexey Bataev88202be2017-07-27 13:20:36 +000010542 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(),
10543 TaskgroupDescriptor);
Alexey Bataevc5e02582014-06-16 07:08:35 +000010544 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010545 return RD.Vars.empty();
10546}
Alexey Bataevc5e02582014-06-16 07:08:35 +000010547
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010548OMPClause *Sema::ActOnOpenMPReductionClause(
10549 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
10550 SourceLocation ColonLoc, SourceLocation EndLoc,
10551 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
10552 ArrayRef<Expr *> UnresolvedReductions) {
10553 ReductionData RD(VarList.size());
10554
Alexey Bataev169d96a2017-07-18 20:17:46 +000010555 if (ActOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList,
10556 StartLoc, LParenLoc, ColonLoc, EndLoc,
10557 ReductionIdScopeSpec, ReductionId,
10558 UnresolvedReductions, RD))
Alexey Bataevc5e02582014-06-16 07:08:35 +000010559 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +000010560
Alexey Bataevc5e02582014-06-16 07:08:35 +000010561 return OMPReductionClause::Create(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010562 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
10563 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
10564 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
10565 buildPreInits(Context, RD.ExprCaptures),
10566 buildPostUpdate(*this, RD.ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +000010567}
10568
Alexey Bataev169d96a2017-07-18 20:17:46 +000010569OMPClause *Sema::ActOnOpenMPTaskReductionClause(
10570 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
10571 SourceLocation ColonLoc, SourceLocation EndLoc,
10572 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
10573 ArrayRef<Expr *> UnresolvedReductions) {
10574 ReductionData RD(VarList.size());
10575
10576 if (ActOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction,
10577 VarList, StartLoc, LParenLoc, ColonLoc,
10578 EndLoc, ReductionIdScopeSpec, ReductionId,
10579 UnresolvedReductions, RD))
10580 return nullptr;
10581
10582 return OMPTaskReductionClause::Create(
10583 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
10584 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
10585 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
10586 buildPreInits(Context, RD.ExprCaptures),
10587 buildPostUpdate(*this, RD.ExprPostUpdates));
10588}
10589
Alexey Bataevfa312f32017-07-21 18:48:21 +000010590OMPClause *Sema::ActOnOpenMPInReductionClause(
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
10597 if (ActOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList,
10598 StartLoc, LParenLoc, ColonLoc, EndLoc,
10599 ReductionIdScopeSpec, ReductionId,
10600 UnresolvedReductions, RD))
10601 return nullptr;
10602
10603 return OMPInReductionClause::Create(
10604 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
10605 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
Alexey Bataev88202be2017-07-27 13:20:36 +000010606 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors,
Alexey Bataevfa312f32017-07-21 18:48:21 +000010607 buildPreInits(Context, RD.ExprCaptures),
10608 buildPostUpdate(*this, RD.ExprPostUpdates));
10609}
10610
Alexey Bataevecba70f2016-04-12 11:02:11 +000010611bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
10612 SourceLocation LinLoc) {
10613 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
10614 LinKind == OMPC_LINEAR_unknown) {
10615 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
10616 return true;
10617 }
10618 return false;
10619}
10620
10621bool Sema::CheckOpenMPLinearDecl(ValueDecl *D, SourceLocation ELoc,
10622 OpenMPLinearClauseKind LinKind,
10623 QualType Type) {
10624 auto *VD = dyn_cast_or_null<VarDecl>(D);
10625 // A variable must not have an incomplete type or a reference type.
10626 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
10627 return true;
10628 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
10629 !Type->isReferenceType()) {
10630 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
10631 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
10632 return true;
10633 }
10634 Type = Type.getNonReferenceType();
10635
10636 // A list item must not be const-qualified.
10637 if (Type.isConstant(Context)) {
10638 Diag(ELoc, diag::err_omp_const_variable)
10639 << getOpenMPClauseName(OMPC_linear);
10640 if (D) {
10641 bool IsDecl =
10642 !VD ||
10643 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
10644 Diag(D->getLocation(),
10645 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
10646 << D;
10647 }
10648 return true;
10649 }
10650
10651 // A list item must be of integral or pointer type.
10652 Type = Type.getUnqualifiedType().getCanonicalType();
10653 const auto *Ty = Type.getTypePtrOrNull();
10654 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
10655 !Ty->isPointerType())) {
10656 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
10657 if (D) {
10658 bool IsDecl =
10659 !VD ||
10660 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
10661 Diag(D->getLocation(),
10662 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
10663 << D;
10664 }
10665 return true;
10666 }
10667 return false;
10668}
10669
Alexey Bataev182227b2015-08-20 10:54:39 +000010670OMPClause *Sema::ActOnOpenMPLinearClause(
10671 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
10672 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
10673 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +000010674 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010675 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +000010676 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +000010677 SmallVector<Decl *, 4> ExprCaptures;
10678 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +000010679 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +000010680 LinKind = OMPC_LINEAR_val;
Alexey Bataeved09d242014-05-28 05:53:51 +000010681 for (auto &RefExpr : VarList) {
10682 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010683 SourceLocation ELoc;
10684 SourceRange ERange;
10685 Expr *SimpleRefExpr = RefExpr;
10686 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
10687 /*AllowArraySection=*/false);
10688 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +000010689 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000010690 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010691 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +000010692 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +000010693 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010694 ValueDecl *D = Res.first;
10695 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +000010696 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +000010697
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010698 QualType Type = D->getType();
10699 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +000010700
10701 // OpenMP [2.14.3.7, linear clause]
10702 // A list-item cannot appear in more than one linear clause.
10703 // A list-item that appears in a linear clause cannot appear in any
10704 // other data-sharing attribute clause.
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010705 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman8dba6642014-04-22 13:09:42 +000010706 if (DVar.RefExpr) {
10707 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
10708 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010709 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +000010710 continue;
10711 }
10712
Alexey Bataevecba70f2016-04-12 11:02:11 +000010713 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +000010714 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +000010715 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +000010716
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010717 // Build private copy of original var.
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010718 auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(),
10719 D->hasAttrs() ? &D->getAttrs() : nullptr);
10720 auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +000010721 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010722 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000010723 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010724 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010725 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev78849fb2016-03-09 09:49:00 +000010726 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
10727 if (!IsOpenMPCapturedDecl(D)) {
10728 ExprCaptures.push_back(Ref->getDecl());
10729 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
10730 ExprResult RefRes = DefaultLvalueConversion(Ref);
10731 if (!RefRes.isUsable())
10732 continue;
10733 ExprResult PostUpdateRes =
10734 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
10735 SimpleRefExpr, RefRes.get());
10736 if (!PostUpdateRes.isUsable())
10737 continue;
10738 ExprPostUpdates.push_back(
10739 IgnoredValueConversions(PostUpdateRes.get()).get());
10740 }
10741 }
10742 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000010743 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010744 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000010745 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010746 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000010747 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000010748 /*DirectInit=*/false);
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010749 auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
10750
10751 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010752 Vars.push_back((VD || CurContext->isDependentContext())
10753 ? RefExpr->IgnoreParens()
10754 : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010755 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +000010756 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +000010757 }
10758
10759 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000010760 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000010761
10762 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +000010763 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000010764 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
10765 !Step->isInstantiationDependent() &&
10766 !Step->containsUnexpandedParameterPack()) {
10767 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +000010768 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +000010769 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000010770 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +000010771 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +000010772
Alexander Musman3276a272015-03-21 10:12:56 +000010773 // Build var to save the step value.
10774 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +000010775 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +000010776 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +000010777 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +000010778 ExprResult CalcStep =
10779 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +000010780 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +000010781
Alexander Musman8dba6642014-04-22 13:09:42 +000010782 // Warn about zero linear step (it would be probably better specified as
10783 // making corresponding variables 'const').
10784 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +000010785 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
10786 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +000010787 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
10788 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +000010789 if (!IsConstant && CalcStep.isUsable()) {
10790 // Calculate the step beforehand instead of doing this on each iteration.
10791 // (This is not used if the number of iterations may be kfold-ed).
10792 CalcStepExpr = CalcStep.get();
10793 }
Alexander Musman8dba6642014-04-22 13:09:42 +000010794 }
10795
Alexey Bataev182227b2015-08-20 10:54:39 +000010796 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
10797 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +000010798 StepExpr, CalcStepExpr,
10799 buildPreInits(Context, ExprCaptures),
10800 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +000010801}
10802
Alexey Bataev5dff95c2016-04-22 03:56:56 +000010803static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
10804 Expr *NumIterations, Sema &SemaRef,
10805 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +000010806 // Walk the vars and build update/final expressions for the CodeGen.
10807 SmallVector<Expr *, 8> Updates;
10808 SmallVector<Expr *, 8> Finals;
10809 Expr *Step = Clause.getStep();
10810 Expr *CalcStep = Clause.getCalcStep();
10811 // OpenMP [2.14.3.7, linear clause]
10812 // If linear-step is not specified it is assumed to be 1.
10813 if (Step == nullptr)
10814 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +000010815 else if (CalcStep) {
Alexander Musman3276a272015-03-21 10:12:56 +000010816 Step = cast<BinaryOperator>(CalcStep)->getLHS();
Alexey Bataev5a3af132016-03-29 08:58:54 +000010817 }
Alexander Musman3276a272015-03-21 10:12:56 +000010818 bool HasErrors = false;
10819 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010820 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000010821 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +000010822 for (auto &RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +000010823 SourceLocation ELoc;
10824 SourceRange ERange;
10825 Expr *SimpleRefExpr = RefExpr;
10826 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange,
10827 /*AllowArraySection=*/false);
10828 ValueDecl *D = Res.first;
10829 if (Res.second || !D) {
10830 Updates.push_back(nullptr);
10831 Finals.push_back(nullptr);
10832 HasErrors = true;
10833 continue;
10834 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +000010835 auto &&Info = Stack->isLoopControlVariable(D);
Alexey Bataev2b86f212017-11-29 21:31:48 +000010836 // OpenMP [2.15.11, distribute simd Construct]
10837 // A list item may not appear in a linear clause, unless it is the loop
10838 // iteration variable.
10839 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) &&
10840 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) {
10841 SemaRef.Diag(ELoc,
10842 diag::err_omp_linear_distribute_var_non_loop_iteration);
10843 Updates.push_back(nullptr);
10844 Finals.push_back(nullptr);
10845 HasErrors = true;
10846 continue;
10847 }
Alexander Musman3276a272015-03-21 10:12:56 +000010848 Expr *InitExpr = *CurInit;
10849
10850 // Build privatized reference to the current linear var.
David Majnemer9d168222016-08-05 17:44:54 +000010851 auto *DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000010852 Expr *CapturedRef;
10853 if (LinKind == OMPC_LINEAR_uval)
10854 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
10855 else
10856 CapturedRef =
10857 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
10858 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
10859 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +000010860
10861 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000010862 ExprResult Update;
10863 if (!Info.first) {
10864 Update =
10865 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
10866 InitExpr, IV, Step, /* Subtract */ false);
10867 } else
10868 Update = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +000010869 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
10870 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +000010871
10872 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000010873 ExprResult Final;
10874 if (!Info.first) {
10875 Final = BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
10876 InitExpr, NumIterations, Step,
10877 /* Subtract */ false);
10878 } else
10879 Final = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +000010880 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
10881 /*DiscardedValue=*/true);
Alexey Bataev5dff95c2016-04-22 03:56:56 +000010882
Alexander Musman3276a272015-03-21 10:12:56 +000010883 if (!Update.isUsable() || !Final.isUsable()) {
10884 Updates.push_back(nullptr);
10885 Finals.push_back(nullptr);
10886 HasErrors = true;
10887 } else {
10888 Updates.push_back(Update.get());
10889 Finals.push_back(Final.get());
10890 }
Richard Trieucc3949d2016-02-18 22:34:54 +000010891 ++CurInit;
10892 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +000010893 }
10894 Clause.setUpdates(Updates);
10895 Clause.setFinals(Finals);
10896 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +000010897}
10898
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010899OMPClause *Sema::ActOnOpenMPAlignedClause(
10900 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
10901 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
10902
10903 SmallVector<Expr *, 8> Vars;
10904 for (auto &RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +000010905 assert(RefExpr && "NULL expr in OpenMP linear clause.");
10906 SourceLocation ELoc;
10907 SourceRange ERange;
10908 Expr *SimpleRefExpr = RefExpr;
10909 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
10910 /*AllowArraySection=*/false);
10911 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010912 // It will be analyzed later.
10913 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010914 }
Alexey Bataev1efd1662016-03-29 10:59:56 +000010915 ValueDecl *D = Res.first;
10916 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010917 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010918
Alexey Bataev1efd1662016-03-29 10:59:56 +000010919 QualType QType = D->getType();
10920 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010921
10922 // OpenMP [2.8.1, simd construct, Restrictions]
10923 // The type of list items appearing in the aligned clause must be
10924 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010925 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010926 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +000010927 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010928 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +000010929 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010930 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +000010931 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010932 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +000010933 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010934 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +000010935 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010936 continue;
10937 }
10938
10939 // OpenMP [2.8.1, simd construct, Restrictions]
10940 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev1efd1662016-03-29 10:59:56 +000010941 if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +000010942 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010943 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
10944 << getOpenMPClauseName(OMPC_aligned);
10945 continue;
10946 }
10947
Alexey Bataev1efd1662016-03-29 10:59:56 +000010948 DeclRefExpr *Ref = nullptr;
10949 if (!VD && IsOpenMPCapturedDecl(D))
10950 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
10951 Vars.push_back(DefaultFunctionArrayConversion(
10952 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
10953 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +000010954 }
10955
10956 // OpenMP [2.8.1, simd construct, Description]
10957 // The parameter of the aligned clause, alignment, must be a constant
10958 // positive integer expression.
10959 // If no optional parameter is specified, implementation-defined default
10960 // alignments for SIMD instructions on the target platforms are assumed.
10961 if (Alignment != nullptr) {
10962 ExprResult AlignResult =
10963 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
10964 if (AlignResult.isInvalid())
10965 return nullptr;
10966 Alignment = AlignResult.get();
10967 }
10968 if (Vars.empty())
10969 return nullptr;
10970
10971 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
10972 EndLoc, Vars, Alignment);
10973}
10974
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010975OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
10976 SourceLocation StartLoc,
10977 SourceLocation LParenLoc,
10978 SourceLocation EndLoc) {
10979 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010980 SmallVector<Expr *, 8> SrcExprs;
10981 SmallVector<Expr *, 8> DstExprs;
10982 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +000010983 for (auto &RefExpr : VarList) {
10984 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
10985 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010986 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000010987 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000010988 SrcExprs.push_back(nullptr);
10989 DstExprs.push_back(nullptr);
10990 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010991 continue;
10992 }
10993
Alexey Bataeved09d242014-05-28 05:53:51 +000010994 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000010995 // OpenMP [2.1, C/C++]
10996 // A list item is a variable name.
10997 // OpenMP [2.14.4.1, Restrictions, p.1]
10998 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +000010999 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011000 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000011001 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
11002 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011003 continue;
11004 }
11005
11006 Decl *D = DE->getDecl();
11007 VarDecl *VD = cast<VarDecl>(D);
11008
11009 QualType Type = VD->getType();
11010 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
11011 // It will be analyzed later.
11012 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011013 SrcExprs.push_back(nullptr);
11014 DstExprs.push_back(nullptr);
11015 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011016 continue;
11017 }
11018
11019 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
11020 // A list item that appears in a copyin clause must be threadprivate.
11021 if (!DSAStack->isThreadPrivate(VD)) {
11022 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +000011023 << getOpenMPClauseName(OMPC_copyin)
11024 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011025 continue;
11026 }
11027
11028 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
11029 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +000011030 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011031 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011032 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000011033 auto *SrcVD =
11034 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
11035 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +000011036 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011037 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
11038 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000011039 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
11040 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011041 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011042 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011043 // For arrays generate assignment operation for single element and replace
11044 // it by the original array element in CodeGen.
11045 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
11046 PseudoDstExpr, PseudoSrcExpr);
11047 if (AssignmentOp.isInvalid())
11048 continue;
11049 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
11050 /*DiscardedValue=*/true);
11051 if (AssignmentOp.isInvalid())
11052 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011053
11054 DSAStack->addDSA(VD, DE, OMPC_copyin);
11055 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011056 SrcExprs.push_back(PseudoSrcExpr);
11057 DstExprs.push_back(PseudoDstExpr);
11058 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011059 }
11060
Alexey Bataeved09d242014-05-28 05:53:51 +000011061 if (Vars.empty())
11062 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011063
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011064 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
11065 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011066}
11067
Alexey Bataevbae9a792014-06-27 10:37:06 +000011068OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
11069 SourceLocation StartLoc,
11070 SourceLocation LParenLoc,
11071 SourceLocation EndLoc) {
11072 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +000011073 SmallVector<Expr *, 8> SrcExprs;
11074 SmallVector<Expr *, 8> DstExprs;
11075 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +000011076 for (auto &RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +000011077 assert(RefExpr && "NULL expr in OpenMP linear clause.");
11078 SourceLocation ELoc;
11079 SourceRange ERange;
11080 Expr *SimpleRefExpr = RefExpr;
11081 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
11082 /*AllowArraySection=*/false);
11083 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000011084 // It will be analyzed later.
11085 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000011086 SrcExprs.push_back(nullptr);
11087 DstExprs.push_back(nullptr);
11088 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011089 }
Alexey Bataeve122da12016-03-17 10:50:17 +000011090 ValueDecl *D = Res.first;
11091 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +000011092 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000011093
Alexey Bataeve122da12016-03-17 10:50:17 +000011094 QualType Type = D->getType();
11095 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011096
11097 // OpenMP [2.14.4.2, Restrictions, p.2]
11098 // A list item that appears in a copyprivate clause may not appear in a
11099 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +000011100 if (!VD || !DSAStack->isThreadPrivate(VD)) {
11101 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +000011102 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
11103 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000011104 Diag(ELoc, diag::err_omp_wrong_dsa)
11105 << getOpenMPClauseName(DVar.CKind)
11106 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve122da12016-03-17 10:50:17 +000011107 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011108 continue;
11109 }
11110
11111 // OpenMP [2.11.4.2, Restrictions, p.1]
11112 // All list items that appear in a copyprivate clause must be either
11113 // threadprivate or private in the enclosing context.
11114 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +000011115 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011116 if (DVar.CKind == OMPC_shared) {
11117 Diag(ELoc, diag::err_omp_required_access)
11118 << getOpenMPClauseName(OMPC_copyprivate)
11119 << "threadprivate or private in the enclosing context";
Alexey Bataeve122da12016-03-17 10:50:17 +000011120 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011121 continue;
11122 }
11123 }
11124 }
11125
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011126 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000011127 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011128 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +000011129 << getOpenMPClauseName(OMPC_copyprivate) << Type
11130 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011131 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +000011132 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011133 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +000011134 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011135 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +000011136 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011137 continue;
11138 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +000011139
Alexey Bataevbae9a792014-06-27 10:37:06 +000011140 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
11141 // A variable of class type (or array thereof) that appears in a
11142 // copyin clause requires an accessible, unambiguous copy assignment
11143 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011144 Type = Context.getBaseElementType(Type.getNonReferenceType())
11145 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +000011146 auto *SrcVD =
Alexey Bataeve122da12016-03-17 10:50:17 +000011147 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src",
11148 D->hasAttrs() ? &D->getAttrs() : nullptr);
11149 auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
Alexey Bataev420d45b2015-04-14 05:11:24 +000011150 auto *DstVD =
Alexey Bataeve122da12016-03-17 10:50:17 +000011151 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst",
11152 D->hasAttrs() ? &D->getAttrs() : nullptr);
David Majnemer9d168222016-08-05 17:44:54 +000011153 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataeve122da12016-03-17 10:50:17 +000011154 auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
Alexey Bataeva63048e2015-03-23 06:18:07 +000011155 PseudoDstExpr, PseudoSrcExpr);
11156 if (AssignmentOp.isInvalid())
11157 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +000011158 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +000011159 /*DiscardedValue=*/true);
11160 if (AssignmentOp.isInvalid())
11161 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000011162
11163 // No need to mark vars as copyprivate, they are already threadprivate or
11164 // implicitly private.
Alexey Bataeve122da12016-03-17 10:50:17 +000011165 assert(VD || IsOpenMPCapturedDecl(D));
11166 Vars.push_back(
11167 VD ? RefExpr->IgnoreParens()
11168 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +000011169 SrcExprs.push_back(PseudoSrcExpr);
11170 DstExprs.push_back(PseudoDstExpr);
11171 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +000011172 }
11173
11174 if (Vars.empty())
11175 return nullptr;
11176
Alexey Bataeva63048e2015-03-23 06:18:07 +000011177 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11178 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011179}
11180
Alexey Bataev6125da92014-07-21 11:26:11 +000011181OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
11182 SourceLocation StartLoc,
11183 SourceLocation LParenLoc,
11184 SourceLocation EndLoc) {
11185 if (VarList.empty())
11186 return nullptr;
11187
11188 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
11189}
Alexey Bataevdea47612014-07-23 07:46:59 +000011190
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011191OMPClause *
11192Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
11193 SourceLocation DepLoc, SourceLocation ColonLoc,
11194 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
11195 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +000011196 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011197 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +000011198 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000011199 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +000011200 return nullptr;
11201 }
11202 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011203 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
11204 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +000011205 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011206 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000011207 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
11208 /*Last=*/OMPC_DEPEND_unknown, Except)
11209 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011210 return nullptr;
11211 }
11212 SmallVector<Expr *, 8> Vars;
Alexey Bataev8b427062016-05-25 12:36:08 +000011213 DSAStackTy::OperatorOffsetTy OpsOffs;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011214 llvm::APSInt DepCounter(/*BitWidth=*/32);
11215 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
11216 if (DepKind == OMPC_DEPEND_sink) {
11217 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
11218 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
11219 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011220 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011221 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011222 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
11223 DSAStack->getParentOrderedRegionParam()) {
11224 for (auto &RefExpr : VarList) {
11225 assert(RefExpr && "NULL expr in OpenMP shared clause.");
Alexey Bataev8b427062016-05-25 12:36:08 +000011226 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011227 // It will be analyzed later.
11228 Vars.push_back(RefExpr);
11229 continue;
11230 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011231
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011232 SourceLocation ELoc = RefExpr->getExprLoc();
11233 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
11234 if (DepKind == OMPC_DEPEND_sink) {
11235 if (DepCounter >= TotalDepCount) {
11236 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
11237 continue;
11238 }
11239 ++DepCounter;
11240 // OpenMP [2.13.9, Summary]
11241 // depend(dependence-type : vec), where dependence-type is:
11242 // 'sink' and where vec is the iteration vector, which has the form:
11243 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
11244 // where n is the value specified by the ordered clause in the loop
11245 // directive, xi denotes the loop iteration variable of the i-th nested
11246 // loop associated with the loop directive, and di is a constant
11247 // non-negative integer.
Alexey Bataev8b427062016-05-25 12:36:08 +000011248 if (CurContext->isDependentContext()) {
11249 // It will be analyzed later.
11250 Vars.push_back(RefExpr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011251 continue;
11252 }
Alexey Bataev8b427062016-05-25 12:36:08 +000011253 SimpleExpr = SimpleExpr->IgnoreImplicit();
11254 OverloadedOperatorKind OOK = OO_None;
11255 SourceLocation OOLoc;
11256 Expr *LHS = SimpleExpr;
11257 Expr *RHS = nullptr;
11258 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
11259 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
11260 OOLoc = BO->getOperatorLoc();
11261 LHS = BO->getLHS()->IgnoreParenImpCasts();
11262 RHS = BO->getRHS()->IgnoreParenImpCasts();
11263 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
11264 OOK = OCE->getOperator();
11265 OOLoc = OCE->getOperatorLoc();
11266 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
11267 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
11268 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
11269 OOK = MCE->getMethodDecl()
11270 ->getNameInfo()
11271 .getName()
11272 .getCXXOverloadedOperator();
11273 OOLoc = MCE->getCallee()->getExprLoc();
11274 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
11275 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
11276 }
11277 SourceLocation ELoc;
11278 SourceRange ERange;
11279 auto Res = getPrivateItem(*this, LHS, ELoc, ERange,
11280 /*AllowArraySection=*/false);
11281 if (Res.second) {
11282 // It will be analyzed later.
11283 Vars.push_back(RefExpr);
11284 }
11285 ValueDecl *D = Res.first;
11286 if (!D)
11287 continue;
11288
11289 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
11290 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
11291 continue;
11292 }
11293 if (RHS) {
11294 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
11295 RHS, OMPC_depend, /*StrictlyPositive=*/false);
11296 if (RHSRes.isInvalid())
11297 continue;
11298 }
11299 if (!CurContext->isDependentContext() &&
11300 DSAStack->getParentOrderedRegionParam() &&
11301 DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
Rachel Craik1cf49e42017-09-19 21:04:23 +000011302 ValueDecl* VD = DSAStack->getParentLoopControlVariable(
11303 DepCounter.getZExtValue());
11304 if (VD) {
11305 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
11306 << 1 << VD;
11307 } else {
11308 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0;
11309 }
Alexey Bataev8b427062016-05-25 12:36:08 +000011310 continue;
11311 }
11312 OpsOffs.push_back({RHS, OOK});
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011313 } else {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011314 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011315 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
Alexey Bataev31300ed2016-02-04 11:27:03 +000011316 (ASE &&
11317 !ASE->getBase()
11318 ->getType()
11319 .getNonReferenceType()
11320 ->isPointerType() &&
11321 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev463a9fe2017-07-27 19:15:30 +000011322 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
11323 << RefExpr->getSourceRange();
11324 continue;
11325 }
11326 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
11327 getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevd070a582017-10-25 15:54:04 +000011328 ExprResult Res = CreateBuiltinUnaryOp(ELoc, UO_AddrOf,
Alexey Bataev463a9fe2017-07-27 19:15:30 +000011329 RefExpr->IgnoreParenImpCasts());
11330 getDiagnostics().setSuppressAllDiagnostics(Suppress);
11331 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr)) {
11332 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
11333 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011334 continue;
11335 }
11336 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011337 Vars.push_back(RefExpr->IgnoreParenImpCasts());
11338 }
11339
11340 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
11341 TotalDepCount > VarList.size() &&
Rachel Craik1cf49e42017-09-19 21:04:23 +000011342 DSAStack->getParentOrderedRegionParam() &&
11343 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) {
11344 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration) << 1
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011345 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
11346 }
11347 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
11348 Vars.empty())
11349 return nullptr;
11350 }
Alexey Bataev8b427062016-05-25 12:36:08 +000011351 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11352 DepKind, DepLoc, ColonLoc, Vars);
11353 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source)
11354 DSAStack->addDoacrossDependClause(C, OpsOffs);
11355 return C;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011356}
Michael Wonge710d542015-08-07 16:16:36 +000011357
11358OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
11359 SourceLocation LParenLoc,
11360 SourceLocation EndLoc) {
11361 Expr *ValExpr = Device;
Alexey Bataev931e19b2017-10-02 16:32:39 +000011362 Stmt *HelperValStmt = nullptr;
Michael Wonge710d542015-08-07 16:16:36 +000011363
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011364 // OpenMP [2.9.1, Restrictions]
11365 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000011366 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
11367 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011368 return nullptr;
11369
Alexey Bataev931e19b2017-10-02 16:32:39 +000011370 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000011371 OpenMPDirectiveKind CaptureRegion =
11372 getOpenMPCaptureRegionForClause(DKind, OMPC_device);
11373 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000011374 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataev931e19b2017-10-02 16:32:39 +000011375 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
11376 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
11377 HelperValStmt = buildPreInits(Context, Captures);
11378 }
11379
11380 return new (Context)
11381 OMPDeviceClause(ValExpr, HelperValStmt, StartLoc, LParenLoc, EndLoc);
Michael Wonge710d542015-08-07 16:16:36 +000011382}
Kelvin Li0bff7af2015-11-23 05:32:03 +000011383
Kelvin Li0bff7af2015-11-23 05:32:03 +000011384static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
11385 DSAStackTy *Stack, QualType QTy) {
11386 NamedDecl *ND;
11387 if (QTy->isIncompleteType(&ND)) {
11388 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
11389 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +000011390 }
11391 return true;
11392}
11393
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011394/// \brief Return true if it can be proven that the provided array expression
11395/// (array section or array subscript) does NOT specify the whole size of the
11396/// array whose base type is \a BaseQTy.
11397static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
11398 const Expr *E,
11399 QualType BaseQTy) {
11400 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
11401
11402 // If this is an array subscript, it refers to the whole size if the size of
11403 // the dimension is constant and equals 1. Also, an array section assumes the
11404 // format of an array subscript if no colon is used.
11405 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
11406 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
11407 return ATy->getSize().getSExtValue() != 1;
11408 // Size can't be evaluated statically.
11409 return false;
11410 }
11411
11412 assert(OASE && "Expecting array section if not an array subscript.");
11413 auto *LowerBound = OASE->getLowerBound();
11414 auto *Length = OASE->getLength();
11415
11416 // If there is a lower bound that does not evaluates to zero, we are not
David Majnemer9d168222016-08-05 17:44:54 +000011417 // covering the whole dimension.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011418 if (LowerBound) {
11419 llvm::APSInt ConstLowerBound;
11420 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
11421 return false; // Can't get the integer value as a constant.
11422 if (ConstLowerBound.getSExtValue())
11423 return true;
11424 }
11425
11426 // If we don't have a length we covering the whole dimension.
11427 if (!Length)
11428 return false;
11429
11430 // If the base is a pointer, we don't have a way to get the size of the
11431 // pointee.
11432 if (BaseQTy->isPointerType())
11433 return false;
11434
11435 // We can only check if the length is the same as the size of the dimension
11436 // if we have a constant array.
11437 auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
11438 if (!CATy)
11439 return false;
11440
11441 llvm::APSInt ConstLength;
11442 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
11443 return false; // Can't get the integer value as a constant.
11444
11445 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
11446}
11447
11448// Return true if it can be proven that the provided array expression (array
11449// section or array subscript) does NOT specify a single element of the array
11450// whose base type is \a BaseQTy.
11451static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
David Majnemer9d168222016-08-05 17:44:54 +000011452 const Expr *E,
11453 QualType BaseQTy) {
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011454 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
11455
11456 // An array subscript always refer to a single element. Also, an array section
11457 // assumes the format of an array subscript if no colon is used.
11458 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
11459 return false;
11460
11461 assert(OASE && "Expecting array section if not an array subscript.");
11462 auto *Length = OASE->getLength();
11463
11464 // If we don't have a length we have to check if the array has unitary size
11465 // for this dimension. Also, we should always expect a length if the base type
11466 // is pointer.
11467 if (!Length) {
11468 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
11469 return ATy->getSize().getSExtValue() != 1;
11470 // We cannot assume anything.
11471 return false;
11472 }
11473
11474 // Check if the length evaluates to 1.
11475 llvm::APSInt ConstLength;
11476 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
11477 return false; // Can't get the integer value as a constant.
11478
11479 return ConstLength.getSExtValue() != 1;
11480}
11481
Samuel Antao661c0902016-05-26 17:39:58 +000011482// Return the expression of the base of the mappable expression or null if it
11483// cannot be determined and do all the necessary checks to see if the expression
11484// is valid as a standalone mappable expression. In the process, record all the
Samuel Antao90927002016-04-26 14:54:23 +000011485// components of the expression.
11486static Expr *CheckMapClauseExpressionBase(
11487 Sema &SemaRef, Expr *E,
Samuel Antao661c0902016-05-26 17:39:58 +000011488 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
Alexey Bataevb7a9b742017-12-05 19:20:09 +000011489 OpenMPClauseKind CKind, bool NoDiagnose) {
Samuel Antao5de996e2016-01-22 20:21:36 +000011490 SourceLocation ELoc = E->getExprLoc();
11491 SourceRange ERange = E->getSourceRange();
11492
11493 // The base of elements of list in a map clause have to be either:
11494 // - a reference to variable or field.
11495 // - a member expression.
11496 // - an array expression.
11497 //
11498 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
11499 // reference to 'r'.
11500 //
11501 // If we have:
11502 //
11503 // struct SS {
11504 // Bla S;
11505 // foo() {
11506 // #pragma omp target map (S.Arr[:12]);
11507 // }
11508 // }
11509 //
11510 // We want to retrieve the member expression 'this->S';
11511
11512 Expr *RelevantExpr = nullptr;
11513
Samuel Antao5de996e2016-01-22 20:21:36 +000011514 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
11515 // If a list item is an array section, it must specify contiguous storage.
11516 //
11517 // For this restriction it is sufficient that we make sure only references
11518 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011519 // exist except in the rightmost expression (unless they cover the whole
11520 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +000011521 //
11522 // r.ArrS[3:5].Arr[6:7]
11523 //
11524 // r.ArrS[3:5].x
11525 //
11526 // but these would be valid:
11527 // r.ArrS[3].Arr[6:7]
11528 //
11529 // r.ArrS[3].x
11530
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011531 bool AllowUnitySizeArraySection = true;
11532 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +000011533
Dmitry Polukhin644a9252016-03-11 07:58:34 +000011534 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +000011535 E = E->IgnoreParenImpCasts();
11536
11537 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
11538 if (!isa<VarDecl>(CurE->getDecl()))
Alexey Bataev27041fa2017-12-05 15:22:49 +000011539 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000011540
11541 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011542
11543 // If we got a reference to a declaration, we should not expect any array
11544 // section before that.
11545 AllowUnitySizeArraySection = false;
11546 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000011547
11548 // Record the component.
Alexey Bataev27041fa2017-12-05 15:22:49 +000011549 CurComponents.emplace_back(CurE, CurE->getDecl());
11550 } else if (auto *CurE = dyn_cast<MemberExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +000011551 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
11552
11553 if (isa<CXXThisExpr>(BaseE))
11554 // We found a base expression: this->Val.
11555 RelevantExpr = CurE;
11556 else
11557 E = BaseE;
11558
11559 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000011560 if (!NoDiagnose) {
11561 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
11562 << CurE->getSourceRange();
11563 return nullptr;
11564 }
11565 if (RelevantExpr)
11566 return nullptr;
11567 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000011568 }
11569
11570 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
11571
11572 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
11573 // A bit-field cannot appear in a map clause.
11574 //
11575 if (FD->isBitField()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000011576 if (!NoDiagnose) {
11577 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
11578 << CurE->getSourceRange() << getOpenMPClauseName(CKind);
11579 return nullptr;
11580 }
11581 if (RelevantExpr)
11582 return nullptr;
11583 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000011584 }
11585
11586 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
11587 // If the type of a list item is a reference to a type T then the type
11588 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011589 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000011590
11591 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
11592 // A list item cannot be a variable that is a member of a structure with
11593 // a union type.
11594 //
Alexey Bataevb7a9b742017-12-05 19:20:09 +000011595 if (auto *RT = CurType->getAs<RecordType>()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000011596 if (RT->isUnionType()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000011597 if (!NoDiagnose) {
11598 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
11599 << CurE->getSourceRange();
11600 return nullptr;
11601 }
11602 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000011603 }
Alexey Bataevb7a9b742017-12-05 19:20:09 +000011604 }
Samuel Antao5de996e2016-01-22 20:21:36 +000011605
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011606 // If we got a member expression, we should not expect any array section
11607 // before that:
11608 //
11609 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
11610 // If a list item is an element of a structure, only the rightmost symbol
11611 // of the variable reference can be an array section.
11612 //
11613 AllowUnitySizeArraySection = false;
11614 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000011615
11616 // Record the component.
Alexey Bataev27041fa2017-12-05 15:22:49 +000011617 CurComponents.emplace_back(CurE, FD);
11618 } else if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +000011619 E = CurE->getBase()->IgnoreParenImpCasts();
11620
11621 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000011622 if (!NoDiagnose) {
11623 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
11624 << 0 << CurE->getSourceRange();
11625 return nullptr;
11626 }
11627 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000011628 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011629
11630 // If we got an array subscript that express the whole dimension we
11631 // can have any array expressions before. If it only expressing part of
11632 // the dimension, we can only have unitary-size array expressions.
11633 if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
11634 E->getType()))
11635 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000011636
11637 // Record the component - we don't have any declaration associated.
Alexey Bataev27041fa2017-12-05 15:22:49 +000011638 CurComponents.emplace_back(CurE, nullptr);
11639 } else if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000011640 assert(!NoDiagnose && "Array sections cannot be implicitly mapped.");
Samuel Antao5de996e2016-01-22 20:21:36 +000011641 E = CurE->getBase()->IgnoreParenImpCasts();
11642
Alexey Bataev27041fa2017-12-05 15:22:49 +000011643 QualType CurType =
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011644 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
11645
Samuel Antao5de996e2016-01-22 20:21:36 +000011646 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
11647 // If the type of a list item is a reference to a type T then the type
11648 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +000011649 if (CurType->isReferenceType())
11650 CurType = CurType->getPointeeType();
11651
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011652 bool IsPointer = CurType->isAnyPointerType();
11653
11654 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000011655 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
11656 << 0 << CurE->getSourceRange();
Alexey Bataev27041fa2017-12-05 15:22:49 +000011657 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000011658 }
11659
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011660 bool NotWhole =
11661 CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
11662 bool NotUnity =
11663 CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
11664
Samuel Antaodab51bb2016-07-18 23:22:11 +000011665 if (AllowWholeSizeArraySection) {
11666 // Any array section is currently allowed. Allowing a whole size array
11667 // section implies allowing a unity array section as well.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011668 //
11669 // If this array section refers to the whole dimension we can still
11670 // accept other array sections before this one, except if the base is a
11671 // pointer. Otherwise, only unitary sections are accepted.
11672 if (NotWhole || IsPointer)
11673 AllowWholeSizeArraySection = false;
Samuel Antaodab51bb2016-07-18 23:22:11 +000011674 } else if (AllowUnitySizeArraySection && NotUnity) {
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011675 // A unity or whole array section is not allowed and that is not
11676 // compatible with the properties of the current array section.
11677 SemaRef.Diag(
11678 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
11679 << CurE->getSourceRange();
Alexey Bataev27041fa2017-12-05 15:22:49 +000011680 return nullptr;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011681 }
Samuel Antao90927002016-04-26 14:54:23 +000011682
11683 // Record the component - we don't have any declaration associated.
Alexey Bataev27041fa2017-12-05 15:22:49 +000011684 CurComponents.emplace_back(CurE, nullptr);
11685 } else {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000011686 if (!NoDiagnose) {
11687 // If nothing else worked, this is not a valid map clause expression.
11688 SemaRef.Diag(
11689 ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
11690 << ERange;
11691 }
Alexey Bataev27041fa2017-12-05 15:22:49 +000011692 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000011693 }
Samuel Antao5de996e2016-01-22 20:21:36 +000011694 }
11695
11696 return RelevantExpr;
11697}
11698
11699// Return true if expression E associated with value VD has conflicts with other
11700// map information.
Samuel Antao90927002016-04-26 14:54:23 +000011701static bool CheckMapConflicts(
11702 Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD, Expr *E,
11703 bool CurrentRegionOnly,
Samuel Antao661c0902016-05-26 17:39:58 +000011704 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
11705 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000011706 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +000011707 SourceLocation ELoc = E->getExprLoc();
11708 SourceRange ERange = E->getSourceRange();
11709
11710 // In order to easily check the conflicts we need to match each component of
11711 // the expression under test with the components of the expressions that are
11712 // already in the stack.
11713
Samuel Antao5de996e2016-01-22 20:21:36 +000011714 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000011715 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000011716 "Map clause expression with unexpected base!");
11717
11718 // Variables to help detecting enclosing problems in data environment nests.
11719 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +000011720 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000011721
Samuel Antao90927002016-04-26 14:54:23 +000011722 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
11723 VD, CurrentRegionOnly,
11724 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
Samuel Antao6890b092016-07-28 14:25:09 +000011725 StackComponents,
11726 OpenMPClauseKind) -> bool {
Samuel Antao90927002016-04-26 14:54:23 +000011727
Samuel Antao5de996e2016-01-22 20:21:36 +000011728 assert(!StackComponents.empty() &&
11729 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000011730 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000011731 "Map clause expression with unexpected base!");
11732
Samuel Antao90927002016-04-26 14:54:23 +000011733 // The whole expression in the stack.
11734 auto *RE = StackComponents.front().getAssociatedExpression();
11735
Samuel Antao5de996e2016-01-22 20:21:36 +000011736 // Expressions must start from the same base. Here we detect at which
11737 // point both expressions diverge from each other and see if we can
11738 // detect if the memory referred to both expressions is contiguous and
11739 // do not overlap.
11740 auto CI = CurComponents.rbegin();
11741 auto CE = CurComponents.rend();
11742 auto SI = StackComponents.rbegin();
11743 auto SE = StackComponents.rend();
11744 for (; CI != CE && SI != SE; ++CI, ++SI) {
11745
11746 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
11747 // At most one list item can be an array item derived from a given
11748 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +000011749 if (CurrentRegionOnly &&
11750 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
11751 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
11752 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
11753 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
11754 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +000011755 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +000011756 << CI->getAssociatedExpression()->getSourceRange();
11757 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
11758 diag::note_used_here)
11759 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +000011760 return true;
11761 }
11762
11763 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +000011764 if (CI->getAssociatedExpression()->getStmtClass() !=
11765 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +000011766 break;
11767
11768 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +000011769 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +000011770 break;
11771 }
Kelvin Li9f645ae2016-07-18 22:49:16 +000011772 // Check if the extra components of the expressions in the enclosing
11773 // data environment are redundant for the current base declaration.
11774 // If they are, the maps completely overlap, which is legal.
11775 for (; SI != SE; ++SI) {
11776 QualType Type;
11777 if (auto *ASE =
David Majnemer9d168222016-08-05 17:44:54 +000011778 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
Kelvin Li9f645ae2016-07-18 22:49:16 +000011779 Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
David Majnemer9d168222016-08-05 17:44:54 +000011780 } else if (auto *OASE = dyn_cast<OMPArraySectionExpr>(
11781 SI->getAssociatedExpression())) {
Kelvin Li9f645ae2016-07-18 22:49:16 +000011782 auto *E = OASE->getBase()->IgnoreParenImpCasts();
11783 Type =
11784 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
11785 }
11786 if (Type.isNull() || Type->isAnyPointerType() ||
11787 CheckArrayExpressionDoesNotReferToWholeSize(
11788 SemaRef, SI->getAssociatedExpression(), Type))
11789 break;
11790 }
Samuel Antao5de996e2016-01-22 20:21:36 +000011791
11792 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
11793 // List items of map clauses in the same construct must not share
11794 // original storage.
11795 //
11796 // If the expressions are exactly the same or one is a subset of the
11797 // other, it means they are sharing storage.
11798 if (CI == CE && SI == SE) {
11799 if (CurrentRegionOnly) {
Samuel Antao661c0902016-05-26 17:39:58 +000011800 if (CKind == OMPC_map)
11801 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
11802 else {
Samuel Antaoec172c62016-05-26 17:49:04 +000011803 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000011804 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
11805 << ERange;
11806 }
Samuel Antao5de996e2016-01-22 20:21:36 +000011807 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
11808 << RE->getSourceRange();
11809 return true;
11810 } else {
11811 // If we find the same expression in the enclosing data environment,
11812 // that is legal.
11813 IsEnclosedByDataEnvironmentExpr = true;
11814 return false;
11815 }
11816 }
11817
Samuel Antao90927002016-04-26 14:54:23 +000011818 QualType DerivedType =
11819 std::prev(CI)->getAssociatedDeclaration()->getType();
11820 SourceLocation DerivedLoc =
11821 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000011822
11823 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
11824 // If the type of a list item is a reference to a type T then the type
11825 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000011826 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000011827
11828 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
11829 // A variable for which the type is pointer and an array section
11830 // derived from that variable must not appear as list items of map
11831 // clauses of the same construct.
11832 //
11833 // Also, cover one of the cases in:
11834 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
11835 // If any part of the original storage of a list item has corresponding
11836 // storage in the device data environment, all of the original storage
11837 // must have corresponding storage in the device data environment.
11838 //
11839 if (DerivedType->isAnyPointerType()) {
11840 if (CI == CE || SI == SE) {
11841 SemaRef.Diag(
11842 DerivedLoc,
11843 diag::err_omp_pointer_mapped_along_with_derived_section)
11844 << DerivedLoc;
11845 } else {
11846 assert(CI != CE && SI != SE);
11847 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
11848 << DerivedLoc;
11849 }
11850 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
11851 << RE->getSourceRange();
11852 return true;
11853 }
11854
11855 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
11856 // List items of map clauses in the same construct must not share
11857 // original storage.
11858 //
11859 // An expression is a subset of the other.
11860 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
Samuel Antao661c0902016-05-26 17:39:58 +000011861 if (CKind == OMPC_map)
11862 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
11863 else {
Samuel Antaoec172c62016-05-26 17:49:04 +000011864 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000011865 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
11866 << ERange;
11867 }
Samuel Antao5de996e2016-01-22 20:21:36 +000011868 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
11869 << RE->getSourceRange();
11870 return true;
11871 }
11872
11873 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000011874 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000011875 if (!CurrentRegionOnly && SI != SE)
11876 EnclosingExpr = RE;
11877
11878 // The current expression is a subset of the expression in the data
11879 // environment.
11880 IsEnclosedByDataEnvironmentExpr |=
11881 (!CurrentRegionOnly && CI != CE && SI == SE);
11882
11883 return false;
11884 });
11885
11886 if (CurrentRegionOnly)
11887 return FoundError;
11888
11889 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
11890 // If any part of the original storage of a list item has corresponding
11891 // storage in the device data environment, all of the original storage must
11892 // have corresponding storage in the device data environment.
11893 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
11894 // If a list item is an element of a structure, and a different element of
11895 // the structure has a corresponding list item in the device data environment
11896 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000011897 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000011898 // data environment prior to the task encountering the construct.
11899 //
11900 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
11901 SemaRef.Diag(ELoc,
11902 diag::err_omp_original_storage_is_shared_and_does_not_contain)
11903 << ERange;
11904 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
11905 << EnclosingExpr->getSourceRange();
11906 return true;
11907 }
11908
11909 return FoundError;
11910}
11911
Samuel Antao661c0902016-05-26 17:39:58 +000011912namespace {
11913// Utility struct that gathers all the related lists associated with a mappable
11914// expression.
11915struct MappableVarListInfo final {
11916 // The list of expressions.
11917 ArrayRef<Expr *> VarList;
11918 // The list of processed expressions.
11919 SmallVector<Expr *, 16> ProcessedVarList;
11920 // The mappble components for each expression.
11921 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
11922 // The base declaration of the variable.
11923 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
11924
11925 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
11926 // We have a list of components and base declarations for each entry in the
11927 // variable list.
11928 VarComponents.reserve(VarList.size());
11929 VarBaseDeclarations.reserve(VarList.size());
11930 }
11931};
11932}
11933
11934// Check the validity of the provided variable list for the provided clause kind
11935// \a CKind. In the check process the valid expressions, and mappable expression
11936// components and variables are extracted and used to fill \a Vars,
11937// \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and
11938// \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'.
11939static void
11940checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS,
11941 OpenMPClauseKind CKind, MappableVarListInfo &MVLI,
11942 SourceLocation StartLoc,
11943 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
11944 bool IsMapTypeImplicit = false) {
Samuel Antaoec172c62016-05-26 17:49:04 +000011945 // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
11946 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
Samuel Antao661c0902016-05-26 17:39:58 +000011947 "Unexpected clause kind with mappable expressions!");
Kelvin Li0bff7af2015-11-23 05:32:03 +000011948
Samuel Antao90927002016-04-26 14:54:23 +000011949 // Keep track of the mappable components and base declarations in this clause.
11950 // Each entry in the list is going to have a list of components associated. We
11951 // record each set of the components so that we can build the clause later on.
11952 // In the end we should have the same amount of declarations and component
11953 // lists.
Samuel Antao90927002016-04-26 14:54:23 +000011954
Samuel Antao661c0902016-05-26 17:39:58 +000011955 for (auto &RE : MVLI.VarList) {
Samuel Antaoec172c62016-05-26 17:49:04 +000011956 assert(RE && "Null expr in omp to/from/map clause");
Kelvin Li0bff7af2015-11-23 05:32:03 +000011957 SourceLocation ELoc = RE->getExprLoc();
11958
Kelvin Li0bff7af2015-11-23 05:32:03 +000011959 auto *VE = RE->IgnoreParenLValueCasts();
11960
11961 if (VE->isValueDependent() || VE->isTypeDependent() ||
11962 VE->isInstantiationDependent() ||
11963 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000011964 // We can only analyze this information once the missing information is
11965 // resolved.
Samuel Antao661c0902016-05-26 17:39:58 +000011966 MVLI.ProcessedVarList.push_back(RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011967 continue;
11968 }
11969
11970 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000011971
Samuel Antao5de996e2016-01-22 20:21:36 +000011972 if (!RE->IgnoreParenImpCasts()->isLValue()) {
Samuel Antao661c0902016-05-26 17:39:58 +000011973 SemaRef.Diag(ELoc,
11974 diag::err_omp_expected_named_var_member_or_array_expression)
Samuel Antao5de996e2016-01-22 20:21:36 +000011975 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000011976 continue;
11977 }
11978
Samuel Antao90927002016-04-26 14:54:23 +000011979 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
11980 ValueDecl *CurDeclaration = nullptr;
11981
11982 // Obtain the array or member expression bases if required. Also, fill the
11983 // components array with all the components identified in the process.
Alexey Bataevb7a9b742017-12-05 19:20:09 +000011984 auto *BE = CheckMapClauseExpressionBase(SemaRef, SimpleExpr, CurComponents,
11985 CKind, /*NoDiagnose=*/false);
Samuel Antao5de996e2016-01-22 20:21:36 +000011986 if (!BE)
11987 continue;
11988
Samuel Antao90927002016-04-26 14:54:23 +000011989 assert(!CurComponents.empty() &&
11990 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000011991
Samuel Antao90927002016-04-26 14:54:23 +000011992 // For the following checks, we rely on the base declaration which is
11993 // expected to be associated with the last component. The declaration is
11994 // expected to be a variable or a field (if 'this' is being mapped).
11995 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
11996 assert(CurDeclaration && "Null decl on map clause.");
11997 assert(
11998 CurDeclaration->isCanonicalDecl() &&
11999 "Expecting components to have associated only canonical declarations.");
12000
12001 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
12002 auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000012003
12004 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000012005 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000012006
12007 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
Samuel Antao661c0902016-05-26 17:39:58 +000012008 // threadprivate variables cannot appear in a map clause.
12009 // OpenMP 4.5 [2.10.5, target update Construct]
12010 // threadprivate variables cannot appear in a from clause.
12011 if (VD && DSAS->isThreadPrivate(VD)) {
12012 auto DVar = DSAS->getTopDSA(VD, false);
12013 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
12014 << getOpenMPClauseName(CKind);
12015 ReportOriginalDSA(SemaRef, DSAS, VD, DVar);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012016 continue;
12017 }
12018
Samuel Antao5de996e2016-01-22 20:21:36 +000012019 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
12020 // A list item cannot appear in both a map clause and a data-sharing
12021 // attribute clause on the same construct.
Kelvin Li0bff7af2015-11-23 05:32:03 +000012022
Samuel Antao5de996e2016-01-22 20:21:36 +000012023 // Check conflicts with other map clause expressions. We check the conflicts
12024 // with the current construct separately from the enclosing data
Samuel Antao661c0902016-05-26 17:39:58 +000012025 // environment, because the restrictions are different. We only have to
12026 // check conflicts across regions for the map clauses.
12027 if (CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
12028 /*CurrentRegionOnly=*/true, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000012029 break;
Samuel Antao661c0902016-05-26 17:39:58 +000012030 if (CKind == OMPC_map &&
12031 CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
12032 /*CurrentRegionOnly=*/false, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000012033 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000012034
Samuel Antao661c0902016-05-26 17:39:58 +000012035 // OpenMP 4.5 [2.10.5, target update Construct]
Samuel Antao5de996e2016-01-22 20:21:36 +000012036 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12037 // If the type of a list item is a reference to a type T then the type will
12038 // be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000012039 QualType Type = CurDeclaration->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000012040
Samuel Antao661c0902016-05-26 17:39:58 +000012041 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
12042 // A list item in a to or from clause must have a mappable type.
Samuel Antao5de996e2016-01-22 20:21:36 +000012043 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000012044 // A list item must have a mappable type.
Samuel Antao661c0902016-05-26 17:39:58 +000012045 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
12046 DSAS, Type))
Kelvin Li0bff7af2015-11-23 05:32:03 +000012047 continue;
12048
Samuel Antao661c0902016-05-26 17:39:58 +000012049 if (CKind == OMPC_map) {
12050 // target enter data
12051 // OpenMP [2.10.2, Restrictions, p. 99]
12052 // A map-type must be specified in all map clauses and must be either
12053 // to or alloc.
12054 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
12055 if (DKind == OMPD_target_enter_data &&
12056 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
12057 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
12058 << (IsMapTypeImplicit ? 1 : 0)
12059 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
12060 << getOpenMPDirectiveName(DKind);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000012061 continue;
12062 }
Samuel Antao661c0902016-05-26 17:39:58 +000012063
12064 // target exit_data
12065 // OpenMP [2.10.3, Restrictions, p. 102]
12066 // A map-type must be specified in all map clauses and must be either
12067 // from, release, or delete.
12068 if (DKind == OMPD_target_exit_data &&
12069 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
12070 MapType == OMPC_MAP_delete)) {
12071 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
12072 << (IsMapTypeImplicit ? 1 : 0)
12073 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
12074 << getOpenMPDirectiveName(DKind);
12075 continue;
12076 }
12077
12078 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
12079 // A list item cannot appear in both a map clause and a data-sharing
12080 // attribute clause on the same construct
Kelvin Li83c451e2016-12-25 04:52:54 +000012081 if ((DKind == OMPD_target || DKind == OMPD_target_teams ||
Kelvin Li80e8f562016-12-29 22:16:30 +000012082 DKind == OMPD_target_teams_distribute ||
Kelvin Li1851df52017-01-03 05:23:48 +000012083 DKind == OMPD_target_teams_distribute_parallel_for ||
Kelvin Lida681182017-01-10 18:08:18 +000012084 DKind == OMPD_target_teams_distribute_parallel_for_simd ||
12085 DKind == OMPD_target_teams_distribute_simd) && VD) {
Samuel Antao661c0902016-05-26 17:39:58 +000012086 auto DVar = DSAS->getTopDSA(VD, false);
12087 if (isOpenMPPrivate(DVar.CKind)) {
Samuel Antao6890b092016-07-28 14:25:09 +000012088 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Samuel Antao661c0902016-05-26 17:39:58 +000012089 << getOpenMPClauseName(DVar.CKind)
Samuel Antao6890b092016-07-28 14:25:09 +000012090 << getOpenMPClauseName(OMPC_map)
Samuel Antao661c0902016-05-26 17:39:58 +000012091 << getOpenMPDirectiveName(DSAS->getCurrentDirective());
12092 ReportOriginalDSA(SemaRef, DSAS, CurDeclaration, DVar);
12093 continue;
12094 }
12095 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +000012096 }
12097
Samuel Antao90927002016-04-26 14:54:23 +000012098 // Save the current expression.
Samuel Antao661c0902016-05-26 17:39:58 +000012099 MVLI.ProcessedVarList.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000012100
12101 // Store the components in the stack so that they can be used to check
12102 // against other clauses later on.
Samuel Antao6890b092016-07-28 14:25:09 +000012103 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
12104 /*WhereFoundClauseKind=*/OMPC_map);
Samuel Antao90927002016-04-26 14:54:23 +000012105
12106 // Save the components and declaration to create the clause. For purposes of
12107 // the clause creation, any component list that has has base 'this' uses
Samuel Antao686c70c2016-05-26 17:30:50 +000012108 // null as base declaration.
Samuel Antao661c0902016-05-26 17:39:58 +000012109 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
12110 MVLI.VarComponents.back().append(CurComponents.begin(),
12111 CurComponents.end());
12112 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
12113 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012114 }
Samuel Antao661c0902016-05-26 17:39:58 +000012115}
12116
12117OMPClause *
12118Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
12119 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
12120 SourceLocation MapLoc, SourceLocation ColonLoc,
12121 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
12122 SourceLocation LParenLoc, SourceLocation EndLoc) {
12123 MappableVarListInfo MVLI(VarList);
12124 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc,
12125 MapType, IsMapTypeImplicit);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012126
Samuel Antao5de996e2016-01-22 20:21:36 +000012127 // We need to produce a map clause even if we don't have variables so that
12128 // other diagnostics related with non-existing map clauses are accurate.
Samuel Antao661c0902016-05-26 17:39:58 +000012129 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc,
12130 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
12131 MVLI.VarComponents, MapTypeModifier, MapType,
12132 IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012133}
Kelvin Li099bb8c2015-11-24 20:50:12 +000012134
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012135QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
12136 TypeResult ParsedType) {
12137 assert(ParsedType.isUsable());
12138
12139 QualType ReductionType = GetTypeFromParser(ParsedType.get());
12140 if (ReductionType.isNull())
12141 return QualType();
12142
12143 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
12144 // A type name in a declare reduction directive cannot be a function type, an
12145 // array type, a reference type, or a type qualified with const, volatile or
12146 // restrict.
12147 if (ReductionType.hasQualifiers()) {
12148 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
12149 return QualType();
12150 }
12151
12152 if (ReductionType->isFunctionType()) {
12153 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
12154 return QualType();
12155 }
12156 if (ReductionType->isReferenceType()) {
12157 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
12158 return QualType();
12159 }
12160 if (ReductionType->isArrayType()) {
12161 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
12162 return QualType();
12163 }
12164 return ReductionType;
12165}
12166
12167Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
12168 Scope *S, DeclContext *DC, DeclarationName Name,
12169 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
12170 AccessSpecifier AS, Decl *PrevDeclInScope) {
12171 SmallVector<Decl *, 8> Decls;
12172 Decls.reserve(ReductionTypes.size());
12173
12174 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
Richard Smithbecb92d2017-10-10 22:33:17 +000012175 forRedeclarationInCurContext());
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012176 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
12177 // A reduction-identifier may not be re-declared in the current scope for the
12178 // same type or for a type that is compatible according to the base language
12179 // rules.
12180 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
12181 OMPDeclareReductionDecl *PrevDRD = nullptr;
12182 bool InCompoundScope = true;
12183 if (S != nullptr) {
12184 // Find previous declaration with the same name not referenced in other
12185 // declarations.
12186 FunctionScopeInfo *ParentFn = getEnclosingFunction();
12187 InCompoundScope =
12188 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
12189 LookupName(Lookup, S);
12190 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
12191 /*AllowInlineNamespace=*/false);
12192 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
12193 auto Filter = Lookup.makeFilter();
12194 while (Filter.hasNext()) {
12195 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
12196 if (InCompoundScope) {
12197 auto I = UsedAsPrevious.find(PrevDecl);
12198 if (I == UsedAsPrevious.end())
12199 UsedAsPrevious[PrevDecl] = false;
12200 if (auto *D = PrevDecl->getPrevDeclInScope())
12201 UsedAsPrevious[D] = true;
12202 }
12203 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
12204 PrevDecl->getLocation();
12205 }
12206 Filter.done();
12207 if (InCompoundScope) {
12208 for (auto &PrevData : UsedAsPrevious) {
12209 if (!PrevData.second) {
12210 PrevDRD = PrevData.first;
12211 break;
12212 }
12213 }
12214 }
12215 } else if (PrevDeclInScope != nullptr) {
12216 auto *PrevDRDInScope = PrevDRD =
12217 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
12218 do {
12219 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
12220 PrevDRDInScope->getLocation();
12221 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
12222 } while (PrevDRDInScope != nullptr);
12223 }
12224 for (auto &TyData : ReductionTypes) {
12225 auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
12226 bool Invalid = false;
12227 if (I != PreviousRedeclTypes.end()) {
12228 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
12229 << TyData.first;
12230 Diag(I->second, diag::note_previous_definition);
12231 Invalid = true;
12232 }
12233 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
12234 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
12235 Name, TyData.first, PrevDRD);
12236 DC->addDecl(DRD);
12237 DRD->setAccess(AS);
12238 Decls.push_back(DRD);
12239 if (Invalid)
12240 DRD->setInvalidDecl();
12241 else
12242 PrevDRD = DRD;
12243 }
12244
12245 return DeclGroupPtrTy::make(
12246 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
12247}
12248
12249void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
12250 auto *DRD = cast<OMPDeclareReductionDecl>(D);
12251
12252 // Enter new function scope.
12253 PushFunctionScope();
12254 getCurFunction()->setHasBranchProtectedScope();
12255 getCurFunction()->setHasOMPDeclareReductionCombiner();
12256
12257 if (S != nullptr)
12258 PushDeclContext(S, DRD);
12259 else
12260 CurContext = DRD;
12261
Faisal Valid143a0c2017-04-01 21:30:49 +000012262 PushExpressionEvaluationContext(
12263 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012264
12265 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012266 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
12267 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
12268 // uses semantics of argument handles by value, but it should be passed by
12269 // reference. C lang does not support references, so pass all parameters as
12270 // pointers.
12271 // Create 'T omp_in;' variable.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012272 auto *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012273 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012274 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
12275 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
12276 // uses semantics of argument handles by value, but it should be passed by
12277 // reference. C lang does not support references, so pass all parameters as
12278 // pointers.
12279 // Create 'T omp_out;' variable.
12280 auto *OmpOutParm =
12281 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
12282 if (S != nullptr) {
12283 PushOnScopeChains(OmpInParm, S);
12284 PushOnScopeChains(OmpOutParm, S);
12285 } else {
12286 DRD->addDecl(OmpInParm);
12287 DRD->addDecl(OmpOutParm);
12288 }
12289}
12290
12291void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
12292 auto *DRD = cast<OMPDeclareReductionDecl>(D);
12293 DiscardCleanupsInEvaluationContext();
12294 PopExpressionEvaluationContext();
12295
12296 PopDeclContext();
12297 PopFunctionScopeInfo();
12298
12299 if (Combiner != nullptr)
12300 DRD->setCombiner(Combiner);
12301 else
12302 DRD->setInvalidDecl();
12303}
12304
Alexey Bataev070f43a2017-09-06 14:49:58 +000012305VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012306 auto *DRD = cast<OMPDeclareReductionDecl>(D);
12307
12308 // Enter new function scope.
12309 PushFunctionScope();
12310 getCurFunction()->setHasBranchProtectedScope();
12311
12312 if (S != nullptr)
12313 PushDeclContext(S, DRD);
12314 else
12315 CurContext = DRD;
12316
Faisal Valid143a0c2017-04-01 21:30:49 +000012317 PushExpressionEvaluationContext(
12318 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012319
12320 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012321 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
12322 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
12323 // uses semantics of argument handles by value, but it should be passed by
12324 // reference. C lang does not support references, so pass all parameters as
12325 // pointers.
12326 // Create 'T omp_priv;' variable.
12327 auto *OmpPrivParm =
12328 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012329 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
12330 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
12331 // uses semantics of argument handles by value, but it should be passed by
12332 // reference. C lang does not support references, so pass all parameters as
12333 // pointers.
12334 // Create 'T omp_orig;' variable.
12335 auto *OmpOrigParm =
12336 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012337 if (S != nullptr) {
12338 PushOnScopeChains(OmpPrivParm, S);
12339 PushOnScopeChains(OmpOrigParm, S);
12340 } else {
12341 DRD->addDecl(OmpPrivParm);
12342 DRD->addDecl(OmpOrigParm);
12343 }
Alexey Bataev070f43a2017-09-06 14:49:58 +000012344 return OmpPrivParm;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012345}
12346
Alexey Bataev070f43a2017-09-06 14:49:58 +000012347void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer,
12348 VarDecl *OmpPrivParm) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012349 auto *DRD = cast<OMPDeclareReductionDecl>(D);
12350 DiscardCleanupsInEvaluationContext();
12351 PopExpressionEvaluationContext();
12352
12353 PopDeclContext();
12354 PopFunctionScopeInfo();
12355
Alexey Bataev070f43a2017-09-06 14:49:58 +000012356 if (Initializer != nullptr) {
12357 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit);
12358 } else if (OmpPrivParm->hasInit()) {
12359 DRD->setInitializer(OmpPrivParm->getInit(),
12360 OmpPrivParm->isDirectInit()
12361 ? OMPDeclareReductionDecl::DirectInit
12362 : OMPDeclareReductionDecl::CopyInit);
12363 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012364 DRD->setInvalidDecl();
Alexey Bataev070f43a2017-09-06 14:49:58 +000012365 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012366}
12367
12368Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
12369 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
12370 for (auto *D : DeclReductions.get()) {
12371 if (IsValid) {
12372 auto *DRD = cast<OMPDeclareReductionDecl>(D);
12373 if (S != nullptr)
12374 PushOnScopeChains(DRD, S, /*AddToContext=*/false);
12375 } else
12376 D->setInvalidDecl();
12377 }
12378 return DeclReductions;
12379}
12380
David Majnemer9d168222016-08-05 17:44:54 +000012381OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
Kelvin Li099bb8c2015-11-24 20:50:12 +000012382 SourceLocation StartLoc,
12383 SourceLocation LParenLoc,
12384 SourceLocation EndLoc) {
12385 Expr *ValExpr = NumTeams;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000012386 Stmt *HelperValStmt = nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000012387
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012388 // OpenMP [teams Constrcut, Restrictions]
12389 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000012390 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
12391 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012392 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000012393
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000012394 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000012395 OpenMPDirectiveKind CaptureRegion =
12396 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams);
12397 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000012398 ValExpr = MakeFullExpr(ValExpr).get();
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000012399 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
12400 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
12401 HelperValStmt = buildPreInits(Context, Captures);
12402 }
12403
12404 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion,
12405 StartLoc, LParenLoc, EndLoc);
Kelvin Li099bb8c2015-11-24 20:50:12 +000012406}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012407
12408OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
12409 SourceLocation StartLoc,
12410 SourceLocation LParenLoc,
12411 SourceLocation EndLoc) {
12412 Expr *ValExpr = ThreadLimit;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000012413 Stmt *HelperValStmt = nullptr;
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012414
12415 // OpenMP [teams Constrcut, Restrictions]
12416 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000012417 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
12418 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012419 return nullptr;
12420
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000012421 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000012422 OpenMPDirectiveKind CaptureRegion =
12423 getOpenMPCaptureRegionForClause(DKind, OMPC_thread_limit);
12424 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000012425 ValExpr = MakeFullExpr(ValExpr).get();
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000012426 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
12427 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
12428 HelperValStmt = buildPreInits(Context, Captures);
12429 }
12430
12431 return new (Context) OMPThreadLimitClause(
12432 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012433}
Alexey Bataeva0569352015-12-01 10:17:31 +000012434
12435OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
12436 SourceLocation StartLoc,
12437 SourceLocation LParenLoc,
12438 SourceLocation EndLoc) {
12439 Expr *ValExpr = Priority;
12440
12441 // OpenMP [2.9.1, task Constrcut]
12442 // The priority-value is a non-negative numerical scalar expression.
12443 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
12444 /*StrictlyPositive=*/false))
12445 return nullptr;
12446
12447 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
12448}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000012449
12450OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
12451 SourceLocation StartLoc,
12452 SourceLocation LParenLoc,
12453 SourceLocation EndLoc) {
12454 Expr *ValExpr = Grainsize;
12455
12456 // OpenMP [2.9.2, taskloop Constrcut]
12457 // The parameter of the grainsize clause must be a positive integer
12458 // expression.
12459 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
12460 /*StrictlyPositive=*/true))
12461 return nullptr;
12462
12463 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
12464}
Alexey Bataev382967a2015-12-08 12:06:20 +000012465
12466OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
12467 SourceLocation StartLoc,
12468 SourceLocation LParenLoc,
12469 SourceLocation EndLoc) {
12470 Expr *ValExpr = NumTasks;
12471
12472 // OpenMP [2.9.2, taskloop Constrcut]
12473 // The parameter of the num_tasks clause must be a positive integer
12474 // expression.
12475 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
12476 /*StrictlyPositive=*/true))
12477 return nullptr;
12478
12479 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
12480}
12481
Alexey Bataev28c75412015-12-15 08:19:24 +000012482OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
12483 SourceLocation LParenLoc,
12484 SourceLocation EndLoc) {
12485 // OpenMP [2.13.2, critical construct, Description]
12486 // ... where hint-expression is an integer constant expression that evaluates
12487 // to a valid lock hint.
12488 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
12489 if (HintExpr.isInvalid())
12490 return nullptr;
12491 return new (Context)
12492 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
12493}
12494
Carlo Bertollib4adf552016-01-15 18:50:31 +000012495OMPClause *Sema::ActOnOpenMPDistScheduleClause(
12496 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
12497 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
12498 SourceLocation EndLoc) {
12499 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
12500 std::string Values;
12501 Values += "'";
12502 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
12503 Values += "'";
12504 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
12505 << Values << getOpenMPClauseName(OMPC_dist_schedule);
12506 return nullptr;
12507 }
12508 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000012509 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000012510 if (ChunkSize) {
12511 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
12512 !ChunkSize->isInstantiationDependent() &&
12513 !ChunkSize->containsUnexpandedParameterPack()) {
12514 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
12515 ExprResult Val =
12516 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
12517 if (Val.isInvalid())
12518 return nullptr;
12519
12520 ValExpr = Val.get();
12521
12522 // OpenMP [2.7.1, Restrictions]
12523 // chunk_size must be a loop invariant integer expression with a positive
12524 // value.
12525 llvm::APSInt Result;
12526 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
12527 if (Result.isSigned() && !Result.isStrictlyPositive()) {
12528 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
12529 << "dist_schedule" << ChunkSize->getSourceRange();
12530 return nullptr;
12531 }
Alexey Bataev2ba67042017-11-28 21:11:44 +000012532 } else if (getOpenMPCaptureRegionForClause(
12533 DSAStack->getCurrentDirective(), OMPC_dist_schedule) !=
12534 OMPD_unknown &&
Alexey Bataevb46cdea2016-06-15 11:20:48 +000012535 !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000012536 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +000012537 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
12538 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
12539 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000012540 }
12541 }
12542 }
12543
12544 return new (Context)
12545 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000012546 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000012547}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000012548
12549OMPClause *Sema::ActOnOpenMPDefaultmapClause(
12550 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
12551 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
12552 SourceLocation KindLoc, SourceLocation EndLoc) {
12553 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
David Majnemer9d168222016-08-05 17:44:54 +000012554 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) {
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000012555 std::string Value;
12556 SourceLocation Loc;
12557 Value += "'";
12558 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
12559 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000012560 OMPC_DEFAULTMAP_MODIFIER_tofrom);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000012561 Loc = MLoc;
12562 } else {
12563 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000012564 OMPC_DEFAULTMAP_scalar);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000012565 Loc = KindLoc;
12566 }
12567 Value += "'";
12568 Diag(Loc, diag::err_omp_unexpected_clause_value)
12569 << Value << getOpenMPClauseName(OMPC_defaultmap);
12570 return nullptr;
12571 }
Alexey Bataev2fd0cb22017-10-05 17:51:39 +000012572 DSAStack->setDefaultDMAToFromScalar(StartLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000012573
12574 return new (Context)
12575 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
12576}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012577
12578bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
12579 DeclContext *CurLexicalContext = getCurLexicalContext();
12580 if (!CurLexicalContext->isFileContext() &&
12581 !CurLexicalContext->isExternCContext() &&
Alexey Bataev502ec492017-10-03 20:00:00 +000012582 !CurLexicalContext->isExternCXXContext() &&
12583 !isa<CXXRecordDecl>(CurLexicalContext) &&
12584 !isa<ClassTemplateDecl>(CurLexicalContext) &&
12585 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) &&
12586 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012587 Diag(Loc, diag::err_omp_region_not_file_context);
12588 return false;
12589 }
12590 if (IsInOpenMPDeclareTargetContext) {
12591 Diag(Loc, diag::err_omp_enclosed_declare_target);
12592 return false;
12593 }
12594
12595 IsInOpenMPDeclareTargetContext = true;
12596 return true;
12597}
12598
12599void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
12600 assert(IsInOpenMPDeclareTargetContext &&
12601 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
12602
12603 IsInOpenMPDeclareTargetContext = false;
12604}
12605
David Majnemer9d168222016-08-05 17:44:54 +000012606void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope,
12607 CXXScopeSpec &ScopeSpec,
12608 const DeclarationNameInfo &Id,
12609 OMPDeclareTargetDeclAttr::MapTypeTy MT,
12610 NamedDeclSetType &SameDirectiveDecls) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012611 LookupResult Lookup(*this, Id, LookupOrdinaryName);
12612 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
12613
12614 if (Lookup.isAmbiguous())
12615 return;
12616 Lookup.suppressDiagnostics();
12617
12618 if (!Lookup.isSingleResult()) {
12619 if (TypoCorrection Corrected =
12620 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
12621 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
12622 CTK_ErrorRecovery)) {
12623 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
12624 << Id.getName());
12625 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
12626 return;
12627 }
12628
12629 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
12630 return;
12631 }
12632
12633 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
12634 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
12635 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
12636 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
12637
12638 if (!ND->hasAttr<OMPDeclareTargetDeclAttr>()) {
12639 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
12640 ND->addAttr(A);
12641 if (ASTMutationListener *ML = Context.getASTMutationListener())
12642 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
Kelvin Li1ce87c72017-12-12 20:08:12 +000012643 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Id.getLoc());
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012644 } else if (ND->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() != MT) {
12645 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
12646 << Id.getName();
12647 }
12648 } else
12649 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
12650}
12651
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012652static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
12653 Sema &SemaRef, Decl *D) {
12654 if (!D)
12655 return;
12656 Decl *LD = nullptr;
12657 if (isa<TagDecl>(D)) {
12658 LD = cast<TagDecl>(D)->getDefinition();
12659 } else if (isa<VarDecl>(D)) {
12660 LD = cast<VarDecl>(D)->getDefinition();
12661
12662 // If this is an implicit variable that is legal and we do not need to do
12663 // anything.
12664 if (cast<VarDecl>(D)->isImplicit()) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012665 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
12666 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
12667 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012668 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012669 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012670 return;
12671 }
12672
12673 } else if (isa<FunctionDecl>(D)) {
12674 const FunctionDecl *FD = nullptr;
12675 if (cast<FunctionDecl>(D)->hasBody(FD))
12676 LD = const_cast<FunctionDecl *>(FD);
12677
12678 // If the definition is associated with the current declaration in the
12679 // target region (it can be e.g. a lambda) that is legal and we do not need
12680 // to do anything else.
12681 if (LD == D) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012682 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
12683 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
12684 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012685 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012686 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012687 return;
12688 }
12689 }
12690 if (!LD)
12691 LD = D;
12692 if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() &&
12693 (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) {
12694 // Outlined declaration is not declared target.
12695 if (LD->isOutOfLine()) {
12696 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
12697 SemaRef.Diag(SL, diag::note_used_here) << SR;
12698 } else {
12699 DeclContext *DC = LD->getDeclContext();
12700 while (DC) {
12701 if (isa<FunctionDecl>(DC) &&
12702 cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>())
12703 break;
12704 DC = DC->getParent();
12705 }
12706 if (DC)
12707 return;
12708
12709 // Is not declared in target context.
12710 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
12711 SemaRef.Diag(SL, diag::note_used_here) << SR;
12712 }
12713 // Mark decl as declared target to prevent further diagnostic.
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012714 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
12715 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
12716 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012717 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012718 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012719 }
12720}
12721
12722static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
12723 Sema &SemaRef, DSAStackTy *Stack,
12724 ValueDecl *VD) {
12725 if (VD->hasAttr<OMPDeclareTargetDeclAttr>())
12726 return true;
12727 if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType()))
12728 return false;
12729 return true;
12730}
12731
Kelvin Li1ce87c72017-12-12 20:08:12 +000012732void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D,
12733 SourceLocation IdLoc) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012734 if (!D || D->isInvalidDecl())
12735 return;
12736 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
12737 SourceLocation SL = E ? E->getLocStart() : D->getLocation();
12738 // 2.10.6: threadprivate variable cannot appear in a declare target directive.
12739 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
12740 if (DSAStack->isThreadPrivate(VD)) {
12741 Diag(SL, diag::err_omp_threadprivate_in_target);
12742 ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
12743 return;
12744 }
12745 }
12746 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
12747 // Problem if any with var declared with incomplete type will be reported
12748 // as normal, so no need to check it here.
12749 if ((E || !VD->getType()->isIncompleteType()) &&
12750 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) {
12751 // Mark decl as declared target to prevent further diagnostic.
12752 if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012753 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
12754 Context, OMPDeclareTargetDeclAttr::MT_To);
12755 VD->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012756 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012757 ML->DeclarationMarkedOpenMPDeclareTarget(VD, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012758 }
12759 return;
12760 }
12761 }
Kelvin Li1ce87c72017-12-12 20:08:12 +000012762 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
12763 if (FD->hasAttr<OMPDeclareTargetDeclAttr>() &&
12764 (FD->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() ==
12765 OMPDeclareTargetDeclAttr::MT_Link)) {
12766 assert(IdLoc.isValid() && "Source location is expected");
12767 Diag(IdLoc, diag::err_omp_function_in_link_clause);
12768 Diag(FD->getLocation(), diag::note_defined_here) << FD;
12769 return;
12770 }
12771 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012772 if (!E) {
12773 // Checking declaration inside declare target region.
12774 if (!D->hasAttr<OMPDeclareTargetDeclAttr>() &&
12775 (isa<VarDecl>(D) || isa<FunctionDecl>(D))) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012776 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
12777 Context, OMPDeclareTargetDeclAttr::MT_To);
12778 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012779 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012780 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012781 }
12782 return;
12783 }
12784 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
12785}
Samuel Antao661c0902016-05-26 17:39:58 +000012786
12787OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
12788 SourceLocation StartLoc,
12789 SourceLocation LParenLoc,
12790 SourceLocation EndLoc) {
12791 MappableVarListInfo MVLI(VarList);
12792 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc);
12793 if (MVLI.ProcessedVarList.empty())
12794 return nullptr;
12795
12796 return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc,
12797 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
12798 MVLI.VarComponents);
12799}
Samuel Antaoec172c62016-05-26 17:49:04 +000012800
12801OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
12802 SourceLocation StartLoc,
12803 SourceLocation LParenLoc,
12804 SourceLocation EndLoc) {
12805 MappableVarListInfo MVLI(VarList);
12806 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc);
12807 if (MVLI.ProcessedVarList.empty())
12808 return nullptr;
12809
12810 return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc,
12811 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
12812 MVLI.VarComponents);
12813}
Carlo Bertolli2404b172016-07-13 15:37:16 +000012814
12815OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
12816 SourceLocation StartLoc,
12817 SourceLocation LParenLoc,
12818 SourceLocation EndLoc) {
Samuel Antaocc10b852016-07-28 14:23:26 +000012819 MappableVarListInfo MVLI(VarList);
12820 SmallVector<Expr *, 8> PrivateCopies;
12821 SmallVector<Expr *, 8> Inits;
12822
Carlo Bertolli2404b172016-07-13 15:37:16 +000012823 for (auto &RefExpr : VarList) {
12824 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
12825 SourceLocation ELoc;
12826 SourceRange ERange;
12827 Expr *SimpleRefExpr = RefExpr;
12828 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
12829 if (Res.second) {
12830 // It will be analyzed later.
Samuel Antaocc10b852016-07-28 14:23:26 +000012831 MVLI.ProcessedVarList.push_back(RefExpr);
12832 PrivateCopies.push_back(nullptr);
12833 Inits.push_back(nullptr);
Carlo Bertolli2404b172016-07-13 15:37:16 +000012834 }
12835 ValueDecl *D = Res.first;
12836 if (!D)
12837 continue;
12838
12839 QualType Type = D->getType();
Samuel Antaocc10b852016-07-28 14:23:26 +000012840 Type = Type.getNonReferenceType().getUnqualifiedType();
12841
12842 auto *VD = dyn_cast<VarDecl>(D);
12843
12844 // Item should be a pointer or reference to pointer.
12845 if (!Type->isPointerType()) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000012846 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
12847 << 0 << RefExpr->getSourceRange();
12848 continue;
12849 }
Samuel Antaocc10b852016-07-28 14:23:26 +000012850
12851 // Build the private variable and the expression that refers to it.
12852 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
12853 D->hasAttrs() ? &D->getAttrs() : nullptr);
12854 if (VDPrivate->isInvalidDecl())
12855 continue;
12856
12857 CurContext->addDecl(VDPrivate);
12858 auto VDPrivateRefExpr = buildDeclRefExpr(
12859 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
12860
12861 // Add temporary variable to initialize the private copy of the pointer.
12862 auto *VDInit =
12863 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
12864 auto *VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
12865 RefExpr->getExprLoc());
12866 AddInitializerToDecl(VDPrivate,
12867 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000012868 /*DirectInit=*/false);
Samuel Antaocc10b852016-07-28 14:23:26 +000012869
12870 // If required, build a capture to implement the privatization initialized
12871 // with the current list item value.
12872 DeclRefExpr *Ref = nullptr;
12873 if (!VD)
12874 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
12875 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
12876 PrivateCopies.push_back(VDPrivateRefExpr);
12877 Inits.push_back(VDInitRefExpr);
12878
12879 // We need to add a data sharing attribute for this variable to make sure it
12880 // is correctly captured. A variable that shows up in a use_device_ptr has
12881 // similar properties of a first private variable.
12882 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
12883
12884 // Create a mappable component for the list item. List items in this clause
12885 // only need a component.
12886 MVLI.VarBaseDeclarations.push_back(D);
12887 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
12888 MVLI.VarComponents.back().push_back(
12889 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D));
Carlo Bertolli2404b172016-07-13 15:37:16 +000012890 }
12891
Samuel Antaocc10b852016-07-28 14:23:26 +000012892 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli2404b172016-07-13 15:37:16 +000012893 return nullptr;
12894
Samuel Antaocc10b852016-07-28 14:23:26 +000012895 return OMPUseDevicePtrClause::Create(
12896 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
12897 PrivateCopies, Inits, MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli2404b172016-07-13 15:37:16 +000012898}
Carlo Bertolli70594e92016-07-13 17:16:49 +000012899
12900OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
12901 SourceLocation StartLoc,
12902 SourceLocation LParenLoc,
12903 SourceLocation EndLoc) {
Samuel Antao6890b092016-07-28 14:25:09 +000012904 MappableVarListInfo MVLI(VarList);
Carlo Bertolli70594e92016-07-13 17:16:49 +000012905 for (auto &RefExpr : VarList) {
Kelvin Li84376252016-12-14 15:39:58 +000012906 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause.");
Carlo Bertolli70594e92016-07-13 17:16:49 +000012907 SourceLocation ELoc;
12908 SourceRange ERange;
12909 Expr *SimpleRefExpr = RefExpr;
12910 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
12911 if (Res.second) {
12912 // It will be analyzed later.
Samuel Antao6890b092016-07-28 14:25:09 +000012913 MVLI.ProcessedVarList.push_back(RefExpr);
Carlo Bertolli70594e92016-07-13 17:16:49 +000012914 }
12915 ValueDecl *D = Res.first;
12916 if (!D)
12917 continue;
12918
12919 QualType Type = D->getType();
12920 // item should be a pointer or array or reference to pointer or array
12921 if (!Type.getNonReferenceType()->isPointerType() &&
12922 !Type.getNonReferenceType()->isArrayType()) {
12923 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
12924 << 0 << RefExpr->getSourceRange();
12925 continue;
12926 }
Samuel Antao6890b092016-07-28 14:25:09 +000012927
12928 // Check if the declaration in the clause does not show up in any data
12929 // sharing attribute.
12930 auto DVar = DSAStack->getTopDSA(D, false);
12931 if (isOpenMPPrivate(DVar.CKind)) {
12932 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
12933 << getOpenMPClauseName(DVar.CKind)
12934 << getOpenMPClauseName(OMPC_is_device_ptr)
12935 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
12936 ReportOriginalDSA(*this, DSAStack, D, DVar);
12937 continue;
12938 }
12939
12940 Expr *ConflictExpr;
12941 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +000012942 D, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +000012943 [&ConflictExpr](
12944 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
12945 OpenMPClauseKind) -> bool {
12946 ConflictExpr = R.front().getAssociatedExpression();
12947 return true;
12948 })) {
12949 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
12950 Diag(ConflictExpr->getExprLoc(), diag::note_used_here)
12951 << ConflictExpr->getSourceRange();
12952 continue;
12953 }
12954
12955 // Store the components in the stack so that they can be used to check
12956 // against other clauses later on.
12957 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D);
12958 DSAStack->addMappableExpressionComponents(
12959 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
12960
12961 // Record the expression we've just processed.
12962 MVLI.ProcessedVarList.push_back(SimpleRefExpr);
12963
12964 // Create a mappable component for the list item. List items in this clause
12965 // only need a component. We use a null declaration to signal fields in
12966 // 'this'.
12967 assert((isa<DeclRefExpr>(SimpleRefExpr) ||
12968 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
12969 "Unexpected device pointer expression!");
12970 MVLI.VarBaseDeclarations.push_back(
12971 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr);
12972 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
12973 MVLI.VarComponents.back().push_back(MC);
Carlo Bertolli70594e92016-07-13 17:16:49 +000012974 }
12975
Samuel Antao6890b092016-07-28 14:25:09 +000012976 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli70594e92016-07-13 17:16:49 +000012977 return nullptr;
12978
Samuel Antao6890b092016-07-28 14:25:09 +000012979 return OMPIsDevicePtrClause::Create(
12980 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
12981 MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli70594e92016-07-13 17:16:49 +000012982}