blob: 59ac0348cd433aa1e4b3d5b23c7bdab6f557e5e8 [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
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000010/// 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 Bataeve3727102018-04-18 15:57:46 +000038static const Expr *checkMapClauseExpressionBase(
Alexey Bataevf47c4b42017-09-26 13:47:31 +000039 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 {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000044/// Default data sharing attributes, which can be applied to directive.
Alexey Bataev758e55e2013-09-06 18:03:48 +000045enum DefaultDataSharingAttributes {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000046 DSA_unspecified = 0, /// Data sharing attribute not specified.
47 DSA_none = 1 << 0, /// Default data sharing attribute 'none'.
48 DSA_shared = 1 << 1, /// Default data sharing attribute 'shared'.
Alexey Bataev2fd0cb22017-10-05 17:51:39 +000049};
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
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000057/// Stack for tracking declarations used in OpenMP directives and
Alexey Bataev758e55e2013-09-06 18:03:48 +000058/// clauses and their data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +000059class DSAStackTy {
Alexey Bataev758e55e2013-09-06 18:03:48 +000060public:
Alexey Bataeve3727102018-04-18 15:57:46 +000061 struct DSAVarData {
Alexey Bataev7ace49d2016-05-17 08:55:33 +000062 OpenMPDirectiveKind DKind = OMPD_unknown;
63 OpenMPClauseKind CKind = OMPC_unknown;
Alexey Bataeve3727102018-04-18 15:57:46 +000064 const Expr *RefExpr = nullptr;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000065 DeclRefExpr *PrivateCopy = nullptr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000066 SourceLocation ImplicitDSALoc;
Alexey Bataev4d4624c2017-07-20 16:47:47 +000067 DSAVarData() = default;
Alexey Bataeve3727102018-04-18 15:57:46 +000068 DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
69 const Expr *RefExpr, DeclRefExpr *PrivateCopy,
70 SourceLocation ImplicitDSALoc)
Alexey Bataevf189cb72017-07-24 14:52:13 +000071 : DKind(DKind), CKind(CKind), RefExpr(RefExpr),
72 PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000073 };
Alexey Bataeve3727102018-04-18 15:57:46 +000074 using OperatorOffsetTy =
75 llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>;
Alexey Bataevf138fda2018-08-13 19:04:24 +000076 using DoacrossDependMapTy =
77 llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>;
Alexey Bataeved09d242014-05-28 05:53:51 +000078
Alexey Bataev758e55e2013-09-06 18:03:48 +000079private:
Alexey Bataeve3727102018-04-18 15:57:46 +000080 struct DSAInfo {
Alexey Bataev7ace49d2016-05-17 08:55:33 +000081 OpenMPClauseKind Attributes = OMPC_unknown;
82 /// Pointer to a reference expression and a flag which shows that the
83 /// variable is marked as lastprivate(true) or not (false).
Alexey Bataeve3727102018-04-18 15:57:46 +000084 llvm::PointerIntPair<const Expr *, 1, bool> RefExpr;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000085 DeclRefExpr *PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +000086 };
Alexey Bataeve3727102018-04-18 15:57:46 +000087 using DeclSAMapTy = llvm::SmallDenseMap<const ValueDecl *, DSAInfo, 8>;
88 using AlignedMapTy = llvm::SmallDenseMap<const ValueDecl *, const Expr *, 8>;
89 using LCDeclInfo = std::pair<unsigned, VarDecl *>;
90 using LoopControlVariablesMapTy =
91 llvm::SmallDenseMap<const ValueDecl *, LCDeclInfo, 8>;
Samuel Antao6890b092016-07-28 14:25:09 +000092 /// Struct that associates a component with the clause kind where they are
93 /// found.
94 struct MappedExprComponentTy {
95 OMPClauseMappableExprCommon::MappableExprComponentLists Components;
96 OpenMPClauseKind Kind = OMPC_unknown;
97 };
Alexey Bataeve3727102018-04-18 15:57:46 +000098 using MappedExprComponentsTy =
99 llvm::DenseMap<const ValueDecl *, MappedExprComponentTy>;
100 using CriticalsWithHintsTy =
101 llvm::StringMap<std::pair<const OMPCriticalDirective *, llvm::APSInt>>;
Alexey Bataevfa312f32017-07-21 18:48:21 +0000102 struct ReductionData {
Alexey Bataeve3727102018-04-18 15:57:46 +0000103 using BOKPtrType = llvm::PointerEmbeddedInt<BinaryOperatorKind, 16>;
Alexey Bataevfa312f32017-07-21 18:48:21 +0000104 SourceRange ReductionRange;
Alexey Bataevf87fa882017-07-21 19:26:22 +0000105 llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp;
Alexey Bataevfa312f32017-07-21 18:48:21 +0000106 ReductionData() = default;
107 void set(BinaryOperatorKind BO, SourceRange RR) {
108 ReductionRange = RR;
109 ReductionOp = BO;
110 }
111 void set(const Expr *RefExpr, SourceRange RR) {
112 ReductionRange = RR;
113 ReductionOp = RefExpr;
114 }
115 };
Alexey Bataeve3727102018-04-18 15:57:46 +0000116 using DeclReductionMapTy =
117 llvm::SmallDenseMap<const ValueDecl *, ReductionData, 4>;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000118
Alexey Bataeve3727102018-04-18 15:57:46 +0000119 struct SharingMapTy {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000120 DeclSAMapTy SharingMap;
Alexey Bataevfa312f32017-07-21 18:48:21 +0000121 DeclReductionMapTy ReductionMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000122 AlignedMapTy AlignedMap;
Samuel Antao90927002016-04-26 14:54:23 +0000123 MappedExprComponentsTy MappedExprComponents;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000124 LoopControlVariablesMapTy LCVMap;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000125 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000126 SourceLocation DefaultAttrLoc;
Alexey Bataev2fd0cb22017-10-05 17:51:39 +0000127 DefaultMapAttributes DefaultMapAttr = DMA_unspecified;
128 SourceLocation DefaultMapAttrLoc;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000129 OpenMPDirectiveKind Directive = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000130 DeclarationNameInfo DirectiveName;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000131 Scope *CurScope = nullptr;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000132 SourceLocation ConstructLoc;
Alexey Bataev8b427062016-05-25 12:36:08 +0000133 /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to
134 /// get the data (loop counters etc.) about enclosing loop-based construct.
135 /// This data is required during codegen.
136 DoacrossDependMapTy DoacrossDepends;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000137 /// first argument (Expr *) contains optional argument of the
Alexey Bataev346265e2015-09-25 10:37:12 +0000138 /// 'ordered' clause, the second one is true if the regions has 'ordered'
139 /// clause, false otherwise.
Alexey Bataevf138fda2018-08-13 19:04:24 +0000140 llvm::Optional<std::pair<const Expr *, OMPOrderedClause *>> OrderedRegion;
Alexey Bataev6ab5bb12018-10-29 15:01:58 +0000141 unsigned AssociatedLoops = 1;
142 const Decl *PossiblyLoopCounter = nullptr;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000143 bool NowaitRegion = false;
144 bool CancelRegion = false;
Alexey Bataev6ab5bb12018-10-29 15:01:58 +0000145 bool LoopStart = false;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000146 SourceLocation InnerTeamsRegionLoc;
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000147 /// Reference to the taskgroup task_reduction reference expression.
148 Expr *TaskgroupReductionRef = nullptr;
Alexey Bataeved09d242014-05-28 05:53:51 +0000149 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000150 Scope *CurScope, SourceLocation Loc)
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000151 : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
152 ConstructLoc(Loc) {}
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000153 SharingMapTy() = default;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000154 };
155
Alexey Bataeve3727102018-04-18 15:57:46 +0000156 using StackTy = SmallVector<SharingMapTy, 4>;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000157
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000158 /// Stack of used declaration and their data-sharing attributes.
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000159 DeclSAMapTy Threadprivates;
Alexey Bataev4b465392017-04-26 15:06:24 +0000160 const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr;
161 SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000162 /// true, if check for DSA must be from parent directive, false, if
Alexey Bataev39f915b82015-05-08 10:41:21 +0000163 /// from current directive.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000164 OpenMPClauseKind ClauseKindMode = OMPC_unknown;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000165 Sema &SemaRef;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000166 bool ForceCapturing = false;
Alexey Bataev60705422018-10-30 15:50:12 +0000167 /// true if all the vaiables in the target executable directives must be
168 /// captured by reference.
169 bool ForceCaptureByReferenceInTargetExecutable = false;
Alexey Bataev28c75412015-12-15 08:19:24 +0000170 CriticalsWithHintsTy Criticals;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000171
Alexey Bataeve3727102018-04-18 15:57:46 +0000172 using iterator = StackTy::const_reverse_iterator;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000173
Alexey Bataeve3727102018-04-18 15:57:46 +0000174 DSAVarData getDSA(iterator &Iter, ValueDecl *D) const;
Alexey Bataevec3da872014-01-31 05:15:34 +0000175
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000176 /// Checks if the variable is a local for OpenMP region.
Alexey Bataeve3727102018-04-18 15:57:46 +0000177 bool isOpenMPLocal(VarDecl *D, iterator Iter) const;
Alexey Bataeved09d242014-05-28 05:53:51 +0000178
Alexey Bataev4b465392017-04-26 15:06:24 +0000179 bool isStackEmpty() const {
180 return Stack.empty() ||
181 Stack.back().second != CurrentNonCapturingFunctionScope ||
182 Stack.back().first.empty();
183 }
184
Kelvin Li1408f912018-09-26 04:28:39 +0000185 /// Vector of previously declared requires directives
186 SmallVector<const OMPRequiresDecl *, 2> RequiresDecls;
187
Alexey Bataev758e55e2013-09-06 18:03:48 +0000188public:
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000189 explicit DSAStackTy(Sema &S) : SemaRef(S) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000190
Alexey Bataevaac108a2015-06-23 04:51:00 +0000191 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
Alexey Bataev3f82cfc2017-12-13 15:28:44 +0000192 OpenMPClauseKind getClauseParsingMode() const {
193 assert(isClauseParsingMode() && "Must be in clause parsing mode.");
194 return ClauseKindMode;
195 }
Alexey Bataevaac108a2015-06-23 04:51:00 +0000196 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000197
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000198 bool isForceVarCapturing() const { return ForceCapturing; }
199 void setForceVarCapturing(bool V) { ForceCapturing = V; }
200
Alexey Bataev60705422018-10-30 15:50:12 +0000201 void setForceCaptureByReferenceInTargetExecutable(bool V) {
202 ForceCaptureByReferenceInTargetExecutable = V;
203 }
204 bool isForceCaptureByReferenceInTargetExecutable() const {
205 return ForceCaptureByReferenceInTargetExecutable;
206 }
207
Alexey Bataev758e55e2013-09-06 18:03:48 +0000208 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000209 Scope *CurScope, SourceLocation Loc) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000210 if (Stack.empty() ||
211 Stack.back().second != CurrentNonCapturingFunctionScope)
212 Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope);
213 Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc);
214 Stack.back().first.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000215 }
216
217 void pop() {
Alexey Bataev4b465392017-04-26 15:06:24 +0000218 assert(!Stack.back().first.empty() &&
219 "Data-sharing attributes stack is empty!");
220 Stack.back().first.pop_back();
221 }
222
Alexey Bataev6ab5bb12018-10-29 15:01:58 +0000223 /// Marks that we're started loop parsing.
224 void loopInit() {
225 assert(isOpenMPLoopDirective(getCurrentDirective()) &&
226 "Expected loop-based directive.");
227 Stack.back().first.back().LoopStart = true;
228 }
229 /// Start capturing of the variables in the loop context.
230 void loopStart() {
231 assert(isOpenMPLoopDirective(getCurrentDirective()) &&
232 "Expected loop-based directive.");
233 Stack.back().first.back().LoopStart = false;
234 }
235 /// true, if variables are captured, false otherwise.
236 bool isLoopStarted() const {
237 assert(isOpenMPLoopDirective(getCurrentDirective()) &&
238 "Expected loop-based directive.");
239 return !Stack.back().first.back().LoopStart;
240 }
241 /// Marks (or clears) declaration as possibly loop counter.
242 void resetPossibleLoopCounter(const Decl *D = nullptr) {
243 Stack.back().first.back().PossiblyLoopCounter =
244 D ? D->getCanonicalDecl() : D;
245 }
246 /// Gets the possible loop counter decl.
247 const Decl *getPossiblyLoopCunter() const {
248 return Stack.back().first.back().PossiblyLoopCounter;
249 }
Alexey Bataev4b465392017-04-26 15:06:24 +0000250 /// Start new OpenMP region stack in new non-capturing function.
251 void pushFunction() {
252 const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction();
253 assert(!isa<CapturingScopeInfo>(CurFnScope));
254 CurrentNonCapturingFunctionScope = CurFnScope;
255 }
256 /// Pop region stack for non-capturing function.
257 void popFunction(const FunctionScopeInfo *OldFSI) {
258 if (!Stack.empty() && Stack.back().second == OldFSI) {
259 assert(Stack.back().first.empty());
260 Stack.pop_back();
261 }
262 CurrentNonCapturingFunctionScope = nullptr;
263 for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) {
264 if (!isa<CapturingScopeInfo>(FSI)) {
265 CurrentNonCapturingFunctionScope = FSI;
266 break;
267 }
268 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000269 }
270
Alexey Bataeve3727102018-04-18 15:57:46 +0000271 void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) {
Alexey Bataev43a919f2018-04-13 17:48:43 +0000272 Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint);
Alexey Bataev28c75412015-12-15 08:19:24 +0000273 }
Alexey Bataeve3727102018-04-18 15:57:46 +0000274 const std::pair<const OMPCriticalDirective *, llvm::APSInt>
Alexey Bataev28c75412015-12-15 08:19:24 +0000275 getCriticalWithHint(const DeclarationNameInfo &Name) const {
276 auto I = Criticals.find(Name.getAsString());
277 if (I != Criticals.end())
278 return I->second;
279 return std::make_pair(nullptr, llvm::APSInt());
280 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000281 /// If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000282 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000283 /// for diagnostics.
Alexey Bataeve3727102018-04-18 15:57:46 +0000284 const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000285
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000286 /// Register specified variable as loop control variable.
Alexey Bataeve3727102018-04-18 15:57:46 +0000287 void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000288 /// Check if the specified variable is a loop control variable for
Alexey Bataev9c821032015-04-30 04:23:23 +0000289 /// current region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000290 /// \return The index of the loop control variable in the list of associated
291 /// for-loops (from outer to inner).
Alexey Bataeve3727102018-04-18 15:57:46 +0000292 const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000293 /// Check if the specified variable is a loop control variable for
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000294 /// parent region.
295 /// \return The index of the loop control variable in the list of associated
296 /// for-loops (from outer to inner).
Alexey Bataeve3727102018-04-18 15:57:46 +0000297 const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000298 /// Get the loop control variable for the I-th loop (or nullptr) in
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000299 /// parent directive.
Alexey Bataeve3727102018-04-18 15:57:46 +0000300 const ValueDecl *getParentLoopControlVariable(unsigned I) const;
Alexey Bataev9c821032015-04-30 04:23:23 +0000301
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000302 /// Adds explicit data sharing attribute to the specified declaration.
Alexey Bataeve3727102018-04-18 15:57:46 +0000303 void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
Alexey Bataev90c228f2016-02-08 09:29:13 +0000304 DeclRefExpr *PrivateCopy = nullptr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000305
Alexey Bataevfa312f32017-07-21 18:48:21 +0000306 /// Adds additional information for the reduction items with the reduction id
307 /// represented as an operator.
Alexey Bataeve3727102018-04-18 15:57:46 +0000308 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000309 BinaryOperatorKind BOK);
Alexey Bataevfa312f32017-07-21 18:48:21 +0000310 /// Adds additional information for the reduction items with the reduction id
311 /// represented as reduction identifier.
Alexey Bataeve3727102018-04-18 15:57:46 +0000312 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000313 const Expr *ReductionRef);
Alexey Bataevfa312f32017-07-21 18:48:21 +0000314 /// Returns the location and reduction operation from the innermost parent
315 /// region for the given \p D.
Alexey Bataeve3727102018-04-18 15:57:46 +0000316 const DSAVarData
317 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
318 BinaryOperatorKind &BOK,
319 Expr *&TaskgroupDescriptor) const;
Alexey Bataevfa312f32017-07-21 18:48:21 +0000320 /// Returns the location and reduction operation from the innermost parent
321 /// region for the given \p D.
Alexey Bataeve3727102018-04-18 15:57:46 +0000322 const DSAVarData
323 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
324 const Expr *&ReductionRef,
325 Expr *&TaskgroupDescriptor) const;
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000326 /// Return reduction reference expression for the current taskgroup.
327 Expr *getTaskgroupReductionRef() const {
328 assert(Stack.back().first.back().Directive == OMPD_taskgroup &&
329 "taskgroup reference expression requested for non taskgroup "
330 "directive.");
331 return Stack.back().first.back().TaskgroupReductionRef;
332 }
Alexey Bataev88202be2017-07-27 13:20:36 +0000333 /// Checks if the given \p VD declaration is actually a taskgroup reduction
334 /// descriptor variable at the \p Level of OpenMP regions.
Alexey Bataeve3727102018-04-18 15:57:46 +0000335 bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const {
Alexey Bataev88202be2017-07-27 13:20:36 +0000336 return Stack.back().first[Level].TaskgroupReductionRef &&
337 cast<DeclRefExpr>(Stack.back().first[Level].TaskgroupReductionRef)
338 ->getDecl() == VD;
339 }
Alexey Bataevfa312f32017-07-21 18:48:21 +0000340
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000341 /// Returns data sharing attributes from top of the stack for the
Alexey Bataev758e55e2013-09-06 18:03:48 +0000342 /// specified declaration.
Alexey Bataeve3727102018-04-18 15:57:46 +0000343 const DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000344 /// Returns data-sharing attributes for the specified declaration.
Alexey Bataeve3727102018-04-18 15:57:46 +0000345 const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000346 /// Checks if the specified variables has data-sharing attributes which
Alexey Bataevf29276e2014-06-18 04:14:57 +0000347 /// match specified \a CPred predicate in any directive which matches \a DPred
348 /// predicate.
Alexey Bataeve3727102018-04-18 15:57:46 +0000349 const DSAVarData
350 hasDSA(ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
351 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
352 bool FromParent) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000353 /// Checks if the specified variables has data-sharing attributes which
Alexey Bataevf29276e2014-06-18 04:14:57 +0000354 /// match specified \a CPred predicate in any innermost directive which
355 /// matches \a DPred predicate.
Alexey Bataeve3727102018-04-18 15:57:46 +0000356 const DSAVarData
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000357 hasInnermostDSA(ValueDecl *D,
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000358 const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
359 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +0000360 bool FromParent) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000361 /// Checks if the specified variables has explicit data-sharing
Alexey Bataevaac108a2015-06-23 04:51:00 +0000362 /// attributes which match specified \a CPred predicate at the specified
363 /// OpenMP region.
Alexey Bataeve3727102018-04-18 15:57:46 +0000364 bool hasExplicitDSA(const ValueDecl *D,
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000365 const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
Alexey Bataeve3727102018-04-18 15:57:46 +0000366 unsigned Level, bool NotLastprivate = false) const;
Samuel Antao4be30e92015-10-02 17:14:03 +0000367
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000368 /// Returns true if the directive at level \Level matches in the
Samuel Antao4be30e92015-10-02 17:14:03 +0000369 /// specified \a DPred predicate.
370 bool hasExplicitDirective(
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000371 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +0000372 unsigned Level) const;
Samuel Antao4be30e92015-10-02 17:14:03 +0000373
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000374 /// Finds a directive which matches specified \a DPred predicate.
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000375 bool hasDirective(
376 const llvm::function_ref<bool(
377 OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)>
378 DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +0000379 bool FromParent) const;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000380
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000381 /// Returns currently analyzed directive.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000382 OpenMPDirectiveKind getCurrentDirective() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000383 return isStackEmpty() ? OMPD_unknown : Stack.back().first.back().Directive;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000384 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000385 /// Returns directive kind at specified level.
Alexey Bataevdfa430f2017-12-08 15:03:50 +0000386 OpenMPDirectiveKind getDirective(unsigned Level) const {
387 assert(!isStackEmpty() && "No directive at specified level.");
388 return Stack.back().first[Level].Directive;
389 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000390 /// Returns parent directive.
Alexey Bataev549210e2014-06-24 04:39:47 +0000391 OpenMPDirectiveKind getParentDirective() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000392 if (isStackEmpty() || Stack.back().first.size() == 1)
393 return OMPD_unknown;
394 return std::next(Stack.back().first.rbegin())->Directive;
Alexey Bataev549210e2014-06-24 04:39:47 +0000395 }
Alexey Bataev6ab5bb12018-10-29 15:01:58 +0000396
Kelvin Li1408f912018-09-26 04:28:39 +0000397 /// Add requires decl to internal vector
398 void addRequiresDecl(OMPRequiresDecl *RD) {
399 RequiresDecls.push_back(RD);
400 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000401
Kelvin Li1408f912018-09-26 04:28:39 +0000402 /// Checks for a duplicate clause amongst previously declared requires
403 /// directives
404 bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const {
405 bool IsDuplicate = false;
406 for (OMPClause *CNew : ClauseList) {
407 for (const OMPRequiresDecl *D : RequiresDecls) {
408 for (const OMPClause *CPrev : D->clauselists()) {
409 if (CNew->getClauseKind() == CPrev->getClauseKind()) {
410 SemaRef.Diag(CNew->getBeginLoc(),
411 diag::err_omp_requires_clause_redeclaration)
412 << getOpenMPClauseName(CNew->getClauseKind());
413 SemaRef.Diag(CPrev->getBeginLoc(),
414 diag::note_omp_requires_previous_clause)
415 << getOpenMPClauseName(CPrev->getClauseKind());
416 IsDuplicate = true;
417 }
418 }
419 }
420 }
421 return IsDuplicate;
422 }
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +0000423
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000424 /// Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000425 void setDefaultDSANone(SourceLocation Loc) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000426 assert(!isStackEmpty());
427 Stack.back().first.back().DefaultAttr = DSA_none;
428 Stack.back().first.back().DefaultAttrLoc = Loc;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000429 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000430 /// Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000431 void setDefaultDSAShared(SourceLocation Loc) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000432 assert(!isStackEmpty());
433 Stack.back().first.back().DefaultAttr = DSA_shared;
434 Stack.back().first.back().DefaultAttrLoc = Loc;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000435 }
Alexey Bataev2fd0cb22017-10-05 17:51:39 +0000436 /// Set default data mapping attribute to 'tofrom:scalar'.
437 void setDefaultDMAToFromScalar(SourceLocation Loc) {
438 assert(!isStackEmpty());
439 Stack.back().first.back().DefaultMapAttr = DMA_tofrom_scalar;
440 Stack.back().first.back().DefaultMapAttrLoc = Loc;
441 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000442
443 DefaultDataSharingAttributes getDefaultDSA() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000444 return isStackEmpty() ? DSA_unspecified
445 : Stack.back().first.back().DefaultAttr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000446 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000447 SourceLocation getDefaultDSALocation() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000448 return isStackEmpty() ? SourceLocation()
449 : Stack.back().first.back().DefaultAttrLoc;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000450 }
Alexey Bataev2fd0cb22017-10-05 17:51:39 +0000451 DefaultMapAttributes getDefaultDMA() const {
452 return isStackEmpty() ? DMA_unspecified
453 : Stack.back().first.back().DefaultMapAttr;
454 }
455 DefaultMapAttributes getDefaultDMAAtLevel(unsigned Level) const {
456 return Stack.back().first[Level].DefaultMapAttr;
457 }
458 SourceLocation getDefaultDMALocation() const {
459 return isStackEmpty() ? SourceLocation()
460 : Stack.back().first.back().DefaultMapAttrLoc;
461 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000462
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000463 /// Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000464 bool isThreadPrivate(VarDecl *D) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000465 const DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000466 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000467 }
468
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000469 /// Marks current region as ordered (it has an 'ordered' clause).
Alexey Bataevf138fda2018-08-13 19:04:24 +0000470 void setOrderedRegion(bool IsOrdered, const Expr *Param,
471 OMPOrderedClause *Clause) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000472 assert(!isStackEmpty());
Alexey Bataevf138fda2018-08-13 19:04:24 +0000473 if (IsOrdered)
474 Stack.back().first.back().OrderedRegion.emplace(Param, Clause);
475 else
476 Stack.back().first.back().OrderedRegion.reset();
477 }
478 /// Returns true, if region is ordered (has associated 'ordered' clause),
479 /// false - otherwise.
480 bool isOrderedRegion() const {
481 if (isStackEmpty())
482 return false;
483 return Stack.back().first.rbegin()->OrderedRegion.hasValue();
484 }
485 /// Returns optional parameter for the ordered region.
486 std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const {
487 if (isStackEmpty() ||
488 !Stack.back().first.rbegin()->OrderedRegion.hasValue())
489 return std::make_pair(nullptr, nullptr);
490 return Stack.back().first.rbegin()->OrderedRegion.getValue();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000491 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000492 /// Returns true, if parent region is ordered (has associated
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000493 /// 'ordered' clause), false - otherwise.
494 bool isParentOrderedRegion() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000495 if (isStackEmpty() || Stack.back().first.size() == 1)
496 return false;
Alexey Bataevf138fda2018-08-13 19:04:24 +0000497 return std::next(Stack.back().first.rbegin())->OrderedRegion.hasValue();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000498 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000499 /// Returns optional parameter for the ordered region.
Alexey Bataevf138fda2018-08-13 19:04:24 +0000500 std::pair<const Expr *, OMPOrderedClause *>
501 getParentOrderedRegionParam() const {
502 if (isStackEmpty() || Stack.back().first.size() == 1 ||
503 !std::next(Stack.back().first.rbegin())->OrderedRegion.hasValue())
504 return std::make_pair(nullptr, nullptr);
505 return std::next(Stack.back().first.rbegin())->OrderedRegion.getValue();
Alexey Bataev346265e2015-09-25 10:37:12 +0000506 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000507 /// Marks current region as nowait (it has a 'nowait' clause).
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000508 void setNowaitRegion(bool IsNowait = true) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000509 assert(!isStackEmpty());
510 Stack.back().first.back().NowaitRegion = IsNowait;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000511 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000512 /// Returns true, if parent region is nowait (has associated
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000513 /// 'nowait' clause), false - otherwise.
514 bool isParentNowaitRegion() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000515 if (isStackEmpty() || Stack.back().first.size() == 1)
516 return false;
517 return std::next(Stack.back().first.rbegin())->NowaitRegion;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000518 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000519 /// Marks parent region as cancel region.
Alexey Bataev25e5b442015-09-15 12:52:43 +0000520 void setParentCancelRegion(bool Cancel = true) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000521 if (!isStackEmpty() && Stack.back().first.size() > 1) {
522 auto &StackElemRef = *std::next(Stack.back().first.rbegin());
523 StackElemRef.CancelRegion |= StackElemRef.CancelRegion || Cancel;
524 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000525 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000526 /// Return true if current region has inner cancel construct.
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000527 bool isCancelRegion() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000528 return isStackEmpty() ? false : Stack.back().first.back().CancelRegion;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000529 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000530
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000531 /// Set collapse value for the region.
Alexey Bataev4b465392017-04-26 15:06:24 +0000532 void setAssociatedLoops(unsigned Val) {
533 assert(!isStackEmpty());
534 Stack.back().first.back().AssociatedLoops = Val;
535 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000536 /// Return collapse value for region.
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000537 unsigned getAssociatedLoops() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000538 return isStackEmpty() ? 0 : Stack.back().first.back().AssociatedLoops;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000539 }
Alexey Bataev9c821032015-04-30 04:23:23 +0000540
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000541 /// Marks current target region as one with closely nested teams
Alexey Bataev13314bf2014-10-09 04:18:56 +0000542 /// region.
543 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000544 if (!isStackEmpty() && Stack.back().first.size() > 1) {
545 std::next(Stack.back().first.rbegin())->InnerTeamsRegionLoc =
546 TeamsRegionLoc;
547 }
Alexey Bataev13314bf2014-10-09 04:18:56 +0000548 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000549 /// Returns true, if current region has closely nested teams region.
Alexey Bataev13314bf2014-10-09 04:18:56 +0000550 bool hasInnerTeamsRegion() const {
551 return getInnerTeamsRegionLoc().isValid();
552 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000553 /// Returns location of the nested teams region (if any).
Alexey Bataev13314bf2014-10-09 04:18:56 +0000554 SourceLocation getInnerTeamsRegionLoc() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000555 return isStackEmpty() ? SourceLocation()
556 : Stack.back().first.back().InnerTeamsRegionLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000557 }
558
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000559 Scope *getCurScope() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000560 return isStackEmpty() ? nullptr : Stack.back().first.back().CurScope;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000561 }
Alexey Bataeve3727102018-04-18 15:57:46 +0000562 SourceLocation getConstructLoc() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000563 return isStackEmpty() ? SourceLocation()
564 : Stack.back().first.back().ConstructLoc;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000565 }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000566
Samuel Antao4c8035b2016-12-12 18:00:20 +0000567 /// Do the check specified in \a Check to all component lists and return true
568 /// if any issue is found.
Samuel Antao90927002016-04-26 14:54:23 +0000569 bool checkMappableExprComponentListsForDecl(
Alexey Bataeve3727102018-04-18 15:57:46 +0000570 const ValueDecl *VD, bool CurrentRegionOnly,
Samuel Antao6890b092016-07-28 14:25:09 +0000571 const llvm::function_ref<
572 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000573 OpenMPClauseKind)>
Alexey Bataeve3727102018-04-18 15:57:46 +0000574 Check) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000575 if (isStackEmpty())
576 return false;
577 auto SI = Stack.back().first.rbegin();
578 auto SE = Stack.back().first.rend();
Samuel Antao5de996e2016-01-22 20:21:36 +0000579
580 if (SI == SE)
581 return false;
582
Alexey Bataeve3727102018-04-18 15:57:46 +0000583 if (CurrentRegionOnly)
Samuel Antao5de996e2016-01-22 20:21:36 +0000584 SE = std::next(SI);
Alexey Bataeve3727102018-04-18 15:57:46 +0000585 else
586 std::advance(SI, 1);
Samuel Antao5de996e2016-01-22 20:21:36 +0000587
588 for (; SI != SE; ++SI) {
Samuel Antao90927002016-04-26 14:54:23 +0000589 auto MI = SI->MappedExprComponents.find(VD);
590 if (MI != SI->MappedExprComponents.end())
Alexey Bataeve3727102018-04-18 15:57:46 +0000591 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
592 MI->second.Components)
Samuel Antao6890b092016-07-28 14:25:09 +0000593 if (Check(L, MI->second.Kind))
Samuel Antao5de996e2016-01-22 20:21:36 +0000594 return true;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000595 }
Samuel Antao5de996e2016-01-22 20:21:36 +0000596 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000597 }
598
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000599 /// Do the check specified in \a Check to all component lists at a given level
600 /// and return true if any issue is found.
601 bool checkMappableExprComponentListsForDeclAtLevel(
Alexey Bataeve3727102018-04-18 15:57:46 +0000602 const ValueDecl *VD, unsigned Level,
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000603 const llvm::function_ref<
604 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000605 OpenMPClauseKind)>
Alexey Bataeve3727102018-04-18 15:57:46 +0000606 Check) const {
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000607 if (isStackEmpty())
608 return false;
609
610 auto StartI = Stack.back().first.begin();
611 auto EndI = Stack.back().first.end();
612 if (std::distance(StartI, EndI) <= (int)Level)
613 return false;
614 std::advance(StartI, Level);
615
616 auto MI = StartI->MappedExprComponents.find(VD);
617 if (MI != StartI->MappedExprComponents.end())
Alexey Bataeve3727102018-04-18 15:57:46 +0000618 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
619 MI->second.Components)
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000620 if (Check(L, MI->second.Kind))
621 return true;
622 return false;
623 }
624
Samuel Antao4c8035b2016-12-12 18:00:20 +0000625 /// Create a new mappable expression component list associated with a given
626 /// declaration and initialize it with the provided list of components.
Samuel Antao90927002016-04-26 14:54:23 +0000627 void addMappableExpressionComponents(
Alexey Bataeve3727102018-04-18 15:57:46 +0000628 const ValueDecl *VD,
Samuel Antao6890b092016-07-28 14:25:09 +0000629 OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
630 OpenMPClauseKind WhereFoundClauseKind) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000631 assert(!isStackEmpty() &&
Samuel Antao90927002016-04-26 14:54:23 +0000632 "Not expecting to retrieve components from a empty stack!");
Alexey Bataeve3727102018-04-18 15:57:46 +0000633 MappedExprComponentTy &MEC =
634 Stack.back().first.back().MappedExprComponents[VD];
Samuel Antao90927002016-04-26 14:54:23 +0000635 // Create new entry and append the new components there.
Samuel Antao6890b092016-07-28 14:25:09 +0000636 MEC.Components.resize(MEC.Components.size() + 1);
637 MEC.Components.back().append(Components.begin(), Components.end());
638 MEC.Kind = WhereFoundClauseKind;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000639 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000640
641 unsigned getNestingLevel() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000642 assert(!isStackEmpty());
643 return Stack.back().first.size() - 1;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000644 }
Alexey Bataeve3727102018-04-18 15:57:46 +0000645 void addDoacrossDependClause(OMPDependClause *C,
646 const OperatorOffsetTy &OpsOffs) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000647 assert(!isStackEmpty() && Stack.back().first.size() > 1);
Alexey Bataeve3727102018-04-18 15:57:46 +0000648 SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
Alexey Bataev4b465392017-04-26 15:06:24 +0000649 assert(isOpenMPWorksharingDirective(StackElem.Directive));
Alexey Bataeve3727102018-04-18 15:57:46 +0000650 StackElem.DoacrossDepends.try_emplace(C, OpsOffs);
Alexey Bataev8b427062016-05-25 12:36:08 +0000651 }
652 llvm::iterator_range<DoacrossDependMapTy::const_iterator>
653 getDoacrossDependClauses() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000654 assert(!isStackEmpty());
Alexey Bataeve3727102018-04-18 15:57:46 +0000655 const SharingMapTy &StackElem = Stack.back().first.back();
Alexey Bataev4b465392017-04-26 15:06:24 +0000656 if (isOpenMPWorksharingDirective(StackElem.Directive)) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000657 const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends;
Alexey Bataev8b427062016-05-25 12:36:08 +0000658 return llvm::make_range(Ref.begin(), Ref.end());
659 }
Alexey Bataev4b465392017-04-26 15:06:24 +0000660 return llvm::make_range(StackElem.DoacrossDepends.end(),
661 StackElem.DoacrossDepends.end());
Alexey Bataev8b427062016-05-25 12:36:08 +0000662 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000663};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000664bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
Alexey Bataev35aaee62016-04-13 13:36:48 +0000665 return isOpenMPParallelDirective(DKind) || isOpenMPTaskingDirective(DKind) ||
666 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000667}
Alexey Bataeve3727102018-04-18 15:57:46 +0000668
Alexey Bataeved09d242014-05-28 05:53:51 +0000669} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000670
Alexey Bataeve3727102018-04-18 15:57:46 +0000671static const Expr *getExprAsWritten(const Expr *E) {
Bill Wendling7c44da22018-10-31 03:48:47 +0000672 if (const auto *FE = dyn_cast<FullExpr>(E))
673 E = FE->getSubExpr();
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000674
Alexey Bataeve3727102018-04-18 15:57:46 +0000675 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000676 E = MTE->GetTemporaryExpr();
677
Alexey Bataeve3727102018-04-18 15:57:46 +0000678 while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000679 E = Binder->getSubExpr();
680
Alexey Bataeve3727102018-04-18 15:57:46 +0000681 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000682 E = ICE->getSubExprAsWritten();
683 return E->IgnoreParens();
684}
685
Alexey Bataeve3727102018-04-18 15:57:46 +0000686static Expr *getExprAsWritten(Expr *E) {
687 return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E)));
688}
689
690static const ValueDecl *getCanonicalDecl(const ValueDecl *D) {
691 if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D))
692 if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000693 D = ME->getMemberDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +0000694 const auto *VD = dyn_cast<VarDecl>(D);
695 const auto *FD = dyn_cast<FieldDecl>(D);
David Majnemer9d168222016-08-05 17:44:54 +0000696 if (VD != nullptr) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000697 VD = VD->getCanonicalDecl();
698 D = VD;
699 } else {
700 assert(FD);
701 FD = FD->getCanonicalDecl();
702 D = FD;
703 }
704 return D;
705}
706
Alexey Bataeve3727102018-04-18 15:57:46 +0000707static ValueDecl *getCanonicalDecl(ValueDecl *D) {
708 return const_cast<ValueDecl *>(
709 getCanonicalDecl(const_cast<const ValueDecl *>(D)));
710}
711
712DSAStackTy::DSAVarData DSAStackTy::getDSA(iterator &Iter,
713 ValueDecl *D) const {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000714 D = getCanonicalDecl(D);
715 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000716 const auto *FD = dyn_cast<FieldDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000717 DSAVarData DVar;
Alexey Bataev4b465392017-04-26 15:06:24 +0000718 if (isStackEmpty() || Iter == Stack.back().first.rend()) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000719 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
720 // in a region but not in construct]
721 // File-scope or namespace-scope variables referenced in called routines
722 // in the region are shared unless they appear in a threadprivate
723 // directive.
Alexey Bataeve3727102018-04-18 15:57:46 +0000724 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000725 DVar.CKind = OMPC_shared;
726
727 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
728 // in a region but not in construct]
729 // Variables with static storage duration that are declared in called
730 // routines in the region are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000731 if (VD && VD->hasGlobalStorage())
732 DVar.CKind = OMPC_shared;
733
734 // Non-static data members are shared by default.
735 if (FD)
Alexey Bataev750a58b2014-03-18 12:19:12 +0000736 DVar.CKind = OMPC_shared;
737
Alexey Bataev758e55e2013-09-06 18:03:48 +0000738 return DVar;
739 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000740
Alexey Bataevec3da872014-01-31 05:15:34 +0000741 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
742 // in a Construct, C/C++, predetermined, p.1]
743 // Variables with automatic storage duration that are declared in a scope
744 // inside the construct are private.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000745 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
746 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000747 DVar.CKind = OMPC_private;
748 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000749 }
750
Alexey Bataeveffbdf12017-07-21 17:24:30 +0000751 DVar.DKind = Iter->Directive;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000752 // Explicitly specified attributes and local variables with predetermined
753 // attributes.
754 if (Iter->SharingMap.count(D)) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000755 const DSAInfo &Data = Iter->SharingMap.lookup(D);
756 DVar.RefExpr = Data.RefExpr.getPointer();
757 DVar.PrivateCopy = Data.PrivateCopy;
758 DVar.CKind = Data.Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000759 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000760 return DVar;
761 }
762
763 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
764 // in a Construct, C/C++, implicitly determined, p.1]
765 // In a parallel or task construct, the data-sharing attributes of these
766 // variables are determined by the default clause, if present.
767 switch (Iter->DefaultAttr) {
768 case DSA_shared:
769 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000770 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000771 return DVar;
772 case DSA_none:
773 return DVar;
774 case DSA_unspecified:
775 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
776 // in a Construct, implicitly determined, p.2]
777 // In a parallel construct, if no default clause is present, these
778 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000779 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000780 if (isOpenMPParallelDirective(DVar.DKind) ||
781 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000782 DVar.CKind = OMPC_shared;
783 return DVar;
784 }
785
786 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
787 // in a Construct, implicitly determined, p.4]
788 // In a task construct, if no default clause is present, a variable that in
789 // the enclosing context is determined to be shared by all implicit tasks
790 // bound to the current team is shared.
Alexey Bataev35aaee62016-04-13 13:36:48 +0000791 if (isOpenMPTaskingDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000792 DSAVarData DVarTemp;
Alexey Bataeve3727102018-04-18 15:57:46 +0000793 iterator I = Iter, E = Stack.back().first.rend();
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000794 do {
795 ++I;
Alexey Bataeved09d242014-05-28 05:53:51 +0000796 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
Alexey Bataev35aaee62016-04-13 13:36:48 +0000797 // Referenced in a Construct, implicitly determined, p.6]
Alexey Bataev758e55e2013-09-06 18:03:48 +0000798 // In a task construct, if no default clause is present, a variable
799 // whose data-sharing attribute is not determined by the rules above is
800 // firstprivate.
801 DVarTemp = getDSA(I, D);
802 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000803 DVar.RefExpr = nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000804 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000805 return DVar;
806 }
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000807 } while (I != E && !isParallelOrTaskRegion(I->Directive));
Alexey Bataev758e55e2013-09-06 18:03:48 +0000808 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000809 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000810 return DVar;
811 }
812 }
813 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
814 // in a Construct, implicitly determined, p.3]
815 // For constructs other than task, if no default clause is present, these
816 // variables inherit their data-sharing attributes from the enclosing
817 // context.
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000818 return getDSA(++Iter, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000819}
820
Alexey Bataeve3727102018-04-18 15:57:46 +0000821const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D,
822 const Expr *NewDE) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000823 assert(!isStackEmpty() && "Data sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000824 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000825 SharingMapTy &StackElem = Stack.back().first.back();
Alexey Bataev4b465392017-04-26 15:06:24 +0000826 auto It = StackElem.AlignedMap.find(D);
827 if (It == StackElem.AlignedMap.end()) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000828 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
Alexey Bataev4b465392017-04-26 15:06:24 +0000829 StackElem.AlignedMap[D] = NewDE;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000830 return nullptr;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000831 }
Alexey Bataeve3727102018-04-18 15:57:46 +0000832 assert(It->second && "Unexpected nullptr expr in the aligned map");
833 return It->second;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000834}
835
Alexey Bataeve3727102018-04-18 15:57:46 +0000836void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000837 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000838 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000839 SharingMapTy &StackElem = Stack.back().first.back();
840 StackElem.LCVMap.try_emplace(
841 D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture));
Alexey Bataev9c821032015-04-30 04:23:23 +0000842}
843
Alexey Bataeve3727102018-04-18 15:57:46 +0000844const DSAStackTy::LCDeclInfo
845DSAStackTy::isLoopControlVariable(const ValueDecl *D) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000846 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000847 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000848 const SharingMapTy &StackElem = Stack.back().first.back();
Alexey Bataev4b465392017-04-26 15:06:24 +0000849 auto It = StackElem.LCVMap.find(D);
850 if (It != StackElem.LCVMap.end())
851 return It->second;
852 return {0, nullptr};
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000853}
854
Alexey Bataeve3727102018-04-18 15:57:46 +0000855const DSAStackTy::LCDeclInfo
856DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000857 assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
858 "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000859 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000860 const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
Alexey Bataev4b465392017-04-26 15:06:24 +0000861 auto It = StackElem.LCVMap.find(D);
862 if (It != StackElem.LCVMap.end())
863 return It->second;
864 return {0, nullptr};
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000865}
866
Alexey Bataeve3727102018-04-18 15:57:46 +0000867const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000868 assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
869 "Data-sharing attributes stack is empty");
Alexey Bataeve3727102018-04-18 15:57:46 +0000870 const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
Alexey Bataev4b465392017-04-26 15:06:24 +0000871 if (StackElem.LCVMap.size() < I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000872 return nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +0000873 for (const auto &Pair : StackElem.LCVMap)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000874 if (Pair.second.first == I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000875 return Pair.first;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000876 return nullptr;
Alexey Bataev9c821032015-04-30 04:23:23 +0000877}
878
Alexey Bataeve3727102018-04-18 15:57:46 +0000879void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
Alexey Bataev90c228f2016-02-08 09:29:13 +0000880 DeclRefExpr *PrivateCopy) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000881 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000882 if (A == OMPC_threadprivate) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000883 DSAInfo &Data = Threadprivates[D];
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000884 Data.Attributes = A;
885 Data.RefExpr.setPointer(E);
886 Data.PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000887 } else {
Alexey Bataev4b465392017-04-26 15:06:24 +0000888 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataeve3727102018-04-18 15:57:46 +0000889 DSAInfo &Data = Stack.back().first.back().SharingMap[D];
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000890 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
891 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
892 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
893 (isLoopControlVariable(D).first && A == OMPC_private));
894 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
895 Data.RefExpr.setInt(/*IntVal=*/true);
896 return;
897 }
898 const bool IsLastprivate =
899 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
900 Data.Attributes = A;
901 Data.RefExpr.setPointerAndInt(E, IsLastprivate);
902 Data.PrivateCopy = PrivateCopy;
903 if (PrivateCopy) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000904 DSAInfo &Data =
905 Stack.back().first.back().SharingMap[PrivateCopy->getDecl()];
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000906 Data.Attributes = A;
907 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
908 Data.PrivateCopy = nullptr;
909 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000910 }
911}
912
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000913/// Build a variable declaration for OpenMP loop iteration variable.
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000914static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev63cc8e92018-03-20 14:45:59 +0000915 StringRef Name, const AttrVec *Attrs = nullptr,
916 DeclRefExpr *OrigRef = nullptr) {
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000917 DeclContext *DC = SemaRef.CurContext;
918 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
919 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
Alexey Bataeve3727102018-04-18 15:57:46 +0000920 auto *Decl =
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000921 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
922 if (Attrs) {
923 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
924 I != E; ++I)
925 Decl->addAttr(*I);
926 }
927 Decl->setImplicit();
Alexey Bataev63cc8e92018-03-20 14:45:59 +0000928 if (OrigRef) {
929 Decl->addAttr(
930 OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef));
931 }
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000932 return Decl;
933}
934
935static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
936 SourceLocation Loc,
937 bool RefersToCapture = false) {
938 D->setReferenced();
939 D->markUsed(S.Context);
940 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
941 SourceLocation(), D, RefersToCapture, Loc, Ty,
942 VK_LValue);
943}
944
Alexey Bataeve3727102018-04-18 15:57:46 +0000945void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000946 BinaryOperatorKind BOK) {
Alexey Bataevfa312f32017-07-21 18:48:21 +0000947 D = getCanonicalDecl(D);
948 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataevfa312f32017-07-21 18:48:21 +0000949 assert(
Richard Trieu09f14112017-07-21 21:29:35 +0000950 Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000951 "Additional reduction info may be specified only for reduction items.");
Alexey Bataeve3727102018-04-18 15:57:46 +0000952 ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D];
Alexey Bataevfa312f32017-07-21 18:48:21 +0000953 assert(ReductionData.ReductionRange.isInvalid() &&
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000954 Stack.back().first.back().Directive == OMPD_taskgroup &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000955 "Additional reduction info may be specified only once for reduction "
956 "items.");
957 ReductionData.set(BOK, SR);
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000958 Expr *&TaskgroupReductionRef =
959 Stack.back().first.back().TaskgroupReductionRef;
960 if (!TaskgroupReductionRef) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000961 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
962 SemaRef.Context.VoidPtrTy, ".task_red.");
Alexey Bataevd070a582017-10-25 15:54:04 +0000963 TaskgroupReductionRef =
964 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000965 }
Alexey Bataevfa312f32017-07-21 18:48:21 +0000966}
967
Alexey Bataeve3727102018-04-18 15:57:46 +0000968void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000969 const Expr *ReductionRef) {
Alexey Bataevfa312f32017-07-21 18:48:21 +0000970 D = getCanonicalDecl(D);
971 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataevfa312f32017-07-21 18:48:21 +0000972 assert(
Richard Trieu09f14112017-07-21 21:29:35 +0000973 Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000974 "Additional reduction info may be specified only for reduction items.");
Alexey Bataeve3727102018-04-18 15:57:46 +0000975 ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D];
Alexey Bataevfa312f32017-07-21 18:48:21 +0000976 assert(ReductionData.ReductionRange.isInvalid() &&
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000977 Stack.back().first.back().Directive == OMPD_taskgroup &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000978 "Additional reduction info may be specified only once for reduction "
979 "items.");
980 ReductionData.set(ReductionRef, SR);
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000981 Expr *&TaskgroupReductionRef =
982 Stack.back().first.back().TaskgroupReductionRef;
983 if (!TaskgroupReductionRef) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000984 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
985 SemaRef.Context.VoidPtrTy, ".task_red.");
Alexey Bataevd070a582017-10-25 15:54:04 +0000986 TaskgroupReductionRef =
987 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000988 }
Alexey Bataevfa312f32017-07-21 18:48:21 +0000989}
990
Alexey Bataeve3727102018-04-18 15:57:46 +0000991const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
992 const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK,
993 Expr *&TaskgroupDescriptor) const {
Alexey Bataevfa312f32017-07-21 18:48:21 +0000994 D = getCanonicalDecl(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +0000995 assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
996 if (Stack.back().first.empty())
997 return DSAVarData();
Alexey Bataeve3727102018-04-18 15:57:46 +0000998 for (iterator I = std::next(Stack.back().first.rbegin(), 1),
999 E = Stack.back().first.rend();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001000 I != E; std::advance(I, 1)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001001 const DSAInfo &Data = I->SharingMap.lookup(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +00001002 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
Alexey Bataevfa312f32017-07-21 18:48:21 +00001003 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00001004 const ReductionData &ReductionData = I->ReductionMap.lookup(D);
Alexey Bataevfa312f32017-07-21 18:48:21 +00001005 if (!ReductionData.ReductionOp ||
1006 ReductionData.ReductionOp.is<const Expr *>())
Alexey Bataevf189cb72017-07-24 14:52:13 +00001007 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001008 SR = ReductionData.ReductionRange;
Alexey Bataevf87fa882017-07-21 19:26:22 +00001009 BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>();
Alexey Bataev88202be2017-07-27 13:20:36 +00001010 assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1011 "expression for the descriptor is not "
1012 "set.");
1013 TaskgroupDescriptor = I->TaskgroupReductionRef;
Alexey Bataevf189cb72017-07-24 14:52:13 +00001014 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
1015 Data.PrivateCopy, I->DefaultAttrLoc);
Alexey Bataevfa312f32017-07-21 18:48:21 +00001016 }
Alexey Bataevf189cb72017-07-24 14:52:13 +00001017 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001018}
1019
Alexey Bataeve3727102018-04-18 15:57:46 +00001020const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1021 const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef,
1022 Expr *&TaskgroupDescriptor) const {
Alexey Bataevfa312f32017-07-21 18:48:21 +00001023 D = getCanonicalDecl(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +00001024 assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1025 if (Stack.back().first.empty())
1026 return DSAVarData();
Alexey Bataeve3727102018-04-18 15:57:46 +00001027 for (iterator I = std::next(Stack.back().first.rbegin(), 1),
1028 E = Stack.back().first.rend();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001029 I != E; std::advance(I, 1)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001030 const DSAInfo &Data = I->SharingMap.lookup(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +00001031 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
Alexey Bataevfa312f32017-07-21 18:48:21 +00001032 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00001033 const ReductionData &ReductionData = I->ReductionMap.lookup(D);
Alexey Bataevfa312f32017-07-21 18:48:21 +00001034 if (!ReductionData.ReductionOp ||
1035 !ReductionData.ReductionOp.is<const Expr *>())
Alexey Bataevf189cb72017-07-24 14:52:13 +00001036 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001037 SR = ReductionData.ReductionRange;
1038 ReductionRef = ReductionData.ReductionOp.get<const Expr *>();
Alexey Bataev88202be2017-07-27 13:20:36 +00001039 assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1040 "expression for the descriptor is not "
1041 "set.");
1042 TaskgroupDescriptor = I->TaskgroupReductionRef;
Alexey Bataevf189cb72017-07-24 14:52:13 +00001043 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
1044 Data.PrivateCopy, I->DefaultAttrLoc);
Alexey Bataevfa312f32017-07-21 18:48:21 +00001045 }
Alexey Bataevf189cb72017-07-24 14:52:13 +00001046 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001047}
1048
Alexey Bataeve3727102018-04-18 15:57:46 +00001049bool DSAStackTy::isOpenMPLocal(VarDecl *D, iterator Iter) const {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001050 D = D->getCanonicalDecl();
Alexey Bataev852525d2018-03-02 17:17:12 +00001051 if (!isStackEmpty()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001052 iterator I = Iter, E = Stack.back().first.rend();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001053 Scope *TopScope = nullptr;
Alexey Bataev852525d2018-03-02 17:17:12 +00001054 while (I != E && !isParallelOrTaskRegion(I->Directive) &&
1055 !isOpenMPTargetExecutionDirective(I->Directive))
Alexey Bataevec3da872014-01-31 05:15:34 +00001056 ++I;
Alexey Bataeved09d242014-05-28 05:53:51 +00001057 if (I == E)
1058 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001059 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001060 Scope *CurScope = getCurScope();
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001061 while (CurScope != TopScope && !CurScope->isDeclScope(D))
Alexey Bataev758e55e2013-09-06 18:03:48 +00001062 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +00001063 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001064 }
Alexey Bataevec3da872014-01-31 05:15:34 +00001065 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001066}
1067
Alexey Bataeve3727102018-04-18 15:57:46 +00001068const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
1069 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001070 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001071 DSAVarData DVar;
1072
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001073 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001074 auto TI = Threadprivates.find(D);
1075 if (TI != Threadprivates.end()) {
1076 DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001077 DVar.CKind = OMPC_threadprivate;
1078 return DVar;
Alexey Bataeve3727102018-04-18 15:57:46 +00001079 }
1080 if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
Alexey Bataev817d7f32017-11-14 21:01:01 +00001081 DVar.RefExpr = buildDeclRefExpr(
1082 SemaRef, VD, D->getType().getNonReferenceType(),
1083 VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation());
1084 DVar.CKind = OMPC_threadprivate;
1085 addDSA(D, DVar.RefExpr, OMPC_threadprivate);
Alexey Bataev852525d2018-03-02 17:17:12 +00001086 return DVar;
1087 }
1088 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1089 // in a Construct, C/C++, predetermined, p.1]
1090 // Variables appearing in threadprivate directives are threadprivate.
1091 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
1092 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1093 SemaRef.getLangOpts().OpenMPUseTLS &&
1094 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
1095 (VD && VD->getStorageClass() == SC_Register &&
1096 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
1097 DVar.RefExpr = buildDeclRefExpr(
1098 SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation());
1099 DVar.CKind = OMPC_threadprivate;
1100 addDSA(D, DVar.RefExpr, OMPC_threadprivate);
1101 return DVar;
1102 }
1103 if (SemaRef.getLangOpts().OpenMPCUDAMode && VD &&
1104 VD->isLocalVarDeclOrParm() && !isStackEmpty() &&
1105 !isLoopControlVariable(D).first) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001106 iterator IterTarget =
Alexey Bataev852525d2018-03-02 17:17:12 +00001107 std::find_if(Stack.back().first.rbegin(), Stack.back().first.rend(),
1108 [](const SharingMapTy &Data) {
1109 return isOpenMPTargetExecutionDirective(Data.Directive);
1110 });
1111 if (IterTarget != Stack.back().first.rend()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001112 iterator ParentIterTarget = std::next(IterTarget, 1);
1113 for (iterator Iter = Stack.back().first.rbegin();
1114 Iter != ParentIterTarget; std::advance(Iter, 1)) {
Alexey Bataev852525d2018-03-02 17:17:12 +00001115 if (isOpenMPLocal(VD, Iter)) {
1116 DVar.RefExpr =
1117 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1118 D->getLocation());
1119 DVar.CKind = OMPC_threadprivate;
1120 return DVar;
1121 }
Alexey Bataev852525d2018-03-02 17:17:12 +00001122 }
1123 if (!isClauseParsingMode() || IterTarget != Stack.back().first.rbegin()) {
1124 auto DSAIter = IterTarget->SharingMap.find(D);
1125 if (DSAIter != IterTarget->SharingMap.end() &&
1126 isOpenMPPrivate(DSAIter->getSecond().Attributes)) {
1127 DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer();
1128 DVar.CKind = OMPC_threadprivate;
1129 return DVar;
Alexey Bataeve3727102018-04-18 15:57:46 +00001130 }
1131 iterator End = Stack.back().first.rend();
1132 if (!SemaRef.isOpenMPCapturedByRef(
1133 D, std::distance(ParentIterTarget, End))) {
Alexey Bataev852525d2018-03-02 17:17:12 +00001134 DVar.RefExpr =
1135 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1136 IterTarget->ConstructLoc);
1137 DVar.CKind = OMPC_threadprivate;
1138 return DVar;
1139 }
1140 }
1141 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001142 }
1143
Alexey Bataev4b465392017-04-26 15:06:24 +00001144 if (isStackEmpty())
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001145 // Not in OpenMP execution region and top scope was already checked.
1146 return DVar;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001147
Alexey Bataev758e55e2013-09-06 18:03:48 +00001148 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +00001149 // in a Construct, C/C++, predetermined, p.4]
1150 // Static data members are shared.
1151 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1152 // in a Construct, C/C++, predetermined, p.7]
1153 // Variables with static storage duration that are declared in a scope
1154 // inside the construct are shared.
Alexey Bataeve3727102018-04-18 15:57:46 +00001155 auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; };
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001156 if (VD && VD->isStaticDataMember()) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001157 DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent);
Alexey Bataevdffa93a2015-12-10 08:20:58 +00001158 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +00001159 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001160
Alexey Bataevdffa93a2015-12-10 08:20:58 +00001161 DVar.CKind = OMPC_shared;
1162 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001163 }
1164
1165 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00001166 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
1167 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001168 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1169 // in a Construct, C/C++, predetermined, p.6]
1170 // Variables with const qualified type having no mutable member are
1171 // shared.
Alexey Bataeve3727102018-04-18 15:57:46 +00001172 const CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +00001173 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +00001174 if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
1175 if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate())
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00001176 RD = CTD->getTemplatedDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001177 if (IsConstant &&
Alexey Bataev4bcad7f2016-02-10 10:50:12 +00001178 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
1179 RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001180 // Variables with const-qualified type having no mutable member may be
1181 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataeve3727102018-04-18 15:57:46 +00001182 DSAVarData DVarTemp =
1183 hasDSA(D, [](OpenMPClauseKind C) { return C == OMPC_firstprivate; },
1184 MatchesAlways, FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001185 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
Alexey Bataev9a757382018-02-16 19:16:54 +00001186 return DVarTemp;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001187
Alexey Bataev758e55e2013-09-06 18:03:48 +00001188 DVar.CKind = OMPC_shared;
1189 return DVar;
1190 }
1191
Alexey Bataev758e55e2013-09-06 18:03:48 +00001192 // Explicitly specified attributes and local variables with predetermined
1193 // attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +00001194 iterator I = Stack.back().first.rbegin();
1195 iterator EndI = Stack.back().first.rend();
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001196 if (FromParent && I != EndI)
1197 std::advance(I, 1);
Alexey Bataeve3727102018-04-18 15:57:46 +00001198 auto It = I->SharingMap.find(D);
1199 if (It != I->SharingMap.end()) {
1200 const DSAInfo &Data = It->getSecond();
1201 DVar.RefExpr = Data.RefExpr.getPointer();
1202 DVar.PrivateCopy = Data.PrivateCopy;
1203 DVar.CKind = Data.Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001204 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev4d4624c2017-07-20 16:47:47 +00001205 DVar.DKind = I->Directive;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001206 }
1207
1208 return DVar;
1209}
1210
Alexey Bataeve3727102018-04-18 15:57:46 +00001211const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1212 bool FromParent) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001213 if (isStackEmpty()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001214 iterator I;
Alexey Bataev4b465392017-04-26 15:06:24 +00001215 return getDSA(I, D);
1216 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001217 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +00001218 iterator StartI = Stack.back().first.rbegin();
1219 iterator EndI = Stack.back().first.rend();
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001220 if (FromParent && StartI != EndI)
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001221 std::advance(StartI, 1);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001222 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001223}
1224
Alexey Bataeve3727102018-04-18 15:57:46 +00001225const DSAStackTy::DSAVarData
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001226DSAStackTy::hasDSA(ValueDecl *D,
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001227 const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1228 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001229 bool FromParent) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001230 if (isStackEmpty())
1231 return {};
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001232 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +00001233 iterator I = Stack.back().first.rbegin();
1234 iterator EndI = Stack.back().first.rend();
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001235 if (FromParent && I != EndI)
Alexey Bataev0e6fc1c2017-04-27 14:46:26 +00001236 std::advance(I, 1);
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001237 for (; I != EndI; std::advance(I, 1)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001238 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +00001239 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00001240 iterator NewI = I;
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001241 DSAVarData DVar = getDSA(NewI, D);
1242 if (I == NewI && CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001243 return DVar;
Alexey Bataev60859c02017-04-27 15:10:33 +00001244 }
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001245 return {};
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001246}
1247
Alexey Bataeve3727102018-04-18 15:57:46 +00001248const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001249 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1250 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001251 bool FromParent) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001252 if (isStackEmpty())
1253 return {};
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001254 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +00001255 iterator StartI = Stack.back().first.rbegin();
1256 iterator EndI = Stack.back().first.rend();
Alexey Bataeve3978122016-07-19 05:06:39 +00001257 if (FromParent && StartI != EndI)
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001258 std::advance(StartI, 1);
Alexey Bataeve3978122016-07-19 05:06:39 +00001259 if (StartI == EndI || !DPred(StartI->Directive))
Alexey Bataev4b465392017-04-26 15:06:24 +00001260 return {};
Alexey Bataeve3727102018-04-18 15:57:46 +00001261 iterator NewI = StartI;
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001262 DSAVarData DVar = getDSA(NewI, D);
1263 return (NewI == StartI && CPred(DVar.CKind)) ? DVar : DSAVarData();
Alexey Bataevc5e02582014-06-16 07:08:35 +00001264}
1265
Alexey Bataevaac108a2015-06-23 04:51:00 +00001266bool DSAStackTy::hasExplicitDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00001267 const ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1268 unsigned Level, bool NotLastprivate) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001269 if (isStackEmpty())
1270 return false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001271 D = getCanonicalDecl(D);
Alexey Bataev4b465392017-04-26 15:06:24 +00001272 auto StartI = Stack.back().first.begin();
1273 auto EndI = Stack.back().first.end();
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +00001274 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +00001275 return false;
1276 std::advance(StartI, Level);
Alexey Bataeve3727102018-04-18 15:57:46 +00001277 auto I = StartI->SharingMap.find(D);
1278 return (I != StartI->SharingMap.end()) &&
1279 I->getSecond().RefExpr.getPointer() &&
1280 CPred(I->getSecond().Attributes) &&
1281 (!NotLastprivate || !I->getSecond().RefExpr.getInt());
Alexey Bataevaac108a2015-06-23 04:51:00 +00001282}
1283
Samuel Antao4be30e92015-10-02 17:14:03 +00001284bool DSAStackTy::hasExplicitDirective(
Alexey Bataeve3727102018-04-18 15:57:46 +00001285 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1286 unsigned Level) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001287 if (isStackEmpty())
1288 return false;
1289 auto StartI = Stack.back().first.begin();
1290 auto EndI = Stack.back().first.end();
Samuel Antao4be30e92015-10-02 17:14:03 +00001291 if (std::distance(StartI, EndI) <= (int)Level)
1292 return false;
1293 std::advance(StartI, Level);
1294 return DPred(StartI->Directive);
1295}
1296
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001297bool DSAStackTy::hasDirective(
1298 const llvm::function_ref<bool(OpenMPDirectiveKind,
1299 const DeclarationNameInfo &, SourceLocation)>
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001300 DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001301 bool FromParent) const {
Samuel Antaof0d79752016-05-27 15:21:27 +00001302 // We look only in the enclosing region.
Alexey Bataev4b465392017-04-26 15:06:24 +00001303 if (isStackEmpty())
Samuel Antaof0d79752016-05-27 15:21:27 +00001304 return false;
Alexey Bataev4b465392017-04-26 15:06:24 +00001305 auto StartI = std::next(Stack.back().first.rbegin());
1306 auto EndI = Stack.back().first.rend();
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001307 if (FromParent && StartI != EndI)
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001308 StartI = std::next(StartI);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001309 for (auto I = StartI, EE = EndI; I != EE; ++I) {
1310 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
1311 return true;
1312 }
1313 return false;
1314}
1315
Alexey Bataev758e55e2013-09-06 18:03:48 +00001316void Sema::InitDataSharingAttributesStack() {
1317 VarDataSharingAttributesStack = new DSAStackTy(*this);
1318}
1319
1320#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
1321
Alexey Bataev4b465392017-04-26 15:06:24 +00001322void Sema::pushOpenMPFunctionRegion() {
1323 DSAStack->pushFunction();
1324}
1325
1326void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) {
1327 DSAStack->popFunction(OldFSI);
1328}
1329
Alexey Bataeve3727102018-04-18 15:57:46 +00001330bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level) const {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001331 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1332
Alexey Bataeve3727102018-04-18 15:57:46 +00001333 ASTContext &Ctx = getASTContext();
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001334 bool IsByRef = true;
1335
1336 // Find the directive that is associated with the provided scope.
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00001337 D = cast<ValueDecl>(D->getCanonicalDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00001338 QualType Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001339
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001340 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001341 // This table summarizes how a given variable should be passed to the device
1342 // given its type and the clauses where it appears. This table is based on
1343 // the description in OpenMP 4.5 [2.10.4, target Construct] and
1344 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
1345 //
1346 // =========================================================================
1347 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
1348 // | |(tofrom:scalar)| | pvt | | | |
1349 // =========================================================================
1350 // | scl | | | | - | | bycopy|
1351 // | scl | | - | x | - | - | bycopy|
1352 // | scl | | x | - | - | - | null |
1353 // | scl | x | | | - | | byref |
1354 // | scl | x | - | x | - | - | bycopy|
1355 // | scl | x | x | - | - | - | null |
1356 // | scl | | - | - | - | x | byref |
1357 // | scl | x | - | - | - | x | byref |
1358 //
1359 // | agg | n.a. | | | - | | byref |
1360 // | agg | n.a. | - | x | - | - | byref |
1361 // | agg | n.a. | x | - | - | - | null |
1362 // | agg | n.a. | - | - | - | x | byref |
1363 // | agg | n.a. | - | - | - | x[] | byref |
1364 //
1365 // | ptr | n.a. | | | - | | bycopy|
1366 // | ptr | n.a. | - | x | - | - | bycopy|
1367 // | ptr | n.a. | x | - | - | - | null |
1368 // | ptr | n.a. | - | - | - | x | byref |
1369 // | ptr | n.a. | - | - | - | x[] | bycopy|
1370 // | ptr | n.a. | - | - | x | | bycopy|
1371 // | ptr | n.a. | - | - | x | x | bycopy|
1372 // | ptr | n.a. | - | - | x | x[] | bycopy|
1373 // =========================================================================
1374 // Legend:
1375 // scl - scalar
1376 // ptr - pointer
1377 // agg - aggregate
1378 // x - applies
1379 // - - invalid in this combination
1380 // [] - mapped with an array section
1381 // byref - should be mapped by reference
1382 // byval - should be mapped by value
1383 // null - initialize a local variable to null on the device
1384 //
1385 // Observations:
1386 // - All scalar declarations that show up in a map clause have to be passed
1387 // by reference, because they may have been mapped in the enclosing data
1388 // environment.
1389 // - If the scalar value does not fit the size of uintptr, it has to be
1390 // passed by reference, regardless the result in the table above.
1391 // - For pointers mapped by value that have either an implicit map or an
1392 // array section, the runtime library may pass the NULL value to the
1393 // device instead of the value passed to it by the compiler.
1394
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001395 if (Ty->isReferenceType())
1396 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
Samuel Antao86ace552016-04-27 22:40:57 +00001397
1398 // Locate map clauses and see if the variable being captured is referred to
1399 // in any of those clauses. Here we only care about variables, not fields,
1400 // because fields are part of aggregates.
1401 bool IsVariableUsedInMapClause = false;
1402 bool IsVariableAssociatedWithSection = false;
1403
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +00001404 DSAStack->checkMappableExprComponentListsForDeclAtLevel(
Alexey Bataeve3727102018-04-18 15:57:46 +00001405 D, Level,
1406 [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D](
1407 OMPClauseMappableExprCommon::MappableExprComponentListRef
Samuel Antao6890b092016-07-28 14:25:09 +00001408 MapExprComponents,
1409 OpenMPClauseKind WhereFoundClauseKind) {
1410 // Only the map clause information influences how a variable is
1411 // captured. E.g. is_device_ptr does not require changing the default
Samuel Antao4c8035b2016-12-12 18:00:20 +00001412 // behavior.
Samuel Antao6890b092016-07-28 14:25:09 +00001413 if (WhereFoundClauseKind != OMPC_map)
1414 return false;
Samuel Antao86ace552016-04-27 22:40:57 +00001415
1416 auto EI = MapExprComponents.rbegin();
1417 auto EE = MapExprComponents.rend();
1418
1419 assert(EI != EE && "Invalid map expression!");
1420
1421 if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
1422 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
1423
1424 ++EI;
1425 if (EI == EE)
1426 return false;
1427
1428 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
1429 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
1430 isa<MemberExpr>(EI->getAssociatedExpression())) {
1431 IsVariableAssociatedWithSection = true;
1432 // There is nothing more we need to know about this variable.
1433 return true;
1434 }
1435
1436 // Keep looking for more map info.
1437 return false;
1438 });
1439
1440 if (IsVariableUsedInMapClause) {
1441 // If variable is identified in a map clause it is always captured by
1442 // reference except if it is a pointer that is dereferenced somehow.
1443 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
1444 } else {
Alexey Bataev3f96fe62017-12-13 17:31:39 +00001445 // By default, all the data that has a scalar type is mapped by copy
1446 // (except for reduction variables).
1447 IsByRef =
Alexey Bataev60705422018-10-30 15:50:12 +00001448 (DSAStack->isForceCaptureByReferenceInTargetExecutable() &&
1449 !Ty->isAnyPointerType()) ||
Alexey Bataev3f96fe62017-12-13 17:31:39 +00001450 !Ty->isScalarType() ||
1451 DSAStack->getDefaultDMAAtLevel(Level) == DMA_tofrom_scalar ||
1452 DSAStack->hasExplicitDSA(
1453 D, [](OpenMPClauseKind K) { return K == OMPC_reduction; }, Level);
Samuel Antao86ace552016-04-27 22:40:57 +00001454 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001455 }
1456
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001457 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00001458 IsByRef =
Alexey Bataev60705422018-10-30 15:50:12 +00001459 ((DSAStack->isForceCaptureByReferenceInTargetExecutable() &&
1460 !Ty->isAnyPointerType()) ||
1461 !DSAStack->hasExplicitDSA(
1462 D,
1463 [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
1464 Level, /*NotLastprivate=*/true)) &&
Alexey Bataev8e769ee2017-12-22 21:01:52 +00001465 // If the variable is artificial and must be captured by value - try to
1466 // capture by value.
Alexey Bataevd2202ca2017-12-27 17:58:32 +00001467 !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() &&
1468 !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue());
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001469 }
1470
Samuel Antao86ace552016-04-27 22:40:57 +00001471 // When passing data by copy, we need to make sure it fits the uintptr size
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001472 // and alignment, because the runtime library only deals with uintptr types.
1473 // If it does not fit the uintptr size, we need to pass the data by reference
1474 // instead.
1475 if (!IsByRef &&
1476 (Ctx.getTypeSizeInChars(Ty) >
1477 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001478 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001479 IsByRef = true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001480 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001481
1482 return IsByRef;
1483}
1484
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001485unsigned Sema::getOpenMPNestingLevel() const {
1486 assert(getLangOpts().OpenMP);
1487 return DSAStack->getNestingLevel();
1488}
1489
Jonas Hahnfeld87d44262017-11-18 21:00:46 +00001490bool Sema::isInOpenMPTargetExecutionDirective() const {
1491 return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) &&
1492 !DSAStack->isClauseParsingMode()) ||
1493 DSAStack->hasDirective(
1494 [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
1495 SourceLocation) -> bool {
1496 return isOpenMPTargetExecutionDirective(K);
1497 },
1498 false);
1499}
1500
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001501VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +00001502 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001503 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +00001504
1505 // If we are attempting to capture a global variable in a directive with
1506 // 'target' we return true so that this global is also mapped to the device.
1507 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001508 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001509 if (VD && !VD->hasLocalStorage()) {
1510 if (isInOpenMPDeclareTargetContext() &&
1511 (getCurCapturedRegion() || getCurBlock() || getCurLambda())) {
1512 // Try to mark variable as declare target if it is used in capturing
1513 // regions.
Alexey Bataev97b72212018-08-14 18:31:20 +00001514 if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001515 checkDeclIsAllowedInOpenMPTarget(nullptr, VD);
Alexey Bataev92327c52018-03-26 16:40:55 +00001516 return nullptr;
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001517 } else if (isInOpenMPTargetExecutionDirective()) {
1518 // If the declaration is enclosed in a 'declare target' directive,
1519 // then it should not be captured.
1520 //
Alexey Bataev97b72212018-08-14 18:31:20 +00001521 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001522 return nullptr;
1523 return VD;
1524 }
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001525 }
Alexey Bataev60705422018-10-30 15:50:12 +00001526 // Capture variables captured by reference in lambdas for target-based
1527 // directives.
1528 if (VD && !DSAStack->isClauseParsingMode()) {
1529 if (const auto *RD = VD->getType()
1530 .getCanonicalType()
1531 .getNonReferenceType()
1532 ->getAsCXXRecordDecl()) {
1533 bool SavedForceCaptureByReferenceInTargetExecutable =
1534 DSAStack->isForceCaptureByReferenceInTargetExecutable();
1535 DSAStack->setForceCaptureByReferenceInTargetExecutable(/*V=*/true);
1536 if (RD->isLambda())
1537 for (const LambdaCapture &LC : RD->captures()) {
1538 if (LC.getCaptureKind() == LCK_ByRef) {
1539 VarDecl *VD = LC.getCapturedVar();
1540 DeclContext *VDC = VD->getDeclContext();
1541 if (!VDC->Encloses(CurContext))
1542 continue;
1543 DSAStackTy::DSAVarData DVarPrivate =
1544 DSAStack->getTopDSA(VD, /*FromParent=*/false);
1545 // Do not capture already captured variables.
1546 if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) &&
1547 DVarPrivate.CKind == OMPC_unknown &&
1548 !DSAStack->checkMappableExprComponentListsForDecl(
1549 D, /*CurrentRegionOnly=*/true,
1550 [](OMPClauseMappableExprCommon::
1551 MappableExprComponentListRef,
1552 OpenMPClauseKind) { return true; }))
1553 MarkVariableReferenced(LC.getLocation(), LC.getCapturedVar());
1554 } else if (LC.getCaptureKind() == LCK_This) {
1555 CheckCXXThisCapture(LC.getLocation());
1556 }
1557 }
1558 DSAStack->setForceCaptureByReferenceInTargetExecutable(
1559 SavedForceCaptureByReferenceInTargetExecutable);
1560 }
1561 }
Samuel Antao4be30e92015-10-02 17:14:03 +00001562
Alexey Bataev48977c32015-08-04 08:10:48 +00001563 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
1564 (!DSAStack->isClauseParsingMode() ||
1565 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001566 auto &&Info = DSAStack->isLoopControlVariable(D);
1567 if (Info.first ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001568 (VD && VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001569 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001570 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001571 return VD ? VD : Info.second;
Alexey Bataeve3727102018-04-18 15:57:46 +00001572 DSAStackTy::DSAVarData DVarPrivate =
1573 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001574 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +00001575 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00001576 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate,
1577 [](OpenMPDirectiveKind) { return true; },
1578 DSAStack->isClauseParsingMode());
Alexey Bataev90c228f2016-02-08 09:29:13 +00001579 if (DVarPrivate.CKind != OMPC_unknown)
1580 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001581 }
Alexey Bataev90c228f2016-02-08 09:29:13 +00001582 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +00001583}
1584
Alexey Bataevdfa430f2017-12-08 15:03:50 +00001585void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex,
1586 unsigned Level) const {
1587 SmallVector<OpenMPDirectiveKind, 4> Regions;
1588 getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level));
1589 FunctionScopesIndex -= Regions.size();
1590}
1591
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00001592void Sema::startOpenMPLoop() {
1593 assert(LangOpts.OpenMP && "OpenMP must be enabled.");
1594 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective()))
1595 DSAStack->loopInit();
1596}
1597
Alexey Bataeve3727102018-04-18 15:57:46 +00001598bool Sema::isOpenMPPrivateDecl(const ValueDecl *D, unsigned Level) const {
Alexey Bataevaac108a2015-06-23 04:51:00 +00001599 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00001600 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
1601 if (DSAStack->getAssociatedLoops() > 0 &&
1602 !DSAStack->isLoopStarted()) {
1603 DSAStack->resetPossibleLoopCounter(D);
1604 DSAStack->loopStart();
1605 return true;
1606 }
1607 if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() ||
1608 DSAStack->isLoopControlVariable(D).first) &&
1609 !DSAStack->hasExplicitDSA(
1610 D, [](OpenMPClauseKind K) { return K != OMPC_private; }, Level) &&
1611 !isOpenMPSimdDirective(DSAStack->getCurrentDirective()))
1612 return true;
1613 }
Alexey Bataevaac108a2015-06-23 04:51:00 +00001614 return DSAStack->hasExplicitDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00001615 D, [](OpenMPClauseKind K) { return K == OMPC_private; }, Level) ||
Alexey Bataev3f82cfc2017-12-13 15:28:44 +00001616 (DSAStack->isClauseParsingMode() &&
1617 DSAStack->getClauseParsingMode() == OMPC_private) ||
Alexey Bataev88202be2017-07-27 13:20:36 +00001618 // Consider taskgroup reduction descriptor variable a private to avoid
1619 // possible capture in the region.
1620 (DSAStack->hasExplicitDirective(
1621 [](OpenMPDirectiveKind K) { return K == OMPD_taskgroup; },
1622 Level) &&
1623 DSAStack->isTaskgroupReductionRef(D, Level));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001624}
1625
Alexey Bataeve3727102018-04-18 15:57:46 +00001626void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D,
1627 unsigned Level) {
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001628 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1629 D = getCanonicalDecl(D);
1630 OpenMPClauseKind OMPC = OMPC_unknown;
1631 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) {
1632 const unsigned NewLevel = I - 1;
1633 if (DSAStack->hasExplicitDSA(D,
1634 [&OMPC](const OpenMPClauseKind K) {
1635 if (isOpenMPPrivate(K)) {
1636 OMPC = K;
1637 return true;
1638 }
1639 return false;
1640 },
1641 NewLevel))
1642 break;
1643 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel(
1644 D, NewLevel,
1645 [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
1646 OpenMPClauseKind) { return true; })) {
1647 OMPC = OMPC_map;
1648 break;
1649 }
1650 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1651 NewLevel)) {
Alexey Bataev63cc8e92018-03-20 14:45:59 +00001652 OMPC = OMPC_map;
1653 if (D->getType()->isScalarType() &&
1654 DSAStack->getDefaultDMAAtLevel(NewLevel) !=
1655 DefaultMapAttributes::DMA_tofrom_scalar)
1656 OMPC = OMPC_firstprivate;
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001657 break;
1658 }
1659 }
1660 if (OMPC != OMPC_unknown)
1661 FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, OMPC));
1662}
1663
Alexey Bataeve3727102018-04-18 15:57:46 +00001664bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D,
1665 unsigned Level) const {
Samuel Antao4be30e92015-10-02 17:14:03 +00001666 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1667 // Return true if the current level is no longer enclosed in a target region.
1668
Alexey Bataeve3727102018-04-18 15:57:46 +00001669 const auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001670 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00001671 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1672 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +00001673}
1674
Alexey Bataeved09d242014-05-28 05:53:51 +00001675void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001676
1677void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
1678 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001679 Scope *CurScope, SourceLocation Loc) {
1680 DSAStack->push(DKind, DirName, CurScope, Loc);
Faisal Valid143a0c2017-04-01 21:30:49 +00001681 PushExpressionEvaluationContext(
1682 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001683}
1684
Alexey Bataevaac108a2015-06-23 04:51:00 +00001685void Sema::StartOpenMPClause(OpenMPClauseKind K) {
1686 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001687}
1688
Alexey Bataevaac108a2015-06-23 04:51:00 +00001689void Sema::EndOpenMPClause() {
1690 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001691}
1692
Alexey Bataev758e55e2013-09-06 18:03:48 +00001693void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001694 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
1695 // A variable of class type (or array thereof) that appears in a lastprivate
1696 // clause requires an accessible, unambiguous default constructor for the
1697 // class type, unless the list item is also specified in a firstprivate
1698 // clause.
Alexey Bataeve3727102018-04-18 15:57:46 +00001699 if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
1700 for (OMPClause *C : D->clauses()) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001701 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
1702 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeve3727102018-04-18 15:57:46 +00001703 for (Expr *DE : Clause->varlists()) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001704 if (DE->isValueDependent() || DE->isTypeDependent()) {
1705 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001706 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +00001707 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00001708 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
Alexey Bataeve3727102018-04-18 15:57:46 +00001709 auto *VD = cast<VarDecl>(DRE->getDecl());
Alexey Bataev005248a2016-02-25 05:25:57 +00001710 QualType Type = VD->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +00001711 const DSAStackTy::DSAVarData DVar =
1712 DSAStack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001713 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001714 // Generate helper private variable and initialize it with the
1715 // default value. The address of the original variable is replaced
1716 // by the address of the new private variable in CodeGen. This new
1717 // variable is not added to IdResolver, so the code in the OpenMP
1718 // region uses original variable for proper diagnostics.
Alexey Bataeve3727102018-04-18 15:57:46 +00001719 VarDecl *VDPrivate = buildVarDecl(
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001720 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev63cc8e92018-03-20 14:45:59 +00001721 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE);
Richard Smith3beb7c62017-01-12 02:27:38 +00001722 ActOnUninitializedDecl(VDPrivate);
Alexey Bataev38e89532015-04-16 04:54:05 +00001723 if (VDPrivate->isInvalidDecl())
1724 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001725 PrivateCopies.push_back(buildDeclRefExpr(
1726 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001727 } else {
1728 // The variable is also a firstprivate, so initialization sequence
1729 // for private copy is generated already.
1730 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001731 }
1732 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001733 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001734 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001735 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001736 }
1737 }
1738 }
1739
Alexey Bataev758e55e2013-09-06 18:03:48 +00001740 DSAStack->pop();
1741 DiscardCleanupsInEvaluationContext();
1742 PopExpressionEvaluationContext();
1743}
1744
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001745static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1746 Expr *NumIterations, Sema &SemaRef,
1747 Scope *S, DSAStackTy *Stack);
Alexander Musman3276a272015-03-21 10:12:56 +00001748
Alexey Bataeva769e072013-03-22 06:34:35 +00001749namespace {
1750
Alexey Bataeve3727102018-04-18 15:57:46 +00001751class VarDeclFilterCCC final : public CorrectionCandidateCallback {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001752private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001753 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001754
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001755public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001756 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001757 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001758 NamedDecl *ND = Candidate.getCorrectionDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +00001759 if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001760 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001761 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1762 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001763 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001764 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001765 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001766};
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001767
Alexey Bataeve3727102018-04-18 15:57:46 +00001768class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001769private:
1770 Sema &SemaRef;
1771
1772public:
1773 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
1774 bool ValidateCandidate(const TypoCorrection &Candidate) override {
1775 NamedDecl *ND = Candidate.getCorrectionDecl();
Kelvin Li59e3d192017-11-30 18:52:06 +00001776 if (ND && (isa<VarDecl>(ND) || isa<FunctionDecl>(ND))) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001777 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1778 SemaRef.getCurScope());
1779 }
1780 return false;
1781 }
1782};
1783
Alexey Bataeved09d242014-05-28 05:53:51 +00001784} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001785
1786ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1787 CXXScopeSpec &ScopeSpec,
1788 const DeclarationNameInfo &Id) {
1789 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1790 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1791
1792 if (Lookup.isAmbiguous())
1793 return ExprError();
1794
1795 VarDecl *VD;
1796 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001797 if (TypoCorrection Corrected = CorrectTypo(
1798 Id, LookupOrdinaryName, CurScope, nullptr,
1799 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001800 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001801 PDiag(Lookup.empty()
1802 ? diag::err_undeclared_var_use_suggest
1803 : diag::err_omp_expected_var_arg_suggest)
1804 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001805 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001806 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001807 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1808 : diag::err_omp_expected_var_arg)
1809 << Id.getName();
1810 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001811 }
Alexey Bataeve3727102018-04-18 15:57:46 +00001812 } else if (!(VD = Lookup.getAsSingle<VarDecl>())) {
1813 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
1814 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1815 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001816 }
1817 Lookup.suppressDiagnostics();
1818
1819 // OpenMP [2.9.2, Syntax, C/C++]
1820 // Variables must be file-scope, namespace-scope, or static block-scope.
1821 if (!VD->hasGlobalStorage()) {
1822 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001823 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1824 bool IsDecl =
1825 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001826 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001827 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1828 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001829 return ExprError();
1830 }
1831
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001832 VarDecl *CanonicalVD = VD->getCanonicalDecl();
George Burgess IV00f70bd2018-03-01 05:43:23 +00001833 NamedDecl *ND = CanonicalVD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001834 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1835 // A threadprivate directive for file-scope variables must appear outside
1836 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001837 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1838 !getCurLexicalContext()->isTranslationUnit()) {
1839 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001840 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1841 bool IsDecl =
1842 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1843 Diag(VD->getLocation(),
1844 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1845 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001846 return ExprError();
1847 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001848 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1849 // A threadprivate directive for static class member variables must appear
1850 // in the class definition, in the same scope in which the member
1851 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001852 if (CanonicalVD->isStaticDataMember() &&
1853 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1854 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001855 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1856 bool IsDecl =
1857 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1858 Diag(VD->getLocation(),
1859 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1860 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001861 return ExprError();
1862 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001863 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1864 // A threadprivate directive for namespace-scope variables must appear
1865 // outside any definition or declaration other than the namespace
1866 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001867 if (CanonicalVD->getDeclContext()->isNamespace() &&
1868 (!getCurLexicalContext()->isFileContext() ||
1869 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1870 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001871 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1872 bool IsDecl =
1873 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1874 Diag(VD->getLocation(),
1875 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1876 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001877 return ExprError();
1878 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001879 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1880 // A threadprivate directive for static block-scope variables must appear
1881 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001882 if (CanonicalVD->isStaticLocal() && CurScope &&
1883 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001884 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001885 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1886 bool IsDecl =
1887 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1888 Diag(VD->getLocation(),
1889 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1890 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001891 return ExprError();
1892 }
1893
1894 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1895 // A threadprivate directive must lexically precede all references to any
1896 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001897 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001898 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001899 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001900 return ExprError();
1901 }
1902
1903 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00001904 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1905 SourceLocation(), VD,
1906 /*RefersToEnclosingVariableOrCapture=*/false,
1907 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001908}
1909
Alexey Bataeved09d242014-05-28 05:53:51 +00001910Sema::DeclGroupPtrTy
1911Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1912 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001913 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001914 CurContext->addDecl(D);
1915 return DeclGroupPtrTy::make(DeclGroupRef(D));
1916 }
David Blaikie0403cb12016-01-15 23:43:25 +00001917 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001918}
1919
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001920namespace {
Alexey Bataeve3727102018-04-18 15:57:46 +00001921class LocalVarRefChecker final
1922 : public ConstStmtVisitor<LocalVarRefChecker, bool> {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001923 Sema &SemaRef;
1924
1925public:
1926 bool VisitDeclRefExpr(const DeclRefExpr *E) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001927 if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001928 if (VD->hasLocalStorage()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001929 SemaRef.Diag(E->getBeginLoc(),
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001930 diag::err_omp_local_var_in_threadprivate_init)
1931 << E->getSourceRange();
1932 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1933 << VD << VD->getSourceRange();
1934 return true;
1935 }
1936 }
1937 return false;
1938 }
1939 bool VisitStmt(const Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001940 for (const Stmt *Child : S->children()) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001941 if (Child && Visit(Child))
1942 return true;
1943 }
1944 return false;
1945 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001946 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001947};
1948} // namespace
1949
Alexey Bataeved09d242014-05-28 05:53:51 +00001950OMPThreadPrivateDecl *
1951Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001952 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +00001953 for (Expr *RefExpr : VarList) {
1954 auto *DE = cast<DeclRefExpr>(RefExpr);
1955 auto *VD = cast<VarDecl>(DE->getDecl());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001956 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001957
Alexey Bataev376b4a42016-02-09 09:41:09 +00001958 // Mark variable as used.
1959 VD->setReferenced();
1960 VD->markUsed(Context);
1961
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001962 QualType QType = VD->getType();
1963 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1964 // It will be analyzed later.
1965 Vars.push_back(DE);
1966 continue;
1967 }
1968
Alexey Bataeva769e072013-03-22 06:34:35 +00001969 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1970 // A threadprivate variable must not have an incomplete type.
1971 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001972 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001973 continue;
1974 }
1975
1976 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1977 // A threadprivate variable must not have a reference type.
1978 if (VD->getType()->isReferenceType()) {
1979 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001980 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1981 bool IsDecl =
1982 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1983 Diag(VD->getLocation(),
1984 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1985 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001986 continue;
1987 }
1988
Samuel Antaof8b50122015-07-13 22:54:53 +00001989 // Check if this is a TLS variable. If TLS is not being supported, produce
1990 // the corresponding diagnostic.
1991 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1992 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1993 getLangOpts().OpenMPUseTLS &&
1994 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001995 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1996 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001997 Diag(ILoc, diag::err_omp_var_thread_local)
1998 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001999 bool IsDecl =
2000 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2001 Diag(VD->getLocation(),
2002 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2003 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00002004 continue;
2005 }
2006
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002007 // Check if initial value of threadprivate variable reference variable with
2008 // local storage (it is not supported by runtime).
Alexey Bataeve3727102018-04-18 15:57:46 +00002009 if (const Expr *Init = VD->getAnyInitializer()) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002010 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002011 if (Checker.Visit(Init))
2012 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002013 }
2014
Alexey Bataeved09d242014-05-28 05:53:51 +00002015 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00002016 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00002017 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
2018 Context, SourceRange(Loc, Loc)));
Alexey Bataeve3727102018-04-18 15:57:46 +00002019 if (ASTMutationListener *ML = Context.getASTMutationListener())
Alexey Bataev97720002014-11-11 04:05:39 +00002020 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00002021 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002022 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00002023 if (!Vars.empty()) {
2024 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
2025 Vars);
2026 D->setAccess(AS_public);
2027 }
2028 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00002029}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002030
Kelvin Li1408f912018-09-26 04:28:39 +00002031Sema::DeclGroupPtrTy
2032Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc,
2033 ArrayRef<OMPClause *> ClauseList) {
2034 OMPRequiresDecl *D = nullptr;
2035 if (!CurContext->isFileContext()) {
2036 Diag(Loc, diag::err_omp_invalid_scope) << "requires";
2037 } else {
2038 D = CheckOMPRequiresDecl(Loc, ClauseList);
2039 if (D) {
2040 CurContext->addDecl(D);
2041 DSAStack->addRequiresDecl(D);
2042 }
2043 }
2044 return DeclGroupPtrTy::make(DeclGroupRef(D));
2045}
2046
2047OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc,
2048 ArrayRef<OMPClause *> ClauseList) {
2049 if (!DSAStack->hasDuplicateRequiresClause(ClauseList))
2050 return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc,
2051 ClauseList);
2052 return nullptr;
2053}
2054
Alexey Bataeve3727102018-04-18 15:57:46 +00002055static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
2056 const ValueDecl *D,
2057 const DSAStackTy::DSAVarData &DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00002058 bool IsLoopIterVar = false) {
2059 if (DVar.RefExpr) {
2060 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
2061 << getOpenMPClauseName(DVar.CKind);
2062 return;
2063 }
2064 enum {
2065 PDSA_StaticMemberShared,
2066 PDSA_StaticLocalVarShared,
2067 PDSA_LoopIterVarPrivate,
2068 PDSA_LoopIterVarLinear,
2069 PDSA_LoopIterVarLastprivate,
2070 PDSA_ConstVarShared,
2071 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002072 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00002073 PDSA_LocalVarPrivate,
2074 PDSA_Implicit
2075 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00002076 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002077 auto ReportLoc = D->getLocation();
2078 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002079 if (IsLoopIterVar) {
2080 if (DVar.CKind == OMPC_private)
2081 Reason = PDSA_LoopIterVarPrivate;
2082 else if (DVar.CKind == OMPC_lastprivate)
2083 Reason = PDSA_LoopIterVarLastprivate;
2084 else
2085 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev35aaee62016-04-13 13:36:48 +00002086 } else if (isOpenMPTaskingDirective(DVar.DKind) &&
2087 DVar.CKind == OMPC_firstprivate) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002088 Reason = PDSA_TaskVarFirstprivate;
2089 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002090 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00002091 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002092 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00002093 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002094 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00002095 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002096 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00002097 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002098 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00002099 ReportHint = true;
2100 Reason = PDSA_LocalVarPrivate;
2101 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00002102 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002103 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00002104 << Reason << ReportHint
2105 << getOpenMPDirectiveName(Stack->getCurrentDirective());
2106 } else if (DVar.ImplicitDSALoc.isValid()) {
2107 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
2108 << getOpenMPClauseName(DVar.CKind);
2109 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00002110}
2111
Alexey Bataev758e55e2013-09-06 18:03:48 +00002112namespace {
Alexey Bataeve3727102018-04-18 15:57:46 +00002113class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> {
Alexey Bataev758e55e2013-09-06 18:03:48 +00002114 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00002115 Sema &SemaRef;
Alexey Bataeve3727102018-04-18 15:57:46 +00002116 bool ErrorFound = false;
2117 CapturedStmt *CS = nullptr;
2118 llvm::SmallVector<Expr *, 4> ImplicitFirstprivate;
2119 llvm::SmallVector<Expr *, 4> ImplicitMap;
2120 Sema::VarsWithInheritedDSAType VarsWithInheritedDSA;
2121 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations;
Alexey Bataeved09d242014-05-28 05:53:51 +00002122
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00002123 void VisitSubCaptures(OMPExecutableDirective *S) {
2124 // Check implicitly captured variables.
2125 if (!S->hasAssociatedStmt() || !S->getAssociatedStmt())
2126 return;
2127 for (const CapturedStmt::Capture &Cap :
2128 S->getInnermostCapturedStmt()->captures()) {
2129 if (!Cap.capturesVariable())
2130 continue;
2131 VarDecl *VD = Cap.getCapturedVar();
2132 // Do not try to map the variable if it or its sub-component was mapped
2133 // already.
2134 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) &&
2135 Stack->checkMappableExprComponentListsForDecl(
2136 VD, /*CurrentRegionOnly=*/true,
2137 [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
2138 OpenMPClauseKind) { return true; }))
2139 continue;
2140 DeclRefExpr *DRE = buildDeclRefExpr(
2141 SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context),
2142 Cap.getLocation(), /*RefersToCapture=*/true);
2143 Visit(DRE);
2144 }
2145 }
2146
Alexey Bataev758e55e2013-09-06 18:03:48 +00002147public:
2148 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00002149 if (E->isTypeDependent() || E->isValueDependent() ||
2150 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
2151 return;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002152 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00002153 VD = VD->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002154 // Skip internally declared variables.
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002155 if (VD->hasLocalStorage() && !CS->capturesVariable(VD))
Alexey Bataeved09d242014-05-28 05:53:51 +00002156 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002157
Alexey Bataeve3727102018-04-18 15:57:46 +00002158 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002159 // Check if the variable has explicit DSA set and stop analysis if it so.
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00002160 if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second)
David Majnemer9d168222016-08-05 17:44:54 +00002161 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002162
Alexey Bataevafe50572017-10-06 17:00:28 +00002163 // Skip internally declared static variables.
Alexey Bataev92327c52018-03-26 16:40:55 +00002164 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
Alexey Bataev97b72212018-08-14 18:31:20 +00002165 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
Alexey Bataev92327c52018-03-26 16:40:55 +00002166 if (VD->hasGlobalStorage() && !CS->capturesVariable(VD) &&
2167 (!Res || *Res != OMPDeclareTargetDeclAttr::MT_Link))
Alexey Bataevafe50572017-10-06 17:00:28 +00002168 return;
2169
Alexey Bataeve3727102018-04-18 15:57:46 +00002170 SourceLocation ELoc = E->getExprLoc();
2171 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002172 // The default(none) clause requires that each variable that is referenced
2173 // in the construct, and does not have a predetermined data-sharing
2174 // attribute, must have its data-sharing attribute explicitly determined
2175 // by being listed in a data-sharing attribute clause.
2176 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002177 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00002178 VarsWithInheritedDSA.count(VD) == 0) {
2179 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002180 return;
2181 }
2182
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002183 if (isOpenMPTargetExecutionDirective(DKind) &&
2184 !Stack->isLoopControlVariable(VD).first) {
2185 if (!Stack->checkMappableExprComponentListsForDecl(
2186 VD, /*CurrentRegionOnly=*/true,
2187 [](OMPClauseMappableExprCommon::MappableExprComponentListRef
2188 StackComponents,
2189 OpenMPClauseKind) {
2190 // Variable is used if it has been marked as an array, array
2191 // section or the variable iself.
2192 return StackComponents.size() == 1 ||
2193 std::all_of(
2194 std::next(StackComponents.rbegin()),
2195 StackComponents.rend(),
2196 [](const OMPClauseMappableExprCommon::
2197 MappableComponent &MC) {
2198 return MC.getAssociatedDeclaration() ==
2199 nullptr &&
2200 (isa<OMPArraySectionExpr>(
2201 MC.getAssociatedExpression()) ||
2202 isa<ArraySubscriptExpr>(
2203 MC.getAssociatedExpression()));
2204 });
2205 })) {
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002206 bool IsFirstprivate = false;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002207 // By default lambdas are captured as firstprivates.
2208 if (const auto *RD =
2209 VD->getType().getNonReferenceType()->getAsCXXRecordDecl())
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002210 IsFirstprivate = RD->isLambda();
2211 IsFirstprivate =
2212 IsFirstprivate ||
2213 (VD->getType().getNonReferenceType()->isScalarType() &&
Alexey Bataev92327c52018-03-26 16:40:55 +00002214 Stack->getDefaultDMA() != DMA_tofrom_scalar && !Res);
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002215 if (IsFirstprivate)
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002216 ImplicitFirstprivate.emplace_back(E);
2217 else
2218 ImplicitMap.emplace_back(E);
2219 return;
2220 }
2221 }
2222
Alexey Bataev758e55e2013-09-06 18:03:48 +00002223 // OpenMP [2.9.3.6, Restrictions, p.2]
2224 // A list item that appears in a reduction clause of the innermost
2225 // enclosing worksharing or parallel construct may not be accessed in an
2226 // explicit task.
Alexey Bataev7ace49d2016-05-17 08:55:33 +00002227 DVar = Stack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00002228 VD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
2229 [](OpenMPDirectiveKind K) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00002230 return isOpenMPParallelDirective(K) ||
2231 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2232 },
Alexey Bataeveffbdf12017-07-21 17:24:30 +00002233 /*FromParent=*/true);
Alexey Bataev35aaee62016-04-13 13:36:48 +00002234 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00002235 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00002236 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
Alexey Bataeve3727102018-04-18 15:57:46 +00002237 reportOriginalDsa(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002238 return;
2239 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002240
2241 // Define implicit data-sharing attributes for task.
Alexey Bataeve3727102018-04-18 15:57:46 +00002242 DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00002243 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
2244 !Stack->isLoopControlVariable(VD).first)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002245 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002246 }
2247 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002248 void VisitMemberExpr(MemberExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00002249 if (E->isTypeDependent() || E->isValueDependent() ||
2250 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
2251 return;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002252 auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002253 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002254 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002255 if (!FD)
2256 return;
Alexey Bataeve3727102018-04-18 15:57:46 +00002257 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002258 // Check if the variable has explicit DSA set and stop analysis if it
2259 // so.
2260 if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second)
2261 return;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002262
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002263 if (isOpenMPTargetExecutionDirective(DKind) &&
2264 !Stack->isLoopControlVariable(FD).first &&
2265 !Stack->checkMappableExprComponentListsForDecl(
2266 FD, /*CurrentRegionOnly=*/true,
2267 [](OMPClauseMappableExprCommon::MappableExprComponentListRef
2268 StackComponents,
2269 OpenMPClauseKind) {
2270 return isa<CXXThisExpr>(
2271 cast<MemberExpr>(
2272 StackComponents.back().getAssociatedExpression())
2273 ->getBase()
2274 ->IgnoreParens());
2275 })) {
2276 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
2277 // A bit-field cannot appear in a map clause.
2278 //
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002279 if (FD->isBitField())
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002280 return;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002281 ImplicitMap.emplace_back(E);
2282 return;
2283 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002284
Alexey Bataeve3727102018-04-18 15:57:46 +00002285 SourceLocation ELoc = E->getExprLoc();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002286 // OpenMP [2.9.3.6, Restrictions, p.2]
2287 // A list item that appears in a reduction clause of the innermost
2288 // enclosing worksharing or parallel construct may not be accessed in
2289 // an explicit task.
2290 DVar = Stack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00002291 FD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
2292 [](OpenMPDirectiveKind K) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002293 return isOpenMPParallelDirective(K) ||
2294 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2295 },
2296 /*FromParent=*/true);
2297 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
2298 ErrorFound = true;
2299 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
Alexey Bataeve3727102018-04-18 15:57:46 +00002300 reportOriginalDsa(SemaRef, Stack, FD, DVar);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002301 return;
2302 }
2303
2304 // Define implicit data-sharing attributes for task.
Alexey Bataeve3727102018-04-18 15:57:46 +00002305 DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002306 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
Alexey Bataevb40e05202018-10-24 18:53:12 +00002307 !Stack->isLoopControlVariable(FD).first) {
2308 // Check if there is a captured expression for the current field in the
2309 // region. Do not mark it as firstprivate unless there is no captured
2310 // expression.
2311 // TODO: try to make it firstprivate.
2312 if (DVar.CKind != OMPC_unknown)
2313 ImplicitFirstprivate.push_back(E);
2314 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002315 return;
2316 }
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002317 if (isOpenMPTargetExecutionDirective(DKind)) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002318 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
Alexey Bataeve3727102018-04-18 15:57:46 +00002319 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map,
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002320 /*NoDiagnose=*/true))
Alexey Bataev27041fa2017-12-05 15:22:49 +00002321 return;
Alexey Bataeve3727102018-04-18 15:57:46 +00002322 const auto *VD = cast<ValueDecl>(
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002323 CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl());
2324 if (!Stack->checkMappableExprComponentListsForDecl(
2325 VD, /*CurrentRegionOnly=*/true,
2326 [&CurComponents](
2327 OMPClauseMappableExprCommon::MappableExprComponentListRef
2328 StackComponents,
2329 OpenMPClauseKind) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002330 auto CCI = CurComponents.rbegin();
Alexey Bataev5ec38932017-09-26 16:19:04 +00002331 auto CCE = CurComponents.rend();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002332 for (const auto &SC : llvm::reverse(StackComponents)) {
2333 // Do both expressions have the same kind?
2334 if (CCI->getAssociatedExpression()->getStmtClass() !=
2335 SC.getAssociatedExpression()->getStmtClass())
2336 if (!(isa<OMPArraySectionExpr>(
2337 SC.getAssociatedExpression()) &&
2338 isa<ArraySubscriptExpr>(
2339 CCI->getAssociatedExpression())))
2340 return false;
2341
Alexey Bataeve3727102018-04-18 15:57:46 +00002342 const Decl *CCD = CCI->getAssociatedDeclaration();
2343 const Decl *SCD = SC.getAssociatedDeclaration();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002344 CCD = CCD ? CCD->getCanonicalDecl() : nullptr;
2345 SCD = SCD ? SCD->getCanonicalDecl() : nullptr;
2346 if (SCD != CCD)
2347 return false;
2348 std::advance(CCI, 1);
Alexey Bataev5ec38932017-09-26 16:19:04 +00002349 if (CCI == CCE)
2350 break;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002351 }
2352 return true;
2353 })) {
2354 Visit(E->getBase());
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002355 }
Alexey Bataeve3727102018-04-18 15:57:46 +00002356 } else {
Alexey Bataev7fcacd82016-11-28 15:55:15 +00002357 Visit(E->getBase());
Alexey Bataeve3727102018-04-18 15:57:46 +00002358 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002359 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002360 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002361 for (OMPClause *C : S->clauses()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002362 // Skip analysis of arguments of implicitly defined firstprivate clause
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002363 // for task|target directives.
2364 // Skip analysis of arguments of implicitly defined map clause for target
2365 // directives.
2366 if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) &&
2367 C->isImplicit())) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002368 for (Stmt *CC : C->children()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002369 if (CC)
2370 Visit(CC);
2371 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002372 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002373 }
Alexey Bataevf07946e2018-10-29 20:17:42 +00002374 // Check implicitly captured variables.
2375 VisitSubCaptures(S);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002376 }
2377 void VisitStmt(Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002378 for (Stmt *C : S->children()) {
Alexey Bataev8fc7b5f2018-10-25 15:35:27 +00002379 if (C) {
2380 if (auto *OED = dyn_cast<OMPExecutableDirective>(C)) {
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00002381 // Check implicitly captured variables in the task-based directives to
Alexey Bataev8fc7b5f2018-10-25 15:35:27 +00002382 // check if they must be firstprivatized.
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00002383 VisitSubCaptures(OED);
Alexey Bataev8fc7b5f2018-10-25 15:35:27 +00002384 } else {
2385 Visit(C);
2386 }
2387 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002388 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002389 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002390
Alexey Bataeve3727102018-04-18 15:57:46 +00002391 bool isErrorFound() const { return ErrorFound; }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002392 ArrayRef<Expr *> getImplicitFirstprivate() const {
2393 return ImplicitFirstprivate;
2394 }
2395 ArrayRef<Expr *> getImplicitMap() const { return ImplicitMap; }
Alexey Bataeve3727102018-04-18 15:57:46 +00002396 const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002397 return VarsWithInheritedDSA;
2398 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002399
Alexey Bataev7ff55242014-06-19 09:13:45 +00002400 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
2401 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00002402};
Alexey Bataeved09d242014-05-28 05:53:51 +00002403} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00002404
Alexey Bataevbae9a792014-06-27 10:37:06 +00002405void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00002406 switch (DKind) {
Kelvin Li70a12c52016-07-13 21:51:49 +00002407 case OMPD_parallel:
2408 case OMPD_parallel_for:
2409 case OMPD_parallel_for_simd:
2410 case OMPD_parallel_sections:
Carlo Bertolliba1487b2017-10-04 14:12:09 +00002411 case OMPD_teams:
Alexey Bataev999277a2017-12-06 14:31:09 +00002412 case OMPD_teams_distribute:
2413 case OMPD_teams_distribute_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002414 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Alexey Bataev2377fe92015-09-10 08:12:02 +00002415 QualType KmpInt32PtrTy =
2416 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002417 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002418 std::make_pair(".global_tid.", KmpInt32PtrTy),
2419 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2420 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00002421 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00002422 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2423 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00002424 break;
2425 }
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002426 case OMPD_target_teams:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00002427 case OMPD_target_parallel:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00002428 case OMPD_target_parallel_for:
Alexey Bataevdfa430f2017-12-08 15:03:50 +00002429 case OMPD_target_parallel_for_simd:
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00002430 case OMPD_target_teams_distribute:
2431 case OMPD_target_teams_distribute_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002432 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2433 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2434 QualType KmpInt32PtrTy =
2435 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2436 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00002437 FunctionProtoType::ExtProtoInfo EPI;
2438 EPI.Variadic = true;
2439 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2440 Sema::CapturedParamNameType Params[] = {
2441 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002442 std::make_pair(".part_id.", KmpInt32PtrTy),
2443 std::make_pair(".privates.", VoidPtrTy),
2444 std::make_pair(
2445 ".copy_fn.",
2446 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00002447 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2448 std::make_pair(StringRef(), QualType()) // __context with shared vars
2449 };
2450 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2451 Params);
Alexey Bataev0c869ef2018-01-16 15:57:07 +00002452 // Mark this captured region as inlined, because we don't use outlined
2453 // function directly.
2454 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2455 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002456 Context, AlwaysInlineAttr::Keyword_forceinline));
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002457 Sema::CapturedParamNameType ParamsTarget[] = {
2458 std::make_pair(StringRef(), QualType()) // __context with shared vars
2459 };
2460 // Start a captured region for 'target' with no implicit parameters.
2461 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2462 ParamsTarget);
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002463 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002464 std::make_pair(".global_tid.", KmpInt32PtrTy),
2465 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2466 std::make_pair(StringRef(), QualType()) // __context with shared vars
2467 };
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002468 // Start a captured region for 'teams' or 'parallel'. Both regions have
2469 // the same implicit parameters.
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002470 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002471 ParamsTeamsOrParallel);
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002472 break;
2473 }
Alexey Bataev8451efa2018-01-15 19:06:12 +00002474 case OMPD_target:
2475 case OMPD_target_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002476 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2477 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2478 QualType KmpInt32PtrTy =
2479 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2480 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00002481 FunctionProtoType::ExtProtoInfo EPI;
2482 EPI.Variadic = true;
2483 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2484 Sema::CapturedParamNameType Params[] = {
2485 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002486 std::make_pair(".part_id.", KmpInt32PtrTy),
2487 std::make_pair(".privates.", VoidPtrTy),
2488 std::make_pair(
2489 ".copy_fn.",
2490 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00002491 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2492 std::make_pair(StringRef(), QualType()) // __context with shared vars
2493 };
2494 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2495 Params);
2496 // Mark this captured region as inlined, because we don't use outlined
2497 // function directly.
2498 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2499 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002500 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev8451efa2018-01-15 19:06:12 +00002501 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2502 std::make_pair(StringRef(), QualType()));
2503 break;
2504 }
Kelvin Li70a12c52016-07-13 21:51:49 +00002505 case OMPD_simd:
2506 case OMPD_for:
2507 case OMPD_for_simd:
2508 case OMPD_sections:
2509 case OMPD_section:
2510 case OMPD_single:
2511 case OMPD_master:
2512 case OMPD_critical:
Kelvin Lia579b912016-07-14 02:54:56 +00002513 case OMPD_taskgroup:
2514 case OMPD_distribute:
Alexey Bataev46506272017-12-05 17:41:34 +00002515 case OMPD_distribute_simd:
Kelvin Li70a12c52016-07-13 21:51:49 +00002516 case OMPD_ordered:
2517 case OMPD_atomic:
Alexey Bataev8451efa2018-01-15 19:06:12 +00002518 case OMPD_target_data: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002519 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002520 std::make_pair(StringRef(), QualType()) // __context with shared vars
2521 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00002522 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2523 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002524 break;
2525 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002526 case OMPD_task: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002527 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2528 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2529 QualType KmpInt32PtrTy =
2530 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2531 QualType Args[] = {VoidPtrTy};
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002532 FunctionProtoType::ExtProtoInfo EPI;
2533 EPI.Variadic = true;
2534 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002535 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00002536 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002537 std::make_pair(".part_id.", KmpInt32PtrTy),
2538 std::make_pair(".privates.", VoidPtrTy),
2539 std::make_pair(
2540 ".copy_fn.",
2541 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev48591dd2016-04-20 04:01:36 +00002542 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002543 std::make_pair(StringRef(), QualType()) // __context with shared vars
2544 };
2545 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2546 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00002547 // Mark this captured region as inlined, because we don't use outlined
2548 // function directly.
2549 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2550 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002551 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002552 break;
2553 }
Alexey Bataev1e73ef32016-04-28 12:14:51 +00002554 case OMPD_taskloop:
2555 case OMPD_taskloop_simd: {
Alexey Bataev7292c292016-04-25 12:22:29 +00002556 QualType KmpInt32Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002557 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1)
2558 .withConst();
Alexey Bataev7292c292016-04-25 12:22:29 +00002559 QualType KmpUInt64Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002560 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0)
2561 .withConst();
Alexey Bataev7292c292016-04-25 12:22:29 +00002562 QualType KmpInt64Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002563 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1)
2564 .withConst();
2565 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2566 QualType KmpInt32PtrTy =
2567 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2568 QualType Args[] = {VoidPtrTy};
Alexey Bataev7292c292016-04-25 12:22:29 +00002569 FunctionProtoType::ExtProtoInfo EPI;
2570 EPI.Variadic = true;
2571 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev49f6e782015-12-01 04:18:41 +00002572 Sema::CapturedParamNameType Params[] = {
Alexey Bataev7292c292016-04-25 12:22:29 +00002573 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002574 std::make_pair(".part_id.", KmpInt32PtrTy),
2575 std::make_pair(".privates.", VoidPtrTy),
Alexey Bataev7292c292016-04-25 12:22:29 +00002576 std::make_pair(
2577 ".copy_fn.",
2578 Context.getPointerType(CopyFnType).withConst().withRestrict()),
2579 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2580 std::make_pair(".lb.", KmpUInt64Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002581 std::make_pair(".ub.", KmpUInt64Ty),
2582 std::make_pair(".st.", KmpInt64Ty),
Alexey Bataev7292c292016-04-25 12:22:29 +00002583 std::make_pair(".liter.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002584 std::make_pair(".reductions.", VoidPtrTy),
Alexey Bataev49f6e782015-12-01 04:18:41 +00002585 std::make_pair(StringRef(), QualType()) // __context with shared vars
2586 };
2587 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2588 Params);
Alexey Bataev7292c292016-04-25 12:22:29 +00002589 // Mark this captured region as inlined, because we don't use outlined
2590 // function directly.
2591 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2592 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002593 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev49f6e782015-12-01 04:18:41 +00002594 break;
2595 }
Kelvin Li4a39add2016-07-05 05:00:15 +00002596 case OMPD_distribute_parallel_for_simd:
Alexey Bataev647dd842018-01-15 20:59:40 +00002597 case OMPD_distribute_parallel_for: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002598 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli9925f152016-06-27 14:55:37 +00002599 QualType KmpInt32PtrTy =
2600 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2601 Sema::CapturedParamNameType Params[] = {
2602 std::make_pair(".global_tid.", KmpInt32PtrTy),
2603 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002604 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2605 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli9925f152016-06-27 14:55:37 +00002606 std::make_pair(StringRef(), QualType()) // __context with shared vars
2607 };
2608 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2609 Params);
2610 break;
2611 }
Alexey Bataev647dd842018-01-15 20:59:40 +00002612 case OMPD_target_teams_distribute_parallel_for:
2613 case OMPD_target_teams_distribute_parallel_for_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002614 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli52978c32018-01-03 21:12:44 +00002615 QualType KmpInt32PtrTy =
2616 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002617 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
Carlo Bertolli52978c32018-01-03 21:12:44 +00002618
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002619 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00002620 FunctionProtoType::ExtProtoInfo EPI;
2621 EPI.Variadic = true;
2622 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2623 Sema::CapturedParamNameType Params[] = {
2624 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002625 std::make_pair(".part_id.", KmpInt32PtrTy),
2626 std::make_pair(".privates.", VoidPtrTy),
2627 std::make_pair(
2628 ".copy_fn.",
2629 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00002630 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2631 std::make_pair(StringRef(), QualType()) // __context with shared vars
2632 };
2633 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2634 Params);
Alexey Bataev9f9fb0b2018-01-16 19:02:33 +00002635 // Mark this captured region as inlined, because we don't use outlined
2636 // function directly.
2637 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2638 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002639 Context, AlwaysInlineAttr::Keyword_forceinline));
Carlo Bertolli52978c32018-01-03 21:12:44 +00002640 Sema::CapturedParamNameType ParamsTarget[] = {
2641 std::make_pair(StringRef(), QualType()) // __context with shared vars
2642 };
2643 // Start a captured region for 'target' with no implicit parameters.
2644 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2645 ParamsTarget);
2646
2647 Sema::CapturedParamNameType ParamsTeams[] = {
2648 std::make_pair(".global_tid.", KmpInt32PtrTy),
2649 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2650 std::make_pair(StringRef(), QualType()) // __context with shared vars
2651 };
2652 // Start a captured region for 'target' with no implicit parameters.
2653 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2654 ParamsTeams);
2655
2656 Sema::CapturedParamNameType ParamsParallel[] = {
2657 std::make_pair(".global_tid.", KmpInt32PtrTy),
2658 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002659 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2660 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli52978c32018-01-03 21:12:44 +00002661 std::make_pair(StringRef(), QualType()) // __context with shared vars
2662 };
2663 // Start a captured region for 'teams' or 'parallel'. Both regions have
2664 // the same implicit parameters.
2665 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2666 ParamsParallel);
2667 break;
2668 }
2669
Alexey Bataev46506272017-12-05 17:41:34 +00002670 case OMPD_teams_distribute_parallel_for:
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00002671 case OMPD_teams_distribute_parallel_for_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002672 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli62fae152017-11-20 20:46:39 +00002673 QualType KmpInt32PtrTy =
2674 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2675
2676 Sema::CapturedParamNameType ParamsTeams[] = {
2677 std::make_pair(".global_tid.", KmpInt32PtrTy),
2678 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2679 std::make_pair(StringRef(), QualType()) // __context with shared vars
2680 };
2681 // Start a captured region for 'target' with no implicit parameters.
2682 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2683 ParamsTeams);
2684
2685 Sema::CapturedParamNameType ParamsParallel[] = {
2686 std::make_pair(".global_tid.", KmpInt32PtrTy),
2687 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002688 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2689 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli62fae152017-11-20 20:46:39 +00002690 std::make_pair(StringRef(), QualType()) // __context with shared vars
2691 };
2692 // Start a captured region for 'teams' or 'parallel'. Both regions have
2693 // the same implicit parameters.
2694 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2695 ParamsParallel);
2696 break;
2697 }
Alexey Bataev7828b252017-11-21 17:08:48 +00002698 case OMPD_target_update:
2699 case OMPD_target_enter_data:
2700 case OMPD_target_exit_data: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002701 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2702 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2703 QualType KmpInt32PtrTy =
2704 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2705 QualType Args[] = {VoidPtrTy};
Alexey Bataev7828b252017-11-21 17:08:48 +00002706 FunctionProtoType::ExtProtoInfo EPI;
2707 EPI.Variadic = true;
2708 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2709 Sema::CapturedParamNameType Params[] = {
2710 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002711 std::make_pair(".part_id.", KmpInt32PtrTy),
2712 std::make_pair(".privates.", VoidPtrTy),
2713 std::make_pair(
2714 ".copy_fn.",
2715 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev7828b252017-11-21 17:08:48 +00002716 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2717 std::make_pair(StringRef(), QualType()) // __context with shared vars
2718 };
2719 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2720 Params);
2721 // Mark this captured region as inlined, because we don't use outlined
2722 // function directly.
2723 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2724 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002725 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev7828b252017-11-21 17:08:48 +00002726 break;
2727 }
Alexey Bataev9959db52014-05-06 10:08:46 +00002728 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00002729 case OMPD_taskyield:
2730 case OMPD_barrier:
2731 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002732 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00002733 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00002734 case OMPD_flush:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002735 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00002736 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00002737 case OMPD_declare_target:
2738 case OMPD_end_declare_target:
Kelvin Li1408f912018-09-26 04:28:39 +00002739 case OMPD_requires:
Alexey Bataev9959db52014-05-06 10:08:46 +00002740 llvm_unreachable("OpenMP Directive is not allowed");
2741 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00002742 llvm_unreachable("Unknown OpenMP directive");
2743 }
2744}
2745
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002746int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) {
2747 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2748 getOpenMPCaptureRegions(CaptureRegions, DKind);
2749 return CaptureRegions.size();
2750}
2751
Alexey Bataev3392d762016-02-16 11:18:12 +00002752static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00002753 Expr *CaptureExpr, bool WithInit,
2754 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00002755 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00002756 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00002757 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00002758 QualType Ty = Init->getType();
2759 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002760 if (S.getLangOpts().CPlusPlus) {
Alexey Bataev4244be22016-02-11 05:35:55 +00002761 Ty = C.getLValueReferenceType(Ty);
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002762 } else {
Alexey Bataev4244be22016-02-11 05:35:55 +00002763 Ty = C.getPointerType(Ty);
2764 ExprResult Res =
2765 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
2766 if (!Res.isUsable())
2767 return nullptr;
2768 Init = Res.get();
2769 }
Alexey Bataev61205072016-03-02 04:57:40 +00002770 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00002771 }
Alexey Bataeva7206b92016-12-20 16:51:02 +00002772 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty,
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002773 CaptureExpr->getBeginLoc());
Alexey Bataev2bbf7212016-03-03 03:52:24 +00002774 if (!WithInit)
Alexey Bataeve3727102018-04-18 15:57:46 +00002775 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C));
Alexey Bataev4244be22016-02-11 05:35:55 +00002776 S.CurContext->addHiddenDecl(CED);
Richard Smith3beb7c62017-01-12 02:27:38 +00002777 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00002778 return CED;
2779}
2780
Alexey Bataev61205072016-03-02 04:57:40 +00002781static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
2782 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00002783 OMPCapturedExprDecl *CD;
Alexey Bataeve3727102018-04-18 15:57:46 +00002784 if (VarDecl *VD = S.isOpenMPCapturedDecl(D))
Alexey Bataevb7a34b62016-02-25 03:59:29 +00002785 CD = cast<OMPCapturedExprDecl>(VD);
Alexey Bataeve3727102018-04-18 15:57:46 +00002786 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00002787 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
2788 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00002789 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00002790 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00002791}
2792
Alexey Bataev5a3af132016-03-29 08:58:54 +00002793static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002794 CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00002795 if (!Ref) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002796 OMPCapturedExprDecl *CD = buildCaptureDecl(
2797 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr,
2798 /*WithInit=*/true, /*AsExpression=*/true);
Alexey Bataev5a3af132016-03-29 08:58:54 +00002799 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
2800 CaptureExpr->getExprLoc());
2801 }
2802 ExprResult Res = Ref;
2803 if (!S.getLangOpts().CPlusPlus &&
2804 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002805 Ref->getType()->isPointerType()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00002806 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002807 if (!Res.isUsable())
2808 return ExprError();
2809 }
2810 return S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00002811}
2812
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002813namespace {
2814// OpenMP directives parsed in this section are represented as a
2815// CapturedStatement with an associated statement. If a syntax error
2816// is detected during the parsing of the associated statement, the
2817// compiler must abort processing and close the CapturedStatement.
2818//
2819// Combined directives such as 'target parallel' have more than one
2820// nested CapturedStatements. This RAII ensures that we unwind out
2821// of all the nested CapturedStatements when an error is found.
2822class CaptureRegionUnwinderRAII {
2823private:
2824 Sema &S;
2825 bool &ErrorFound;
Alexey Bataeve3727102018-04-18 15:57:46 +00002826 OpenMPDirectiveKind DKind = OMPD_unknown;
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002827
2828public:
2829 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
2830 OpenMPDirectiveKind DKind)
2831 : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
2832 ~CaptureRegionUnwinderRAII() {
2833 if (ErrorFound) {
2834 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind);
2835 while (--ThisCaptureLevel >= 0)
2836 S.ActOnCapturedRegionError();
2837 }
2838 }
2839};
2840} // namespace
2841
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002842StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
2843 ArrayRef<OMPClause *> Clauses) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002844 bool ErrorFound = false;
2845 CaptureRegionUnwinderRAII CaptureRegionUnwinder(
2846 *this, ErrorFound, DSAStack->getCurrentDirective());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002847 if (!S.isUsable()) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002848 ErrorFound = true;
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002849 return StmtError();
2850 }
Alexey Bataev993d2802015-12-28 06:23:08 +00002851
Alexey Bataev2ba67042017-11-28 21:11:44 +00002852 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2853 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
Alexey Bataev993d2802015-12-28 06:23:08 +00002854 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00002855 OMPScheduleClause *SC = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +00002856 SmallVector<const OMPLinearClause *, 4> LCs;
2857 SmallVector<const OMPClauseWithPreInit *, 4> PICs;
Alexey Bataev040d5402015-05-12 08:35:28 +00002858 // This is required for proper codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00002859 for (OMPClause *Clause : Clauses) {
Alexey Bataev88202be2017-07-27 13:20:36 +00002860 if (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) &&
2861 Clause->getClauseKind() == OMPC_in_reduction) {
2862 // Capture taskgroup task_reduction descriptors inside the tasking regions
2863 // with the corresponding in_reduction items.
2864 auto *IRC = cast<OMPInReductionClause>(Clause);
Alexey Bataeve3727102018-04-18 15:57:46 +00002865 for (Expr *E : IRC->taskgroup_descriptors())
Alexey Bataev88202be2017-07-27 13:20:36 +00002866 if (E)
2867 MarkDeclarationsReferencedInExpr(E);
2868 }
Alexey Bataev16dc7b62015-05-20 03:46:04 +00002869 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00002870 Clause->getClauseKind() == OMPC_copyprivate ||
2871 (getLangOpts().OpenMPUseTLS &&
2872 getASTContext().getTargetInfo().isTLSSupported() &&
2873 Clause->getClauseKind() == OMPC_copyin)) {
2874 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00002875 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeve3727102018-04-18 15:57:46 +00002876 for (Stmt *VarRef : Clause->children()) {
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002877 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00002878 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002879 }
2880 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00002881 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev2ba67042017-11-28 21:11:44 +00002882 } else if (CaptureRegions.size() > 1 ||
2883 CaptureRegions.back() != OMPD_unknown) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002884 if (auto *C = OMPClauseWithPreInit::get(Clause))
2885 PICs.push_back(C);
Alexey Bataev005248a2016-02-25 05:25:57 +00002886 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002887 if (Expr *E = C->getPostUpdateExpr())
Alexey Bataev005248a2016-02-25 05:25:57 +00002888 MarkDeclarationsReferencedInExpr(E);
2889 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002890 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002891 if (Clause->getClauseKind() == OMPC_schedule)
2892 SC = cast<OMPScheduleClause>(Clause);
2893 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00002894 OC = cast<OMPOrderedClause>(Clause);
2895 else if (Clause->getClauseKind() == OMPC_linear)
2896 LCs.push_back(cast<OMPLinearClause>(Clause));
2897 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002898 // OpenMP, 2.7.1 Loop Construct, Restrictions
2899 // The nonmonotonic modifier cannot be specified if an ordered clause is
2900 // specified.
2901 if (SC &&
2902 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
2903 SC->getSecondScheduleModifier() ==
2904 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
2905 OC) {
2906 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
2907 ? SC->getFirstScheduleModifierLoc()
2908 : SC->getSecondScheduleModifierLoc(),
2909 diag::err_omp_schedule_nonmonotonic_ordered)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00002910 << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
Alexey Bataev6402bca2015-12-28 07:25:51 +00002911 ErrorFound = true;
2912 }
Alexey Bataev993d2802015-12-28 06:23:08 +00002913 if (!LCs.empty() && OC && OC->getNumForLoops()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002914 for (const OMPLinearClause *C : LCs) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002915 Diag(C->getBeginLoc(), diag::err_omp_linear_ordered)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00002916 << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
Alexey Bataev993d2802015-12-28 06:23:08 +00002917 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002918 ErrorFound = true;
2919 }
Alexey Bataev113438c2015-12-30 12:06:23 +00002920 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
2921 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
2922 OC->getNumForLoops()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002923 Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd)
Alexey Bataev113438c2015-12-30 12:06:23 +00002924 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
2925 ErrorFound = true;
2926 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002927 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00002928 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002929 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002930 StmtResult SR = S;
Alexey Bataev2ba67042017-11-28 21:11:44 +00002931 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002932 // Mark all variables in private list clauses as used in inner region.
2933 // Required for proper codegen of combined directives.
2934 // TODO: add processing for other clauses.
Alexey Bataev2ba67042017-11-28 21:11:44 +00002935 if (ThisCaptureRegion != OMPD_unknown) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002936 for (const clang::OMPClauseWithPreInit *C : PICs) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002937 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
2938 // Find the particular capture region for the clause if the
2939 // directive is a combined one with multiple capture regions.
2940 // If the directive is not a combined one, the capture region
2941 // associated with the clause is OMPD_unknown and is generated
2942 // only once.
2943 if (CaptureRegion == ThisCaptureRegion ||
2944 CaptureRegion == OMPD_unknown) {
2945 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002946 for (Decl *D : DS->decls())
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002947 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
2948 }
2949 }
2950 }
2951 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002952 SR = ActOnCapturedRegionEnd(SR.get());
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002953 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002954 return SR;
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002955}
2956
Jonas Hahnfeld64a9e3c2017-02-22 06:49:10 +00002957static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
2958 OpenMPDirectiveKind CancelRegion,
2959 SourceLocation StartLoc) {
2960 // CancelRegion is only needed for cancel and cancellation_point.
2961 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point)
2962 return false;
2963
2964 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for ||
2965 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup)
2966 return false;
2967
2968 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region)
2969 << getOpenMPDirectiveName(CancelRegion);
2970 return true;
2971}
2972
Alexey Bataeve3727102018-04-18 15:57:46 +00002973static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002974 OpenMPDirectiveKind CurrentRegion,
2975 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002976 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002977 SourceLocation StartLoc) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002978 if (Stack->getCurScope()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002979 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective();
2980 OpenMPDirectiveKind OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002981 bool NestingProhibited = false;
2982 bool CloseNesting = true;
David Majnemer9d168222016-08-05 17:44:54 +00002983 bool OrphanSeen = false;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002984 enum {
2985 NoRecommend,
2986 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002987 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002988 ShouldBeInTargetRegion,
2989 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002990 } Recommend = NoRecommend;
Kelvin Lifd8b5742016-07-01 14:30:25 +00002991 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002992 // OpenMP [2.16, Nesting of Regions]
2993 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002994 // OpenMP [2.8.1,simd Construct, Restrictions]
Kelvin Lifd8b5742016-07-01 14:30:25 +00002995 // An ordered construct with the simd clause is the only OpenMP
2996 // construct that can appear in the simd region.
David Majnemer9d168222016-08-05 17:44:54 +00002997 // Allowing a SIMD construct nested in another SIMD construct is an
Kelvin Lifd8b5742016-07-01 14:30:25 +00002998 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
2999 // message.
3000 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
3001 ? diag::err_omp_prohibited_region_simd
3002 : diag::warn_omp_nesting_simd);
3003 return CurrentRegion != OMPD_simd;
Alexey Bataev549210e2014-06-24 04:39:47 +00003004 }
Alexey Bataev0162e452014-07-22 10:10:35 +00003005 if (ParentRegion == OMPD_atomic) {
3006 // OpenMP [2.16, Nesting of Regions]
3007 // OpenMP constructs may not be nested inside an atomic region.
3008 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
3009 return true;
3010 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003011 if (CurrentRegion == OMPD_section) {
3012 // OpenMP [2.7.2, sections Construct, Restrictions]
3013 // Orphaned section directives are prohibited. That is, the section
3014 // directives must appear within the sections construct and must not be
3015 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003016 if (ParentRegion != OMPD_sections &&
3017 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003018 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
3019 << (ParentRegion != OMPD_unknown)
3020 << getOpenMPDirectiveName(ParentRegion);
3021 return true;
3022 }
3023 return false;
3024 }
Kelvin Li2b51f722016-07-26 04:32:50 +00003025 // Allow some constructs (except teams) to be orphaned (they could be
David Majnemer9d168222016-08-05 17:44:54 +00003026 // used in functions, called from OpenMP regions with the required
Kelvin Li2b51f722016-07-26 04:32:50 +00003027 // preconditions).
Kelvin Libf594a52016-12-17 05:48:59 +00003028 if (ParentRegion == OMPD_unknown &&
3029 !isOpenMPNestingTeamsDirective(CurrentRegion))
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003030 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00003031 if (CurrentRegion == OMPD_cancellation_point ||
3032 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003033 // OpenMP [2.16, Nesting of Regions]
3034 // A cancellation point construct for which construct-type-clause is
3035 // taskgroup must be nested inside a task construct. A cancellation
3036 // point construct for which construct-type-clause is not taskgroup must
3037 // be closely nested inside an OpenMP construct that matches the type
3038 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00003039 // A cancel construct for which construct-type-clause is taskgroup must be
3040 // nested inside a task construct. A cancel construct for which
3041 // construct-type-clause is not taskgroup must be closely nested inside an
3042 // OpenMP construct that matches the type specified in
3043 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003044 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003045 !((CancelRegion == OMPD_parallel &&
3046 (ParentRegion == OMPD_parallel ||
3047 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00003048 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003049 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00003050 ParentRegion == OMPD_target_parallel_for ||
3051 ParentRegion == OMPD_distribute_parallel_for ||
Alexey Bataev16e79882017-11-22 21:12:03 +00003052 ParentRegion == OMPD_teams_distribute_parallel_for ||
3053 ParentRegion == OMPD_target_teams_distribute_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003054 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
3055 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00003056 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
3057 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003058 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00003059 // OpenMP [2.16, Nesting of Regions]
3060 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003061 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00003062 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003063 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003064 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
3065 // OpenMP [2.16, Nesting of Regions]
3066 // A critical region may not be nested (closely or otherwise) inside a
3067 // critical region with the same name. Note that this restriction is not
3068 // sufficient to prevent deadlock.
3069 SourceLocation PreviousCriticalLoc;
David Majnemer9d168222016-08-05 17:44:54 +00003070 bool DeadLock = Stack->hasDirective(
3071 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
3072 const DeclarationNameInfo &DNI,
Alexey Bataeve3727102018-04-18 15:57:46 +00003073 SourceLocation Loc) {
David Majnemer9d168222016-08-05 17:44:54 +00003074 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
3075 PreviousCriticalLoc = Loc;
3076 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003077 }
3078 return false;
David Majnemer9d168222016-08-05 17:44:54 +00003079 },
3080 false /* skip top directive */);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003081 if (DeadLock) {
3082 SemaRef.Diag(StartLoc,
3083 diag::err_omp_prohibited_region_critical_same_name)
3084 << CurrentName.getName();
3085 if (PreviousCriticalLoc.isValid())
3086 SemaRef.Diag(PreviousCriticalLoc,
3087 diag::note_omp_previous_critical_region);
3088 return true;
3089 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003090 } else if (CurrentRegion == OMPD_barrier) {
3091 // OpenMP [2.16, Nesting of Regions]
3092 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003093 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003094 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3095 isOpenMPTaskingDirective(ParentRegion) ||
3096 ParentRegion == OMPD_master ||
3097 ParentRegion == OMPD_critical ||
3098 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00003099 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Kelvin Li579e41c2016-11-30 23:51:03 +00003100 !isOpenMPParallelDirective(CurrentRegion) &&
3101 !isOpenMPTeamsDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003102 // OpenMP [2.16, Nesting of Regions]
3103 // A worksharing region may not be closely nested inside a worksharing,
3104 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003105 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3106 isOpenMPTaskingDirective(ParentRegion) ||
3107 ParentRegion == OMPD_master ||
3108 ParentRegion == OMPD_critical ||
3109 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003110 Recommend = ShouldBeInParallelRegion;
3111 } else if (CurrentRegion == OMPD_ordered) {
3112 // OpenMP [2.16, Nesting of Regions]
3113 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00003114 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003115 // An ordered region must be closely nested inside a loop region (or
3116 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003117 // OpenMP [2.8.1,simd Construct, Restrictions]
3118 // An ordered construct with the simd clause is the only OpenMP construct
3119 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003120 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003121 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003122 !(isOpenMPSimdDirective(ParentRegion) ||
3123 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003124 Recommend = ShouldBeInOrderedRegion;
Kelvin Libf594a52016-12-17 05:48:59 +00003125 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00003126 // OpenMP [2.16, Nesting of Regions]
3127 // If specified, a teams construct must be contained within a target
3128 // construct.
3129 NestingProhibited = ParentRegion != OMPD_target;
Kelvin Li2b51f722016-07-26 04:32:50 +00003130 OrphanSeen = ParentRegion == OMPD_unknown;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003131 Recommend = ShouldBeInTargetRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003132 }
Kelvin Libf594a52016-12-17 05:48:59 +00003133 if (!NestingProhibited &&
3134 !isOpenMPTargetExecutionDirective(CurrentRegion) &&
3135 !isOpenMPTargetDataManagementDirective(CurrentRegion) &&
3136 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00003137 // OpenMP [2.16, Nesting of Regions]
3138 // distribute, parallel, parallel sections, parallel workshare, and the
3139 // parallel loop and parallel loop SIMD constructs are the only OpenMP
3140 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003141 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
3142 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00003143 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003144 }
David Majnemer9d168222016-08-05 17:44:54 +00003145 if (!NestingProhibited &&
Kelvin Li02532872016-08-05 14:37:37 +00003146 isOpenMPNestingDistributeDirective(CurrentRegion)) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003147 // OpenMP 4.5 [2.17 Nesting of Regions]
3148 // The region associated with the distribute construct must be strictly
3149 // nested inside a teams region
Kelvin Libf594a52016-12-17 05:48:59 +00003150 NestingProhibited =
3151 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003152 Recommend = ShouldBeInTeamsRegion;
3153 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003154 if (!NestingProhibited &&
3155 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
3156 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
3157 // OpenMP 4.5 [2.17 Nesting of Regions]
3158 // If a target, target update, target data, target enter data, or
3159 // target exit data construct is encountered during execution of a
3160 // target region, the behavior is unspecified.
3161 NestingProhibited = Stack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +00003162 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
Alexey Bataeve3727102018-04-18 15:57:46 +00003163 SourceLocation) {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003164 if (isOpenMPTargetExecutionDirective(K)) {
3165 OffendingRegion = K;
3166 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003167 }
3168 return false;
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003169 },
3170 false /* don't skip top directive */);
3171 CloseNesting = false;
3172 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003173 if (NestingProhibited) {
Kelvin Li2b51f722016-07-26 04:32:50 +00003174 if (OrphanSeen) {
3175 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
3176 << getOpenMPDirectiveName(CurrentRegion) << Recommend;
3177 } else {
3178 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
3179 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
3180 << Recommend << getOpenMPDirectiveName(CurrentRegion);
3181 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003182 return true;
3183 }
3184 }
3185 return false;
3186}
3187
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003188static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
3189 ArrayRef<OMPClause *> Clauses,
3190 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
3191 bool ErrorFound = false;
3192 unsigned NamedModifiersNumber = 0;
3193 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
3194 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00003195 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00003196 for (const OMPClause *C : Clauses) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003197 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
3198 // At most one if clause without a directive-name-modifier can appear on
3199 // the directive.
3200 OpenMPDirectiveKind CurNM = IC->getNameModifier();
3201 if (FoundNameModifiers[CurNM]) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003202 S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003203 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
3204 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
3205 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003206 } else if (CurNM != OMPD_unknown) {
3207 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003208 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003209 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003210 FoundNameModifiers[CurNM] = IC;
3211 if (CurNM == OMPD_unknown)
3212 continue;
3213 // Check if the specified name modifier is allowed for the current
3214 // directive.
3215 // At most one if clause with the particular directive-name-modifier can
3216 // appear on the directive.
3217 bool MatchFound = false;
3218 for (auto NM : AllowedNameModifiers) {
3219 if (CurNM == NM) {
3220 MatchFound = true;
3221 break;
3222 }
3223 }
3224 if (!MatchFound) {
3225 S.Diag(IC->getNameModifierLoc(),
3226 diag::err_omp_wrong_if_directive_name_modifier)
3227 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
3228 ErrorFound = true;
3229 }
3230 }
3231 }
3232 // If any if clause on the directive includes a directive-name-modifier then
3233 // all if clauses on the directive must include a directive-name-modifier.
3234 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3235 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003236 S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003237 diag::err_omp_no_more_if_clause);
3238 } else {
3239 std::string Values;
3240 std::string Sep(", ");
3241 unsigned AllowedCnt = 0;
3242 unsigned TotalAllowedNum =
3243 AllowedNameModifiers.size() - NamedModifiersNumber;
3244 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3245 ++Cnt) {
3246 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3247 if (!FoundNameModifiers[NM]) {
3248 Values += "'";
3249 Values += getOpenMPDirectiveName(NM);
3250 Values += "'";
3251 if (AllowedCnt + 2 == TotalAllowedNum)
3252 Values += " or ";
3253 else if (AllowedCnt + 1 != TotalAllowedNum)
3254 Values += Sep;
3255 ++AllowedCnt;
3256 }
3257 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003258 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003259 diag::err_omp_unnamed_if_clause)
3260 << (TotalAllowedNum > 1) << Values;
3261 }
Alexey Bataeve3727102018-04-18 15:57:46 +00003262 for (SourceLocation Loc : NameModifierLoc) {
Alexey Bataevecb156a2015-09-15 17:23:56 +00003263 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3264 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003265 ErrorFound = true;
3266 }
3267 return ErrorFound;
3268}
3269
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003270StmtResult Sema::ActOnOpenMPExecutableDirective(
3271 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3272 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3273 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003274 StmtResult Res = StmtError();
Jonas Hahnfeld64a9e3c2017-02-22 06:49:10 +00003275 // First check CancelRegion which is then used in checkNestingOfRegions.
3276 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) ||
3277 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003278 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00003279 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003280
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003281 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataeve3727102018-04-18 15:57:46 +00003282 VarsWithInheritedDSAType VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003283 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00003284 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00003285 if (AStmt && !CurContext->isDependentContext()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00003286 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3287
3288 // Check default data sharing attributes for referenced variables.
3289 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
Arpith Chacko Jacob1f46b702017-01-23 15:38:49 +00003290 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind);
3291 Stmt *S = AStmt;
3292 while (--ThisCaptureLevel >= 0)
3293 S = cast<CapturedStmt>(S)->getCapturedStmt();
3294 DSAChecker.Visit(S);
Alexey Bataev68446b72014-07-18 07:47:19 +00003295 if (DSAChecker.isErrorFound())
3296 return StmtError();
3297 // Generate list of implicitly defined firstprivate variables.
3298 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00003299
Alexey Bataev88202be2017-07-27 13:20:36 +00003300 SmallVector<Expr *, 4> ImplicitFirstprivates(
3301 DSAChecker.getImplicitFirstprivate().begin(),
3302 DSAChecker.getImplicitFirstprivate().end());
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003303 SmallVector<Expr *, 4> ImplicitMaps(DSAChecker.getImplicitMap().begin(),
3304 DSAChecker.getImplicitMap().end());
Alexey Bataev88202be2017-07-27 13:20:36 +00003305 // Mark taskgroup task_reduction descriptors as implicitly firstprivate.
Alexey Bataeve3727102018-04-18 15:57:46 +00003306 for (OMPClause *C : Clauses) {
Alexey Bataev88202be2017-07-27 13:20:36 +00003307 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003308 for (Expr *E : IRC->taskgroup_descriptors())
Alexey Bataev88202be2017-07-27 13:20:36 +00003309 if (E)
3310 ImplicitFirstprivates.emplace_back(E);
3311 }
3312 }
3313 if (!ImplicitFirstprivates.empty()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00003314 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
Alexey Bataev88202be2017-07-27 13:20:36 +00003315 ImplicitFirstprivates, SourceLocation(), SourceLocation(),
3316 SourceLocation())) {
Alexey Bataev68446b72014-07-18 07:47:19 +00003317 ClausesWithImplicit.push_back(Implicit);
3318 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
Alexey Bataev88202be2017-07-27 13:20:36 +00003319 ImplicitFirstprivates.size();
Alexey Bataeve3727102018-04-18 15:57:46 +00003320 } else {
Alexey Bataev68446b72014-07-18 07:47:19 +00003321 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003322 }
Alexey Bataev68446b72014-07-18 07:47:19 +00003323 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003324 if (!ImplicitMaps.empty()) {
3325 if (OMPClause *Implicit = ActOnOpenMPMapClause(
3326 OMPC_MAP_unknown, OMPC_MAP_tofrom, /*IsMapTypeImplicit=*/true,
3327 SourceLocation(), SourceLocation(), ImplicitMaps,
3328 SourceLocation(), SourceLocation(), SourceLocation())) {
3329 ClausesWithImplicit.emplace_back(Implicit);
3330 ErrorFound |=
3331 cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMaps.size();
Alexey Bataeve3727102018-04-18 15:57:46 +00003332 } else {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003333 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003334 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003335 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003336 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003337
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003338 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003339 switch (Kind) {
3340 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00003341 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3342 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003343 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003344 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003345 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003346 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3347 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003348 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003349 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003350 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3351 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003352 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003353 case OMPD_for_simd:
3354 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3355 EndLoc, VarsWithInheritedDSA);
3356 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003357 case OMPD_sections:
3358 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3359 EndLoc);
3360 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003361 case OMPD_section:
3362 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003363 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003364 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3365 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003366 case OMPD_single:
3367 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3368 EndLoc);
3369 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003370 case OMPD_master:
3371 assert(ClausesWithImplicit.empty() &&
3372 "No clauses are allowed for 'omp master' directive");
3373 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3374 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003375 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003376 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3377 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003378 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003379 case OMPD_parallel_for:
3380 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3381 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003382 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003383 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003384 case OMPD_parallel_for_simd:
3385 Res = ActOnOpenMPParallelForSimdDirective(
3386 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003387 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003388 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003389 case OMPD_parallel_sections:
3390 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3391 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003392 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003393 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003394 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003395 Res =
3396 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003397 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003398 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003399 case OMPD_taskyield:
3400 assert(ClausesWithImplicit.empty() &&
3401 "No clauses are allowed for 'omp taskyield' directive");
3402 assert(AStmt == nullptr &&
3403 "No associated statement allowed for 'omp taskyield' directive");
3404 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3405 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003406 case OMPD_barrier:
3407 assert(ClausesWithImplicit.empty() &&
3408 "No clauses are allowed for 'omp barrier' directive");
3409 assert(AStmt == nullptr &&
3410 "No associated statement allowed for 'omp barrier' directive");
3411 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3412 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003413 case OMPD_taskwait:
3414 assert(ClausesWithImplicit.empty() &&
3415 "No clauses are allowed for 'omp taskwait' directive");
3416 assert(AStmt == nullptr &&
3417 "No associated statement allowed for 'omp taskwait' directive");
3418 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3419 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003420 case OMPD_taskgroup:
Alexey Bataev169d96a2017-07-18 20:17:46 +00003421 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc,
3422 EndLoc);
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003423 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003424 case OMPD_flush:
3425 assert(AStmt == nullptr &&
3426 "No associated statement allowed for 'omp flush' directive");
3427 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3428 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003429 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003430 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3431 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003432 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003433 case OMPD_atomic:
3434 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3435 EndLoc);
3436 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003437 case OMPD_teams:
3438 Res =
3439 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3440 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003441 case OMPD_target:
3442 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3443 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003444 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003445 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003446 case OMPD_target_parallel:
3447 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3448 StartLoc, EndLoc);
3449 AllowedNameModifiers.push_back(OMPD_target);
3450 AllowedNameModifiers.push_back(OMPD_parallel);
3451 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003452 case OMPD_target_parallel_for:
3453 Res = ActOnOpenMPTargetParallelForDirective(
3454 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3455 AllowedNameModifiers.push_back(OMPD_target);
3456 AllowedNameModifiers.push_back(OMPD_parallel);
3457 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003458 case OMPD_cancellation_point:
3459 assert(ClausesWithImplicit.empty() &&
3460 "No clauses are allowed for 'omp cancellation point' directive");
3461 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3462 "cancellation point' directive");
3463 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3464 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003465 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003466 assert(AStmt == nullptr &&
3467 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003468 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3469 CancelRegion);
3470 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003471 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003472 case OMPD_target_data:
3473 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3474 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003475 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003476 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003477 case OMPD_target_enter_data:
3478 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00003479 EndLoc, AStmt);
Samuel Antaodf67fc42016-01-19 19:15:56 +00003480 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3481 break;
Samuel Antao72590762016-01-19 20:04:50 +00003482 case OMPD_target_exit_data:
3483 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00003484 EndLoc, AStmt);
Samuel Antao72590762016-01-19 20:04:50 +00003485 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3486 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003487 case OMPD_taskloop:
3488 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3489 EndLoc, VarsWithInheritedDSA);
3490 AllowedNameModifiers.push_back(OMPD_taskloop);
3491 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003492 case OMPD_taskloop_simd:
3493 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3494 EndLoc, VarsWithInheritedDSA);
3495 AllowedNameModifiers.push_back(OMPD_taskloop);
3496 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003497 case OMPD_distribute:
3498 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3499 EndLoc, VarsWithInheritedDSA);
3500 break;
Samuel Antao686c70c2016-05-26 17:30:50 +00003501 case OMPD_target_update:
Alexey Bataev7828b252017-11-21 17:08:48 +00003502 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc,
3503 EndLoc, AStmt);
Samuel Antao686c70c2016-05-26 17:30:50 +00003504 AllowedNameModifiers.push_back(OMPD_target_update);
3505 break;
Carlo Bertolli9925f152016-06-27 14:55:37 +00003506 case OMPD_distribute_parallel_for:
3507 Res = ActOnOpenMPDistributeParallelForDirective(
3508 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3509 AllowedNameModifiers.push_back(OMPD_parallel);
3510 break;
Kelvin Li4a39add2016-07-05 05:00:15 +00003511 case OMPD_distribute_parallel_for_simd:
3512 Res = ActOnOpenMPDistributeParallelForSimdDirective(
3513 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3514 AllowedNameModifiers.push_back(OMPD_parallel);
3515 break;
Kelvin Li787f3fc2016-07-06 04:45:38 +00003516 case OMPD_distribute_simd:
3517 Res = ActOnOpenMPDistributeSimdDirective(
3518 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3519 break;
Kelvin Lia579b912016-07-14 02:54:56 +00003520 case OMPD_target_parallel_for_simd:
3521 Res = ActOnOpenMPTargetParallelForSimdDirective(
3522 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3523 AllowedNameModifiers.push_back(OMPD_target);
3524 AllowedNameModifiers.push_back(OMPD_parallel);
3525 break;
Kelvin Li986330c2016-07-20 22:57:10 +00003526 case OMPD_target_simd:
3527 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3528 EndLoc, VarsWithInheritedDSA);
3529 AllowedNameModifiers.push_back(OMPD_target);
3530 break;
Kelvin Li02532872016-08-05 14:37:37 +00003531 case OMPD_teams_distribute:
David Majnemer9d168222016-08-05 17:44:54 +00003532 Res = ActOnOpenMPTeamsDistributeDirective(
3533 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Kelvin Li02532872016-08-05 14:37:37 +00003534 break;
Kelvin Li4e325f72016-10-25 12:50:55 +00003535 case OMPD_teams_distribute_simd:
3536 Res = ActOnOpenMPTeamsDistributeSimdDirective(
3537 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3538 break;
Kelvin Li579e41c2016-11-30 23:51:03 +00003539 case OMPD_teams_distribute_parallel_for_simd:
3540 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
3541 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3542 AllowedNameModifiers.push_back(OMPD_parallel);
3543 break;
Kelvin Li7ade93f2016-12-09 03:24:30 +00003544 case OMPD_teams_distribute_parallel_for:
3545 Res = ActOnOpenMPTeamsDistributeParallelForDirective(
3546 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3547 AllowedNameModifiers.push_back(OMPD_parallel);
3548 break;
Kelvin Libf594a52016-12-17 05:48:59 +00003549 case OMPD_target_teams:
3550 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc,
3551 EndLoc);
3552 AllowedNameModifiers.push_back(OMPD_target);
3553 break;
Kelvin Li83c451e2016-12-25 04:52:54 +00003554 case OMPD_target_teams_distribute:
3555 Res = ActOnOpenMPTargetTeamsDistributeDirective(
3556 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3557 AllowedNameModifiers.push_back(OMPD_target);
3558 break;
Kelvin Li80e8f562016-12-29 22:16:30 +00003559 case OMPD_target_teams_distribute_parallel_for:
3560 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
3561 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3562 AllowedNameModifiers.push_back(OMPD_target);
3563 AllowedNameModifiers.push_back(OMPD_parallel);
3564 break;
Kelvin Li1851df52017-01-03 05:23:48 +00003565 case OMPD_target_teams_distribute_parallel_for_simd:
3566 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
3567 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3568 AllowedNameModifiers.push_back(OMPD_target);
3569 AllowedNameModifiers.push_back(OMPD_parallel);
3570 break;
Kelvin Lida681182017-01-10 18:08:18 +00003571 case OMPD_target_teams_distribute_simd:
3572 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
3573 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3574 AllowedNameModifiers.push_back(OMPD_target);
3575 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003576 case OMPD_declare_target:
3577 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003578 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003579 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003580 case OMPD_declare_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00003581 case OMPD_requires:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003582 llvm_unreachable("OpenMP Directive is not allowed");
3583 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003584 llvm_unreachable("Unknown OpenMP directive");
3585 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003586
Alexey Bataeve3727102018-04-18 15:57:46 +00003587 for (const auto &P : VarsWithInheritedDSA) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003588 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3589 << P.first << P.second->getSourceRange();
3590 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003591 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3592
3593 if (!AllowedNameModifiers.empty())
3594 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3595 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003596
Alexey Bataeved09d242014-05-28 05:53:51 +00003597 if (ErrorFound)
3598 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003599 return Res;
3600}
3601
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003602Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3603 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00003604 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00003605 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3606 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003607 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00003608 assert(Linears.size() == LinModifiers.size());
3609 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00003610 if (!DG || DG.get().isNull())
3611 return DeclGroupPtrTy();
3612
3613 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00003614 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003615 return DG;
3616 }
Alexey Bataeve3727102018-04-18 15:57:46 +00003617 Decl *ADecl = DG.get().getSingleDecl();
Alexey Bataev587e1de2016-03-30 10:43:55 +00003618 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3619 ADecl = FTD->getTemplatedDecl();
3620
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003621 auto *FD = dyn_cast<FunctionDecl>(ADecl);
3622 if (!FD) {
3623 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003624 return DeclGroupPtrTy();
3625 }
3626
Alexey Bataev2af33e32016-04-07 12:45:37 +00003627 // OpenMP [2.8.2, declare simd construct, Description]
3628 // The parameter of the simdlen clause must be a constant positive integer
3629 // expression.
3630 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003631 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00003632 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003633 // OpenMP [2.8.2, declare simd construct, Description]
3634 // The special this pointer can be used as if was one of the arguments to the
3635 // function in any of the linear, aligned, or uniform clauses.
3636 // The uniform clause declares one or more arguments to have an invariant
3637 // value for all concurrent invocations of the function in the execution of a
3638 // single SIMD loop.
Alexey Bataeve3727102018-04-18 15:57:46 +00003639 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs;
3640 const Expr *UniformedLinearThis = nullptr;
3641 for (const Expr *E : Uniforms) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003642 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003643 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3644 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003645 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3646 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00003647 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
Alexey Bataev43a919f2018-04-13 17:48:43 +00003648 UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003649 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003650 }
3651 if (isa<CXXThisExpr>(E)) {
3652 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003653 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003654 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003655 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3656 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00003657 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00003658 // OpenMP [2.8.2, declare simd construct, Description]
3659 // The aligned clause declares that the object to which each list item points
3660 // is aligned to the number of bytes expressed in the optional parameter of
3661 // the aligned clause.
3662 // The special this pointer can be used as if was one of the arguments to the
3663 // function in any of the linear, aligned, or uniform clauses.
3664 // The type of list items appearing in the aligned clause must be array,
3665 // pointer, reference to array, or reference to pointer.
Alexey Bataeve3727102018-04-18 15:57:46 +00003666 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs;
3667 const Expr *AlignedThis = nullptr;
3668 for (const Expr *E : Aligneds) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003669 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003670 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3671 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3672 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevd93d3762016-04-12 09:35:56 +00003673 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3674 FD->getParamDecl(PVD->getFunctionScopeIndex())
3675 ->getCanonicalDecl() == CanonPVD) {
3676 // OpenMP [2.8.1, simd construct, Restrictions]
3677 // A list-item cannot appear in more than one aligned clause.
3678 if (AlignedArgs.count(CanonPVD) > 0) {
3679 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3680 << 1 << E->getSourceRange();
3681 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3682 diag::note_omp_explicit_dsa)
3683 << getOpenMPClauseName(OMPC_aligned);
3684 continue;
3685 }
3686 AlignedArgs[CanonPVD] = E;
3687 QualType QTy = PVD->getType()
3688 .getNonReferenceType()
3689 .getUnqualifiedType()
3690 .getCanonicalType();
3691 const Type *Ty = QTy.getTypePtrOrNull();
3692 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3693 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3694 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3695 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3696 }
3697 continue;
3698 }
3699 }
3700 if (isa<CXXThisExpr>(E)) {
3701 if (AlignedThis) {
3702 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3703 << 2 << E->getSourceRange();
3704 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3705 << getOpenMPClauseName(OMPC_aligned);
3706 }
3707 AlignedThis = E;
3708 continue;
3709 }
3710 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3711 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3712 }
3713 // The optional parameter of the aligned clause, alignment, must be a constant
3714 // positive integer expression. If no optional parameter is specified,
3715 // implementation-defined default alignments for SIMD instructions on the
3716 // target platforms are assumed.
Alexey Bataeve3727102018-04-18 15:57:46 +00003717 SmallVector<const Expr *, 4> NewAligns;
3718 for (Expr *E : Alignments) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003719 ExprResult Align;
3720 if (E)
3721 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3722 NewAligns.push_back(Align.get());
3723 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00003724 // OpenMP [2.8.2, declare simd construct, Description]
3725 // The linear clause declares one or more list items to be private to a SIMD
3726 // lane and to have a linear relationship with respect to the iteration space
3727 // of a loop.
3728 // The special this pointer can be used as if was one of the arguments to the
3729 // function in any of the linear, aligned, or uniform clauses.
3730 // When a linear-step expression is specified in a linear clause it must be
3731 // either a constant integer expression or an integer-typed parameter that is
3732 // specified in a uniform clause on the directive.
Alexey Bataeve3727102018-04-18 15:57:46 +00003733 llvm::DenseMap<const Decl *, const Expr *> LinearArgs;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003734 const bool IsUniformedThis = UniformedLinearThis != nullptr;
3735 auto MI = LinModifiers.begin();
Alexey Bataeve3727102018-04-18 15:57:46 +00003736 for (const Expr *E : Linears) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003737 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3738 ++MI;
3739 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003740 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3741 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3742 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevecba70f2016-04-12 11:02:11 +00003743 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3744 FD->getParamDecl(PVD->getFunctionScopeIndex())
3745 ->getCanonicalDecl() == CanonPVD) {
3746 // OpenMP [2.15.3.7, linear Clause, Restrictions]
3747 // A list-item cannot appear in more than one linear clause.
3748 if (LinearArgs.count(CanonPVD) > 0) {
3749 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3750 << getOpenMPClauseName(OMPC_linear)
3751 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3752 Diag(LinearArgs[CanonPVD]->getExprLoc(),
3753 diag::note_omp_explicit_dsa)
3754 << getOpenMPClauseName(OMPC_linear);
3755 continue;
3756 }
3757 // Each argument can appear in at most one uniform or linear clause.
3758 if (UniformedArgs.count(CanonPVD) > 0) {
3759 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3760 << getOpenMPClauseName(OMPC_linear)
3761 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3762 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3763 diag::note_omp_explicit_dsa)
3764 << getOpenMPClauseName(OMPC_uniform);
3765 continue;
3766 }
3767 LinearArgs[CanonPVD] = E;
3768 if (E->isValueDependent() || E->isTypeDependent() ||
3769 E->isInstantiationDependent() ||
3770 E->containsUnexpandedParameterPack())
3771 continue;
3772 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3773 PVD->getOriginalType());
3774 continue;
3775 }
3776 }
3777 if (isa<CXXThisExpr>(E)) {
3778 if (UniformedLinearThis) {
3779 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3780 << getOpenMPClauseName(OMPC_linear)
3781 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3782 << E->getSourceRange();
3783 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3784 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3785 : OMPC_linear);
3786 continue;
3787 }
3788 UniformedLinearThis = E;
3789 if (E->isValueDependent() || E->isTypeDependent() ||
3790 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3791 continue;
3792 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3793 E->getType());
3794 continue;
3795 }
3796 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3797 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3798 }
3799 Expr *Step = nullptr;
3800 Expr *NewStep = nullptr;
3801 SmallVector<Expr *, 4> NewSteps;
Alexey Bataeve3727102018-04-18 15:57:46 +00003802 for (Expr *E : Steps) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003803 // Skip the same step expression, it was checked already.
3804 if (Step == E || !E) {
3805 NewSteps.push_back(E ? NewStep : nullptr);
3806 continue;
3807 }
3808 Step = E;
Alexey Bataeve3727102018-04-18 15:57:46 +00003809 if (const auto *DRE = dyn_cast<DeclRefExpr>(Step))
3810 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3811 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevecba70f2016-04-12 11:02:11 +00003812 if (UniformedArgs.count(CanonPVD) == 0) {
3813 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3814 << Step->getSourceRange();
3815 } else if (E->isValueDependent() || E->isTypeDependent() ||
3816 E->isInstantiationDependent() ||
3817 E->containsUnexpandedParameterPack() ||
Alexey Bataeve3727102018-04-18 15:57:46 +00003818 CanonPVD->getType()->hasIntegerRepresentation()) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003819 NewSteps.push_back(Step);
Alexey Bataeve3727102018-04-18 15:57:46 +00003820 } else {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003821 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3822 << Step->getSourceRange();
3823 }
3824 continue;
3825 }
3826 NewStep = Step;
3827 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3828 !Step->isInstantiationDependent() &&
3829 !Step->containsUnexpandedParameterPack()) {
3830 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3831 .get();
3832 if (NewStep)
3833 NewStep = VerifyIntegerConstantExpression(NewStep).get();
3834 }
3835 NewSteps.push_back(NewStep);
3836 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003837 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3838 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00003839 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00003840 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3841 const_cast<Expr **>(Linears.data()), Linears.size(),
3842 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3843 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003844 ADecl->addAttr(NewAttr);
3845 return ConvertDeclToDeclGroup(ADecl);
3846}
3847
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003848StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3849 Stmt *AStmt,
3850 SourceLocation StartLoc,
3851 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003852 if (!AStmt)
3853 return StmtError();
3854
Alexey Bataeve3727102018-04-18 15:57:46 +00003855 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev9959db52014-05-06 10:08:46 +00003856 // 1.2.2 OpenMP Language Terminology
3857 // Structured block - An executable statement with a single entry at the
3858 // top and a single exit at the bottom.
3859 // The point of exit cannot be a branch out of the structured block.
3860 // longjmp() and throw() must not violate the entry/exit criteria.
3861 CS->getCapturedDecl()->setNothrow();
3862
Reid Kleckner87a31802018-03-12 21:43:02 +00003863 setFunctionHasBranchProtectedScope();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003864
Alexey Bataev25e5b442015-09-15 12:52:43 +00003865 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3866 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003867}
3868
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003869namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003870/// Helper class for checking canonical form of the OpenMP loops and
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003871/// extracting iteration space of each loop in the loop nest, that will be used
3872/// for IR generation.
3873class OpenMPIterationSpaceChecker {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003874 /// Reference to Sema.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003875 Sema &SemaRef;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003876 /// A location for diagnostics (when there is no some better location).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003877 SourceLocation DefaultLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003878 /// A location for diagnostics (when increment is not compatible).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003879 SourceLocation ConditionLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003880 /// A source location for referring to loop init later.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003881 SourceRange InitSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003882 /// A source location for referring to condition later.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003883 SourceRange ConditionSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003884 /// A source location for referring to increment later.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003885 SourceRange IncrementSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003886 /// Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003887 ValueDecl *LCDecl = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003888 /// Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003889 Expr *LCRef = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003890 /// Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003891 Expr *LB = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003892 /// Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003893 Expr *UB = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003894 /// Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003895 Expr *Step = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003896 /// This flag is true when condition is one of:
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003897 /// Var < UB
3898 /// Var <= UB
3899 /// UB > Var
3900 /// UB >= Var
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003901 bool TestIsLessOp = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003902 /// This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003903 bool TestIsStrictOp = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003904 /// This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003905 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003906
3907public:
3908 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003909 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003910 /// Check init-expr for canonical loop form and save loop counter
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003911 /// variable - #Var and its initialization value - #LB.
Alexey Bataeve3727102018-04-18 15:57:46 +00003912 bool checkAndSetInit(Stmt *S, bool EmitDiags = true);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003913 /// Check test-expr for canonical form, save upper-bound (#UB), flags
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003914 /// for less/greater and for strict/non-strict comparison.
Alexey Bataeve3727102018-04-18 15:57:46 +00003915 bool checkAndSetCond(Expr *S);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003916 /// Check incr-expr for canonical loop form and return true if it
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003917 /// does not conform, otherwise save loop step (#Step).
Alexey Bataeve3727102018-04-18 15:57:46 +00003918 bool checkAndSetInc(Expr *S);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003919 /// Return the loop counter variable.
Alexey Bataeve3727102018-04-18 15:57:46 +00003920 ValueDecl *getLoopDecl() const { return LCDecl; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003921 /// Return the reference expression to loop counter variable.
Alexey Bataeve3727102018-04-18 15:57:46 +00003922 Expr *getLoopDeclRefExpr() const { return LCRef; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003923 /// Source range of the loop init.
Alexey Bataeve3727102018-04-18 15:57:46 +00003924 SourceRange getInitSrcRange() const { return InitSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003925 /// Source range of the loop condition.
Alexey Bataeve3727102018-04-18 15:57:46 +00003926 SourceRange getConditionSrcRange() const { return ConditionSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003927 /// Source range of the loop increment.
Alexey Bataeve3727102018-04-18 15:57:46 +00003928 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003929 /// True if the step should be subtracted.
Alexey Bataeve3727102018-04-18 15:57:46 +00003930 bool shouldSubtractStep() const { return SubtractStep; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003931 /// Build the expression to calculate the number of iterations.
Alexey Bataeve3727102018-04-18 15:57:46 +00003932 Expr *buildNumIterations(
3933 Scope *S, const bool LimitedType,
3934 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003935 /// Build the precondition expression for the loops.
Alexey Bataeve3727102018-04-18 15:57:46 +00003936 Expr *
3937 buildPreCond(Scope *S, Expr *Cond,
3938 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003939 /// Build reference expression to the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00003940 DeclRefExpr *
3941 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
3942 DSAStackTy &DSA) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003943 /// Build reference expression to the private counter be used for
Alexey Bataeva8899172015-08-06 12:30:57 +00003944 /// codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00003945 Expr *buildPrivateCounterVar() const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003946 /// Build initialization of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00003947 Expr *buildCounterInit() const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003948 /// Build step of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00003949 Expr *buildCounterStep() const;
Alexey Bataevf138fda2018-08-13 19:04:24 +00003950 /// Build loop data with counter value for depend clauses in ordered
3951 /// directives.
3952 Expr *
3953 buildOrderedLoopData(Scope *S, Expr *Counter,
3954 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
3955 SourceLocation Loc, Expr *Inc = nullptr,
3956 OverloadedOperatorKind OOK = OO_Amp);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003957 /// Return true if any expression is dependent.
Alexey Bataeve3727102018-04-18 15:57:46 +00003958 bool dependent() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003959
3960private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003961 /// Check the right-hand side of an assignment in the increment
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003962 /// expression.
Alexey Bataeve3727102018-04-18 15:57:46 +00003963 bool checkAndSetIncRHS(Expr *RHS);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003964 /// Helper to set loop counter variable and its initializer.
Alexey Bataeve3727102018-04-18 15:57:46 +00003965 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003966 /// Helper to set upper bound.
Alexey Bataeve3727102018-04-18 15:57:46 +00003967 bool setUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003968 SourceLocation SL);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003969 /// Helper to set loop increment.
Alexey Bataeve3727102018-04-18 15:57:46 +00003970 bool setStep(Expr *NewStep, bool Subtract);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003971};
3972
Alexey Bataeve3727102018-04-18 15:57:46 +00003973bool OpenMPIterationSpaceChecker::dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003974 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003975 assert(!LB && !UB && !Step);
3976 return false;
3977 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003978 return LCDecl->getType()->isDependentType() ||
3979 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
3980 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003981}
3982
Alexey Bataeve3727102018-04-18 15:57:46 +00003983bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl,
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003984 Expr *NewLCRefExpr,
3985 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003986 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003987 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003988 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003989 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003990 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003991 LCDecl = getCanonicalDecl(NewLCDecl);
3992 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003993 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3994 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003995 if ((Ctor->isCopyOrMoveConstructor() ||
3996 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3997 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003998 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003999 LB = NewLB;
4000 return false;
4001}
4002
Alexey Bataeve3727102018-04-18 15:57:46 +00004003bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00004004 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004005 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004006 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
4007 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004008 if (!NewUB)
4009 return true;
4010 UB = NewUB;
4011 TestIsLessOp = LessOp;
4012 TestIsStrictOp = StrictOp;
4013 ConditionSrcRange = SR;
4014 ConditionLoc = SL;
4015 return false;
4016}
4017
Alexey Bataeve3727102018-04-18 15:57:46 +00004018bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004019 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004020 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004021 if (!NewStep)
4022 return true;
4023 if (!NewStep->isValueDependent()) {
4024 // Check that the step is integer expression.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004025 SourceLocation StepLoc = NewStep->getBeginLoc();
Alexey Bataev5372fb82017-08-31 23:06:52 +00004026 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion(
4027 StepLoc, getExprAsWritten(NewStep));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004028 if (Val.isInvalid())
4029 return true;
4030 NewStep = Val.get();
4031
4032 // OpenMP [2.6, Canonical Loop Form, Restrictions]
4033 // If test-expr is of form var relational-op b and relational-op is < or
4034 // <= then incr-expr must cause var to increase on each iteration of the
4035 // loop. If test-expr is of form var relational-op b and relational-op is
4036 // > or >= then incr-expr must cause var to decrease on each iteration of
4037 // the loop.
4038 // If test-expr is of form b relational-op var and relational-op is < or
4039 // <= then incr-expr must cause var to decrease on each iteration of the
4040 // loop. If test-expr is of form b relational-op var and relational-op is
4041 // > or >= then incr-expr must cause var to increase on each iteration of
4042 // the loop.
4043 llvm::APSInt Result;
4044 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
4045 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
4046 bool IsConstNeg =
4047 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004048 bool IsConstPos =
4049 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004050 bool IsConstZero = IsConstant && !Result.getBoolValue();
4051 if (UB && (IsConstZero ||
4052 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00004053 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004054 SemaRef.Diag(NewStep->getExprLoc(),
4055 diag::err_omp_loop_incr_not_compatible)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004056 << LCDecl << TestIsLessOp << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004057 SemaRef.Diag(ConditionLoc,
4058 diag::note_omp_loop_cond_requres_compatible_incr)
4059 << TestIsLessOp << ConditionSrcRange;
4060 return true;
4061 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004062 if (TestIsLessOp == Subtract) {
David Majnemer9d168222016-08-05 17:44:54 +00004063 NewStep =
4064 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep)
4065 .get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004066 Subtract = !Subtract;
4067 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004068 }
4069
4070 Step = NewStep;
4071 SubtractStep = Subtract;
4072 return false;
4073}
4074
Alexey Bataeve3727102018-04-18 15:57:46 +00004075bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004076 // Check init-expr for canonical loop form and save loop counter
4077 // variable - #Var and its initialization value - #LB.
4078 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
4079 // var = lb
4080 // integer-type var = lb
4081 // random-access-iterator-type var = lb
4082 // pointer-type var = lb
4083 //
4084 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00004085 if (EmitDiags) {
4086 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
4087 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004088 return true;
4089 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004090 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4091 if (!ExprTemp->cleanupsHaveSideEffects())
4092 S = ExprTemp->getSubExpr();
4093
Alexander Musmana5f070a2014-10-01 06:03:56 +00004094 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004095 if (Expr *E = dyn_cast<Expr>(S))
4096 S = E->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00004097 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004098 if (BO->getOpcode() == BO_Assign) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004099 Expr *LHS = BO->getLHS()->IgnoreParens();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004100 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
4101 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4102 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataeve3727102018-04-18 15:57:46 +00004103 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4104 return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004105 }
4106 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4107 if (ME->isArrow() &&
4108 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
Alexey Bataeve3727102018-04-18 15:57:46 +00004109 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004110 }
4111 }
David Majnemer9d168222016-08-05 17:44:54 +00004112 } else if (auto *DS = dyn_cast<DeclStmt>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004113 if (DS->isSingleDecl()) {
David Majnemer9d168222016-08-05 17:44:54 +00004114 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00004115 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004116 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00004117 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004118 SemaRef.Diag(S->getBeginLoc(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004119 diag::ext_omp_loop_not_canonical_init)
4120 << S->getSourceRange();
Alexey Bataevf138fda2018-08-13 19:04:24 +00004121 return setLCDeclAndLB(
4122 Var,
4123 buildDeclRefExpr(SemaRef, Var,
4124 Var->getType().getNonReferenceType(),
4125 DS->getBeginLoc()),
4126 Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004127 }
4128 }
4129 }
David Majnemer9d168222016-08-05 17:44:54 +00004130 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004131 if (CE->getOperator() == OO_Equal) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004132 Expr *LHS = CE->getArg(0);
David Majnemer9d168222016-08-05 17:44:54 +00004133 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004134 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4135 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataeve3727102018-04-18 15:57:46 +00004136 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4137 return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004138 }
4139 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4140 if (ME->isArrow() &&
4141 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
Alexey Bataeve3727102018-04-18 15:57:46 +00004142 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004143 }
4144 }
4145 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004146
Alexey Bataeve3727102018-04-18 15:57:46 +00004147 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004148 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00004149 if (EmitDiags) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004150 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init)
Alexey Bataev9c821032015-04-30 04:23:23 +00004151 << S->getSourceRange();
4152 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004153 return true;
4154}
4155
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004156/// Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004157/// variable (which may be the loop variable) if possible.
Alexey Bataeve3727102018-04-18 15:57:46 +00004158static const ValueDecl *getInitLCDecl(const Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004159 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00004160 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004161 E = getExprAsWritten(E);
Alexey Bataeve3727102018-04-18 15:57:46 +00004162 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004163 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004164 if ((Ctor->isCopyOrMoveConstructor() ||
4165 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4166 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004167 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00004168 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
4169 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004170 return getCanonicalDecl(VD);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004171 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004172 if (const auto *ME = dyn_cast_or_null<MemberExpr>(E))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004173 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4174 return getCanonicalDecl(ME->getMemberDecl());
4175 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004176}
4177
Alexey Bataeve3727102018-04-18 15:57:46 +00004178bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004179 // Check test-expr for canonical form, save upper-bound UB, flags for
4180 // less/greater and for strict/non-strict comparison.
4181 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4182 // var relational-op b
4183 // b relational-op var
4184 //
4185 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004186 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004187 return true;
4188 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004189 S = getExprAsWritten(S);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004190 SourceLocation CondLoc = S->getBeginLoc();
David Majnemer9d168222016-08-05 17:44:54 +00004191 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004192 if (BO->isRelationalOp()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004193 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4194 return setUB(BO->getRHS(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004195 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
4196 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4197 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +00004198 if (getInitLCDecl(BO->getRHS()) == LCDecl)
4199 return setUB(BO->getLHS(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004200 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
4201 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4202 BO->getSourceRange(), BO->getOperatorLoc());
4203 }
David Majnemer9d168222016-08-05 17:44:54 +00004204 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004205 if (CE->getNumArgs() == 2) {
4206 auto Op = CE->getOperator();
4207 switch (Op) {
4208 case OO_Greater:
4209 case OO_GreaterEqual:
4210 case OO_Less:
4211 case OO_LessEqual:
Alexey Bataeve3727102018-04-18 15:57:46 +00004212 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4213 return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004214 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4215 CE->getOperatorLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +00004216 if (getInitLCDecl(CE->getArg(1)) == LCDecl)
4217 return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004218 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4219 CE->getOperatorLoc());
4220 break;
4221 default:
4222 break;
4223 }
4224 }
4225 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004226 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004227 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004228 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004229 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004230 return true;
4231}
4232
Alexey Bataeve3727102018-04-18 15:57:46 +00004233bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004234 // RHS of canonical loop form increment can be:
4235 // var + incr
4236 // incr + var
4237 // var - incr
4238 //
4239 RHS = RHS->IgnoreParenImpCasts();
David Majnemer9d168222016-08-05 17:44:54 +00004240 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004241 if (BO->isAdditiveOp()) {
4242 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataeve3727102018-04-18 15:57:46 +00004243 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4244 return setStep(BO->getRHS(), !IsAdd);
4245 if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl)
4246 return setStep(BO->getLHS(), /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004247 }
David Majnemer9d168222016-08-05 17:44:54 +00004248 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004249 bool IsAdd = CE->getOperator() == OO_Plus;
4250 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004251 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4252 return setStep(CE->getArg(1), !IsAdd);
4253 if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl)
4254 return setStep(CE->getArg(0), /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004255 }
4256 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004257 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004258 return false;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004259 SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004260 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004261 return true;
4262}
4263
Alexey Bataeve3727102018-04-18 15:57:46 +00004264bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004265 // Check incr-expr for canonical loop form and return true if it
4266 // does not conform.
4267 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4268 // ++var
4269 // var++
4270 // --var
4271 // var--
4272 // var += incr
4273 // var -= incr
4274 // var = var + incr
4275 // var = incr + var
4276 // var = var - incr
4277 //
4278 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004279 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004280 return true;
4281 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004282 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4283 if (!ExprTemp->cleanupsHaveSideEffects())
4284 S = ExprTemp->getSubExpr();
4285
Alexander Musmana5f070a2014-10-01 06:03:56 +00004286 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004287 S = S->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00004288 if (auto *UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004289 if (UO->isIncrementDecrementOp() &&
Alexey Bataeve3727102018-04-18 15:57:46 +00004290 getInitLCDecl(UO->getSubExpr()) == LCDecl)
4291 return setStep(SemaRef
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004292 .ActOnIntegerConstant(UO->getBeginLoc(),
David Majnemer9d168222016-08-05 17:44:54 +00004293 (UO->isDecrementOp() ? -1 : 1))
4294 .get(),
Alexey Bataeve3727102018-04-18 15:57:46 +00004295 /*Subtract=*/false);
David Majnemer9d168222016-08-05 17:44:54 +00004296 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004297 switch (BO->getOpcode()) {
4298 case BO_AddAssign:
4299 case BO_SubAssign:
Alexey Bataeve3727102018-04-18 15:57:46 +00004300 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4301 return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004302 break;
4303 case BO_Assign:
Alexey Bataeve3727102018-04-18 15:57:46 +00004304 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4305 return checkAndSetIncRHS(BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004306 break;
4307 default:
4308 break;
4309 }
David Majnemer9d168222016-08-05 17:44:54 +00004310 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004311 switch (CE->getOperator()) {
4312 case OO_PlusPlus:
4313 case OO_MinusMinus:
Alexey Bataeve3727102018-04-18 15:57:46 +00004314 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4315 return setStep(SemaRef
David Majnemer9d168222016-08-05 17:44:54 +00004316 .ActOnIntegerConstant(
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004317 CE->getBeginLoc(),
David Majnemer9d168222016-08-05 17:44:54 +00004318 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
4319 .get(),
Alexey Bataeve3727102018-04-18 15:57:46 +00004320 /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004321 break;
4322 case OO_PlusEqual:
4323 case OO_MinusEqual:
Alexey Bataeve3727102018-04-18 15:57:46 +00004324 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4325 return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004326 break;
4327 case OO_Equal:
Alexey Bataeve3727102018-04-18 15:57:46 +00004328 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4329 return checkAndSetIncRHS(CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004330 break;
4331 default:
4332 break;
4333 }
4334 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004335 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004336 return false;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004337 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004338 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004339 return true;
4340}
Alexander Musmana5f070a2014-10-01 06:03:56 +00004341
Alexey Bataev5a3af132016-03-29 08:58:54 +00004342static ExprResult
4343tryBuildCapture(Sema &SemaRef, Expr *Capture,
Alexey Bataeve3727102018-04-18 15:57:46 +00004344 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00004345 if (SemaRef.CurContext->isDependentContext())
4346 return ExprResult(Capture);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004347 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
4348 return SemaRef.PerformImplicitConversion(
4349 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
4350 /*AllowExplicit=*/true);
4351 auto I = Captures.find(Capture);
4352 if (I != Captures.end())
4353 return buildCapture(SemaRef, Capture, I->second);
4354 DeclRefExpr *Ref = nullptr;
4355 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4356 Captures[Capture] = Ref;
4357 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004358}
4359
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004360/// Build the expression to calculate the number of iterations.
Alexey Bataeve3727102018-04-18 15:57:46 +00004361Expr *OpenMPIterationSpaceChecker::buildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004362 Scope *S, const bool LimitedType,
Alexey Bataeve3727102018-04-18 15:57:46 +00004363 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004364 ExprResult Diff;
Alexey Bataeve3727102018-04-18 15:57:46 +00004365 QualType VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004366 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004367 SemaRef.getLangOpts().CPlusPlus) {
4368 // Upper - Lower
Alexey Bataeve3727102018-04-18 15:57:46 +00004369 Expr *UBExpr = TestIsLessOp ? UB : LB;
4370 Expr *LBExpr = TestIsLessOp ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00004371 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
4372 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004373 if (!Upper || !Lower)
4374 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004375
4376 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4377
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004378 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004379 // BuildBinOp already emitted error, this one is to point user to upper
4380 // and lower bound, and to tell what is passed to 'operator-'.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004381 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004382 << Upper->getSourceRange() << Lower->getSourceRange();
4383 return nullptr;
4384 }
4385 }
4386
4387 if (!Diff.isUsable())
4388 return nullptr;
4389
4390 // Upper - Lower [- 1]
4391 if (TestIsStrictOp)
4392 Diff = SemaRef.BuildBinOp(
4393 S, DefaultLoc, BO_Sub, Diff.get(),
4394 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4395 if (!Diff.isUsable())
4396 return nullptr;
4397
4398 // Upper - Lower [- 1] + Step
Alexey Bataeve3727102018-04-18 15:57:46 +00004399 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004400 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004401 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004402 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004403 if (!Diff.isUsable())
4404 return nullptr;
4405
4406 // Parentheses (for dumping/debugging purposes only).
4407 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4408 if (!Diff.isUsable())
4409 return nullptr;
4410
4411 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004412 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004413 if (!Diff.isUsable())
4414 return nullptr;
4415
Alexander Musman174b3ca2014-10-06 11:16:29 +00004416 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004417 QualType Type = Diff.get()->getType();
Alexey Bataeve3727102018-04-18 15:57:46 +00004418 ASTContext &C = SemaRef.Context;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004419 bool UseVarType = VarType->hasIntegerRepresentation() &&
4420 C.getTypeSize(Type) > C.getTypeSize(VarType);
4421 if (!Type->isIntegerType() || UseVarType) {
4422 unsigned NewSize =
4423 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4424 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4425 : Type->hasSignedIntegerRepresentation();
4426 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00004427 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4428 Diff = SemaRef.PerformImplicitConversion(
4429 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4430 if (!Diff.isUsable())
4431 return nullptr;
4432 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004433 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004434 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00004435 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4436 if (NewSize != C.getTypeSize(Type)) {
4437 if (NewSize < C.getTypeSize(Type)) {
4438 assert(NewSize == 64 && "incorrect loop var size");
4439 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4440 << InitSrcRange << ConditionSrcRange;
4441 }
4442 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004443 NewSize, Type->hasSignedIntegerRepresentation() ||
4444 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00004445 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4446 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4447 Sema::AA_Converting, true);
4448 if (!Diff.isUsable())
4449 return nullptr;
4450 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004451 }
4452 }
4453
Alexander Musmana5f070a2014-10-01 06:03:56 +00004454 return Diff.get();
4455}
4456
Alexey Bataeve3727102018-04-18 15:57:46 +00004457Expr *OpenMPIterationSpaceChecker::buildPreCond(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004458 Scope *S, Expr *Cond,
Alexey Bataeve3727102018-04-18 15:57:46 +00004459 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004460 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4461 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4462 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004463
Alexey Bataeve3727102018-04-18 15:57:46 +00004464 ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures);
4465 ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004466 if (!NewLB.isUsable() || !NewUB.isUsable())
4467 return nullptr;
4468
Alexey Bataeve3727102018-04-18 15:57:46 +00004469 ExprResult CondExpr =
4470 SemaRef.BuildBinOp(S, DefaultLoc,
4471 TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
4472 : (TestIsStrictOp ? BO_GT : BO_GE),
4473 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004474 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004475 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4476 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00004477 CondExpr = SemaRef.PerformImplicitConversion(
4478 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4479 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004480 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00004481 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4482 // Otherwise use original loop conditon and evaluate it in runtime.
4483 return CondExpr.isUsable() ? CondExpr.get() : Cond;
4484}
4485
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004486/// Build reference expression to the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004487DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar(
Alexey Bataevf138fda2018-08-13 19:04:24 +00004488 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
4489 DSAStackTy &DSA) const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004490 auto *VD = dyn_cast<VarDecl>(LCDecl);
4491 if (!VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004492 VD = SemaRef.isOpenMPCapturedDecl(LCDecl);
4493 DeclRefExpr *Ref = buildDeclRefExpr(
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004494 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00004495 const DSAStackTy::DSAVarData Data =
4496 DSA.getTopDSA(LCDecl, /*FromParent=*/false);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004497 // If the loop control decl is explicitly marked as private, do not mark it
4498 // as captured again.
4499 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4500 Captures.insert(std::make_pair(LCRef, Ref));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004501 return Ref;
4502 }
4503 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00004504 DefaultLoc);
4505}
4506
Alexey Bataeve3727102018-04-18 15:57:46 +00004507Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004508 if (LCDecl && !LCDecl->isInvalidDecl()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004509 QualType Type = LCDecl->getType().getNonReferenceType();
4510 VarDecl *PrivateVar = buildVarDecl(
Alexey Bataev63cc8e92018-03-20 14:45:59 +00004511 SemaRef, DefaultLoc, Type, LCDecl->getName(),
4512 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr,
4513 isa<VarDecl>(LCDecl)
4514 ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc)
4515 : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00004516 if (PrivateVar->isInvalidDecl())
4517 return nullptr;
4518 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4519 }
4520 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004521}
4522
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004523/// Build initialization of the counter to be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004524Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004525
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004526/// Build step of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004527Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004528
Alexey Bataevf138fda2018-08-13 19:04:24 +00004529Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData(
4530 Scope *S, Expr *Counter,
4531 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc,
4532 Expr *Inc, OverloadedOperatorKind OOK) {
4533 Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get();
4534 if (!Cnt)
4535 return nullptr;
4536 if (Inc) {
4537 assert((OOK == OO_Plus || OOK == OO_Minus) &&
4538 "Expected only + or - operations for depend clauses.");
4539 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub;
4540 Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get();
4541 if (!Cnt)
4542 return nullptr;
4543 }
4544 ExprResult Diff;
4545 QualType VarType = LCDecl->getType().getNonReferenceType();
4546 if (VarType->isIntegerType() || VarType->isPointerType() ||
4547 SemaRef.getLangOpts().CPlusPlus) {
4548 // Upper - Lower
4549 Expr *Upper =
4550 TestIsLessOp ? Cnt : tryBuildCapture(SemaRef, UB, Captures).get();
4551 Expr *Lower =
4552 TestIsLessOp ? tryBuildCapture(SemaRef, LB, Captures).get() : Cnt;
4553 if (!Upper || !Lower)
4554 return nullptr;
4555
4556 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4557
4558 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
4559 // BuildBinOp already emitted error, this one is to point user to upper
4560 // and lower bound, and to tell what is passed to 'operator-'.
4561 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
4562 << Upper->getSourceRange() << Lower->getSourceRange();
4563 return nullptr;
4564 }
4565 }
4566
4567 if (!Diff.isUsable())
4568 return nullptr;
4569
4570 // Parentheses (for dumping/debugging purposes only).
4571 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4572 if (!Diff.isUsable())
4573 return nullptr;
4574
4575 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
4576 if (!NewStep.isUsable())
4577 return nullptr;
4578 // (Upper - Lower) / Step
4579 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
4580 if (!Diff.isUsable())
4581 return nullptr;
4582
4583 return Diff.get();
4584}
4585
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004586/// Iteration space of a single for loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004587struct LoopIterationSpace final {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004588 /// Condition of the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004589 Expr *PreCond = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004590 /// This expression calculates the number of iterations in the loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004591 /// It is always possible to calculate it before starting the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004592 Expr *NumIterations = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004593 /// The loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004594 Expr *CounterVar = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004595 /// Private loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004596 Expr *PrivateCounterVar = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004597 /// This is initializer for the initial value of #CounterVar.
Alexey Bataev8b427062016-05-25 12:36:08 +00004598 Expr *CounterInit = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004599 /// This is step for the #CounterVar used to generate its update:
Alexander Musmana5f070a2014-10-01 06:03:56 +00004600 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
Alexey Bataev8b427062016-05-25 12:36:08 +00004601 Expr *CounterStep = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004602 /// Should step be subtracted?
Alexey Bataev8b427062016-05-25 12:36:08 +00004603 bool Subtract = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004604 /// Source range of the loop init.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004605 SourceRange InitSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004606 /// Source range of the loop condition.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004607 SourceRange CondSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004608 /// Source range of the loop increment.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004609 SourceRange IncSrcRange;
4610};
4611
Alexey Bataev23b69422014-06-18 07:08:49 +00004612} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004613
Alexey Bataev9c821032015-04-30 04:23:23 +00004614void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4615 assert(getLangOpts().OpenMP && "OpenMP is not active.");
4616 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004617 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4618 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00004619 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4620 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00004621 if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) {
4622 if (ValueDecl *D = ISC.getLoopDecl()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004623 auto *VD = dyn_cast<VarDecl>(D);
4624 if (!VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004625 if (VarDecl *Private = isOpenMPCapturedDecl(D)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004626 VD = Private;
Alexey Bataeve3727102018-04-18 15:57:46 +00004627 } else {
4628 DeclRefExpr *Ref = buildCapture(*this, D, ISC.getLoopDeclRefExpr(),
4629 /*WithInit=*/false);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004630 VD = cast<VarDecl>(Ref->getDecl());
4631 }
4632 }
4633 DSAStack->addLoopControlVariable(D, VD);
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00004634 const Decl *LD = DSAStack->getPossiblyLoopCunter();
4635 if (LD != D->getCanonicalDecl()) {
4636 DSAStack->resetPossibleLoopCounter();
4637 if (auto *Var = dyn_cast_or_null<VarDecl>(LD))
4638 MarkDeclarationsReferencedInExpr(
4639 buildDeclRefExpr(*this, const_cast<VarDecl *>(Var),
4640 Var->getType().getNonLValueExprType(Context),
4641 ForLoc, /*RefersToCapture=*/true));
4642 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004643 }
4644 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004645 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00004646 }
4647}
4648
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004649/// Called on a for stmt to check and extract its iteration space
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004650/// for further processing (such as collapsing).
Alexey Bataeve3727102018-04-18 15:57:46 +00004651static bool checkOpenMPIterationSpace(
Alexey Bataev4acb8592014-07-07 13:01:15 +00004652 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4653 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataevf138fda2018-08-13 19:04:24 +00004654 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr,
4655 Expr *OrderedLoopCountExpr,
Alexey Bataeve3727102018-04-18 15:57:46 +00004656 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004657 LoopIterationSpace &ResultIterSpace,
Alexey Bataeve3727102018-04-18 15:57:46 +00004658 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004659 // OpenMP [2.6, Canonical Loop Form]
4660 // for (init-expr; test-expr; incr-expr) structured-block
David Majnemer9d168222016-08-05 17:44:54 +00004661 auto *For = dyn_cast_or_null<ForStmt>(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004662 if (!For) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004663 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004664 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
Alexey Bataevf138fda2018-08-13 19:04:24 +00004665 << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount
Alexey Bataev10e775f2015-07-30 11:36:16 +00004666 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
Alexey Bataevf138fda2018-08-13 19:04:24 +00004667 if (TotalNestedLoopCount > 1) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00004668 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4669 SemaRef.Diag(DSA.getConstructLoc(),
4670 diag::note_omp_collapse_ordered_expr)
4671 << 2 << CollapseLoopCountExpr->getSourceRange()
4672 << OrderedLoopCountExpr->getSourceRange();
4673 else if (CollapseLoopCountExpr)
4674 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4675 diag::note_omp_collapse_ordered_expr)
4676 << 0 << CollapseLoopCountExpr->getSourceRange();
4677 else
4678 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4679 diag::note_omp_collapse_ordered_expr)
4680 << 1 << OrderedLoopCountExpr->getSourceRange();
4681 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004682 return true;
4683 }
4684 assert(For->getBody());
4685
4686 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4687
4688 // Check init.
Alexey Bataeve3727102018-04-18 15:57:46 +00004689 Stmt *Init = For->getInit();
4690 if (ISC.checkAndSetInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004691 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004692
4693 bool HasErrors = false;
4694
4695 // Check loop variable's type.
Alexey Bataeve3727102018-04-18 15:57:46 +00004696 if (ValueDecl *LCDecl = ISC.getLoopDecl()) {
4697 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004698
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004699 // OpenMP [2.6, Canonical Loop Form]
4700 // Var is one of the following:
4701 // A variable of signed or unsigned integer type.
4702 // For C++, a variable of a random access iterator type.
4703 // For C, a variable of a pointer type.
Alexey Bataeve3727102018-04-18 15:57:46 +00004704 QualType VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004705 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4706 !VarType->isPointerType() &&
4707 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004708 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004709 << SemaRef.getLangOpts().CPlusPlus;
4710 HasErrors = true;
4711 }
4712
4713 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4714 // a Construct
4715 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4716 // parallel for construct is (are) private.
4717 // The loop iteration variable in the associated for-loop of a simd
4718 // construct with just one associated for-loop is linear with a
4719 // constant-linear-step that is the increment of the associated for-loop.
4720 // Exclude loop var from the list of variables with implicitly defined data
4721 // sharing attributes.
4722 VarsWithImplicitDSA.erase(LCDecl);
4723
4724 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4725 // in a Construct, C/C++].
4726 // The loop iteration variable in the associated for-loop of a simd
4727 // construct with just one associated for-loop may be listed in a linear
4728 // clause with a constant-linear-step that is the increment of the
4729 // associated for-loop.
4730 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4731 // parallel for construct may be listed in a private or lastprivate clause.
4732 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4733 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4734 // declared in the loop and it is predetermined as a private.
Alexey Bataeve3727102018-04-18 15:57:46 +00004735 OpenMPClauseKind PredeterminedCKind =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004736 isOpenMPSimdDirective(DKind)
4737 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4738 : OMPC_private;
4739 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4740 DVar.CKind != PredeterminedCKind) ||
4741 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4742 isOpenMPDistributeDirective(DKind)) &&
4743 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4744 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4745 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004746 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004747 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4748 << getOpenMPClauseName(PredeterminedCKind);
4749 if (DVar.RefExpr == nullptr)
4750 DVar.CKind = PredeterminedCKind;
Alexey Bataeve3727102018-04-18 15:57:46 +00004751 reportOriginalDsa(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004752 HasErrors = true;
4753 } else if (LoopDeclRefExpr != nullptr) {
4754 // Make the loop iteration variable private (for worksharing constructs),
4755 // linear (for simd directives with the only one associated loop) or
4756 // lastprivate (for simd directives with several collapsed or ordered
4757 // loops).
4758 if (DVar.CKind == OMPC_unknown)
Alexey Bataev7ace49d2016-05-17 08:55:33 +00004759 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
4760 [](OpenMPDirectiveKind) -> bool { return true; },
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004761 /*FromParent=*/false);
4762 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4763 }
4764
4765 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4766
4767 // Check test-expr.
Alexey Bataeve3727102018-04-18 15:57:46 +00004768 HasErrors |= ISC.checkAndSetCond(For->getCond());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004769
4770 // Check incr-expr.
Alexey Bataeve3727102018-04-18 15:57:46 +00004771 HasErrors |= ISC.checkAndSetInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004772 }
4773
Alexey Bataeve3727102018-04-18 15:57:46 +00004774 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004775 return HasErrors;
4776
Alexander Musmana5f070a2014-10-01 06:03:56 +00004777 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004778 ResultIterSpace.PreCond =
Alexey Bataeve3727102018-04-18 15:57:46 +00004779 ISC.buildPreCond(DSA.getCurScope(), For->getCond(), Captures);
4780 ResultIterSpace.NumIterations = ISC.buildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004781 DSA.getCurScope(),
4782 (isOpenMPWorksharingDirective(DKind) ||
4783 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4784 Captures);
Alexey Bataeve3727102018-04-18 15:57:46 +00004785 ResultIterSpace.CounterVar = ISC.buildCounterVar(Captures, DSA);
4786 ResultIterSpace.PrivateCounterVar = ISC.buildPrivateCounterVar();
4787 ResultIterSpace.CounterInit = ISC.buildCounterInit();
4788 ResultIterSpace.CounterStep = ISC.buildCounterStep();
4789 ResultIterSpace.InitSrcRange = ISC.getInitSrcRange();
4790 ResultIterSpace.CondSrcRange = ISC.getConditionSrcRange();
4791 ResultIterSpace.IncSrcRange = ISC.getIncrementSrcRange();
4792 ResultIterSpace.Subtract = ISC.shouldSubtractStep();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004793
Alexey Bataev62dbb972015-04-22 11:59:37 +00004794 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4795 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004796 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004797 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004798 ResultIterSpace.CounterInit == nullptr ||
4799 ResultIterSpace.CounterStep == nullptr);
Alexey Bataevf138fda2018-08-13 19:04:24 +00004800 if (!HasErrors && DSA.isOrderedRegion()) {
4801 if (DSA.getOrderedRegionParam().second->getNumForLoops()) {
4802 if (CurrentNestedLoopCount <
4803 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) {
4804 DSA.getOrderedRegionParam().second->setLoopNumIterations(
4805 CurrentNestedLoopCount, ResultIterSpace.NumIterations);
4806 DSA.getOrderedRegionParam().second->setLoopCounter(
4807 CurrentNestedLoopCount, ResultIterSpace.CounterVar);
4808 }
4809 }
4810 for (auto &Pair : DSA.getDoacrossDependClauses()) {
4811 if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) {
4812 // Erroneous case - clause has some problems.
4813 continue;
4814 }
4815 if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink &&
4816 Pair.second.size() <= CurrentNestedLoopCount) {
4817 // Erroneous case - clause has some problems.
4818 Pair.first->setLoopData(CurrentNestedLoopCount, nullptr);
4819 continue;
4820 }
4821 Expr *CntValue;
4822 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
4823 CntValue = ISC.buildOrderedLoopData(
4824 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures,
4825 Pair.first->getDependencyLoc());
4826 else
4827 CntValue = ISC.buildOrderedLoopData(
4828 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures,
4829 Pair.first->getDependencyLoc(),
4830 Pair.second[CurrentNestedLoopCount].first,
4831 Pair.second[CurrentNestedLoopCount].second);
4832 Pair.first->setLoopData(CurrentNestedLoopCount, CntValue);
4833 }
4834 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004835
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004836 return HasErrors;
4837}
4838
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004839/// Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004840static ExprResult
Alexey Bataeve3727102018-04-18 15:57:46 +00004841buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004842 ExprResult Start,
Alexey Bataeve3727102018-04-18 15:57:46 +00004843 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004844 // Build 'VarRef = Start.
Alexey Bataeve3727102018-04-18 15:57:46 +00004845 ExprResult NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004846 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004847 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004848 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00004849 VarRef.get()->getType())) {
4850 NewStart = SemaRef.PerformImplicitConversion(
4851 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4852 /*AllowExplicit=*/true);
4853 if (!NewStart.isUsable())
4854 return ExprError();
4855 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004856
Alexey Bataeve3727102018-04-18 15:57:46 +00004857 ExprResult Init =
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004858 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4859 return Init;
4860}
4861
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004862/// Build 'VarRef = Start + Iter * Step'.
Alexey Bataeve3727102018-04-18 15:57:46 +00004863static ExprResult buildCounterUpdate(
4864 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4865 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract,
4866 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004867 // Add parentheses (for debugging purposes only).
4868 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4869 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4870 !Step.isUsable())
4871 return ExprError();
4872
Alexey Bataev5a3af132016-03-29 08:58:54 +00004873 ExprResult NewStep = Step;
4874 if (Captures)
4875 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004876 if (NewStep.isInvalid())
4877 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004878 ExprResult Update =
4879 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004880 if (!Update.isUsable())
4881 return ExprError();
4882
Alexey Bataevc0214e02016-02-16 12:13:49 +00004883 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4884 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004885 ExprResult NewStart = Start;
4886 if (Captures)
4887 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004888 if (NewStart.isInvalid())
4889 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004890
Alexey Bataevc0214e02016-02-16 12:13:49 +00004891 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4892 ExprResult SavedUpdate = Update;
4893 ExprResult UpdateVal;
4894 if (VarRef.get()->getType()->isOverloadableType() ||
4895 NewStart.get()->getType()->isOverloadableType() ||
4896 Update.get()->getType()->isOverloadableType()) {
4897 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4898 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4899 Update =
4900 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4901 if (Update.isUsable()) {
4902 UpdateVal =
4903 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4904 VarRef.get(), SavedUpdate.get());
4905 if (UpdateVal.isUsable()) {
4906 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4907 UpdateVal.get());
4908 }
4909 }
4910 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4911 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004912
Alexey Bataevc0214e02016-02-16 12:13:49 +00004913 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4914 if (!Update.isUsable() || !UpdateVal.isUsable()) {
4915 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4916 NewStart.get(), SavedUpdate.get());
4917 if (!Update.isUsable())
4918 return ExprError();
4919
Alexey Bataev11481f52016-02-17 10:29:05 +00004920 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4921 VarRef.get()->getType())) {
4922 Update = SemaRef.PerformImplicitConversion(
4923 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4924 if (!Update.isUsable())
4925 return ExprError();
4926 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00004927
4928 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4929 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004930 return Update;
4931}
4932
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004933/// Convert integer expression \a E to make it have at least \a Bits
Alexander Musmana5f070a2014-10-01 06:03:56 +00004934/// bits.
Alexey Bataeve3727102018-04-18 15:57:46 +00004935static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004936 if (E == nullptr)
4937 return ExprError();
Alexey Bataeve3727102018-04-18 15:57:46 +00004938 ASTContext &C = SemaRef.Context;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004939 QualType OldType = E->getType();
4940 unsigned HasBits = C.getTypeSize(OldType);
4941 if (HasBits >= Bits)
4942 return ExprResult(E);
4943 // OK to convert to signed, because new type has more bits than old.
4944 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4945 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4946 true);
4947}
4948
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004949/// Check if the given expression \a E is a constant integer that fits
Alexander Musmana5f070a2014-10-01 06:03:56 +00004950/// into \a Bits bits.
Alexey Bataeve3727102018-04-18 15:57:46 +00004951static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004952 if (E == nullptr)
4953 return false;
4954 llvm::APSInt Result;
4955 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4956 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4957 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004958}
4959
Alexey Bataev5a3af132016-03-29 08:58:54 +00004960/// Build preinits statement for the given declarations.
4961static Stmt *buildPreInits(ASTContext &Context,
Alexey Bataevc5514062017-10-25 15:44:52 +00004962 MutableArrayRef<Decl *> PreInits) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004963 if (!PreInits.empty()) {
4964 return new (Context) DeclStmt(
4965 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
4966 SourceLocation(), SourceLocation());
4967 }
4968 return nullptr;
4969}
4970
4971/// Build preinits statement for the given declarations.
Alexey Bataevc5514062017-10-25 15:44:52 +00004972static Stmt *
4973buildPreInits(ASTContext &Context,
Alexey Bataeve3727102018-04-18 15:57:46 +00004974 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004975 if (!Captures.empty()) {
4976 SmallVector<Decl *, 16> PreInits;
Alexey Bataeve3727102018-04-18 15:57:46 +00004977 for (const auto &Pair : Captures)
Alexey Bataev5a3af132016-03-29 08:58:54 +00004978 PreInits.push_back(Pair.second->getDecl());
4979 return buildPreInits(Context, PreInits);
4980 }
4981 return nullptr;
4982}
4983
4984/// Build postupdate expression for the given list of postupdates expressions.
4985static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
4986 Expr *PostUpdate = nullptr;
4987 if (!PostUpdates.empty()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004988 for (Expr *E : PostUpdates) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004989 Expr *ConvE = S.BuildCStyleCastExpr(
4990 E->getExprLoc(),
4991 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
4992 E->getExprLoc(), E)
4993 .get();
4994 PostUpdate = PostUpdate
4995 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
4996 PostUpdate, ConvE)
4997 .get()
4998 : ConvE;
4999 }
5000 }
5001 return PostUpdate;
5002}
5003
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005004/// Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00005005/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
5006/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005007static unsigned
Alexey Bataeve3727102018-04-18 15:57:46 +00005008checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
Alexey Bataev10e775f2015-07-30 11:36:16 +00005009 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
5010 DSAStackTy &DSA,
Alexey Bataeve3727102018-04-18 15:57:46 +00005011 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00005012 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005013 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005014 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005015 // Found 'collapse' clause - calculate collapse number.
5016 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005017 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005018 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005019 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00005020 unsigned OrderedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005021 if (OrderedLoopCountExpr) {
5022 // Found 'ordered' clause - calculate collapse number.
5023 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005024 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
5025 if (Result.getLimitedValue() < NestedLoopCount) {
5026 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
5027 diag::err_omp_wrong_ordered_loop_count)
5028 << OrderedLoopCountExpr->getSourceRange();
5029 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
5030 diag::note_collapse_loop_count)
5031 << CollapseLoopCountExpr->getSourceRange();
5032 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00005033 OrderedLoopCount = Result.getLimitedValue();
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005034 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005035 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005036 // This is helper routine for loop directives (e.g., 'for', 'simd',
5037 // 'for simd', etc.).
Alexey Bataeve3727102018-04-18 15:57:46 +00005038 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005039 SmallVector<LoopIterationSpace, 4> IterSpaces;
Alexey Bataevf138fda2018-08-13 19:04:24 +00005040 IterSpaces.resize(std::max(OrderedLoopCount, NestedLoopCount));
Alexander Musmana5f070a2014-10-01 06:03:56 +00005041 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005042 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataevf138fda2018-08-13 19:04:24 +00005043 if (checkOpenMPIterationSpace(
5044 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
5045 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
5046 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt],
5047 Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00005048 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005049 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005050 // OpenMP [2.8.1, simd construct, Restrictions]
5051 // All loops associated with the construct must be perfectly nested; that
5052 // is, there must be no intervening code nor any OpenMP directive between
5053 // any two loops.
5054 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005055 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00005056 for (unsigned Cnt = NestedLoopCount; Cnt < OrderedLoopCount; ++Cnt) {
5057 if (checkOpenMPIterationSpace(
5058 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
5059 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
5060 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt],
5061 Captures))
5062 return 0;
5063 if (Cnt > 0 && IterSpaces[Cnt].CounterVar) {
5064 // Handle initialization of captured loop iterator variables.
5065 auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar);
5066 if (isa<OMPCapturedExprDecl>(DRE->getDecl())) {
5067 Captures[DRE] = DRE;
5068 }
5069 }
5070 // Move on to the next nested for loop, or to the loop body.
5071 // OpenMP [2.8.1, simd construct, Restrictions]
5072 // All loops associated with the construct must be perfectly nested; that
5073 // is, there must be no intervening code nor any OpenMP directive between
5074 // any two loops.
5075 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
5076 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005077
Alexander Musmana5f070a2014-10-01 06:03:56 +00005078 Built.clear(/* size */ NestedLoopCount);
5079
5080 if (SemaRef.CurContext->isDependentContext())
5081 return NestedLoopCount;
5082
5083 // An example of what is generated for the following code:
5084 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00005085 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00005086 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00005087 // for (k = 0; k < NK; ++k)
5088 // for (j = J0; j < NJ; j+=2) {
5089 // <loop body>
5090 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005091 //
5092 // We generate the code below.
5093 // Note: the loop body may be outlined in CodeGen.
5094 // Note: some counters may be C++ classes, operator- is used to find number of
5095 // iterations and operator+= to calculate counter value.
5096 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
5097 // or i64 is currently supported).
5098 //
5099 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
5100 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
5101 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
5102 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
5103 // // similar updates for vars in clauses (e.g. 'linear')
5104 // <loop body (using local i and j)>
5105 // }
5106 // i = NI; // assign final values of counters
5107 // j = NJ;
5108 //
5109
5110 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
5111 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00005112 // Precondition tests if there is at least one iteration (all conditions are
5113 // true).
5114 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexey Bataeve3727102018-04-18 15:57:46 +00005115 Expr *N0 = IterSpaces[0].NumIterations;
5116 ExprResult LastIteration32 =
5117 widenIterationCount(/*Bits=*/32,
5118 SemaRef
5119 .PerformImplicitConversion(
5120 N0->IgnoreImpCasts(), N0->getType(),
5121 Sema::AA_Converting, /*AllowExplicit=*/true)
5122 .get(),
5123 SemaRef);
5124 ExprResult LastIteration64 = widenIterationCount(
5125 /*Bits=*/64,
5126 SemaRef
5127 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(),
5128 Sema::AA_Converting,
5129 /*AllowExplicit=*/true)
5130 .get(),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005131 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005132
5133 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
5134 return NestedLoopCount;
5135
Alexey Bataeve3727102018-04-18 15:57:46 +00005136 ASTContext &C = SemaRef.Context;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005137 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
5138
5139 Scope *CurScope = DSA.getCurScope();
5140 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00005141 if (PreCond.isUsable()) {
Alexey Bataeva7206b92016-12-20 16:51:02 +00005142 PreCond =
5143 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd,
5144 PreCond.get(), IterSpaces[Cnt].PreCond);
Alexey Bataev62dbb972015-04-22 11:59:37 +00005145 }
Alexey Bataeve3727102018-04-18 15:57:46 +00005146 Expr *N = IterSpaces[Cnt].NumIterations;
Alexey Bataeva7206b92016-12-20 16:51:02 +00005147 SourceLocation Loc = N->getExprLoc();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005148 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
5149 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005150 LastIteration32 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005151 CurScope, Loc, BO_Mul, LastIteration32.get(),
David Majnemer9d168222016-08-05 17:44:54 +00005152 SemaRef
5153 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5154 Sema::AA_Converting,
5155 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005156 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005157 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005158 LastIteration64 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005159 CurScope, Loc, BO_Mul, LastIteration64.get(),
David Majnemer9d168222016-08-05 17:44:54 +00005160 SemaRef
5161 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5162 Sema::AA_Converting,
5163 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005164 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005165 }
5166
5167 // Choose either the 32-bit or 64-bit version.
5168 ExprResult LastIteration = LastIteration64;
5169 if (LastIteration32.isUsable() &&
5170 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
5171 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
Alexey Bataeve3727102018-04-18 15:57:46 +00005172 fitsInto(
5173 /*Bits=*/32,
Alexander Musmana5f070a2014-10-01 06:03:56 +00005174 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
5175 LastIteration64.get(), SemaRef)))
5176 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00005177 QualType VType = LastIteration.get()->getType();
5178 QualType RealVType = VType;
5179 QualType StrideVType = VType;
5180 if (isOpenMPTaskLoopDirective(DKind)) {
5181 VType =
5182 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
5183 StrideVType =
5184 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
5185 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005186
5187 if (!LastIteration.isUsable())
5188 return 0;
5189
5190 // Save the number of iterations.
5191 ExprResult NumIterations = LastIteration;
5192 {
5193 LastIteration = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005194 CurScope, LastIteration.get()->getExprLoc(), BO_Sub,
5195 LastIteration.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00005196 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5197 if (!LastIteration.isUsable())
5198 return 0;
5199 }
5200
5201 // Calculate the last iteration number beforehand instead of doing this on
5202 // each iteration. Do not do this if the number of iterations may be kfold-ed.
5203 llvm::APSInt Result;
5204 bool IsConstant =
5205 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
5206 ExprResult CalcLastIteration;
5207 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005208 ExprResult SaveRef =
5209 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005210 LastIteration = SaveRef;
5211
5212 // Prepare SaveRef + 1.
5213 NumIterations = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005214 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00005215 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5216 if (!NumIterations.isUsable())
5217 return 0;
5218 }
5219
5220 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
5221
David Majnemer9d168222016-08-05 17:44:54 +00005222 // Build variables passed into runtime, necessary for worksharing directives.
Carlo Bertolliffafe102017-04-20 00:39:39 +00005223 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005224 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5225 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005226 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005227 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
5228 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005229 SemaRef.AddInitializerToDecl(LBDecl,
5230 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5231 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005232
5233 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005234 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
5235 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005236 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
Richard Smith3beb7c62017-01-12 02:27:38 +00005237 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005238
5239 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
5240 // This will be used to implement clause 'lastprivate'.
5241 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005242 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
5243 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005244 SemaRef.AddInitializerToDecl(ILDecl,
5245 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5246 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005247
5248 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00005249 VarDecl *STDecl =
5250 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
5251 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005252 SemaRef.AddInitializerToDecl(STDecl,
5253 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
5254 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005255
5256 // Build expression: UB = min(UB, LastIteration)
David Majnemer9d168222016-08-05 17:44:54 +00005257 // It is necessary for CodeGen of directives with static scheduling.
Alexander Musmanc6388682014-12-15 07:07:06 +00005258 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
5259 UB.get(), LastIteration.get());
5260 ExprResult CondOp = SemaRef.ActOnConditionalOp(
Alexey Bataev86ec3fe2018-07-25 14:40:26 +00005261 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(),
5262 LastIteration.get(), UB.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00005263 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
5264 CondOp.get());
5265 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
Carlo Bertolli9925f152016-06-27 14:55:37 +00005266
5267 // If we have a combined directive that combines 'distribute', 'for' or
5268 // 'simd' we need to be able to access the bounds of the schedule of the
5269 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
5270 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
5271 if (isOpenMPLoopBoundSharingDirective(DKind)) {
Carlo Bertolliffafe102017-04-20 00:39:39 +00005272 // Lower bound variable, initialized with zero.
5273 VarDecl *CombLBDecl =
5274 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb");
5275 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc);
5276 SemaRef.AddInitializerToDecl(
5277 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5278 /*DirectInit*/ false);
5279
5280 // Upper bound variable, initialized with last iteration number.
5281 VarDecl *CombUBDecl =
5282 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub");
5283 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc);
5284 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(),
5285 /*DirectInit*/ false);
5286
5287 ExprResult CombIsUBGreater = SemaRef.BuildBinOp(
5288 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get());
5289 ExprResult CombCondOp =
5290 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(),
5291 LastIteration.get(), CombUB.get());
5292 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(),
5293 CombCondOp.get());
5294 CombEUB = SemaRef.ActOnFinishFullExpr(CombEUB.get());
5295
Alexey Bataeve3727102018-04-18 15:57:46 +00005296 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005297 // We expect to have at least 2 more parameters than the 'parallel'
5298 // directive does - the lower and upper bounds of the previous schedule.
5299 assert(CD->getNumParams() >= 4 &&
5300 "Unexpected number of parameters in loop combined directive");
5301
5302 // Set the proper type for the bounds given what we learned from the
5303 // enclosed loops.
Alexey Bataeve3727102018-04-18 15:57:46 +00005304 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
5305 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
Carlo Bertolli9925f152016-06-27 14:55:37 +00005306
5307 // Previous lower and upper bounds are obtained from the region
5308 // parameters.
5309 PrevLB =
5310 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
5311 PrevUB =
5312 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
5313 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005314 }
5315
5316 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005317 ExprResult IV;
Carlo Bertolliffafe102017-04-20 00:39:39 +00005318 ExprResult Init, CombInit;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005319 {
Alexey Bataev7292c292016-04-25 12:22:29 +00005320 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
5321 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
David Majnemer9d168222016-08-05 17:44:54 +00005322 Expr *RHS =
5323 (isOpenMPWorksharingDirective(DKind) ||
5324 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
5325 ? LB.get()
5326 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005327 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
5328 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Carlo Bertolliffafe102017-04-20 00:39:39 +00005329
5330 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5331 Expr *CombRHS =
5332 (isOpenMPWorksharingDirective(DKind) ||
5333 isOpenMPTaskLoopDirective(DKind) ||
5334 isOpenMPDistributeDirective(DKind))
5335 ? CombLB.get()
5336 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
5337 CombInit =
5338 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS);
5339 CombInit = SemaRef.ActOnFinishFullExpr(CombInit.get());
5340 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005341 }
5342
Alexander Musmanc6388682014-12-15 07:07:06 +00005343 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005344 SourceLocation CondLoc = AStmt->getBeginLoc();
Alexander Musmanc6388682014-12-15 07:07:06 +00005345 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005346 (isOpenMPWorksharingDirective(DKind) ||
5347 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00005348 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
5349 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
5350 NumIterations.get());
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005351 ExprResult CombDistCond;
5352 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5353 CombDistCond =
Gheorghe-Teodor Bercea9d6341f2018-10-29 19:44:25 +00005354 SemaRef.BuildBinOp(
5355 CurScope, CondLoc, BO_LT, IV.get(), NumIterations.get());
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005356 }
5357
Carlo Bertolliffafe102017-04-20 00:39:39 +00005358 ExprResult CombCond;
5359 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5360 CombCond =
5361 SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), CombUB.get());
5362 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005363 // Loop increment (IV = IV + 1)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005364 SourceLocation IncLoc = AStmt->getBeginLoc();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005365 ExprResult Inc =
5366 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
5367 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
5368 if (!Inc.isUsable())
5369 return 0;
5370 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00005371 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
5372 if (!Inc.isUsable())
5373 return 0;
5374
5375 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
5376 // Used for directives with static scheduling.
Carlo Bertolliffafe102017-04-20 00:39:39 +00005377 // In combined construct, add combined version that use CombLB and CombUB
5378 // base variables for the update
5379 ExprResult NextLB, NextUB, CombNextLB, CombNextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005380 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5381 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005382 // LB + ST
5383 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
5384 if (!NextLB.isUsable())
5385 return 0;
5386 // LB = LB + ST
5387 NextLB =
5388 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
5389 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
5390 if (!NextLB.isUsable())
5391 return 0;
5392 // UB + ST
5393 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
5394 if (!NextUB.isUsable())
5395 return 0;
5396 // UB = UB + ST
5397 NextUB =
5398 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
5399 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
5400 if (!NextUB.isUsable())
5401 return 0;
Carlo Bertolliffafe102017-04-20 00:39:39 +00005402 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5403 CombNextLB =
5404 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get());
5405 if (!NextLB.isUsable())
5406 return 0;
5407 // LB = LB + ST
5408 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(),
5409 CombNextLB.get());
5410 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get());
5411 if (!CombNextLB.isUsable())
5412 return 0;
5413 // UB + ST
5414 CombNextUB =
5415 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get());
5416 if (!CombNextUB.isUsable())
5417 return 0;
5418 // UB = UB + ST
5419 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(),
5420 CombNextUB.get());
5421 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get());
5422 if (!CombNextUB.isUsable())
5423 return 0;
5424 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005425 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005426
Carlo Bertolliffafe102017-04-20 00:39:39 +00005427 // Create increment expression for distribute loop when combined in a same
Carlo Bertolli8429d812017-02-17 21:29:13 +00005428 // directive with for as IV = IV + ST; ensure upper bound expression based
5429 // on PrevUB instead of NumIterations - used to implement 'for' when found
5430 // in combination with 'distribute', like in 'distribute parallel for'
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005431 SourceLocation DistIncLoc = AStmt->getBeginLoc();
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005432 ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond;
Carlo Bertolli8429d812017-02-17 21:29:13 +00005433 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5434 DistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get());
5435 assert(DistCond.isUsable() && "distribute cond expr was not built");
5436
5437 DistInc =
5438 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get());
5439 assert(DistInc.isUsable() && "distribute inc expr was not built");
5440 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(),
5441 DistInc.get());
5442 DistInc = SemaRef.ActOnFinishFullExpr(DistInc.get());
5443 assert(DistInc.isUsable() && "distribute inc expr was not built");
5444
5445 // Build expression: UB = min(UB, prevUB) for #for in composite or combined
5446 // construct
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005447 SourceLocation DistEUBLoc = AStmt->getBeginLoc();
Carlo Bertolli8429d812017-02-17 21:29:13 +00005448 ExprResult IsUBGreater =
5449 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get());
5450 ExprResult CondOp = SemaRef.ActOnConditionalOp(
5451 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get());
5452 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(),
5453 CondOp.get());
5454 PrevEUB = SemaRef.ActOnFinishFullExpr(PrevEUB.get());
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005455
5456 // Build IV <= PrevUB to be used in parallel for is in combination with
5457 // a distribute directive with schedule(static, 1)
5458 ParForInDistCond =
5459 SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), PrevUB.get());
Carlo Bertolli8429d812017-02-17 21:29:13 +00005460 }
5461
Alexander Musmana5f070a2014-10-01 06:03:56 +00005462 // Build updates and final values of the loop counters.
5463 bool HasErrors = false;
5464 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005465 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005466 Built.Updates.resize(NestedLoopCount);
5467 Built.Finals.resize(NestedLoopCount);
5468 {
5469 ExprResult Div;
5470 // Go from inner nested loop to outer.
5471 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5472 LoopIterationSpace &IS = IterSpaces[Cnt];
5473 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
5474 // Build: Iter = (IV / Div) % IS.NumIters
5475 // where Div is product of previous iterations' IS.NumIters.
5476 ExprResult Iter;
5477 if (Div.isUsable()) {
5478 Iter =
5479 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
5480 } else {
5481 Iter = IV;
5482 assert((Cnt == (int)NestedLoopCount - 1) &&
5483 "unusable div expected on first iteration only");
5484 }
5485
5486 if (Cnt != 0 && Iter.isUsable())
5487 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
5488 IS.NumIterations);
5489 if (!Iter.isUsable()) {
5490 HasErrors = true;
5491 break;
5492 }
5493
Alexey Bataev39f915b82015-05-08 10:41:21 +00005494 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005495 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00005496 DeclRefExpr *CounterVar = buildDeclRefExpr(
5497 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
5498 /*RefersToCapture=*/true);
5499 ExprResult Init = buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005500 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005501 if (!Init.isUsable()) {
5502 HasErrors = true;
5503 break;
5504 }
Alexey Bataeve3727102018-04-18 15:57:46 +00005505 ExprResult Update = buildCounterUpdate(
Alexey Bataev5a3af132016-03-29 08:58:54 +00005506 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
5507 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005508 if (!Update.isUsable()) {
5509 HasErrors = true;
5510 break;
5511 }
5512
5513 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
Alexey Bataeve3727102018-04-18 15:57:46 +00005514 ExprResult Final = buildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00005515 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005516 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005517 if (!Final.isUsable()) {
5518 HasErrors = true;
5519 break;
5520 }
5521
5522 // Build Div for the next iteration: Div <- Div * IS.NumIters
5523 if (Cnt != 0) {
5524 if (Div.isUnset())
5525 Div = IS.NumIterations;
5526 else
5527 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
5528 IS.NumIterations);
5529
5530 // Add parentheses (for debugging purposes only).
5531 if (Div.isUsable())
Alexey Bataev8b427062016-05-25 12:36:08 +00005532 Div = tryBuildCapture(SemaRef, Div.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005533 if (!Div.isUsable()) {
5534 HasErrors = true;
5535 break;
5536 }
5537 }
5538 if (!Update.isUsable() || !Final.isUsable()) {
5539 HasErrors = true;
5540 break;
5541 }
5542 // Save results
5543 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00005544 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005545 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005546 Built.Updates[Cnt] = Update.get();
5547 Built.Finals[Cnt] = Final.get();
5548 }
5549 }
5550
5551 if (HasErrors)
5552 return 0;
5553
5554 // Save results
5555 Built.IterationVarRef = IV.get();
5556 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00005557 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00005558 Built.CalcLastIteration =
5559 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005560 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00005561 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005562 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005563 Built.Init = Init.get();
5564 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005565 Built.LB = LB.get();
5566 Built.UB = UB.get();
5567 Built.IL = IL.get();
5568 Built.ST = ST.get();
5569 Built.EUB = EUB.get();
5570 Built.NLB = NextLB.get();
5571 Built.NUB = NextUB.get();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005572 Built.PrevLB = PrevLB.get();
5573 Built.PrevUB = PrevUB.get();
Carlo Bertolli8429d812017-02-17 21:29:13 +00005574 Built.DistInc = DistInc.get();
5575 Built.PrevEUB = PrevEUB.get();
Carlo Bertolliffafe102017-04-20 00:39:39 +00005576 Built.DistCombinedFields.LB = CombLB.get();
5577 Built.DistCombinedFields.UB = CombUB.get();
5578 Built.DistCombinedFields.EUB = CombEUB.get();
5579 Built.DistCombinedFields.Init = CombInit.get();
5580 Built.DistCombinedFields.Cond = CombCond.get();
5581 Built.DistCombinedFields.NLB = CombNextLB.get();
5582 Built.DistCombinedFields.NUB = CombNextUB.get();
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005583 Built.DistCombinedFields.DistCond = CombDistCond.get();
5584 Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005585
Alexey Bataevabfc0692014-06-25 06:52:00 +00005586 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005587}
5588
Alexey Bataev10e775f2015-07-30 11:36:16 +00005589static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005590 auto CollapseClauses =
5591 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
5592 if (CollapseClauses.begin() != CollapseClauses.end())
5593 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005594 return nullptr;
5595}
5596
Alexey Bataev10e775f2015-07-30 11:36:16 +00005597static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005598 auto OrderedClauses =
5599 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
5600 if (OrderedClauses.begin() != OrderedClauses.end())
5601 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005602 return nullptr;
5603}
5604
Kelvin Lic5609492016-07-15 04:39:07 +00005605static bool checkSimdlenSafelenSpecified(Sema &S,
5606 const ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005607 const OMPSafelenClause *Safelen = nullptr;
5608 const OMPSimdlenClause *Simdlen = nullptr;
Kelvin Lic5609492016-07-15 04:39:07 +00005609
Alexey Bataeve3727102018-04-18 15:57:46 +00005610 for (const OMPClause *Clause : Clauses) {
Kelvin Lic5609492016-07-15 04:39:07 +00005611 if (Clause->getClauseKind() == OMPC_safelen)
5612 Safelen = cast<OMPSafelenClause>(Clause);
5613 else if (Clause->getClauseKind() == OMPC_simdlen)
5614 Simdlen = cast<OMPSimdlenClause>(Clause);
5615 if (Safelen && Simdlen)
5616 break;
5617 }
5618
5619 if (Simdlen && Safelen) {
5620 llvm::APSInt SimdlenRes, SafelenRes;
Alexey Bataeve3727102018-04-18 15:57:46 +00005621 const Expr *SimdlenLength = Simdlen->getSimdlen();
5622 const Expr *SafelenLength = Safelen->getSafelen();
Kelvin Lic5609492016-07-15 04:39:07 +00005623 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
5624 SimdlenLength->isInstantiationDependent() ||
5625 SimdlenLength->containsUnexpandedParameterPack())
5626 return false;
5627 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
5628 SafelenLength->isInstantiationDependent() ||
5629 SafelenLength->containsUnexpandedParameterPack())
5630 return false;
5631 SimdlenLength->EvaluateAsInt(SimdlenRes, S.Context);
5632 SafelenLength->EvaluateAsInt(SafelenRes, S.Context);
5633 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
5634 // If both simdlen and safelen clauses are specified, the value of the
5635 // simdlen parameter must be less than or equal to the value of the safelen
5636 // parameter.
5637 if (SimdlenRes > SafelenRes) {
5638 S.Diag(SimdlenLength->getExprLoc(),
5639 diag::err_omp_wrong_simdlen_safelen_values)
5640 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
5641 return true;
5642 }
Alexey Bataev66b15b52015-08-21 11:14:16 +00005643 }
5644 return false;
5645}
5646
Alexey Bataeve3727102018-04-18 15:57:46 +00005647StmtResult
5648Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
5649 SourceLocation StartLoc, SourceLocation EndLoc,
5650 VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005651 if (!AStmt)
5652 return StmtError();
5653
5654 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005655 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005656 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5657 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00005658 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataev10e775f2015-07-30 11:36:16 +00005659 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5660 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005661 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005662 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005663
Alexander Musmana5f070a2014-10-01 06:03:56 +00005664 assert((CurContext->isDependentContext() || B.builtAll()) &&
5665 "omp simd loop exprs were not built");
5666
Alexander Musman3276a272015-03-21 10:12:56 +00005667 if (!CurContext->isDependentContext()) {
5668 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005669 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005670 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexander Musman3276a272015-03-21 10:12:56 +00005671 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005672 B.NumIterations, *this, CurScope,
5673 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00005674 return StmtError();
5675 }
5676 }
5677
Kelvin Lic5609492016-07-15 04:39:07 +00005678 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005679 return StmtError();
5680
Reid Kleckner87a31802018-03-12 21:43:02 +00005681 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005682 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5683 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005684}
5685
Alexey Bataeve3727102018-04-18 15:57:46 +00005686StmtResult
5687Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
5688 SourceLocation StartLoc, SourceLocation EndLoc,
5689 VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005690 if (!AStmt)
5691 return StmtError();
5692
5693 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005694 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005695 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5696 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00005697 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataev10e775f2015-07-30 11:36:16 +00005698 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5699 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005700 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005701 return StmtError();
5702
Alexander Musmana5f070a2014-10-01 06:03:56 +00005703 assert((CurContext->isDependentContext() || B.builtAll()) &&
5704 "omp for loop exprs were not built");
5705
Alexey Bataev54acd402015-08-04 11:18:19 +00005706 if (!CurContext->isDependentContext()) {
5707 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005708 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005709 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00005710 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005711 B.NumIterations, *this, CurScope,
5712 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005713 return StmtError();
5714 }
5715 }
5716
Reid Kleckner87a31802018-03-12 21:43:02 +00005717 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005718 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005719 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005720}
5721
Alexander Musmanf82886e2014-09-18 05:12:34 +00005722StmtResult Sema::ActOnOpenMPForSimdDirective(
5723 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00005724 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005725 if (!AStmt)
5726 return StmtError();
5727
5728 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005729 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005730 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5731 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00005732 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00005733 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00005734 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5735 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005736 if (NestedLoopCount == 0)
5737 return StmtError();
5738
Alexander Musmanc6388682014-12-15 07:07:06 +00005739 assert((CurContext->isDependentContext() || B.builtAll()) &&
5740 "omp for simd loop exprs were not built");
5741
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005742 if (!CurContext->isDependentContext()) {
5743 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005744 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005745 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005746 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005747 B.NumIterations, *this, CurScope,
5748 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005749 return StmtError();
5750 }
5751 }
5752
Kelvin Lic5609492016-07-15 04:39:07 +00005753 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005754 return StmtError();
5755
Reid Kleckner87a31802018-03-12 21:43:02 +00005756 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005757 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5758 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005759}
5760
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005761StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5762 Stmt *AStmt,
5763 SourceLocation StartLoc,
5764 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005765 if (!AStmt)
5766 return StmtError();
5767
5768 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005769 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00005770 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005771 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00005772 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005773 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005774 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005775 return StmtError();
5776 // All associated statements must be '#pragma omp section' except for
5777 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005778 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005779 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5780 if (SectionStmt)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005781 Diag(SectionStmt->getBeginLoc(),
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005782 diag::err_omp_sections_substmt_not_section);
5783 return StmtError();
5784 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005785 cast<OMPSectionDirective>(SectionStmt)
5786 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005787 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005788 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005789 Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005790 return StmtError();
5791 }
5792
Reid Kleckner87a31802018-03-12 21:43:02 +00005793 setFunctionHasBranchProtectedScope();
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005794
Alexey Bataev25e5b442015-09-15 12:52:43 +00005795 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5796 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005797}
5798
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005799StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5800 SourceLocation StartLoc,
5801 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005802 if (!AStmt)
5803 return StmtError();
5804
5805 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005806
Reid Kleckner87a31802018-03-12 21:43:02 +00005807 setFunctionHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00005808 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005809
Alexey Bataev25e5b442015-09-15 12:52:43 +00005810 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5811 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005812}
5813
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005814StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5815 Stmt *AStmt,
5816 SourceLocation StartLoc,
5817 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005818 if (!AStmt)
5819 return StmtError();
5820
5821 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00005822
Reid Kleckner87a31802018-03-12 21:43:02 +00005823 setFunctionHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00005824
Alexey Bataev3255bf32015-01-19 05:20:46 +00005825 // OpenMP [2.7.3, single Construct, Restrictions]
5826 // The copyprivate clause must not be used with the nowait clause.
Alexey Bataeve3727102018-04-18 15:57:46 +00005827 const OMPClause *Nowait = nullptr;
5828 const OMPClause *Copyprivate = nullptr;
5829 for (const OMPClause *Clause : Clauses) {
Alexey Bataev3255bf32015-01-19 05:20:46 +00005830 if (Clause->getClauseKind() == OMPC_nowait)
5831 Nowait = Clause;
5832 else if (Clause->getClauseKind() == OMPC_copyprivate)
5833 Copyprivate = Clause;
5834 if (Copyprivate && Nowait) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005835 Diag(Copyprivate->getBeginLoc(),
Alexey Bataev3255bf32015-01-19 05:20:46 +00005836 diag::err_omp_single_copyprivate_with_nowait);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005837 Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here);
Alexey Bataev3255bf32015-01-19 05:20:46 +00005838 return StmtError();
5839 }
5840 }
5841
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005842 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5843}
5844
Alexander Musman80c22892014-07-17 08:54:58 +00005845StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5846 SourceLocation StartLoc,
5847 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005848 if (!AStmt)
5849 return StmtError();
5850
5851 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00005852
Reid Kleckner87a31802018-03-12 21:43:02 +00005853 setFunctionHasBranchProtectedScope();
Alexander Musman80c22892014-07-17 08:54:58 +00005854
5855 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5856}
5857
Alexey Bataev28c75412015-12-15 08:19:24 +00005858StmtResult Sema::ActOnOpenMPCriticalDirective(
5859 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5860 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005861 if (!AStmt)
5862 return StmtError();
5863
5864 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005865
Alexey Bataev28c75412015-12-15 08:19:24 +00005866 bool ErrorFound = false;
5867 llvm::APSInt Hint;
5868 SourceLocation HintLoc;
5869 bool DependentHint = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00005870 for (const OMPClause *C : Clauses) {
Alexey Bataev28c75412015-12-15 08:19:24 +00005871 if (C->getClauseKind() == OMPC_hint) {
5872 if (!DirName.getName()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005873 Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name);
Alexey Bataev28c75412015-12-15 08:19:24 +00005874 ErrorFound = true;
5875 }
5876 Expr *E = cast<OMPHintClause>(C)->getHint();
5877 if (E->isTypeDependent() || E->isValueDependent() ||
Alexey Bataeve3727102018-04-18 15:57:46 +00005878 E->isInstantiationDependent()) {
Alexey Bataev28c75412015-12-15 08:19:24 +00005879 DependentHint = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00005880 } else {
Alexey Bataev28c75412015-12-15 08:19:24 +00005881 Hint = E->EvaluateKnownConstInt(Context);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005882 HintLoc = C->getBeginLoc();
Alexey Bataev28c75412015-12-15 08:19:24 +00005883 }
5884 }
5885 }
5886 if (ErrorFound)
5887 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00005888 const auto Pair = DSAStack->getCriticalWithHint(DirName);
Alexey Bataev28c75412015-12-15 08:19:24 +00005889 if (Pair.first && DirName.getName() && !DependentHint) {
5890 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5891 Diag(StartLoc, diag::err_omp_critical_with_hint);
Alexey Bataeve3727102018-04-18 15:57:46 +00005892 if (HintLoc.isValid())
Alexey Bataev28c75412015-12-15 08:19:24 +00005893 Diag(HintLoc, diag::note_omp_critical_hint_here)
5894 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +00005895 else
Alexey Bataev28c75412015-12-15 08:19:24 +00005896 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
Alexey Bataeve3727102018-04-18 15:57:46 +00005897 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005898 Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here)
Alexey Bataev28c75412015-12-15 08:19:24 +00005899 << 1
5900 << C->getHint()->EvaluateKnownConstInt(Context).toString(
5901 /*Radix=*/10, /*Signed=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +00005902 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005903 Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1;
Alexey Bataeve3727102018-04-18 15:57:46 +00005904 }
Alexey Bataev28c75412015-12-15 08:19:24 +00005905 }
5906 }
5907
Reid Kleckner87a31802018-03-12 21:43:02 +00005908 setFunctionHasBranchProtectedScope();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005909
Alexey Bataev28c75412015-12-15 08:19:24 +00005910 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5911 Clauses, AStmt);
5912 if (!Pair.first && DirName.getName() && !DependentHint)
5913 DSAStack->addCriticalWithHint(Dir, Hint);
5914 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005915}
5916
Alexey Bataev4acb8592014-07-07 13:01:15 +00005917StmtResult Sema::ActOnOpenMPParallelForDirective(
5918 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00005919 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005920 if (!AStmt)
5921 return StmtError();
5922
Alexey Bataeve3727102018-04-18 15:57:46 +00005923 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005924 // 1.2.2 OpenMP Language Terminology
5925 // Structured block - An executable statement with a single entry at the
5926 // top and a single exit at the bottom.
5927 // The point of exit cannot be a branch out of the structured block.
5928 // longjmp() and throw() must not violate the entry/exit criteria.
5929 CS->getCapturedDecl()->setNothrow();
5930
Alexander Musmanc6388682014-12-15 07:07:06 +00005931 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005932 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5933 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005934 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00005935 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00005936 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5937 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005938 if (NestedLoopCount == 0)
5939 return StmtError();
5940
Alexander Musmana5f070a2014-10-01 06:03:56 +00005941 assert((CurContext->isDependentContext() || B.builtAll()) &&
5942 "omp parallel for loop exprs were not built");
5943
Alexey Bataev54acd402015-08-04 11:18:19 +00005944 if (!CurContext->isDependentContext()) {
5945 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005946 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005947 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00005948 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005949 B.NumIterations, *this, CurScope,
5950 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005951 return StmtError();
5952 }
5953 }
5954
Reid Kleckner87a31802018-03-12 21:43:02 +00005955 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005956 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005957 NestedLoopCount, Clauses, AStmt, B,
5958 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00005959}
5960
Alexander Musmane4e893b2014-09-23 09:33:00 +00005961StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5962 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00005963 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005964 if (!AStmt)
5965 return StmtError();
5966
Alexey Bataeve3727102018-04-18 15:57:46 +00005967 auto *CS = cast<CapturedStmt>(AStmt);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005968 // 1.2.2 OpenMP Language Terminology
5969 // Structured block - An executable statement with a single entry at the
5970 // top and a single exit at the bottom.
5971 // The point of exit cannot be a branch out of the structured block.
5972 // longjmp() and throw() must not violate the entry/exit criteria.
5973 CS->getCapturedDecl()->setNothrow();
5974
Alexander Musmanc6388682014-12-15 07:07:06 +00005975 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005976 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5977 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00005978 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00005979 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00005980 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5981 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005982 if (NestedLoopCount == 0)
5983 return StmtError();
5984
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005985 if (!CurContext->isDependentContext()) {
5986 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005987 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005988 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005989 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005990 B.NumIterations, *this, CurScope,
5991 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005992 return StmtError();
5993 }
5994 }
5995
Kelvin Lic5609492016-07-15 04:39:07 +00005996 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005997 return StmtError();
5998
Reid Kleckner87a31802018-03-12 21:43:02 +00005999 setFunctionHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00006000 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00006001 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00006002}
6003
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006004StmtResult
6005Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
6006 Stmt *AStmt, SourceLocation StartLoc,
6007 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006008 if (!AStmt)
6009 return StmtError();
6010
6011 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006012 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00006013 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006014 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00006015 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006016 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00006017 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006018 return StmtError();
6019 // All associated statements must be '#pragma omp section' except for
6020 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00006021 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006022 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
6023 if (SectionStmt)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006024 Diag(SectionStmt->getBeginLoc(),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006025 diag::err_omp_parallel_sections_substmt_not_section);
6026 return StmtError();
6027 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006028 cast<OMPSectionDirective>(SectionStmt)
6029 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006030 }
6031 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006032 Diag(AStmt->getBeginLoc(),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006033 diag::err_omp_parallel_sections_not_compound_stmt);
6034 return StmtError();
6035 }
6036
Reid Kleckner87a31802018-03-12 21:43:02 +00006037 setFunctionHasBranchProtectedScope();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006038
Alexey Bataev25e5b442015-09-15 12:52:43 +00006039 return OMPParallelSectionsDirective::Create(
6040 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006041}
6042
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006043StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
6044 Stmt *AStmt, SourceLocation StartLoc,
6045 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006046 if (!AStmt)
6047 return StmtError();
6048
David Majnemer9d168222016-08-05 17:44:54 +00006049 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006050 // 1.2.2 OpenMP Language Terminology
6051 // Structured block - An executable statement with a single entry at the
6052 // top and a single exit at the bottom.
6053 // The point of exit cannot be a branch out of the structured block.
6054 // longjmp() and throw() must not violate the entry/exit criteria.
6055 CS->getCapturedDecl()->setNothrow();
6056
Reid Kleckner87a31802018-03-12 21:43:02 +00006057 setFunctionHasBranchProtectedScope();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006058
Alexey Bataev25e5b442015-09-15 12:52:43 +00006059 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
6060 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006061}
6062
Alexey Bataev68446b72014-07-18 07:47:19 +00006063StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
6064 SourceLocation EndLoc) {
6065 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
6066}
6067
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00006068StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
6069 SourceLocation EndLoc) {
6070 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
6071}
6072
Alexey Bataev2df347a2014-07-18 10:17:07 +00006073StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
6074 SourceLocation EndLoc) {
6075 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
6076}
6077
Alexey Bataev169d96a2017-07-18 20:17:46 +00006078StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses,
6079 Stmt *AStmt,
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006080 SourceLocation StartLoc,
6081 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006082 if (!AStmt)
6083 return StmtError();
6084
6085 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006086
Reid Kleckner87a31802018-03-12 21:43:02 +00006087 setFunctionHasBranchProtectedScope();
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006088
Alexey Bataev169d96a2017-07-18 20:17:46 +00006089 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses,
Alexey Bataev3b1b8952017-07-25 15:53:26 +00006090 AStmt,
6091 DSAStack->getTaskgroupReductionRef());
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006092}
6093
Alexey Bataev6125da92014-07-21 11:26:11 +00006094StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
6095 SourceLocation StartLoc,
6096 SourceLocation EndLoc) {
6097 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
6098 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
6099}
6100
Alexey Bataev346265e2015-09-25 10:37:12 +00006101StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
6102 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006103 SourceLocation StartLoc,
6104 SourceLocation EndLoc) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006105 const OMPClause *DependFound = nullptr;
6106 const OMPClause *DependSourceClause = nullptr;
6107 const OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00006108 bool ErrorFound = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00006109 const OMPThreadsClause *TC = nullptr;
6110 const OMPSIMDClause *SC = nullptr;
6111 for (const OMPClause *C : Clauses) {
Alexey Bataeveb482352015-12-18 05:05:56 +00006112 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
6113 DependFound = C;
6114 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
6115 if (DependSourceClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006116 Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
Alexey Bataeveb482352015-12-18 05:05:56 +00006117 << getOpenMPDirectiveName(OMPD_ordered)
6118 << getOpenMPClauseName(OMPC_depend) << 2;
6119 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006120 } else {
Alexey Bataeveb482352015-12-18 05:05:56 +00006121 DependSourceClause = C;
Alexey Bataeve3727102018-04-18 15:57:46 +00006122 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006123 if (DependSinkClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006124 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006125 << 0;
6126 ErrorFound = true;
6127 }
6128 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
6129 if (DependSourceClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006130 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006131 << 1;
6132 ErrorFound = true;
6133 }
6134 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00006135 }
Alexey Bataeve3727102018-04-18 15:57:46 +00006136 } else if (C->getClauseKind() == OMPC_threads) {
Alexey Bataev346265e2015-09-25 10:37:12 +00006137 TC = cast<OMPThreadsClause>(C);
Alexey Bataeve3727102018-04-18 15:57:46 +00006138 } else if (C->getClauseKind() == OMPC_simd) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006139 SC = cast<OMPSIMDClause>(C);
Alexey Bataeve3727102018-04-18 15:57:46 +00006140 }
Alexey Bataev346265e2015-09-25 10:37:12 +00006141 }
Alexey Bataeveb482352015-12-18 05:05:56 +00006142 if (!ErrorFound && !SC &&
6143 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006144 // OpenMP [2.8.1,simd Construct, Restrictions]
6145 // An ordered construct with the simd clause is the only OpenMP construct
6146 // that can appear in the simd region.
6147 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00006148 ErrorFound = true;
6149 } else if (DependFound && (TC || SC)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006150 Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd)
Alexey Bataeveb482352015-12-18 05:05:56 +00006151 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
6152 ErrorFound = true;
Alexey Bataevf138fda2018-08-13 19:04:24 +00006153 } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006154 Diag(DependFound->getBeginLoc(),
Alexey Bataeveb482352015-12-18 05:05:56 +00006155 diag::err_omp_ordered_directive_without_param);
6156 ErrorFound = true;
6157 } else if (TC || Clauses.empty()) {
Alexey Bataevf138fda2018-08-13 19:04:24 +00006158 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006159 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc;
Alexey Bataeveb482352015-12-18 05:05:56 +00006160 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
6161 << (TC != nullptr);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006162 Diag(Param->getBeginLoc(), diag::note_omp_ordered_param);
Alexey Bataeveb482352015-12-18 05:05:56 +00006163 ErrorFound = true;
6164 }
6165 }
6166 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006167 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00006168
6169 if (AStmt) {
6170 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6171
Reid Kleckner87a31802018-03-12 21:43:02 +00006172 setFunctionHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006173 }
Alexey Bataev346265e2015-09-25 10:37:12 +00006174
6175 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006176}
6177
Alexey Bataev1d160b12015-03-13 12:27:31 +00006178namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006179/// Helper class for checking expression in 'omp atomic [update]'
Alexey Bataev1d160b12015-03-13 12:27:31 +00006180/// construct.
6181class OpenMPAtomicUpdateChecker {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006182 /// Error results for atomic update expressions.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006183 enum ExprAnalysisErrorCode {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006184 /// A statement is not an expression statement.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006185 NotAnExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006186 /// Expression is not builtin binary or unary operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006187 NotABinaryOrUnaryExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006188 /// Unary operation is not post-/pre- increment/decrement operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006189 NotAnUnaryIncDecExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006190 /// An expression is not of scalar type.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006191 NotAScalarType,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006192 /// A binary operation is not an assignment operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006193 NotAnAssignmentOp,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006194 /// RHS part of the binary operation is not a binary expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006195 NotABinaryExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006196 /// RHS part is not additive/multiplicative/shift/biwise binary
Alexey Bataev1d160b12015-03-13 12:27:31 +00006197 /// expression.
6198 NotABinaryOperator,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006199 /// RHS binary operation does not have reference to the updated LHS
Alexey Bataev1d160b12015-03-13 12:27:31 +00006200 /// part.
6201 NotAnUpdateExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006202 /// No errors is found.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006203 NoError
6204 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006205 /// Reference to Sema.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006206 Sema &SemaRef;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006207 /// A location for note diagnostics (when error is found).
Alexey Bataev1d160b12015-03-13 12:27:31 +00006208 SourceLocation NoteLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006209 /// 'x' lvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006210 Expr *X;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006211 /// 'expr' rvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006212 Expr *E;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006213 /// Helper expression of the form
Alexey Bataevb4505a72015-03-30 05:20:59 +00006214 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6215 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6216 Expr *UpdateExpr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006217 /// Is 'x' a LHS in a RHS part of full update expression. It is
Alexey Bataevb4505a72015-03-30 05:20:59 +00006218 /// important for non-associative operations.
6219 bool IsXLHSInRHSPart;
6220 BinaryOperatorKind Op;
6221 SourceLocation OpLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006222 /// true if the source expression is a postfix unary operation, false
Alexey Bataevb78ca832015-04-01 03:33:17 +00006223 /// if it is a prefix unary operation.
6224 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006225
6226public:
6227 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00006228 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00006229 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006230 /// Check specified statement that it is suitable for 'atomic update'
Alexey Bataev1d160b12015-03-13 12:27:31 +00006231 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00006232 /// expression. If DiagId and NoteId == 0, then only check is performed
6233 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006234 /// \param DiagId Diagnostic which should be emitted if error is found.
6235 /// \param NoteId Diagnostic note for the main error message.
6236 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00006237 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006238 /// Return the 'x' lvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006239 Expr *getX() const { return X; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006240 /// Return the 'expr' rvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006241 Expr *getExpr() const { return E; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006242 /// Return the update expression used in calculation of the updated
Alexey Bataevb4505a72015-03-30 05:20:59 +00006243 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6244 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6245 Expr *getUpdateExpr() const { return UpdateExpr; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006246 /// Return true if 'x' is LHS in RHS part of full update expression,
Alexey Bataevb4505a72015-03-30 05:20:59 +00006247 /// false otherwise.
6248 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
6249
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006250 /// true if the source expression is a postfix unary operation, false
Alexey Bataevb78ca832015-04-01 03:33:17 +00006251 /// if it is a prefix unary operation.
6252 bool isPostfixUpdate() const { return IsPostfixUpdate; }
6253
Alexey Bataev1d160b12015-03-13 12:27:31 +00006254private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00006255 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
6256 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00006257};
6258} // namespace
6259
6260bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
6261 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
6262 ExprAnalysisErrorCode ErrorFound = NoError;
6263 SourceLocation ErrorLoc, NoteLoc;
6264 SourceRange ErrorRange, NoteRange;
6265 // Allowed constructs are:
6266 // x = x binop expr;
6267 // x = expr binop x;
6268 if (AtomicBinOp->getOpcode() == BO_Assign) {
6269 X = AtomicBinOp->getLHS();
Alexey Bataeve3727102018-04-18 15:57:46 +00006270 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006271 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
6272 if (AtomicInnerBinOp->isMultiplicativeOp() ||
6273 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
6274 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006275 Op = AtomicInnerBinOp->getOpcode();
6276 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataeve3727102018-04-18 15:57:46 +00006277 Expr *LHS = AtomicInnerBinOp->getLHS();
6278 Expr *RHS = AtomicInnerBinOp->getRHS();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006279 llvm::FoldingSetNodeID XId, LHSId, RHSId;
6280 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
6281 /*Canonical=*/true);
6282 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
6283 /*Canonical=*/true);
6284 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
6285 /*Canonical=*/true);
6286 if (XId == LHSId) {
6287 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006288 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006289 } else if (XId == RHSId) {
6290 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006291 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006292 } else {
6293 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6294 ErrorRange = AtomicInnerBinOp->getSourceRange();
6295 NoteLoc = X->getExprLoc();
6296 NoteRange = X->getSourceRange();
6297 ErrorFound = NotAnUpdateExpression;
6298 }
6299 } else {
6300 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6301 ErrorRange = AtomicInnerBinOp->getSourceRange();
6302 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
6303 NoteRange = SourceRange(NoteLoc, NoteLoc);
6304 ErrorFound = NotABinaryOperator;
6305 }
6306 } else {
6307 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
6308 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
6309 ErrorFound = NotABinaryExpression;
6310 }
6311 } else {
6312 ErrorLoc = AtomicBinOp->getExprLoc();
6313 ErrorRange = AtomicBinOp->getSourceRange();
6314 NoteLoc = AtomicBinOp->getOperatorLoc();
6315 NoteRange = SourceRange(NoteLoc, NoteLoc);
6316 ErrorFound = NotAnAssignmentOp;
6317 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006318 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006319 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6320 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6321 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006322 }
6323 if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006324 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006325 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006326}
6327
6328bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
6329 unsigned NoteId) {
6330 ExprAnalysisErrorCode ErrorFound = NoError;
6331 SourceLocation ErrorLoc, NoteLoc;
6332 SourceRange ErrorRange, NoteRange;
6333 // Allowed constructs are:
6334 // x++;
6335 // x--;
6336 // ++x;
6337 // --x;
6338 // x binop= expr;
6339 // x = x binop expr;
6340 // x = expr binop x;
6341 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
6342 AtomicBody = AtomicBody->IgnoreParenImpCasts();
6343 if (AtomicBody->getType()->isScalarType() ||
6344 AtomicBody->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006345 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006346 AtomicBody->IgnoreParenImpCasts())) {
6347 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00006348 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006349 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00006350 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006351 E = AtomicCompAssignOp->getRHS();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006352 X = AtomicCompAssignOp->getLHS()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006353 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006354 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
6355 AtomicBody->IgnoreParenImpCasts())) {
6356 // Check for Binary Operation
David Majnemer9d168222016-08-05 17:44:54 +00006357 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
Alexey Bataevb4505a72015-03-30 05:20:59 +00006358 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006359 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
David Majnemer9d168222016-08-05 17:44:54 +00006360 AtomicBody->IgnoreParenImpCasts())) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006361 // Check for Unary Operation
6362 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006363 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006364 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
6365 OpLoc = AtomicUnaryOp->getOperatorLoc();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006366 X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006367 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
6368 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006369 } else {
6370 ErrorFound = NotAnUnaryIncDecExpression;
6371 ErrorLoc = AtomicUnaryOp->getExprLoc();
6372 ErrorRange = AtomicUnaryOp->getSourceRange();
6373 NoteLoc = AtomicUnaryOp->getOperatorLoc();
6374 NoteRange = SourceRange(NoteLoc, NoteLoc);
6375 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006376 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006377 ErrorFound = NotABinaryOrUnaryExpression;
6378 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
6379 NoteRange = ErrorRange = AtomicBody->getSourceRange();
6380 }
6381 } else {
6382 ErrorFound = NotAScalarType;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006383 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006384 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6385 }
6386 } else {
6387 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006388 NoteLoc = ErrorLoc = S->getBeginLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006389 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6390 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006391 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006392 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6393 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6394 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006395 }
6396 if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006397 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006398 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006399 // Build an update expression of form 'OpaqueValueExpr(x) binop
6400 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
6401 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
6402 auto *OVEX = new (SemaRef.getASTContext())
6403 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
6404 auto *OVEExpr = new (SemaRef.getASTContext())
6405 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
Alexey Bataeve3727102018-04-18 15:57:46 +00006406 ExprResult Update =
Alexey Bataevb4505a72015-03-30 05:20:59 +00006407 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
6408 IsXLHSInRHSPart ? OVEExpr : OVEX);
6409 if (Update.isInvalid())
6410 return true;
6411 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
6412 Sema::AA_Casting);
6413 if (Update.isInvalid())
6414 return true;
6415 UpdateExpr = Update.get();
6416 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00006417 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006418}
6419
Alexey Bataev0162e452014-07-22 10:10:35 +00006420StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
6421 Stmt *AStmt,
6422 SourceLocation StartLoc,
6423 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006424 if (!AStmt)
6425 return StmtError();
6426
David Majnemer9d168222016-08-05 17:44:54 +00006427 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00006428 // 1.2.2 OpenMP Language Terminology
6429 // Structured block - An executable statement with a single entry at the
6430 // top and a single exit at the bottom.
6431 // The point of exit cannot be a branch out of the structured block.
6432 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00006433 OpenMPClauseKind AtomicKind = OMPC_unknown;
6434 SourceLocation AtomicKindLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00006435 for (const OMPClause *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00006436 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00006437 C->getClauseKind() == OMPC_update ||
6438 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00006439 if (AtomicKind != OMPC_unknown) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006440 Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00006441 << SourceRange(C->getBeginLoc(), C->getEndLoc());
Alexey Bataevdea47612014-07-23 07:46:59 +00006442 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
6443 << getOpenMPClauseName(AtomicKind);
6444 } else {
6445 AtomicKind = C->getClauseKind();
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006446 AtomicKindLoc = C->getBeginLoc();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006447 }
6448 }
6449 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006450
Alexey Bataeve3727102018-04-18 15:57:46 +00006451 Stmt *Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00006452 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
6453 Body = EWC->getSubExpr();
6454
Alexey Bataev62cec442014-11-18 10:14:22 +00006455 Expr *X = nullptr;
6456 Expr *V = nullptr;
6457 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006458 Expr *UE = nullptr;
6459 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006460 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00006461 // OpenMP [2.12.6, atomic Construct]
6462 // In the next expressions:
6463 // * x and v (as applicable) are both l-value expressions with scalar type.
6464 // * During the execution of an atomic region, multiple syntactic
6465 // occurrences of x must designate the same storage location.
6466 // * Neither of v and expr (as applicable) may access the storage location
6467 // designated by x.
6468 // * Neither of x and expr (as applicable) may access the storage location
6469 // designated by v.
6470 // * expr is an expression with scalar type.
6471 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
6472 // * binop, binop=, ++, and -- are not overloaded operators.
6473 // * The expression x binop expr must be numerically equivalent to x binop
6474 // (expr). This requirement is satisfied if the operators in expr have
6475 // precedence greater than binop, or by using parentheses around expr or
6476 // subexpressions of expr.
6477 // * The expression expr binop x must be numerically equivalent to (expr)
6478 // binop x. This requirement is satisfied if the operators in expr have
6479 // precedence equal to or greater than binop, or by using parentheses around
6480 // expr or subexpressions of expr.
6481 // * For forms that allow multiple occurrences of x, the number of times
6482 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00006483 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006484 enum {
6485 NotAnExpression,
6486 NotAnAssignmentOp,
6487 NotAScalarType,
6488 NotAnLValue,
6489 NoError
6490 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00006491 SourceLocation ErrorLoc, NoteLoc;
6492 SourceRange ErrorRange, NoteRange;
6493 // If clause is read:
6494 // v = x;
Alexey Bataeve3727102018-04-18 15:57:46 +00006495 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
6496 const auto *AtomicBinOp =
Alexey Bataev62cec442014-11-18 10:14:22 +00006497 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6498 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6499 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6500 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
6501 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6502 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
6503 if (!X->isLValue() || !V->isLValue()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006504 const Expr *NotLValueExpr = X->isLValue() ? V : X;
Alexey Bataev62cec442014-11-18 10:14:22 +00006505 ErrorFound = NotAnLValue;
6506 ErrorLoc = AtomicBinOp->getExprLoc();
6507 ErrorRange = AtomicBinOp->getSourceRange();
6508 NoteLoc = NotLValueExpr->getExprLoc();
6509 NoteRange = NotLValueExpr->getSourceRange();
6510 }
6511 } else if (!X->isInstantiationDependent() ||
6512 !V->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006513 const Expr *NotScalarExpr =
Alexey Bataev62cec442014-11-18 10:14:22 +00006514 (X->isInstantiationDependent() || X->getType()->isScalarType())
6515 ? V
6516 : X;
6517 ErrorFound = NotAScalarType;
6518 ErrorLoc = AtomicBinOp->getExprLoc();
6519 ErrorRange = AtomicBinOp->getSourceRange();
6520 NoteLoc = NotScalarExpr->getExprLoc();
6521 NoteRange = NotScalarExpr->getSourceRange();
6522 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006523 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00006524 ErrorFound = NotAnAssignmentOp;
6525 ErrorLoc = AtomicBody->getExprLoc();
6526 ErrorRange = AtomicBody->getSourceRange();
6527 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6528 : AtomicBody->getExprLoc();
6529 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6530 : AtomicBody->getSourceRange();
6531 }
6532 } else {
6533 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006534 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataev62cec442014-11-18 10:14:22 +00006535 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006536 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006537 if (ErrorFound != NoError) {
6538 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
6539 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006540 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6541 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00006542 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00006543 }
6544 if (CurContext->isDependentContext())
Alexey Bataev62cec442014-11-18 10:14:22 +00006545 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00006546 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006547 enum {
6548 NotAnExpression,
6549 NotAnAssignmentOp,
6550 NotAScalarType,
6551 NotAnLValue,
6552 NoError
6553 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006554 SourceLocation ErrorLoc, NoteLoc;
6555 SourceRange ErrorRange, NoteRange;
6556 // If clause is write:
6557 // x = expr;
Alexey Bataeve3727102018-04-18 15:57:46 +00006558 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
6559 const auto *AtomicBinOp =
Alexey Bataevf33eba62014-11-28 07:21:40 +00006560 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6561 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00006562 X = AtomicBinOp->getLHS();
6563 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006564 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6565 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
6566 if (!X->isLValue()) {
6567 ErrorFound = NotAnLValue;
6568 ErrorLoc = AtomicBinOp->getExprLoc();
6569 ErrorRange = AtomicBinOp->getSourceRange();
6570 NoteLoc = X->getExprLoc();
6571 NoteRange = X->getSourceRange();
6572 }
6573 } else if (!X->isInstantiationDependent() ||
6574 !E->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006575 const Expr *NotScalarExpr =
Alexey Bataevf33eba62014-11-28 07:21:40 +00006576 (X->isInstantiationDependent() || X->getType()->isScalarType())
6577 ? E
6578 : X;
6579 ErrorFound = NotAScalarType;
6580 ErrorLoc = AtomicBinOp->getExprLoc();
6581 ErrorRange = AtomicBinOp->getSourceRange();
6582 NoteLoc = NotScalarExpr->getExprLoc();
6583 NoteRange = NotScalarExpr->getSourceRange();
6584 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006585 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00006586 ErrorFound = NotAnAssignmentOp;
6587 ErrorLoc = AtomicBody->getExprLoc();
6588 ErrorRange = AtomicBody->getSourceRange();
6589 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6590 : AtomicBody->getExprLoc();
6591 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6592 : AtomicBody->getSourceRange();
6593 }
6594 } else {
6595 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006596 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006597 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006598 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00006599 if (ErrorFound != NoError) {
6600 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
6601 << ErrorRange;
6602 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6603 << NoteRange;
6604 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00006605 }
6606 if (CurContext->isDependentContext())
Alexey Bataevf33eba62014-11-28 07:21:40 +00006607 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006608 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006609 // If clause is update:
6610 // x++;
6611 // x--;
6612 // ++x;
6613 // --x;
6614 // x binop= expr;
6615 // x = x binop expr;
6616 // x = expr binop x;
6617 OpenMPAtomicUpdateChecker Checker(*this);
6618 if (Checker.checkStatement(
6619 Body, (AtomicKind == OMPC_update)
6620 ? diag::err_omp_atomic_update_not_expression_statement
6621 : diag::err_omp_atomic_not_expression_statement,
6622 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00006623 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006624 if (!CurContext->isDependentContext()) {
6625 E = Checker.getExpr();
6626 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006627 UE = Checker.getUpdateExpr();
6628 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00006629 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006630 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006631 enum {
6632 NotAnAssignmentOp,
6633 NotACompoundStatement,
6634 NotTwoSubstatements,
6635 NotASpecificExpression,
6636 NoError
6637 } ErrorFound = NoError;
6638 SourceLocation ErrorLoc, NoteLoc;
6639 SourceRange ErrorRange, NoteRange;
Alexey Bataeve3727102018-04-18 15:57:46 +00006640 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006641 // If clause is a capture:
6642 // v = x++;
6643 // v = x--;
6644 // v = ++x;
6645 // v = --x;
6646 // v = x binop= expr;
6647 // v = x = x binop expr;
6648 // v = x = expr binop x;
Alexey Bataeve3727102018-04-18 15:57:46 +00006649 const auto *AtomicBinOp =
Alexey Bataevb78ca832015-04-01 03:33:17 +00006650 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6651 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6652 V = AtomicBinOp->getLHS();
6653 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6654 OpenMPAtomicUpdateChecker Checker(*this);
6655 if (Checker.checkStatement(
6656 Body, diag::err_omp_atomic_capture_not_expression_statement,
6657 diag::note_omp_atomic_update))
6658 return StmtError();
6659 E = Checker.getExpr();
6660 X = Checker.getX();
6661 UE = Checker.getUpdateExpr();
6662 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6663 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00006664 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006665 ErrorLoc = AtomicBody->getExprLoc();
6666 ErrorRange = AtomicBody->getSourceRange();
6667 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6668 : AtomicBody->getExprLoc();
6669 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6670 : AtomicBody->getSourceRange();
6671 ErrorFound = NotAnAssignmentOp;
6672 }
6673 if (ErrorFound != NoError) {
6674 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6675 << ErrorRange;
6676 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6677 return StmtError();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006678 }
Alexey Bataeve3727102018-04-18 15:57:46 +00006679 if (CurContext->isDependentContext())
6680 UE = V = E = X = nullptr;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006681 } else {
6682 // If clause is a capture:
6683 // { v = x; x = expr; }
6684 // { v = x; x++; }
6685 // { v = x; x--; }
6686 // { v = x; ++x; }
6687 // { v = x; --x; }
6688 // { v = x; x binop= expr; }
6689 // { v = x; x = x binop expr; }
6690 // { v = x; x = expr binop x; }
6691 // { x++; v = x; }
6692 // { x--; v = x; }
6693 // { ++x; v = x; }
6694 // { --x; v = x; }
6695 // { x binop= expr; v = x; }
6696 // { x = x binop expr; v = x; }
6697 // { x = expr binop x; v = x; }
6698 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6699 // Check that this is { expr1; expr2; }
6700 if (CS->size() == 2) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006701 Stmt *First = CS->body_front();
6702 Stmt *Second = CS->body_back();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006703 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6704 First = EWC->getSubExpr()->IgnoreParenImpCasts();
6705 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6706 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6707 // Need to find what subexpression is 'v' and what is 'x'.
6708 OpenMPAtomicUpdateChecker Checker(*this);
6709 bool IsUpdateExprFound = !Checker.checkStatement(Second);
6710 BinaryOperator *BinOp = nullptr;
6711 if (IsUpdateExprFound) {
6712 BinOp = dyn_cast<BinaryOperator>(First);
6713 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6714 }
6715 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6716 // { v = x; x++; }
6717 // { v = x; x--; }
6718 // { v = x; ++x; }
6719 // { v = x; --x; }
6720 // { v = x; x binop= expr; }
6721 // { v = x; x = x binop expr; }
6722 // { v = x; x = expr binop x; }
6723 // Check that the first expression has form v = x.
Alexey Bataeve3727102018-04-18 15:57:46 +00006724 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006725 llvm::FoldingSetNodeID XId, PossibleXId;
6726 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6727 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6728 IsUpdateExprFound = XId == PossibleXId;
6729 if (IsUpdateExprFound) {
6730 V = BinOp->getLHS();
6731 X = Checker.getX();
6732 E = Checker.getExpr();
6733 UE = Checker.getUpdateExpr();
6734 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006735 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006736 }
6737 }
6738 if (!IsUpdateExprFound) {
6739 IsUpdateExprFound = !Checker.checkStatement(First);
6740 BinOp = nullptr;
6741 if (IsUpdateExprFound) {
6742 BinOp = dyn_cast<BinaryOperator>(Second);
6743 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6744 }
6745 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6746 // { x++; v = x; }
6747 // { x--; v = x; }
6748 // { ++x; v = x; }
6749 // { --x; v = x; }
6750 // { x binop= expr; v = x; }
6751 // { x = x binop expr; v = x; }
6752 // { x = expr binop x; v = x; }
6753 // Check that the second expression has form v = x.
Alexey Bataeve3727102018-04-18 15:57:46 +00006754 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006755 llvm::FoldingSetNodeID XId, PossibleXId;
6756 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6757 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6758 IsUpdateExprFound = XId == PossibleXId;
6759 if (IsUpdateExprFound) {
6760 V = BinOp->getLHS();
6761 X = Checker.getX();
6762 E = Checker.getExpr();
6763 UE = Checker.getUpdateExpr();
6764 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006765 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006766 }
6767 }
6768 }
6769 if (!IsUpdateExprFound) {
6770 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00006771 auto *FirstExpr = dyn_cast<Expr>(First);
6772 auto *SecondExpr = dyn_cast<Expr>(Second);
6773 if (!FirstExpr || !SecondExpr ||
6774 !(FirstExpr->isInstantiationDependent() ||
6775 SecondExpr->isInstantiationDependent())) {
6776 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6777 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006778 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00006779 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006780 : First->getBeginLoc();
Alexey Bataev5a195472015-09-04 12:55:50 +00006781 NoteRange = ErrorRange = FirstBinOp
6782 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00006783 : SourceRange(ErrorLoc, ErrorLoc);
6784 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006785 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6786 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6787 ErrorFound = NotAnAssignmentOp;
6788 NoteLoc = ErrorLoc = SecondBinOp
6789 ? SecondBinOp->getOperatorLoc()
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006790 : Second->getBeginLoc();
Alexey Bataev5a195472015-09-04 12:55:50 +00006791 NoteRange = ErrorRange =
6792 SecondBinOp ? SecondBinOp->getSourceRange()
6793 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00006794 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00006795 Expr *PossibleXRHSInFirst =
Alexey Bataev5a195472015-09-04 12:55:50 +00006796 FirstBinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00006797 Expr *PossibleXLHSInSecond =
Alexey Bataev5a195472015-09-04 12:55:50 +00006798 SecondBinOp->getLHS()->IgnoreParenImpCasts();
6799 llvm::FoldingSetNodeID X1Id, X2Id;
6800 PossibleXRHSInFirst->Profile(X1Id, Context,
6801 /*Canonical=*/true);
6802 PossibleXLHSInSecond->Profile(X2Id, Context,
6803 /*Canonical=*/true);
6804 IsUpdateExprFound = X1Id == X2Id;
6805 if (IsUpdateExprFound) {
6806 V = FirstBinOp->getLHS();
6807 X = SecondBinOp->getLHS();
6808 E = SecondBinOp->getRHS();
6809 UE = nullptr;
6810 IsXLHSInRHSPart = false;
6811 IsPostfixUpdate = true;
6812 } else {
6813 ErrorFound = NotASpecificExpression;
6814 ErrorLoc = FirstBinOp->getExprLoc();
6815 ErrorRange = FirstBinOp->getSourceRange();
6816 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6817 NoteRange = SecondBinOp->getRHS()->getSourceRange();
6818 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006819 }
6820 }
6821 }
6822 }
6823 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006824 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006825 NoteRange = ErrorRange =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006826 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
Alexey Bataevb78ca832015-04-01 03:33:17 +00006827 ErrorFound = NotTwoSubstatements;
6828 }
6829 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006830 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006831 NoteRange = ErrorRange =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006832 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
Alexey Bataevb78ca832015-04-01 03:33:17 +00006833 ErrorFound = NotACompoundStatement;
6834 }
6835 if (ErrorFound != NoError) {
6836 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6837 << ErrorRange;
6838 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6839 return StmtError();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006840 }
Alexey Bataeve3727102018-04-18 15:57:46 +00006841 if (CurContext->isDependentContext())
6842 UE = V = E = X = nullptr;
Alexey Bataev459dec02014-07-24 06:46:57 +00006843 }
Alexey Bataevdea47612014-07-23 07:46:59 +00006844 }
Alexey Bataev0162e452014-07-22 10:10:35 +00006845
Reid Kleckner87a31802018-03-12 21:43:02 +00006846 setFunctionHasBranchProtectedScope();
Alexey Bataev0162e452014-07-22 10:10:35 +00006847
Alexey Bataev62cec442014-11-18 10:14:22 +00006848 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00006849 X, V, E, UE, IsXLHSInRHSPart,
6850 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00006851}
6852
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006853StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6854 Stmt *AStmt,
6855 SourceLocation StartLoc,
6856 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006857 if (!AStmt)
6858 return StmtError();
6859
Alexey Bataeve3727102018-04-18 15:57:46 +00006860 auto *CS = cast<CapturedStmt>(AStmt);
Samuel Antao4af1b7b2015-12-02 17:44:43 +00006861 // 1.2.2 OpenMP Language Terminology
6862 // Structured block - An executable statement with a single entry at the
6863 // top and a single exit at the bottom.
6864 // The point of exit cannot be a branch out of the structured block.
6865 // longjmp() and throw() must not violate the entry/exit criteria.
6866 CS->getCapturedDecl()->setNothrow();
Alexey Bataev8451efa2018-01-15 19:06:12 +00006867 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target);
6868 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6869 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6870 // 1.2.2 OpenMP Language Terminology
6871 // Structured block - An executable statement with a single entry at the
6872 // top and a single exit at the bottom.
6873 // The point of exit cannot be a branch out of the structured block.
6874 // longjmp() and throw() must not violate the entry/exit criteria.
6875 CS->getCapturedDecl()->setNothrow();
6876 }
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006877
Alexey Bataev13314bf2014-10-09 04:18:56 +00006878 // OpenMP [2.16, Nesting of Regions]
6879 // If specified, a teams construct must be contained within a target
6880 // construct. That target construct must contain no statements or directives
6881 // outside of the teams construct.
6882 if (DSAStack->hasInnerTeamsRegion()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006883 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true);
Alexey Bataev13314bf2014-10-09 04:18:56 +00006884 bool OMPTeamsFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006885 if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00006886 auto I = CS->body_begin();
6887 while (I != CS->body_end()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006888 const auto *OED = dyn_cast<OMPExecutableDirective>(*I);
Alexey Bataev13314bf2014-10-09 04:18:56 +00006889 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6890 OMPTeamsFound = false;
6891 break;
6892 }
6893 ++I;
6894 }
6895 assert(I != CS->body_end() && "Not found statement");
6896 S = *I;
Kelvin Li3834dce2016-06-27 19:15:43 +00006897 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00006898 const auto *OED = dyn_cast<OMPExecutableDirective>(S);
Kelvin Li3834dce2016-06-27 19:15:43 +00006899 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
Alexey Bataev13314bf2014-10-09 04:18:56 +00006900 }
6901 if (!OMPTeamsFound) {
6902 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6903 Diag(DSAStack->getInnerTeamsRegionLoc(),
6904 diag::note_omp_nested_teams_construct_here);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006905 Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here)
Alexey Bataev13314bf2014-10-09 04:18:56 +00006906 << isa<OMPExecutableDirective>(S);
6907 return StmtError();
6908 }
6909 }
6910
Reid Kleckner87a31802018-03-12 21:43:02 +00006911 setFunctionHasBranchProtectedScope();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006912
6913 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6914}
6915
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006916StmtResult
6917Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6918 Stmt *AStmt, SourceLocation StartLoc,
6919 SourceLocation EndLoc) {
6920 if (!AStmt)
6921 return StmtError();
6922
Alexey Bataeve3727102018-04-18 15:57:46 +00006923 auto *CS = cast<CapturedStmt>(AStmt);
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006924 // 1.2.2 OpenMP Language Terminology
6925 // Structured block - An executable statement with a single entry at the
6926 // top and a single exit at the bottom.
6927 // The point of exit cannot be a branch out of the structured block.
6928 // longjmp() and throw() must not violate the entry/exit criteria.
6929 CS->getCapturedDecl()->setNothrow();
Alexey Bataev8451efa2018-01-15 19:06:12 +00006930 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel);
6931 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6932 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6933 // 1.2.2 OpenMP Language Terminology
6934 // Structured block - An executable statement with a single entry at the
6935 // top and a single exit at the bottom.
6936 // The point of exit cannot be a branch out of the structured block.
6937 // longjmp() and throw() must not violate the entry/exit criteria.
6938 CS->getCapturedDecl()->setNothrow();
6939 }
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006940
Reid Kleckner87a31802018-03-12 21:43:02 +00006941 setFunctionHasBranchProtectedScope();
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006942
6943 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6944 AStmt);
6945}
6946
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006947StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6948 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00006949 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006950 if (!AStmt)
6951 return StmtError();
6952
Alexey Bataeve3727102018-04-18 15:57:46 +00006953 auto *CS = cast<CapturedStmt>(AStmt);
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006954 // 1.2.2 OpenMP Language Terminology
6955 // Structured block - An executable statement with a single entry at the
6956 // top and a single exit at the bottom.
6957 // The point of exit cannot be a branch out of the structured block.
6958 // longjmp() and throw() must not violate the entry/exit criteria.
6959 CS->getCapturedDecl()->setNothrow();
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00006960 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
6961 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6962 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6963 // 1.2.2 OpenMP Language Terminology
6964 // Structured block - An executable statement with a single entry at the
6965 // top and a single exit at the bottom.
6966 // The point of exit cannot be a branch out of the structured block.
6967 // longjmp() and throw() must not violate the entry/exit criteria.
6968 CS->getCapturedDecl()->setNothrow();
6969 }
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006970
6971 OMPLoopDirective::HelperExprs B;
6972 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6973 // define the nested loops number.
6974 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00006975 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00006976 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006977 VarsWithImplicitDSA, B);
6978 if (NestedLoopCount == 0)
6979 return StmtError();
6980
6981 assert((CurContext->isDependentContext() || B.builtAll()) &&
6982 "omp target parallel for loop exprs were not built");
6983
6984 if (!CurContext->isDependentContext()) {
6985 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00006986 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00006987 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006988 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006989 B.NumIterations, *this, CurScope,
6990 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006991 return StmtError();
6992 }
6993 }
6994
Reid Kleckner87a31802018-03-12 21:43:02 +00006995 setFunctionHasBranchProtectedScope();
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006996 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
6997 NestedLoopCount, Clauses, AStmt,
6998 B, DSAStack->isCancelRegion());
6999}
7000
Alexey Bataev95b64a92017-05-30 16:00:04 +00007001/// Check for existence of a map clause in the list of clauses.
7002static bool hasClauses(ArrayRef<OMPClause *> Clauses,
7003 const OpenMPClauseKind K) {
7004 return llvm::any_of(
7005 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; });
7006}
Samuel Antaodf67fc42016-01-19 19:15:56 +00007007
Alexey Bataev95b64a92017-05-30 16:00:04 +00007008template <typename... Params>
7009static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K,
7010 const Params... ClauseTypes) {
7011 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...);
Samuel Antaodf67fc42016-01-19 19:15:56 +00007012}
7013
Michael Wong65f367f2015-07-21 13:44:28 +00007014StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
7015 Stmt *AStmt,
7016 SourceLocation StartLoc,
7017 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007018 if (!AStmt)
7019 return StmtError();
7020
7021 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7022
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00007023 // OpenMP [2.10.1, Restrictions, p. 97]
7024 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00007025 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) {
7026 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
7027 << "'map' or 'use_device_ptr'"
David Majnemer9d168222016-08-05 17:44:54 +00007028 << getOpenMPDirectiveName(OMPD_target_data);
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00007029 return StmtError();
7030 }
7031
Reid Kleckner87a31802018-03-12 21:43:02 +00007032 setFunctionHasBranchProtectedScope();
Michael Wong65f367f2015-07-21 13:44:28 +00007033
7034 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
7035 AStmt);
7036}
7037
Samuel Antaodf67fc42016-01-19 19:15:56 +00007038StmtResult
7039Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
7040 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00007041 SourceLocation EndLoc, Stmt *AStmt) {
7042 if (!AStmt)
7043 return StmtError();
7044
Alexey Bataeve3727102018-04-18 15:57:46 +00007045 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00007046 // 1.2.2 OpenMP Language Terminology
7047 // Structured block - An executable statement with a single entry at the
7048 // top and a single exit at the bottom.
7049 // The point of exit cannot be a branch out of the structured block.
7050 // longjmp() and throw() must not violate the entry/exit criteria.
7051 CS->getCapturedDecl()->setNothrow();
7052 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data);
7053 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7054 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7055 // 1.2.2 OpenMP Language Terminology
7056 // Structured block - An executable statement with a single entry at the
7057 // top and a single exit at the bottom.
7058 // The point of exit cannot be a branch out of the structured block.
7059 // longjmp() and throw() must not violate the entry/exit criteria.
7060 CS->getCapturedDecl()->setNothrow();
7061 }
7062
Samuel Antaodf67fc42016-01-19 19:15:56 +00007063 // OpenMP [2.10.2, Restrictions, p. 99]
7064 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00007065 if (!hasClauses(Clauses, OMPC_map)) {
7066 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
7067 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data);
Samuel Antaodf67fc42016-01-19 19:15:56 +00007068 return StmtError();
7069 }
7070
Alexey Bataev7828b252017-11-21 17:08:48 +00007071 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
7072 AStmt);
Samuel Antaodf67fc42016-01-19 19:15:56 +00007073}
7074
Samuel Antao72590762016-01-19 20:04:50 +00007075StmtResult
7076Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
7077 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00007078 SourceLocation EndLoc, Stmt *AStmt) {
7079 if (!AStmt)
7080 return StmtError();
7081
Alexey Bataeve3727102018-04-18 15:57:46 +00007082 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00007083 // 1.2.2 OpenMP Language Terminology
7084 // Structured block - An executable statement with a single entry at the
7085 // top and a single exit at the bottom.
7086 // The point of exit cannot be a branch out of the structured block.
7087 // longjmp() and throw() must not violate the entry/exit criteria.
7088 CS->getCapturedDecl()->setNothrow();
7089 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data);
7090 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7091 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7092 // 1.2.2 OpenMP Language Terminology
7093 // Structured block - An executable statement with a single entry at the
7094 // top and a single exit at the bottom.
7095 // The point of exit cannot be a branch out of the structured block.
7096 // longjmp() and throw() must not violate the entry/exit criteria.
7097 CS->getCapturedDecl()->setNothrow();
7098 }
7099
Samuel Antao72590762016-01-19 20:04:50 +00007100 // OpenMP [2.10.3, Restrictions, p. 102]
7101 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00007102 if (!hasClauses(Clauses, OMPC_map)) {
7103 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
7104 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data);
Samuel Antao72590762016-01-19 20:04:50 +00007105 return StmtError();
7106 }
7107
Alexey Bataev7828b252017-11-21 17:08:48 +00007108 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
7109 AStmt);
Samuel Antao72590762016-01-19 20:04:50 +00007110}
7111
Samuel Antao686c70c2016-05-26 17:30:50 +00007112StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
7113 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00007114 SourceLocation EndLoc,
7115 Stmt *AStmt) {
7116 if (!AStmt)
7117 return StmtError();
7118
Alexey Bataeve3727102018-04-18 15:57:46 +00007119 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00007120 // 1.2.2 OpenMP Language Terminology
7121 // Structured block - An executable statement with a single entry at the
7122 // top and a single exit at the bottom.
7123 // The point of exit cannot be a branch out of the structured block.
7124 // longjmp() and throw() must not violate the entry/exit criteria.
7125 CS->getCapturedDecl()->setNothrow();
7126 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update);
7127 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7128 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7129 // 1.2.2 OpenMP Language Terminology
7130 // Structured block - An executable statement with a single entry at the
7131 // top and a single exit at the bottom.
7132 // The point of exit cannot be a branch out of the structured block.
7133 // longjmp() and throw() must not violate the entry/exit criteria.
7134 CS->getCapturedDecl()->setNothrow();
7135 }
7136
Alexey Bataev95b64a92017-05-30 16:00:04 +00007137 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) {
Samuel Antao686c70c2016-05-26 17:30:50 +00007138 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
7139 return StmtError();
7140 }
Alexey Bataev7828b252017-11-21 17:08:48 +00007141 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses,
7142 AStmt);
Samuel Antao686c70c2016-05-26 17:30:50 +00007143}
7144
Alexey Bataev13314bf2014-10-09 04:18:56 +00007145StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
7146 Stmt *AStmt, SourceLocation StartLoc,
7147 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007148 if (!AStmt)
7149 return StmtError();
7150
Alexey Bataeve3727102018-04-18 15:57:46 +00007151 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev13314bf2014-10-09 04:18:56 +00007152 // 1.2.2 OpenMP Language Terminology
7153 // Structured block - An executable statement with a single entry at the
7154 // top and a single exit at the bottom.
7155 // The point of exit cannot be a branch out of the structured block.
7156 // longjmp() and throw() must not violate the entry/exit criteria.
7157 CS->getCapturedDecl()->setNothrow();
7158
Reid Kleckner87a31802018-03-12 21:43:02 +00007159 setFunctionHasBranchProtectedScope();
Alexey Bataev13314bf2014-10-09 04:18:56 +00007160
Alexey Bataevceabd412017-11-30 18:01:54 +00007161 DSAStack->setParentTeamsRegionLoc(StartLoc);
7162
Alexey Bataev13314bf2014-10-09 04:18:56 +00007163 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
7164}
7165
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007166StmtResult
7167Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
7168 SourceLocation EndLoc,
7169 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007170 if (DSAStack->isParentNowaitRegion()) {
7171 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
7172 return StmtError();
7173 }
7174 if (DSAStack->isParentOrderedRegion()) {
7175 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
7176 return StmtError();
7177 }
7178 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
7179 CancelRegion);
7180}
7181
Alexey Bataev87933c72015-09-18 08:07:34 +00007182StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
7183 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00007184 SourceLocation EndLoc,
7185 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev80909872015-07-02 11:25:17 +00007186 if (DSAStack->isParentNowaitRegion()) {
7187 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
7188 return StmtError();
7189 }
7190 if (DSAStack->isParentOrderedRegion()) {
7191 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
7192 return StmtError();
7193 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00007194 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00007195 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
7196 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00007197}
7198
Alexey Bataev382967a2015-12-08 12:06:20 +00007199static bool checkGrainsizeNumTasksClauses(Sema &S,
7200 ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007201 const OMPClause *PrevClause = nullptr;
Alexey Bataev382967a2015-12-08 12:06:20 +00007202 bool ErrorFound = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00007203 for (const OMPClause *C : Clauses) {
Alexey Bataev382967a2015-12-08 12:06:20 +00007204 if (C->getClauseKind() == OMPC_grainsize ||
7205 C->getClauseKind() == OMPC_num_tasks) {
7206 if (!PrevClause)
7207 PrevClause = C;
7208 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007209 S.Diag(C->getBeginLoc(),
Alexey Bataev382967a2015-12-08 12:06:20 +00007210 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
7211 << getOpenMPClauseName(C->getClauseKind())
7212 << getOpenMPClauseName(PrevClause->getClauseKind());
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007213 S.Diag(PrevClause->getBeginLoc(),
Alexey Bataev382967a2015-12-08 12:06:20 +00007214 diag::note_omp_previous_grainsize_num_tasks)
7215 << getOpenMPClauseName(PrevClause->getClauseKind());
7216 ErrorFound = true;
7217 }
7218 }
7219 }
7220 return ErrorFound;
7221}
7222
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007223static bool checkReductionClauseWithNogroup(Sema &S,
7224 ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007225 const OMPClause *ReductionClause = nullptr;
7226 const OMPClause *NogroupClause = nullptr;
7227 for (const OMPClause *C : Clauses) {
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007228 if (C->getClauseKind() == OMPC_reduction) {
7229 ReductionClause = C;
7230 if (NogroupClause)
7231 break;
7232 continue;
7233 }
7234 if (C->getClauseKind() == OMPC_nogroup) {
7235 NogroupClause = C;
7236 if (ReductionClause)
7237 break;
7238 continue;
7239 }
7240 }
7241 if (ReductionClause && NogroupClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007242 S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup)
7243 << SourceRange(NogroupClause->getBeginLoc(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007244 NogroupClause->getEndLoc());
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007245 return true;
7246 }
7247 return false;
7248}
7249
Alexey Bataev49f6e782015-12-01 04:18:41 +00007250StmtResult Sema::ActOnOpenMPTaskLoopDirective(
7251 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007252 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00007253 if (!AStmt)
7254 return StmtError();
7255
7256 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7257 OMPLoopDirective::HelperExprs B;
7258 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7259 // define the nested loops number.
7260 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007261 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007262 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00007263 VarsWithImplicitDSA, B);
7264 if (NestedLoopCount == 0)
7265 return StmtError();
7266
7267 assert((CurContext->isDependentContext() || B.builtAll()) &&
7268 "omp for loop exprs were not built");
7269
Alexey Bataev382967a2015-12-08 12:06:20 +00007270 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7271 // The grainsize clause and num_tasks clause are mutually exclusive and may
7272 // not appear on the same taskloop directive.
7273 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7274 return StmtError();
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007275 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7276 // If a reduction clause is present on the taskloop directive, the nogroup
7277 // clause must not be specified.
7278 if (checkReductionClauseWithNogroup(*this, Clauses))
7279 return StmtError();
Alexey Bataev382967a2015-12-08 12:06:20 +00007280
Reid Kleckner87a31802018-03-12 21:43:02 +00007281 setFunctionHasBranchProtectedScope();
Alexey Bataev49f6e782015-12-01 04:18:41 +00007282 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
7283 NestedLoopCount, Clauses, AStmt, B);
7284}
7285
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007286StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
7287 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007288 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007289 if (!AStmt)
7290 return StmtError();
7291
7292 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7293 OMPLoopDirective::HelperExprs B;
7294 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7295 // define the nested loops number.
7296 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007297 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007298 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
7299 VarsWithImplicitDSA, B);
7300 if (NestedLoopCount == 0)
7301 return StmtError();
7302
7303 assert((CurContext->isDependentContext() || B.builtAll()) &&
7304 "omp for loop exprs were not built");
7305
Alexey Bataev5a3af132016-03-29 08:58:54 +00007306 if (!CurContext->isDependentContext()) {
7307 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007308 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007309 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007310 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007311 B.NumIterations, *this, CurScope,
7312 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007313 return StmtError();
7314 }
7315 }
7316
Alexey Bataev382967a2015-12-08 12:06:20 +00007317 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7318 // The grainsize clause and num_tasks clause are mutually exclusive and may
7319 // not appear on the same taskloop directive.
7320 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7321 return StmtError();
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007322 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7323 // If a reduction clause is present on the taskloop directive, the nogroup
7324 // clause must not be specified.
7325 if (checkReductionClauseWithNogroup(*this, Clauses))
7326 return StmtError();
Alexey Bataev438388c2017-11-22 18:34:02 +00007327 if (checkSimdlenSafelenSpecified(*this, Clauses))
7328 return StmtError();
Alexey Bataev382967a2015-12-08 12:06:20 +00007329
Reid Kleckner87a31802018-03-12 21:43:02 +00007330 setFunctionHasBranchProtectedScope();
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007331 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
7332 NestedLoopCount, Clauses, AStmt, B);
7333}
7334
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007335StmtResult Sema::ActOnOpenMPDistributeDirective(
7336 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007337 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007338 if (!AStmt)
7339 return StmtError();
7340
7341 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7342 OMPLoopDirective::HelperExprs B;
7343 // In presence of clause 'collapse' with number of loops, it will
7344 // define the nested loops number.
7345 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007346 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007347 nullptr /*ordered not a clause on distribute*/, AStmt,
7348 *this, *DSAStack, VarsWithImplicitDSA, B);
7349 if (NestedLoopCount == 0)
7350 return StmtError();
7351
7352 assert((CurContext->isDependentContext() || B.builtAll()) &&
7353 "omp for loop exprs were not built");
7354
Reid Kleckner87a31802018-03-12 21:43:02 +00007355 setFunctionHasBranchProtectedScope();
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007356 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
7357 NestedLoopCount, Clauses, AStmt, B);
7358}
7359
Carlo Bertolli9925f152016-06-27 14:55:37 +00007360StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
7361 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007362 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Carlo Bertolli9925f152016-06-27 14:55:37 +00007363 if (!AStmt)
7364 return StmtError();
7365
Alexey Bataeve3727102018-04-18 15:57:46 +00007366 auto *CS = cast<CapturedStmt>(AStmt);
Carlo Bertolli9925f152016-06-27 14:55:37 +00007367 // 1.2.2 OpenMP Language Terminology
7368 // Structured block - An executable statement with a single entry at the
7369 // top and a single exit at the bottom.
7370 // The point of exit cannot be a branch out of the structured block.
7371 // longjmp() and throw() must not violate the entry/exit criteria.
7372 CS->getCapturedDecl()->setNothrow();
Alexey Bataev7f96c372017-11-22 17:19:31 +00007373 for (int ThisCaptureLevel =
7374 getOpenMPCaptureLevels(OMPD_distribute_parallel_for);
7375 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7376 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7377 // 1.2.2 OpenMP Language Terminology
7378 // Structured block - An executable statement with a single entry at the
7379 // top and a single exit at the bottom.
7380 // The point of exit cannot be a branch out of the structured block.
7381 // longjmp() and throw() must not violate the entry/exit criteria.
7382 CS->getCapturedDecl()->setNothrow();
7383 }
Carlo Bertolli9925f152016-06-27 14:55:37 +00007384
7385 OMPLoopDirective::HelperExprs B;
7386 // In presence of clause 'collapse' with number of loops, it will
7387 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007388 unsigned NestedLoopCount = checkOpenMPLoop(
Carlo Bertolli9925f152016-06-27 14:55:37 +00007389 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataev7f96c372017-11-22 17:19:31 +00007390 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Carlo Bertolli9925f152016-06-27 14:55:37 +00007391 VarsWithImplicitDSA, B);
7392 if (NestedLoopCount == 0)
7393 return StmtError();
7394
7395 assert((CurContext->isDependentContext() || B.builtAll()) &&
7396 "omp for loop exprs were not built");
7397
Reid Kleckner87a31802018-03-12 21:43:02 +00007398 setFunctionHasBranchProtectedScope();
Carlo Bertolli9925f152016-06-27 14:55:37 +00007399 return OMPDistributeParallelForDirective::Create(
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00007400 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7401 DSAStack->isCancelRegion());
Carlo Bertolli9925f152016-06-27 14:55:37 +00007402}
7403
Kelvin Li4a39add2016-07-05 05:00:15 +00007404StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
7405 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007406 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li4a39add2016-07-05 05:00:15 +00007407 if (!AStmt)
7408 return StmtError();
7409
Alexey Bataeve3727102018-04-18 15:57:46 +00007410 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li4a39add2016-07-05 05:00:15 +00007411 // 1.2.2 OpenMP Language Terminology
7412 // Structured block - An executable statement with a single entry at the
7413 // top and a single exit at the bottom.
7414 // The point of exit cannot be a branch out of the structured block.
7415 // longjmp() and throw() must not violate the entry/exit criteria.
7416 CS->getCapturedDecl()->setNothrow();
Alexey Bataev974acd62017-11-27 19:38:52 +00007417 for (int ThisCaptureLevel =
7418 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd);
7419 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7420 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7421 // 1.2.2 OpenMP Language Terminology
7422 // Structured block - An executable statement with a single entry at the
7423 // top and a single exit at the bottom.
7424 // The point of exit cannot be a branch out of the structured block.
7425 // longjmp() and throw() must not violate the entry/exit criteria.
7426 CS->getCapturedDecl()->setNothrow();
7427 }
Kelvin Li4a39add2016-07-05 05:00:15 +00007428
7429 OMPLoopDirective::HelperExprs B;
7430 // In presence of clause 'collapse' with number of loops, it will
7431 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007432 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li4a39add2016-07-05 05:00:15 +00007433 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev974acd62017-11-27 19:38:52 +00007434 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li4a39add2016-07-05 05:00:15 +00007435 VarsWithImplicitDSA, B);
7436 if (NestedLoopCount == 0)
7437 return StmtError();
7438
7439 assert((CurContext->isDependentContext() || B.builtAll()) &&
7440 "omp for loop exprs were not built");
7441
Alexey Bataev438388c2017-11-22 18:34:02 +00007442 if (!CurContext->isDependentContext()) {
7443 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007444 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00007445 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7446 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7447 B.NumIterations, *this, CurScope,
7448 DSAStack))
7449 return StmtError();
7450 }
7451 }
7452
Kelvin Lic5609492016-07-15 04:39:07 +00007453 if (checkSimdlenSafelenSpecified(*this, Clauses))
7454 return StmtError();
7455
Reid Kleckner87a31802018-03-12 21:43:02 +00007456 setFunctionHasBranchProtectedScope();
Kelvin Li4a39add2016-07-05 05:00:15 +00007457 return OMPDistributeParallelForSimdDirective::Create(
7458 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7459}
7460
Kelvin Li787f3fc2016-07-06 04:45:38 +00007461StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
7462 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007463 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li787f3fc2016-07-06 04:45:38 +00007464 if (!AStmt)
7465 return StmtError();
7466
Alexey Bataeve3727102018-04-18 15:57:46 +00007467 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li787f3fc2016-07-06 04:45:38 +00007468 // 1.2.2 OpenMP Language Terminology
7469 // Structured block - An executable statement with a single entry at the
7470 // top and a single exit at the bottom.
7471 // The point of exit cannot be a branch out of the structured block.
7472 // longjmp() and throw() must not violate the entry/exit criteria.
7473 CS->getCapturedDecl()->setNothrow();
Alexey Bataev617db5f2017-12-04 15:38:33 +00007474 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd);
7475 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7476 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7477 // 1.2.2 OpenMP Language Terminology
7478 // Structured block - An executable statement with a single entry at the
7479 // top and a single exit at the bottom.
7480 // The point of exit cannot be a branch out of the structured block.
7481 // longjmp() and throw() must not violate the entry/exit criteria.
7482 CS->getCapturedDecl()->setNothrow();
7483 }
Kelvin Li787f3fc2016-07-06 04:45:38 +00007484
7485 OMPLoopDirective::HelperExprs B;
7486 // In presence of clause 'collapse' with number of loops, it will
7487 // define the nested loops number.
7488 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007489 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev617db5f2017-12-04 15:38:33 +00007490 nullptr /*ordered not a clause on distribute*/, CS, *this,
7491 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li787f3fc2016-07-06 04:45:38 +00007492 if (NestedLoopCount == 0)
7493 return StmtError();
7494
7495 assert((CurContext->isDependentContext() || B.builtAll()) &&
7496 "omp for loop exprs were not built");
7497
Alexey Bataev438388c2017-11-22 18:34:02 +00007498 if (!CurContext->isDependentContext()) {
7499 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007500 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00007501 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7502 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7503 B.NumIterations, *this, CurScope,
7504 DSAStack))
7505 return StmtError();
7506 }
7507 }
7508
Kelvin Lic5609492016-07-15 04:39:07 +00007509 if (checkSimdlenSafelenSpecified(*this, Clauses))
7510 return StmtError();
7511
Reid Kleckner87a31802018-03-12 21:43:02 +00007512 setFunctionHasBranchProtectedScope();
Kelvin Li787f3fc2016-07-06 04:45:38 +00007513 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
7514 NestedLoopCount, Clauses, AStmt, B);
7515}
7516
Kelvin Lia579b912016-07-14 02:54:56 +00007517StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
7518 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007519 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Lia579b912016-07-14 02:54:56 +00007520 if (!AStmt)
7521 return StmtError();
7522
Alexey Bataeve3727102018-04-18 15:57:46 +00007523 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Lia579b912016-07-14 02:54:56 +00007524 // 1.2.2 OpenMP Language Terminology
7525 // Structured block - An executable statement with a single entry at the
7526 // top and a single exit at the bottom.
7527 // The point of exit cannot be a branch out of the structured block.
7528 // longjmp() and throw() must not violate the entry/exit criteria.
7529 CS->getCapturedDecl()->setNothrow();
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007530 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
7531 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7532 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7533 // 1.2.2 OpenMP Language Terminology
7534 // Structured block - An executable statement with a single entry at the
7535 // top and a single exit at the bottom.
7536 // The point of exit cannot be a branch out of the structured block.
7537 // longjmp() and throw() must not violate the entry/exit criteria.
7538 CS->getCapturedDecl()->setNothrow();
7539 }
Kelvin Lia579b912016-07-14 02:54:56 +00007540
7541 OMPLoopDirective::HelperExprs B;
7542 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7543 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007544 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Lia579b912016-07-14 02:54:56 +00007545 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007546 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Kelvin Lia579b912016-07-14 02:54:56 +00007547 VarsWithImplicitDSA, B);
7548 if (NestedLoopCount == 0)
7549 return StmtError();
7550
7551 assert((CurContext->isDependentContext() || B.builtAll()) &&
7552 "omp target parallel for simd loop exprs were not built");
7553
7554 if (!CurContext->isDependentContext()) {
7555 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007556 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007557 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Lia579b912016-07-14 02:54:56 +00007558 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7559 B.NumIterations, *this, CurScope,
7560 DSAStack))
7561 return StmtError();
7562 }
7563 }
Kelvin Lic5609492016-07-15 04:39:07 +00007564 if (checkSimdlenSafelenSpecified(*this, Clauses))
Kelvin Lia579b912016-07-14 02:54:56 +00007565 return StmtError();
7566
Reid Kleckner87a31802018-03-12 21:43:02 +00007567 setFunctionHasBranchProtectedScope();
Kelvin Lia579b912016-07-14 02:54:56 +00007568 return OMPTargetParallelForSimdDirective::Create(
7569 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7570}
7571
Kelvin Li986330c2016-07-20 22:57:10 +00007572StmtResult Sema::ActOnOpenMPTargetSimdDirective(
7573 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007574 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li986330c2016-07-20 22:57:10 +00007575 if (!AStmt)
7576 return StmtError();
7577
Alexey Bataeve3727102018-04-18 15:57:46 +00007578 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li986330c2016-07-20 22:57:10 +00007579 // 1.2.2 OpenMP Language Terminology
7580 // Structured block - An executable statement with a single entry at the
7581 // top and a single exit at the bottom.
7582 // The point of exit cannot be a branch out of the structured block.
7583 // longjmp() and throw() must not violate the entry/exit criteria.
7584 CS->getCapturedDecl()->setNothrow();
Alexey Bataevf8365372017-11-17 17:57:25 +00007585 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd);
7586 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7587 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7588 // 1.2.2 OpenMP Language Terminology
7589 // Structured block - An executable statement with a single entry at the
7590 // top and a single exit at the bottom.
7591 // The point of exit cannot be a branch out of the structured block.
7592 // longjmp() and throw() must not violate the entry/exit criteria.
7593 CS->getCapturedDecl()->setNothrow();
7594 }
7595
Kelvin Li986330c2016-07-20 22:57:10 +00007596 OMPLoopDirective::HelperExprs B;
7597 // In presence of clause 'collapse' with number of loops, it will define the
7598 // nested loops number.
David Majnemer9d168222016-08-05 17:44:54 +00007599 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007600 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
Alexey Bataevf8365372017-11-17 17:57:25 +00007601 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Kelvin Li986330c2016-07-20 22:57:10 +00007602 VarsWithImplicitDSA, B);
7603 if (NestedLoopCount == 0)
7604 return StmtError();
7605
7606 assert((CurContext->isDependentContext() || B.builtAll()) &&
7607 "omp target simd loop exprs were not built");
7608
7609 if (!CurContext->isDependentContext()) {
7610 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007611 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007612 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Li986330c2016-07-20 22:57:10 +00007613 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7614 B.NumIterations, *this, CurScope,
7615 DSAStack))
7616 return StmtError();
7617 }
7618 }
7619
7620 if (checkSimdlenSafelenSpecified(*this, Clauses))
7621 return StmtError();
7622
Reid Kleckner87a31802018-03-12 21:43:02 +00007623 setFunctionHasBranchProtectedScope();
Kelvin Li986330c2016-07-20 22:57:10 +00007624 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
7625 NestedLoopCount, Clauses, AStmt, B);
7626}
7627
Kelvin Li02532872016-08-05 14:37:37 +00007628StmtResult Sema::ActOnOpenMPTeamsDistributeDirective(
7629 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007630 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li02532872016-08-05 14:37:37 +00007631 if (!AStmt)
7632 return StmtError();
7633
Alexey Bataeve3727102018-04-18 15:57:46 +00007634 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li02532872016-08-05 14:37:37 +00007635 // 1.2.2 OpenMP Language Terminology
7636 // Structured block - An executable statement with a single entry at the
7637 // top and a single exit at the bottom.
7638 // The point of exit cannot be a branch out of the structured block.
7639 // longjmp() and throw() must not violate the entry/exit criteria.
7640 CS->getCapturedDecl()->setNothrow();
Alexey Bataev95c6dd42017-11-29 15:14:16 +00007641 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute);
7642 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7643 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7644 // 1.2.2 OpenMP Language Terminology
7645 // Structured block - An executable statement with a single entry at the
7646 // top and a single exit at the bottom.
7647 // The point of exit cannot be a branch out of the structured block.
7648 // longjmp() and throw() must not violate the entry/exit criteria.
7649 CS->getCapturedDecl()->setNothrow();
7650 }
Kelvin Li02532872016-08-05 14:37:37 +00007651
7652 OMPLoopDirective::HelperExprs B;
7653 // In presence of clause 'collapse' with number of loops, it will
7654 // define the nested loops number.
7655 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007656 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses),
Alexey Bataev95c6dd42017-11-29 15:14:16 +00007657 nullptr /*ordered not a clause on distribute*/, CS, *this,
7658 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li02532872016-08-05 14:37:37 +00007659 if (NestedLoopCount == 0)
7660 return StmtError();
7661
7662 assert((CurContext->isDependentContext() || B.builtAll()) &&
7663 "omp teams distribute loop exprs were not built");
7664
Reid Kleckner87a31802018-03-12 21:43:02 +00007665 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007666
7667 DSAStack->setParentTeamsRegionLoc(StartLoc);
7668
David Majnemer9d168222016-08-05 17:44:54 +00007669 return OMPTeamsDistributeDirective::Create(
7670 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Kelvin Li02532872016-08-05 14:37:37 +00007671}
7672
Kelvin Li4e325f72016-10-25 12:50:55 +00007673StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective(
7674 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007675 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li4e325f72016-10-25 12:50:55 +00007676 if (!AStmt)
7677 return StmtError();
7678
Alexey Bataeve3727102018-04-18 15:57:46 +00007679 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li4e325f72016-10-25 12:50:55 +00007680 // 1.2.2 OpenMP Language Terminology
7681 // Structured block - An executable statement with a single entry at the
7682 // top and a single exit at the bottom.
7683 // The point of exit cannot be a branch out of the structured block.
7684 // longjmp() and throw() must not violate the entry/exit criteria.
7685 CS->getCapturedDecl()->setNothrow();
Alexey Bataev999277a2017-12-06 14:31:09 +00007686 for (int ThisCaptureLevel =
7687 getOpenMPCaptureLevels(OMPD_teams_distribute_simd);
7688 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7689 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7690 // 1.2.2 OpenMP Language Terminology
7691 // Structured block - An executable statement with a single entry at the
7692 // top and a single exit at the bottom.
7693 // The point of exit cannot be a branch out of the structured block.
7694 // longjmp() and throw() must not violate the entry/exit criteria.
7695 CS->getCapturedDecl()->setNothrow();
7696 }
7697
Kelvin Li4e325f72016-10-25 12:50:55 +00007698
7699 OMPLoopDirective::HelperExprs B;
7700 // In presence of clause 'collapse' with number of loops, it will
7701 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007702 unsigned NestedLoopCount = checkOpenMPLoop(
Samuel Antao4c8035b2016-12-12 18:00:20 +00007703 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev999277a2017-12-06 14:31:09 +00007704 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Samuel Antao4c8035b2016-12-12 18:00:20 +00007705 VarsWithImplicitDSA, B);
Kelvin Li4e325f72016-10-25 12:50:55 +00007706
7707 if (NestedLoopCount == 0)
7708 return StmtError();
7709
7710 assert((CurContext->isDependentContext() || B.builtAll()) &&
7711 "omp teams distribute simd loop exprs were not built");
7712
7713 if (!CurContext->isDependentContext()) {
7714 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007715 for (OMPClause *C : Clauses) {
Kelvin Li4e325f72016-10-25 12:50:55 +00007716 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7717 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7718 B.NumIterations, *this, CurScope,
7719 DSAStack))
7720 return StmtError();
7721 }
7722 }
7723
7724 if (checkSimdlenSafelenSpecified(*this, Clauses))
7725 return StmtError();
7726
Reid Kleckner87a31802018-03-12 21:43:02 +00007727 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007728
7729 DSAStack->setParentTeamsRegionLoc(StartLoc);
7730
Kelvin Li4e325f72016-10-25 12:50:55 +00007731 return OMPTeamsDistributeSimdDirective::Create(
7732 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7733}
7734
Kelvin Li579e41c2016-11-30 23:51:03 +00007735StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective(
7736 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007737 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li579e41c2016-11-30 23:51:03 +00007738 if (!AStmt)
7739 return StmtError();
7740
Alexey Bataeve3727102018-04-18 15:57:46 +00007741 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li579e41c2016-11-30 23:51:03 +00007742 // 1.2.2 OpenMP Language Terminology
7743 // Structured block - An executable statement with a single entry at the
7744 // top and a single exit at the bottom.
7745 // The point of exit cannot be a branch out of the structured block.
7746 // longjmp() and throw() must not violate the entry/exit criteria.
7747 CS->getCapturedDecl()->setNothrow();
7748
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00007749 for (int ThisCaptureLevel =
7750 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd);
7751 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7752 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7753 // 1.2.2 OpenMP Language Terminology
7754 // Structured block - An executable statement with a single entry at the
7755 // top and a single exit at the bottom.
7756 // The point of exit cannot be a branch out of the structured block.
7757 // longjmp() and throw() must not violate the entry/exit criteria.
7758 CS->getCapturedDecl()->setNothrow();
7759 }
7760
Kelvin Li579e41c2016-11-30 23:51:03 +00007761 OMPLoopDirective::HelperExprs B;
7762 // In presence of clause 'collapse' with number of loops, it will
7763 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007764 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li579e41c2016-11-30 23:51:03 +00007765 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00007766 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li579e41c2016-11-30 23:51:03 +00007767 VarsWithImplicitDSA, B);
7768
7769 if (NestedLoopCount == 0)
7770 return StmtError();
7771
7772 assert((CurContext->isDependentContext() || B.builtAll()) &&
7773 "omp for loop exprs were not built");
7774
7775 if (!CurContext->isDependentContext()) {
7776 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007777 for (OMPClause *C : Clauses) {
Kelvin Li579e41c2016-11-30 23:51:03 +00007778 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7779 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7780 B.NumIterations, *this, CurScope,
7781 DSAStack))
7782 return StmtError();
7783 }
7784 }
7785
7786 if (checkSimdlenSafelenSpecified(*this, Clauses))
7787 return StmtError();
7788
Reid Kleckner87a31802018-03-12 21:43:02 +00007789 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007790
7791 DSAStack->setParentTeamsRegionLoc(StartLoc);
7792
Kelvin Li579e41c2016-11-30 23:51:03 +00007793 return OMPTeamsDistributeParallelForSimdDirective::Create(
7794 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7795}
7796
Kelvin Li7ade93f2016-12-09 03:24:30 +00007797StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective(
7798 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007799 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li7ade93f2016-12-09 03:24:30 +00007800 if (!AStmt)
7801 return StmtError();
7802
Alexey Bataeve3727102018-04-18 15:57:46 +00007803 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li7ade93f2016-12-09 03:24:30 +00007804 // 1.2.2 OpenMP Language Terminology
7805 // Structured block - An executable statement with a single entry at the
7806 // top and a single exit at the bottom.
7807 // The point of exit cannot be a branch out of the structured block.
7808 // longjmp() and throw() must not violate the entry/exit criteria.
7809 CS->getCapturedDecl()->setNothrow();
7810
Carlo Bertolli62fae152017-11-20 20:46:39 +00007811 for (int ThisCaptureLevel =
7812 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for);
7813 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7814 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7815 // 1.2.2 OpenMP Language Terminology
7816 // Structured block - An executable statement with a single entry at the
7817 // top and a single exit at the bottom.
7818 // The point of exit cannot be a branch out of the structured block.
7819 // longjmp() and throw() must not violate the entry/exit criteria.
7820 CS->getCapturedDecl()->setNothrow();
7821 }
7822
Kelvin Li7ade93f2016-12-09 03:24:30 +00007823 OMPLoopDirective::HelperExprs B;
7824 // In presence of clause 'collapse' with number of loops, it will
7825 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007826 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li7ade93f2016-12-09 03:24:30 +00007827 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
Carlo Bertolli62fae152017-11-20 20:46:39 +00007828 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li7ade93f2016-12-09 03:24:30 +00007829 VarsWithImplicitDSA, B);
7830
7831 if (NestedLoopCount == 0)
7832 return StmtError();
7833
7834 assert((CurContext->isDependentContext() || B.builtAll()) &&
7835 "omp for loop exprs were not built");
7836
Reid Kleckner87a31802018-03-12 21:43:02 +00007837 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007838
7839 DSAStack->setParentTeamsRegionLoc(StartLoc);
7840
Kelvin Li7ade93f2016-12-09 03:24:30 +00007841 return OMPTeamsDistributeParallelForDirective::Create(
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00007842 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7843 DSAStack->isCancelRegion());
Kelvin Li7ade93f2016-12-09 03:24:30 +00007844}
7845
Kelvin Libf594a52016-12-17 05:48:59 +00007846StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses,
7847 Stmt *AStmt,
7848 SourceLocation StartLoc,
7849 SourceLocation EndLoc) {
7850 if (!AStmt)
7851 return StmtError();
7852
Alexey Bataeve3727102018-04-18 15:57:46 +00007853 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Libf594a52016-12-17 05:48:59 +00007854 // 1.2.2 OpenMP Language Terminology
7855 // Structured block - An executable statement with a single entry at the
7856 // top and a single exit at the bottom.
7857 // The point of exit cannot be a branch out of the structured block.
7858 // longjmp() and throw() must not violate the entry/exit criteria.
7859 CS->getCapturedDecl()->setNothrow();
7860
Alexey Bataevf9fc42e2017-11-22 14:25:55 +00007861 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams);
7862 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7863 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7864 // 1.2.2 OpenMP Language Terminology
7865 // Structured block - An executable statement with a single entry at the
7866 // top and a single exit at the bottom.
7867 // The point of exit cannot be a branch out of the structured block.
7868 // longjmp() and throw() must not violate the entry/exit criteria.
7869 CS->getCapturedDecl()->setNothrow();
7870 }
Reid Kleckner87a31802018-03-12 21:43:02 +00007871 setFunctionHasBranchProtectedScope();
Kelvin Libf594a52016-12-17 05:48:59 +00007872
7873 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses,
7874 AStmt);
7875}
7876
Kelvin Li83c451e2016-12-25 04:52:54 +00007877StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective(
7878 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007879 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li83c451e2016-12-25 04:52:54 +00007880 if (!AStmt)
7881 return StmtError();
7882
Alexey Bataeve3727102018-04-18 15:57:46 +00007883 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li83c451e2016-12-25 04:52:54 +00007884 // 1.2.2 OpenMP Language Terminology
7885 // Structured block - An executable statement with a single entry at the
7886 // top and a single exit at the bottom.
7887 // The point of exit cannot be a branch out of the structured block.
7888 // longjmp() and throw() must not violate the entry/exit criteria.
7889 CS->getCapturedDecl()->setNothrow();
Alexey Bataevdfa430f2017-12-08 15:03:50 +00007890 for (int ThisCaptureLevel =
7891 getOpenMPCaptureLevels(OMPD_target_teams_distribute);
7892 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7893 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7894 // 1.2.2 OpenMP Language Terminology
7895 // Structured block - An executable statement with a single entry at the
7896 // top and a single exit at the bottom.
7897 // The point of exit cannot be a branch out of the structured block.
7898 // longjmp() and throw() must not violate the entry/exit criteria.
7899 CS->getCapturedDecl()->setNothrow();
7900 }
Kelvin Li83c451e2016-12-25 04:52:54 +00007901
7902 OMPLoopDirective::HelperExprs B;
7903 // In presence of clause 'collapse' with number of loops, it will
7904 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007905 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataevdfa430f2017-12-08 15:03:50 +00007906 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses),
7907 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li83c451e2016-12-25 04:52:54 +00007908 VarsWithImplicitDSA, B);
7909 if (NestedLoopCount == 0)
7910 return StmtError();
7911
7912 assert((CurContext->isDependentContext() || B.builtAll()) &&
7913 "omp target teams distribute loop exprs were not built");
7914
Reid Kleckner87a31802018-03-12 21:43:02 +00007915 setFunctionHasBranchProtectedScope();
Kelvin Li83c451e2016-12-25 04:52:54 +00007916 return OMPTargetTeamsDistributeDirective::Create(
7917 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7918}
7919
Kelvin Li80e8f562016-12-29 22:16:30 +00007920StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective(
7921 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007922 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li80e8f562016-12-29 22:16:30 +00007923 if (!AStmt)
7924 return StmtError();
7925
Alexey Bataeve3727102018-04-18 15:57:46 +00007926 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li80e8f562016-12-29 22:16:30 +00007927 // 1.2.2 OpenMP Language Terminology
7928 // Structured block - An executable statement with a single entry at the
7929 // top and a single exit at the bottom.
7930 // The point of exit cannot be a branch out of the structured block.
7931 // longjmp() and throw() must not violate the entry/exit criteria.
7932 CS->getCapturedDecl()->setNothrow();
Carlo Bertolli52978c32018-01-03 21:12:44 +00007933 for (int ThisCaptureLevel =
7934 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for);
7935 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7936 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7937 // 1.2.2 OpenMP Language Terminology
7938 // Structured block - An executable statement with a single entry at the
7939 // top and a single exit at the bottom.
7940 // The point of exit cannot be a branch out of the structured block.
7941 // longjmp() and throw() must not violate the entry/exit criteria.
7942 CS->getCapturedDecl()->setNothrow();
7943 }
7944
Kelvin Li80e8f562016-12-29 22:16:30 +00007945 OMPLoopDirective::HelperExprs B;
7946 // In presence of clause 'collapse' with number of loops, it will
7947 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007948 unsigned NestedLoopCount = checkOpenMPLoop(
Carlo Bertolli52978c32018-01-03 21:12:44 +00007949 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
7950 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li80e8f562016-12-29 22:16:30 +00007951 VarsWithImplicitDSA, B);
7952 if (NestedLoopCount == 0)
7953 return StmtError();
7954
7955 assert((CurContext->isDependentContext() || B.builtAll()) &&
7956 "omp target teams distribute parallel for loop exprs were not built");
7957
Alexey Bataev647dd842018-01-15 20:59:40 +00007958 if (!CurContext->isDependentContext()) {
7959 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007960 for (OMPClause *C : Clauses) {
Alexey Bataev647dd842018-01-15 20:59:40 +00007961 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7962 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7963 B.NumIterations, *this, CurScope,
7964 DSAStack))
7965 return StmtError();
7966 }
7967 }
7968
Reid Kleckner87a31802018-03-12 21:43:02 +00007969 setFunctionHasBranchProtectedScope();
Kelvin Li80e8f562016-12-29 22:16:30 +00007970 return OMPTargetTeamsDistributeParallelForDirective::Create(
Alexey Bataev16e79882017-11-22 21:12:03 +00007971 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7972 DSAStack->isCancelRegion());
Kelvin Li80e8f562016-12-29 22:16:30 +00007973}
7974
Kelvin Li1851df52017-01-03 05:23:48 +00007975StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
7976 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007977 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li1851df52017-01-03 05:23:48 +00007978 if (!AStmt)
7979 return StmtError();
7980
Alexey Bataeve3727102018-04-18 15:57:46 +00007981 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li1851df52017-01-03 05:23:48 +00007982 // 1.2.2 OpenMP Language Terminology
7983 // Structured block - An executable statement with a single entry at the
7984 // top and a single exit at the bottom.
7985 // The point of exit cannot be a branch out of the structured block.
7986 // longjmp() and throw() must not violate the entry/exit criteria.
7987 CS->getCapturedDecl()->setNothrow();
Alexey Bataev647dd842018-01-15 20:59:40 +00007988 for (int ThisCaptureLevel = getOpenMPCaptureLevels(
7989 OMPD_target_teams_distribute_parallel_for_simd);
7990 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7991 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7992 // 1.2.2 OpenMP Language Terminology
7993 // Structured block - An executable statement with a single entry at the
7994 // top and a single exit at the bottom.
7995 // The point of exit cannot be a branch out of the structured block.
7996 // longjmp() and throw() must not violate the entry/exit criteria.
7997 CS->getCapturedDecl()->setNothrow();
7998 }
Kelvin Li1851df52017-01-03 05:23:48 +00007999
8000 OMPLoopDirective::HelperExprs B;
8001 // In presence of clause 'collapse' with number of loops, it will
8002 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00008003 unsigned NestedLoopCount =
8004 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd,
Alexey Bataev647dd842018-01-15 20:59:40 +00008005 getCollapseNumberExpr(Clauses),
8006 nullptr /*ordered not a clause on distribute*/, CS, *this,
8007 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li1851df52017-01-03 05:23:48 +00008008 if (NestedLoopCount == 0)
8009 return StmtError();
8010
8011 assert((CurContext->isDependentContext() || B.builtAll()) &&
8012 "omp target teams distribute parallel for simd loop exprs were not "
8013 "built");
8014
8015 if (!CurContext->isDependentContext()) {
8016 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00008017 for (OMPClause *C : Clauses) {
Kelvin Li1851df52017-01-03 05:23:48 +00008018 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8019 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8020 B.NumIterations, *this, CurScope,
8021 DSAStack))
8022 return StmtError();
8023 }
8024 }
8025
Alexey Bataev438388c2017-11-22 18:34:02 +00008026 if (checkSimdlenSafelenSpecified(*this, Clauses))
8027 return StmtError();
8028
Reid Kleckner87a31802018-03-12 21:43:02 +00008029 setFunctionHasBranchProtectedScope();
Kelvin Li1851df52017-01-03 05:23:48 +00008030 return OMPTargetTeamsDistributeParallelForSimdDirective::Create(
8031 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8032}
8033
Kelvin Lida681182017-01-10 18:08:18 +00008034StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective(
8035 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00008036 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Lida681182017-01-10 18:08:18 +00008037 if (!AStmt)
8038 return StmtError();
8039
8040 auto *CS = cast<CapturedStmt>(AStmt);
8041 // 1.2.2 OpenMP Language Terminology
8042 // Structured block - An executable statement with a single entry at the
8043 // top and a single exit at the bottom.
8044 // The point of exit cannot be a branch out of the structured block.
8045 // longjmp() and throw() must not violate the entry/exit criteria.
8046 CS->getCapturedDecl()->setNothrow();
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00008047 for (int ThisCaptureLevel =
8048 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd);
8049 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8050 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8051 // 1.2.2 OpenMP Language Terminology
8052 // Structured block - An executable statement with a single entry at the
8053 // top and a single exit at the bottom.
8054 // The point of exit cannot be a branch out of the structured block.
8055 // longjmp() and throw() must not violate the entry/exit criteria.
8056 CS->getCapturedDecl()->setNothrow();
8057 }
Kelvin Lida681182017-01-10 18:08:18 +00008058
8059 OMPLoopDirective::HelperExprs B;
8060 // In presence of clause 'collapse' with number of loops, it will
8061 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00008062 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Lida681182017-01-10 18:08:18 +00008063 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00008064 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Lida681182017-01-10 18:08:18 +00008065 VarsWithImplicitDSA, B);
8066 if (NestedLoopCount == 0)
8067 return StmtError();
8068
8069 assert((CurContext->isDependentContext() || B.builtAll()) &&
8070 "omp target teams distribute simd loop exprs were not built");
8071
Alexey Bataev438388c2017-11-22 18:34:02 +00008072 if (!CurContext->isDependentContext()) {
8073 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00008074 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00008075 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8076 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8077 B.NumIterations, *this, CurScope,
8078 DSAStack))
8079 return StmtError();
8080 }
8081 }
8082
8083 if (checkSimdlenSafelenSpecified(*this, Clauses))
8084 return StmtError();
8085
Reid Kleckner87a31802018-03-12 21:43:02 +00008086 setFunctionHasBranchProtectedScope();
Kelvin Lida681182017-01-10 18:08:18 +00008087 return OMPTargetTeamsDistributeSimdDirective::Create(
8088 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8089}
8090
Alexey Bataeved09d242014-05-28 05:53:51 +00008091OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008092 SourceLocation StartLoc,
8093 SourceLocation LParenLoc,
8094 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008095 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008096 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00008097 case OMPC_final:
8098 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
8099 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00008100 case OMPC_num_threads:
8101 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
8102 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00008103 case OMPC_safelen:
8104 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
8105 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00008106 case OMPC_simdlen:
8107 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
8108 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00008109 case OMPC_collapse:
8110 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
8111 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00008112 case OMPC_ordered:
8113 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
8114 break;
Michael Wonge710d542015-08-07 16:16:36 +00008115 case OMPC_device:
8116 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
8117 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00008118 case OMPC_num_teams:
8119 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
8120 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008121 case OMPC_thread_limit:
8122 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
8123 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00008124 case OMPC_priority:
8125 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
8126 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008127 case OMPC_grainsize:
8128 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
8129 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00008130 case OMPC_num_tasks:
8131 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
8132 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00008133 case OMPC_hint:
8134 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
8135 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008136 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008137 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008138 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008139 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008140 case OMPC_private:
8141 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00008142 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008143 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00008144 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008145 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008146 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00008147 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008148 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008149 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008150 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00008151 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008152 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008153 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008154 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00008155 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008156 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008157 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008158 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008159 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008160 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008161 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00008162 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008163 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008164 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00008165 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008166 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008167 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008168 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008169 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00008170 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00008171 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00008172 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00008173 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00008174 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00008175 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00008176 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00008177 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00008178 case OMPC_atomic_default_mem_order:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008179 llvm_unreachable("Clause is not allowed.");
8180 }
8181 return Res;
8182}
8183
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008184// An OpenMP directive such as 'target parallel' has two captured regions:
8185// for the 'target' and 'parallel' respectively. This function returns
8186// the region in which to capture expressions associated with a clause.
8187// A return value of OMPD_unknown signifies that the expression should not
8188// be captured.
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008189static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
8190 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
8191 OpenMPDirectiveKind NameModifier = OMPD_unknown) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008192 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008193 switch (CKind) {
8194 case OMPC_if:
8195 switch (DKind) {
8196 case OMPD_target_parallel:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008197 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00008198 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008199 // If this clause applies to the nested 'parallel' region, capture within
8200 // the 'target' region, otherwise do not capture.
8201 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
8202 CaptureRegion = OMPD_target;
8203 break;
Carlo Bertolli52978c32018-01-03 21:12:44 +00008204 case OMPD_target_teams_distribute_parallel_for:
8205 case OMPD_target_teams_distribute_parallel_for_simd:
8206 // If this clause applies to the nested 'parallel' region, capture within
8207 // the 'teams' region, otherwise do not capture.
8208 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
8209 CaptureRegion = OMPD_teams;
8210 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +00008211 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008212 case OMPD_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008213 CaptureRegion = OMPD_teams;
8214 break;
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008215 case OMPD_target_update:
Alexey Bataevfab20e42017-12-27 18:49:38 +00008216 case OMPD_target_enter_data:
8217 case OMPD_target_exit_data:
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008218 CaptureRegion = OMPD_task;
8219 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008220 case OMPD_cancel:
8221 case OMPD_parallel:
8222 case OMPD_parallel_sections:
8223 case OMPD_parallel_for:
8224 case OMPD_parallel_for_simd:
8225 case OMPD_target:
8226 case OMPD_target_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008227 case OMPD_target_teams:
8228 case OMPD_target_teams_distribute:
8229 case OMPD_target_teams_distribute_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008230 case OMPD_distribute_parallel_for:
8231 case OMPD_distribute_parallel_for_simd:
8232 case OMPD_task:
8233 case OMPD_taskloop:
8234 case OMPD_taskloop_simd:
8235 case OMPD_target_data:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008236 // Do not capture if-clause expressions.
8237 break;
8238 case OMPD_threadprivate:
8239 case OMPD_taskyield:
8240 case OMPD_barrier:
8241 case OMPD_taskwait:
8242 case OMPD_cancellation_point:
8243 case OMPD_flush:
8244 case OMPD_declare_reduction:
8245 case OMPD_declare_simd:
8246 case OMPD_declare_target:
8247 case OMPD_end_declare_target:
8248 case OMPD_teams:
8249 case OMPD_simd:
8250 case OMPD_for:
8251 case OMPD_for_simd:
8252 case OMPD_sections:
8253 case OMPD_section:
8254 case OMPD_single:
8255 case OMPD_master:
8256 case OMPD_critical:
8257 case OMPD_taskgroup:
8258 case OMPD_distribute:
8259 case OMPD_ordered:
8260 case OMPD_atomic:
8261 case OMPD_distribute_simd:
8262 case OMPD_teams_distribute:
8263 case OMPD_teams_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008264 case OMPD_requires:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008265 llvm_unreachable("Unexpected OpenMP directive with if-clause");
8266 case OMPD_unknown:
8267 llvm_unreachable("Unknown OpenMP directive");
8268 }
8269 break;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008270 case OMPC_num_threads:
8271 switch (DKind) {
8272 case OMPD_target_parallel:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008273 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00008274 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008275 CaptureRegion = OMPD_target;
8276 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +00008277 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008278 case OMPD_teams_distribute_parallel_for_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008279 case OMPD_target_teams_distribute_parallel_for:
8280 case OMPD_target_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008281 CaptureRegion = OMPD_teams;
8282 break;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008283 case OMPD_parallel:
8284 case OMPD_parallel_sections:
8285 case OMPD_parallel_for:
8286 case OMPD_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008287 case OMPD_distribute_parallel_for:
8288 case OMPD_distribute_parallel_for_simd:
8289 // Do not capture num_threads-clause expressions.
8290 break;
8291 case OMPD_target_data:
8292 case OMPD_target_enter_data:
8293 case OMPD_target_exit_data:
8294 case OMPD_target_update:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008295 case OMPD_target:
8296 case OMPD_target_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008297 case OMPD_target_teams:
8298 case OMPD_target_teams_distribute:
8299 case OMPD_target_teams_distribute_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008300 case OMPD_cancel:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008301 case OMPD_task:
8302 case OMPD_taskloop:
8303 case OMPD_taskloop_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008304 case OMPD_threadprivate:
8305 case OMPD_taskyield:
8306 case OMPD_barrier:
8307 case OMPD_taskwait:
8308 case OMPD_cancellation_point:
8309 case OMPD_flush:
8310 case OMPD_declare_reduction:
8311 case OMPD_declare_simd:
8312 case OMPD_declare_target:
8313 case OMPD_end_declare_target:
8314 case OMPD_teams:
8315 case OMPD_simd:
8316 case OMPD_for:
8317 case OMPD_for_simd:
8318 case OMPD_sections:
8319 case OMPD_section:
8320 case OMPD_single:
8321 case OMPD_master:
8322 case OMPD_critical:
8323 case OMPD_taskgroup:
8324 case OMPD_distribute:
8325 case OMPD_ordered:
8326 case OMPD_atomic:
8327 case OMPD_distribute_simd:
8328 case OMPD_teams_distribute:
8329 case OMPD_teams_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008330 case OMPD_requires:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008331 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause");
8332 case OMPD_unknown:
8333 llvm_unreachable("Unknown OpenMP directive");
8334 }
8335 break;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008336 case OMPC_num_teams:
8337 switch (DKind) {
8338 case OMPD_target_teams:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008339 case OMPD_target_teams_distribute:
8340 case OMPD_target_teams_distribute_simd:
8341 case OMPD_target_teams_distribute_parallel_for:
8342 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008343 CaptureRegion = OMPD_target;
8344 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008345 case OMPD_teams_distribute_parallel_for:
8346 case OMPD_teams_distribute_parallel_for_simd:
8347 case OMPD_teams:
8348 case OMPD_teams_distribute:
8349 case OMPD_teams_distribute_simd:
8350 // Do not capture num_teams-clause expressions.
8351 break;
8352 case OMPD_distribute_parallel_for:
8353 case OMPD_distribute_parallel_for_simd:
8354 case OMPD_task:
8355 case OMPD_taskloop:
8356 case OMPD_taskloop_simd:
8357 case OMPD_target_data:
8358 case OMPD_target_enter_data:
8359 case OMPD_target_exit_data:
8360 case OMPD_target_update:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008361 case OMPD_cancel:
8362 case OMPD_parallel:
8363 case OMPD_parallel_sections:
8364 case OMPD_parallel_for:
8365 case OMPD_parallel_for_simd:
8366 case OMPD_target:
8367 case OMPD_target_simd:
8368 case OMPD_target_parallel:
8369 case OMPD_target_parallel_for:
8370 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008371 case OMPD_threadprivate:
8372 case OMPD_taskyield:
8373 case OMPD_barrier:
8374 case OMPD_taskwait:
8375 case OMPD_cancellation_point:
8376 case OMPD_flush:
8377 case OMPD_declare_reduction:
8378 case OMPD_declare_simd:
8379 case OMPD_declare_target:
8380 case OMPD_end_declare_target:
8381 case OMPD_simd:
8382 case OMPD_for:
8383 case OMPD_for_simd:
8384 case OMPD_sections:
8385 case OMPD_section:
8386 case OMPD_single:
8387 case OMPD_master:
8388 case OMPD_critical:
8389 case OMPD_taskgroup:
8390 case OMPD_distribute:
8391 case OMPD_ordered:
8392 case OMPD_atomic:
8393 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008394 case OMPD_requires:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008395 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
8396 case OMPD_unknown:
8397 llvm_unreachable("Unknown OpenMP directive");
8398 }
8399 break;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008400 case OMPC_thread_limit:
8401 switch (DKind) {
8402 case OMPD_target_teams:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008403 case OMPD_target_teams_distribute:
8404 case OMPD_target_teams_distribute_simd:
8405 case OMPD_target_teams_distribute_parallel_for:
8406 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008407 CaptureRegion = OMPD_target;
8408 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008409 case OMPD_teams_distribute_parallel_for:
8410 case OMPD_teams_distribute_parallel_for_simd:
8411 case OMPD_teams:
8412 case OMPD_teams_distribute:
8413 case OMPD_teams_distribute_simd:
8414 // Do not capture thread_limit-clause expressions.
8415 break;
8416 case OMPD_distribute_parallel_for:
8417 case OMPD_distribute_parallel_for_simd:
8418 case OMPD_task:
8419 case OMPD_taskloop:
8420 case OMPD_taskloop_simd:
8421 case OMPD_target_data:
8422 case OMPD_target_enter_data:
8423 case OMPD_target_exit_data:
8424 case OMPD_target_update:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008425 case OMPD_cancel:
8426 case OMPD_parallel:
8427 case OMPD_parallel_sections:
8428 case OMPD_parallel_for:
8429 case OMPD_parallel_for_simd:
8430 case OMPD_target:
8431 case OMPD_target_simd:
8432 case OMPD_target_parallel:
8433 case OMPD_target_parallel_for:
8434 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008435 case OMPD_threadprivate:
8436 case OMPD_taskyield:
8437 case OMPD_barrier:
8438 case OMPD_taskwait:
8439 case OMPD_cancellation_point:
8440 case OMPD_flush:
8441 case OMPD_declare_reduction:
8442 case OMPD_declare_simd:
8443 case OMPD_declare_target:
8444 case OMPD_end_declare_target:
8445 case OMPD_simd:
8446 case OMPD_for:
8447 case OMPD_for_simd:
8448 case OMPD_sections:
8449 case OMPD_section:
8450 case OMPD_single:
8451 case OMPD_master:
8452 case OMPD_critical:
8453 case OMPD_taskgroup:
8454 case OMPD_distribute:
8455 case OMPD_ordered:
8456 case OMPD_atomic:
8457 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008458 case OMPD_requires:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008459 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause");
8460 case OMPD_unknown:
8461 llvm_unreachable("Unknown OpenMP directive");
8462 }
8463 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008464 case OMPC_schedule:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008465 switch (DKind) {
Alexey Bataev2ba67042017-11-28 21:11:44 +00008466 case OMPD_parallel_for:
8467 case OMPD_parallel_for_simd:
Alexey Bataev7f96c372017-11-22 17:19:31 +00008468 case OMPD_distribute_parallel_for:
Alexey Bataev974acd62017-11-27 19:38:52 +00008469 case OMPD_distribute_parallel_for_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008470 case OMPD_teams_distribute_parallel_for:
8471 case OMPD_teams_distribute_parallel_for_simd:
8472 case OMPD_target_parallel_for:
8473 case OMPD_target_parallel_for_simd:
8474 case OMPD_target_teams_distribute_parallel_for:
8475 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataev7f96c372017-11-22 17:19:31 +00008476 CaptureRegion = OMPD_parallel;
8477 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008478 case OMPD_for:
8479 case OMPD_for_simd:
8480 // Do not capture schedule-clause expressions.
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008481 break;
8482 case OMPD_task:
8483 case OMPD_taskloop:
8484 case OMPD_taskloop_simd:
8485 case OMPD_target_data:
8486 case OMPD_target_enter_data:
8487 case OMPD_target_exit_data:
8488 case OMPD_target_update:
8489 case OMPD_teams:
8490 case OMPD_teams_distribute:
8491 case OMPD_teams_distribute_simd:
8492 case OMPD_target_teams_distribute:
8493 case OMPD_target_teams_distribute_simd:
8494 case OMPD_target:
8495 case OMPD_target_simd:
8496 case OMPD_target_parallel:
8497 case OMPD_cancel:
8498 case OMPD_parallel:
8499 case OMPD_parallel_sections:
8500 case OMPD_threadprivate:
8501 case OMPD_taskyield:
8502 case OMPD_barrier:
8503 case OMPD_taskwait:
8504 case OMPD_cancellation_point:
8505 case OMPD_flush:
8506 case OMPD_declare_reduction:
8507 case OMPD_declare_simd:
8508 case OMPD_declare_target:
8509 case OMPD_end_declare_target:
8510 case OMPD_simd:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008511 case OMPD_sections:
8512 case OMPD_section:
8513 case OMPD_single:
8514 case OMPD_master:
8515 case OMPD_critical:
8516 case OMPD_taskgroup:
8517 case OMPD_distribute:
8518 case OMPD_ordered:
8519 case OMPD_atomic:
8520 case OMPD_distribute_simd:
8521 case OMPD_target_teams:
Kelvin Li1408f912018-09-26 04:28:39 +00008522 case OMPD_requires:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008523 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
8524 case OMPD_unknown:
8525 llvm_unreachable("Unknown OpenMP directive");
8526 }
8527 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008528 case OMPC_dist_schedule:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008529 switch (DKind) {
8530 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008531 case OMPD_teams_distribute_parallel_for_simd:
8532 case OMPD_teams_distribute:
8533 case OMPD_teams_distribute_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008534 case OMPD_target_teams_distribute_parallel_for:
8535 case OMPD_target_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008536 case OMPD_target_teams_distribute:
8537 case OMPD_target_teams_distribute_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008538 CaptureRegion = OMPD_teams;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008539 break;
8540 case OMPD_distribute_parallel_for:
8541 case OMPD_distribute_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008542 case OMPD_distribute:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008543 case OMPD_distribute_simd:
8544 // Do not capture thread_limit-clause expressions.
8545 break;
8546 case OMPD_parallel_for:
8547 case OMPD_parallel_for_simd:
8548 case OMPD_target_parallel_for_simd:
8549 case OMPD_target_parallel_for:
8550 case OMPD_task:
8551 case OMPD_taskloop:
8552 case OMPD_taskloop_simd:
8553 case OMPD_target_data:
8554 case OMPD_target_enter_data:
8555 case OMPD_target_exit_data:
8556 case OMPD_target_update:
8557 case OMPD_teams:
8558 case OMPD_target:
8559 case OMPD_target_simd:
8560 case OMPD_target_parallel:
8561 case OMPD_cancel:
8562 case OMPD_parallel:
8563 case OMPD_parallel_sections:
8564 case OMPD_threadprivate:
8565 case OMPD_taskyield:
8566 case OMPD_barrier:
8567 case OMPD_taskwait:
8568 case OMPD_cancellation_point:
8569 case OMPD_flush:
8570 case OMPD_declare_reduction:
8571 case OMPD_declare_simd:
8572 case OMPD_declare_target:
8573 case OMPD_end_declare_target:
8574 case OMPD_simd:
8575 case OMPD_for:
8576 case OMPD_for_simd:
8577 case OMPD_sections:
8578 case OMPD_section:
8579 case OMPD_single:
8580 case OMPD_master:
8581 case OMPD_critical:
8582 case OMPD_taskgroup:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008583 case OMPD_ordered:
8584 case OMPD_atomic:
8585 case OMPD_target_teams:
Kelvin Li1408f912018-09-26 04:28:39 +00008586 case OMPD_requires:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008587 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
8588 case OMPD_unknown:
8589 llvm_unreachable("Unknown OpenMP directive");
8590 }
8591 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008592 case OMPC_device:
8593 switch (DKind) {
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008594 case OMPD_target_update:
Alexey Bataevfab20e42017-12-27 18:49:38 +00008595 case OMPD_target_enter_data:
8596 case OMPD_target_exit_data:
Alexey Bataev8451efa2018-01-15 19:06:12 +00008597 case OMPD_target:
Alexey Bataevf41c88f2018-01-16 15:05:16 +00008598 case OMPD_target_simd:
Alexey Bataev0c869ef2018-01-16 15:57:07 +00008599 case OMPD_target_teams:
Alexey Bataev54d5c7d2018-01-16 16:27:49 +00008600 case OMPD_target_parallel:
Alexey Bataev79df7562018-01-16 16:46:46 +00008601 case OMPD_target_teams_distribute:
Alexey Bataev8d16a432018-01-16 17:22:50 +00008602 case OMPD_target_teams_distribute_simd:
Alexey Bataev8ed895512018-01-16 17:41:04 +00008603 case OMPD_target_parallel_for:
Alexey Bataevd60d1ba2018-01-16 17:55:15 +00008604 case OMPD_target_parallel_for_simd:
Alexey Bataev9f9fb0b2018-01-16 19:02:33 +00008605 case OMPD_target_teams_distribute_parallel_for:
Alexey Bataev9350fc32018-01-16 19:18:24 +00008606 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008607 CaptureRegion = OMPD_task;
8608 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008609 case OMPD_target_data:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008610 // Do not capture device-clause expressions.
8611 break;
8612 case OMPD_teams_distribute_parallel_for:
8613 case OMPD_teams_distribute_parallel_for_simd:
8614 case OMPD_teams:
8615 case OMPD_teams_distribute:
8616 case OMPD_teams_distribute_simd:
8617 case OMPD_distribute_parallel_for:
8618 case OMPD_distribute_parallel_for_simd:
8619 case OMPD_task:
8620 case OMPD_taskloop:
8621 case OMPD_taskloop_simd:
8622 case OMPD_cancel:
8623 case OMPD_parallel:
8624 case OMPD_parallel_sections:
8625 case OMPD_parallel_for:
8626 case OMPD_parallel_for_simd:
8627 case OMPD_threadprivate:
8628 case OMPD_taskyield:
8629 case OMPD_barrier:
8630 case OMPD_taskwait:
8631 case OMPD_cancellation_point:
8632 case OMPD_flush:
8633 case OMPD_declare_reduction:
8634 case OMPD_declare_simd:
8635 case OMPD_declare_target:
8636 case OMPD_end_declare_target:
8637 case OMPD_simd:
8638 case OMPD_for:
8639 case OMPD_for_simd:
8640 case OMPD_sections:
8641 case OMPD_section:
8642 case OMPD_single:
8643 case OMPD_master:
8644 case OMPD_critical:
8645 case OMPD_taskgroup:
8646 case OMPD_distribute:
8647 case OMPD_ordered:
8648 case OMPD_atomic:
8649 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008650 case OMPD_requires:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008651 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
8652 case OMPD_unknown:
8653 llvm_unreachable("Unknown OpenMP directive");
8654 }
8655 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008656 case OMPC_firstprivate:
8657 case OMPC_lastprivate:
8658 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008659 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008660 case OMPC_in_reduction:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008661 case OMPC_linear:
8662 case OMPC_default:
8663 case OMPC_proc_bind:
8664 case OMPC_final:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008665 case OMPC_safelen:
8666 case OMPC_simdlen:
8667 case OMPC_collapse:
8668 case OMPC_private:
8669 case OMPC_shared:
8670 case OMPC_aligned:
8671 case OMPC_copyin:
8672 case OMPC_copyprivate:
8673 case OMPC_ordered:
8674 case OMPC_nowait:
8675 case OMPC_untied:
8676 case OMPC_mergeable:
8677 case OMPC_threadprivate:
8678 case OMPC_flush:
8679 case OMPC_read:
8680 case OMPC_write:
8681 case OMPC_update:
8682 case OMPC_capture:
8683 case OMPC_seq_cst:
8684 case OMPC_depend:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008685 case OMPC_threads:
8686 case OMPC_simd:
8687 case OMPC_map:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008688 case OMPC_priority:
8689 case OMPC_grainsize:
8690 case OMPC_nogroup:
8691 case OMPC_num_tasks:
8692 case OMPC_hint:
8693 case OMPC_defaultmap:
8694 case OMPC_unknown:
8695 case OMPC_uniform:
8696 case OMPC_to:
8697 case OMPC_from:
8698 case OMPC_use_device_ptr:
8699 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00008700 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00008701 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00008702 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00008703 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00008704 case OMPC_atomic_default_mem_order:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008705 llvm_unreachable("Unexpected OpenMP clause.");
8706 }
8707 return CaptureRegion;
8708}
8709
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008710OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
8711 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008712 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008713 SourceLocation NameModifierLoc,
8714 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008715 SourceLocation EndLoc) {
8716 Expr *ValExpr = Condition;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008717 Stmt *HelperValStmt = nullptr;
8718 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008719 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
8720 !Condition->isInstantiationDependent() &&
8721 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00008722 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008723 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008724 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008725
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008726 ValExpr = Val.get();
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008727
8728 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
8729 CaptureRegion =
8730 getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier);
Alexey Bataev2ba67042017-11-28 21:11:44 +00008731 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008732 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00008733 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008734 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8735 HelperValStmt = buildPreInits(Context, Captures);
8736 }
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008737 }
8738
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008739 return new (Context)
8740 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
8741 LParenLoc, NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008742}
8743
Alexey Bataev3778b602014-07-17 07:32:53 +00008744OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
8745 SourceLocation StartLoc,
8746 SourceLocation LParenLoc,
8747 SourceLocation EndLoc) {
8748 Expr *ValExpr = Condition;
8749 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
8750 !Condition->isInstantiationDependent() &&
8751 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00008752 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataev3778b602014-07-17 07:32:53 +00008753 if (Val.isInvalid())
8754 return nullptr;
8755
Richard Smith03a4aa32016-06-23 19:02:52 +00008756 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataev3778b602014-07-17 07:32:53 +00008757 }
8758
8759 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8760}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00008761ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
8762 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00008763 if (!Op)
8764 return ExprError();
8765
8766 class IntConvertDiagnoser : public ICEConvertDiagnoser {
8767 public:
8768 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00008769 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00008770 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
8771 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008772 return S.Diag(Loc, diag::err_omp_not_integral) << T;
8773 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008774 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
8775 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008776 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
8777 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008778 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
8779 QualType T,
8780 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008781 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
8782 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008783 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
8784 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008785 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00008786 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00008787 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008788 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
8789 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008790 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
8791 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008792 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
8793 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008794 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00008795 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00008796 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008797 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
8798 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008799 llvm_unreachable("conversion functions are permitted");
8800 }
8801 } ConvertDiagnoser;
8802 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
8803}
8804
Alexey Bataeve3727102018-04-18 15:57:46 +00008805static bool isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00008806 OpenMPClauseKind CKind,
8807 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008808 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
8809 !ValExpr->isInstantiationDependent()) {
8810 SourceLocation Loc = ValExpr->getExprLoc();
8811 ExprResult Value =
8812 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
8813 if (Value.isInvalid())
8814 return false;
8815
8816 ValExpr = Value.get();
8817 // The expression must evaluate to a non-negative integer value.
8818 llvm::APSInt Result;
8819 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00008820 Result.isSigned() &&
8821 !((!StrictlyPositive && Result.isNonNegative()) ||
8822 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008823 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00008824 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
8825 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008826 return false;
8827 }
8828 }
8829 return true;
8830}
8831
Alexey Bataev568a8332014-03-06 06:15:19 +00008832OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
8833 SourceLocation StartLoc,
8834 SourceLocation LParenLoc,
8835 SourceLocation EndLoc) {
8836 Expr *ValExpr = NumThreads;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008837 Stmt *HelperValStmt = nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00008838
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008839 // OpenMP [2.5, Restrictions]
8840 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +00008841 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
Alexey Bataeva0569352015-12-01 10:17:31 +00008842 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008843 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00008844
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008845 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +00008846 OpenMPDirectiveKind CaptureRegion =
8847 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads);
8848 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008849 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00008850 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008851 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8852 HelperValStmt = buildPreInits(Context, Captures);
8853 }
8854
8855 return new (Context) OMPNumThreadsClause(
8856 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00008857}
8858
Alexey Bataev62c87d22014-03-21 04:51:18 +00008859ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008860 OpenMPClauseKind CKind,
8861 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00008862 if (!E)
8863 return ExprError();
8864 if (E->isValueDependent() || E->isTypeDependent() ||
8865 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00008866 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00008867 llvm::APSInt Result;
8868 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
8869 if (ICE.isInvalid())
8870 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008871 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
8872 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00008873 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008874 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
8875 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00008876 return ExprError();
8877 }
Alexander Musman09184fe2014-09-30 05:29:28 +00008878 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
8879 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
8880 << E->getSourceRange();
8881 return ExprError();
8882 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008883 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
8884 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00008885 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008886 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00008887 return ICE;
8888}
8889
8890OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
8891 SourceLocation LParenLoc,
8892 SourceLocation EndLoc) {
8893 // OpenMP [2.8.1, simd construct, Description]
8894 // The parameter of the safelen clause must be a constant
8895 // positive integer expression.
8896 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
8897 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008898 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00008899 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00008900 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00008901}
8902
Alexey Bataev66b15b52015-08-21 11:14:16 +00008903OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
8904 SourceLocation LParenLoc,
8905 SourceLocation EndLoc) {
8906 // OpenMP [2.8.1, simd construct, Description]
8907 // The parameter of the simdlen clause must be a constant
8908 // positive integer expression.
8909 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
8910 if (Simdlen.isInvalid())
8911 return nullptr;
8912 return new (Context)
8913 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
8914}
8915
Alexander Musman64d33f12014-06-04 07:53:32 +00008916OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
8917 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00008918 SourceLocation LParenLoc,
8919 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00008920 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00008921 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00008922 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00008923 // The parameter of the collapse clause must be a constant
8924 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00008925 ExprResult NumForLoopsResult =
8926 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
8927 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00008928 return nullptr;
8929 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00008930 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00008931}
8932
Alexey Bataev10e775f2015-07-30 11:36:16 +00008933OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
8934 SourceLocation EndLoc,
8935 SourceLocation LParenLoc,
8936 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00008937 // OpenMP [2.7.1, loop construct, Description]
8938 // OpenMP [2.8.1, simd construct, Description]
8939 // OpenMP [2.9.6, distribute construct, Description]
8940 // The parameter of the ordered clause must be a constant
8941 // positive integer expression if any.
8942 if (NumForLoops && LParenLoc.isValid()) {
8943 ExprResult NumForLoopsResult =
8944 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
8945 if (NumForLoopsResult.isInvalid())
8946 return nullptr;
8947 NumForLoops = NumForLoopsResult.get();
Alexey Bataeve3727102018-04-18 15:57:46 +00008948 } else {
Alexey Bataev346265e2015-09-25 10:37:12 +00008949 NumForLoops = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +00008950 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00008951 auto *Clause = OMPOrderedClause::Create(
8952 Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0,
8953 StartLoc, LParenLoc, EndLoc);
8954 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause);
8955 return Clause;
Alexey Bataev10e775f2015-07-30 11:36:16 +00008956}
8957
Alexey Bataeved09d242014-05-28 05:53:51 +00008958OMPClause *Sema::ActOnOpenMPSimpleClause(
8959 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
8960 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008961 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008962 switch (Kind) {
8963 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00008964 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00008965 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
8966 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008967 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008968 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00008969 Res = ActOnOpenMPProcBindClause(
8970 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
8971 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008972 break;
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00008973 case OMPC_atomic_default_mem_order:
8974 Res = ActOnOpenMPAtomicDefaultMemOrderClause(
8975 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument),
8976 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
8977 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008978 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00008979 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00008980 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00008981 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008982 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00008983 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008984 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008985 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008986 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00008987 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00008988 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00008989 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008990 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008991 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00008992 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008993 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008994 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008995 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008996 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00008997 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008998 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008999 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009000 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00009001 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009002 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00009003 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00009004 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00009005 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009006 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009007 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00009008 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00009009 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009010 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00009011 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009012 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009013 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009014 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009015 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00009016 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00009017 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009018 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00009019 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009020 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009021 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009022 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00009023 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00009024 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00009025 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00009026 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00009027 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00009028 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009029 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009030 case OMPC_dynamic_allocators:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009031 llvm_unreachable("Clause is not allowed.");
9032 }
9033 return Res;
9034}
9035
Alexey Bataev6402bca2015-12-28 07:25:51 +00009036static std::string
9037getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
9038 ArrayRef<unsigned> Exclude = llvm::None) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009039 SmallString<256> Buffer;
9040 llvm::raw_svector_ostream Out(Buffer);
Alexey Bataev6402bca2015-12-28 07:25:51 +00009041 unsigned Bound = Last >= 2 ? Last - 2 : 0;
9042 unsigned Skipped = Exclude.size();
9043 auto S = Exclude.begin(), E = Exclude.end();
Alexey Bataeve3727102018-04-18 15:57:46 +00009044 for (unsigned I = First; I < Last; ++I) {
9045 if (std::find(S, E, I) != E) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00009046 --Skipped;
9047 continue;
9048 }
Alexey Bataeve3727102018-04-18 15:57:46 +00009049 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'";
9050 if (I == Bound - Skipped)
9051 Out << " or ";
9052 else if (I != Bound + 1 - Skipped)
9053 Out << ", ";
Alexey Bataev6402bca2015-12-28 07:25:51 +00009054 }
Alexey Bataeve3727102018-04-18 15:57:46 +00009055 return Out.str();
Alexey Bataev6402bca2015-12-28 07:25:51 +00009056}
9057
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009058OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
9059 SourceLocation KindKwLoc,
9060 SourceLocation StartLoc,
9061 SourceLocation LParenLoc,
9062 SourceLocation EndLoc) {
9063 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00009064 static_assert(OMPC_DEFAULT_unknown > 0,
9065 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009066 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009067 << getListOfPossibleValues(OMPC_default, /*First=*/0,
9068 /*Last=*/OMPC_DEFAULT_unknown)
9069 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009070 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009071 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00009072 switch (Kind) {
9073 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009074 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009075 break;
9076 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009077 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009078 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009079 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009080 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00009081 break;
9082 }
Alexey Bataeved09d242014-05-28 05:53:51 +00009083 return new (Context)
9084 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009085}
9086
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009087OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
9088 SourceLocation KindKwLoc,
9089 SourceLocation StartLoc,
9090 SourceLocation LParenLoc,
9091 SourceLocation EndLoc) {
9092 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009093 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009094 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
9095 /*Last=*/OMPC_PROC_BIND_unknown)
9096 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009097 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009098 }
Alexey Bataeved09d242014-05-28 05:53:51 +00009099 return new (Context)
9100 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009101}
9102
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009103OMPClause *Sema::ActOnOpenMPAtomicDefaultMemOrderClause(
9104 OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc,
9105 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
9106 if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) {
9107 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
9108 << getListOfPossibleValues(
9109 OMPC_atomic_default_mem_order, /*First=*/0,
9110 /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown)
9111 << getOpenMPClauseName(OMPC_atomic_default_mem_order);
9112 return nullptr;
9113 }
9114 return new (Context) OMPAtomicDefaultMemOrderClause(Kind, KindKwLoc, StartLoc,
9115 LParenLoc, EndLoc);
9116}
9117
Alexey Bataev56dafe82014-06-20 07:16:17 +00009118OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00009119 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00009120 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00009121 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00009122 SourceLocation EndLoc) {
9123 OMPClause *Res = nullptr;
9124 switch (Kind) {
9125 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00009126 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
9127 assert(Argument.size() == NumberOfElements &&
9128 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009129 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00009130 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
9131 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
9132 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
9133 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
9134 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009135 break;
9136 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00009137 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
9138 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
9139 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
9140 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009141 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00009142 case OMPC_dist_schedule:
9143 Res = ActOnOpenMPDistScheduleClause(
9144 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
9145 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
9146 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009147 case OMPC_defaultmap:
9148 enum { Modifier, DefaultmapKind };
9149 Res = ActOnOpenMPDefaultmapClause(
9150 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
9151 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
David Majnemer9d168222016-08-05 17:44:54 +00009152 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind],
9153 EndLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009154 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00009155 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009156 case OMPC_num_threads:
9157 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009158 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009159 case OMPC_collapse:
9160 case OMPC_default:
9161 case OMPC_proc_bind:
9162 case OMPC_private:
9163 case OMPC_firstprivate:
9164 case OMPC_lastprivate:
9165 case OMPC_shared:
9166 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00009167 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00009168 case OMPC_in_reduction:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009169 case OMPC_linear:
9170 case OMPC_aligned:
9171 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009172 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009173 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00009174 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009175 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009176 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009177 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00009178 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009179 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00009180 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00009181 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00009182 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009183 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009184 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00009185 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00009186 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009187 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00009188 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009189 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009190 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009191 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009192 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00009193 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00009194 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009195 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009196 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009197 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00009198 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00009199 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00009200 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00009201 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00009202 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00009203 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009204 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009205 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009206 case OMPC_atomic_default_mem_order:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009207 llvm_unreachable("Clause is not allowed.");
9208 }
9209 return Res;
9210}
9211
Alexey Bataev6402bca2015-12-28 07:25:51 +00009212static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
9213 OpenMPScheduleClauseModifier M2,
9214 SourceLocation M1Loc, SourceLocation M2Loc) {
9215 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
9216 SmallVector<unsigned, 2> Excluded;
9217 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
9218 Excluded.push_back(M2);
9219 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
9220 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
9221 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
9222 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
9223 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
9224 << getListOfPossibleValues(OMPC_schedule,
9225 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
9226 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
9227 Excluded)
9228 << getOpenMPClauseName(OMPC_schedule);
9229 return true;
9230 }
9231 return false;
9232}
9233
Alexey Bataev56dafe82014-06-20 07:16:17 +00009234OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00009235 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00009236 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00009237 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
9238 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
9239 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
9240 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
9241 return nullptr;
9242 // OpenMP, 2.7.1, Loop Construct, Restrictions
9243 // Either the monotonic modifier or the nonmonotonic modifier can be specified
9244 // but not both.
9245 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
9246 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
9247 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
9248 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
9249 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
9250 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
9251 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
9252 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
9253 return nullptr;
9254 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00009255 if (Kind == OMPC_SCHEDULE_unknown) {
9256 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00009257 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
9258 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
9259 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
9260 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
9261 Exclude);
9262 } else {
9263 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
9264 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009265 }
9266 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
9267 << Values << getOpenMPClauseName(OMPC_schedule);
9268 return nullptr;
9269 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00009270 // OpenMP, 2.7.1, Loop Construct, Restrictions
9271 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
9272 // schedule(guided).
9273 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
9274 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
9275 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
9276 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
9277 diag::err_omp_schedule_nonmonotonic_static);
9278 return nullptr;
9279 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00009280 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00009281 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00009282 if (ChunkSize) {
9283 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
9284 !ChunkSize->isInstantiationDependent() &&
9285 !ChunkSize->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00009286 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
Alexey Bataev56dafe82014-06-20 07:16:17 +00009287 ExprResult Val =
9288 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
9289 if (Val.isInvalid())
9290 return nullptr;
9291
9292 ValExpr = Val.get();
9293
9294 // OpenMP [2.7.1, Restrictions]
9295 // chunk_size must be a loop invariant integer expression with a positive
9296 // value.
9297 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00009298 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
9299 if (Result.isSigned() && !Result.isStrictlyPositive()) {
9300 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00009301 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00009302 return nullptr;
9303 }
Alexey Bataev2ba67042017-11-28 21:11:44 +00009304 } else if (getOpenMPCaptureRegionForClause(
9305 DSAStack->getCurrentDirective(), OMPC_schedule) !=
9306 OMPD_unknown &&
Alexey Bataevb46cdea2016-06-15 11:20:48 +00009307 !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00009308 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00009309 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev5a3af132016-03-29 08:58:54 +00009310 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
9311 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009312 }
9313 }
9314 }
9315
Alexey Bataev6402bca2015-12-28 07:25:51 +00009316 return new (Context)
9317 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00009318 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009319}
9320
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009321OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
9322 SourceLocation StartLoc,
9323 SourceLocation EndLoc) {
9324 OMPClause *Res = nullptr;
9325 switch (Kind) {
9326 case OMPC_ordered:
9327 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
9328 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00009329 case OMPC_nowait:
9330 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
9331 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009332 case OMPC_untied:
9333 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
9334 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009335 case OMPC_mergeable:
9336 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
9337 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009338 case OMPC_read:
9339 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
9340 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00009341 case OMPC_write:
9342 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
9343 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00009344 case OMPC_update:
9345 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
9346 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00009347 case OMPC_capture:
9348 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
9349 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009350 case OMPC_seq_cst:
9351 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
9352 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00009353 case OMPC_threads:
9354 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
9355 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009356 case OMPC_simd:
9357 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
9358 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00009359 case OMPC_nogroup:
9360 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
9361 break;
Kelvin Li1408f912018-09-26 04:28:39 +00009362 case OMPC_unified_address:
9363 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc);
9364 break;
Patrick Lyster4a370b92018-10-01 13:47:43 +00009365 case OMPC_unified_shared_memory:
9366 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
9367 break;
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009368 case OMPC_reverse_offload:
9369 Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc);
9370 break;
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009371 case OMPC_dynamic_allocators:
9372 Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc);
9373 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009374 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00009375 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009376 case OMPC_num_threads:
9377 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009378 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009379 case OMPC_collapse:
9380 case OMPC_schedule:
9381 case OMPC_private:
9382 case OMPC_firstprivate:
9383 case OMPC_lastprivate:
9384 case OMPC_shared:
9385 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00009386 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00009387 case OMPC_in_reduction:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009388 case OMPC_linear:
9389 case OMPC_aligned:
9390 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009391 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009392 case OMPC_default:
9393 case OMPC_proc_bind:
9394 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00009395 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009396 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00009397 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00009398 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009399 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009400 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009401 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009402 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00009403 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009404 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00009405 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009406 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009407 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009408 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00009409 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00009410 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00009411 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00009412 case OMPC_is_device_ptr:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009413 case OMPC_atomic_default_mem_order:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009414 llvm_unreachable("Clause is not allowed.");
9415 }
9416 return Res;
9417}
9418
Alexey Bataev236070f2014-06-20 11:19:47 +00009419OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
9420 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00009421 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00009422 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
9423}
9424
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009425OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
9426 SourceLocation EndLoc) {
9427 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
9428}
9429
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009430OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
9431 SourceLocation EndLoc) {
9432 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
9433}
9434
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009435OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
9436 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009437 return new (Context) OMPReadClause(StartLoc, EndLoc);
9438}
9439
Alexey Bataevdea47612014-07-23 07:46:59 +00009440OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
9441 SourceLocation EndLoc) {
9442 return new (Context) OMPWriteClause(StartLoc, EndLoc);
9443}
9444
Alexey Bataev67a4f222014-07-23 10:25:33 +00009445OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
9446 SourceLocation EndLoc) {
9447 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
9448}
9449
Alexey Bataev459dec02014-07-24 06:46:57 +00009450OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
9451 SourceLocation EndLoc) {
9452 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
9453}
9454
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009455OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
9456 SourceLocation EndLoc) {
9457 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
9458}
9459
Alexey Bataev346265e2015-09-25 10:37:12 +00009460OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
9461 SourceLocation EndLoc) {
9462 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
9463}
9464
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009465OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
9466 SourceLocation EndLoc) {
9467 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
9468}
9469
Alexey Bataevb825de12015-12-07 10:51:44 +00009470OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
9471 SourceLocation EndLoc) {
9472 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
9473}
9474
Kelvin Li1408f912018-09-26 04:28:39 +00009475OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc,
9476 SourceLocation EndLoc) {
9477 return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc);
9478}
9479
Patrick Lyster4a370b92018-10-01 13:47:43 +00009480OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc,
9481 SourceLocation EndLoc) {
9482 return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
9483}
9484
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009485OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc,
9486 SourceLocation EndLoc) {
9487 return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc);
9488}
9489
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009490OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc,
9491 SourceLocation EndLoc) {
9492 return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc);
9493}
9494
Alexey Bataevc5e02582014-06-16 07:08:35 +00009495OMPClause *Sema::ActOnOpenMPVarListClause(
9496 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
9497 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
9498 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009499 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00009500 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
9501 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
9502 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009503 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009504 switch (Kind) {
9505 case OMPC_private:
9506 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9507 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009508 case OMPC_firstprivate:
9509 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9510 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00009511 case OMPC_lastprivate:
9512 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9513 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00009514 case OMPC_shared:
9515 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
9516 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009517 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00009518 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9519 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00009520 break;
Alexey Bataev169d96a2017-07-18 20:17:46 +00009521 case OMPC_task_reduction:
9522 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9523 EndLoc, ReductionIdScopeSpec,
9524 ReductionId);
9525 break;
Alexey Bataevfa312f32017-07-21 18:48:21 +00009526 case OMPC_in_reduction:
9527 Res =
9528 ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9529 EndLoc, ReductionIdScopeSpec, ReductionId);
9530 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00009531 case OMPC_linear:
9532 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00009533 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00009534 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009535 case OMPC_aligned:
9536 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
9537 ColonLoc, EndLoc);
9538 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009539 case OMPC_copyin:
9540 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
9541 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009542 case OMPC_copyprivate:
9543 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9544 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00009545 case OMPC_flush:
9546 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
9547 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009548 case OMPC_depend:
David Majnemer9d168222016-08-05 17:44:54 +00009549 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
Kelvin Li0bff7af2015-11-23 05:32:03 +00009550 StartLoc, LParenLoc, EndLoc);
9551 break;
9552 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00009553 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
9554 DepLinMapLoc, ColonLoc, VarList, StartLoc,
9555 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009556 break;
Samuel Antao661c0902016-05-26 17:39:58 +00009557 case OMPC_to:
9558 Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc);
9559 break;
Samuel Antaoec172c62016-05-26 17:49:04 +00009560 case OMPC_from:
9561 Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc);
9562 break;
Carlo Bertolli2404b172016-07-13 15:37:16 +00009563 case OMPC_use_device_ptr:
9564 Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
9565 break;
Carlo Bertolli70594e92016-07-13 17:16:49 +00009566 case OMPC_is_device_ptr:
9567 Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
9568 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009569 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00009570 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00009571 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00009572 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009573 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00009574 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009575 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009576 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009577 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009578 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00009579 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009580 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009581 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009582 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009583 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00009584 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00009585 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00009586 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009587 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00009588 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00009589 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009590 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009591 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009592 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009593 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009594 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00009595 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00009596 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009597 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00009598 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009599 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009600 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009601 case OMPC_uniform:
Kelvin Li1408f912018-09-26 04:28:39 +00009602 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00009603 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009604 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009605 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009606 case OMPC_atomic_default_mem_order:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009607 llvm_unreachable("Clause is not allowed.");
9608 }
9609 return Res;
9610}
9611
Alexey Bataev90c228f2016-02-08 09:29:13 +00009612ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00009613 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00009614 ExprResult Res = BuildDeclRefExpr(
9615 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
9616 if (!Res.isUsable())
9617 return ExprError();
9618 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
9619 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
9620 if (!Res.isUsable())
9621 return ExprError();
9622 }
9623 if (VK != VK_LValue && Res.get()->isGLValue()) {
9624 Res = DefaultLvalueConversion(Res.get());
9625 if (!Res.isUsable())
9626 return ExprError();
9627 }
9628 return Res;
9629}
9630
Alexey Bataev60da77e2016-02-29 05:54:20 +00009631static std::pair<ValueDecl *, bool>
9632getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
9633 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00009634 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
9635 RefExpr->containsUnexpandedParameterPack())
9636 return std::make_pair(nullptr, true);
9637
Alexey Bataevd985eda2016-02-10 11:29:16 +00009638 // OpenMP [3.1, C/C++]
9639 // A list item is a variable name.
9640 // OpenMP [2.9.3.3, Restrictions, p.1]
9641 // A variable that is part of another variable (as an array or
9642 // structure element) cannot appear in a private clause.
9643 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009644 enum {
9645 NoArrayExpr = -1,
9646 ArraySubscript = 0,
9647 OMPArraySection = 1
9648 } IsArrayExpr = NoArrayExpr;
9649 if (AllowArraySection) {
9650 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009651 Expr *Base = ASE->getBase()->IgnoreParenImpCasts();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009652 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
9653 Base = TempASE->getBase()->IgnoreParenImpCasts();
9654 RefExpr = Base;
9655 IsArrayExpr = ArraySubscript;
9656 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009657 Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009658 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
9659 Base = TempOASE->getBase()->IgnoreParenImpCasts();
9660 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
9661 Base = TempASE->getBase()->IgnoreParenImpCasts();
9662 RefExpr = Base;
9663 IsArrayExpr = OMPArraySection;
9664 }
9665 }
9666 ELoc = RefExpr->getExprLoc();
9667 ERange = RefExpr->getSourceRange();
9668 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00009669 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
9670 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
9671 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
9672 (S.getCurrentThisType().isNull() || !ME ||
9673 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
9674 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009675 if (IsArrayExpr != NoArrayExpr) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009676 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
9677 << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +00009678 } else {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009679 S.Diag(ELoc,
9680 AllowArraySection
9681 ? diag::err_omp_expected_var_name_member_expr_or_array_item
9682 : diag::err_omp_expected_var_name_member_expr)
9683 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
9684 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009685 return std::make_pair(nullptr, false);
9686 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +00009687 return std::make_pair(
9688 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009689}
9690
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009691OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
9692 SourceLocation StartLoc,
9693 SourceLocation LParenLoc,
9694 SourceLocation EndLoc) {
9695 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00009696 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeve3727102018-04-18 15:57:46 +00009697 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009698 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009699 SourceLocation ELoc;
9700 SourceRange ERange;
9701 Expr *SimpleRefExpr = RefExpr;
9702 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009703 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009704 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009705 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009706 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009707 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009708 ValueDecl *D = Res.first;
9709 if (!D)
9710 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009711
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009712 QualType Type = D->getType();
9713 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009714
9715 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9716 // A variable that appears in a private clause must not have an incomplete
9717 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009718 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009719 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009720 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009721
Alexey Bataev758e55e2013-09-06 18:03:48 +00009722 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9723 // in a Construct]
9724 // Variables with the predetermined data-sharing attributes may not be
9725 // listed in data-sharing attributes clauses, except for the cases
9726 // listed below. For these exceptions only, listing a predetermined
9727 // variable in a data-sharing attribute clause is allowed and overrides
9728 // the variable's predetermined data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +00009729 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009730 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009731 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9732 << getOpenMPClauseName(OMPC_private);
Alexey Bataeve3727102018-04-18 15:57:46 +00009733 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009734 continue;
9735 }
9736
Alexey Bataeve3727102018-04-18 15:57:46 +00009737 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009738 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009739 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Kelvin Libf594a52016-12-17 05:48:59 +00009740 isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009741 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
9742 << getOpenMPClauseName(OMPC_private) << Type
Kelvin Libf594a52016-12-17 05:48:59 +00009743 << getOpenMPDirectiveName(CurrDir);
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009744 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009745 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009746 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009747 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009748 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009749 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009750 continue;
9751 }
9752
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009753 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
9754 // A list item cannot appear in both a map clause and a data-sharing
9755 // attribute clause on the same construct
Alexey Bataeve3727102018-04-18 15:57:46 +00009756 if (isOpenMPTargetExecutionDirective(CurrDir)) {
Samuel Antao6890b092016-07-28 14:25:09 +00009757 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +00009758 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +00009759 VD, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +00009760 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
9761 OpenMPClauseKind WhereFoundClauseKind) -> bool {
9762 ConflictKind = WhereFoundClauseKind;
9763 return true;
9764 })) {
9765 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009766 << getOpenMPClauseName(OMPC_private)
Samuel Antao6890b092016-07-28 14:25:09 +00009767 << getOpenMPClauseName(ConflictKind)
Kelvin Libf594a52016-12-17 05:48:59 +00009768 << getOpenMPDirectiveName(CurrDir);
Alexey Bataeve3727102018-04-18 15:57:46 +00009769 reportOriginalDsa(*this, DSAStack, D, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009770 continue;
9771 }
9772 }
9773
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009774 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
9775 // A variable of class type (or array thereof) that appears in a private
9776 // clause requires an accessible, unambiguous default constructor for the
9777 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00009778 // Generate helper private variable and initialize it with the default
9779 // value. The address of the original variable is replaced by the address of
9780 // the new private variable in CodeGen. This new variable is not added to
9781 // IdResolver, so the code in the OpenMP region uses original variable for
9782 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009783 Type = Type.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +00009784 VarDecl *VDPrivate =
Alexey Bataev63cc8e92018-03-20 14:45:59 +00009785 buildVarDecl(*this, ELoc, Type, D->getName(),
9786 D->hasAttrs() ? &D->getAttrs() : nullptr,
9787 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Richard Smith3beb7c62017-01-12 02:27:38 +00009788 ActOnUninitializedDecl(VDPrivate);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009789 if (VDPrivate->isInvalidDecl())
9790 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00009791 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009792 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009793
Alexey Bataev90c228f2016-02-08 09:29:13 +00009794 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009795 if (!VD && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00009796 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00009797 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009798 Vars.push_back((VD || CurContext->isDependentContext())
9799 ? RefExpr->IgnoreParens()
9800 : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009801 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009802 }
9803
Alexey Bataeved09d242014-05-28 05:53:51 +00009804 if (Vars.empty())
9805 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009806
Alexey Bataev03b340a2014-10-21 03:16:40 +00009807 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9808 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009809}
9810
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009811namespace {
9812class DiagsUninitializedSeveretyRAII {
9813private:
9814 DiagnosticsEngine &Diags;
9815 SourceLocation SavedLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00009816 bool IsIgnored = false;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009817
9818public:
9819 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
9820 bool IsIgnored)
9821 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
9822 if (!IsIgnored) {
9823 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
9824 /*Map*/ diag::Severity::Ignored, Loc);
9825 }
9826 }
9827 ~DiagsUninitializedSeveretyRAII() {
9828 if (!IsIgnored)
9829 Diags.popMappings(SavedLoc);
9830 }
9831};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00009832}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009833
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009834OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
9835 SourceLocation StartLoc,
9836 SourceLocation LParenLoc,
9837 SourceLocation EndLoc) {
9838 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009839 SmallVector<Expr *, 8> PrivateCopies;
9840 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00009841 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009842 bool IsImplicitClause =
9843 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
Alexey Bataeve3727102018-04-18 15:57:46 +00009844 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009845
Alexey Bataeve3727102018-04-18 15:57:46 +00009846 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009847 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009848 SourceLocation ELoc;
9849 SourceRange ERange;
9850 Expr *SimpleRefExpr = RefExpr;
9851 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009852 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009853 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009854 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009855 PrivateCopies.push_back(nullptr);
9856 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009857 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009858 ValueDecl *D = Res.first;
9859 if (!D)
9860 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009861
Alexey Bataev60da77e2016-02-29 05:54:20 +00009862 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00009863 QualType Type = D->getType();
9864 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009865
9866 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9867 // A variable that appears in a private clause must not have an incomplete
9868 // type or a reference type.
9869 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00009870 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009871 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009872 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009873
9874 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
9875 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00009876 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009877 // class type.
Alexey Bataeve3727102018-04-18 15:57:46 +00009878 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009879
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009880 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00009881 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009882 if (!IsImplicitClause) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009883 DSAStackTy::DSAVarData DVar =
9884 DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00009885 TopDVar = DVar;
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009886 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009887 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009888 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
9889 // A list item that specifies a given variable may not appear in more
9890 // than one clause on the same directive, except that a variable may be
9891 // specified in both firstprivate and lastprivate clauses.
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009892 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
9893 // A list item may appear in a firstprivate or lastprivate clause but not
9894 // both.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009895 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevb358f992017-12-01 17:40:15 +00009896 (isOpenMPDistributeDirective(CurrDir) ||
9897 DVar.CKind != OMPC_lastprivate) &&
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009898 DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009899 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00009900 << getOpenMPClauseName(DVar.CKind)
9901 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +00009902 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009903 continue;
9904 }
9905
9906 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9907 // in a Construct]
9908 // Variables with the predetermined data-sharing attributes may not be
9909 // listed in data-sharing attributes clauses, except for the cases
9910 // listed below. For these exceptions only, listing a predetermined
9911 // variable in a data-sharing attribute clause is allowed and overrides
9912 // the variable's predetermined data-sharing attributes.
9913 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9914 // in a Construct, C/C++, p.2]
9915 // Variables with const-qualified type having no mutable member may be
9916 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00009917 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009918 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
9919 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00009920 << getOpenMPClauseName(DVar.CKind)
9921 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +00009922 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009923 continue;
9924 }
9925
9926 // OpenMP [2.9.3.4, Restrictions, p.2]
9927 // A list item that is private within a parallel region must not appear
9928 // in a firstprivate clause on a worksharing construct if any of the
9929 // worksharing regions arising from the worksharing construct ever bind
9930 // to any of the parallel regions arising from the parallel construct.
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009931 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
9932 // A list item that is private within a teams region must not appear in a
9933 // firstprivate clause on a distribute construct if any of the distribute
9934 // regions arising from the distribute construct ever bind to any of the
9935 // teams regions arising from the teams construct.
9936 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
9937 // A list item that appears in a reduction clause of a teams construct
9938 // must not appear in a firstprivate clause on a distribute construct if
9939 // any of the distribute regions arising from the distribute construct
9940 // ever bind to any of the teams regions arising from the teams construct.
9941 if ((isOpenMPWorksharingDirective(CurrDir) ||
9942 isOpenMPDistributeDirective(CurrDir)) &&
Kelvin Li579e41c2016-11-30 23:51:03 +00009943 !isOpenMPParallelDirective(CurrDir) &&
9944 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00009945 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009946 if (DVar.CKind != OMPC_shared &&
9947 (isOpenMPParallelDirective(DVar.DKind) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009948 isOpenMPTeamsDirective(DVar.DKind) ||
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009949 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00009950 Diag(ELoc, diag::err_omp_required_access)
9951 << getOpenMPClauseName(OMPC_firstprivate)
9952 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +00009953 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00009954 continue;
9955 }
9956 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009957 // OpenMP [2.9.3.4, Restrictions, p.3]
9958 // A list item that appears in a reduction clause of a parallel construct
9959 // must not appear in a firstprivate clause on a worksharing or task
9960 // construct if any of the worksharing or task regions arising from the
9961 // worksharing or task construct ever bind to any of the parallel regions
9962 // arising from the parallel construct.
9963 // OpenMP [2.9.3.4, Restrictions, p.4]
9964 // A list item that appears in a reduction clause in worksharing
9965 // construct must not appear in a firstprivate clause in a task construct
9966 // encountered during execution of any of the worksharing regions arising
9967 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +00009968 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00009969 DVar = DSAStack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00009970 D, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
9971 [](OpenMPDirectiveKind K) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00009972 return isOpenMPParallelDirective(K) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009973 isOpenMPWorksharingDirective(K) ||
9974 isOpenMPTeamsDirective(K);
Alexey Bataev7ace49d2016-05-17 08:55:33 +00009975 },
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009976 /*FromParent=*/true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009977 if (DVar.CKind == OMPC_reduction &&
9978 (isOpenMPParallelDirective(DVar.DKind) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009979 isOpenMPWorksharingDirective(DVar.DKind) ||
9980 isOpenMPTeamsDirective(DVar.DKind))) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009981 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
9982 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataeve3727102018-04-18 15:57:46 +00009983 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009984 continue;
9985 }
9986 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00009987
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009988 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
9989 // A list item cannot appear in both a map clause and a data-sharing
9990 // attribute clause on the same construct
Alexey Bataevb358f992017-12-01 17:40:15 +00009991 if (isOpenMPTargetExecutionDirective(CurrDir)) {
Samuel Antao6890b092016-07-28 14:25:09 +00009992 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +00009993 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +00009994 VD, /*CurrentRegionOnly=*/true,
Alexey Bataeve3727102018-04-18 15:57:46 +00009995 [&ConflictKind](
9996 OMPClauseMappableExprCommon::MappableExprComponentListRef,
9997 OpenMPClauseKind WhereFoundClauseKind) {
Samuel Antao6890b092016-07-28 14:25:09 +00009998 ConflictKind = WhereFoundClauseKind;
9999 return true;
10000 })) {
10001 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010002 << getOpenMPClauseName(OMPC_firstprivate)
Samuel Antao6890b092016-07-28 14:25:09 +000010003 << getOpenMPClauseName(ConflictKind)
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010004 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000010005 reportOriginalDsa(*this, DSAStack, D, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010006 continue;
10007 }
10008 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010009 }
10010
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010011 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000010012 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +000010013 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010014 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
10015 << getOpenMPClauseName(OMPC_firstprivate) << Type
10016 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
10017 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +000010018 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010019 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +000010020 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010021 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +000010022 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010023 continue;
10024 }
10025
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010026 Type = Type.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010027 VarDecl *VDPrivate =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000010028 buildVarDecl(*this, ELoc, Type, D->getName(),
10029 D->hasAttrs() ? &D->getAttrs() : nullptr,
10030 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010031 // Generate helper private variable and initialize it with the value of the
10032 // original variable. The address of the original variable is replaced by
10033 // the address of the new private variable in the CodeGen. This new variable
10034 // is not added to IdResolver, so the code in the OpenMP region uses
10035 // original variable for proper diagnostics and variable capturing.
10036 Expr *VDInitRefExpr = nullptr;
10037 // For arrays generate initializer for single element and replace it by the
10038 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010039 if (Type->isArrayType()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010040 VarDecl *VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +000010041 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010042 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataeve3727102018-04-18 15:57:46 +000010043 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010044 ElemType = ElemType.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010045 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
10046 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +000010047 InitializedEntity Entity =
10048 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010049 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
10050
10051 InitializationSequence InitSeq(*this, Entity, Kind, Init);
10052 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
10053 if (Result.isInvalid())
10054 VDPrivate->setInvalidDecl();
10055 else
10056 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010057 // Remove temp variable declaration.
10058 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010059 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +000010060 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
10061 ".firstprivate.temp");
Alexey Bataevd985eda2016-02-10 11:29:16 +000010062 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
10063 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +000010064 AddInitializerToDecl(VDPrivate,
10065 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000010066 /*DirectInit=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010067 }
10068 if (VDPrivate->isInvalidDecl()) {
10069 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +000010070 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010071 diag::note_omp_task_predetermined_firstprivate_here);
10072 }
10073 continue;
10074 }
10075 CurContext->addDecl(VDPrivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000010076 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +000010077 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
10078 RefExpr->getExprLoc());
10079 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010080 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010081 if (TopDVar.CKind == OMPC_lastprivate) {
Alexey Bataev005248a2016-02-25 05:25:57 +000010082 Ref = TopDVar.PrivateCopy;
Alexey Bataeve3727102018-04-18 15:57:46 +000010083 } else {
Alexey Bataev61205072016-03-02 04:57:40 +000010084 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataeve3727102018-04-18 15:57:46 +000010085 if (!isOpenMPCapturedDecl(D))
Alexey Bataev005248a2016-02-25 05:25:57 +000010086 ExprCaptures.push_back(Ref->getDecl());
10087 }
Alexey Bataev417089f2016-02-17 13:19:37 +000010088 }
Alexey Bataevd985eda2016-02-10 11:29:16 +000010089 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010090 Vars.push_back((VD || CurContext->isDependentContext())
10091 ? RefExpr->IgnoreParens()
10092 : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010093 PrivateCopies.push_back(VDPrivateRefExpr);
10094 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010095 }
10096
Alexey Bataeved09d242014-05-28 05:53:51 +000010097 if (Vars.empty())
10098 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010099
10100 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +000010101 Vars, PrivateCopies, Inits,
10102 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010103}
10104
Alexander Musman1bb328c2014-06-04 13:06:39 +000010105OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
10106 SourceLocation StartLoc,
10107 SourceLocation LParenLoc,
10108 SourceLocation EndLoc) {
10109 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +000010110 SmallVector<Expr *, 8> SrcExprs;
10111 SmallVector<Expr *, 8> DstExprs;
10112 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +000010113 SmallVector<Decl *, 4> ExprCaptures;
10114 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeve3727102018-04-18 15:57:46 +000010115 for (Expr *RefExpr : VarList) {
Alexander Musman1bb328c2014-06-04 13:06:39 +000010116 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +000010117 SourceLocation ELoc;
10118 SourceRange ERange;
10119 Expr *SimpleRefExpr = RefExpr;
10120 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +000010121 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +000010122 // It will be analyzed later.
10123 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +000010124 SrcExprs.push_back(nullptr);
10125 DstExprs.push_back(nullptr);
10126 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010127 }
Alexey Bataev74caaf22016-02-20 04:09:36 +000010128 ValueDecl *D = Res.first;
10129 if (!D)
10130 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +000010131
Alexey Bataev74caaf22016-02-20 04:09:36 +000010132 QualType Type = D->getType();
10133 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010134
10135 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
10136 // A variable that appears in a lastprivate clause must not have an
10137 // incomplete type or a reference type.
10138 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +000010139 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +000010140 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010141 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +000010142
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010143 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexander Musman1bb328c2014-06-04 13:06:39 +000010144 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
10145 // in a Construct]
10146 // Variables with the predetermined data-sharing attributes may not be
10147 // listed in data-sharing attributes clauses, except for the cases
10148 // listed below.
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010149 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
10150 // A list item may appear in a firstprivate or lastprivate clause but not
10151 // both.
Alexey Bataeve3727102018-04-18 15:57:46 +000010152 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010153 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
Alexey Bataevb358f992017-12-01 17:40:15 +000010154 (isOpenMPDistributeDirective(CurrDir) ||
10155 DVar.CKind != OMPC_firstprivate) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +000010156 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
10157 Diag(ELoc, diag::err_omp_wrong_dsa)
10158 << getOpenMPClauseName(DVar.CKind)
10159 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000010160 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010161 continue;
10162 }
10163
Alexey Bataevf29276e2014-06-18 04:14:57 +000010164 // OpenMP [2.14.3.5, Restrictions, p.2]
10165 // A list item that is private within a parallel region, or that appears in
10166 // the reduction clause of a parallel construct, must not appear in a
10167 // lastprivate clause on a worksharing construct if any of the corresponding
10168 // worksharing regions ever binds to any of the corresponding parallel
10169 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +000010170 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +000010171 if (isOpenMPWorksharingDirective(CurrDir) &&
Kelvin Li579e41c2016-11-30 23:51:03 +000010172 !isOpenMPParallelDirective(CurrDir) &&
10173 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +000010174 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +000010175 if (DVar.CKind != OMPC_shared) {
10176 Diag(ELoc, diag::err_omp_required_access)
10177 << getOpenMPClauseName(OMPC_lastprivate)
10178 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +000010179 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +000010180 continue;
10181 }
10182 }
Alexey Bataev74caaf22016-02-20 04:09:36 +000010183
Alexander Musman1bb328c2014-06-04 13:06:39 +000010184 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +000010185 // A variable of class type (or array thereof) that appears in a
10186 // lastprivate clause requires an accessible, unambiguous default
10187 // constructor for the class type, unless the list item is also specified
10188 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +000010189 // A variable of class type (or array thereof) that appears in a
10190 // lastprivate clause requires an accessible, unambiguous copy assignment
10191 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +000010192 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010193 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(),
10194 Type.getUnqualifiedType(), ".lastprivate.src",
10195 D->hasAttrs() ? &D->getAttrs() : nullptr);
10196 DeclRefExpr *PseudoSrcExpr =
Alexey Bataev74caaf22016-02-20 04:09:36 +000010197 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataeve3727102018-04-18 15:57:46 +000010198 VarDecl *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +000010199 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +000010200 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000010201 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +000010202 // For arrays generate assignment operation for single element and replace
10203 // it by the original array element in CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +000010204 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
10205 PseudoDstExpr, PseudoSrcExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +000010206 if (AssignmentOp.isInvalid())
10207 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +000010208 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +000010209 /*DiscardedValue=*/true);
10210 if (AssignmentOp.isInvalid())
10211 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +000010212
Alexey Bataev74caaf22016-02-20 04:09:36 +000010213 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010214 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010215 if (TopDVar.CKind == OMPC_firstprivate) {
Alexey Bataev005248a2016-02-25 05:25:57 +000010216 Ref = TopDVar.PrivateCopy;
Alexey Bataeve3727102018-04-18 15:57:46 +000010217 } else {
Alexey Bataev61205072016-03-02 04:57:40 +000010218 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000010219 if (!isOpenMPCapturedDecl(D))
Alexey Bataev005248a2016-02-25 05:25:57 +000010220 ExprCaptures.push_back(Ref->getDecl());
10221 }
10222 if (TopDVar.CKind == OMPC_firstprivate ||
Alexey Bataeve3727102018-04-18 15:57:46 +000010223 (!isOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010224 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +000010225 ExprResult RefRes = DefaultLvalueConversion(Ref);
10226 if (!RefRes.isUsable())
10227 continue;
10228 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +000010229 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
10230 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +000010231 if (!PostUpdateRes.isUsable())
10232 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +000010233 ExprPostUpdates.push_back(
10234 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +000010235 }
10236 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010237 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010238 Vars.push_back((VD || CurContext->isDependentContext())
10239 ? RefExpr->IgnoreParens()
10240 : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +000010241 SrcExprs.push_back(PseudoSrcExpr);
10242 DstExprs.push_back(PseudoDstExpr);
10243 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +000010244 }
10245
10246 if (Vars.empty())
10247 return nullptr;
10248
10249 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +000010250 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +000010251 buildPreInits(Context, ExprCaptures),
10252 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +000010253}
10254
Alexey Bataev758e55e2013-09-06 18:03:48 +000010255OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
10256 SourceLocation StartLoc,
10257 SourceLocation LParenLoc,
10258 SourceLocation EndLoc) {
10259 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +000010260 for (Expr *RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010261 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +000010262 SourceLocation ELoc;
10263 SourceRange ERange;
10264 Expr *SimpleRefExpr = RefExpr;
10265 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010266 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +000010267 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000010268 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010269 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010270 ValueDecl *D = Res.first;
10271 if (!D)
10272 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +000010273
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010274 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010275 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
10276 // in a Construct]
10277 // Variables with the predetermined data-sharing attributes may not be
10278 // listed in data-sharing attributes clauses, except for the cases
10279 // listed below. For these exceptions only, listing a predetermined
10280 // variable in a data-sharing attribute clause is allowed and overrides
10281 // the variable's predetermined data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +000010282 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataeved09d242014-05-28 05:53:51 +000010283 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
10284 DVar.RefExpr) {
10285 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
10286 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +000010287 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010288 continue;
10289 }
10290
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010291 DeclRefExpr *Ref = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000010292 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +000010293 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010294 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010295 Vars.push_back((VD || !Ref || CurContext->isDependentContext())
10296 ? RefExpr->IgnoreParens()
10297 : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010298 }
10299
Alexey Bataeved09d242014-05-28 05:53:51 +000010300 if (Vars.empty())
10301 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +000010302
10303 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
10304}
10305
Alexey Bataevc5e02582014-06-16 07:08:35 +000010306namespace {
10307class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
10308 DSAStackTy *Stack;
10309
10310public:
10311 bool VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010312 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
10313 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataevc5e02582014-06-16 07:08:35 +000010314 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
10315 return false;
10316 if (DVar.CKind != OMPC_unknown)
10317 return true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010318 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +000010319 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; },
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010320 /*FromParent=*/true);
Alexey Bataeve3727102018-04-18 15:57:46 +000010321 return DVarPrivate.CKind != OMPC_unknown;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010322 }
10323 return false;
10324 }
10325 bool VisitStmt(Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010326 for (Stmt *Child : S->children()) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010327 if (Child && Visit(Child))
10328 return true;
10329 }
10330 return false;
10331 }
Alexey Bataev23b69422014-06-18 07:08:49 +000010332 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +000010333};
Alexey Bataev23b69422014-06-18 07:08:49 +000010334} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +000010335
Alexey Bataev60da77e2016-02-29 05:54:20 +000010336namespace {
10337// Transform MemberExpression for specified FieldDecl of current class to
10338// DeclRefExpr to specified OMPCapturedExprDecl.
10339class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
10340 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
Alexey Bataeve3727102018-04-18 15:57:46 +000010341 ValueDecl *Field = nullptr;
10342 DeclRefExpr *CapturedExpr = nullptr;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010343
10344public:
10345 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
10346 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
10347
10348 ExprResult TransformMemberExpr(MemberExpr *E) {
10349 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
10350 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +000010351 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +000010352 return CapturedExpr;
10353 }
10354 return BaseTransform::TransformMemberExpr(E);
10355 }
10356 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
10357};
10358} // namespace
10359
Alexey Bataev97d18bf2018-04-11 19:21:00 +000010360template <typename T, typename U>
10361static T filterLookupForUDR(SmallVectorImpl<U> &Lookups,
10362 const llvm::function_ref<T(ValueDecl *)> Gen) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010363 for (U &Set : Lookups) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010364 for (auto *D : Set) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010365 if (T Res = Gen(cast<ValueDecl>(D)))
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010366 return Res;
10367 }
10368 }
10369 return T();
10370}
10371
Alexey Bataev43b90b72018-09-12 16:31:59 +000010372static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) {
10373 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case");
10374
10375 for (auto RD : D->redecls()) {
10376 // Don't bother with extra checks if we already know this one isn't visible.
10377 if (RD == D)
10378 continue;
10379
10380 auto ND = cast<NamedDecl>(RD);
10381 if (LookupResult::isVisible(SemaRef, ND))
10382 return ND;
10383 }
10384
10385 return nullptr;
10386}
10387
10388static void
10389argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &ReductionId,
10390 SourceLocation Loc, QualType Ty,
10391 SmallVectorImpl<UnresolvedSet<8>> &Lookups) {
10392 // Find all of the associated namespaces and classes based on the
10393 // arguments we have.
10394 Sema::AssociatedNamespaceSet AssociatedNamespaces;
10395 Sema::AssociatedClassSet AssociatedClasses;
10396 OpaqueValueExpr OVE(Loc, Ty, VK_LValue);
10397 SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces,
10398 AssociatedClasses);
10399
10400 // C++ [basic.lookup.argdep]p3:
10401 // Let X be the lookup set produced by unqualified lookup (3.4.1)
10402 // and let Y be the lookup set produced by argument dependent
10403 // lookup (defined as follows). If X contains [...] then Y is
10404 // empty. Otherwise Y is the set of declarations found in the
10405 // namespaces associated with the argument types as described
10406 // below. The set of declarations found by the lookup of the name
10407 // is the union of X and Y.
10408 //
10409 // Here, we compute Y and add its members to the overloaded
10410 // candidate set.
10411 for (auto *NS : AssociatedNamespaces) {
10412 // When considering an associated namespace, the lookup is the
10413 // same as the lookup performed when the associated namespace is
10414 // used as a qualifier (3.4.3.2) except that:
10415 //
10416 // -- Any using-directives in the associated namespace are
10417 // ignored.
10418 //
10419 // -- Any namespace-scope friend functions declared in
10420 // associated classes are visible within their respective
10421 // namespaces even if they are not visible during an ordinary
10422 // lookup (11.4).
10423 DeclContext::lookup_result R = NS->lookup(ReductionId.getName());
10424 for (auto *D : R) {
10425 auto *Underlying = D;
10426 if (auto *USD = dyn_cast<UsingShadowDecl>(D))
10427 Underlying = USD->getTargetDecl();
10428
10429 if (!isa<OMPDeclareReductionDecl>(Underlying))
10430 continue;
10431
10432 if (!SemaRef.isVisible(D)) {
10433 D = findAcceptableDecl(SemaRef, D);
10434 if (!D)
10435 continue;
10436 if (auto *USD = dyn_cast<UsingShadowDecl>(D))
10437 Underlying = USD->getTargetDecl();
10438 }
10439 Lookups.emplace_back();
10440 Lookups.back().addDecl(Underlying);
10441 }
10442 }
10443}
10444
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010445static ExprResult
10446buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
10447 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
10448 const DeclarationNameInfo &ReductionId, QualType Ty,
10449 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
10450 if (ReductionIdScopeSpec.isInvalid())
10451 return ExprError();
10452 SmallVector<UnresolvedSet<8>, 4> Lookups;
10453 if (S) {
10454 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
10455 Lookup.suppressDiagnostics();
10456 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010457 NamedDecl *D = Lookup.getRepresentativeDecl();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010458 do {
10459 S = S->getParent();
10460 } while (S && !S->isDeclScope(D));
10461 if (S)
10462 S = S->getParent();
Alexey Bataev43b90b72018-09-12 16:31:59 +000010463 Lookups.emplace_back();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010464 Lookups.back().append(Lookup.begin(), Lookup.end());
10465 Lookup.clear();
10466 }
10467 } else if (auto *ULE =
10468 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
10469 Lookups.push_back(UnresolvedSet<8>());
10470 Decl *PrevD = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000010471 for (NamedDecl *D : ULE->decls()) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010472 if (D == PrevD)
10473 Lookups.push_back(UnresolvedSet<8>());
10474 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
10475 Lookups.back().addDecl(DRD);
10476 PrevD = D;
10477 }
10478 }
Alexey Bataevfdc20352017-08-25 15:43:55 +000010479 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() ||
10480 Ty->isInstantiationDependentType() ||
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010481 Ty->containsUnexpandedParameterPack() ||
Alexey Bataeve3727102018-04-18 15:57:46 +000010482 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010483 return !D->isInvalidDecl() &&
10484 (D->getType()->isDependentType() ||
10485 D->getType()->isInstantiationDependentType() ||
10486 D->getType()->containsUnexpandedParameterPack());
10487 })) {
10488 UnresolvedSet<8> ResSet;
Alexey Bataeve3727102018-04-18 15:57:46 +000010489 for (const UnresolvedSet<8> &Set : Lookups) {
Alexey Bataev43b90b72018-09-12 16:31:59 +000010490 if (Set.empty())
10491 continue;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010492 ResSet.append(Set.begin(), Set.end());
10493 // The last item marks the end of all declarations at the specified scope.
10494 ResSet.addDecl(Set[Set.size() - 1]);
10495 }
10496 return UnresolvedLookupExpr::Create(
10497 SemaRef.Context, /*NamingClass=*/nullptr,
10498 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
10499 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
10500 }
Alexey Bataev43b90b72018-09-12 16:31:59 +000010501 // Lookup inside the classes.
10502 // C++ [over.match.oper]p3:
10503 // For a unary operator @ with an operand of a type whose
10504 // cv-unqualified version is T1, and for a binary operator @ with
10505 // a left operand of a type whose cv-unqualified version is T1 and
10506 // a right operand of a type whose cv-unqualified version is T2,
10507 // three sets of candidate functions, designated member
10508 // candidates, non-member candidates and built-in candidates, are
10509 // constructed as follows:
10510 // -- If T1 is a complete class type or a class currently being
10511 // defined, the set of member candidates is the result of the
10512 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise,
10513 // the set of member candidates is empty.
10514 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
10515 Lookup.suppressDiagnostics();
10516 if (const auto *TyRec = Ty->getAs<RecordType>()) {
10517 // Complete the type if it can be completed.
10518 // If the type is neither complete nor being defined, bail out now.
10519 if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() ||
10520 TyRec->getDecl()->getDefinition()) {
10521 Lookup.clear();
10522 SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl());
10523 if (Lookup.empty()) {
10524 Lookups.emplace_back();
10525 Lookups.back().append(Lookup.begin(), Lookup.end());
10526 }
10527 }
10528 }
10529 // Perform ADL.
10530 argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010531 if (auto *VD = filterLookupForUDR<ValueDecl *>(
10532 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
10533 if (!D->isInvalidDecl() &&
10534 SemaRef.Context.hasSameType(D->getType(), Ty))
10535 return D;
10536 return nullptr;
10537 }))
10538 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
10539 if (auto *VD = filterLookupForUDR<ValueDecl *>(
10540 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
10541 if (!D->isInvalidDecl() &&
10542 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
10543 !Ty.isMoreQualifiedThan(D->getType()))
10544 return D;
10545 return nullptr;
10546 })) {
10547 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
10548 /*DetectVirtual=*/false);
10549 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
10550 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
10551 VD->getType().getUnqualifiedType()))) {
10552 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
10553 /*DiagID=*/0) !=
10554 Sema::AR_inaccessible) {
10555 SemaRef.BuildBasePathArray(Paths, BasePath);
10556 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
10557 }
10558 }
10559 }
10560 }
10561 if (ReductionIdScopeSpec.isSet()) {
10562 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
10563 return ExprError();
10564 }
10565 return ExprEmpty();
10566}
10567
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010568namespace {
10569/// Data for the reduction-based clauses.
10570struct ReductionData {
10571 /// List of original reduction items.
10572 SmallVector<Expr *, 8> Vars;
10573 /// List of private copies of the reduction items.
10574 SmallVector<Expr *, 8> Privates;
10575 /// LHS expressions for the reduction_op expressions.
10576 SmallVector<Expr *, 8> LHSs;
10577 /// RHS expressions for the reduction_op expressions.
10578 SmallVector<Expr *, 8> RHSs;
10579 /// Reduction operation expression.
10580 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev88202be2017-07-27 13:20:36 +000010581 /// Taskgroup descriptors for the corresponding reduction items in
10582 /// in_reduction clauses.
10583 SmallVector<Expr *, 8> TaskgroupDescriptors;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010584 /// List of captures for clause.
10585 SmallVector<Decl *, 4> ExprCaptures;
10586 /// List of postupdate expressions.
10587 SmallVector<Expr *, 4> ExprPostUpdates;
10588 ReductionData() = delete;
10589 /// Reserves required memory for the reduction data.
10590 ReductionData(unsigned Size) {
10591 Vars.reserve(Size);
10592 Privates.reserve(Size);
10593 LHSs.reserve(Size);
10594 RHSs.reserve(Size);
10595 ReductionOps.reserve(Size);
Alexey Bataev88202be2017-07-27 13:20:36 +000010596 TaskgroupDescriptors.reserve(Size);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010597 ExprCaptures.reserve(Size);
10598 ExprPostUpdates.reserve(Size);
10599 }
10600 /// Stores reduction item and reduction operation only (required for dependent
10601 /// reduction item).
10602 void push(Expr *Item, Expr *ReductionOp) {
10603 Vars.emplace_back(Item);
10604 Privates.emplace_back(nullptr);
10605 LHSs.emplace_back(nullptr);
10606 RHSs.emplace_back(nullptr);
10607 ReductionOps.emplace_back(ReductionOp);
Alexey Bataev88202be2017-07-27 13:20:36 +000010608 TaskgroupDescriptors.emplace_back(nullptr);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010609 }
10610 /// Stores reduction data.
Alexey Bataev88202be2017-07-27 13:20:36 +000010611 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp,
10612 Expr *TaskgroupDescriptor) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010613 Vars.emplace_back(Item);
10614 Privates.emplace_back(Private);
10615 LHSs.emplace_back(LHS);
10616 RHSs.emplace_back(RHS);
10617 ReductionOps.emplace_back(ReductionOp);
Alexey Bataev88202be2017-07-27 13:20:36 +000010618 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010619 }
10620};
10621} // namespace
10622
Alexey Bataeve3727102018-04-18 15:57:46 +000010623static bool checkOMPArraySectionConstantForReduction(
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010624 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement,
10625 SmallVectorImpl<llvm::APSInt> &ArraySizes) {
10626 const Expr *Length = OASE->getLength();
10627 if (Length == nullptr) {
10628 // For array sections of the form [1:] or [:], we would need to analyze
10629 // the lower bound...
10630 if (OASE->getColonLoc().isValid())
10631 return false;
10632
10633 // This is an array subscript which has implicit length 1!
10634 SingleElement = true;
10635 ArraySizes.push_back(llvm::APSInt::get(1));
10636 } else {
10637 llvm::APSInt ConstantLengthValue;
10638 if (!Length->EvaluateAsInt(ConstantLengthValue, Context))
10639 return false;
10640
10641 SingleElement = (ConstantLengthValue.getSExtValue() == 1);
10642 ArraySizes.push_back(ConstantLengthValue);
10643 }
10644
10645 // Get the base of this array section and walk up from there.
10646 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
10647
10648 // We require length = 1 for all array sections except the right-most to
10649 // guarantee that the memory region is contiguous and has no holes in it.
10650 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) {
10651 Length = TempOASE->getLength();
10652 if (Length == nullptr) {
10653 // For array sections of the form [1:] or [:], we would need to analyze
10654 // the lower bound...
10655 if (OASE->getColonLoc().isValid())
10656 return false;
10657
10658 // This is an array subscript which has implicit length 1!
10659 ArraySizes.push_back(llvm::APSInt::get(1));
10660 } else {
10661 llvm::APSInt ConstantLengthValue;
10662 if (!Length->EvaluateAsInt(ConstantLengthValue, Context) ||
10663 ConstantLengthValue.getSExtValue() != 1)
10664 return false;
10665
10666 ArraySizes.push_back(ConstantLengthValue);
10667 }
10668 Base = TempOASE->getBase()->IgnoreParenImpCasts();
10669 }
10670
10671 // If we have a single element, we don't need to add the implicit lengths.
10672 if (!SingleElement) {
10673 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) {
10674 // Has implicit length 1!
10675 ArraySizes.push_back(llvm::APSInt::get(1));
10676 Base = TempASE->getBase()->IgnoreParenImpCasts();
10677 }
10678 }
10679
10680 // This array section can be privatized as a single value or as a constant
10681 // sized array.
10682 return true;
10683}
10684
Alexey Bataeve3727102018-04-18 15:57:46 +000010685static bool actOnOMPReductionKindClause(
Alexey Bataev169d96a2017-07-18 20:17:46 +000010686 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind,
10687 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
10688 SourceLocation ColonLoc, SourceLocation EndLoc,
10689 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010690 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010691 DeclarationName DN = ReductionId.getName();
10692 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator();
Alexey Bataevc5e02582014-06-16 07:08:35 +000010693 BinaryOperatorKind BOK = BO_Comma;
10694
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010695 ASTContext &Context = S.Context;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010696 // OpenMP [2.14.3.6, reduction clause]
10697 // C
10698 // reduction-identifier is either an identifier or one of the following
10699 // operators: +, -, *, &, |, ^, && and ||
10700 // C++
10701 // reduction-identifier is either an id-expression or one of the following
10702 // operators: +, -, *, &, |, ^, && and ||
Alexey Bataevc5e02582014-06-16 07:08:35 +000010703 switch (OOK) {
10704 case OO_Plus:
10705 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010706 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010707 break;
10708 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010709 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010710 break;
10711 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010712 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010713 break;
10714 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010715 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010716 break;
10717 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010718 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010719 break;
10720 case OO_AmpAmp:
10721 BOK = BO_LAnd;
10722 break;
10723 case OO_PipePipe:
10724 BOK = BO_LOr;
10725 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010726 case OO_New:
10727 case OO_Delete:
10728 case OO_Array_New:
10729 case OO_Array_Delete:
10730 case OO_Slash:
10731 case OO_Percent:
10732 case OO_Tilde:
10733 case OO_Exclaim:
10734 case OO_Equal:
10735 case OO_Less:
10736 case OO_Greater:
10737 case OO_LessEqual:
10738 case OO_GreaterEqual:
10739 case OO_PlusEqual:
10740 case OO_MinusEqual:
10741 case OO_StarEqual:
10742 case OO_SlashEqual:
10743 case OO_PercentEqual:
10744 case OO_CaretEqual:
10745 case OO_AmpEqual:
10746 case OO_PipeEqual:
10747 case OO_LessLess:
10748 case OO_GreaterGreater:
10749 case OO_LessLessEqual:
10750 case OO_GreaterGreaterEqual:
10751 case OO_EqualEqual:
10752 case OO_ExclaimEqual:
Richard Smithd30b23d2017-12-01 02:13:10 +000010753 case OO_Spaceship:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010754 case OO_PlusPlus:
10755 case OO_MinusMinus:
10756 case OO_Comma:
10757 case OO_ArrowStar:
10758 case OO_Arrow:
10759 case OO_Call:
10760 case OO_Subscript:
10761 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +000010762 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010763 case NUM_OVERLOADED_OPERATORS:
10764 llvm_unreachable("Unexpected reduction identifier");
10765 case OO_None:
Alexey Bataeve3727102018-04-18 15:57:46 +000010766 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010767 if (II->isStr("max"))
10768 BOK = BO_GT;
10769 else if (II->isStr("min"))
10770 BOK = BO_LT;
10771 }
10772 break;
10773 }
10774 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010775 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +000010776 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataev4d4624c2017-07-20 16:47:47 +000010777 else
10778 ReductionIdRange.setBegin(ReductionId.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +000010779 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +000010780
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010781 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
10782 bool FirstIter = true;
Alexey Bataeve3727102018-04-18 15:57:46 +000010783 for (Expr *RefExpr : VarList) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010784 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +000010785 // OpenMP [2.1, C/C++]
10786 // A list item is a variable or array section, subject to the restrictions
10787 // specified in Section 2.4 on page 42 and in each of the sections
10788 // describing clauses and directives for which a list appears.
10789 // OpenMP [2.14.3.3, Restrictions, p.1]
10790 // A variable that is part of another variable (as an array or
10791 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010792 if (!FirstIter && IR != ER)
10793 ++IR;
10794 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010795 SourceLocation ELoc;
10796 SourceRange ERange;
10797 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010798 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
Alexey Bataev60da77e2016-02-29 05:54:20 +000010799 /*AllowArraySection=*/true);
10800 if (Res.second) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010801 // Try to find 'declare reduction' corresponding construct before using
10802 // builtin/overloaded operators.
10803 QualType Type = Context.DependentTy;
10804 CXXCastPath BasePath;
10805 ExprResult DeclareReductionRef = buildDeclareReductionRef(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010806 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010807 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010808 Expr *ReductionOp = nullptr;
10809 if (S.CurContext->isDependentContext() &&
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010810 (DeclareReductionRef.isUnset() ||
10811 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010812 ReductionOp = DeclareReductionRef.get();
10813 // It will be analyzed later.
10814 RD.push(RefExpr, ReductionOp);
Alexey Bataevc5e02582014-06-16 07:08:35 +000010815 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000010816 ValueDecl *D = Res.first;
10817 if (!D)
10818 continue;
10819
Alexey Bataev88202be2017-07-27 13:20:36 +000010820 Expr *TaskgroupDescriptor = nullptr;
Alexey Bataeva1764212015-09-30 09:22:36 +000010821 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010822 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
10823 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
Alexey Bataeve3727102018-04-18 15:57:46 +000010824 if (ASE) {
Alexey Bataev31300ed2016-02-04 11:27:03 +000010825 Type = ASE->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010826 } else if (OASE) {
10827 QualType BaseType =
10828 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
10829 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
Alexey Bataeva1764212015-09-30 09:22:36 +000010830 Type = ATy->getElementType();
10831 else
10832 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +000010833 Type = Type.getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010834 } else {
Alexey Bataev60da77e2016-02-29 05:54:20 +000010835 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
Alexey Bataeve3727102018-04-18 15:57:46 +000010836 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000010837 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +000010838
Alexey Bataevc5e02582014-06-16 07:08:35 +000010839 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
10840 // A variable that appears in a private clause must not have an incomplete
10841 // type or a reference type.
Joel E. Denny3cabf732018-06-28 19:54:49 +000010842 if (S.RequireCompleteType(ELoc, D->getType(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010843 diag::err_omp_reduction_incomplete_type))
Alexey Bataevc5e02582014-06-16 07:08:35 +000010844 continue;
10845 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +000010846 // A list item that appears in a reduction clause must not be
10847 // const-qualified.
10848 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataev169d96a2017-07-18 20:17:46 +000010849 S.Diag(ELoc, diag::err_omp_const_reduction_list_item) << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010850 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010851 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10852 VarDecl::DeclarationOnly;
10853 S.Diag(D->getLocation(),
10854 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +000010855 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +000010856 }
Alexey Bataevc5e02582014-06-16 07:08:35 +000010857 continue;
10858 }
Alexey Bataevbc529672018-09-28 19:33:14 +000010859
10860 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective();
Alexey Bataevc5e02582014-06-16 07:08:35 +000010861 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
10862 // If a list-item is a reference type then it must bind to the same object
10863 // for all threads of the team.
Alexey Bataevbc529672018-09-28 19:33:14 +000010864 if (!ASE && !OASE) {
10865 if (VD) {
10866 VarDecl *VDDef = VD->getDefinition();
10867 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
10868 DSARefChecker Check(Stack);
10869 if (Check.Visit(VDDef->getInit())) {
10870 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg)
10871 << getOpenMPClauseName(ClauseKind) << ERange;
10872 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
10873 continue;
10874 }
Alexey Bataeva1764212015-09-30 09:22:36 +000010875 }
Alexey Bataevc5e02582014-06-16 07:08:35 +000010876 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010877
Alexey Bataevbc529672018-09-28 19:33:14 +000010878 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
10879 // in a Construct]
10880 // Variables with the predetermined data-sharing attributes may not be
10881 // listed in data-sharing attributes clauses, except for the cases
10882 // listed below. For these exceptions only, listing a predetermined
10883 // variable in a data-sharing attribute clause is allowed and overrides
10884 // the variable's predetermined data-sharing attributes.
10885 // OpenMP [2.14.3.6, Restrictions, p.3]
10886 // Any number of reduction clauses can be specified on the directive,
10887 // but a list item can appear only once in the reduction clauses for that
10888 // directive.
10889 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false);
10890 if (DVar.CKind == OMPC_reduction) {
10891 S.Diag(ELoc, diag::err_omp_once_referenced)
10892 << getOpenMPClauseName(ClauseKind);
10893 if (DVar.RefExpr)
10894 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
10895 continue;
10896 }
10897 if (DVar.CKind != OMPC_unknown) {
10898 S.Diag(ELoc, diag::err_omp_wrong_dsa)
10899 << getOpenMPClauseName(DVar.CKind)
10900 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataeve3727102018-04-18 15:57:46 +000010901 reportOriginalDsa(S, Stack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010902 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +000010903 }
Alexey Bataevbc529672018-09-28 19:33:14 +000010904
10905 // OpenMP [2.14.3.6, Restrictions, p.1]
10906 // A list item that appears in a reduction clause of a worksharing
10907 // construct must be shared in the parallel regions to which any of the
10908 // worksharing regions arising from the worksharing construct bind.
10909 if (isOpenMPWorksharingDirective(CurrDir) &&
10910 !isOpenMPParallelDirective(CurrDir) &&
10911 !isOpenMPTeamsDirective(CurrDir)) {
10912 DVar = Stack->getImplicitDSA(D, true);
10913 if (DVar.CKind != OMPC_shared) {
10914 S.Diag(ELoc, diag::err_omp_required_access)
10915 << getOpenMPClauseName(OMPC_reduction)
10916 << getOpenMPClauseName(OMPC_shared);
10917 reportOriginalDsa(S, Stack, D, DVar);
10918 continue;
10919 }
10920 }
Alexey Bataevf29276e2014-06-18 04:14:57 +000010921 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010922
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010923 // Try to find 'declare reduction' corresponding construct before using
10924 // builtin/overloaded operators.
10925 CXXCastPath BasePath;
10926 ExprResult DeclareReductionRef = buildDeclareReductionRef(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010927 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010928 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
10929 if (DeclareReductionRef.isInvalid())
10930 continue;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010931 if (S.CurContext->isDependentContext() &&
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010932 (DeclareReductionRef.isUnset() ||
10933 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010934 RD.push(RefExpr, DeclareReductionRef.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010935 continue;
10936 }
10937 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
10938 // Not allowed reduction identifier is found.
Stephen Kellyf2ceec42018-08-09 21:08:08 +000010939 S.Diag(ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010940 diag::err_omp_unknown_reduction_identifier)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010941 << Type << ReductionIdRange;
10942 continue;
10943 }
10944
10945 // OpenMP [2.14.3.6, reduction clause, Restrictions]
10946 // The type of a list item that appears in a reduction clause must be valid
10947 // for the reduction-identifier. For a max or min reduction in C, the type
10948 // of the list item must be an allowed arithmetic data type: char, int,
10949 // float, double, or _Bool, possibly modified with long, short, signed, or
10950 // unsigned. For a max or min reduction in C++, the type of the list item
10951 // must be an allowed arithmetic data type: char, wchar_t, int, float,
10952 // double, or bool, possibly modified with long, short, signed, or unsigned.
10953 if (DeclareReductionRef.isUnset()) {
10954 if ((BOK == BO_GT || BOK == BO_LT) &&
10955 !(Type->isScalarType() ||
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010956 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
10957 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
Alexey Bataev169d96a2017-07-18 20:17:46 +000010958 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010959 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010960 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10961 VarDecl::DeclarationOnly;
10962 S.Diag(D->getLocation(),
10963 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010964 << D;
10965 }
10966 continue;
10967 }
10968 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010969 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) {
Alexey Bataev169d96a2017-07-18 20:17:46 +000010970 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg)
10971 << getOpenMPClauseName(ClauseKind);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010972 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010973 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10974 VarDecl::DeclarationOnly;
10975 S.Diag(D->getLocation(),
10976 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010977 << D;
10978 }
10979 continue;
10980 }
10981 }
10982
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010983 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010984 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs",
10985 D->hasAttrs() ? &D->getAttrs() : nullptr);
10986 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(),
10987 D->hasAttrs() ? &D->getAttrs() : nullptr);
10988 QualType PrivateTy = Type;
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010989
10990 // Try if we can determine constant lengths for all array sections and avoid
10991 // the VLA.
10992 bool ConstantLengthOASE = false;
10993 if (OASE) {
10994 bool SingleElement;
10995 llvm::SmallVector<llvm::APSInt, 4> ArraySizes;
Alexey Bataeve3727102018-04-18 15:57:46 +000010996 ConstantLengthOASE = checkOMPArraySectionConstantForReduction(
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010997 Context, OASE, SingleElement, ArraySizes);
10998
10999 // If we don't have a single element, we must emit a constant array type.
11000 if (ConstantLengthOASE && !SingleElement) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011001 for (llvm::APSInt &Size : ArraySizes)
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011002 PrivateTy = Context.getConstantArrayType(
11003 PrivateTy, Size, ArrayType::Normal, /*IndexTypeQuals=*/0);
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011004 }
11005 }
11006
11007 if ((OASE && !ConstantLengthOASE) ||
Jonas Hahnfeld96087f32017-11-02 13:30:42 +000011008 (!OASE && !ASE &&
Alexey Bataev60da77e2016-02-29 05:54:20 +000011009 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Jonas Hahnfeld87d44262017-11-18 21:00:46 +000011010 if (!Context.getTargetInfo().isVLASupported() &&
11011 S.shouldDiagnoseTargetSupportFromOpenMP()) {
11012 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
11013 S.Diag(ELoc, diag::note_vla_unsupported);
11014 continue;
11015 }
David Majnemer9d168222016-08-05 17:44:54 +000011016 // For arrays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011017 // Create pseudo array type for private copy. The size for this array will
11018 // be generated during codegen.
11019 // For array subscripts or single variables Private Ty is the same as Type
11020 // (type of the variable or single array element).
11021 PrivateTy = Context.getVariableArrayType(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011022 Type,
Alexey Bataevd070a582017-10-25 15:54:04 +000011023 new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue),
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011024 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +000011025 } else if (!ASE && !OASE &&
Alexey Bataeve3727102018-04-18 15:57:46 +000011026 Context.getAsArrayType(D->getType().getNonReferenceType())) {
Alexey Bataev60da77e2016-02-29 05:54:20 +000011027 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000011028 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011029 // Private copy.
Alexey Bataeve3727102018-04-18 15:57:46 +000011030 VarDecl *PrivateVD =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000011031 buildVarDecl(S, ELoc, PrivateTy, D->getName(),
11032 D->hasAttrs() ? &D->getAttrs() : nullptr,
11033 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011034 // Add initializer for private variable.
11035 Expr *Init = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000011036 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc);
11037 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011038 if (DeclareReductionRef.isUsable()) {
11039 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
11040 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
11041 if (DRD->getInitializer()) {
11042 Init = DRDRef;
11043 RHSVD->setInit(DRDRef);
11044 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011045 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011046 } else {
11047 switch (BOK) {
11048 case BO_Add:
11049 case BO_Xor:
11050 case BO_Or:
11051 case BO_LOr:
11052 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
11053 if (Type->isScalarType() || Type->isAnyComplexType())
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011054 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011055 break;
11056 case BO_Mul:
11057 case BO_LAnd:
11058 if (Type->isScalarType() || Type->isAnyComplexType()) {
11059 // '*' and '&&' reduction ops - initializer is '1'.
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011060 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +000011061 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011062 break;
11063 case BO_And: {
11064 // '&' reduction op - initializer is '~0'.
11065 QualType OrigType = Type;
11066 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
11067 Type = ComplexTy->getElementType();
11068 if (Type->isRealFloatingType()) {
11069 llvm::APFloat InitValue =
11070 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
11071 /*isIEEE=*/true);
11072 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
11073 Type, ELoc);
11074 } else if (Type->isScalarType()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011075 uint64_t Size = Context.getTypeSize(Type);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011076 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
11077 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
11078 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
11079 }
11080 if (Init && OrigType->isAnyComplexType()) {
11081 // Init = 0xFFFF + 0xFFFFi;
11082 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011083 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011084 }
11085 Type = OrigType;
11086 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011087 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011088 case BO_LT:
11089 case BO_GT: {
11090 // 'min' reduction op - initializer is 'Largest representable number in
11091 // the reduction list item type'.
11092 // 'max' reduction op - initializer is 'Least representable number in
11093 // the reduction list item type'.
11094 if (Type->isIntegerType() || Type->isPointerType()) {
11095 bool IsSigned = Type->hasSignedIntegerRepresentation();
Alexey Bataeve3727102018-04-18 15:57:46 +000011096 uint64_t Size = Context.getTypeSize(Type);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011097 QualType IntTy =
11098 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
11099 llvm::APInt InitValue =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011100 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
11101 : llvm::APInt::getMinValue(Size)
11102 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
11103 : llvm::APInt::getMaxValue(Size);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011104 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
11105 if (Type->isPointerType()) {
11106 // Cast to pointer type.
Alexey Bataeve3727102018-04-18 15:57:46 +000011107 ExprResult CastExpr = S.BuildCStyleCastExpr(
Alexey Bataevd070a582017-10-25 15:54:04 +000011108 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011109 if (CastExpr.isInvalid())
11110 continue;
11111 Init = CastExpr.get();
11112 }
11113 } else if (Type->isRealFloatingType()) {
11114 llvm::APFloat InitValue = llvm::APFloat::getLargest(
11115 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
11116 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
11117 Type, ELoc);
11118 }
11119 break;
11120 }
11121 case BO_PtrMemD:
11122 case BO_PtrMemI:
11123 case BO_MulAssign:
11124 case BO_Div:
11125 case BO_Rem:
11126 case BO_Sub:
11127 case BO_Shl:
11128 case BO_Shr:
11129 case BO_LE:
11130 case BO_GE:
11131 case BO_EQ:
11132 case BO_NE:
Richard Smithc70f1d62017-12-14 15:16:18 +000011133 case BO_Cmp:
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011134 case BO_AndAssign:
11135 case BO_XorAssign:
11136 case BO_OrAssign:
11137 case BO_Assign:
11138 case BO_AddAssign:
11139 case BO_SubAssign:
11140 case BO_DivAssign:
11141 case BO_RemAssign:
11142 case BO_ShlAssign:
11143 case BO_ShrAssign:
11144 case BO_Comma:
11145 llvm_unreachable("Unexpected reduction operation");
11146 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011147 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011148 if (Init && DeclareReductionRef.isUnset())
11149 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false);
11150 else if (!Init)
11151 S.ActOnUninitializedDecl(RHSVD);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011152 if (RHSVD->isInvalidDecl())
11153 continue;
11154 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011155 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible)
11156 << Type << ReductionIdRange;
11157 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
11158 VarDecl::DeclarationOnly;
11159 S.Diag(D->getLocation(),
11160 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +000011161 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011162 continue;
11163 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011164 // Store initializer for single element in private copy. Will be used during
11165 // codegen.
11166 PrivateVD->setInit(RHSVD->getInit());
11167 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataeve3727102018-04-18 15:57:46 +000011168 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011169 ExprResult ReductionOp;
11170 if (DeclareReductionRef.isUsable()) {
11171 QualType RedTy = DeclareReductionRef.get()->getType();
11172 QualType PtrRedTy = Context.getPointerType(RedTy);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011173 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
11174 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011175 if (!BasePath.empty()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011176 LHS = S.DefaultLvalueConversion(LHS.get());
11177 RHS = S.DefaultLvalueConversion(RHS.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011178 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
11179 CK_UncheckedDerivedToBase, LHS.get(),
11180 &BasePath, LHS.get()->getValueKind());
11181 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
11182 CK_UncheckedDerivedToBase, RHS.get(),
11183 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011184 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011185 FunctionProtoType::ExtProtoInfo EPI;
11186 QualType Params[] = {PtrRedTy, PtrRedTy};
11187 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
11188 auto *OVE = new (Context) OpaqueValueExpr(
11189 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011190 S.DefaultLvalueConversion(DeclareReductionRef.get()).get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011191 Expr *Args[] = {LHS.get(), RHS.get()};
11192 ReductionOp = new (Context)
11193 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
11194 } else {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011195 ReductionOp = S.BuildBinOp(
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011196 Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, LHSDRE, RHSDRE);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011197 if (ReductionOp.isUsable()) {
11198 if (BOK != BO_LT && BOK != BO_GT) {
11199 ReductionOp =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011200 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011201 BO_Assign, LHSDRE, ReductionOp.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011202 } else {
Alexey Bataevd070a582017-10-25 15:54:04 +000011203 auto *ConditionalOp = new (Context)
11204 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE,
11205 Type, VK_LValue, OK_Ordinary);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011206 ReductionOp =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011207 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011208 BO_Assign, LHSDRE, ConditionalOp);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011209 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000011210 if (ReductionOp.isUsable())
11211 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011212 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000011213 if (!ReductionOp.isUsable())
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011214 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +000011215 }
11216
Alexey Bataevfa312f32017-07-21 18:48:21 +000011217 // OpenMP [2.15.4.6, Restrictions, p.2]
11218 // A list item that appears in an in_reduction clause of a task construct
11219 // must appear in a task_reduction clause of a construct associated with a
11220 // taskgroup region that includes the participating task in its taskgroup
11221 // set. The construct associated with the innermost region that meets this
11222 // condition must specify the same reduction-identifier as the in_reduction
11223 // clause.
11224 if (ClauseKind == OMPC_in_reduction) {
Alexey Bataevfa312f32017-07-21 18:48:21 +000011225 SourceRange ParentSR;
11226 BinaryOperatorKind ParentBOK;
11227 const Expr *ParentReductionOp;
Alexey Bataev88202be2017-07-27 13:20:36 +000011228 Expr *ParentBOKTD, *ParentReductionOpTD;
Alexey Bataevf189cb72017-07-24 14:52:13 +000011229 DSAStackTy::DSAVarData ParentBOKDSA =
Alexey Bataev88202be2017-07-27 13:20:36 +000011230 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK,
11231 ParentBOKTD);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011232 DSAStackTy::DSAVarData ParentReductionOpDSA =
Alexey Bataev88202be2017-07-27 13:20:36 +000011233 Stack->getTopMostTaskgroupReductionData(
11234 D, ParentSR, ParentReductionOp, ParentReductionOpTD);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011235 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown;
11236 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown;
11237 if (!IsParentBOK && !IsParentReductionOp) {
11238 S.Diag(ELoc, diag::err_omp_in_reduction_not_task_reduction);
11239 continue;
11240 }
Alexey Bataevfa312f32017-07-21 18:48:21 +000011241 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) ||
11242 (DeclareReductionRef.isUsable() && IsParentBOK) || BOK != ParentBOK ||
11243 IsParentReductionOp) {
11244 bool EmitError = true;
11245 if (IsParentReductionOp && DeclareReductionRef.isUsable()) {
11246 llvm::FoldingSetNodeID RedId, ParentRedId;
11247 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true);
11248 DeclareReductionRef.get()->Profile(RedId, Context,
11249 /*Canonical=*/true);
11250 EmitError = RedId != ParentRedId;
11251 }
11252 if (EmitError) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011253 S.Diag(ReductionId.getBeginLoc(),
Alexey Bataevfa312f32017-07-21 18:48:21 +000011254 diag::err_omp_reduction_identifier_mismatch)
11255 << ReductionIdRange << RefExpr->getSourceRange();
11256 S.Diag(ParentSR.getBegin(),
11257 diag::note_omp_previous_reduction_identifier)
Alexey Bataevf189cb72017-07-24 14:52:13 +000011258 << ParentSR
11259 << (IsParentBOK ? ParentBOKDSA.RefExpr
11260 : ParentReductionOpDSA.RefExpr)
11261 ->getSourceRange();
Alexey Bataevfa312f32017-07-21 18:48:21 +000011262 continue;
11263 }
11264 }
Alexey Bataev88202be2017-07-27 13:20:36 +000011265 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD;
11266 assert(TaskgroupDescriptor && "Taskgroup descriptor must be defined.");
Alexey Bataevfa312f32017-07-21 18:48:21 +000011267 }
11268
Alexey Bataev60da77e2016-02-29 05:54:20 +000011269 DeclRefExpr *Ref = nullptr;
11270 Expr *VarsExpr = RefExpr->IgnoreParens();
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011271 if (!VD && !S.CurContext->isDependentContext()) {
Alexey Bataev60da77e2016-02-29 05:54:20 +000011272 if (ASE || OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011273 TransformExprToCaptures RebuildToCapture(S, D);
Alexey Bataev60da77e2016-02-29 05:54:20 +000011274 VarsExpr =
11275 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
11276 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +000011277 } else {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011278 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +000011279 }
Alexey Bataeve3727102018-04-18 15:57:46 +000011280 if (!S.isOpenMPCapturedDecl(D)) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011281 RD.ExprCaptures.emplace_back(Ref->getDecl());
Alexey Bataev5a3af132016-03-29 08:58:54 +000011282 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011283 ExprResult RefRes = S.DefaultLvalueConversion(Ref);
Alexey Bataev5a3af132016-03-29 08:58:54 +000011284 if (!RefRes.isUsable())
11285 continue;
11286 ExprResult PostUpdateRes =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011287 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
11288 RefRes.get());
Alexey Bataev5a3af132016-03-29 08:58:54 +000011289 if (!PostUpdateRes.isUsable())
11290 continue;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011291 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
11292 Stack->getCurrentDirective() == OMPD_taskgroup) {
11293 S.Diag(RefExpr->getExprLoc(),
11294 diag::err_omp_reduction_non_addressable_expression)
Alexey Bataevbcd0ae02017-07-11 19:16:44 +000011295 << RefExpr->getSourceRange();
11296 continue;
11297 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011298 RD.ExprPostUpdates.emplace_back(
11299 S.IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +000011300 }
11301 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000011302 }
Alexey Bataev169d96a2017-07-18 20:17:46 +000011303 // All reduction items are still marked as reduction (to do not increase
11304 // code base size).
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011305 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011306 if (CurrDir == OMPD_taskgroup) {
11307 if (DeclareReductionRef.isUsable())
Alexey Bataev3b1b8952017-07-25 15:53:26 +000011308 Stack->addTaskgroupReductionData(D, ReductionIdRange,
11309 DeclareReductionRef.get());
Alexey Bataevf189cb72017-07-24 14:52:13 +000011310 else
Alexey Bataev3b1b8952017-07-25 15:53:26 +000011311 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011312 }
Alexey Bataev88202be2017-07-27 13:20:36 +000011313 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(),
11314 TaskgroupDescriptor);
Alexey Bataevc5e02582014-06-16 07:08:35 +000011315 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011316 return RD.Vars.empty();
11317}
Alexey Bataevc5e02582014-06-16 07:08:35 +000011318
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011319OMPClause *Sema::ActOnOpenMPReductionClause(
11320 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11321 SourceLocation ColonLoc, SourceLocation EndLoc,
11322 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11323 ArrayRef<Expr *> UnresolvedReductions) {
11324 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000011325 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList,
Alexey Bataev169d96a2017-07-18 20:17:46 +000011326 StartLoc, LParenLoc, ColonLoc, EndLoc,
11327 ReductionIdScopeSpec, ReductionId,
11328 UnresolvedReductions, RD))
Alexey Bataevc5e02582014-06-16 07:08:35 +000011329 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +000011330
Alexey Bataevc5e02582014-06-16 07:08:35 +000011331 return OMPReductionClause::Create(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011332 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11333 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
11334 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
11335 buildPreInits(Context, RD.ExprCaptures),
11336 buildPostUpdate(*this, RD.ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +000011337}
11338
Alexey Bataev169d96a2017-07-18 20:17:46 +000011339OMPClause *Sema::ActOnOpenMPTaskReductionClause(
11340 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11341 SourceLocation ColonLoc, SourceLocation EndLoc,
11342 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11343 ArrayRef<Expr *> UnresolvedReductions) {
11344 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000011345 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList,
11346 StartLoc, LParenLoc, ColonLoc, EndLoc,
11347 ReductionIdScopeSpec, ReductionId,
Alexey Bataev169d96a2017-07-18 20:17:46 +000011348 UnresolvedReductions, RD))
11349 return nullptr;
11350
11351 return OMPTaskReductionClause::Create(
11352 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11353 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
11354 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
11355 buildPreInits(Context, RD.ExprCaptures),
11356 buildPostUpdate(*this, RD.ExprPostUpdates));
11357}
11358
Alexey Bataevfa312f32017-07-21 18:48:21 +000011359OMPClause *Sema::ActOnOpenMPInReductionClause(
11360 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11361 SourceLocation ColonLoc, SourceLocation EndLoc,
11362 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11363 ArrayRef<Expr *> UnresolvedReductions) {
11364 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000011365 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList,
Alexey Bataevfa312f32017-07-21 18:48:21 +000011366 StartLoc, LParenLoc, ColonLoc, EndLoc,
11367 ReductionIdScopeSpec, ReductionId,
11368 UnresolvedReductions, RD))
11369 return nullptr;
11370
11371 return OMPInReductionClause::Create(
11372 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11373 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
Alexey Bataev88202be2017-07-27 13:20:36 +000011374 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors,
Alexey Bataevfa312f32017-07-21 18:48:21 +000011375 buildPreInits(Context, RD.ExprCaptures),
11376 buildPostUpdate(*this, RD.ExprPostUpdates));
11377}
11378
Alexey Bataevecba70f2016-04-12 11:02:11 +000011379bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
11380 SourceLocation LinLoc) {
11381 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
11382 LinKind == OMPC_LINEAR_unknown) {
11383 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
11384 return true;
11385 }
11386 return false;
11387}
11388
Alexey Bataeve3727102018-04-18 15:57:46 +000011389bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc,
Alexey Bataevecba70f2016-04-12 11:02:11 +000011390 OpenMPLinearClauseKind LinKind,
11391 QualType Type) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011392 const auto *VD = dyn_cast_or_null<VarDecl>(D);
Alexey Bataevecba70f2016-04-12 11:02:11 +000011393 // A variable must not have an incomplete type or a reference type.
11394 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
11395 return true;
11396 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
11397 !Type->isReferenceType()) {
11398 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
11399 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
11400 return true;
11401 }
11402 Type = Type.getNonReferenceType();
11403
11404 // A list item must not be const-qualified.
11405 if (Type.isConstant(Context)) {
11406 Diag(ELoc, diag::err_omp_const_variable)
11407 << getOpenMPClauseName(OMPC_linear);
11408 if (D) {
11409 bool IsDecl =
11410 !VD ||
11411 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
11412 Diag(D->getLocation(),
11413 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
11414 << D;
11415 }
11416 return true;
11417 }
11418
11419 // A list item must be of integral or pointer type.
11420 Type = Type.getUnqualifiedType().getCanonicalType();
11421 const auto *Ty = Type.getTypePtrOrNull();
11422 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
11423 !Ty->isPointerType())) {
11424 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
11425 if (D) {
11426 bool IsDecl =
11427 !VD ||
11428 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
11429 Diag(D->getLocation(),
11430 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
11431 << D;
11432 }
11433 return true;
11434 }
11435 return false;
11436}
11437
Alexey Bataev182227b2015-08-20 10:54:39 +000011438OMPClause *Sema::ActOnOpenMPLinearClause(
11439 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
11440 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
11441 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +000011442 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011443 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +000011444 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +000011445 SmallVector<Decl *, 4> ExprCaptures;
11446 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +000011447 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +000011448 LinKind = OMPC_LINEAR_val;
Alexey Bataeve3727102018-04-18 15:57:46 +000011449 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000011450 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011451 SourceLocation ELoc;
11452 SourceRange ERange;
11453 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011454 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011455 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +000011456 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000011457 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011458 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +000011459 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +000011460 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011461 ValueDecl *D = Res.first;
11462 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +000011463 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +000011464
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011465 QualType Type = D->getType();
11466 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +000011467
11468 // OpenMP [2.14.3.7, linear clause]
11469 // A list-item cannot appear in more than one linear clause.
11470 // A list-item that appears in a linear clause cannot appear in any
11471 // other data-sharing attribute clause.
Alexey Bataeve3727102018-04-18 15:57:46 +000011472 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexander Musman8dba6642014-04-22 13:09:42 +000011473 if (DVar.RefExpr) {
11474 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
11475 << getOpenMPClauseName(OMPC_linear);
Alexey Bataeve3727102018-04-18 15:57:46 +000011476 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +000011477 continue;
11478 }
11479
Alexey Bataevecba70f2016-04-12 11:02:11 +000011480 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +000011481 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +000011482 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +000011483
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011484 // Build private copy of original var.
Alexey Bataeve3727102018-04-18 15:57:46 +000011485 VarDecl *Private =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000011486 buildVarDecl(*this, ELoc, Type, D->getName(),
11487 D->hasAttrs() ? &D->getAttrs() : nullptr,
11488 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011489 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +000011490 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011491 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011492 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011493 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000011494 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev78849fb2016-03-09 09:49:00 +000011495 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011496 if (!isOpenMPCapturedDecl(D)) {
Alexey Bataev78849fb2016-03-09 09:49:00 +000011497 ExprCaptures.push_back(Ref->getDecl());
11498 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
11499 ExprResult RefRes = DefaultLvalueConversion(Ref);
11500 if (!RefRes.isUsable())
11501 continue;
11502 ExprResult PostUpdateRes =
11503 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
11504 SimpleRefExpr, RefRes.get());
11505 if (!PostUpdateRes.isUsable())
11506 continue;
11507 ExprPostUpdates.push_back(
11508 IgnoredValueConversions(PostUpdateRes.get()).get());
11509 }
11510 }
11511 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011512 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011513 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011514 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011515 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011516 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000011517 /*DirectInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011518 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011519
11520 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000011521 Vars.push_back((VD || CurContext->isDependentContext())
11522 ? RefExpr->IgnoreParens()
11523 : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011524 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +000011525 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +000011526 }
11527
11528 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000011529 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000011530
11531 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +000011532 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000011533 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
11534 !Step->isInstantiationDependent() &&
11535 !Step->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011536 SourceLocation StepLoc = Step->getBeginLoc();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +000011537 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +000011538 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000011539 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +000011540 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +000011541
Alexander Musman3276a272015-03-21 10:12:56 +000011542 // Build var to save the step value.
11543 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +000011544 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +000011545 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +000011546 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +000011547 ExprResult CalcStep =
11548 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +000011549 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +000011550
Alexander Musman8dba6642014-04-22 13:09:42 +000011551 // Warn about zero linear step (it would be probably better specified as
11552 // making corresponding variables 'const').
11553 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +000011554 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
11555 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +000011556 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
11557 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +000011558 if (!IsConstant && CalcStep.isUsable()) {
11559 // Calculate the step beforehand instead of doing this on each iteration.
11560 // (This is not used if the number of iterations may be kfold-ed).
11561 CalcStepExpr = CalcStep.get();
11562 }
Alexander Musman8dba6642014-04-22 13:09:42 +000011563 }
11564
Alexey Bataev182227b2015-08-20 10:54:39 +000011565 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
11566 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +000011567 StepExpr, CalcStepExpr,
11568 buildPreInits(Context, ExprCaptures),
11569 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +000011570}
11571
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011572static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
11573 Expr *NumIterations, Sema &SemaRef,
11574 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +000011575 // Walk the vars and build update/final expressions for the CodeGen.
11576 SmallVector<Expr *, 8> Updates;
11577 SmallVector<Expr *, 8> Finals;
11578 Expr *Step = Clause.getStep();
11579 Expr *CalcStep = Clause.getCalcStep();
11580 // OpenMP [2.14.3.7, linear clause]
11581 // If linear-step is not specified it is assumed to be 1.
Alexey Bataeve3727102018-04-18 15:57:46 +000011582 if (!Step)
Alexander Musman3276a272015-03-21 10:12:56 +000011583 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000011584 else if (CalcStep)
Alexander Musman3276a272015-03-21 10:12:56 +000011585 Step = cast<BinaryOperator>(CalcStep)->getLHS();
11586 bool HasErrors = false;
11587 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011588 auto CurPrivate = Clause.privates().begin();
Alexey Bataeve3727102018-04-18 15:57:46 +000011589 OpenMPLinearClauseKind LinKind = Clause.getModifier();
11590 for (Expr *RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011591 SourceLocation ELoc;
11592 SourceRange ERange;
11593 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011594 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange);
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011595 ValueDecl *D = Res.first;
11596 if (Res.second || !D) {
11597 Updates.push_back(nullptr);
11598 Finals.push_back(nullptr);
11599 HasErrors = true;
11600 continue;
11601 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011602 auto &&Info = Stack->isLoopControlVariable(D);
Alexey Bataev2b86f212017-11-29 21:31:48 +000011603 // OpenMP [2.15.11, distribute simd Construct]
11604 // A list item may not appear in a linear clause, unless it is the loop
11605 // iteration variable.
11606 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) &&
11607 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) {
11608 SemaRef.Diag(ELoc,
11609 diag::err_omp_linear_distribute_var_non_loop_iteration);
11610 Updates.push_back(nullptr);
11611 Finals.push_back(nullptr);
11612 HasErrors = true;
11613 continue;
11614 }
Alexander Musman3276a272015-03-21 10:12:56 +000011615 Expr *InitExpr = *CurInit;
11616
11617 // Build privatized reference to the current linear var.
David Majnemer9d168222016-08-05 17:44:54 +000011618 auto *DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011619 Expr *CapturedRef;
11620 if (LinKind == OMPC_LINEAR_uval)
11621 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
11622 else
11623 CapturedRef =
11624 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
11625 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
11626 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +000011627
11628 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011629 ExprResult Update;
Alexey Bataeve3727102018-04-18 15:57:46 +000011630 if (!Info.first)
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011631 Update =
Alexey Bataeve3727102018-04-18 15:57:46 +000011632 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011633 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011634 else
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011635 Update = *CurPrivate;
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011636 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +000011637 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +000011638
11639 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011640 ExprResult Final;
Alexey Bataeve3727102018-04-18 15:57:46 +000011641 if (!Info.first)
11642 Final =
11643 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
11644 InitExpr, NumIterations, Step, /*Subtract=*/false);
11645 else
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011646 Final = *CurPrivate;
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011647 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +000011648 /*DiscardedValue=*/true);
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011649
Alexander Musman3276a272015-03-21 10:12:56 +000011650 if (!Update.isUsable() || !Final.isUsable()) {
11651 Updates.push_back(nullptr);
11652 Finals.push_back(nullptr);
11653 HasErrors = true;
11654 } else {
11655 Updates.push_back(Update.get());
11656 Finals.push_back(Final.get());
11657 }
Richard Trieucc3949d2016-02-18 22:34:54 +000011658 ++CurInit;
11659 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +000011660 }
11661 Clause.setUpdates(Updates);
11662 Clause.setFinals(Finals);
11663 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +000011664}
11665
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011666OMPClause *Sema::ActOnOpenMPAlignedClause(
11667 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
11668 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011669 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +000011670 for (Expr *RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +000011671 assert(RefExpr && "NULL expr in OpenMP linear clause.");
11672 SourceLocation ELoc;
11673 SourceRange ERange;
11674 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011675 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev1efd1662016-03-29 10:59:56 +000011676 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011677 // It will be analyzed later.
11678 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011679 }
Alexey Bataev1efd1662016-03-29 10:59:56 +000011680 ValueDecl *D = Res.first;
11681 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011682 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011683
Alexey Bataev1efd1662016-03-29 10:59:56 +000011684 QualType QType = D->getType();
11685 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011686
11687 // OpenMP [2.8.1, simd construct, Restrictions]
11688 // The type of list items appearing in the aligned clause must be
11689 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011690 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011691 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +000011692 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011693 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +000011694 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011695 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +000011696 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011697 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +000011698 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011699 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +000011700 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011701 continue;
11702 }
11703
11704 // OpenMP [2.8.1, simd construct, Restrictions]
11705 // A list-item cannot appear in more than one aligned clause.
Alexey Bataeve3727102018-04-18 15:57:46 +000011706 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +000011707 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011708 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
11709 << getOpenMPClauseName(OMPC_aligned);
11710 continue;
11711 }
11712
Alexey Bataev1efd1662016-03-29 10:59:56 +000011713 DeclRefExpr *Ref = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000011714 if (!VD && isOpenMPCapturedDecl(D))
Alexey Bataev1efd1662016-03-29 10:59:56 +000011715 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
11716 Vars.push_back(DefaultFunctionArrayConversion(
11717 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
11718 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011719 }
11720
11721 // OpenMP [2.8.1, simd construct, Description]
11722 // The parameter of the aligned clause, alignment, must be a constant
11723 // positive integer expression.
11724 // If no optional parameter is specified, implementation-defined default
11725 // alignments for SIMD instructions on the target platforms are assumed.
11726 if (Alignment != nullptr) {
11727 ExprResult AlignResult =
11728 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
11729 if (AlignResult.isInvalid())
11730 return nullptr;
11731 Alignment = AlignResult.get();
11732 }
11733 if (Vars.empty())
11734 return nullptr;
11735
11736 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
11737 EndLoc, Vars, Alignment);
11738}
11739
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011740OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
11741 SourceLocation StartLoc,
11742 SourceLocation LParenLoc,
11743 SourceLocation EndLoc) {
11744 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011745 SmallVector<Expr *, 8> SrcExprs;
11746 SmallVector<Expr *, 8> DstExprs;
11747 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeve3727102018-04-18 15:57:46 +000011748 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000011749 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
11750 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011751 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000011752 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011753 SrcExprs.push_back(nullptr);
11754 DstExprs.push_back(nullptr);
11755 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011756 continue;
11757 }
11758
Alexey Bataeved09d242014-05-28 05:53:51 +000011759 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011760 // OpenMP [2.1, C/C++]
11761 // A list item is a variable name.
11762 // OpenMP [2.14.4.1, Restrictions, p.1]
11763 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeve3727102018-04-18 15:57:46 +000011764 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011765 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000011766 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
11767 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011768 continue;
11769 }
11770
11771 Decl *D = DE->getDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000011772 auto *VD = cast<VarDecl>(D);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011773
11774 QualType Type = VD->getType();
11775 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
11776 // It will be analyzed later.
11777 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011778 SrcExprs.push_back(nullptr);
11779 DstExprs.push_back(nullptr);
11780 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011781 continue;
11782 }
11783
11784 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
11785 // A list item that appears in a copyin clause must be threadprivate.
11786 if (!DSAStack->isThreadPrivate(VD)) {
11787 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +000011788 << getOpenMPClauseName(OMPC_copyin)
11789 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011790 continue;
11791 }
11792
11793 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
11794 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +000011795 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011796 // operator for the class type.
Alexey Bataeve3727102018-04-18 15:57:46 +000011797 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
11798 VarDecl *SrcVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011799 buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000011800 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011801 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011802 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +000011803 VarDecl *DstVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011804 buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst",
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000011805 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011806 DeclRefExpr *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011807 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011808 // For arrays generate assignment operation for single element and replace
11809 // it by the original array element in CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +000011810 ExprResult AssignmentOp =
11811 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr,
11812 PseudoSrcExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011813 if (AssignmentOp.isInvalid())
11814 continue;
11815 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
11816 /*DiscardedValue=*/true);
11817 if (AssignmentOp.isInvalid())
11818 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011819
11820 DSAStack->addDSA(VD, DE, OMPC_copyin);
11821 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011822 SrcExprs.push_back(PseudoSrcExpr);
11823 DstExprs.push_back(PseudoDstExpr);
11824 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011825 }
11826
Alexey Bataeved09d242014-05-28 05:53:51 +000011827 if (Vars.empty())
11828 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011829
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011830 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
11831 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011832}
11833
Alexey Bataevbae9a792014-06-27 10:37:06 +000011834OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
11835 SourceLocation StartLoc,
11836 SourceLocation LParenLoc,
11837 SourceLocation EndLoc) {
11838 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +000011839 SmallVector<Expr *, 8> SrcExprs;
11840 SmallVector<Expr *, 8> DstExprs;
11841 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeve3727102018-04-18 15:57:46 +000011842 for (Expr *RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +000011843 assert(RefExpr && "NULL expr in OpenMP linear clause.");
11844 SourceLocation ELoc;
11845 SourceRange ERange;
11846 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011847 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataeve122da12016-03-17 10:50:17 +000011848 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000011849 // It will be analyzed later.
11850 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000011851 SrcExprs.push_back(nullptr);
11852 DstExprs.push_back(nullptr);
11853 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011854 }
Alexey Bataeve122da12016-03-17 10:50:17 +000011855 ValueDecl *D = Res.first;
11856 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +000011857 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000011858
Alexey Bataeve122da12016-03-17 10:50:17 +000011859 QualType Type = D->getType();
11860 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011861
11862 // OpenMP [2.14.4.2, Restrictions, p.2]
11863 // A list item that appears in a copyprivate clause may not appear in a
11864 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +000011865 if (!VD || !DSAStack->isThreadPrivate(VD)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011866 DSAStackTy::DSAVarData DVar =
11867 DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataeva63048e2015-03-23 06:18:07 +000011868 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
11869 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000011870 Diag(ELoc, diag::err_omp_wrong_dsa)
11871 << getOpenMPClauseName(DVar.CKind)
11872 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000011873 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011874 continue;
11875 }
11876
11877 // OpenMP [2.11.4.2, Restrictions, p.1]
11878 // All list items that appear in a copyprivate clause must be either
11879 // threadprivate or private in the enclosing context.
11880 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +000011881 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011882 if (DVar.CKind == OMPC_shared) {
11883 Diag(ELoc, diag::err_omp_required_access)
11884 << getOpenMPClauseName(OMPC_copyprivate)
11885 << "threadprivate or private in the enclosing context";
Alexey Bataeve3727102018-04-18 15:57:46 +000011886 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011887 continue;
11888 }
11889 }
11890 }
11891
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011892 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000011893 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011894 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +000011895 << getOpenMPClauseName(OMPC_copyprivate) << Type
11896 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011897 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +000011898 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011899 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +000011900 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011901 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +000011902 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011903 continue;
11904 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +000011905
Alexey Bataevbae9a792014-06-27 10:37:06 +000011906 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
11907 // A variable of class type (or array thereof) that appears in a
11908 // copyin clause requires an accessible, unambiguous copy assignment
11909 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011910 Type = Context.getBaseElementType(Type.getNonReferenceType())
11911 .getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000011912 VarDecl *SrcVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011913 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src",
Alexey Bataeve122da12016-03-17 10:50:17 +000011914 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011915 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
11916 VarDecl *DstVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011917 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst",
Alexey Bataeve122da12016-03-17 10:50:17 +000011918 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011919 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
11920 ExprResult AssignmentOp = BuildBinOp(
11921 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000011922 if (AssignmentOp.isInvalid())
11923 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +000011924 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +000011925 /*DiscardedValue=*/true);
11926 if (AssignmentOp.isInvalid())
11927 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000011928
11929 // No need to mark vars as copyprivate, they are already threadprivate or
11930 // implicitly private.
Alexey Bataeve3727102018-04-18 15:57:46 +000011931 assert(VD || isOpenMPCapturedDecl(D));
Alexey Bataeve122da12016-03-17 10:50:17 +000011932 Vars.push_back(
11933 VD ? RefExpr->IgnoreParens()
11934 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +000011935 SrcExprs.push_back(PseudoSrcExpr);
11936 DstExprs.push_back(PseudoDstExpr);
11937 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +000011938 }
11939
11940 if (Vars.empty())
11941 return nullptr;
11942
Alexey Bataeva63048e2015-03-23 06:18:07 +000011943 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11944 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011945}
11946
Alexey Bataev6125da92014-07-21 11:26:11 +000011947OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
11948 SourceLocation StartLoc,
11949 SourceLocation LParenLoc,
11950 SourceLocation EndLoc) {
11951 if (VarList.empty())
11952 return nullptr;
11953
11954 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
11955}
Alexey Bataevdea47612014-07-23 07:46:59 +000011956
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011957OMPClause *
11958Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
11959 SourceLocation DepLoc, SourceLocation ColonLoc,
11960 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
11961 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +000011962 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011963 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +000011964 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000011965 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +000011966 return nullptr;
11967 }
11968 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011969 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
11970 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +000011971 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011972 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000011973 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
11974 /*Last=*/OMPC_DEPEND_unknown, Except)
11975 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011976 return nullptr;
11977 }
11978 SmallVector<Expr *, 8> Vars;
Alexey Bataev8b427062016-05-25 12:36:08 +000011979 DSAStackTy::OperatorOffsetTy OpsOffs;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011980 llvm::APSInt DepCounter(/*BitWidth=*/32);
11981 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
Alexey Bataevf138fda2018-08-13 19:04:24 +000011982 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) {
11983 if (const Expr *OrderedCountExpr =
11984 DSAStack->getParentOrderedRegionParam().first) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011985 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
11986 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011987 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011988 }
Alexey Bataeve3727102018-04-18 15:57:46 +000011989 for (Expr *RefExpr : VarList) {
Alexey Bataev17daedf2018-02-15 22:42:57 +000011990 assert(RefExpr && "NULL expr in OpenMP shared clause.");
11991 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
11992 // It will be analyzed later.
11993 Vars.push_back(RefExpr);
11994 continue;
11995 }
11996
11997 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataeve3727102018-04-18 15:57:46 +000011998 Expr *SimpleExpr = RefExpr->IgnoreParenCasts();
Alexey Bataev17daedf2018-02-15 22:42:57 +000011999 if (DepKind == OMPC_DEPEND_sink) {
Alexey Bataevf138fda2018-08-13 19:04:24 +000012000 if (DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000012001 DepCounter >= TotalDepCount) {
12002 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
12003 continue;
12004 }
12005 ++DepCounter;
12006 // OpenMP [2.13.9, Summary]
12007 // depend(dependence-type : vec), where dependence-type is:
12008 // 'sink' and where vec is the iteration vector, which has the form:
12009 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
12010 // where n is the value specified by the ordered clause in the loop
12011 // directive, xi denotes the loop iteration variable of the i-th nested
12012 // loop associated with the loop directive, and di is a constant
12013 // non-negative integer.
12014 if (CurContext->isDependentContext()) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012015 // It will be analyzed later.
12016 Vars.push_back(RefExpr);
12017 continue;
12018 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012019 SimpleExpr = SimpleExpr->IgnoreImplicit();
12020 OverloadedOperatorKind OOK = OO_None;
12021 SourceLocation OOLoc;
12022 Expr *LHS = SimpleExpr;
12023 Expr *RHS = nullptr;
12024 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
12025 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
12026 OOLoc = BO->getOperatorLoc();
12027 LHS = BO->getLHS()->IgnoreParenImpCasts();
12028 RHS = BO->getRHS()->IgnoreParenImpCasts();
12029 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
12030 OOK = OCE->getOperator();
12031 OOLoc = OCE->getOperatorLoc();
12032 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
12033 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
12034 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
12035 OOK = MCE->getMethodDecl()
12036 ->getNameInfo()
12037 .getName()
12038 .getCXXOverloadedOperator();
12039 OOLoc = MCE->getCallee()->getExprLoc();
12040 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
12041 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012042 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012043 SourceLocation ELoc;
12044 SourceRange ERange;
Alexey Bataevbc529672018-09-28 19:33:14 +000012045 auto Res = getPrivateItem(*this, LHS, ELoc, ERange);
Alexey Bataev17daedf2018-02-15 22:42:57 +000012046 if (Res.second) {
12047 // It will be analyzed later.
12048 Vars.push_back(RefExpr);
12049 }
12050 ValueDecl *D = Res.first;
12051 if (!D)
12052 continue;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012053
Alexey Bataev17daedf2018-02-15 22:42:57 +000012054 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
12055 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
12056 continue;
12057 }
12058 if (RHS) {
12059 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
12060 RHS, OMPC_depend, /*StrictlyPositive=*/false);
12061 if (RHSRes.isInvalid())
12062 continue;
12063 }
12064 if (!CurContext->isDependentContext() &&
Alexey Bataevf138fda2018-08-13 19:04:24 +000012065 DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000012066 DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012067 const ValueDecl *VD =
Alexey Bataev17daedf2018-02-15 22:42:57 +000012068 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue());
Alexey Bataeve3727102018-04-18 15:57:46 +000012069 if (VD)
Alexey Bataev17daedf2018-02-15 22:42:57 +000012070 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
12071 << 1 << VD;
Alexey Bataeve3727102018-04-18 15:57:46 +000012072 else
Alexey Bataev17daedf2018-02-15 22:42:57 +000012073 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0;
Alexey Bataev17daedf2018-02-15 22:42:57 +000012074 continue;
12075 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012076 OpsOffs.emplace_back(RHS, OOK);
Alexey Bataev17daedf2018-02-15 22:42:57 +000012077 } else {
12078 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
12079 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
12080 (ASE &&
12081 !ASE->getBase()->getType().getNonReferenceType()->isPointerType() &&
12082 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
12083 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
12084 << RefExpr->getSourceRange();
12085 continue;
12086 }
12087 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
12088 getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
12089 ExprResult Res =
12090 CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RefExpr->IgnoreParenImpCasts());
12091 getDiagnostics().setSuppressAllDiagnostics(Suppress);
12092 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr)) {
12093 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
12094 << RefExpr->getSourceRange();
12095 continue;
12096 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012097 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012098 Vars.push_back(RefExpr->IgnoreParenImpCasts());
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012099 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012100
12101 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
12102 TotalDepCount > VarList.size() &&
Alexey Bataevf138fda2018-08-13 19:04:24 +000012103 DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000012104 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) {
12105 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
12106 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
12107 }
12108 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
12109 Vars.empty())
12110 return nullptr;
12111
Alexey Bataev8b427062016-05-25 12:36:08 +000012112 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataevf138fda2018-08-13 19:04:24 +000012113 DepKind, DepLoc, ColonLoc, Vars,
12114 TotalDepCount.getZExtValue());
Alexey Bataev17daedf2018-02-15 22:42:57 +000012115 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) &&
12116 DSAStack->isParentOrderedRegion())
Alexey Bataev8b427062016-05-25 12:36:08 +000012117 DSAStack->addDoacrossDependClause(C, OpsOffs);
12118 return C;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012119}
Michael Wonge710d542015-08-07 16:16:36 +000012120
12121OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
12122 SourceLocation LParenLoc,
12123 SourceLocation EndLoc) {
12124 Expr *ValExpr = Device;
Alexey Bataev931e19b2017-10-02 16:32:39 +000012125 Stmt *HelperValStmt = nullptr;
Michael Wonge710d542015-08-07 16:16:36 +000012126
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012127 // OpenMP [2.9.1, Restrictions]
12128 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000012129 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
Alexey Bataeva0569352015-12-01 10:17:31 +000012130 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012131 return nullptr;
12132
Alexey Bataev931e19b2017-10-02 16:32:39 +000012133 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000012134 OpenMPDirectiveKind CaptureRegion =
12135 getOpenMPCaptureRegionForClause(DKind, OMPC_device);
12136 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000012137 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000012138 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev931e19b2017-10-02 16:32:39 +000012139 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
12140 HelperValStmt = buildPreInits(Context, Captures);
12141 }
12142
Alexey Bataev8451efa2018-01-15 19:06:12 +000012143 return new (Context) OMPDeviceClause(ValExpr, HelperValStmt, CaptureRegion,
12144 StartLoc, LParenLoc, EndLoc);
Michael Wonge710d542015-08-07 16:16:36 +000012145}
Kelvin Li0bff7af2015-11-23 05:32:03 +000012146
Alexey Bataeve3727102018-04-18 15:57:46 +000012147static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
Alexey Bataev95c23e72018-02-27 21:31:11 +000012148 DSAStackTy *Stack, QualType QTy,
12149 bool FullCheck = true) {
Kelvin Li0bff7af2015-11-23 05:32:03 +000012150 NamedDecl *ND;
12151 if (QTy->isIncompleteType(&ND)) {
12152 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
12153 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +000012154 }
Alexey Bataev95c23e72018-02-27 21:31:11 +000012155 if (FullCheck && !SemaRef.CurContext->isDependentContext() &&
12156 !QTy.isTrivialType(SemaRef.Context))
12157 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR;
Kelvin Li0bff7af2015-11-23 05:32:03 +000012158 return true;
12159}
12160
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000012161/// Return true if it can be proven that the provided array expression
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012162/// (array section or array subscript) does NOT specify the whole size of the
12163/// array whose base type is \a BaseQTy.
Alexey Bataeve3727102018-04-18 15:57:46 +000012164static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012165 const Expr *E,
12166 QualType BaseQTy) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012167 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012168
12169 // If this is an array subscript, it refers to the whole size if the size of
12170 // the dimension is constant and equals 1. Also, an array section assumes the
12171 // format of an array subscript if no colon is used.
12172 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012173 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012174 return ATy->getSize().getSExtValue() != 1;
12175 // Size can't be evaluated statically.
12176 return false;
12177 }
12178
12179 assert(OASE && "Expecting array section if not an array subscript.");
Alexey Bataeve3727102018-04-18 15:57:46 +000012180 const Expr *LowerBound = OASE->getLowerBound();
12181 const Expr *Length = OASE->getLength();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012182
12183 // If there is a lower bound that does not evaluates to zero, we are not
David Majnemer9d168222016-08-05 17:44:54 +000012184 // covering the whole dimension.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012185 if (LowerBound) {
12186 llvm::APSInt ConstLowerBound;
12187 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
12188 return false; // Can't get the integer value as a constant.
12189 if (ConstLowerBound.getSExtValue())
12190 return true;
12191 }
12192
12193 // If we don't have a length we covering the whole dimension.
12194 if (!Length)
12195 return false;
12196
12197 // If the base is a pointer, we don't have a way to get the size of the
12198 // pointee.
12199 if (BaseQTy->isPointerType())
12200 return false;
12201
12202 // We can only check if the length is the same as the size of the dimension
12203 // if we have a constant array.
Alexey Bataeve3727102018-04-18 15:57:46 +000012204 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012205 if (!CATy)
12206 return false;
12207
12208 llvm::APSInt ConstLength;
12209 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
12210 return false; // Can't get the integer value as a constant.
12211
12212 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
12213}
12214
12215// Return true if it can be proven that the provided array expression (array
12216// section or array subscript) does NOT specify a single element of the array
12217// whose base type is \a BaseQTy.
Alexey Bataeve3727102018-04-18 15:57:46 +000012218static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
David Majnemer9d168222016-08-05 17:44:54 +000012219 const Expr *E,
12220 QualType BaseQTy) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012221 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012222
12223 // An array subscript always refer to a single element. Also, an array section
12224 // assumes the format of an array subscript if no colon is used.
12225 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
12226 return false;
12227
12228 assert(OASE && "Expecting array section if not an array subscript.");
Alexey Bataeve3727102018-04-18 15:57:46 +000012229 const Expr *Length = OASE->getLength();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012230
12231 // If we don't have a length we have to check if the array has unitary size
12232 // for this dimension. Also, we should always expect a length if the base type
12233 // is pointer.
12234 if (!Length) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012235 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012236 return ATy->getSize().getSExtValue() != 1;
12237 // We cannot assume anything.
12238 return false;
12239 }
12240
12241 // Check if the length evaluates to 1.
12242 llvm::APSInt ConstLength;
12243 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
12244 return false; // Can't get the integer value as a constant.
12245
12246 return ConstLength.getSExtValue() != 1;
12247}
12248
Samuel Antao661c0902016-05-26 17:39:58 +000012249// Return the expression of the base of the mappable expression or null if it
12250// cannot be determined and do all the necessary checks to see if the expression
12251// is valid as a standalone mappable expression. In the process, record all the
Samuel Antao90927002016-04-26 14:54:23 +000012252// components of the expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000012253static const Expr *checkMapClauseExpressionBase(
Samuel Antao90927002016-04-26 14:54:23 +000012254 Sema &SemaRef, Expr *E,
Samuel Antao661c0902016-05-26 17:39:58 +000012255 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012256 OpenMPClauseKind CKind, bool NoDiagnose) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012257 SourceLocation ELoc = E->getExprLoc();
12258 SourceRange ERange = E->getSourceRange();
12259
12260 // The base of elements of list in a map clause have to be either:
12261 // - a reference to variable or field.
12262 // - a member expression.
12263 // - an array expression.
12264 //
12265 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
12266 // reference to 'r'.
12267 //
12268 // If we have:
12269 //
12270 // struct SS {
12271 // Bla S;
12272 // foo() {
12273 // #pragma omp target map (S.Arr[:12]);
12274 // }
12275 // }
12276 //
12277 // We want to retrieve the member expression 'this->S';
12278
Alexey Bataeve3727102018-04-18 15:57:46 +000012279 const Expr *RelevantExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012280
Samuel Antao5de996e2016-01-22 20:21:36 +000012281 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
12282 // If a list item is an array section, it must specify contiguous storage.
12283 //
12284 // For this restriction it is sufficient that we make sure only references
12285 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012286 // exist except in the rightmost expression (unless they cover the whole
12287 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +000012288 //
12289 // r.ArrS[3:5].Arr[6:7]
12290 //
12291 // r.ArrS[3:5].x
12292 //
12293 // but these would be valid:
12294 // r.ArrS[3].Arr[6:7]
12295 //
12296 // r.ArrS[3].x
12297
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012298 bool AllowUnitySizeArraySection = true;
12299 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +000012300
Dmitry Polukhin644a9252016-03-11 07:58:34 +000012301 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012302 E = E->IgnoreParenImpCasts();
12303
12304 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
12305 if (!isa<VarDecl>(CurE->getDecl()))
Alexey Bataev27041fa2017-12-05 15:22:49 +000012306 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012307
12308 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012309
12310 // If we got a reference to a declaration, we should not expect any array
12311 // section before that.
12312 AllowUnitySizeArraySection = false;
12313 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000012314
12315 // Record the component.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012316 CurComponents.emplace_back(CurE, CurE->getDecl());
12317 } else if (auto *CurE = dyn_cast<MemberExpr>(E)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012318 Expr *BaseE = CurE->getBase()->IgnoreParenImpCasts();
Samuel Antao5de996e2016-01-22 20:21:36 +000012319
12320 if (isa<CXXThisExpr>(BaseE))
12321 // We found a base expression: this->Val.
12322 RelevantExpr = CurE;
12323 else
12324 E = BaseE;
12325
12326 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012327 if (!NoDiagnose) {
12328 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
12329 << CurE->getSourceRange();
12330 return nullptr;
12331 }
12332 if (RelevantExpr)
12333 return nullptr;
12334 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000012335 }
12336
12337 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
12338
12339 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
12340 // A bit-field cannot appear in a map clause.
12341 //
12342 if (FD->isBitField()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012343 if (!NoDiagnose) {
12344 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
12345 << CurE->getSourceRange() << getOpenMPClauseName(CKind);
12346 return nullptr;
12347 }
12348 if (RelevantExpr)
12349 return nullptr;
12350 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000012351 }
12352
12353 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12354 // If the type of a list item is a reference to a type T then the type
12355 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012356 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000012357
12358 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
12359 // A list item cannot be a variable that is a member of a structure with
12360 // a union type.
12361 //
Alexey Bataeve3727102018-04-18 15:57:46 +000012362 if (CurType->isUnionType()) {
12363 if (!NoDiagnose) {
12364 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
12365 << CurE->getSourceRange();
12366 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012367 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012368 continue;
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012369 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012370
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012371 // If we got a member expression, we should not expect any array section
12372 // before that:
12373 //
12374 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
12375 // If a list item is an element of a structure, only the rightmost symbol
12376 // of the variable reference can be an array section.
12377 //
12378 AllowUnitySizeArraySection = false;
12379 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000012380
12381 // Record the component.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012382 CurComponents.emplace_back(CurE, FD);
12383 } else if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012384 E = CurE->getBase()->IgnoreParenImpCasts();
12385
12386 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012387 if (!NoDiagnose) {
12388 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
12389 << 0 << CurE->getSourceRange();
12390 return nullptr;
12391 }
12392 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000012393 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012394
12395 // If we got an array subscript that express the whole dimension we
12396 // can have any array expressions before. If it only expressing part of
12397 // the dimension, we can only have unitary-size array expressions.
Alexey Bataeve3727102018-04-18 15:57:46 +000012398 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012399 E->getType()))
12400 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000012401
12402 // Record the component - we don't have any declaration associated.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012403 CurComponents.emplace_back(CurE, nullptr);
12404 } else if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012405 assert(!NoDiagnose && "Array sections cannot be implicitly mapped.");
Samuel Antao5de996e2016-01-22 20:21:36 +000012406 E = CurE->getBase()->IgnoreParenImpCasts();
12407
Alexey Bataev27041fa2017-12-05 15:22:49 +000012408 QualType CurType =
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012409 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
12410
Samuel Antao5de996e2016-01-22 20:21:36 +000012411 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12412 // If the type of a list item is a reference to a type T then the type
12413 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +000012414 if (CurType->isReferenceType())
12415 CurType = CurType->getPointeeType();
12416
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012417 bool IsPointer = CurType->isAnyPointerType();
12418
12419 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012420 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
12421 << 0 << CurE->getSourceRange();
Alexey Bataev27041fa2017-12-05 15:22:49 +000012422 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012423 }
12424
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012425 bool NotWhole =
Alexey Bataeve3727102018-04-18 15:57:46 +000012426 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012427 bool NotUnity =
Alexey Bataeve3727102018-04-18 15:57:46 +000012428 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012429
Samuel Antaodab51bb2016-07-18 23:22:11 +000012430 if (AllowWholeSizeArraySection) {
12431 // Any array section is currently allowed. Allowing a whole size array
12432 // section implies allowing a unity array section as well.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012433 //
12434 // If this array section refers to the whole dimension we can still
12435 // accept other array sections before this one, except if the base is a
12436 // pointer. Otherwise, only unitary sections are accepted.
12437 if (NotWhole || IsPointer)
12438 AllowWholeSizeArraySection = false;
Samuel Antaodab51bb2016-07-18 23:22:11 +000012439 } else if (AllowUnitySizeArraySection && NotUnity) {
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012440 // A unity or whole array section is not allowed and that is not
12441 // compatible with the properties of the current array section.
12442 SemaRef.Diag(
12443 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
12444 << CurE->getSourceRange();
Alexey Bataev27041fa2017-12-05 15:22:49 +000012445 return nullptr;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012446 }
Samuel Antao90927002016-04-26 14:54:23 +000012447
12448 // Record the component - we don't have any declaration associated.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012449 CurComponents.emplace_back(CurE, nullptr);
12450 } else {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012451 if (!NoDiagnose) {
12452 // If nothing else worked, this is not a valid map clause expression.
12453 SemaRef.Diag(
12454 ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
12455 << ERange;
12456 }
Alexey Bataev27041fa2017-12-05 15:22:49 +000012457 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012458 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012459 }
12460
12461 return RelevantExpr;
12462}
12463
12464// Return true if expression E associated with value VD has conflicts with other
12465// map information.
Alexey Bataeve3727102018-04-18 15:57:46 +000012466static bool checkMapConflicts(
12467 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E,
Samuel Antao90927002016-04-26 14:54:23 +000012468 bool CurrentRegionOnly,
Samuel Antao661c0902016-05-26 17:39:58 +000012469 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
12470 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012471 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +000012472 SourceLocation ELoc = E->getExprLoc();
12473 SourceRange ERange = E->getSourceRange();
12474
12475 // In order to easily check the conflicts we need to match each component of
12476 // the expression under test with the components of the expressions that are
12477 // already in the stack.
12478
Samuel Antao5de996e2016-01-22 20:21:36 +000012479 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000012480 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000012481 "Map clause expression with unexpected base!");
12482
12483 // Variables to help detecting enclosing problems in data environment nests.
12484 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +000012485 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012486
Samuel Antao90927002016-04-26 14:54:23 +000012487 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
12488 VD, CurrentRegionOnly,
Alexey Bataeve3727102018-04-18 15:57:46 +000012489 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc,
12490 ERange, CKind, &EnclosingExpr,
12491 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef
12492 StackComponents,
12493 OpenMPClauseKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012494 assert(!StackComponents.empty() &&
12495 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000012496 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000012497 "Map clause expression with unexpected base!");
Fangrui Song16fe49a2018-04-18 19:32:01 +000012498 (void)VD;
Samuel Antao5de996e2016-01-22 20:21:36 +000012499
Samuel Antao90927002016-04-26 14:54:23 +000012500 // The whole expression in the stack.
Alexey Bataeve3727102018-04-18 15:57:46 +000012501 const Expr *RE = StackComponents.front().getAssociatedExpression();
Samuel Antao90927002016-04-26 14:54:23 +000012502
Samuel Antao5de996e2016-01-22 20:21:36 +000012503 // Expressions must start from the same base. Here we detect at which
12504 // point both expressions diverge from each other and see if we can
12505 // detect if the memory referred to both expressions is contiguous and
12506 // do not overlap.
12507 auto CI = CurComponents.rbegin();
12508 auto CE = CurComponents.rend();
12509 auto SI = StackComponents.rbegin();
12510 auto SE = StackComponents.rend();
12511 for (; CI != CE && SI != SE; ++CI, ++SI) {
12512
12513 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
12514 // At most one list item can be an array item derived from a given
12515 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +000012516 if (CurrentRegionOnly &&
12517 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
12518 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
12519 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
12520 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
12521 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +000012522 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +000012523 << CI->getAssociatedExpression()->getSourceRange();
12524 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
12525 diag::note_used_here)
12526 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +000012527 return true;
12528 }
12529
12530 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +000012531 if (CI->getAssociatedExpression()->getStmtClass() !=
12532 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +000012533 break;
12534
12535 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +000012536 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +000012537 break;
12538 }
Kelvin Li9f645ae2016-07-18 22:49:16 +000012539 // Check if the extra components of the expressions in the enclosing
12540 // data environment are redundant for the current base declaration.
12541 // If they are, the maps completely overlap, which is legal.
12542 for (; SI != SE; ++SI) {
12543 QualType Type;
Alexey Bataeve3727102018-04-18 15:57:46 +000012544 if (const auto *ASE =
David Majnemer9d168222016-08-05 17:44:54 +000012545 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
Kelvin Li9f645ae2016-07-18 22:49:16 +000012546 Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
Alexey Bataeve3727102018-04-18 15:57:46 +000012547 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(
David Majnemer9d168222016-08-05 17:44:54 +000012548 SI->getAssociatedExpression())) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012549 const Expr *E = OASE->getBase()->IgnoreParenImpCasts();
Kelvin Li9f645ae2016-07-18 22:49:16 +000012550 Type =
12551 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
12552 }
12553 if (Type.isNull() || Type->isAnyPointerType() ||
Alexey Bataeve3727102018-04-18 15:57:46 +000012554 checkArrayExpressionDoesNotReferToWholeSize(
Kelvin Li9f645ae2016-07-18 22:49:16 +000012555 SemaRef, SI->getAssociatedExpression(), Type))
12556 break;
12557 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012558
12559 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
12560 // List items of map clauses in the same construct must not share
12561 // original storage.
12562 //
12563 // If the expressions are exactly the same or one is a subset of the
12564 // other, it means they are sharing storage.
12565 if (CI == CE && SI == SE) {
12566 if (CurrentRegionOnly) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012567 if (CKind == OMPC_map) {
Samuel Antao661c0902016-05-26 17:39:58 +000012568 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +000012569 } else {
Samuel Antaoec172c62016-05-26 17:49:04 +000012570 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000012571 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
12572 << ERange;
12573 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012574 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12575 << RE->getSourceRange();
12576 return true;
Samuel Antao5de996e2016-01-22 20:21:36 +000012577 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012578 // If we find the same expression in the enclosing data environment,
12579 // that is legal.
12580 IsEnclosedByDataEnvironmentExpr = true;
12581 return false;
Samuel Antao5de996e2016-01-22 20:21:36 +000012582 }
12583
Samuel Antao90927002016-04-26 14:54:23 +000012584 QualType DerivedType =
12585 std::prev(CI)->getAssociatedDeclaration()->getType();
12586 SourceLocation DerivedLoc =
12587 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000012588
12589 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12590 // If the type of a list item is a reference to a type T then the type
12591 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000012592 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000012593
12594 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
12595 // A variable for which the type is pointer and an array section
12596 // derived from that variable must not appear as list items of map
12597 // clauses of the same construct.
12598 //
12599 // Also, cover one of the cases in:
12600 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
12601 // If any part of the original storage of a list item has corresponding
12602 // storage in the device data environment, all of the original storage
12603 // must have corresponding storage in the device data environment.
12604 //
12605 if (DerivedType->isAnyPointerType()) {
12606 if (CI == CE || SI == SE) {
12607 SemaRef.Diag(
12608 DerivedLoc,
12609 diag::err_omp_pointer_mapped_along_with_derived_section)
12610 << DerivedLoc;
Alexey Bataev2819260b2018-02-27 17:42:00 +000012611 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12612 << RE->getSourceRange();
12613 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +000012614 }
12615 if (CI->getAssociatedExpression()->getStmtClass() !=
Alexey Bataev2819260b2018-02-27 17:42:00 +000012616 SI->getAssociatedExpression()->getStmtClass() ||
12617 CI->getAssociatedDeclaration()->getCanonicalDecl() ==
12618 SI->getAssociatedDeclaration()->getCanonicalDecl()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012619 assert(CI != CE && SI != SE);
Alexey Bataev2819260b2018-02-27 17:42:00 +000012620 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced)
Samuel Antao5de996e2016-01-22 20:21:36 +000012621 << DerivedLoc;
Alexey Bataev2819260b2018-02-27 17:42:00 +000012622 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12623 << RE->getSourceRange();
12624 return true;
Samuel Antao5de996e2016-01-22 20:21:36 +000012625 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012626 }
12627
12628 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
12629 // List items of map clauses in the same construct must not share
12630 // original storage.
12631 //
12632 // An expression is a subset of the other.
12633 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012634 if (CKind == OMPC_map) {
Alexey Bataeve82445f2018-09-20 13:54:02 +000012635 if (CI != CE || SI != SE) {
12636 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is
12637 // a pointer.
12638 auto Begin =
12639 CI != CE ? CurComponents.begin() : StackComponents.begin();
12640 auto End = CI != CE ? CurComponents.end() : StackComponents.end();
12641 auto It = Begin;
12642 while (It != End && !It->getAssociatedDeclaration())
12643 std::advance(It, 1);
12644 assert(It != End &&
12645 "Expected at least one component with the declaration.");
12646 if (It != Begin && It->getAssociatedDeclaration()
12647 ->getType()
12648 .getCanonicalType()
12649 ->isAnyPointerType()) {
12650 IsEnclosedByDataEnvironmentExpr = false;
12651 EnclosingExpr = nullptr;
12652 return false;
12653 }
12654 }
Samuel Antao661c0902016-05-26 17:39:58 +000012655 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +000012656 } else {
Samuel Antaoec172c62016-05-26 17:49:04 +000012657 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000012658 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
12659 << ERange;
12660 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012661 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12662 << RE->getSourceRange();
12663 return true;
12664 }
12665
12666 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000012667 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000012668 if (!CurrentRegionOnly && SI != SE)
12669 EnclosingExpr = RE;
12670
12671 // The current expression is a subset of the expression in the data
12672 // environment.
12673 IsEnclosedByDataEnvironmentExpr |=
12674 (!CurrentRegionOnly && CI != CE && SI == SE);
12675
12676 return false;
12677 });
12678
12679 if (CurrentRegionOnly)
12680 return FoundError;
12681
12682 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
12683 // If any part of the original storage of a list item has corresponding
12684 // storage in the device data environment, all of the original storage must
12685 // have corresponding storage in the device data environment.
12686 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
12687 // If a list item is an element of a structure, and a different element of
12688 // the structure has a corresponding list item in the device data environment
12689 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000012690 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000012691 // data environment prior to the task encountering the construct.
12692 //
12693 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
12694 SemaRef.Diag(ELoc,
12695 diag::err_omp_original_storage_is_shared_and_does_not_contain)
12696 << ERange;
12697 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
12698 << EnclosingExpr->getSourceRange();
12699 return true;
12700 }
12701
12702 return FoundError;
12703}
12704
Samuel Antao661c0902016-05-26 17:39:58 +000012705namespace {
12706// Utility struct that gathers all the related lists associated with a mappable
12707// expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000012708struct MappableVarListInfo {
Samuel Antao661c0902016-05-26 17:39:58 +000012709 // The list of expressions.
12710 ArrayRef<Expr *> VarList;
12711 // The list of processed expressions.
12712 SmallVector<Expr *, 16> ProcessedVarList;
12713 // The mappble components for each expression.
12714 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
12715 // The base declaration of the variable.
12716 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
12717
12718 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
12719 // We have a list of components and base declarations for each entry in the
12720 // variable list.
12721 VarComponents.reserve(VarList.size());
12722 VarBaseDeclarations.reserve(VarList.size());
12723 }
12724};
12725}
12726
12727// Check the validity of the provided variable list for the provided clause kind
12728// \a CKind. In the check process the valid expressions, and mappable expression
12729// components and variables are extracted and used to fill \a Vars,
12730// \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and
12731// \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'.
12732static void
12733checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS,
12734 OpenMPClauseKind CKind, MappableVarListInfo &MVLI,
12735 SourceLocation StartLoc,
12736 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
12737 bool IsMapTypeImplicit = false) {
Samuel Antaoec172c62016-05-26 17:49:04 +000012738 // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
12739 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
Samuel Antao661c0902016-05-26 17:39:58 +000012740 "Unexpected clause kind with mappable expressions!");
Kelvin Li0bff7af2015-11-23 05:32:03 +000012741
Samuel Antao90927002016-04-26 14:54:23 +000012742 // Keep track of the mappable components and base declarations in this clause.
12743 // Each entry in the list is going to have a list of components associated. We
12744 // record each set of the components so that we can build the clause later on.
12745 // In the end we should have the same amount of declarations and component
12746 // lists.
Samuel Antao90927002016-04-26 14:54:23 +000012747
Alexey Bataeve3727102018-04-18 15:57:46 +000012748 for (Expr *RE : MVLI.VarList) {
Samuel Antaoec172c62016-05-26 17:49:04 +000012749 assert(RE && "Null expr in omp to/from/map clause");
Kelvin Li0bff7af2015-11-23 05:32:03 +000012750 SourceLocation ELoc = RE->getExprLoc();
12751
Alexey Bataeve3727102018-04-18 15:57:46 +000012752 const Expr *VE = RE->IgnoreParenLValueCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012753
12754 if (VE->isValueDependent() || VE->isTypeDependent() ||
12755 VE->isInstantiationDependent() ||
12756 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012757 // We can only analyze this information once the missing information is
12758 // resolved.
Samuel Antao661c0902016-05-26 17:39:58 +000012759 MVLI.ProcessedVarList.push_back(RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012760 continue;
12761 }
12762
Alexey Bataeve3727102018-04-18 15:57:46 +000012763 Expr *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012764
Samuel Antao5de996e2016-01-22 20:21:36 +000012765 if (!RE->IgnoreParenImpCasts()->isLValue()) {
Samuel Antao661c0902016-05-26 17:39:58 +000012766 SemaRef.Diag(ELoc,
12767 diag::err_omp_expected_named_var_member_or_array_expression)
Samuel Antao5de996e2016-01-22 20:21:36 +000012768 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012769 continue;
12770 }
12771
Samuel Antao90927002016-04-26 14:54:23 +000012772 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
12773 ValueDecl *CurDeclaration = nullptr;
12774
12775 // Obtain the array or member expression bases if required. Also, fill the
12776 // components array with all the components identified in the process.
Alexey Bataeve3727102018-04-18 15:57:46 +000012777 const Expr *BE = checkMapClauseExpressionBase(
12778 SemaRef, SimpleExpr, CurComponents, CKind, /*NoDiagnose=*/false);
Samuel Antao5de996e2016-01-22 20:21:36 +000012779 if (!BE)
12780 continue;
12781
Samuel Antao90927002016-04-26 14:54:23 +000012782 assert(!CurComponents.empty() &&
12783 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000012784
Samuel Antao90927002016-04-26 14:54:23 +000012785 // For the following checks, we rely on the base declaration which is
12786 // expected to be associated with the last component. The declaration is
12787 // expected to be a variable or a field (if 'this' is being mapped).
12788 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
12789 assert(CurDeclaration && "Null decl on map clause.");
12790 assert(
12791 CurDeclaration->isCanonicalDecl() &&
12792 "Expecting components to have associated only canonical declarations.");
12793
12794 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
Alexey Bataeve3727102018-04-18 15:57:46 +000012795 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000012796
12797 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000012798 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000012799
12800 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
Samuel Antao661c0902016-05-26 17:39:58 +000012801 // threadprivate variables cannot appear in a map clause.
12802 // OpenMP 4.5 [2.10.5, target update Construct]
12803 // threadprivate variables cannot appear in a from clause.
12804 if (VD && DSAS->isThreadPrivate(VD)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012805 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
Samuel Antao661c0902016-05-26 17:39:58 +000012806 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
12807 << getOpenMPClauseName(CKind);
Alexey Bataeve3727102018-04-18 15:57:46 +000012808 reportOriginalDsa(SemaRef, DSAS, VD, DVar);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012809 continue;
12810 }
12811
Samuel Antao5de996e2016-01-22 20:21:36 +000012812 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
12813 // A list item cannot appear in both a map clause and a data-sharing
12814 // attribute clause on the same construct.
Kelvin Li0bff7af2015-11-23 05:32:03 +000012815
Samuel Antao5de996e2016-01-22 20:21:36 +000012816 // Check conflicts with other map clause expressions. We check the conflicts
12817 // with the current construct separately from the enclosing data
Samuel Antao661c0902016-05-26 17:39:58 +000012818 // environment, because the restrictions are different. We only have to
12819 // check conflicts across regions for the map clauses.
Alexey Bataeve3727102018-04-18 15:57:46 +000012820 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
Samuel Antao661c0902016-05-26 17:39:58 +000012821 /*CurrentRegionOnly=*/true, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000012822 break;
Samuel Antao661c0902016-05-26 17:39:58 +000012823 if (CKind == OMPC_map &&
Alexey Bataeve3727102018-04-18 15:57:46 +000012824 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
Samuel Antao661c0902016-05-26 17:39:58 +000012825 /*CurrentRegionOnly=*/false, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000012826 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000012827
Samuel Antao661c0902016-05-26 17:39:58 +000012828 // OpenMP 4.5 [2.10.5, target update Construct]
Samuel Antao5de996e2016-01-22 20:21:36 +000012829 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12830 // If the type of a list item is a reference to a type T then the type will
12831 // be considered to be T for all purposes of this clause.
Alexey Bataev354df2e2018-05-02 18:44:10 +000012832 auto I = llvm::find_if(
12833 CurComponents,
12834 [](const OMPClauseMappableExprCommon::MappableComponent &MC) {
12835 return MC.getAssociatedDeclaration();
12836 });
12837 assert(I != CurComponents.end() && "Null decl on map clause.");
12838 QualType Type =
12839 I->getAssociatedDeclaration()->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000012840
Samuel Antao661c0902016-05-26 17:39:58 +000012841 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
12842 // A list item in a to or from clause must have a mappable type.
Samuel Antao5de996e2016-01-22 20:21:36 +000012843 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000012844 // A list item must have a mappable type.
Alexey Bataeve3727102018-04-18 15:57:46 +000012845 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
Samuel Antao661c0902016-05-26 17:39:58 +000012846 DSAS, Type))
Kelvin Li0bff7af2015-11-23 05:32:03 +000012847 continue;
12848
Samuel Antao661c0902016-05-26 17:39:58 +000012849 if (CKind == OMPC_map) {
12850 // target enter data
12851 // OpenMP [2.10.2, Restrictions, p. 99]
12852 // A map-type must be specified in all map clauses and must be either
12853 // to or alloc.
12854 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
12855 if (DKind == OMPD_target_enter_data &&
12856 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
12857 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
12858 << (IsMapTypeImplicit ? 1 : 0)
12859 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
12860 << getOpenMPDirectiveName(DKind);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000012861 continue;
12862 }
Samuel Antao661c0902016-05-26 17:39:58 +000012863
12864 // target exit_data
12865 // OpenMP [2.10.3, Restrictions, p. 102]
12866 // A map-type must be specified in all map clauses and must be either
12867 // from, release, or delete.
12868 if (DKind == OMPD_target_exit_data &&
12869 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
12870 MapType == OMPC_MAP_delete)) {
12871 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
12872 << (IsMapTypeImplicit ? 1 : 0)
12873 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
12874 << getOpenMPDirectiveName(DKind);
12875 continue;
12876 }
12877
12878 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
12879 // A list item cannot appear in both a map clause and a data-sharing
12880 // attribute clause on the same construct
Alexey Bataeve3727102018-04-18 15:57:46 +000012881 if (VD && isOpenMPTargetExecutionDirective(DKind)) {
12882 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
Samuel Antao661c0902016-05-26 17:39:58 +000012883 if (isOpenMPPrivate(DVar.CKind)) {
Samuel Antao6890b092016-07-28 14:25:09 +000012884 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Samuel Antao661c0902016-05-26 17:39:58 +000012885 << getOpenMPClauseName(DVar.CKind)
Samuel Antao6890b092016-07-28 14:25:09 +000012886 << getOpenMPClauseName(OMPC_map)
Samuel Antao661c0902016-05-26 17:39:58 +000012887 << getOpenMPDirectiveName(DSAS->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000012888 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar);
Samuel Antao661c0902016-05-26 17:39:58 +000012889 continue;
12890 }
12891 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +000012892 }
12893
Samuel Antao90927002016-04-26 14:54:23 +000012894 // Save the current expression.
Samuel Antao661c0902016-05-26 17:39:58 +000012895 MVLI.ProcessedVarList.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000012896
12897 // Store the components in the stack so that they can be used to check
12898 // against other clauses later on.
Samuel Antao6890b092016-07-28 14:25:09 +000012899 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
12900 /*WhereFoundClauseKind=*/OMPC_map);
Samuel Antao90927002016-04-26 14:54:23 +000012901
12902 // Save the components and declaration to create the clause. For purposes of
12903 // the clause creation, any component list that has has base 'this' uses
Samuel Antao686c70c2016-05-26 17:30:50 +000012904 // null as base declaration.
Samuel Antao661c0902016-05-26 17:39:58 +000012905 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
12906 MVLI.VarComponents.back().append(CurComponents.begin(),
12907 CurComponents.end());
12908 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
12909 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012910 }
Samuel Antao661c0902016-05-26 17:39:58 +000012911}
12912
12913OMPClause *
12914Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
12915 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
12916 SourceLocation MapLoc, SourceLocation ColonLoc,
12917 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
12918 SourceLocation LParenLoc, SourceLocation EndLoc) {
12919 MappableVarListInfo MVLI(VarList);
12920 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc,
12921 MapType, IsMapTypeImplicit);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012922
Samuel Antao5de996e2016-01-22 20:21:36 +000012923 // We need to produce a map clause even if we don't have variables so that
12924 // other diagnostics related with non-existing map clauses are accurate.
Samuel Antao661c0902016-05-26 17:39:58 +000012925 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc,
12926 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
12927 MVLI.VarComponents, MapTypeModifier, MapType,
12928 IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012929}
Kelvin Li099bb8c2015-11-24 20:50:12 +000012930
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012931QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
12932 TypeResult ParsedType) {
12933 assert(ParsedType.isUsable());
12934
12935 QualType ReductionType = GetTypeFromParser(ParsedType.get());
12936 if (ReductionType.isNull())
12937 return QualType();
12938
12939 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
12940 // A type name in a declare reduction directive cannot be a function type, an
12941 // array type, a reference type, or a type qualified with const, volatile or
12942 // restrict.
12943 if (ReductionType.hasQualifiers()) {
12944 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
12945 return QualType();
12946 }
12947
12948 if (ReductionType->isFunctionType()) {
12949 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
12950 return QualType();
12951 }
12952 if (ReductionType->isReferenceType()) {
12953 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
12954 return QualType();
12955 }
12956 if (ReductionType->isArrayType()) {
12957 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
12958 return QualType();
12959 }
12960 return ReductionType;
12961}
12962
12963Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
12964 Scope *S, DeclContext *DC, DeclarationName Name,
12965 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
12966 AccessSpecifier AS, Decl *PrevDeclInScope) {
12967 SmallVector<Decl *, 8> Decls;
12968 Decls.reserve(ReductionTypes.size());
12969
12970 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
Richard Smithbecb92d2017-10-10 22:33:17 +000012971 forRedeclarationInCurContext());
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012972 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
12973 // A reduction-identifier may not be re-declared in the current scope for the
12974 // same type or for a type that is compatible according to the base language
12975 // rules.
12976 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
12977 OMPDeclareReductionDecl *PrevDRD = nullptr;
12978 bool InCompoundScope = true;
12979 if (S != nullptr) {
12980 // Find previous declaration with the same name not referenced in other
12981 // declarations.
12982 FunctionScopeInfo *ParentFn = getEnclosingFunction();
12983 InCompoundScope =
12984 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
12985 LookupName(Lookup, S);
12986 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
12987 /*AllowInlineNamespace=*/false);
12988 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
Alexey Bataeve3727102018-04-18 15:57:46 +000012989 LookupResult::Filter Filter = Lookup.makeFilter();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012990 while (Filter.hasNext()) {
12991 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
12992 if (InCompoundScope) {
12993 auto I = UsedAsPrevious.find(PrevDecl);
12994 if (I == UsedAsPrevious.end())
12995 UsedAsPrevious[PrevDecl] = false;
Alexey Bataeve3727102018-04-18 15:57:46 +000012996 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope())
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012997 UsedAsPrevious[D] = true;
12998 }
12999 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
13000 PrevDecl->getLocation();
13001 }
13002 Filter.done();
13003 if (InCompoundScope) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013004 for (const auto &PrevData : UsedAsPrevious) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013005 if (!PrevData.second) {
13006 PrevDRD = PrevData.first;
13007 break;
13008 }
13009 }
13010 }
13011 } else if (PrevDeclInScope != nullptr) {
13012 auto *PrevDRDInScope = PrevDRD =
13013 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
13014 do {
13015 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
13016 PrevDRDInScope->getLocation();
13017 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
13018 } while (PrevDRDInScope != nullptr);
13019 }
Alexey Bataeve3727102018-04-18 15:57:46 +000013020 for (const auto &TyData : ReductionTypes) {
13021 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013022 bool Invalid = false;
13023 if (I != PreviousRedeclTypes.end()) {
13024 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
13025 << TyData.first;
13026 Diag(I->second, diag::note_previous_definition);
13027 Invalid = true;
13028 }
13029 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
13030 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
13031 Name, TyData.first, PrevDRD);
13032 DC->addDecl(DRD);
13033 DRD->setAccess(AS);
13034 Decls.push_back(DRD);
13035 if (Invalid)
13036 DRD->setInvalidDecl();
13037 else
13038 PrevDRD = DRD;
13039 }
13040
13041 return DeclGroupPtrTy::make(
13042 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
13043}
13044
13045void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
13046 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13047
13048 // Enter new function scope.
13049 PushFunctionScope();
Reid Kleckner87a31802018-03-12 21:43:02 +000013050 setFunctionHasBranchProtectedScope();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013051 getCurFunction()->setHasOMPDeclareReductionCombiner();
13052
13053 if (S != nullptr)
13054 PushDeclContext(S, DRD);
13055 else
13056 CurContext = DRD;
13057
Faisal Valid143a0c2017-04-01 21:30:49 +000013058 PushExpressionEvaluationContext(
13059 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013060
13061 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013062 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
13063 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
13064 // uses semantics of argument handles by value, but it should be passed by
13065 // reference. C lang does not support references, so pass all parameters as
13066 // pointers.
13067 // Create 'T omp_in;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013068 VarDecl *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013069 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013070 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
13071 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
13072 // uses semantics of argument handles by value, but it should be passed by
13073 // reference. C lang does not support references, so pass all parameters as
13074 // pointers.
13075 // Create 'T omp_out;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013076 VarDecl *OmpOutParm =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013077 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
13078 if (S != nullptr) {
13079 PushOnScopeChains(OmpInParm, S);
13080 PushOnScopeChains(OmpOutParm, S);
13081 } else {
13082 DRD->addDecl(OmpInParm);
13083 DRD->addDecl(OmpOutParm);
13084 }
Alexey Bataeve6aa4692018-09-13 16:54:05 +000013085 Expr *InE =
13086 ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation());
13087 Expr *OutE =
13088 ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation());
13089 DRD->setCombinerData(InE, OutE);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013090}
13091
13092void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
13093 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13094 DiscardCleanupsInEvaluationContext();
13095 PopExpressionEvaluationContext();
13096
13097 PopDeclContext();
13098 PopFunctionScopeInfo();
13099
13100 if (Combiner != nullptr)
13101 DRD->setCombiner(Combiner);
13102 else
13103 DRD->setInvalidDecl();
13104}
13105
Alexey Bataev070f43a2017-09-06 14:49:58 +000013106VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013107 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13108
13109 // Enter new function scope.
13110 PushFunctionScope();
Reid Kleckner87a31802018-03-12 21:43:02 +000013111 setFunctionHasBranchProtectedScope();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013112
13113 if (S != nullptr)
13114 PushDeclContext(S, DRD);
13115 else
13116 CurContext = DRD;
13117
Faisal Valid143a0c2017-04-01 21:30:49 +000013118 PushExpressionEvaluationContext(
13119 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013120
13121 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013122 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
13123 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
13124 // uses semantics of argument handles by value, but it should be passed by
13125 // reference. C lang does not support references, so pass all parameters as
13126 // pointers.
13127 // Create 'T omp_priv;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013128 VarDecl *OmpPrivParm =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013129 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013130 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
13131 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
13132 // uses semantics of argument handles by value, but it should be passed by
13133 // reference. C lang does not support references, so pass all parameters as
13134 // pointers.
13135 // Create 'T omp_orig;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013136 VarDecl *OmpOrigParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013137 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013138 if (S != nullptr) {
13139 PushOnScopeChains(OmpPrivParm, S);
13140 PushOnScopeChains(OmpOrigParm, S);
13141 } else {
13142 DRD->addDecl(OmpPrivParm);
13143 DRD->addDecl(OmpOrigParm);
13144 }
Alexey Bataeve6aa4692018-09-13 16:54:05 +000013145 Expr *OrigE =
13146 ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation());
13147 Expr *PrivE =
13148 ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation());
13149 DRD->setInitializerData(OrigE, PrivE);
Alexey Bataev070f43a2017-09-06 14:49:58 +000013150 return OmpPrivParm;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013151}
13152
Alexey Bataev070f43a2017-09-06 14:49:58 +000013153void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer,
13154 VarDecl *OmpPrivParm) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013155 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13156 DiscardCleanupsInEvaluationContext();
13157 PopExpressionEvaluationContext();
13158
13159 PopDeclContext();
13160 PopFunctionScopeInfo();
13161
Alexey Bataev070f43a2017-09-06 14:49:58 +000013162 if (Initializer != nullptr) {
13163 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit);
13164 } else if (OmpPrivParm->hasInit()) {
13165 DRD->setInitializer(OmpPrivParm->getInit(),
13166 OmpPrivParm->isDirectInit()
13167 ? OMPDeclareReductionDecl::DirectInit
13168 : OMPDeclareReductionDecl::CopyInit);
13169 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013170 DRD->setInvalidDecl();
Alexey Bataev070f43a2017-09-06 14:49:58 +000013171 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013172}
13173
13174Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
13175 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013176 for (Decl *D : DeclReductions.get()) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013177 if (IsValid) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013178 if (S)
13179 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S,
13180 /*AddToContext=*/false);
13181 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013182 D->setInvalidDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000013183 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013184 }
13185 return DeclReductions;
13186}
13187
David Majnemer9d168222016-08-05 17:44:54 +000013188OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
Kelvin Li099bb8c2015-11-24 20:50:12 +000013189 SourceLocation StartLoc,
13190 SourceLocation LParenLoc,
13191 SourceLocation EndLoc) {
13192 Expr *ValExpr = NumTeams;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000013193 Stmt *HelperValStmt = nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000013194
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013195 // OpenMP [teams Constrcut, Restrictions]
13196 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000013197 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
Alexey Bataeva0569352015-12-01 10:17:31 +000013198 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013199 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000013200
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000013201 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000013202 OpenMPDirectiveKind CaptureRegion =
13203 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams);
13204 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000013205 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000013206 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000013207 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13208 HelperValStmt = buildPreInits(Context, Captures);
13209 }
13210
13211 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion,
13212 StartLoc, LParenLoc, EndLoc);
Kelvin Li099bb8c2015-11-24 20:50:12 +000013213}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013214
13215OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
13216 SourceLocation StartLoc,
13217 SourceLocation LParenLoc,
13218 SourceLocation EndLoc) {
13219 Expr *ValExpr = ThreadLimit;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000013220 Stmt *HelperValStmt = nullptr;
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013221
13222 // OpenMP [teams Constrcut, Restrictions]
13223 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000013224 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
Alexey Bataeva0569352015-12-01 10:17:31 +000013225 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013226 return nullptr;
13227
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000013228 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000013229 OpenMPDirectiveKind CaptureRegion =
13230 getOpenMPCaptureRegionForClause(DKind, OMPC_thread_limit);
13231 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000013232 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000013233 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000013234 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13235 HelperValStmt = buildPreInits(Context, Captures);
13236 }
13237
13238 return new (Context) OMPThreadLimitClause(
13239 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013240}
Alexey Bataeva0569352015-12-01 10:17:31 +000013241
13242OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
13243 SourceLocation StartLoc,
13244 SourceLocation LParenLoc,
13245 SourceLocation EndLoc) {
13246 Expr *ValExpr = Priority;
13247
13248 // OpenMP [2.9.1, task Constrcut]
13249 // The priority-value is a non-negative numerical scalar expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000013250 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
Alexey Bataeva0569352015-12-01 10:17:31 +000013251 /*StrictlyPositive=*/false))
13252 return nullptr;
13253
13254 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
13255}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000013256
13257OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
13258 SourceLocation StartLoc,
13259 SourceLocation LParenLoc,
13260 SourceLocation EndLoc) {
13261 Expr *ValExpr = Grainsize;
13262
13263 // OpenMP [2.9.2, taskloop Constrcut]
13264 // The parameter of the grainsize clause must be a positive integer
13265 // expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000013266 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000013267 /*StrictlyPositive=*/true))
13268 return nullptr;
13269
13270 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
13271}
Alexey Bataev382967a2015-12-08 12:06:20 +000013272
13273OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
13274 SourceLocation StartLoc,
13275 SourceLocation LParenLoc,
13276 SourceLocation EndLoc) {
13277 Expr *ValExpr = NumTasks;
13278
13279 // OpenMP [2.9.2, taskloop Constrcut]
13280 // The parameter of the num_tasks clause must be a positive integer
13281 // expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000013282 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
Alexey Bataev382967a2015-12-08 12:06:20 +000013283 /*StrictlyPositive=*/true))
13284 return nullptr;
13285
13286 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
13287}
13288
Alexey Bataev28c75412015-12-15 08:19:24 +000013289OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
13290 SourceLocation LParenLoc,
13291 SourceLocation EndLoc) {
13292 // OpenMP [2.13.2, critical construct, Description]
13293 // ... where hint-expression is an integer constant expression that evaluates
13294 // to a valid lock hint.
13295 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
13296 if (HintExpr.isInvalid())
13297 return nullptr;
13298 return new (Context)
13299 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
13300}
13301
Carlo Bertollib4adf552016-01-15 18:50:31 +000013302OMPClause *Sema::ActOnOpenMPDistScheduleClause(
13303 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
13304 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
13305 SourceLocation EndLoc) {
13306 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
13307 std::string Values;
13308 Values += "'";
13309 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
13310 Values += "'";
13311 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
13312 << Values << getOpenMPClauseName(OMPC_dist_schedule);
13313 return nullptr;
13314 }
13315 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000013316 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000013317 if (ChunkSize) {
13318 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
13319 !ChunkSize->isInstantiationDependent() &&
13320 !ChunkSize->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000013321 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
Carlo Bertollib4adf552016-01-15 18:50:31 +000013322 ExprResult Val =
13323 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
13324 if (Val.isInvalid())
13325 return nullptr;
13326
13327 ValExpr = Val.get();
13328
13329 // OpenMP [2.7.1, Restrictions]
13330 // chunk_size must be a loop invariant integer expression with a positive
13331 // value.
13332 llvm::APSInt Result;
13333 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
13334 if (Result.isSigned() && !Result.isStrictlyPositive()) {
13335 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
13336 << "dist_schedule" << ChunkSize->getSourceRange();
13337 return nullptr;
13338 }
Alexey Bataev2ba67042017-11-28 21:11:44 +000013339 } else if (getOpenMPCaptureRegionForClause(
13340 DSAStack->getCurrentDirective(), OMPC_dist_schedule) !=
13341 OMPD_unknown &&
Alexey Bataevb46cdea2016-06-15 11:20:48 +000013342 !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000013343 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000013344 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev5a3af132016-03-29 08:58:54 +000013345 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13346 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000013347 }
13348 }
13349 }
13350
13351 return new (Context)
13352 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000013353 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000013354}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013355
13356OMPClause *Sema::ActOnOpenMPDefaultmapClause(
13357 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
13358 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
13359 SourceLocation KindLoc, SourceLocation EndLoc) {
13360 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
David Majnemer9d168222016-08-05 17:44:54 +000013361 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) {
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013362 std::string Value;
13363 SourceLocation Loc;
13364 Value += "'";
13365 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
13366 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000013367 OMPC_DEFAULTMAP_MODIFIER_tofrom);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013368 Loc = MLoc;
13369 } else {
13370 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000013371 OMPC_DEFAULTMAP_scalar);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013372 Loc = KindLoc;
13373 }
13374 Value += "'";
13375 Diag(Loc, diag::err_omp_unexpected_clause_value)
13376 << Value << getOpenMPClauseName(OMPC_defaultmap);
13377 return nullptr;
13378 }
Alexey Bataev2fd0cb22017-10-05 17:51:39 +000013379 DSAStack->setDefaultDMAToFromScalar(StartLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013380
13381 return new (Context)
13382 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
13383}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013384
13385bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
13386 DeclContext *CurLexicalContext = getCurLexicalContext();
13387 if (!CurLexicalContext->isFileContext() &&
13388 !CurLexicalContext->isExternCContext() &&
Alexey Bataev502ec492017-10-03 20:00:00 +000013389 !CurLexicalContext->isExternCXXContext() &&
13390 !isa<CXXRecordDecl>(CurLexicalContext) &&
13391 !isa<ClassTemplateDecl>(CurLexicalContext) &&
13392 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) &&
13393 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013394 Diag(Loc, diag::err_omp_region_not_file_context);
13395 return false;
13396 }
Kelvin Libc38e632018-09-10 02:07:09 +000013397 ++DeclareTargetNestingLevel;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013398 return true;
13399}
13400
13401void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
Kelvin Libc38e632018-09-10 02:07:09 +000013402 assert(DeclareTargetNestingLevel > 0 &&
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013403 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
Kelvin Libc38e632018-09-10 02:07:09 +000013404 --DeclareTargetNestingLevel;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013405}
13406
David Majnemer9d168222016-08-05 17:44:54 +000013407void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope,
13408 CXXScopeSpec &ScopeSpec,
13409 const DeclarationNameInfo &Id,
13410 OMPDeclareTargetDeclAttr::MapTypeTy MT,
13411 NamedDeclSetType &SameDirectiveDecls) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013412 LookupResult Lookup(*this, Id, LookupOrdinaryName);
13413 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
13414
13415 if (Lookup.isAmbiguous())
13416 return;
13417 Lookup.suppressDiagnostics();
13418
13419 if (!Lookup.isSingleResult()) {
13420 if (TypoCorrection Corrected =
13421 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
13422 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
13423 CTK_ErrorRecovery)) {
13424 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
13425 << Id.getName());
13426 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
13427 return;
13428 }
13429
13430 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
13431 return;
13432 }
13433
13434 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
Alexey Bataev30a78212018-09-11 13:59:10 +000013435 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND) ||
13436 isa<FunctionTemplateDecl>(ND)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013437 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
13438 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
Alexey Bataev30a78212018-09-11 13:59:10 +000013439 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
13440 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
13441 cast<ValueDecl>(ND));
13442 if (!Res) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013443 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013444 ND->addAttr(A);
13445 if (ASTMutationListener *ML = Context.getASTMutationListener())
13446 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
Kelvin Li1ce87c72017-12-12 20:08:12 +000013447 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Id.getLoc());
Alexey Bataev30a78212018-09-11 13:59:10 +000013448 } else if (*Res != MT) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013449 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
13450 << Id.getName();
13451 }
Alexey Bataeve3727102018-04-18 15:57:46 +000013452 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013453 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
Alexey Bataeve3727102018-04-18 15:57:46 +000013454 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013455}
13456
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013457static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
13458 Sema &SemaRef, Decl *D) {
Alexey Bataev30a78212018-09-11 13:59:10 +000013459 if (!D || !isa<VarDecl>(D))
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013460 return;
Alexey Bataev30a78212018-09-11 13:59:10 +000013461 auto *VD = cast<VarDecl>(D);
13462 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
13463 return;
13464 SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context);
13465 SemaRef.Diag(SL, diag::note_used_here) << SR;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013466}
13467
13468static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
13469 Sema &SemaRef, DSAStackTy *Stack,
13470 ValueDecl *VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013471 return VD->hasAttr<OMPDeclareTargetDeclAttr>() ||
13472 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(),
13473 /*FullCheck=*/false);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013474}
13475
Kelvin Li1ce87c72017-12-12 20:08:12 +000013476void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D,
13477 SourceLocation IdLoc) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013478 if (!D || D->isInvalidDecl())
13479 return;
13480 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
Stephen Kellyf2ceec42018-08-09 21:08:08 +000013481 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation();
Alexey Bataeve3727102018-04-18 15:57:46 +000013482 if (auto *VD = dyn_cast<VarDecl>(D)) {
Alexey Bataevc1943e72018-07-09 19:58:08 +000013483 // Only global variables can be marked as declare target.
Alexey Bataev30a78212018-09-11 13:59:10 +000013484 if (!VD->isFileVarDecl() && !VD->isStaticLocal() &&
13485 !VD->isStaticDataMember())
Alexey Bataevc1943e72018-07-09 19:58:08 +000013486 return;
13487 // 2.10.6: threadprivate variable cannot appear in a declare target
13488 // directive.
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013489 if (DSAStack->isThreadPrivate(VD)) {
13490 Diag(SL, diag::err_omp_threadprivate_in_target);
Alexey Bataeve3727102018-04-18 15:57:46 +000013491 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013492 return;
13493 }
13494 }
Alexey Bataev97b72212018-08-14 18:31:20 +000013495 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
13496 D = FTD->getTemplatedDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000013497 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Alexey Bataev30a78212018-09-11 13:59:10 +000013498 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
13499 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD);
13500 if (Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) {
Kelvin Li1ce87c72017-12-12 20:08:12 +000013501 assert(IdLoc.isValid() && "Source location is expected");
13502 Diag(IdLoc, diag::err_omp_function_in_link_clause);
13503 Diag(FD->getLocation(), diag::note_defined_here) << FD;
13504 return;
13505 }
13506 }
Alexey Bataev30a78212018-09-11 13:59:10 +000013507 if (auto *VD = dyn_cast<ValueDecl>(D)) {
13508 // Problem if any with var declared with incomplete type will be reported
13509 // as normal, so no need to check it here.
13510 if ((E || !VD->getType()->isIncompleteType()) &&
13511 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD))
13512 return;
13513 if (!E && !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) {
13514 // Checking declaration inside declare target region.
13515 if (isa<VarDecl>(D) || isa<FunctionDecl>(D) ||
13516 isa<FunctionTemplateDecl>(D)) {
13517 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
13518 Context, OMPDeclareTargetDeclAttr::MT_To);
13519 D->addAttr(A);
13520 if (ASTMutationListener *ML = Context.getASTMutationListener())
13521 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
13522 }
13523 return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013524 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013525 }
Alexey Bataev30a78212018-09-11 13:59:10 +000013526 if (!E)
13527 return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013528 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
13529}
Samuel Antao661c0902016-05-26 17:39:58 +000013530
13531OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
13532 SourceLocation StartLoc,
13533 SourceLocation LParenLoc,
13534 SourceLocation EndLoc) {
13535 MappableVarListInfo MVLI(VarList);
13536 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc);
13537 if (MVLI.ProcessedVarList.empty())
13538 return nullptr;
13539
13540 return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13541 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
13542 MVLI.VarComponents);
13543}
Samuel Antaoec172c62016-05-26 17:49:04 +000013544
13545OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
13546 SourceLocation StartLoc,
13547 SourceLocation LParenLoc,
13548 SourceLocation EndLoc) {
13549 MappableVarListInfo MVLI(VarList);
13550 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc);
13551 if (MVLI.ProcessedVarList.empty())
13552 return nullptr;
13553
13554 return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13555 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
13556 MVLI.VarComponents);
13557}
Carlo Bertolli2404b172016-07-13 15:37:16 +000013558
13559OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
13560 SourceLocation StartLoc,
13561 SourceLocation LParenLoc,
13562 SourceLocation EndLoc) {
Samuel Antaocc10b852016-07-28 14:23:26 +000013563 MappableVarListInfo MVLI(VarList);
13564 SmallVector<Expr *, 8> PrivateCopies;
13565 SmallVector<Expr *, 8> Inits;
13566
Alexey Bataeve3727102018-04-18 15:57:46 +000013567 for (Expr *RefExpr : VarList) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000013568 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
13569 SourceLocation ELoc;
13570 SourceRange ERange;
13571 Expr *SimpleRefExpr = RefExpr;
13572 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13573 if (Res.second) {
13574 // It will be analyzed later.
Samuel Antaocc10b852016-07-28 14:23:26 +000013575 MVLI.ProcessedVarList.push_back(RefExpr);
13576 PrivateCopies.push_back(nullptr);
13577 Inits.push_back(nullptr);
Carlo Bertolli2404b172016-07-13 15:37:16 +000013578 }
13579 ValueDecl *D = Res.first;
13580 if (!D)
13581 continue;
13582
13583 QualType Type = D->getType();
Samuel Antaocc10b852016-07-28 14:23:26 +000013584 Type = Type.getNonReferenceType().getUnqualifiedType();
13585
13586 auto *VD = dyn_cast<VarDecl>(D);
13587
13588 // Item should be a pointer or reference to pointer.
13589 if (!Type->isPointerType()) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000013590 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
13591 << 0 << RefExpr->getSourceRange();
13592 continue;
13593 }
Samuel Antaocc10b852016-07-28 14:23:26 +000013594
13595 // Build the private variable and the expression that refers to it.
Alexey Bataev63cc8e92018-03-20 14:45:59 +000013596 auto VDPrivate =
13597 buildVarDecl(*this, ELoc, Type, D->getName(),
13598 D->hasAttrs() ? &D->getAttrs() : nullptr,
13599 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Samuel Antaocc10b852016-07-28 14:23:26 +000013600 if (VDPrivate->isInvalidDecl())
13601 continue;
13602
13603 CurContext->addDecl(VDPrivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000013604 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Samuel Antaocc10b852016-07-28 14:23:26 +000013605 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
13606
13607 // Add temporary variable to initialize the private copy of the pointer.
Alexey Bataeve3727102018-04-18 15:57:46 +000013608 VarDecl *VDInit =
Samuel Antaocc10b852016-07-28 14:23:26 +000013609 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
Alexey Bataeve3727102018-04-18 15:57:46 +000013610 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr(
13611 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc());
Samuel Antaocc10b852016-07-28 14:23:26 +000013612 AddInitializerToDecl(VDPrivate,
13613 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000013614 /*DirectInit=*/false);
Samuel Antaocc10b852016-07-28 14:23:26 +000013615
13616 // If required, build a capture to implement the privatization initialized
13617 // with the current list item value.
13618 DeclRefExpr *Ref = nullptr;
13619 if (!VD)
13620 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
13621 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
13622 PrivateCopies.push_back(VDPrivateRefExpr);
13623 Inits.push_back(VDInitRefExpr);
13624
13625 // We need to add a data sharing attribute for this variable to make sure it
13626 // is correctly captured. A variable that shows up in a use_device_ptr has
13627 // similar properties of a first private variable.
13628 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
13629
13630 // Create a mappable component for the list item. List items in this clause
13631 // only need a component.
13632 MVLI.VarBaseDeclarations.push_back(D);
13633 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13634 MVLI.VarComponents.back().push_back(
13635 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D));
Carlo Bertolli2404b172016-07-13 15:37:16 +000013636 }
13637
Samuel Antaocc10b852016-07-28 14:23:26 +000013638 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli2404b172016-07-13 15:37:16 +000013639 return nullptr;
13640
Samuel Antaocc10b852016-07-28 14:23:26 +000013641 return OMPUseDevicePtrClause::Create(
13642 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
13643 PrivateCopies, Inits, MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli2404b172016-07-13 15:37:16 +000013644}
Carlo Bertolli70594e92016-07-13 17:16:49 +000013645
13646OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
13647 SourceLocation StartLoc,
13648 SourceLocation LParenLoc,
13649 SourceLocation EndLoc) {
Samuel Antao6890b092016-07-28 14:25:09 +000013650 MappableVarListInfo MVLI(VarList);
Alexey Bataeve3727102018-04-18 15:57:46 +000013651 for (Expr *RefExpr : VarList) {
Kelvin Li84376252016-12-14 15:39:58 +000013652 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause.");
Carlo Bertolli70594e92016-07-13 17:16:49 +000013653 SourceLocation ELoc;
13654 SourceRange ERange;
13655 Expr *SimpleRefExpr = RefExpr;
13656 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13657 if (Res.second) {
13658 // It will be analyzed later.
Samuel Antao6890b092016-07-28 14:25:09 +000013659 MVLI.ProcessedVarList.push_back(RefExpr);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013660 }
13661 ValueDecl *D = Res.first;
13662 if (!D)
13663 continue;
13664
13665 QualType Type = D->getType();
13666 // item should be a pointer or array or reference to pointer or array
13667 if (!Type.getNonReferenceType()->isPointerType() &&
13668 !Type.getNonReferenceType()->isArrayType()) {
13669 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
13670 << 0 << RefExpr->getSourceRange();
13671 continue;
13672 }
Samuel Antao6890b092016-07-28 14:25:09 +000013673
13674 // Check if the declaration in the clause does not show up in any data
13675 // sharing attribute.
Alexey Bataeve3727102018-04-18 15:57:46 +000013676 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Samuel Antao6890b092016-07-28 14:25:09 +000013677 if (isOpenMPPrivate(DVar.CKind)) {
13678 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
13679 << getOpenMPClauseName(DVar.CKind)
13680 << getOpenMPClauseName(OMPC_is_device_ptr)
13681 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000013682 reportOriginalDsa(*this, DSAStack, D, DVar);
Samuel Antao6890b092016-07-28 14:25:09 +000013683 continue;
13684 }
13685
Alexey Bataeve3727102018-04-18 15:57:46 +000013686 const Expr *ConflictExpr;
Samuel Antao6890b092016-07-28 14:25:09 +000013687 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +000013688 D, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +000013689 [&ConflictExpr](
13690 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
13691 OpenMPClauseKind) -> bool {
13692 ConflictExpr = R.front().getAssociatedExpression();
13693 return true;
13694 })) {
13695 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
13696 Diag(ConflictExpr->getExprLoc(), diag::note_used_here)
13697 << ConflictExpr->getSourceRange();
13698 continue;
13699 }
13700
13701 // Store the components in the stack so that they can be used to check
13702 // against other clauses later on.
13703 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D);
13704 DSAStack->addMappableExpressionComponents(
13705 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
13706
13707 // Record the expression we've just processed.
13708 MVLI.ProcessedVarList.push_back(SimpleRefExpr);
13709
13710 // Create a mappable component for the list item. List items in this clause
13711 // only need a component. We use a null declaration to signal fields in
13712 // 'this'.
13713 assert((isa<DeclRefExpr>(SimpleRefExpr) ||
13714 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
13715 "Unexpected device pointer expression!");
13716 MVLI.VarBaseDeclarations.push_back(
13717 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr);
13718 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13719 MVLI.VarComponents.back().push_back(MC);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013720 }
13721
Samuel Antao6890b092016-07-28 14:25:09 +000013722 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli70594e92016-07-13 17:16:49 +000013723 return nullptr;
13724
Samuel Antao6890b092016-07-28 14:25:09 +000013725 return OMPIsDevicePtrClause::Create(
13726 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
13727 MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013728}