blob: 84c3023081ae74f3ff1344a5e456a8fd880d5d29 [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);
Alexey Bataev92b33652018-11-21 19:41:10 +00001278 if ((I != StartI->SharingMap.end()) &&
Alexey Bataeve3727102018-04-18 15:57:46 +00001279 I->getSecond().RefExpr.getPointer() &&
1280 CPred(I->getSecond().Attributes) &&
Alexey Bataev92b33652018-11-21 19:41:10 +00001281 (!NotLastprivate || !I->getSecond().RefExpr.getInt()))
1282 return true;
1283 // Check predetermined rules for the loop control variables.
1284 auto LI = StartI->LCVMap.find(D);
1285 if (LI != StartI->LCVMap.end())
1286 return CPred(OMPC_private);
1287 return false;
Alexey Bataevaac108a2015-06-23 04:51:00 +00001288}
1289
Samuel Antao4be30e92015-10-02 17:14:03 +00001290bool DSAStackTy::hasExplicitDirective(
Alexey Bataeve3727102018-04-18 15:57:46 +00001291 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1292 unsigned Level) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001293 if (isStackEmpty())
1294 return false;
1295 auto StartI = Stack.back().first.begin();
1296 auto EndI = Stack.back().first.end();
Samuel Antao4be30e92015-10-02 17:14:03 +00001297 if (std::distance(StartI, EndI) <= (int)Level)
1298 return false;
1299 std::advance(StartI, Level);
1300 return DPred(StartI->Directive);
1301}
1302
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001303bool DSAStackTy::hasDirective(
1304 const llvm::function_ref<bool(OpenMPDirectiveKind,
1305 const DeclarationNameInfo &, SourceLocation)>
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001306 DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001307 bool FromParent) const {
Samuel Antaof0d79752016-05-27 15:21:27 +00001308 // We look only in the enclosing region.
Alexey Bataev4b465392017-04-26 15:06:24 +00001309 if (isStackEmpty())
Samuel Antaof0d79752016-05-27 15:21:27 +00001310 return false;
Alexey Bataev4b465392017-04-26 15:06:24 +00001311 auto StartI = std::next(Stack.back().first.rbegin());
1312 auto EndI = Stack.back().first.rend();
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001313 if (FromParent && StartI != EndI)
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001314 StartI = std::next(StartI);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001315 for (auto I = StartI, EE = EndI; I != EE; ++I) {
1316 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
1317 return true;
1318 }
1319 return false;
1320}
1321
Alexey Bataev758e55e2013-09-06 18:03:48 +00001322void Sema::InitDataSharingAttributesStack() {
1323 VarDataSharingAttributesStack = new DSAStackTy(*this);
1324}
1325
1326#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
1327
Alexey Bataev4b465392017-04-26 15:06:24 +00001328void Sema::pushOpenMPFunctionRegion() {
1329 DSAStack->pushFunction();
1330}
1331
1332void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) {
1333 DSAStack->popFunction(OldFSI);
1334}
1335
Alexey Bataeve3727102018-04-18 15:57:46 +00001336bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level) const {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001337 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1338
Alexey Bataeve3727102018-04-18 15:57:46 +00001339 ASTContext &Ctx = getASTContext();
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001340 bool IsByRef = true;
1341
1342 // Find the directive that is associated with the provided scope.
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00001343 D = cast<ValueDecl>(D->getCanonicalDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00001344 QualType Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001345
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001346 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001347 // This table summarizes how a given variable should be passed to the device
1348 // given its type and the clauses where it appears. This table is based on
1349 // the description in OpenMP 4.5 [2.10.4, target Construct] and
1350 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
1351 //
1352 // =========================================================================
1353 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
1354 // | |(tofrom:scalar)| | pvt | | | |
1355 // =========================================================================
1356 // | scl | | | | - | | bycopy|
1357 // | scl | | - | x | - | - | bycopy|
1358 // | scl | | x | - | - | - | null |
1359 // | scl | x | | | - | | byref |
1360 // | scl | x | - | x | - | - | bycopy|
1361 // | scl | x | x | - | - | - | null |
1362 // | scl | | - | - | - | x | byref |
1363 // | scl | x | - | - | - | x | byref |
1364 //
1365 // | agg | n.a. | | | - | | byref |
1366 // | agg | n.a. | - | x | - | - | byref |
1367 // | agg | n.a. | x | - | - | - | null |
1368 // | agg | n.a. | - | - | - | x | byref |
1369 // | agg | n.a. | - | - | - | x[] | byref |
1370 //
1371 // | ptr | n.a. | | | - | | bycopy|
1372 // | ptr | n.a. | - | x | - | - | bycopy|
1373 // | ptr | n.a. | x | - | - | - | null |
1374 // | ptr | n.a. | - | - | - | x | byref |
1375 // | ptr | n.a. | - | - | - | x[] | bycopy|
1376 // | ptr | n.a. | - | - | x | | bycopy|
1377 // | ptr | n.a. | - | - | x | x | bycopy|
1378 // | ptr | n.a. | - | - | x | x[] | bycopy|
1379 // =========================================================================
1380 // Legend:
1381 // scl - scalar
1382 // ptr - pointer
1383 // agg - aggregate
1384 // x - applies
1385 // - - invalid in this combination
1386 // [] - mapped with an array section
1387 // byref - should be mapped by reference
1388 // byval - should be mapped by value
1389 // null - initialize a local variable to null on the device
1390 //
1391 // Observations:
1392 // - All scalar declarations that show up in a map clause have to be passed
1393 // by reference, because they may have been mapped in the enclosing data
1394 // environment.
1395 // - If the scalar value does not fit the size of uintptr, it has to be
1396 // passed by reference, regardless the result in the table above.
1397 // - For pointers mapped by value that have either an implicit map or an
1398 // array section, the runtime library may pass the NULL value to the
1399 // device instead of the value passed to it by the compiler.
1400
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001401 if (Ty->isReferenceType())
1402 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
Samuel Antao86ace552016-04-27 22:40:57 +00001403
1404 // Locate map clauses and see if the variable being captured is referred to
1405 // in any of those clauses. Here we only care about variables, not fields,
1406 // because fields are part of aggregates.
1407 bool IsVariableUsedInMapClause = false;
1408 bool IsVariableAssociatedWithSection = false;
1409
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +00001410 DSAStack->checkMappableExprComponentListsForDeclAtLevel(
Alexey Bataeve3727102018-04-18 15:57:46 +00001411 D, Level,
1412 [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D](
1413 OMPClauseMappableExprCommon::MappableExprComponentListRef
Samuel Antao6890b092016-07-28 14:25:09 +00001414 MapExprComponents,
1415 OpenMPClauseKind WhereFoundClauseKind) {
1416 // Only the map clause information influences how a variable is
1417 // captured. E.g. is_device_ptr does not require changing the default
Samuel Antao4c8035b2016-12-12 18:00:20 +00001418 // behavior.
Samuel Antao6890b092016-07-28 14:25:09 +00001419 if (WhereFoundClauseKind != OMPC_map)
1420 return false;
Samuel Antao86ace552016-04-27 22:40:57 +00001421
1422 auto EI = MapExprComponents.rbegin();
1423 auto EE = MapExprComponents.rend();
1424
1425 assert(EI != EE && "Invalid map expression!");
1426
1427 if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
1428 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
1429
1430 ++EI;
1431 if (EI == EE)
1432 return false;
1433
1434 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
1435 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
1436 isa<MemberExpr>(EI->getAssociatedExpression())) {
1437 IsVariableAssociatedWithSection = true;
1438 // There is nothing more we need to know about this variable.
1439 return true;
1440 }
1441
1442 // Keep looking for more map info.
1443 return false;
1444 });
1445
1446 if (IsVariableUsedInMapClause) {
1447 // If variable is identified in a map clause it is always captured by
1448 // reference except if it is a pointer that is dereferenced somehow.
1449 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
1450 } else {
Alexey Bataev3f96fe62017-12-13 17:31:39 +00001451 // By default, all the data that has a scalar type is mapped by copy
1452 // (except for reduction variables).
1453 IsByRef =
Alexey Bataev60705422018-10-30 15:50:12 +00001454 (DSAStack->isForceCaptureByReferenceInTargetExecutable() &&
1455 !Ty->isAnyPointerType()) ||
Alexey Bataev3f96fe62017-12-13 17:31:39 +00001456 !Ty->isScalarType() ||
1457 DSAStack->getDefaultDMAAtLevel(Level) == DMA_tofrom_scalar ||
1458 DSAStack->hasExplicitDSA(
1459 D, [](OpenMPClauseKind K) { return K == OMPC_reduction; }, Level);
Samuel Antao86ace552016-04-27 22:40:57 +00001460 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001461 }
1462
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001463 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00001464 IsByRef =
Alexey Bataev60705422018-10-30 15:50:12 +00001465 ((DSAStack->isForceCaptureByReferenceInTargetExecutable() &&
1466 !Ty->isAnyPointerType()) ||
1467 !DSAStack->hasExplicitDSA(
1468 D,
1469 [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
1470 Level, /*NotLastprivate=*/true)) &&
Alexey Bataev8e769ee2017-12-22 21:01:52 +00001471 // If the variable is artificial and must be captured by value - try to
1472 // capture by value.
Alexey Bataevd2202ca2017-12-27 17:58:32 +00001473 !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() &&
1474 !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue());
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001475 }
1476
Samuel Antao86ace552016-04-27 22:40:57 +00001477 // When passing data by copy, we need to make sure it fits the uintptr size
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001478 // and alignment, because the runtime library only deals with uintptr types.
1479 // If it does not fit the uintptr size, we need to pass the data by reference
1480 // instead.
1481 if (!IsByRef &&
1482 (Ctx.getTypeSizeInChars(Ty) >
1483 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001484 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001485 IsByRef = true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001486 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001487
1488 return IsByRef;
1489}
1490
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001491unsigned Sema::getOpenMPNestingLevel() const {
1492 assert(getLangOpts().OpenMP);
1493 return DSAStack->getNestingLevel();
1494}
1495
Jonas Hahnfeld87d44262017-11-18 21:00:46 +00001496bool Sema::isInOpenMPTargetExecutionDirective() const {
1497 return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) &&
1498 !DSAStack->isClauseParsingMode()) ||
1499 DSAStack->hasDirective(
1500 [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
1501 SourceLocation) -> bool {
1502 return isOpenMPTargetExecutionDirective(K);
1503 },
1504 false);
1505}
1506
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001507VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +00001508 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001509 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +00001510
1511 // If we are attempting to capture a global variable in a directive with
1512 // 'target' we return true so that this global is also mapped to the device.
1513 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001514 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001515 if (VD && !VD->hasLocalStorage()) {
1516 if (isInOpenMPDeclareTargetContext() &&
1517 (getCurCapturedRegion() || getCurBlock() || getCurLambda())) {
1518 // Try to mark variable as declare target if it is used in capturing
1519 // regions.
Alexey Bataev97b72212018-08-14 18:31:20 +00001520 if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001521 checkDeclIsAllowedInOpenMPTarget(nullptr, VD);
Alexey Bataev92327c52018-03-26 16:40:55 +00001522 return nullptr;
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001523 } else if (isInOpenMPTargetExecutionDirective()) {
1524 // If the declaration is enclosed in a 'declare target' directive,
1525 // then it should not be captured.
1526 //
Alexey Bataev97b72212018-08-14 18:31:20 +00001527 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001528 return nullptr;
1529 return VD;
1530 }
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001531 }
Alexey Bataev60705422018-10-30 15:50:12 +00001532 // Capture variables captured by reference in lambdas for target-based
1533 // directives.
1534 if (VD && !DSAStack->isClauseParsingMode()) {
1535 if (const auto *RD = VD->getType()
1536 .getCanonicalType()
1537 .getNonReferenceType()
1538 ->getAsCXXRecordDecl()) {
1539 bool SavedForceCaptureByReferenceInTargetExecutable =
1540 DSAStack->isForceCaptureByReferenceInTargetExecutable();
1541 DSAStack->setForceCaptureByReferenceInTargetExecutable(/*V=*/true);
Alexey Bataevd1840e52018-11-16 21:13:33 +00001542 if (RD->isLambda()) {
1543 llvm::DenseMap<const VarDecl *, FieldDecl *> Captures;
1544 FieldDecl *ThisCapture;
1545 RD->getCaptureFields(Captures, ThisCapture);
Alexey Bataev60705422018-10-30 15:50:12 +00001546 for (const LambdaCapture &LC : RD->captures()) {
1547 if (LC.getCaptureKind() == LCK_ByRef) {
1548 VarDecl *VD = LC.getCapturedVar();
1549 DeclContext *VDC = VD->getDeclContext();
1550 if (!VDC->Encloses(CurContext))
1551 continue;
1552 DSAStackTy::DSAVarData DVarPrivate =
1553 DSAStack->getTopDSA(VD, /*FromParent=*/false);
1554 // Do not capture already captured variables.
1555 if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) &&
1556 DVarPrivate.CKind == OMPC_unknown &&
1557 !DSAStack->checkMappableExprComponentListsForDecl(
1558 D, /*CurrentRegionOnly=*/true,
1559 [](OMPClauseMappableExprCommon::
1560 MappableExprComponentListRef,
1561 OpenMPClauseKind) { return true; }))
1562 MarkVariableReferenced(LC.getLocation(), LC.getCapturedVar());
1563 } else if (LC.getCaptureKind() == LCK_This) {
Alexey Bataevd1840e52018-11-16 21:13:33 +00001564 QualType ThisTy = getCurrentThisType();
1565 if (!ThisTy.isNull() &&
1566 Context.typesAreCompatible(ThisTy, ThisCapture->getType()))
1567 CheckCXXThisCapture(LC.getLocation());
Alexey Bataev60705422018-10-30 15:50:12 +00001568 }
1569 }
Alexey Bataevd1840e52018-11-16 21:13:33 +00001570 }
Alexey Bataev60705422018-10-30 15:50:12 +00001571 DSAStack->setForceCaptureByReferenceInTargetExecutable(
1572 SavedForceCaptureByReferenceInTargetExecutable);
1573 }
1574 }
Samuel Antao4be30e92015-10-02 17:14:03 +00001575
Alexey Bataev48977c32015-08-04 08:10:48 +00001576 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
1577 (!DSAStack->isClauseParsingMode() ||
1578 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001579 auto &&Info = DSAStack->isLoopControlVariable(D);
1580 if (Info.first ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001581 (VD && VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001582 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001583 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001584 return VD ? VD : Info.second;
Alexey Bataeve3727102018-04-18 15:57:46 +00001585 DSAStackTy::DSAVarData DVarPrivate =
1586 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001587 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +00001588 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00001589 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate,
1590 [](OpenMPDirectiveKind) { return true; },
1591 DSAStack->isClauseParsingMode());
Alexey Bataev90c228f2016-02-08 09:29:13 +00001592 if (DVarPrivate.CKind != OMPC_unknown)
1593 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001594 }
Alexey Bataev90c228f2016-02-08 09:29:13 +00001595 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +00001596}
1597
Alexey Bataevdfa430f2017-12-08 15:03:50 +00001598void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex,
1599 unsigned Level) const {
1600 SmallVector<OpenMPDirectiveKind, 4> Regions;
1601 getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level));
1602 FunctionScopesIndex -= Regions.size();
1603}
1604
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00001605void Sema::startOpenMPLoop() {
1606 assert(LangOpts.OpenMP && "OpenMP must be enabled.");
1607 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective()))
1608 DSAStack->loopInit();
1609}
1610
Alexey Bataeve3727102018-04-18 15:57:46 +00001611bool Sema::isOpenMPPrivateDecl(const ValueDecl *D, unsigned Level) const {
Alexey Bataevaac108a2015-06-23 04:51:00 +00001612 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00001613 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
1614 if (DSAStack->getAssociatedLoops() > 0 &&
1615 !DSAStack->isLoopStarted()) {
1616 DSAStack->resetPossibleLoopCounter(D);
1617 DSAStack->loopStart();
1618 return true;
1619 }
1620 if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() ||
1621 DSAStack->isLoopControlVariable(D).first) &&
1622 !DSAStack->hasExplicitDSA(
1623 D, [](OpenMPClauseKind K) { return K != OMPC_private; }, Level) &&
1624 !isOpenMPSimdDirective(DSAStack->getCurrentDirective()))
1625 return true;
1626 }
Alexey Bataevaac108a2015-06-23 04:51:00 +00001627 return DSAStack->hasExplicitDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00001628 D, [](OpenMPClauseKind K) { return K == OMPC_private; }, Level) ||
Alexey Bataev3f82cfc2017-12-13 15:28:44 +00001629 (DSAStack->isClauseParsingMode() &&
1630 DSAStack->getClauseParsingMode() == OMPC_private) ||
Alexey Bataev88202be2017-07-27 13:20:36 +00001631 // Consider taskgroup reduction descriptor variable a private to avoid
1632 // possible capture in the region.
1633 (DSAStack->hasExplicitDirective(
1634 [](OpenMPDirectiveKind K) { return K == OMPD_taskgroup; },
1635 Level) &&
1636 DSAStack->isTaskgroupReductionRef(D, Level));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001637}
1638
Alexey Bataeve3727102018-04-18 15:57:46 +00001639void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D,
1640 unsigned Level) {
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001641 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1642 D = getCanonicalDecl(D);
1643 OpenMPClauseKind OMPC = OMPC_unknown;
1644 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) {
1645 const unsigned NewLevel = I - 1;
1646 if (DSAStack->hasExplicitDSA(D,
1647 [&OMPC](const OpenMPClauseKind K) {
1648 if (isOpenMPPrivate(K)) {
1649 OMPC = K;
1650 return true;
1651 }
1652 return false;
1653 },
1654 NewLevel))
1655 break;
1656 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel(
1657 D, NewLevel,
1658 [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
1659 OpenMPClauseKind) { return true; })) {
1660 OMPC = OMPC_map;
1661 break;
1662 }
1663 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1664 NewLevel)) {
Alexey Bataev63cc8e92018-03-20 14:45:59 +00001665 OMPC = OMPC_map;
1666 if (D->getType()->isScalarType() &&
1667 DSAStack->getDefaultDMAAtLevel(NewLevel) !=
1668 DefaultMapAttributes::DMA_tofrom_scalar)
1669 OMPC = OMPC_firstprivate;
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001670 break;
1671 }
1672 }
1673 if (OMPC != OMPC_unknown)
1674 FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, OMPC));
1675}
1676
Alexey Bataeve3727102018-04-18 15:57:46 +00001677bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D,
1678 unsigned Level) const {
Samuel Antao4be30e92015-10-02 17:14:03 +00001679 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1680 // Return true if the current level is no longer enclosed in a target region.
1681
Alexey Bataeve3727102018-04-18 15:57:46 +00001682 const auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001683 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00001684 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1685 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +00001686}
1687
Alexey Bataeved09d242014-05-28 05:53:51 +00001688void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001689
1690void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
1691 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001692 Scope *CurScope, SourceLocation Loc) {
1693 DSAStack->push(DKind, DirName, CurScope, Loc);
Faisal Valid143a0c2017-04-01 21:30:49 +00001694 PushExpressionEvaluationContext(
1695 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001696}
1697
Alexey Bataevaac108a2015-06-23 04:51:00 +00001698void Sema::StartOpenMPClause(OpenMPClauseKind K) {
1699 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001700}
1701
Alexey Bataevaac108a2015-06-23 04:51:00 +00001702void Sema::EndOpenMPClause() {
1703 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001704}
1705
Alexey Bataev758e55e2013-09-06 18:03:48 +00001706void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001707 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
1708 // A variable of class type (or array thereof) that appears in a lastprivate
1709 // clause requires an accessible, unambiguous default constructor for the
1710 // class type, unless the list item is also specified in a firstprivate
1711 // clause.
Alexey Bataeve3727102018-04-18 15:57:46 +00001712 if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
1713 for (OMPClause *C : D->clauses()) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001714 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
1715 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeve3727102018-04-18 15:57:46 +00001716 for (Expr *DE : Clause->varlists()) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001717 if (DE->isValueDependent() || DE->isTypeDependent()) {
1718 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001719 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +00001720 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00001721 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
Alexey Bataeve3727102018-04-18 15:57:46 +00001722 auto *VD = cast<VarDecl>(DRE->getDecl());
Alexey Bataev005248a2016-02-25 05:25:57 +00001723 QualType Type = VD->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +00001724 const DSAStackTy::DSAVarData DVar =
1725 DSAStack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001726 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001727 // Generate helper private variable and initialize it with the
1728 // default value. The address of the original variable is replaced
1729 // by the address of the new private variable in CodeGen. This new
1730 // variable is not added to IdResolver, so the code in the OpenMP
1731 // region uses original variable for proper diagnostics.
Alexey Bataeve3727102018-04-18 15:57:46 +00001732 VarDecl *VDPrivate = buildVarDecl(
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001733 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev63cc8e92018-03-20 14:45:59 +00001734 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE);
Richard Smith3beb7c62017-01-12 02:27:38 +00001735 ActOnUninitializedDecl(VDPrivate);
Alexey Bataev38e89532015-04-16 04:54:05 +00001736 if (VDPrivate->isInvalidDecl())
1737 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001738 PrivateCopies.push_back(buildDeclRefExpr(
1739 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001740 } else {
1741 // The variable is also a firstprivate, so initialization sequence
1742 // for private copy is generated already.
1743 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001744 }
1745 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001746 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001747 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001748 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001749 }
1750 }
1751 }
1752
Alexey Bataev758e55e2013-09-06 18:03:48 +00001753 DSAStack->pop();
1754 DiscardCleanupsInEvaluationContext();
1755 PopExpressionEvaluationContext();
1756}
1757
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001758static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1759 Expr *NumIterations, Sema &SemaRef,
1760 Scope *S, DSAStackTy *Stack);
Alexander Musman3276a272015-03-21 10:12:56 +00001761
Alexey Bataeva769e072013-03-22 06:34:35 +00001762namespace {
1763
Alexey Bataeve3727102018-04-18 15:57:46 +00001764class VarDeclFilterCCC final : public CorrectionCandidateCallback {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001765private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001766 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001767
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001768public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001769 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001770 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001771 NamedDecl *ND = Candidate.getCorrectionDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +00001772 if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001773 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001774 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1775 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001776 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001777 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001778 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001779};
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001780
Alexey Bataeve3727102018-04-18 15:57:46 +00001781class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001782private:
1783 Sema &SemaRef;
1784
1785public:
1786 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
1787 bool ValidateCandidate(const TypoCorrection &Candidate) override {
1788 NamedDecl *ND = Candidate.getCorrectionDecl();
Kelvin Li59e3d192017-11-30 18:52:06 +00001789 if (ND && (isa<VarDecl>(ND) || isa<FunctionDecl>(ND))) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001790 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1791 SemaRef.getCurScope());
1792 }
1793 return false;
1794 }
1795};
1796
Alexey Bataeved09d242014-05-28 05:53:51 +00001797} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001798
1799ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1800 CXXScopeSpec &ScopeSpec,
1801 const DeclarationNameInfo &Id) {
1802 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1803 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1804
1805 if (Lookup.isAmbiguous())
1806 return ExprError();
1807
1808 VarDecl *VD;
1809 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001810 if (TypoCorrection Corrected = CorrectTypo(
1811 Id, LookupOrdinaryName, CurScope, nullptr,
1812 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001813 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001814 PDiag(Lookup.empty()
1815 ? diag::err_undeclared_var_use_suggest
1816 : diag::err_omp_expected_var_arg_suggest)
1817 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001818 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001819 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001820 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1821 : diag::err_omp_expected_var_arg)
1822 << Id.getName();
1823 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001824 }
Alexey Bataeve3727102018-04-18 15:57:46 +00001825 } else if (!(VD = Lookup.getAsSingle<VarDecl>())) {
1826 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
1827 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1828 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001829 }
1830 Lookup.suppressDiagnostics();
1831
1832 // OpenMP [2.9.2, Syntax, C/C++]
1833 // Variables must be file-scope, namespace-scope, or static block-scope.
1834 if (!VD->hasGlobalStorage()) {
1835 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001836 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1837 bool IsDecl =
1838 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001839 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001840 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1841 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001842 return ExprError();
1843 }
1844
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001845 VarDecl *CanonicalVD = VD->getCanonicalDecl();
George Burgess IV00f70bd2018-03-01 05:43:23 +00001846 NamedDecl *ND = CanonicalVD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001847 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1848 // A threadprivate directive for file-scope variables must appear outside
1849 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001850 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1851 !getCurLexicalContext()->isTranslationUnit()) {
1852 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001853 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1854 bool IsDecl =
1855 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1856 Diag(VD->getLocation(),
1857 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1858 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001859 return ExprError();
1860 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001861 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1862 // A threadprivate directive for static class member variables must appear
1863 // in the class definition, in the same scope in which the member
1864 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001865 if (CanonicalVD->isStaticDataMember() &&
1866 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1867 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001868 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1869 bool IsDecl =
1870 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1871 Diag(VD->getLocation(),
1872 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1873 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001874 return ExprError();
1875 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001876 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1877 // A threadprivate directive for namespace-scope variables must appear
1878 // outside any definition or declaration other than the namespace
1879 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001880 if (CanonicalVD->getDeclContext()->isNamespace() &&
1881 (!getCurLexicalContext()->isFileContext() ||
1882 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1883 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001884 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1885 bool IsDecl =
1886 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1887 Diag(VD->getLocation(),
1888 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1889 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001890 return ExprError();
1891 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001892 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1893 // A threadprivate directive for static block-scope variables must appear
1894 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001895 if (CanonicalVD->isStaticLocal() && CurScope &&
1896 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001897 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001898 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1899 bool IsDecl =
1900 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1901 Diag(VD->getLocation(),
1902 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1903 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001904 return ExprError();
1905 }
1906
1907 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1908 // A threadprivate directive must lexically precede all references to any
1909 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001910 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001911 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001912 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001913 return ExprError();
1914 }
1915
1916 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00001917 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1918 SourceLocation(), VD,
1919 /*RefersToEnclosingVariableOrCapture=*/false,
1920 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001921}
1922
Alexey Bataeved09d242014-05-28 05:53:51 +00001923Sema::DeclGroupPtrTy
1924Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1925 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001926 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001927 CurContext->addDecl(D);
1928 return DeclGroupPtrTy::make(DeclGroupRef(D));
1929 }
David Blaikie0403cb12016-01-15 23:43:25 +00001930 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001931}
1932
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001933namespace {
Alexey Bataeve3727102018-04-18 15:57:46 +00001934class LocalVarRefChecker final
1935 : public ConstStmtVisitor<LocalVarRefChecker, bool> {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001936 Sema &SemaRef;
1937
1938public:
1939 bool VisitDeclRefExpr(const DeclRefExpr *E) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001940 if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001941 if (VD->hasLocalStorage()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001942 SemaRef.Diag(E->getBeginLoc(),
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001943 diag::err_omp_local_var_in_threadprivate_init)
1944 << E->getSourceRange();
1945 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1946 << VD << VD->getSourceRange();
1947 return true;
1948 }
1949 }
1950 return false;
1951 }
1952 bool VisitStmt(const Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001953 for (const Stmt *Child : S->children()) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001954 if (Child && Visit(Child))
1955 return true;
1956 }
1957 return false;
1958 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001959 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001960};
1961} // namespace
1962
Alexey Bataeved09d242014-05-28 05:53:51 +00001963OMPThreadPrivateDecl *
1964Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001965 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +00001966 for (Expr *RefExpr : VarList) {
1967 auto *DE = cast<DeclRefExpr>(RefExpr);
1968 auto *VD = cast<VarDecl>(DE->getDecl());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001969 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001970
Alexey Bataev376b4a42016-02-09 09:41:09 +00001971 // Mark variable as used.
1972 VD->setReferenced();
1973 VD->markUsed(Context);
1974
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001975 QualType QType = VD->getType();
1976 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1977 // It will be analyzed later.
1978 Vars.push_back(DE);
1979 continue;
1980 }
1981
Alexey Bataeva769e072013-03-22 06:34:35 +00001982 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1983 // A threadprivate variable must not have an incomplete type.
1984 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001985 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001986 continue;
1987 }
1988
1989 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1990 // A threadprivate variable must not have a reference type.
1991 if (VD->getType()->isReferenceType()) {
1992 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001993 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1994 bool IsDecl =
1995 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1996 Diag(VD->getLocation(),
1997 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1998 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001999 continue;
2000 }
2001
Samuel Antaof8b50122015-07-13 22:54:53 +00002002 // Check if this is a TLS variable. If TLS is not being supported, produce
2003 // the corresponding diagnostic.
2004 if ((VD->getTLSKind() != VarDecl::TLS_None &&
2005 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
2006 getLangOpts().OpenMPUseTLS &&
2007 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00002008 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
2009 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00002010 Diag(ILoc, diag::err_omp_var_thread_local)
2011 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00002012 bool IsDecl =
2013 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2014 Diag(VD->getLocation(),
2015 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2016 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00002017 continue;
2018 }
2019
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002020 // Check if initial value of threadprivate variable reference variable with
2021 // local storage (it is not supported by runtime).
Alexey Bataeve3727102018-04-18 15:57:46 +00002022 if (const Expr *Init = VD->getAnyInitializer()) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002023 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002024 if (Checker.Visit(Init))
2025 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002026 }
2027
Alexey Bataeved09d242014-05-28 05:53:51 +00002028 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00002029 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00002030 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
2031 Context, SourceRange(Loc, Loc)));
Alexey Bataeve3727102018-04-18 15:57:46 +00002032 if (ASTMutationListener *ML = Context.getASTMutationListener())
Alexey Bataev97720002014-11-11 04:05:39 +00002033 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00002034 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002035 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00002036 if (!Vars.empty()) {
2037 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
2038 Vars);
2039 D->setAccess(AS_public);
2040 }
2041 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00002042}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002043
Kelvin Li1408f912018-09-26 04:28:39 +00002044Sema::DeclGroupPtrTy
2045Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc,
2046 ArrayRef<OMPClause *> ClauseList) {
2047 OMPRequiresDecl *D = nullptr;
2048 if (!CurContext->isFileContext()) {
2049 Diag(Loc, diag::err_omp_invalid_scope) << "requires";
2050 } else {
2051 D = CheckOMPRequiresDecl(Loc, ClauseList);
2052 if (D) {
2053 CurContext->addDecl(D);
2054 DSAStack->addRequiresDecl(D);
2055 }
2056 }
2057 return DeclGroupPtrTy::make(DeclGroupRef(D));
2058}
2059
2060OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc,
2061 ArrayRef<OMPClause *> ClauseList) {
2062 if (!DSAStack->hasDuplicateRequiresClause(ClauseList))
2063 return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc,
2064 ClauseList);
2065 return nullptr;
2066}
2067
Alexey Bataeve3727102018-04-18 15:57:46 +00002068static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
2069 const ValueDecl *D,
2070 const DSAStackTy::DSAVarData &DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00002071 bool IsLoopIterVar = false) {
2072 if (DVar.RefExpr) {
2073 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
2074 << getOpenMPClauseName(DVar.CKind);
2075 return;
2076 }
2077 enum {
2078 PDSA_StaticMemberShared,
2079 PDSA_StaticLocalVarShared,
2080 PDSA_LoopIterVarPrivate,
2081 PDSA_LoopIterVarLinear,
2082 PDSA_LoopIterVarLastprivate,
2083 PDSA_ConstVarShared,
2084 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002085 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00002086 PDSA_LocalVarPrivate,
2087 PDSA_Implicit
2088 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00002089 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002090 auto ReportLoc = D->getLocation();
2091 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002092 if (IsLoopIterVar) {
2093 if (DVar.CKind == OMPC_private)
2094 Reason = PDSA_LoopIterVarPrivate;
2095 else if (DVar.CKind == OMPC_lastprivate)
2096 Reason = PDSA_LoopIterVarLastprivate;
2097 else
2098 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev35aaee62016-04-13 13:36:48 +00002099 } else if (isOpenMPTaskingDirective(DVar.DKind) &&
2100 DVar.CKind == OMPC_firstprivate) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002101 Reason = PDSA_TaskVarFirstprivate;
2102 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002103 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00002104 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002105 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00002106 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002107 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00002108 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002109 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00002110 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002111 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00002112 ReportHint = true;
2113 Reason = PDSA_LocalVarPrivate;
2114 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00002115 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002116 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00002117 << Reason << ReportHint
2118 << getOpenMPDirectiveName(Stack->getCurrentDirective());
2119 } else if (DVar.ImplicitDSALoc.isValid()) {
2120 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
2121 << getOpenMPClauseName(DVar.CKind);
2122 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00002123}
2124
Alexey Bataev758e55e2013-09-06 18:03:48 +00002125namespace {
Alexey Bataeve3727102018-04-18 15:57:46 +00002126class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> {
Alexey Bataev758e55e2013-09-06 18:03:48 +00002127 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00002128 Sema &SemaRef;
Alexey Bataeve3727102018-04-18 15:57:46 +00002129 bool ErrorFound = false;
2130 CapturedStmt *CS = nullptr;
2131 llvm::SmallVector<Expr *, 4> ImplicitFirstprivate;
2132 llvm::SmallVector<Expr *, 4> ImplicitMap;
2133 Sema::VarsWithInheritedDSAType VarsWithInheritedDSA;
2134 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations;
Alexey Bataeved09d242014-05-28 05:53:51 +00002135
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00002136 void VisitSubCaptures(OMPExecutableDirective *S) {
2137 // Check implicitly captured variables.
2138 if (!S->hasAssociatedStmt() || !S->getAssociatedStmt())
2139 return;
2140 for (const CapturedStmt::Capture &Cap :
2141 S->getInnermostCapturedStmt()->captures()) {
2142 if (!Cap.capturesVariable())
2143 continue;
2144 VarDecl *VD = Cap.getCapturedVar();
2145 // Do not try to map the variable if it or its sub-component was mapped
2146 // already.
2147 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) &&
2148 Stack->checkMappableExprComponentListsForDecl(
2149 VD, /*CurrentRegionOnly=*/true,
2150 [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
2151 OpenMPClauseKind) { return true; }))
2152 continue;
2153 DeclRefExpr *DRE = buildDeclRefExpr(
2154 SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context),
2155 Cap.getLocation(), /*RefersToCapture=*/true);
2156 Visit(DRE);
2157 }
2158 }
2159
Alexey Bataev758e55e2013-09-06 18:03:48 +00002160public:
2161 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00002162 if (E->isTypeDependent() || E->isValueDependent() ||
2163 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
2164 return;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002165 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00002166 VD = VD->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002167 // Skip internally declared variables.
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002168 if (VD->hasLocalStorage() && !CS->capturesVariable(VD))
Alexey Bataeved09d242014-05-28 05:53:51 +00002169 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002170
Alexey Bataeve3727102018-04-18 15:57:46 +00002171 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002172 // Check if the variable has explicit DSA set and stop analysis if it so.
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00002173 if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second)
David Majnemer9d168222016-08-05 17:44:54 +00002174 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002175
Alexey Bataevafe50572017-10-06 17:00:28 +00002176 // Skip internally declared static variables.
Alexey Bataev92327c52018-03-26 16:40:55 +00002177 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
Alexey Bataev97b72212018-08-14 18:31:20 +00002178 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
Alexey Bataev92327c52018-03-26 16:40:55 +00002179 if (VD->hasGlobalStorage() && !CS->capturesVariable(VD) &&
2180 (!Res || *Res != OMPDeclareTargetDeclAttr::MT_Link))
Alexey Bataevafe50572017-10-06 17:00:28 +00002181 return;
2182
Alexey Bataeve3727102018-04-18 15:57:46 +00002183 SourceLocation ELoc = E->getExprLoc();
2184 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002185 // The default(none) clause requires that each variable that is referenced
2186 // in the construct, and does not have a predetermined data-sharing
2187 // attribute, must have its data-sharing attribute explicitly determined
2188 // by being listed in a data-sharing attribute clause.
2189 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002190 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00002191 VarsWithInheritedDSA.count(VD) == 0) {
2192 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002193 return;
2194 }
2195
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002196 if (isOpenMPTargetExecutionDirective(DKind) &&
2197 !Stack->isLoopControlVariable(VD).first) {
2198 if (!Stack->checkMappableExprComponentListsForDecl(
2199 VD, /*CurrentRegionOnly=*/true,
2200 [](OMPClauseMappableExprCommon::MappableExprComponentListRef
2201 StackComponents,
2202 OpenMPClauseKind) {
2203 // Variable is used if it has been marked as an array, array
2204 // section or the variable iself.
2205 return StackComponents.size() == 1 ||
2206 std::all_of(
2207 std::next(StackComponents.rbegin()),
2208 StackComponents.rend(),
2209 [](const OMPClauseMappableExprCommon::
2210 MappableComponent &MC) {
2211 return MC.getAssociatedDeclaration() ==
2212 nullptr &&
2213 (isa<OMPArraySectionExpr>(
2214 MC.getAssociatedExpression()) ||
2215 isa<ArraySubscriptExpr>(
2216 MC.getAssociatedExpression()));
2217 });
2218 })) {
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002219 bool IsFirstprivate = false;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002220 // By default lambdas are captured as firstprivates.
2221 if (const auto *RD =
2222 VD->getType().getNonReferenceType()->getAsCXXRecordDecl())
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002223 IsFirstprivate = RD->isLambda();
2224 IsFirstprivate =
2225 IsFirstprivate ||
2226 (VD->getType().getNonReferenceType()->isScalarType() &&
Alexey Bataev92327c52018-03-26 16:40:55 +00002227 Stack->getDefaultDMA() != DMA_tofrom_scalar && !Res);
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002228 if (IsFirstprivate)
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002229 ImplicitFirstprivate.emplace_back(E);
2230 else
2231 ImplicitMap.emplace_back(E);
2232 return;
2233 }
2234 }
2235
Alexey Bataev758e55e2013-09-06 18:03:48 +00002236 // OpenMP [2.9.3.6, Restrictions, p.2]
2237 // A list item that appears in a reduction clause of the innermost
2238 // enclosing worksharing or parallel construct may not be accessed in an
2239 // explicit task.
Alexey Bataev7ace49d2016-05-17 08:55:33 +00002240 DVar = Stack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00002241 VD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
2242 [](OpenMPDirectiveKind K) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00002243 return isOpenMPParallelDirective(K) ||
2244 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2245 },
Alexey Bataeveffbdf12017-07-21 17:24:30 +00002246 /*FromParent=*/true);
Alexey Bataev35aaee62016-04-13 13:36:48 +00002247 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00002248 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00002249 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
Alexey Bataeve3727102018-04-18 15:57:46 +00002250 reportOriginalDsa(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002251 return;
2252 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002253
2254 // Define implicit data-sharing attributes for task.
Alexey Bataeve3727102018-04-18 15:57:46 +00002255 DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00002256 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
2257 !Stack->isLoopControlVariable(VD).first)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002258 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002259 }
2260 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002261 void VisitMemberExpr(MemberExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00002262 if (E->isTypeDependent() || E->isValueDependent() ||
2263 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
2264 return;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002265 auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002266 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002267 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002268 if (!FD)
2269 return;
Alexey Bataeve3727102018-04-18 15:57:46 +00002270 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002271 // Check if the variable has explicit DSA set and stop analysis if it
2272 // so.
2273 if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second)
2274 return;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002275
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002276 if (isOpenMPTargetExecutionDirective(DKind) &&
2277 !Stack->isLoopControlVariable(FD).first &&
2278 !Stack->checkMappableExprComponentListsForDecl(
2279 FD, /*CurrentRegionOnly=*/true,
2280 [](OMPClauseMappableExprCommon::MappableExprComponentListRef
2281 StackComponents,
2282 OpenMPClauseKind) {
2283 return isa<CXXThisExpr>(
2284 cast<MemberExpr>(
2285 StackComponents.back().getAssociatedExpression())
2286 ->getBase()
2287 ->IgnoreParens());
2288 })) {
2289 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
2290 // A bit-field cannot appear in a map clause.
2291 //
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002292 if (FD->isBitField())
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002293 return;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002294 ImplicitMap.emplace_back(E);
2295 return;
2296 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002297
Alexey Bataeve3727102018-04-18 15:57:46 +00002298 SourceLocation ELoc = E->getExprLoc();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002299 // OpenMP [2.9.3.6, Restrictions, p.2]
2300 // A list item that appears in a reduction clause of the innermost
2301 // enclosing worksharing or parallel construct may not be accessed in
2302 // an explicit task.
2303 DVar = Stack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00002304 FD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
2305 [](OpenMPDirectiveKind K) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002306 return isOpenMPParallelDirective(K) ||
2307 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2308 },
2309 /*FromParent=*/true);
2310 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
2311 ErrorFound = true;
2312 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
Alexey Bataeve3727102018-04-18 15:57:46 +00002313 reportOriginalDsa(SemaRef, Stack, FD, DVar);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002314 return;
2315 }
2316
2317 // Define implicit data-sharing attributes for task.
Alexey Bataeve3727102018-04-18 15:57:46 +00002318 DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002319 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
Alexey Bataevb40e05202018-10-24 18:53:12 +00002320 !Stack->isLoopControlVariable(FD).first) {
2321 // Check if there is a captured expression for the current field in the
2322 // region. Do not mark it as firstprivate unless there is no captured
2323 // expression.
2324 // TODO: try to make it firstprivate.
2325 if (DVar.CKind != OMPC_unknown)
2326 ImplicitFirstprivate.push_back(E);
2327 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002328 return;
2329 }
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002330 if (isOpenMPTargetExecutionDirective(DKind)) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002331 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
Alexey Bataeve3727102018-04-18 15:57:46 +00002332 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map,
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002333 /*NoDiagnose=*/true))
Alexey Bataev27041fa2017-12-05 15:22:49 +00002334 return;
Alexey Bataeve3727102018-04-18 15:57:46 +00002335 const auto *VD = cast<ValueDecl>(
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002336 CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl());
2337 if (!Stack->checkMappableExprComponentListsForDecl(
2338 VD, /*CurrentRegionOnly=*/true,
2339 [&CurComponents](
2340 OMPClauseMappableExprCommon::MappableExprComponentListRef
2341 StackComponents,
2342 OpenMPClauseKind) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002343 auto CCI = CurComponents.rbegin();
Alexey Bataev5ec38932017-09-26 16:19:04 +00002344 auto CCE = CurComponents.rend();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002345 for (const auto &SC : llvm::reverse(StackComponents)) {
2346 // Do both expressions have the same kind?
2347 if (CCI->getAssociatedExpression()->getStmtClass() !=
2348 SC.getAssociatedExpression()->getStmtClass())
2349 if (!(isa<OMPArraySectionExpr>(
2350 SC.getAssociatedExpression()) &&
2351 isa<ArraySubscriptExpr>(
2352 CCI->getAssociatedExpression())))
2353 return false;
2354
Alexey Bataeve3727102018-04-18 15:57:46 +00002355 const Decl *CCD = CCI->getAssociatedDeclaration();
2356 const Decl *SCD = SC.getAssociatedDeclaration();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002357 CCD = CCD ? CCD->getCanonicalDecl() : nullptr;
2358 SCD = SCD ? SCD->getCanonicalDecl() : nullptr;
2359 if (SCD != CCD)
2360 return false;
2361 std::advance(CCI, 1);
Alexey Bataev5ec38932017-09-26 16:19:04 +00002362 if (CCI == CCE)
2363 break;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002364 }
2365 return true;
2366 })) {
2367 Visit(E->getBase());
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002368 }
Alexey Bataeve3727102018-04-18 15:57:46 +00002369 } else {
Alexey Bataev7fcacd82016-11-28 15:55:15 +00002370 Visit(E->getBase());
Alexey Bataeve3727102018-04-18 15:57:46 +00002371 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002372 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002373 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002374 for (OMPClause *C : S->clauses()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002375 // Skip analysis of arguments of implicitly defined firstprivate clause
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002376 // for task|target directives.
2377 // Skip analysis of arguments of implicitly defined map clause for target
2378 // directives.
2379 if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) &&
2380 C->isImplicit())) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002381 for (Stmt *CC : C->children()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002382 if (CC)
2383 Visit(CC);
2384 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002385 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002386 }
Alexey Bataevf07946e2018-10-29 20:17:42 +00002387 // Check implicitly captured variables.
2388 VisitSubCaptures(S);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002389 }
2390 void VisitStmt(Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002391 for (Stmt *C : S->children()) {
Alexey Bataev8fc7b5f2018-10-25 15:35:27 +00002392 if (C) {
2393 if (auto *OED = dyn_cast<OMPExecutableDirective>(C)) {
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00002394 // Check implicitly captured variables in the task-based directives to
Alexey Bataev8fc7b5f2018-10-25 15:35:27 +00002395 // check if they must be firstprivatized.
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00002396 VisitSubCaptures(OED);
Alexey Bataev8fc7b5f2018-10-25 15:35:27 +00002397 } else {
2398 Visit(C);
2399 }
2400 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002401 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002402 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002403
Alexey Bataeve3727102018-04-18 15:57:46 +00002404 bool isErrorFound() const { return ErrorFound; }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002405 ArrayRef<Expr *> getImplicitFirstprivate() const {
2406 return ImplicitFirstprivate;
2407 }
2408 ArrayRef<Expr *> getImplicitMap() const { return ImplicitMap; }
Alexey Bataeve3727102018-04-18 15:57:46 +00002409 const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002410 return VarsWithInheritedDSA;
2411 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002412
Alexey Bataev7ff55242014-06-19 09:13:45 +00002413 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
2414 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00002415};
Alexey Bataeved09d242014-05-28 05:53:51 +00002416} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00002417
Alexey Bataevbae9a792014-06-27 10:37:06 +00002418void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00002419 switch (DKind) {
Kelvin Li70a12c52016-07-13 21:51:49 +00002420 case OMPD_parallel:
2421 case OMPD_parallel_for:
2422 case OMPD_parallel_for_simd:
2423 case OMPD_parallel_sections:
Carlo Bertolliba1487b2017-10-04 14:12:09 +00002424 case OMPD_teams:
Alexey Bataev999277a2017-12-06 14:31:09 +00002425 case OMPD_teams_distribute:
2426 case OMPD_teams_distribute_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002427 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Alexey Bataev2377fe92015-09-10 08:12:02 +00002428 QualType KmpInt32PtrTy =
2429 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002430 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002431 std::make_pair(".global_tid.", KmpInt32PtrTy),
2432 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2433 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00002434 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00002435 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2436 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00002437 break;
2438 }
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002439 case OMPD_target_teams:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00002440 case OMPD_target_parallel:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00002441 case OMPD_target_parallel_for:
Alexey Bataevdfa430f2017-12-08 15:03:50 +00002442 case OMPD_target_parallel_for_simd:
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00002443 case OMPD_target_teams_distribute:
2444 case OMPD_target_teams_distribute_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002445 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2446 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2447 QualType KmpInt32PtrTy =
2448 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2449 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00002450 FunctionProtoType::ExtProtoInfo EPI;
2451 EPI.Variadic = true;
2452 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2453 Sema::CapturedParamNameType Params[] = {
2454 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002455 std::make_pair(".part_id.", KmpInt32PtrTy),
2456 std::make_pair(".privates.", VoidPtrTy),
2457 std::make_pair(
2458 ".copy_fn.",
2459 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00002460 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2461 std::make_pair(StringRef(), QualType()) // __context with shared vars
2462 };
2463 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2464 Params);
Alexey Bataev0c869ef2018-01-16 15:57:07 +00002465 // Mark this captured region as inlined, because we don't use outlined
2466 // function directly.
2467 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2468 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002469 Context, AlwaysInlineAttr::Keyword_forceinline));
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002470 Sema::CapturedParamNameType ParamsTarget[] = {
2471 std::make_pair(StringRef(), QualType()) // __context with shared vars
2472 };
2473 // Start a captured region for 'target' with no implicit parameters.
2474 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2475 ParamsTarget);
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002476 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002477 std::make_pair(".global_tid.", KmpInt32PtrTy),
2478 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2479 std::make_pair(StringRef(), QualType()) // __context with shared vars
2480 };
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002481 // Start a captured region for 'teams' or 'parallel'. Both regions have
2482 // the same implicit parameters.
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002483 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002484 ParamsTeamsOrParallel);
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002485 break;
2486 }
Alexey Bataev8451efa2018-01-15 19:06:12 +00002487 case OMPD_target:
2488 case OMPD_target_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002489 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2490 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2491 QualType KmpInt32PtrTy =
2492 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2493 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00002494 FunctionProtoType::ExtProtoInfo EPI;
2495 EPI.Variadic = true;
2496 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2497 Sema::CapturedParamNameType Params[] = {
2498 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002499 std::make_pair(".part_id.", KmpInt32PtrTy),
2500 std::make_pair(".privates.", VoidPtrTy),
2501 std::make_pair(
2502 ".copy_fn.",
2503 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00002504 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2505 std::make_pair(StringRef(), QualType()) // __context with shared vars
2506 };
2507 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2508 Params);
2509 // Mark this captured region as inlined, because we don't use outlined
2510 // function directly.
2511 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2512 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002513 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev8451efa2018-01-15 19:06:12 +00002514 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2515 std::make_pair(StringRef(), QualType()));
2516 break;
2517 }
Kelvin Li70a12c52016-07-13 21:51:49 +00002518 case OMPD_simd:
2519 case OMPD_for:
2520 case OMPD_for_simd:
2521 case OMPD_sections:
2522 case OMPD_section:
2523 case OMPD_single:
2524 case OMPD_master:
2525 case OMPD_critical:
Kelvin Lia579b912016-07-14 02:54:56 +00002526 case OMPD_taskgroup:
2527 case OMPD_distribute:
Alexey Bataev46506272017-12-05 17:41:34 +00002528 case OMPD_distribute_simd:
Kelvin Li70a12c52016-07-13 21:51:49 +00002529 case OMPD_ordered:
2530 case OMPD_atomic:
Alexey Bataev8451efa2018-01-15 19:06:12 +00002531 case OMPD_target_data: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002532 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002533 std::make_pair(StringRef(), QualType()) // __context with shared vars
2534 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00002535 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2536 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002537 break;
2538 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002539 case OMPD_task: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002540 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2541 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2542 QualType KmpInt32PtrTy =
2543 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2544 QualType Args[] = {VoidPtrTy};
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002545 FunctionProtoType::ExtProtoInfo EPI;
2546 EPI.Variadic = true;
2547 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002548 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00002549 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002550 std::make_pair(".part_id.", KmpInt32PtrTy),
2551 std::make_pair(".privates.", VoidPtrTy),
2552 std::make_pair(
2553 ".copy_fn.",
2554 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev48591dd2016-04-20 04:01:36 +00002555 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002556 std::make_pair(StringRef(), QualType()) // __context with shared vars
2557 };
2558 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2559 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00002560 // Mark this captured region as inlined, because we don't use outlined
2561 // function directly.
2562 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2563 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002564 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002565 break;
2566 }
Alexey Bataev1e73ef32016-04-28 12:14:51 +00002567 case OMPD_taskloop:
2568 case OMPD_taskloop_simd: {
Alexey Bataev7292c292016-04-25 12:22:29 +00002569 QualType KmpInt32Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002570 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1)
2571 .withConst();
Alexey Bataev7292c292016-04-25 12:22:29 +00002572 QualType KmpUInt64Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002573 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0)
2574 .withConst();
Alexey Bataev7292c292016-04-25 12:22:29 +00002575 QualType KmpInt64Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002576 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1)
2577 .withConst();
2578 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2579 QualType KmpInt32PtrTy =
2580 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2581 QualType Args[] = {VoidPtrTy};
Alexey Bataev7292c292016-04-25 12:22:29 +00002582 FunctionProtoType::ExtProtoInfo EPI;
2583 EPI.Variadic = true;
2584 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev49f6e782015-12-01 04:18:41 +00002585 Sema::CapturedParamNameType Params[] = {
Alexey Bataev7292c292016-04-25 12:22:29 +00002586 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002587 std::make_pair(".part_id.", KmpInt32PtrTy),
2588 std::make_pair(".privates.", VoidPtrTy),
Alexey Bataev7292c292016-04-25 12:22:29 +00002589 std::make_pair(
2590 ".copy_fn.",
2591 Context.getPointerType(CopyFnType).withConst().withRestrict()),
2592 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2593 std::make_pair(".lb.", KmpUInt64Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002594 std::make_pair(".ub.", KmpUInt64Ty),
2595 std::make_pair(".st.", KmpInt64Ty),
Alexey Bataev7292c292016-04-25 12:22:29 +00002596 std::make_pair(".liter.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002597 std::make_pair(".reductions.", VoidPtrTy),
Alexey Bataev49f6e782015-12-01 04:18:41 +00002598 std::make_pair(StringRef(), QualType()) // __context with shared vars
2599 };
2600 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2601 Params);
Alexey Bataev7292c292016-04-25 12:22:29 +00002602 // Mark this captured region as inlined, because we don't use outlined
2603 // function directly.
2604 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2605 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002606 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev49f6e782015-12-01 04:18:41 +00002607 break;
2608 }
Kelvin Li4a39add2016-07-05 05:00:15 +00002609 case OMPD_distribute_parallel_for_simd:
Alexey Bataev647dd842018-01-15 20:59:40 +00002610 case OMPD_distribute_parallel_for: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002611 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli9925f152016-06-27 14:55:37 +00002612 QualType KmpInt32PtrTy =
2613 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2614 Sema::CapturedParamNameType Params[] = {
2615 std::make_pair(".global_tid.", KmpInt32PtrTy),
2616 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002617 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2618 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli9925f152016-06-27 14:55:37 +00002619 std::make_pair(StringRef(), QualType()) // __context with shared vars
2620 };
2621 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2622 Params);
2623 break;
2624 }
Alexey Bataev647dd842018-01-15 20:59:40 +00002625 case OMPD_target_teams_distribute_parallel_for:
2626 case OMPD_target_teams_distribute_parallel_for_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002627 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli52978c32018-01-03 21:12:44 +00002628 QualType KmpInt32PtrTy =
2629 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002630 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
Carlo Bertolli52978c32018-01-03 21:12:44 +00002631
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002632 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00002633 FunctionProtoType::ExtProtoInfo EPI;
2634 EPI.Variadic = true;
2635 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2636 Sema::CapturedParamNameType Params[] = {
2637 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002638 std::make_pair(".part_id.", KmpInt32PtrTy),
2639 std::make_pair(".privates.", VoidPtrTy),
2640 std::make_pair(
2641 ".copy_fn.",
2642 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00002643 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2644 std::make_pair(StringRef(), QualType()) // __context with shared vars
2645 };
2646 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2647 Params);
Alexey Bataev9f9fb0b2018-01-16 19:02:33 +00002648 // Mark this captured region as inlined, because we don't use outlined
2649 // function directly.
2650 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2651 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002652 Context, AlwaysInlineAttr::Keyword_forceinline));
Carlo Bertolli52978c32018-01-03 21:12:44 +00002653 Sema::CapturedParamNameType ParamsTarget[] = {
2654 std::make_pair(StringRef(), QualType()) // __context with shared vars
2655 };
2656 // Start a captured region for 'target' with no implicit parameters.
2657 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2658 ParamsTarget);
2659
2660 Sema::CapturedParamNameType ParamsTeams[] = {
2661 std::make_pair(".global_tid.", KmpInt32PtrTy),
2662 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2663 std::make_pair(StringRef(), QualType()) // __context with shared vars
2664 };
2665 // Start a captured region for 'target' with no implicit parameters.
2666 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2667 ParamsTeams);
2668
2669 Sema::CapturedParamNameType ParamsParallel[] = {
2670 std::make_pair(".global_tid.", KmpInt32PtrTy),
2671 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002672 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2673 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli52978c32018-01-03 21:12:44 +00002674 std::make_pair(StringRef(), QualType()) // __context with shared vars
2675 };
2676 // Start a captured region for 'teams' or 'parallel'. Both regions have
2677 // the same implicit parameters.
2678 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2679 ParamsParallel);
2680 break;
2681 }
2682
Alexey Bataev46506272017-12-05 17:41:34 +00002683 case OMPD_teams_distribute_parallel_for:
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00002684 case OMPD_teams_distribute_parallel_for_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002685 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli62fae152017-11-20 20:46:39 +00002686 QualType KmpInt32PtrTy =
2687 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2688
2689 Sema::CapturedParamNameType ParamsTeams[] = {
2690 std::make_pair(".global_tid.", KmpInt32PtrTy),
2691 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2692 std::make_pair(StringRef(), QualType()) // __context with shared vars
2693 };
2694 // Start a captured region for 'target' with no implicit parameters.
2695 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2696 ParamsTeams);
2697
2698 Sema::CapturedParamNameType ParamsParallel[] = {
2699 std::make_pair(".global_tid.", KmpInt32PtrTy),
2700 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002701 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2702 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli62fae152017-11-20 20:46:39 +00002703 std::make_pair(StringRef(), QualType()) // __context with shared vars
2704 };
2705 // Start a captured region for 'teams' or 'parallel'. Both regions have
2706 // the same implicit parameters.
2707 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2708 ParamsParallel);
2709 break;
2710 }
Alexey Bataev7828b252017-11-21 17:08:48 +00002711 case OMPD_target_update:
2712 case OMPD_target_enter_data:
2713 case OMPD_target_exit_data: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002714 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2715 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2716 QualType KmpInt32PtrTy =
2717 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2718 QualType Args[] = {VoidPtrTy};
Alexey Bataev7828b252017-11-21 17:08:48 +00002719 FunctionProtoType::ExtProtoInfo EPI;
2720 EPI.Variadic = true;
2721 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2722 Sema::CapturedParamNameType Params[] = {
2723 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002724 std::make_pair(".part_id.", KmpInt32PtrTy),
2725 std::make_pair(".privates.", VoidPtrTy),
2726 std::make_pair(
2727 ".copy_fn.",
2728 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev7828b252017-11-21 17:08:48 +00002729 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2730 std::make_pair(StringRef(), QualType()) // __context with shared vars
2731 };
2732 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2733 Params);
2734 // Mark this captured region as inlined, because we don't use outlined
2735 // function directly.
2736 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2737 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002738 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev7828b252017-11-21 17:08:48 +00002739 break;
2740 }
Alexey Bataev9959db52014-05-06 10:08:46 +00002741 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00002742 case OMPD_taskyield:
2743 case OMPD_barrier:
2744 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002745 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00002746 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00002747 case OMPD_flush:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002748 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00002749 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00002750 case OMPD_declare_target:
2751 case OMPD_end_declare_target:
Kelvin Li1408f912018-09-26 04:28:39 +00002752 case OMPD_requires:
Alexey Bataev9959db52014-05-06 10:08:46 +00002753 llvm_unreachable("OpenMP Directive is not allowed");
2754 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00002755 llvm_unreachable("Unknown OpenMP directive");
2756 }
2757}
2758
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002759int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) {
2760 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2761 getOpenMPCaptureRegions(CaptureRegions, DKind);
2762 return CaptureRegions.size();
2763}
2764
Alexey Bataev3392d762016-02-16 11:18:12 +00002765static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00002766 Expr *CaptureExpr, bool WithInit,
2767 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00002768 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00002769 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00002770 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00002771 QualType Ty = Init->getType();
2772 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002773 if (S.getLangOpts().CPlusPlus) {
Alexey Bataev4244be22016-02-11 05:35:55 +00002774 Ty = C.getLValueReferenceType(Ty);
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002775 } else {
Alexey Bataev4244be22016-02-11 05:35:55 +00002776 Ty = C.getPointerType(Ty);
2777 ExprResult Res =
2778 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
2779 if (!Res.isUsable())
2780 return nullptr;
2781 Init = Res.get();
2782 }
Alexey Bataev61205072016-03-02 04:57:40 +00002783 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00002784 }
Alexey Bataeva7206b92016-12-20 16:51:02 +00002785 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty,
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002786 CaptureExpr->getBeginLoc());
Alexey Bataev2bbf7212016-03-03 03:52:24 +00002787 if (!WithInit)
Alexey Bataeve3727102018-04-18 15:57:46 +00002788 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C));
Alexey Bataev4244be22016-02-11 05:35:55 +00002789 S.CurContext->addHiddenDecl(CED);
Richard Smith3beb7c62017-01-12 02:27:38 +00002790 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00002791 return CED;
2792}
2793
Alexey Bataev61205072016-03-02 04:57:40 +00002794static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
2795 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00002796 OMPCapturedExprDecl *CD;
Alexey Bataeve3727102018-04-18 15:57:46 +00002797 if (VarDecl *VD = S.isOpenMPCapturedDecl(D))
Alexey Bataevb7a34b62016-02-25 03:59:29 +00002798 CD = cast<OMPCapturedExprDecl>(VD);
Alexey Bataeve3727102018-04-18 15:57:46 +00002799 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00002800 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
2801 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00002802 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00002803 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00002804}
2805
Alexey Bataev5a3af132016-03-29 08:58:54 +00002806static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002807 CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00002808 if (!Ref) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002809 OMPCapturedExprDecl *CD = buildCaptureDecl(
2810 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr,
2811 /*WithInit=*/true, /*AsExpression=*/true);
Alexey Bataev5a3af132016-03-29 08:58:54 +00002812 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
2813 CaptureExpr->getExprLoc());
2814 }
2815 ExprResult Res = Ref;
2816 if (!S.getLangOpts().CPlusPlus &&
2817 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002818 Ref->getType()->isPointerType()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00002819 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002820 if (!Res.isUsable())
2821 return ExprError();
2822 }
2823 return S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00002824}
2825
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002826namespace {
2827// OpenMP directives parsed in this section are represented as a
2828// CapturedStatement with an associated statement. If a syntax error
2829// is detected during the parsing of the associated statement, the
2830// compiler must abort processing and close the CapturedStatement.
2831//
2832// Combined directives such as 'target parallel' have more than one
2833// nested CapturedStatements. This RAII ensures that we unwind out
2834// of all the nested CapturedStatements when an error is found.
2835class CaptureRegionUnwinderRAII {
2836private:
2837 Sema &S;
2838 bool &ErrorFound;
Alexey Bataeve3727102018-04-18 15:57:46 +00002839 OpenMPDirectiveKind DKind = OMPD_unknown;
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002840
2841public:
2842 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
2843 OpenMPDirectiveKind DKind)
2844 : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
2845 ~CaptureRegionUnwinderRAII() {
2846 if (ErrorFound) {
2847 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind);
2848 while (--ThisCaptureLevel >= 0)
2849 S.ActOnCapturedRegionError();
2850 }
2851 }
2852};
2853} // namespace
2854
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002855StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
2856 ArrayRef<OMPClause *> Clauses) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002857 bool ErrorFound = false;
2858 CaptureRegionUnwinderRAII CaptureRegionUnwinder(
2859 *this, ErrorFound, DSAStack->getCurrentDirective());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002860 if (!S.isUsable()) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002861 ErrorFound = true;
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002862 return StmtError();
2863 }
Alexey Bataev993d2802015-12-28 06:23:08 +00002864
Alexey Bataev2ba67042017-11-28 21:11:44 +00002865 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2866 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
Alexey Bataev993d2802015-12-28 06:23:08 +00002867 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00002868 OMPScheduleClause *SC = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +00002869 SmallVector<const OMPLinearClause *, 4> LCs;
2870 SmallVector<const OMPClauseWithPreInit *, 4> PICs;
Alexey Bataev040d5402015-05-12 08:35:28 +00002871 // This is required for proper codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00002872 for (OMPClause *Clause : Clauses) {
Alexey Bataev88202be2017-07-27 13:20:36 +00002873 if (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) &&
2874 Clause->getClauseKind() == OMPC_in_reduction) {
2875 // Capture taskgroup task_reduction descriptors inside the tasking regions
2876 // with the corresponding in_reduction items.
2877 auto *IRC = cast<OMPInReductionClause>(Clause);
Alexey Bataeve3727102018-04-18 15:57:46 +00002878 for (Expr *E : IRC->taskgroup_descriptors())
Alexey Bataev88202be2017-07-27 13:20:36 +00002879 if (E)
2880 MarkDeclarationsReferencedInExpr(E);
2881 }
Alexey Bataev16dc7b62015-05-20 03:46:04 +00002882 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00002883 Clause->getClauseKind() == OMPC_copyprivate ||
2884 (getLangOpts().OpenMPUseTLS &&
2885 getASTContext().getTargetInfo().isTLSSupported() &&
2886 Clause->getClauseKind() == OMPC_copyin)) {
2887 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00002888 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeve3727102018-04-18 15:57:46 +00002889 for (Stmt *VarRef : Clause->children()) {
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002890 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00002891 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002892 }
2893 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00002894 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev2ba67042017-11-28 21:11:44 +00002895 } else if (CaptureRegions.size() > 1 ||
2896 CaptureRegions.back() != OMPD_unknown) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002897 if (auto *C = OMPClauseWithPreInit::get(Clause))
2898 PICs.push_back(C);
Alexey Bataev005248a2016-02-25 05:25:57 +00002899 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002900 if (Expr *E = C->getPostUpdateExpr())
Alexey Bataev005248a2016-02-25 05:25:57 +00002901 MarkDeclarationsReferencedInExpr(E);
2902 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002903 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002904 if (Clause->getClauseKind() == OMPC_schedule)
2905 SC = cast<OMPScheduleClause>(Clause);
2906 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00002907 OC = cast<OMPOrderedClause>(Clause);
2908 else if (Clause->getClauseKind() == OMPC_linear)
2909 LCs.push_back(cast<OMPLinearClause>(Clause));
2910 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002911 // OpenMP, 2.7.1 Loop Construct, Restrictions
2912 // The nonmonotonic modifier cannot be specified if an ordered clause is
2913 // specified.
2914 if (SC &&
2915 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
2916 SC->getSecondScheduleModifier() ==
2917 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
2918 OC) {
2919 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
2920 ? SC->getFirstScheduleModifierLoc()
2921 : SC->getSecondScheduleModifierLoc(),
2922 diag::err_omp_schedule_nonmonotonic_ordered)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00002923 << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
Alexey Bataev6402bca2015-12-28 07:25:51 +00002924 ErrorFound = true;
2925 }
Alexey Bataev993d2802015-12-28 06:23:08 +00002926 if (!LCs.empty() && OC && OC->getNumForLoops()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002927 for (const OMPLinearClause *C : LCs) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002928 Diag(C->getBeginLoc(), diag::err_omp_linear_ordered)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00002929 << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
Alexey Bataev993d2802015-12-28 06:23:08 +00002930 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002931 ErrorFound = true;
2932 }
Alexey Bataev113438c2015-12-30 12:06:23 +00002933 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
2934 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
2935 OC->getNumForLoops()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002936 Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd)
Alexey Bataev113438c2015-12-30 12:06:23 +00002937 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
2938 ErrorFound = true;
2939 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002940 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00002941 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002942 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002943 StmtResult SR = S;
Alexey Bataev2ba67042017-11-28 21:11:44 +00002944 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002945 // Mark all variables in private list clauses as used in inner region.
2946 // Required for proper codegen of combined directives.
2947 // TODO: add processing for other clauses.
Alexey Bataev2ba67042017-11-28 21:11:44 +00002948 if (ThisCaptureRegion != OMPD_unknown) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002949 for (const clang::OMPClauseWithPreInit *C : PICs) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002950 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
2951 // Find the particular capture region for the clause if the
2952 // directive is a combined one with multiple capture regions.
2953 // If the directive is not a combined one, the capture region
2954 // associated with the clause is OMPD_unknown and is generated
2955 // only once.
2956 if (CaptureRegion == ThisCaptureRegion ||
2957 CaptureRegion == OMPD_unknown) {
2958 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002959 for (Decl *D : DS->decls())
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002960 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
2961 }
2962 }
2963 }
2964 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002965 SR = ActOnCapturedRegionEnd(SR.get());
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002966 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002967 return SR;
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002968}
2969
Jonas Hahnfeld64a9e3c2017-02-22 06:49:10 +00002970static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
2971 OpenMPDirectiveKind CancelRegion,
2972 SourceLocation StartLoc) {
2973 // CancelRegion is only needed for cancel and cancellation_point.
2974 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point)
2975 return false;
2976
2977 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for ||
2978 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup)
2979 return false;
2980
2981 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region)
2982 << getOpenMPDirectiveName(CancelRegion);
2983 return true;
2984}
2985
Alexey Bataeve3727102018-04-18 15:57:46 +00002986static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002987 OpenMPDirectiveKind CurrentRegion,
2988 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002989 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002990 SourceLocation StartLoc) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002991 if (Stack->getCurScope()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002992 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective();
2993 OpenMPDirectiveKind OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002994 bool NestingProhibited = false;
2995 bool CloseNesting = true;
David Majnemer9d168222016-08-05 17:44:54 +00002996 bool OrphanSeen = false;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002997 enum {
2998 NoRecommend,
2999 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00003000 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003001 ShouldBeInTargetRegion,
3002 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003003 } Recommend = NoRecommend;
Kelvin Lifd8b5742016-07-01 14:30:25 +00003004 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003005 // OpenMP [2.16, Nesting of Regions]
3006 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003007 // OpenMP [2.8.1,simd Construct, Restrictions]
Kelvin Lifd8b5742016-07-01 14:30:25 +00003008 // An ordered construct with the simd clause is the only OpenMP
3009 // construct that can appear in the simd region.
David Majnemer9d168222016-08-05 17:44:54 +00003010 // Allowing a SIMD construct nested in another SIMD construct is an
Kelvin Lifd8b5742016-07-01 14:30:25 +00003011 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
3012 // message.
3013 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
3014 ? diag::err_omp_prohibited_region_simd
3015 : diag::warn_omp_nesting_simd);
3016 return CurrentRegion != OMPD_simd;
Alexey Bataev549210e2014-06-24 04:39:47 +00003017 }
Alexey Bataev0162e452014-07-22 10:10:35 +00003018 if (ParentRegion == OMPD_atomic) {
3019 // OpenMP [2.16, Nesting of Regions]
3020 // OpenMP constructs may not be nested inside an atomic region.
3021 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
3022 return true;
3023 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003024 if (CurrentRegion == OMPD_section) {
3025 // OpenMP [2.7.2, sections Construct, Restrictions]
3026 // Orphaned section directives are prohibited. That is, the section
3027 // directives must appear within the sections construct and must not be
3028 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003029 if (ParentRegion != OMPD_sections &&
3030 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003031 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
3032 << (ParentRegion != OMPD_unknown)
3033 << getOpenMPDirectiveName(ParentRegion);
3034 return true;
3035 }
3036 return false;
3037 }
Kelvin Li2b51f722016-07-26 04:32:50 +00003038 // Allow some constructs (except teams) to be orphaned (they could be
David Majnemer9d168222016-08-05 17:44:54 +00003039 // used in functions, called from OpenMP regions with the required
Kelvin Li2b51f722016-07-26 04:32:50 +00003040 // preconditions).
Kelvin Libf594a52016-12-17 05:48:59 +00003041 if (ParentRegion == OMPD_unknown &&
3042 !isOpenMPNestingTeamsDirective(CurrentRegion))
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003043 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00003044 if (CurrentRegion == OMPD_cancellation_point ||
3045 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003046 // OpenMP [2.16, Nesting of Regions]
3047 // A cancellation point construct for which construct-type-clause is
3048 // taskgroup must be nested inside a task construct. A cancellation
3049 // point construct for which construct-type-clause is not taskgroup must
3050 // be closely nested inside an OpenMP construct that matches the type
3051 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00003052 // A cancel construct for which construct-type-clause is taskgroup must be
3053 // nested inside a task construct. A cancel construct for which
3054 // construct-type-clause is not taskgroup must be closely nested inside an
3055 // OpenMP construct that matches the type specified in
3056 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003057 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003058 !((CancelRegion == OMPD_parallel &&
3059 (ParentRegion == OMPD_parallel ||
3060 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00003061 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003062 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00003063 ParentRegion == OMPD_target_parallel_for ||
3064 ParentRegion == OMPD_distribute_parallel_for ||
Alexey Bataev16e79882017-11-22 21:12:03 +00003065 ParentRegion == OMPD_teams_distribute_parallel_for ||
3066 ParentRegion == OMPD_target_teams_distribute_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003067 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
3068 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00003069 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
3070 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003071 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00003072 // OpenMP [2.16, Nesting of Regions]
3073 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003074 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00003075 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003076 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003077 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
3078 // OpenMP [2.16, Nesting of Regions]
3079 // A critical region may not be nested (closely or otherwise) inside a
3080 // critical region with the same name. Note that this restriction is not
3081 // sufficient to prevent deadlock.
3082 SourceLocation PreviousCriticalLoc;
David Majnemer9d168222016-08-05 17:44:54 +00003083 bool DeadLock = Stack->hasDirective(
3084 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
3085 const DeclarationNameInfo &DNI,
Alexey Bataeve3727102018-04-18 15:57:46 +00003086 SourceLocation Loc) {
David Majnemer9d168222016-08-05 17:44:54 +00003087 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
3088 PreviousCriticalLoc = Loc;
3089 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003090 }
3091 return false;
David Majnemer9d168222016-08-05 17:44:54 +00003092 },
3093 false /* skip top directive */);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003094 if (DeadLock) {
3095 SemaRef.Diag(StartLoc,
3096 diag::err_omp_prohibited_region_critical_same_name)
3097 << CurrentName.getName();
3098 if (PreviousCriticalLoc.isValid())
3099 SemaRef.Diag(PreviousCriticalLoc,
3100 diag::note_omp_previous_critical_region);
3101 return true;
3102 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003103 } else if (CurrentRegion == OMPD_barrier) {
3104 // OpenMP [2.16, Nesting of Regions]
3105 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003106 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003107 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3108 isOpenMPTaskingDirective(ParentRegion) ||
3109 ParentRegion == OMPD_master ||
3110 ParentRegion == OMPD_critical ||
3111 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00003112 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Kelvin Li579e41c2016-11-30 23:51:03 +00003113 !isOpenMPParallelDirective(CurrentRegion) &&
3114 !isOpenMPTeamsDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003115 // OpenMP [2.16, Nesting of Regions]
3116 // A worksharing region may not be closely nested inside a worksharing,
3117 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003118 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3119 isOpenMPTaskingDirective(ParentRegion) ||
3120 ParentRegion == OMPD_master ||
3121 ParentRegion == OMPD_critical ||
3122 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003123 Recommend = ShouldBeInParallelRegion;
3124 } else if (CurrentRegion == OMPD_ordered) {
3125 // OpenMP [2.16, Nesting of Regions]
3126 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00003127 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003128 // An ordered region must be closely nested inside a loop region (or
3129 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003130 // OpenMP [2.8.1,simd Construct, Restrictions]
3131 // An ordered construct with the simd clause is the only OpenMP construct
3132 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003133 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003134 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003135 !(isOpenMPSimdDirective(ParentRegion) ||
3136 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003137 Recommend = ShouldBeInOrderedRegion;
Kelvin Libf594a52016-12-17 05:48:59 +00003138 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00003139 // OpenMP [2.16, Nesting of Regions]
3140 // If specified, a teams construct must be contained within a target
3141 // construct.
3142 NestingProhibited = ParentRegion != OMPD_target;
Kelvin Li2b51f722016-07-26 04:32:50 +00003143 OrphanSeen = ParentRegion == OMPD_unknown;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003144 Recommend = ShouldBeInTargetRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003145 }
Kelvin Libf594a52016-12-17 05:48:59 +00003146 if (!NestingProhibited &&
3147 !isOpenMPTargetExecutionDirective(CurrentRegion) &&
3148 !isOpenMPTargetDataManagementDirective(CurrentRegion) &&
3149 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00003150 // OpenMP [2.16, Nesting of Regions]
3151 // distribute, parallel, parallel sections, parallel workshare, and the
3152 // parallel loop and parallel loop SIMD constructs are the only OpenMP
3153 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003154 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
3155 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00003156 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003157 }
David Majnemer9d168222016-08-05 17:44:54 +00003158 if (!NestingProhibited &&
Kelvin Li02532872016-08-05 14:37:37 +00003159 isOpenMPNestingDistributeDirective(CurrentRegion)) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003160 // OpenMP 4.5 [2.17 Nesting of Regions]
3161 // The region associated with the distribute construct must be strictly
3162 // nested inside a teams region
Kelvin Libf594a52016-12-17 05:48:59 +00003163 NestingProhibited =
3164 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003165 Recommend = ShouldBeInTeamsRegion;
3166 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003167 if (!NestingProhibited &&
3168 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
3169 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
3170 // OpenMP 4.5 [2.17 Nesting of Regions]
3171 // If a target, target update, target data, target enter data, or
3172 // target exit data construct is encountered during execution of a
3173 // target region, the behavior is unspecified.
3174 NestingProhibited = Stack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +00003175 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
Alexey Bataeve3727102018-04-18 15:57:46 +00003176 SourceLocation) {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003177 if (isOpenMPTargetExecutionDirective(K)) {
3178 OffendingRegion = K;
3179 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003180 }
3181 return false;
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003182 },
3183 false /* don't skip top directive */);
3184 CloseNesting = false;
3185 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003186 if (NestingProhibited) {
Kelvin Li2b51f722016-07-26 04:32:50 +00003187 if (OrphanSeen) {
3188 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
3189 << getOpenMPDirectiveName(CurrentRegion) << Recommend;
3190 } else {
3191 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
3192 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
3193 << Recommend << getOpenMPDirectiveName(CurrentRegion);
3194 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003195 return true;
3196 }
3197 }
3198 return false;
3199}
3200
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003201static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
3202 ArrayRef<OMPClause *> Clauses,
3203 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
3204 bool ErrorFound = false;
3205 unsigned NamedModifiersNumber = 0;
3206 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
3207 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00003208 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00003209 for (const OMPClause *C : Clauses) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003210 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
3211 // At most one if clause without a directive-name-modifier can appear on
3212 // the directive.
3213 OpenMPDirectiveKind CurNM = IC->getNameModifier();
3214 if (FoundNameModifiers[CurNM]) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003215 S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003216 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
3217 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
3218 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003219 } else if (CurNM != OMPD_unknown) {
3220 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003221 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003222 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003223 FoundNameModifiers[CurNM] = IC;
3224 if (CurNM == OMPD_unknown)
3225 continue;
3226 // Check if the specified name modifier is allowed for the current
3227 // directive.
3228 // At most one if clause with the particular directive-name-modifier can
3229 // appear on the directive.
3230 bool MatchFound = false;
3231 for (auto NM : AllowedNameModifiers) {
3232 if (CurNM == NM) {
3233 MatchFound = true;
3234 break;
3235 }
3236 }
3237 if (!MatchFound) {
3238 S.Diag(IC->getNameModifierLoc(),
3239 diag::err_omp_wrong_if_directive_name_modifier)
3240 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
3241 ErrorFound = true;
3242 }
3243 }
3244 }
3245 // If any if clause on the directive includes a directive-name-modifier then
3246 // all if clauses on the directive must include a directive-name-modifier.
3247 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3248 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003249 S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003250 diag::err_omp_no_more_if_clause);
3251 } else {
3252 std::string Values;
3253 std::string Sep(", ");
3254 unsigned AllowedCnt = 0;
3255 unsigned TotalAllowedNum =
3256 AllowedNameModifiers.size() - NamedModifiersNumber;
3257 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3258 ++Cnt) {
3259 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3260 if (!FoundNameModifiers[NM]) {
3261 Values += "'";
3262 Values += getOpenMPDirectiveName(NM);
3263 Values += "'";
3264 if (AllowedCnt + 2 == TotalAllowedNum)
3265 Values += " or ";
3266 else if (AllowedCnt + 1 != TotalAllowedNum)
3267 Values += Sep;
3268 ++AllowedCnt;
3269 }
3270 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003271 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003272 diag::err_omp_unnamed_if_clause)
3273 << (TotalAllowedNum > 1) << Values;
3274 }
Alexey Bataeve3727102018-04-18 15:57:46 +00003275 for (SourceLocation Loc : NameModifierLoc) {
Alexey Bataevecb156a2015-09-15 17:23:56 +00003276 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3277 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003278 ErrorFound = true;
3279 }
3280 return ErrorFound;
3281}
3282
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003283StmtResult Sema::ActOnOpenMPExecutableDirective(
3284 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3285 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3286 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003287 StmtResult Res = StmtError();
Jonas Hahnfeld64a9e3c2017-02-22 06:49:10 +00003288 // First check CancelRegion which is then used in checkNestingOfRegions.
3289 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) ||
3290 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003291 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00003292 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003293
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003294 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataeve3727102018-04-18 15:57:46 +00003295 VarsWithInheritedDSAType VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003296 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00003297 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00003298 if (AStmt && !CurContext->isDependentContext()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00003299 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3300
3301 // Check default data sharing attributes for referenced variables.
3302 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
Arpith Chacko Jacob1f46b702017-01-23 15:38:49 +00003303 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind);
3304 Stmt *S = AStmt;
3305 while (--ThisCaptureLevel >= 0)
3306 S = cast<CapturedStmt>(S)->getCapturedStmt();
3307 DSAChecker.Visit(S);
Alexey Bataev68446b72014-07-18 07:47:19 +00003308 if (DSAChecker.isErrorFound())
3309 return StmtError();
3310 // Generate list of implicitly defined firstprivate variables.
3311 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00003312
Alexey Bataev88202be2017-07-27 13:20:36 +00003313 SmallVector<Expr *, 4> ImplicitFirstprivates(
3314 DSAChecker.getImplicitFirstprivate().begin(),
3315 DSAChecker.getImplicitFirstprivate().end());
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003316 SmallVector<Expr *, 4> ImplicitMaps(DSAChecker.getImplicitMap().begin(),
3317 DSAChecker.getImplicitMap().end());
Alexey Bataev88202be2017-07-27 13:20:36 +00003318 // Mark taskgroup task_reduction descriptors as implicitly firstprivate.
Alexey Bataeve3727102018-04-18 15:57:46 +00003319 for (OMPClause *C : Clauses) {
Alexey Bataev88202be2017-07-27 13:20:36 +00003320 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003321 for (Expr *E : IRC->taskgroup_descriptors())
Alexey Bataev88202be2017-07-27 13:20:36 +00003322 if (E)
3323 ImplicitFirstprivates.emplace_back(E);
3324 }
3325 }
3326 if (!ImplicitFirstprivates.empty()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00003327 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
Alexey Bataev88202be2017-07-27 13:20:36 +00003328 ImplicitFirstprivates, SourceLocation(), SourceLocation(),
3329 SourceLocation())) {
Alexey Bataev68446b72014-07-18 07:47:19 +00003330 ClausesWithImplicit.push_back(Implicit);
3331 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
Alexey Bataev88202be2017-07-27 13:20:36 +00003332 ImplicitFirstprivates.size();
Alexey Bataeve3727102018-04-18 15:57:46 +00003333 } else {
Alexey Bataev68446b72014-07-18 07:47:19 +00003334 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003335 }
Alexey Bataev68446b72014-07-18 07:47:19 +00003336 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003337 if (!ImplicitMaps.empty()) {
3338 if (OMPClause *Implicit = ActOnOpenMPMapClause(
3339 OMPC_MAP_unknown, OMPC_MAP_tofrom, /*IsMapTypeImplicit=*/true,
3340 SourceLocation(), SourceLocation(), ImplicitMaps,
3341 SourceLocation(), SourceLocation(), SourceLocation())) {
3342 ClausesWithImplicit.emplace_back(Implicit);
3343 ErrorFound |=
3344 cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMaps.size();
Alexey Bataeve3727102018-04-18 15:57:46 +00003345 } else {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003346 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003347 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003348 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003349 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003350
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003351 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003352 switch (Kind) {
3353 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00003354 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3355 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003356 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003357 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003358 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003359 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3360 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003361 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003362 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003363 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3364 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003365 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003366 case OMPD_for_simd:
3367 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3368 EndLoc, VarsWithInheritedDSA);
3369 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003370 case OMPD_sections:
3371 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3372 EndLoc);
3373 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003374 case OMPD_section:
3375 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003376 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003377 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3378 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003379 case OMPD_single:
3380 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3381 EndLoc);
3382 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003383 case OMPD_master:
3384 assert(ClausesWithImplicit.empty() &&
3385 "No clauses are allowed for 'omp master' directive");
3386 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3387 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003388 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003389 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3390 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003391 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003392 case OMPD_parallel_for:
3393 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3394 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003395 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003396 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003397 case OMPD_parallel_for_simd:
3398 Res = ActOnOpenMPParallelForSimdDirective(
3399 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003400 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003401 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003402 case OMPD_parallel_sections:
3403 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3404 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003405 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003406 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003407 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003408 Res =
3409 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003410 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003411 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003412 case OMPD_taskyield:
3413 assert(ClausesWithImplicit.empty() &&
3414 "No clauses are allowed for 'omp taskyield' directive");
3415 assert(AStmt == nullptr &&
3416 "No associated statement allowed for 'omp taskyield' directive");
3417 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3418 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003419 case OMPD_barrier:
3420 assert(ClausesWithImplicit.empty() &&
3421 "No clauses are allowed for 'omp barrier' directive");
3422 assert(AStmt == nullptr &&
3423 "No associated statement allowed for 'omp barrier' directive");
3424 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3425 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003426 case OMPD_taskwait:
3427 assert(ClausesWithImplicit.empty() &&
3428 "No clauses are allowed for 'omp taskwait' directive");
3429 assert(AStmt == nullptr &&
3430 "No associated statement allowed for 'omp taskwait' directive");
3431 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3432 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003433 case OMPD_taskgroup:
Alexey Bataev169d96a2017-07-18 20:17:46 +00003434 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc,
3435 EndLoc);
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003436 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003437 case OMPD_flush:
3438 assert(AStmt == nullptr &&
3439 "No associated statement allowed for 'omp flush' directive");
3440 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3441 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003442 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003443 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3444 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003445 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003446 case OMPD_atomic:
3447 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3448 EndLoc);
3449 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003450 case OMPD_teams:
3451 Res =
3452 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3453 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003454 case OMPD_target:
3455 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3456 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003457 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003458 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003459 case OMPD_target_parallel:
3460 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3461 StartLoc, EndLoc);
3462 AllowedNameModifiers.push_back(OMPD_target);
3463 AllowedNameModifiers.push_back(OMPD_parallel);
3464 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003465 case OMPD_target_parallel_for:
3466 Res = ActOnOpenMPTargetParallelForDirective(
3467 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3468 AllowedNameModifiers.push_back(OMPD_target);
3469 AllowedNameModifiers.push_back(OMPD_parallel);
3470 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003471 case OMPD_cancellation_point:
3472 assert(ClausesWithImplicit.empty() &&
3473 "No clauses are allowed for 'omp cancellation point' directive");
3474 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3475 "cancellation point' directive");
3476 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3477 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003478 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003479 assert(AStmt == nullptr &&
3480 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003481 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3482 CancelRegion);
3483 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003484 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003485 case OMPD_target_data:
3486 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3487 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003488 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003489 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003490 case OMPD_target_enter_data:
3491 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00003492 EndLoc, AStmt);
Samuel Antaodf67fc42016-01-19 19:15:56 +00003493 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3494 break;
Samuel Antao72590762016-01-19 20:04:50 +00003495 case OMPD_target_exit_data:
3496 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00003497 EndLoc, AStmt);
Samuel Antao72590762016-01-19 20:04:50 +00003498 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3499 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003500 case OMPD_taskloop:
3501 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3502 EndLoc, VarsWithInheritedDSA);
3503 AllowedNameModifiers.push_back(OMPD_taskloop);
3504 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003505 case OMPD_taskloop_simd:
3506 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3507 EndLoc, VarsWithInheritedDSA);
3508 AllowedNameModifiers.push_back(OMPD_taskloop);
3509 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003510 case OMPD_distribute:
3511 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3512 EndLoc, VarsWithInheritedDSA);
3513 break;
Samuel Antao686c70c2016-05-26 17:30:50 +00003514 case OMPD_target_update:
Alexey Bataev7828b252017-11-21 17:08:48 +00003515 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc,
3516 EndLoc, AStmt);
Samuel Antao686c70c2016-05-26 17:30:50 +00003517 AllowedNameModifiers.push_back(OMPD_target_update);
3518 break;
Carlo Bertolli9925f152016-06-27 14:55:37 +00003519 case OMPD_distribute_parallel_for:
3520 Res = ActOnOpenMPDistributeParallelForDirective(
3521 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3522 AllowedNameModifiers.push_back(OMPD_parallel);
3523 break;
Kelvin Li4a39add2016-07-05 05:00:15 +00003524 case OMPD_distribute_parallel_for_simd:
3525 Res = ActOnOpenMPDistributeParallelForSimdDirective(
3526 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3527 AllowedNameModifiers.push_back(OMPD_parallel);
3528 break;
Kelvin Li787f3fc2016-07-06 04:45:38 +00003529 case OMPD_distribute_simd:
3530 Res = ActOnOpenMPDistributeSimdDirective(
3531 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3532 break;
Kelvin Lia579b912016-07-14 02:54:56 +00003533 case OMPD_target_parallel_for_simd:
3534 Res = ActOnOpenMPTargetParallelForSimdDirective(
3535 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3536 AllowedNameModifiers.push_back(OMPD_target);
3537 AllowedNameModifiers.push_back(OMPD_parallel);
3538 break;
Kelvin Li986330c2016-07-20 22:57:10 +00003539 case OMPD_target_simd:
3540 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3541 EndLoc, VarsWithInheritedDSA);
3542 AllowedNameModifiers.push_back(OMPD_target);
3543 break;
Kelvin Li02532872016-08-05 14:37:37 +00003544 case OMPD_teams_distribute:
David Majnemer9d168222016-08-05 17:44:54 +00003545 Res = ActOnOpenMPTeamsDistributeDirective(
3546 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Kelvin Li02532872016-08-05 14:37:37 +00003547 break;
Kelvin Li4e325f72016-10-25 12:50:55 +00003548 case OMPD_teams_distribute_simd:
3549 Res = ActOnOpenMPTeamsDistributeSimdDirective(
3550 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3551 break;
Kelvin Li579e41c2016-11-30 23:51:03 +00003552 case OMPD_teams_distribute_parallel_for_simd:
3553 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
3554 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3555 AllowedNameModifiers.push_back(OMPD_parallel);
3556 break;
Kelvin Li7ade93f2016-12-09 03:24:30 +00003557 case OMPD_teams_distribute_parallel_for:
3558 Res = ActOnOpenMPTeamsDistributeParallelForDirective(
3559 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3560 AllowedNameModifiers.push_back(OMPD_parallel);
3561 break;
Kelvin Libf594a52016-12-17 05:48:59 +00003562 case OMPD_target_teams:
3563 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc,
3564 EndLoc);
3565 AllowedNameModifiers.push_back(OMPD_target);
3566 break;
Kelvin Li83c451e2016-12-25 04:52:54 +00003567 case OMPD_target_teams_distribute:
3568 Res = ActOnOpenMPTargetTeamsDistributeDirective(
3569 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3570 AllowedNameModifiers.push_back(OMPD_target);
3571 break;
Kelvin Li80e8f562016-12-29 22:16:30 +00003572 case OMPD_target_teams_distribute_parallel_for:
3573 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
3574 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3575 AllowedNameModifiers.push_back(OMPD_target);
3576 AllowedNameModifiers.push_back(OMPD_parallel);
3577 break;
Kelvin Li1851df52017-01-03 05:23:48 +00003578 case OMPD_target_teams_distribute_parallel_for_simd:
3579 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
3580 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3581 AllowedNameModifiers.push_back(OMPD_target);
3582 AllowedNameModifiers.push_back(OMPD_parallel);
3583 break;
Kelvin Lida681182017-01-10 18:08:18 +00003584 case OMPD_target_teams_distribute_simd:
3585 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
3586 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3587 AllowedNameModifiers.push_back(OMPD_target);
3588 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003589 case OMPD_declare_target:
3590 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003591 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003592 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003593 case OMPD_declare_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00003594 case OMPD_requires:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003595 llvm_unreachable("OpenMP Directive is not allowed");
3596 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003597 llvm_unreachable("Unknown OpenMP directive");
3598 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003599
Alexey Bataeve3727102018-04-18 15:57:46 +00003600 for (const auto &P : VarsWithInheritedDSA) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003601 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3602 << P.first << P.second->getSourceRange();
3603 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003604 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3605
3606 if (!AllowedNameModifiers.empty())
3607 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3608 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003609
Alexey Bataeved09d242014-05-28 05:53:51 +00003610 if (ErrorFound)
3611 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003612 return Res;
3613}
3614
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003615Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3616 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00003617 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00003618 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3619 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003620 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00003621 assert(Linears.size() == LinModifiers.size());
3622 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00003623 if (!DG || DG.get().isNull())
3624 return DeclGroupPtrTy();
3625
3626 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00003627 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003628 return DG;
3629 }
Alexey Bataeve3727102018-04-18 15:57:46 +00003630 Decl *ADecl = DG.get().getSingleDecl();
Alexey Bataev587e1de2016-03-30 10:43:55 +00003631 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3632 ADecl = FTD->getTemplatedDecl();
3633
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003634 auto *FD = dyn_cast<FunctionDecl>(ADecl);
3635 if (!FD) {
3636 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003637 return DeclGroupPtrTy();
3638 }
3639
Alexey Bataev2af33e32016-04-07 12:45:37 +00003640 // OpenMP [2.8.2, declare simd construct, Description]
3641 // The parameter of the simdlen clause must be a constant positive integer
3642 // expression.
3643 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003644 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00003645 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003646 // OpenMP [2.8.2, declare simd construct, Description]
3647 // The special this pointer can be used as if was one of the arguments to the
3648 // function in any of the linear, aligned, or uniform clauses.
3649 // The uniform clause declares one or more arguments to have an invariant
3650 // value for all concurrent invocations of the function in the execution of a
3651 // single SIMD loop.
Alexey Bataeve3727102018-04-18 15:57:46 +00003652 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs;
3653 const Expr *UniformedLinearThis = nullptr;
3654 for (const Expr *E : Uniforms) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003655 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003656 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3657 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003658 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3659 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00003660 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
Alexey Bataev43a919f2018-04-13 17:48:43 +00003661 UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003662 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003663 }
3664 if (isa<CXXThisExpr>(E)) {
3665 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003666 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003667 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003668 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3669 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00003670 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00003671 // OpenMP [2.8.2, declare simd construct, Description]
3672 // The aligned clause declares that the object to which each list item points
3673 // is aligned to the number of bytes expressed in the optional parameter of
3674 // the aligned clause.
3675 // The special this pointer can be used as if was one of the arguments to the
3676 // function in any of the linear, aligned, or uniform clauses.
3677 // The type of list items appearing in the aligned clause must be array,
3678 // pointer, reference to array, or reference to pointer.
Alexey Bataeve3727102018-04-18 15:57:46 +00003679 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs;
3680 const Expr *AlignedThis = nullptr;
3681 for (const Expr *E : Aligneds) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003682 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003683 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3684 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3685 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevd93d3762016-04-12 09:35:56 +00003686 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3687 FD->getParamDecl(PVD->getFunctionScopeIndex())
3688 ->getCanonicalDecl() == CanonPVD) {
3689 // OpenMP [2.8.1, simd construct, Restrictions]
3690 // A list-item cannot appear in more than one aligned clause.
3691 if (AlignedArgs.count(CanonPVD) > 0) {
3692 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3693 << 1 << E->getSourceRange();
3694 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3695 diag::note_omp_explicit_dsa)
3696 << getOpenMPClauseName(OMPC_aligned);
3697 continue;
3698 }
3699 AlignedArgs[CanonPVD] = E;
3700 QualType QTy = PVD->getType()
3701 .getNonReferenceType()
3702 .getUnqualifiedType()
3703 .getCanonicalType();
3704 const Type *Ty = QTy.getTypePtrOrNull();
3705 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3706 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3707 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3708 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3709 }
3710 continue;
3711 }
3712 }
3713 if (isa<CXXThisExpr>(E)) {
3714 if (AlignedThis) {
3715 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3716 << 2 << E->getSourceRange();
3717 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3718 << getOpenMPClauseName(OMPC_aligned);
3719 }
3720 AlignedThis = E;
3721 continue;
3722 }
3723 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3724 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3725 }
3726 // The optional parameter of the aligned clause, alignment, must be a constant
3727 // positive integer expression. If no optional parameter is specified,
3728 // implementation-defined default alignments for SIMD instructions on the
3729 // target platforms are assumed.
Alexey Bataeve3727102018-04-18 15:57:46 +00003730 SmallVector<const Expr *, 4> NewAligns;
3731 for (Expr *E : Alignments) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003732 ExprResult Align;
3733 if (E)
3734 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3735 NewAligns.push_back(Align.get());
3736 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00003737 // OpenMP [2.8.2, declare simd construct, Description]
3738 // The linear clause declares one or more list items to be private to a SIMD
3739 // lane and to have a linear relationship with respect to the iteration space
3740 // of a loop.
3741 // The special this pointer can be used as if was one of the arguments to the
3742 // function in any of the linear, aligned, or uniform clauses.
3743 // When a linear-step expression is specified in a linear clause it must be
3744 // either a constant integer expression or an integer-typed parameter that is
3745 // specified in a uniform clause on the directive.
Alexey Bataeve3727102018-04-18 15:57:46 +00003746 llvm::DenseMap<const Decl *, const Expr *> LinearArgs;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003747 const bool IsUniformedThis = UniformedLinearThis != nullptr;
3748 auto MI = LinModifiers.begin();
Alexey Bataeve3727102018-04-18 15:57:46 +00003749 for (const Expr *E : Linears) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003750 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3751 ++MI;
3752 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003753 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3754 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3755 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevecba70f2016-04-12 11:02:11 +00003756 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3757 FD->getParamDecl(PVD->getFunctionScopeIndex())
3758 ->getCanonicalDecl() == CanonPVD) {
3759 // OpenMP [2.15.3.7, linear Clause, Restrictions]
3760 // A list-item cannot appear in more than one linear clause.
3761 if (LinearArgs.count(CanonPVD) > 0) {
3762 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3763 << getOpenMPClauseName(OMPC_linear)
3764 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3765 Diag(LinearArgs[CanonPVD]->getExprLoc(),
3766 diag::note_omp_explicit_dsa)
3767 << getOpenMPClauseName(OMPC_linear);
3768 continue;
3769 }
3770 // Each argument can appear in at most one uniform or linear clause.
3771 if (UniformedArgs.count(CanonPVD) > 0) {
3772 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3773 << getOpenMPClauseName(OMPC_linear)
3774 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3775 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3776 diag::note_omp_explicit_dsa)
3777 << getOpenMPClauseName(OMPC_uniform);
3778 continue;
3779 }
3780 LinearArgs[CanonPVD] = E;
3781 if (E->isValueDependent() || E->isTypeDependent() ||
3782 E->isInstantiationDependent() ||
3783 E->containsUnexpandedParameterPack())
3784 continue;
3785 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3786 PVD->getOriginalType());
3787 continue;
3788 }
3789 }
3790 if (isa<CXXThisExpr>(E)) {
3791 if (UniformedLinearThis) {
3792 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3793 << getOpenMPClauseName(OMPC_linear)
3794 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3795 << E->getSourceRange();
3796 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3797 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3798 : OMPC_linear);
3799 continue;
3800 }
3801 UniformedLinearThis = E;
3802 if (E->isValueDependent() || E->isTypeDependent() ||
3803 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3804 continue;
3805 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3806 E->getType());
3807 continue;
3808 }
3809 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3810 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3811 }
3812 Expr *Step = nullptr;
3813 Expr *NewStep = nullptr;
3814 SmallVector<Expr *, 4> NewSteps;
Alexey Bataeve3727102018-04-18 15:57:46 +00003815 for (Expr *E : Steps) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003816 // Skip the same step expression, it was checked already.
3817 if (Step == E || !E) {
3818 NewSteps.push_back(E ? NewStep : nullptr);
3819 continue;
3820 }
3821 Step = E;
Alexey Bataeve3727102018-04-18 15:57:46 +00003822 if (const auto *DRE = dyn_cast<DeclRefExpr>(Step))
3823 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3824 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevecba70f2016-04-12 11:02:11 +00003825 if (UniformedArgs.count(CanonPVD) == 0) {
3826 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3827 << Step->getSourceRange();
3828 } else if (E->isValueDependent() || E->isTypeDependent() ||
3829 E->isInstantiationDependent() ||
3830 E->containsUnexpandedParameterPack() ||
Alexey Bataeve3727102018-04-18 15:57:46 +00003831 CanonPVD->getType()->hasIntegerRepresentation()) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003832 NewSteps.push_back(Step);
Alexey Bataeve3727102018-04-18 15:57:46 +00003833 } else {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003834 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3835 << Step->getSourceRange();
3836 }
3837 continue;
3838 }
3839 NewStep = Step;
3840 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3841 !Step->isInstantiationDependent() &&
3842 !Step->containsUnexpandedParameterPack()) {
3843 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3844 .get();
3845 if (NewStep)
3846 NewStep = VerifyIntegerConstantExpression(NewStep).get();
3847 }
3848 NewSteps.push_back(NewStep);
3849 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003850 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3851 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00003852 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00003853 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3854 const_cast<Expr **>(Linears.data()), Linears.size(),
3855 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3856 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003857 ADecl->addAttr(NewAttr);
3858 return ConvertDeclToDeclGroup(ADecl);
3859}
3860
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003861StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3862 Stmt *AStmt,
3863 SourceLocation StartLoc,
3864 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003865 if (!AStmt)
3866 return StmtError();
3867
Alexey Bataeve3727102018-04-18 15:57:46 +00003868 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev9959db52014-05-06 10:08:46 +00003869 // 1.2.2 OpenMP Language Terminology
3870 // Structured block - An executable statement with a single entry at the
3871 // top and a single exit at the bottom.
3872 // The point of exit cannot be a branch out of the structured block.
3873 // longjmp() and throw() must not violate the entry/exit criteria.
3874 CS->getCapturedDecl()->setNothrow();
3875
Reid Kleckner87a31802018-03-12 21:43:02 +00003876 setFunctionHasBranchProtectedScope();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003877
Alexey Bataev25e5b442015-09-15 12:52:43 +00003878 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3879 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003880}
3881
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003882namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003883/// Helper class for checking canonical form of the OpenMP loops and
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003884/// extracting iteration space of each loop in the loop nest, that will be used
3885/// for IR generation.
3886class OpenMPIterationSpaceChecker {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003887 /// Reference to Sema.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003888 Sema &SemaRef;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003889 /// A location for diagnostics (when there is no some better location).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003890 SourceLocation DefaultLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003891 /// A location for diagnostics (when increment is not compatible).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003892 SourceLocation ConditionLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003893 /// A source location for referring to loop init later.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003894 SourceRange InitSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003895 /// A source location for referring to condition later.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003896 SourceRange ConditionSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003897 /// A source location for referring to increment later.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003898 SourceRange IncrementSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003899 /// Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003900 ValueDecl *LCDecl = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003901 /// Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003902 Expr *LCRef = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003903 /// Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003904 Expr *LB = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003905 /// Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003906 Expr *UB = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003907 /// Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003908 Expr *Step = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003909 /// This flag is true when condition is one of:
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003910 /// Var < UB
3911 /// Var <= UB
3912 /// UB > Var
3913 /// UB >= Var
Kelvin Liefbe4af2018-11-21 19:10:48 +00003914 /// This will have no value when the condition is !=
3915 llvm::Optional<bool> TestIsLessOp;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003916 /// This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003917 bool TestIsStrictOp = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003918 /// This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003919 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003920
3921public:
3922 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003923 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003924 /// Check init-expr for canonical loop form and save loop counter
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003925 /// variable - #Var and its initialization value - #LB.
Alexey Bataeve3727102018-04-18 15:57:46 +00003926 bool checkAndSetInit(Stmt *S, bool EmitDiags = true);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003927 /// Check test-expr for canonical form, save upper-bound (#UB), flags
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003928 /// for less/greater and for strict/non-strict comparison.
Alexey Bataeve3727102018-04-18 15:57:46 +00003929 bool checkAndSetCond(Expr *S);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003930 /// Check incr-expr for canonical loop form and return true if it
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003931 /// does not conform, otherwise save loop step (#Step).
Alexey Bataeve3727102018-04-18 15:57:46 +00003932 bool checkAndSetInc(Expr *S);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003933 /// Return the loop counter variable.
Alexey Bataeve3727102018-04-18 15:57:46 +00003934 ValueDecl *getLoopDecl() const { return LCDecl; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003935 /// Return the reference expression to loop counter variable.
Alexey Bataeve3727102018-04-18 15:57:46 +00003936 Expr *getLoopDeclRefExpr() const { return LCRef; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003937 /// Source range of the loop init.
Alexey Bataeve3727102018-04-18 15:57:46 +00003938 SourceRange getInitSrcRange() const { return InitSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003939 /// Source range of the loop condition.
Alexey Bataeve3727102018-04-18 15:57:46 +00003940 SourceRange getConditionSrcRange() const { return ConditionSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003941 /// Source range of the loop increment.
Alexey Bataeve3727102018-04-18 15:57:46 +00003942 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003943 /// True if the step should be subtracted.
Alexey Bataeve3727102018-04-18 15:57:46 +00003944 bool shouldSubtractStep() const { return SubtractStep; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003945 /// Build the expression to calculate the number of iterations.
Alexey Bataeve3727102018-04-18 15:57:46 +00003946 Expr *buildNumIterations(
3947 Scope *S, const bool LimitedType,
3948 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003949 /// Build the precondition expression for the loops.
Alexey Bataeve3727102018-04-18 15:57:46 +00003950 Expr *
3951 buildPreCond(Scope *S, Expr *Cond,
3952 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003953 /// Build reference expression to the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00003954 DeclRefExpr *
3955 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
3956 DSAStackTy &DSA) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003957 /// Build reference expression to the private counter be used for
Alexey Bataeva8899172015-08-06 12:30:57 +00003958 /// codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00003959 Expr *buildPrivateCounterVar() const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003960 /// Build initialization of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00003961 Expr *buildCounterInit() const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003962 /// Build step of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00003963 Expr *buildCounterStep() const;
Alexey Bataevf138fda2018-08-13 19:04:24 +00003964 /// Build loop data with counter value for depend clauses in ordered
3965 /// directives.
3966 Expr *
3967 buildOrderedLoopData(Scope *S, Expr *Counter,
3968 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
3969 SourceLocation Loc, Expr *Inc = nullptr,
3970 OverloadedOperatorKind OOK = OO_Amp);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003971 /// Return true if any expression is dependent.
Alexey Bataeve3727102018-04-18 15:57:46 +00003972 bool dependent() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003973
3974private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003975 /// Check the right-hand side of an assignment in the increment
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003976 /// expression.
Alexey Bataeve3727102018-04-18 15:57:46 +00003977 bool checkAndSetIncRHS(Expr *RHS);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003978 /// Helper to set loop counter variable and its initializer.
Alexey Bataeve3727102018-04-18 15:57:46 +00003979 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003980 /// Helper to set upper bound.
Kelvin Liefbe4af2018-11-21 19:10:48 +00003981 bool setUB(Expr *NewUB, llvm::Optional<bool> LessOp, bool StrictOp,
3982 SourceRange SR, SourceLocation SL);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003983 /// Helper to set loop increment.
Alexey Bataeve3727102018-04-18 15:57:46 +00003984 bool setStep(Expr *NewStep, bool Subtract);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003985};
3986
Alexey Bataeve3727102018-04-18 15:57:46 +00003987bool OpenMPIterationSpaceChecker::dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003988 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003989 assert(!LB && !UB && !Step);
3990 return false;
3991 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003992 return LCDecl->getType()->isDependentType() ||
3993 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
3994 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003995}
3996
Alexey Bataeve3727102018-04-18 15:57:46 +00003997bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl,
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003998 Expr *NewLCRefExpr,
3999 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004000 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004001 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00004002 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004003 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004004 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004005 LCDecl = getCanonicalDecl(NewLCDecl);
4006 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004007 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
4008 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004009 if ((Ctor->isCopyOrMoveConstructor() ||
4010 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4011 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004012 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004013 LB = NewLB;
4014 return false;
4015}
4016
Kelvin Liefbe4af2018-11-21 19:10:48 +00004017bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB, llvm::Optional<bool> LessOp,
4018 bool StrictOp, SourceRange SR,
4019 SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004020 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004021 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
4022 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004023 if (!NewUB)
4024 return true;
4025 UB = NewUB;
Kelvin Liefbe4af2018-11-21 19:10:48 +00004026 if (LessOp)
4027 TestIsLessOp = LessOp;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004028 TestIsStrictOp = StrictOp;
4029 ConditionSrcRange = SR;
4030 ConditionLoc = SL;
4031 return false;
4032}
4033
Alexey Bataeve3727102018-04-18 15:57:46 +00004034bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004035 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004036 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004037 if (!NewStep)
4038 return true;
4039 if (!NewStep->isValueDependent()) {
4040 // Check that the step is integer expression.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004041 SourceLocation StepLoc = NewStep->getBeginLoc();
Alexey Bataev5372fb82017-08-31 23:06:52 +00004042 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion(
4043 StepLoc, getExprAsWritten(NewStep));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004044 if (Val.isInvalid())
4045 return true;
4046 NewStep = Val.get();
4047
4048 // OpenMP [2.6, Canonical Loop Form, Restrictions]
4049 // If test-expr is of form var relational-op b and relational-op is < or
4050 // <= then incr-expr must cause var to increase on each iteration of the
4051 // loop. If test-expr is of form var relational-op b and relational-op is
4052 // > or >= then incr-expr must cause var to decrease on each iteration of
4053 // the loop.
4054 // If test-expr is of form b relational-op var and relational-op is < or
4055 // <= then incr-expr must cause var to decrease on each iteration of the
4056 // loop. If test-expr is of form b relational-op var and relational-op is
4057 // > or >= then incr-expr must cause var to increase on each iteration of
4058 // the loop.
4059 llvm::APSInt Result;
4060 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
4061 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
4062 bool IsConstNeg =
4063 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004064 bool IsConstPos =
4065 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004066 bool IsConstZero = IsConstant && !Result.getBoolValue();
Kelvin Liefbe4af2018-11-21 19:10:48 +00004067
4068 // != with increment is treated as <; != with decrement is treated as >
4069 if (!TestIsLessOp.hasValue())
4070 TestIsLessOp = IsConstPos || (IsUnsigned && !Subtract);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004071 if (UB && (IsConstZero ||
Kelvin Liefbe4af2018-11-21 19:10:48 +00004072 (TestIsLessOp.getValue() ?
4073 (IsConstNeg || (IsUnsigned && Subtract)) :
4074 (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004075 SemaRef.Diag(NewStep->getExprLoc(),
4076 diag::err_omp_loop_incr_not_compatible)
Kelvin Liefbe4af2018-11-21 19:10:48 +00004077 << LCDecl << TestIsLessOp.getValue() << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004078 SemaRef.Diag(ConditionLoc,
4079 diag::note_omp_loop_cond_requres_compatible_incr)
Kelvin Liefbe4af2018-11-21 19:10:48 +00004080 << TestIsLessOp.getValue() << ConditionSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004081 return true;
4082 }
Kelvin Liefbe4af2018-11-21 19:10:48 +00004083 if (TestIsLessOp.getValue() == Subtract) {
David Majnemer9d168222016-08-05 17:44:54 +00004084 NewStep =
4085 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep)
4086 .get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004087 Subtract = !Subtract;
4088 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004089 }
4090
4091 Step = NewStep;
4092 SubtractStep = Subtract;
4093 return false;
4094}
4095
Alexey Bataeve3727102018-04-18 15:57:46 +00004096bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004097 // Check init-expr for canonical loop form and save loop counter
4098 // variable - #Var and its initialization value - #LB.
4099 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
4100 // var = lb
4101 // integer-type var = lb
4102 // random-access-iterator-type var = lb
4103 // pointer-type var = lb
4104 //
4105 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00004106 if (EmitDiags) {
4107 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
4108 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004109 return true;
4110 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004111 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4112 if (!ExprTemp->cleanupsHaveSideEffects())
4113 S = ExprTemp->getSubExpr();
4114
Alexander Musmana5f070a2014-10-01 06:03:56 +00004115 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004116 if (Expr *E = dyn_cast<Expr>(S))
4117 S = E->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00004118 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004119 if (BO->getOpcode() == BO_Assign) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004120 Expr *LHS = BO->getLHS()->IgnoreParens();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004121 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
4122 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4123 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataeve3727102018-04-18 15:57:46 +00004124 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4125 return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004126 }
4127 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4128 if (ME->isArrow() &&
4129 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
Alexey Bataeve3727102018-04-18 15:57:46 +00004130 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004131 }
4132 }
David Majnemer9d168222016-08-05 17:44:54 +00004133 } else if (auto *DS = dyn_cast<DeclStmt>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004134 if (DS->isSingleDecl()) {
David Majnemer9d168222016-08-05 17:44:54 +00004135 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00004136 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004137 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00004138 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004139 SemaRef.Diag(S->getBeginLoc(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004140 diag::ext_omp_loop_not_canonical_init)
4141 << S->getSourceRange();
Alexey Bataevf138fda2018-08-13 19:04:24 +00004142 return setLCDeclAndLB(
4143 Var,
4144 buildDeclRefExpr(SemaRef, Var,
4145 Var->getType().getNonReferenceType(),
4146 DS->getBeginLoc()),
4147 Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004148 }
4149 }
4150 }
David Majnemer9d168222016-08-05 17:44:54 +00004151 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004152 if (CE->getOperator() == OO_Equal) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004153 Expr *LHS = CE->getArg(0);
David Majnemer9d168222016-08-05 17:44:54 +00004154 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004155 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4156 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataeve3727102018-04-18 15:57:46 +00004157 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4158 return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004159 }
4160 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4161 if (ME->isArrow() &&
4162 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
Alexey Bataeve3727102018-04-18 15:57:46 +00004163 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004164 }
4165 }
4166 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004167
Alexey Bataeve3727102018-04-18 15:57:46 +00004168 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004169 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00004170 if (EmitDiags) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004171 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init)
Alexey Bataev9c821032015-04-30 04:23:23 +00004172 << S->getSourceRange();
4173 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004174 return true;
4175}
4176
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004177/// Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004178/// variable (which may be the loop variable) if possible.
Alexey Bataeve3727102018-04-18 15:57:46 +00004179static const ValueDecl *getInitLCDecl(const Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004180 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00004181 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004182 E = getExprAsWritten(E);
Alexey Bataeve3727102018-04-18 15:57:46 +00004183 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004184 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004185 if ((Ctor->isCopyOrMoveConstructor() ||
4186 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4187 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004188 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00004189 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
4190 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004191 return getCanonicalDecl(VD);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004192 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004193 if (const auto *ME = dyn_cast_or_null<MemberExpr>(E))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004194 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4195 return getCanonicalDecl(ME->getMemberDecl());
4196 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004197}
4198
Alexey Bataeve3727102018-04-18 15:57:46 +00004199bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004200 // Check test-expr for canonical form, save upper-bound UB, flags for
4201 // less/greater and for strict/non-strict comparison.
4202 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4203 // var relational-op b
4204 // b relational-op var
4205 //
4206 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004207 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004208 return true;
4209 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004210 S = getExprAsWritten(S);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004211 SourceLocation CondLoc = S->getBeginLoc();
David Majnemer9d168222016-08-05 17:44:54 +00004212 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004213 if (BO->isRelationalOp()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004214 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4215 return setUB(BO->getRHS(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004216 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
4217 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4218 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +00004219 if (getInitLCDecl(BO->getRHS()) == LCDecl)
4220 return setUB(BO->getLHS(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004221 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
4222 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4223 BO->getSourceRange(), BO->getOperatorLoc());
Kelvin Liefbe4af2018-11-21 19:10:48 +00004224 } else if (BO->getOpcode() == BO_NE)
4225 return setUB(getInitLCDecl(BO->getLHS()) == LCDecl ?
4226 BO->getRHS() : BO->getLHS(),
4227 /*LessOp=*/llvm::None,
4228 /*StrictOp=*/true,
4229 BO->getSourceRange(), BO->getOperatorLoc());
David Majnemer9d168222016-08-05 17:44:54 +00004230 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004231 if (CE->getNumArgs() == 2) {
4232 auto Op = CE->getOperator();
4233 switch (Op) {
4234 case OO_Greater:
4235 case OO_GreaterEqual:
4236 case OO_Less:
4237 case OO_LessEqual:
Alexey Bataeve3727102018-04-18 15:57:46 +00004238 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4239 return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004240 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4241 CE->getOperatorLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +00004242 if (getInitLCDecl(CE->getArg(1)) == LCDecl)
4243 return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004244 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4245 CE->getOperatorLoc());
4246 break;
Kelvin Liefbe4af2018-11-21 19:10:48 +00004247 case OO_ExclaimEqual:
4248 return setUB(getInitLCDecl(CE->getArg(0)) == LCDecl ?
4249 CE->getArg(1) : CE->getArg(0),
4250 /*LessOp=*/llvm::None,
4251 /*StrictOp=*/true,
4252 CE->getSourceRange(),
4253 CE->getOperatorLoc());
4254 break;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004255 default:
4256 break;
4257 }
4258 }
4259 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004260 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004261 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004262 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004263 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004264 return true;
4265}
4266
Alexey Bataeve3727102018-04-18 15:57:46 +00004267bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004268 // RHS of canonical loop form increment can be:
4269 // var + incr
4270 // incr + var
4271 // var - incr
4272 //
4273 RHS = RHS->IgnoreParenImpCasts();
David Majnemer9d168222016-08-05 17:44:54 +00004274 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004275 if (BO->isAdditiveOp()) {
4276 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataeve3727102018-04-18 15:57:46 +00004277 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4278 return setStep(BO->getRHS(), !IsAdd);
4279 if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl)
4280 return setStep(BO->getLHS(), /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004281 }
David Majnemer9d168222016-08-05 17:44:54 +00004282 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004283 bool IsAdd = CE->getOperator() == OO_Plus;
4284 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004285 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4286 return setStep(CE->getArg(1), !IsAdd);
4287 if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl)
4288 return setStep(CE->getArg(0), /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004289 }
4290 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004291 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004292 return false;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004293 SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004294 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004295 return true;
4296}
4297
Alexey Bataeve3727102018-04-18 15:57:46 +00004298bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004299 // Check incr-expr for canonical loop form and return true if it
4300 // does not conform.
4301 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4302 // ++var
4303 // var++
4304 // --var
4305 // var--
4306 // var += incr
4307 // var -= incr
4308 // var = var + incr
4309 // var = incr + var
4310 // var = var - incr
4311 //
4312 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004313 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004314 return true;
4315 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004316 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4317 if (!ExprTemp->cleanupsHaveSideEffects())
4318 S = ExprTemp->getSubExpr();
4319
Alexander Musmana5f070a2014-10-01 06:03:56 +00004320 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004321 S = S->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00004322 if (auto *UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004323 if (UO->isIncrementDecrementOp() &&
Alexey Bataeve3727102018-04-18 15:57:46 +00004324 getInitLCDecl(UO->getSubExpr()) == LCDecl)
4325 return setStep(SemaRef
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004326 .ActOnIntegerConstant(UO->getBeginLoc(),
David Majnemer9d168222016-08-05 17:44:54 +00004327 (UO->isDecrementOp() ? -1 : 1))
4328 .get(),
Alexey Bataeve3727102018-04-18 15:57:46 +00004329 /*Subtract=*/false);
David Majnemer9d168222016-08-05 17:44:54 +00004330 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004331 switch (BO->getOpcode()) {
4332 case BO_AddAssign:
4333 case BO_SubAssign:
Alexey Bataeve3727102018-04-18 15:57:46 +00004334 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4335 return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004336 break;
4337 case BO_Assign:
Alexey Bataeve3727102018-04-18 15:57:46 +00004338 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4339 return checkAndSetIncRHS(BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004340 break;
4341 default:
4342 break;
4343 }
David Majnemer9d168222016-08-05 17:44:54 +00004344 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004345 switch (CE->getOperator()) {
4346 case OO_PlusPlus:
4347 case OO_MinusMinus:
Alexey Bataeve3727102018-04-18 15:57:46 +00004348 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4349 return setStep(SemaRef
David Majnemer9d168222016-08-05 17:44:54 +00004350 .ActOnIntegerConstant(
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004351 CE->getBeginLoc(),
David Majnemer9d168222016-08-05 17:44:54 +00004352 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
4353 .get(),
Alexey Bataeve3727102018-04-18 15:57:46 +00004354 /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004355 break;
4356 case OO_PlusEqual:
4357 case OO_MinusEqual:
Alexey Bataeve3727102018-04-18 15:57:46 +00004358 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4359 return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004360 break;
4361 case OO_Equal:
Alexey Bataeve3727102018-04-18 15:57:46 +00004362 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4363 return checkAndSetIncRHS(CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004364 break;
4365 default:
4366 break;
4367 }
4368 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004369 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004370 return false;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004371 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004372 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004373 return true;
4374}
Alexander Musmana5f070a2014-10-01 06:03:56 +00004375
Alexey Bataev5a3af132016-03-29 08:58:54 +00004376static ExprResult
4377tryBuildCapture(Sema &SemaRef, Expr *Capture,
Alexey Bataeve3727102018-04-18 15:57:46 +00004378 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00004379 if (SemaRef.CurContext->isDependentContext())
4380 return ExprResult(Capture);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004381 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
4382 return SemaRef.PerformImplicitConversion(
4383 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
4384 /*AllowExplicit=*/true);
4385 auto I = Captures.find(Capture);
4386 if (I != Captures.end())
4387 return buildCapture(SemaRef, Capture, I->second);
4388 DeclRefExpr *Ref = nullptr;
4389 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4390 Captures[Capture] = Ref;
4391 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004392}
4393
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004394/// Build the expression to calculate the number of iterations.
Alexey Bataeve3727102018-04-18 15:57:46 +00004395Expr *OpenMPIterationSpaceChecker::buildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004396 Scope *S, const bool LimitedType,
Alexey Bataeve3727102018-04-18 15:57:46 +00004397 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004398 ExprResult Diff;
Alexey Bataeve3727102018-04-18 15:57:46 +00004399 QualType VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004400 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004401 SemaRef.getLangOpts().CPlusPlus) {
4402 // Upper - Lower
Kelvin Liefbe4af2018-11-21 19:10:48 +00004403 Expr *UBExpr = TestIsLessOp.getValue() ? UB : LB;
4404 Expr *LBExpr = TestIsLessOp.getValue() ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00004405 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
4406 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004407 if (!Upper || !Lower)
4408 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004409
4410 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4411
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004412 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004413 // BuildBinOp already emitted error, this one is to point user to upper
4414 // and lower bound, and to tell what is passed to 'operator-'.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004415 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004416 << Upper->getSourceRange() << Lower->getSourceRange();
4417 return nullptr;
4418 }
4419 }
4420
4421 if (!Diff.isUsable())
4422 return nullptr;
4423
4424 // Upper - Lower [- 1]
4425 if (TestIsStrictOp)
4426 Diff = SemaRef.BuildBinOp(
4427 S, DefaultLoc, BO_Sub, Diff.get(),
4428 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4429 if (!Diff.isUsable())
4430 return nullptr;
4431
4432 // Upper - Lower [- 1] + Step
Alexey Bataeve3727102018-04-18 15:57:46 +00004433 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004434 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004435 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004436 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004437 if (!Diff.isUsable())
4438 return nullptr;
4439
4440 // Parentheses (for dumping/debugging purposes only).
4441 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4442 if (!Diff.isUsable())
4443 return nullptr;
4444
4445 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004446 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004447 if (!Diff.isUsable())
4448 return nullptr;
4449
Alexander Musman174b3ca2014-10-06 11:16:29 +00004450 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004451 QualType Type = Diff.get()->getType();
Alexey Bataeve3727102018-04-18 15:57:46 +00004452 ASTContext &C = SemaRef.Context;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004453 bool UseVarType = VarType->hasIntegerRepresentation() &&
4454 C.getTypeSize(Type) > C.getTypeSize(VarType);
4455 if (!Type->isIntegerType() || UseVarType) {
4456 unsigned NewSize =
4457 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4458 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4459 : Type->hasSignedIntegerRepresentation();
4460 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00004461 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4462 Diff = SemaRef.PerformImplicitConversion(
4463 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4464 if (!Diff.isUsable())
4465 return nullptr;
4466 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004467 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004468 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00004469 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4470 if (NewSize != C.getTypeSize(Type)) {
4471 if (NewSize < C.getTypeSize(Type)) {
4472 assert(NewSize == 64 && "incorrect loop var size");
4473 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4474 << InitSrcRange << ConditionSrcRange;
4475 }
4476 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004477 NewSize, Type->hasSignedIntegerRepresentation() ||
4478 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00004479 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4480 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4481 Sema::AA_Converting, true);
4482 if (!Diff.isUsable())
4483 return nullptr;
4484 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004485 }
4486 }
4487
Alexander Musmana5f070a2014-10-01 06:03:56 +00004488 return Diff.get();
4489}
4490
Alexey Bataeve3727102018-04-18 15:57:46 +00004491Expr *OpenMPIterationSpaceChecker::buildPreCond(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004492 Scope *S, Expr *Cond,
Alexey Bataeve3727102018-04-18 15:57:46 +00004493 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004494 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4495 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4496 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004497
Alexey Bataeve3727102018-04-18 15:57:46 +00004498 ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures);
4499 ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004500 if (!NewLB.isUsable() || !NewUB.isUsable())
4501 return nullptr;
4502
Alexey Bataeve3727102018-04-18 15:57:46 +00004503 ExprResult CondExpr =
4504 SemaRef.BuildBinOp(S, DefaultLoc,
Kelvin Liefbe4af2018-11-21 19:10:48 +00004505 TestIsLessOp.getValue() ?
4506 (TestIsStrictOp ? BO_LT : BO_LE) :
4507 (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataeve3727102018-04-18 15:57:46 +00004508 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004509 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004510 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4511 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00004512 CondExpr = SemaRef.PerformImplicitConversion(
4513 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4514 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004515 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00004516 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4517 // Otherwise use original loop conditon and evaluate it in runtime.
4518 return CondExpr.isUsable() ? CondExpr.get() : Cond;
4519}
4520
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004521/// Build reference expression to the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004522DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar(
Alexey Bataevf138fda2018-08-13 19:04:24 +00004523 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
4524 DSAStackTy &DSA) const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004525 auto *VD = dyn_cast<VarDecl>(LCDecl);
4526 if (!VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004527 VD = SemaRef.isOpenMPCapturedDecl(LCDecl);
4528 DeclRefExpr *Ref = buildDeclRefExpr(
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004529 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00004530 const DSAStackTy::DSAVarData Data =
4531 DSA.getTopDSA(LCDecl, /*FromParent=*/false);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004532 // If the loop control decl is explicitly marked as private, do not mark it
4533 // as captured again.
4534 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4535 Captures.insert(std::make_pair(LCRef, Ref));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004536 return Ref;
4537 }
4538 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00004539 DefaultLoc);
4540}
4541
Alexey Bataeve3727102018-04-18 15:57:46 +00004542Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004543 if (LCDecl && !LCDecl->isInvalidDecl()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004544 QualType Type = LCDecl->getType().getNonReferenceType();
4545 VarDecl *PrivateVar = buildVarDecl(
Alexey Bataev63cc8e92018-03-20 14:45:59 +00004546 SemaRef, DefaultLoc, Type, LCDecl->getName(),
4547 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr,
4548 isa<VarDecl>(LCDecl)
4549 ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc)
4550 : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00004551 if (PrivateVar->isInvalidDecl())
4552 return nullptr;
4553 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4554 }
4555 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004556}
4557
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004558/// Build initialization of the counter to be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004559Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004560
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004561/// Build step of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004562Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004563
Alexey Bataevf138fda2018-08-13 19:04:24 +00004564Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData(
4565 Scope *S, Expr *Counter,
4566 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc,
4567 Expr *Inc, OverloadedOperatorKind OOK) {
4568 Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get();
4569 if (!Cnt)
4570 return nullptr;
4571 if (Inc) {
4572 assert((OOK == OO_Plus || OOK == OO_Minus) &&
4573 "Expected only + or - operations for depend clauses.");
4574 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub;
4575 Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get();
4576 if (!Cnt)
4577 return nullptr;
4578 }
4579 ExprResult Diff;
4580 QualType VarType = LCDecl->getType().getNonReferenceType();
4581 if (VarType->isIntegerType() || VarType->isPointerType() ||
4582 SemaRef.getLangOpts().CPlusPlus) {
4583 // Upper - Lower
4584 Expr *Upper =
Kelvin Liefbe4af2018-11-21 19:10:48 +00004585 TestIsLessOp.getValue() ? Cnt : tryBuildCapture(SemaRef, UB, Captures).get();
Alexey Bataevf138fda2018-08-13 19:04:24 +00004586 Expr *Lower =
Kelvin Liefbe4af2018-11-21 19:10:48 +00004587 TestIsLessOp.getValue() ? tryBuildCapture(SemaRef, LB, Captures).get() : Cnt;
Alexey Bataevf138fda2018-08-13 19:04:24 +00004588 if (!Upper || !Lower)
4589 return nullptr;
4590
4591 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4592
4593 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
4594 // BuildBinOp already emitted error, this one is to point user to upper
4595 // and lower bound, and to tell what is passed to 'operator-'.
4596 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
4597 << Upper->getSourceRange() << Lower->getSourceRange();
4598 return nullptr;
4599 }
4600 }
4601
4602 if (!Diff.isUsable())
4603 return nullptr;
4604
4605 // Parentheses (for dumping/debugging purposes only).
4606 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4607 if (!Diff.isUsable())
4608 return nullptr;
4609
4610 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
4611 if (!NewStep.isUsable())
4612 return nullptr;
4613 // (Upper - Lower) / Step
4614 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
4615 if (!Diff.isUsable())
4616 return nullptr;
4617
4618 return Diff.get();
4619}
4620
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004621/// Iteration space of a single for loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004622struct LoopIterationSpace final {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004623 /// Condition of the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004624 Expr *PreCond = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004625 /// This expression calculates the number of iterations in the loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004626 /// It is always possible to calculate it before starting the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004627 Expr *NumIterations = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004628 /// The loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004629 Expr *CounterVar = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004630 /// Private loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004631 Expr *PrivateCounterVar = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004632 /// This is initializer for the initial value of #CounterVar.
Alexey Bataev8b427062016-05-25 12:36:08 +00004633 Expr *CounterInit = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004634 /// This is step for the #CounterVar used to generate its update:
Alexander Musmana5f070a2014-10-01 06:03:56 +00004635 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
Alexey Bataev8b427062016-05-25 12:36:08 +00004636 Expr *CounterStep = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004637 /// Should step be subtracted?
Alexey Bataev8b427062016-05-25 12:36:08 +00004638 bool Subtract = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004639 /// Source range of the loop init.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004640 SourceRange InitSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004641 /// Source range of the loop condition.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004642 SourceRange CondSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004643 /// Source range of the loop increment.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004644 SourceRange IncSrcRange;
4645};
4646
Alexey Bataev23b69422014-06-18 07:08:49 +00004647} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004648
Alexey Bataev9c821032015-04-30 04:23:23 +00004649void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4650 assert(getLangOpts().OpenMP && "OpenMP is not active.");
4651 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004652 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4653 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00004654 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4655 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00004656 if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) {
4657 if (ValueDecl *D = ISC.getLoopDecl()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004658 auto *VD = dyn_cast<VarDecl>(D);
4659 if (!VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004660 if (VarDecl *Private = isOpenMPCapturedDecl(D)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004661 VD = Private;
Alexey Bataeve3727102018-04-18 15:57:46 +00004662 } else {
4663 DeclRefExpr *Ref = buildCapture(*this, D, ISC.getLoopDeclRefExpr(),
4664 /*WithInit=*/false);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004665 VD = cast<VarDecl>(Ref->getDecl());
4666 }
4667 }
4668 DSAStack->addLoopControlVariable(D, VD);
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00004669 const Decl *LD = DSAStack->getPossiblyLoopCunter();
4670 if (LD != D->getCanonicalDecl()) {
4671 DSAStack->resetPossibleLoopCounter();
4672 if (auto *Var = dyn_cast_or_null<VarDecl>(LD))
4673 MarkDeclarationsReferencedInExpr(
4674 buildDeclRefExpr(*this, const_cast<VarDecl *>(Var),
4675 Var->getType().getNonLValueExprType(Context),
4676 ForLoc, /*RefersToCapture=*/true));
4677 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004678 }
4679 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004680 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00004681 }
4682}
4683
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004684/// Called on a for stmt to check and extract its iteration space
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004685/// for further processing (such as collapsing).
Alexey Bataeve3727102018-04-18 15:57:46 +00004686static bool checkOpenMPIterationSpace(
Alexey Bataev4acb8592014-07-07 13:01:15 +00004687 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4688 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataevf138fda2018-08-13 19:04:24 +00004689 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr,
4690 Expr *OrderedLoopCountExpr,
Alexey Bataeve3727102018-04-18 15:57:46 +00004691 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004692 LoopIterationSpace &ResultIterSpace,
Alexey Bataeve3727102018-04-18 15:57:46 +00004693 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004694 // OpenMP [2.6, Canonical Loop Form]
4695 // for (init-expr; test-expr; incr-expr) structured-block
David Majnemer9d168222016-08-05 17:44:54 +00004696 auto *For = dyn_cast_or_null<ForStmt>(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004697 if (!For) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004698 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004699 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
Alexey Bataevf138fda2018-08-13 19:04:24 +00004700 << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount
Alexey Bataev10e775f2015-07-30 11:36:16 +00004701 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
Alexey Bataevf138fda2018-08-13 19:04:24 +00004702 if (TotalNestedLoopCount > 1) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00004703 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4704 SemaRef.Diag(DSA.getConstructLoc(),
4705 diag::note_omp_collapse_ordered_expr)
4706 << 2 << CollapseLoopCountExpr->getSourceRange()
4707 << OrderedLoopCountExpr->getSourceRange();
4708 else if (CollapseLoopCountExpr)
4709 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4710 diag::note_omp_collapse_ordered_expr)
4711 << 0 << CollapseLoopCountExpr->getSourceRange();
4712 else
4713 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4714 diag::note_omp_collapse_ordered_expr)
4715 << 1 << OrderedLoopCountExpr->getSourceRange();
4716 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004717 return true;
4718 }
4719 assert(For->getBody());
4720
4721 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4722
4723 // Check init.
Alexey Bataeve3727102018-04-18 15:57:46 +00004724 Stmt *Init = For->getInit();
4725 if (ISC.checkAndSetInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004726 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004727
4728 bool HasErrors = false;
4729
4730 // Check loop variable's type.
Alexey Bataeve3727102018-04-18 15:57:46 +00004731 if (ValueDecl *LCDecl = ISC.getLoopDecl()) {
4732 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004733
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004734 // OpenMP [2.6, Canonical Loop Form]
4735 // Var is one of the following:
4736 // A variable of signed or unsigned integer type.
4737 // For C++, a variable of a random access iterator type.
4738 // For C, a variable of a pointer type.
Alexey Bataeve3727102018-04-18 15:57:46 +00004739 QualType VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004740 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4741 !VarType->isPointerType() &&
4742 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004743 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004744 << SemaRef.getLangOpts().CPlusPlus;
4745 HasErrors = true;
4746 }
4747
4748 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4749 // a Construct
4750 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4751 // parallel for construct is (are) private.
4752 // The loop iteration variable in the associated for-loop of a simd
4753 // construct with just one associated for-loop is linear with a
4754 // constant-linear-step that is the increment of the associated for-loop.
4755 // Exclude loop var from the list of variables with implicitly defined data
4756 // sharing attributes.
4757 VarsWithImplicitDSA.erase(LCDecl);
4758
4759 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4760 // in a Construct, C/C++].
4761 // The loop iteration variable in the associated for-loop of a simd
4762 // construct with just one associated for-loop may be listed in a linear
4763 // clause with a constant-linear-step that is the increment of the
4764 // associated for-loop.
4765 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4766 // parallel for construct may be listed in a private or lastprivate clause.
4767 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4768 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4769 // declared in the loop and it is predetermined as a private.
Alexey Bataeve3727102018-04-18 15:57:46 +00004770 OpenMPClauseKind PredeterminedCKind =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004771 isOpenMPSimdDirective(DKind)
4772 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4773 : OMPC_private;
4774 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4775 DVar.CKind != PredeterminedCKind) ||
4776 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4777 isOpenMPDistributeDirective(DKind)) &&
4778 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4779 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4780 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004781 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004782 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4783 << getOpenMPClauseName(PredeterminedCKind);
4784 if (DVar.RefExpr == nullptr)
4785 DVar.CKind = PredeterminedCKind;
Alexey Bataeve3727102018-04-18 15:57:46 +00004786 reportOriginalDsa(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004787 HasErrors = true;
4788 } else if (LoopDeclRefExpr != nullptr) {
4789 // Make the loop iteration variable private (for worksharing constructs),
4790 // linear (for simd directives with the only one associated loop) or
4791 // lastprivate (for simd directives with several collapsed or ordered
4792 // loops).
4793 if (DVar.CKind == OMPC_unknown)
Alexey Bataev7ace49d2016-05-17 08:55:33 +00004794 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
4795 [](OpenMPDirectiveKind) -> bool { return true; },
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004796 /*FromParent=*/false);
4797 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4798 }
4799
4800 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4801
4802 // Check test-expr.
Alexey Bataeve3727102018-04-18 15:57:46 +00004803 HasErrors |= ISC.checkAndSetCond(For->getCond());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004804
4805 // Check incr-expr.
Alexey Bataeve3727102018-04-18 15:57:46 +00004806 HasErrors |= ISC.checkAndSetInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004807 }
4808
Alexey Bataeve3727102018-04-18 15:57:46 +00004809 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004810 return HasErrors;
4811
Alexander Musmana5f070a2014-10-01 06:03:56 +00004812 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004813 ResultIterSpace.PreCond =
Alexey Bataeve3727102018-04-18 15:57:46 +00004814 ISC.buildPreCond(DSA.getCurScope(), For->getCond(), Captures);
4815 ResultIterSpace.NumIterations = ISC.buildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004816 DSA.getCurScope(),
4817 (isOpenMPWorksharingDirective(DKind) ||
4818 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4819 Captures);
Alexey Bataeve3727102018-04-18 15:57:46 +00004820 ResultIterSpace.CounterVar = ISC.buildCounterVar(Captures, DSA);
4821 ResultIterSpace.PrivateCounterVar = ISC.buildPrivateCounterVar();
4822 ResultIterSpace.CounterInit = ISC.buildCounterInit();
4823 ResultIterSpace.CounterStep = ISC.buildCounterStep();
4824 ResultIterSpace.InitSrcRange = ISC.getInitSrcRange();
4825 ResultIterSpace.CondSrcRange = ISC.getConditionSrcRange();
4826 ResultIterSpace.IncSrcRange = ISC.getIncrementSrcRange();
4827 ResultIterSpace.Subtract = ISC.shouldSubtractStep();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004828
Alexey Bataev62dbb972015-04-22 11:59:37 +00004829 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4830 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004831 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004832 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004833 ResultIterSpace.CounterInit == nullptr ||
4834 ResultIterSpace.CounterStep == nullptr);
Alexey Bataevf138fda2018-08-13 19:04:24 +00004835 if (!HasErrors && DSA.isOrderedRegion()) {
4836 if (DSA.getOrderedRegionParam().second->getNumForLoops()) {
4837 if (CurrentNestedLoopCount <
4838 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) {
4839 DSA.getOrderedRegionParam().second->setLoopNumIterations(
4840 CurrentNestedLoopCount, ResultIterSpace.NumIterations);
4841 DSA.getOrderedRegionParam().second->setLoopCounter(
4842 CurrentNestedLoopCount, ResultIterSpace.CounterVar);
4843 }
4844 }
4845 for (auto &Pair : DSA.getDoacrossDependClauses()) {
4846 if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) {
4847 // Erroneous case - clause has some problems.
4848 continue;
4849 }
4850 if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink &&
4851 Pair.second.size() <= CurrentNestedLoopCount) {
4852 // Erroneous case - clause has some problems.
4853 Pair.first->setLoopData(CurrentNestedLoopCount, nullptr);
4854 continue;
4855 }
4856 Expr *CntValue;
4857 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
4858 CntValue = ISC.buildOrderedLoopData(
4859 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures,
4860 Pair.first->getDependencyLoc());
4861 else
4862 CntValue = ISC.buildOrderedLoopData(
4863 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures,
4864 Pair.first->getDependencyLoc(),
4865 Pair.second[CurrentNestedLoopCount].first,
4866 Pair.second[CurrentNestedLoopCount].second);
4867 Pair.first->setLoopData(CurrentNestedLoopCount, CntValue);
4868 }
4869 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004870
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004871 return HasErrors;
4872}
4873
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004874/// Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004875static ExprResult
Alexey Bataeve3727102018-04-18 15:57:46 +00004876buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004877 ExprResult Start,
Alexey Bataeve3727102018-04-18 15:57:46 +00004878 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004879 // Build 'VarRef = Start.
Alexey Bataeve3727102018-04-18 15:57:46 +00004880 ExprResult NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004881 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004882 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004883 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00004884 VarRef.get()->getType())) {
4885 NewStart = SemaRef.PerformImplicitConversion(
4886 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4887 /*AllowExplicit=*/true);
4888 if (!NewStart.isUsable())
4889 return ExprError();
4890 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004891
Alexey Bataeve3727102018-04-18 15:57:46 +00004892 ExprResult Init =
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004893 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4894 return Init;
4895}
4896
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004897/// Build 'VarRef = Start + Iter * Step'.
Alexey Bataeve3727102018-04-18 15:57:46 +00004898static ExprResult buildCounterUpdate(
4899 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4900 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract,
4901 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004902 // Add parentheses (for debugging purposes only).
4903 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4904 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4905 !Step.isUsable())
4906 return ExprError();
4907
Alexey Bataev5a3af132016-03-29 08:58:54 +00004908 ExprResult NewStep = Step;
4909 if (Captures)
4910 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004911 if (NewStep.isInvalid())
4912 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004913 ExprResult Update =
4914 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004915 if (!Update.isUsable())
4916 return ExprError();
4917
Alexey Bataevc0214e02016-02-16 12:13:49 +00004918 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4919 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004920 ExprResult NewStart = Start;
4921 if (Captures)
4922 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004923 if (NewStart.isInvalid())
4924 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004925
Alexey Bataevc0214e02016-02-16 12:13:49 +00004926 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4927 ExprResult SavedUpdate = Update;
4928 ExprResult UpdateVal;
4929 if (VarRef.get()->getType()->isOverloadableType() ||
4930 NewStart.get()->getType()->isOverloadableType() ||
4931 Update.get()->getType()->isOverloadableType()) {
4932 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4933 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4934 Update =
4935 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4936 if (Update.isUsable()) {
4937 UpdateVal =
4938 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4939 VarRef.get(), SavedUpdate.get());
4940 if (UpdateVal.isUsable()) {
4941 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4942 UpdateVal.get());
4943 }
4944 }
4945 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4946 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004947
Alexey Bataevc0214e02016-02-16 12:13:49 +00004948 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4949 if (!Update.isUsable() || !UpdateVal.isUsable()) {
4950 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4951 NewStart.get(), SavedUpdate.get());
4952 if (!Update.isUsable())
4953 return ExprError();
4954
Alexey Bataev11481f52016-02-17 10:29:05 +00004955 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4956 VarRef.get()->getType())) {
4957 Update = SemaRef.PerformImplicitConversion(
4958 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4959 if (!Update.isUsable())
4960 return ExprError();
4961 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00004962
4963 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4964 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004965 return Update;
4966}
4967
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004968/// Convert integer expression \a E to make it have at least \a Bits
Alexander Musmana5f070a2014-10-01 06:03:56 +00004969/// bits.
Alexey Bataeve3727102018-04-18 15:57:46 +00004970static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004971 if (E == nullptr)
4972 return ExprError();
Alexey Bataeve3727102018-04-18 15:57:46 +00004973 ASTContext &C = SemaRef.Context;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004974 QualType OldType = E->getType();
4975 unsigned HasBits = C.getTypeSize(OldType);
4976 if (HasBits >= Bits)
4977 return ExprResult(E);
4978 // OK to convert to signed, because new type has more bits than old.
4979 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4980 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4981 true);
4982}
4983
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004984/// Check if the given expression \a E is a constant integer that fits
Alexander Musmana5f070a2014-10-01 06:03:56 +00004985/// into \a Bits bits.
Alexey Bataeve3727102018-04-18 15:57:46 +00004986static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004987 if (E == nullptr)
4988 return false;
4989 llvm::APSInt Result;
4990 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4991 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4992 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004993}
4994
Alexey Bataev5a3af132016-03-29 08:58:54 +00004995/// Build preinits statement for the given declarations.
4996static Stmt *buildPreInits(ASTContext &Context,
Alexey Bataevc5514062017-10-25 15:44:52 +00004997 MutableArrayRef<Decl *> PreInits) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004998 if (!PreInits.empty()) {
4999 return new (Context) DeclStmt(
5000 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
5001 SourceLocation(), SourceLocation());
5002 }
5003 return nullptr;
5004}
5005
5006/// Build preinits statement for the given declarations.
Alexey Bataevc5514062017-10-25 15:44:52 +00005007static Stmt *
5008buildPreInits(ASTContext &Context,
Alexey Bataeve3727102018-04-18 15:57:46 +00005009 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005010 if (!Captures.empty()) {
5011 SmallVector<Decl *, 16> PreInits;
Alexey Bataeve3727102018-04-18 15:57:46 +00005012 for (const auto &Pair : Captures)
Alexey Bataev5a3af132016-03-29 08:58:54 +00005013 PreInits.push_back(Pair.second->getDecl());
5014 return buildPreInits(Context, PreInits);
5015 }
5016 return nullptr;
5017}
5018
5019/// Build postupdate expression for the given list of postupdates expressions.
5020static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
5021 Expr *PostUpdate = nullptr;
5022 if (!PostUpdates.empty()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005023 for (Expr *E : PostUpdates) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005024 Expr *ConvE = S.BuildCStyleCastExpr(
5025 E->getExprLoc(),
5026 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
5027 E->getExprLoc(), E)
5028 .get();
5029 PostUpdate = PostUpdate
5030 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
5031 PostUpdate, ConvE)
5032 .get()
5033 : ConvE;
5034 }
5035 }
5036 return PostUpdate;
5037}
5038
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005039/// Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00005040/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
5041/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005042static unsigned
Alexey Bataeve3727102018-04-18 15:57:46 +00005043checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
Alexey Bataev10e775f2015-07-30 11:36:16 +00005044 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
5045 DSAStackTy &DSA,
Alexey Bataeve3727102018-04-18 15:57:46 +00005046 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00005047 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005048 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005049 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005050 // Found 'collapse' clause - calculate collapse number.
Fangrui Songf5d33352018-11-30 21:26:09 +00005051 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005052 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Fangrui Songf5d33352018-11-30 21:26:09 +00005053 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005054 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00005055 unsigned OrderedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005056 if (OrderedLoopCountExpr) {
5057 // Found 'ordered' clause - calculate collapse number.
Fangrui Songf5d33352018-11-30 21:26:09 +00005058 llvm::APSInt Result;
5059 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005060 if (Result.getLimitedValue() < NestedLoopCount) {
5061 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
5062 diag::err_omp_wrong_ordered_loop_count)
5063 << OrderedLoopCountExpr->getSourceRange();
5064 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
5065 diag::note_collapse_loop_count)
5066 << CollapseLoopCountExpr->getSourceRange();
5067 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00005068 OrderedLoopCount = Result.getLimitedValue();
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005069 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005070 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005071 // This is helper routine for loop directives (e.g., 'for', 'simd',
5072 // 'for simd', etc.).
Alexey Bataeve3727102018-04-18 15:57:46 +00005073 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005074 SmallVector<LoopIterationSpace, 4> IterSpaces;
Alexey Bataevf138fda2018-08-13 19:04:24 +00005075 IterSpaces.resize(std::max(OrderedLoopCount, NestedLoopCount));
Alexander Musmana5f070a2014-10-01 06:03:56 +00005076 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005077 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataevf138fda2018-08-13 19:04:24 +00005078 if (checkOpenMPIterationSpace(
5079 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
5080 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
5081 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt],
5082 Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00005083 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005084 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005085 // OpenMP [2.8.1, simd construct, Restrictions]
5086 // All loops associated with the construct must be perfectly nested; that
5087 // is, there must be no intervening code nor any OpenMP directive between
5088 // any two loops.
5089 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005090 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00005091 for (unsigned Cnt = NestedLoopCount; Cnt < OrderedLoopCount; ++Cnt) {
5092 if (checkOpenMPIterationSpace(
5093 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
5094 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
5095 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt],
5096 Captures))
5097 return 0;
5098 if (Cnt > 0 && IterSpaces[Cnt].CounterVar) {
5099 // Handle initialization of captured loop iterator variables.
5100 auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar);
5101 if (isa<OMPCapturedExprDecl>(DRE->getDecl())) {
5102 Captures[DRE] = DRE;
5103 }
5104 }
5105 // Move on to the next nested for loop, or to the loop body.
5106 // OpenMP [2.8.1, simd construct, Restrictions]
5107 // All loops associated with the construct must be perfectly nested; that
5108 // is, there must be no intervening code nor any OpenMP directive between
5109 // any two loops.
5110 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
5111 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005112
Alexander Musmana5f070a2014-10-01 06:03:56 +00005113 Built.clear(/* size */ NestedLoopCount);
5114
5115 if (SemaRef.CurContext->isDependentContext())
5116 return NestedLoopCount;
5117
5118 // An example of what is generated for the following code:
5119 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00005120 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00005121 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00005122 // for (k = 0; k < NK; ++k)
5123 // for (j = J0; j < NJ; j+=2) {
5124 // <loop body>
5125 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005126 //
5127 // We generate the code below.
5128 // Note: the loop body may be outlined in CodeGen.
5129 // Note: some counters may be C++ classes, operator- is used to find number of
5130 // iterations and operator+= to calculate counter value.
5131 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
5132 // or i64 is currently supported).
5133 //
5134 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
5135 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
5136 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
5137 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
5138 // // similar updates for vars in clauses (e.g. 'linear')
5139 // <loop body (using local i and j)>
5140 // }
5141 // i = NI; // assign final values of counters
5142 // j = NJ;
5143 //
5144
5145 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
5146 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00005147 // Precondition tests if there is at least one iteration (all conditions are
5148 // true).
5149 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexey Bataeve3727102018-04-18 15:57:46 +00005150 Expr *N0 = IterSpaces[0].NumIterations;
5151 ExprResult LastIteration32 =
5152 widenIterationCount(/*Bits=*/32,
5153 SemaRef
5154 .PerformImplicitConversion(
5155 N0->IgnoreImpCasts(), N0->getType(),
5156 Sema::AA_Converting, /*AllowExplicit=*/true)
5157 .get(),
5158 SemaRef);
5159 ExprResult LastIteration64 = widenIterationCount(
5160 /*Bits=*/64,
5161 SemaRef
5162 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(),
5163 Sema::AA_Converting,
5164 /*AllowExplicit=*/true)
5165 .get(),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005166 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005167
5168 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
5169 return NestedLoopCount;
5170
Alexey Bataeve3727102018-04-18 15:57:46 +00005171 ASTContext &C = SemaRef.Context;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005172 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
5173
5174 Scope *CurScope = DSA.getCurScope();
5175 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00005176 if (PreCond.isUsable()) {
Alexey Bataeva7206b92016-12-20 16:51:02 +00005177 PreCond =
5178 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd,
5179 PreCond.get(), IterSpaces[Cnt].PreCond);
Alexey Bataev62dbb972015-04-22 11:59:37 +00005180 }
Alexey Bataeve3727102018-04-18 15:57:46 +00005181 Expr *N = IterSpaces[Cnt].NumIterations;
Alexey Bataeva7206b92016-12-20 16:51:02 +00005182 SourceLocation Loc = N->getExprLoc();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005183 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
5184 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005185 LastIteration32 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005186 CurScope, Loc, BO_Mul, LastIteration32.get(),
David Majnemer9d168222016-08-05 17:44:54 +00005187 SemaRef
5188 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5189 Sema::AA_Converting,
5190 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005191 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005192 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005193 LastIteration64 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005194 CurScope, Loc, BO_Mul, LastIteration64.get(),
David Majnemer9d168222016-08-05 17:44:54 +00005195 SemaRef
5196 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5197 Sema::AA_Converting,
5198 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005199 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005200 }
5201
5202 // Choose either the 32-bit or 64-bit version.
5203 ExprResult LastIteration = LastIteration64;
5204 if (LastIteration32.isUsable() &&
5205 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
5206 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
Alexey Bataeve3727102018-04-18 15:57:46 +00005207 fitsInto(
5208 /*Bits=*/32,
Alexander Musmana5f070a2014-10-01 06:03:56 +00005209 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
5210 LastIteration64.get(), SemaRef)))
5211 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00005212 QualType VType = LastIteration.get()->getType();
5213 QualType RealVType = VType;
5214 QualType StrideVType = VType;
5215 if (isOpenMPTaskLoopDirective(DKind)) {
5216 VType =
5217 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
5218 StrideVType =
5219 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
5220 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005221
5222 if (!LastIteration.isUsable())
5223 return 0;
5224
5225 // Save the number of iterations.
5226 ExprResult NumIterations = LastIteration;
5227 {
5228 LastIteration = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005229 CurScope, LastIteration.get()->getExprLoc(), BO_Sub,
5230 LastIteration.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00005231 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5232 if (!LastIteration.isUsable())
5233 return 0;
5234 }
5235
5236 // Calculate the last iteration number beforehand instead of doing this on
5237 // each iteration. Do not do this if the number of iterations may be kfold-ed.
5238 llvm::APSInt Result;
5239 bool IsConstant =
5240 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
5241 ExprResult CalcLastIteration;
5242 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005243 ExprResult SaveRef =
5244 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005245 LastIteration = SaveRef;
5246
5247 // Prepare SaveRef + 1.
5248 NumIterations = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005249 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00005250 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5251 if (!NumIterations.isUsable())
5252 return 0;
5253 }
5254
5255 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
5256
David Majnemer9d168222016-08-05 17:44:54 +00005257 // Build variables passed into runtime, necessary for worksharing directives.
Carlo Bertolliffafe102017-04-20 00:39:39 +00005258 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005259 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5260 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005261 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005262 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
5263 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005264 SemaRef.AddInitializerToDecl(LBDecl,
5265 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5266 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005267
5268 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005269 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
5270 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005271 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
Richard Smith3beb7c62017-01-12 02:27:38 +00005272 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005273
5274 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
5275 // This will be used to implement clause 'lastprivate'.
5276 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005277 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
5278 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005279 SemaRef.AddInitializerToDecl(ILDecl,
5280 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5281 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005282
5283 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00005284 VarDecl *STDecl =
5285 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
5286 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005287 SemaRef.AddInitializerToDecl(STDecl,
5288 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
5289 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005290
5291 // Build expression: UB = min(UB, LastIteration)
David Majnemer9d168222016-08-05 17:44:54 +00005292 // It is necessary for CodeGen of directives with static scheduling.
Alexander Musmanc6388682014-12-15 07:07:06 +00005293 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
5294 UB.get(), LastIteration.get());
5295 ExprResult CondOp = SemaRef.ActOnConditionalOp(
Alexey Bataev86ec3fe2018-07-25 14:40:26 +00005296 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(),
5297 LastIteration.get(), UB.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00005298 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
5299 CondOp.get());
5300 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
Carlo Bertolli9925f152016-06-27 14:55:37 +00005301
5302 // If we have a combined directive that combines 'distribute', 'for' or
5303 // 'simd' we need to be able to access the bounds of the schedule of the
5304 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
5305 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
5306 if (isOpenMPLoopBoundSharingDirective(DKind)) {
Carlo Bertolliffafe102017-04-20 00:39:39 +00005307 // Lower bound variable, initialized with zero.
5308 VarDecl *CombLBDecl =
5309 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb");
5310 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc);
5311 SemaRef.AddInitializerToDecl(
5312 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5313 /*DirectInit*/ false);
5314
5315 // Upper bound variable, initialized with last iteration number.
5316 VarDecl *CombUBDecl =
5317 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub");
5318 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc);
5319 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(),
5320 /*DirectInit*/ false);
5321
5322 ExprResult CombIsUBGreater = SemaRef.BuildBinOp(
5323 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get());
5324 ExprResult CombCondOp =
5325 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(),
5326 LastIteration.get(), CombUB.get());
5327 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(),
5328 CombCondOp.get());
5329 CombEUB = SemaRef.ActOnFinishFullExpr(CombEUB.get());
5330
Alexey Bataeve3727102018-04-18 15:57:46 +00005331 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005332 // We expect to have at least 2 more parameters than the 'parallel'
5333 // directive does - the lower and upper bounds of the previous schedule.
5334 assert(CD->getNumParams() >= 4 &&
5335 "Unexpected number of parameters in loop combined directive");
5336
5337 // Set the proper type for the bounds given what we learned from the
5338 // enclosed loops.
Alexey Bataeve3727102018-04-18 15:57:46 +00005339 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
5340 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
Carlo Bertolli9925f152016-06-27 14:55:37 +00005341
5342 // Previous lower and upper bounds are obtained from the region
5343 // parameters.
5344 PrevLB =
5345 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
5346 PrevUB =
5347 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
5348 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005349 }
5350
5351 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005352 ExprResult IV;
Carlo Bertolliffafe102017-04-20 00:39:39 +00005353 ExprResult Init, CombInit;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005354 {
Alexey Bataev7292c292016-04-25 12:22:29 +00005355 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
5356 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
David Majnemer9d168222016-08-05 17:44:54 +00005357 Expr *RHS =
5358 (isOpenMPWorksharingDirective(DKind) ||
5359 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
5360 ? LB.get()
5361 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005362 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
5363 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Carlo Bertolliffafe102017-04-20 00:39:39 +00005364
5365 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5366 Expr *CombRHS =
5367 (isOpenMPWorksharingDirective(DKind) ||
5368 isOpenMPTaskLoopDirective(DKind) ||
5369 isOpenMPDistributeDirective(DKind))
5370 ? CombLB.get()
5371 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
5372 CombInit =
5373 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS);
5374 CombInit = SemaRef.ActOnFinishFullExpr(CombInit.get());
5375 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005376 }
5377
Alexander Musmanc6388682014-12-15 07:07:06 +00005378 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005379 SourceLocation CondLoc = AStmt->getBeginLoc();
Alexander Musmanc6388682014-12-15 07:07:06 +00005380 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005381 (isOpenMPWorksharingDirective(DKind) ||
5382 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00005383 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
5384 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
5385 NumIterations.get());
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005386 ExprResult CombDistCond;
5387 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5388 CombDistCond =
Gheorghe-Teodor Bercea9d6341f2018-10-29 19:44:25 +00005389 SemaRef.BuildBinOp(
5390 CurScope, CondLoc, BO_LT, IV.get(), NumIterations.get());
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005391 }
5392
Carlo Bertolliffafe102017-04-20 00:39:39 +00005393 ExprResult CombCond;
5394 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5395 CombCond =
5396 SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), CombUB.get());
5397 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005398 // Loop increment (IV = IV + 1)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005399 SourceLocation IncLoc = AStmt->getBeginLoc();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005400 ExprResult Inc =
5401 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
5402 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
5403 if (!Inc.isUsable())
5404 return 0;
5405 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00005406 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
5407 if (!Inc.isUsable())
5408 return 0;
5409
5410 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
5411 // Used for directives with static scheduling.
Carlo Bertolliffafe102017-04-20 00:39:39 +00005412 // In combined construct, add combined version that use CombLB and CombUB
5413 // base variables for the update
5414 ExprResult NextLB, NextUB, CombNextLB, CombNextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005415 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5416 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005417 // LB + ST
5418 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
5419 if (!NextLB.isUsable())
5420 return 0;
5421 // LB = LB + ST
5422 NextLB =
5423 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
5424 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
5425 if (!NextLB.isUsable())
5426 return 0;
5427 // UB + ST
5428 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
5429 if (!NextUB.isUsable())
5430 return 0;
5431 // UB = UB + ST
5432 NextUB =
5433 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
5434 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
5435 if (!NextUB.isUsable())
5436 return 0;
Carlo Bertolliffafe102017-04-20 00:39:39 +00005437 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5438 CombNextLB =
5439 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get());
5440 if (!NextLB.isUsable())
5441 return 0;
5442 // LB = LB + ST
5443 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(),
5444 CombNextLB.get());
5445 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get());
5446 if (!CombNextLB.isUsable())
5447 return 0;
5448 // UB + ST
5449 CombNextUB =
5450 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get());
5451 if (!CombNextUB.isUsable())
5452 return 0;
5453 // UB = UB + ST
5454 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(),
5455 CombNextUB.get());
5456 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get());
5457 if (!CombNextUB.isUsable())
5458 return 0;
5459 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005460 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005461
Carlo Bertolliffafe102017-04-20 00:39:39 +00005462 // Create increment expression for distribute loop when combined in a same
Carlo Bertolli8429d812017-02-17 21:29:13 +00005463 // directive with for as IV = IV + ST; ensure upper bound expression based
5464 // on PrevUB instead of NumIterations - used to implement 'for' when found
5465 // in combination with 'distribute', like in 'distribute parallel for'
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005466 SourceLocation DistIncLoc = AStmt->getBeginLoc();
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005467 ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond;
Carlo Bertolli8429d812017-02-17 21:29:13 +00005468 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5469 DistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get());
5470 assert(DistCond.isUsable() && "distribute cond expr was not built");
5471
5472 DistInc =
5473 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get());
5474 assert(DistInc.isUsable() && "distribute inc expr was not built");
5475 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(),
5476 DistInc.get());
5477 DistInc = SemaRef.ActOnFinishFullExpr(DistInc.get());
5478 assert(DistInc.isUsable() && "distribute inc expr was not built");
5479
5480 // Build expression: UB = min(UB, prevUB) for #for in composite or combined
5481 // construct
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005482 SourceLocation DistEUBLoc = AStmt->getBeginLoc();
Carlo Bertolli8429d812017-02-17 21:29:13 +00005483 ExprResult IsUBGreater =
5484 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get());
5485 ExprResult CondOp = SemaRef.ActOnConditionalOp(
5486 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get());
5487 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(),
5488 CondOp.get());
5489 PrevEUB = SemaRef.ActOnFinishFullExpr(PrevEUB.get());
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005490
5491 // Build IV <= PrevUB to be used in parallel for is in combination with
5492 // a distribute directive with schedule(static, 1)
5493 ParForInDistCond =
5494 SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), PrevUB.get());
Carlo Bertolli8429d812017-02-17 21:29:13 +00005495 }
5496
Alexander Musmana5f070a2014-10-01 06:03:56 +00005497 // Build updates and final values of the loop counters.
5498 bool HasErrors = false;
5499 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005500 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005501 Built.Updates.resize(NestedLoopCount);
5502 Built.Finals.resize(NestedLoopCount);
5503 {
5504 ExprResult Div;
5505 // Go from inner nested loop to outer.
5506 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5507 LoopIterationSpace &IS = IterSpaces[Cnt];
5508 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
5509 // Build: Iter = (IV / Div) % IS.NumIters
5510 // where Div is product of previous iterations' IS.NumIters.
5511 ExprResult Iter;
5512 if (Div.isUsable()) {
5513 Iter =
5514 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
5515 } else {
5516 Iter = IV;
5517 assert((Cnt == (int)NestedLoopCount - 1) &&
5518 "unusable div expected on first iteration only");
5519 }
5520
5521 if (Cnt != 0 && Iter.isUsable())
5522 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
5523 IS.NumIterations);
5524 if (!Iter.isUsable()) {
5525 HasErrors = true;
5526 break;
5527 }
5528
Alexey Bataev39f915b82015-05-08 10:41:21 +00005529 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005530 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00005531 DeclRefExpr *CounterVar = buildDeclRefExpr(
5532 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
5533 /*RefersToCapture=*/true);
5534 ExprResult Init = buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005535 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005536 if (!Init.isUsable()) {
5537 HasErrors = true;
5538 break;
5539 }
Alexey Bataeve3727102018-04-18 15:57:46 +00005540 ExprResult Update = buildCounterUpdate(
Alexey Bataev5a3af132016-03-29 08:58:54 +00005541 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
5542 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005543 if (!Update.isUsable()) {
5544 HasErrors = true;
5545 break;
5546 }
5547
5548 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
Alexey Bataeve3727102018-04-18 15:57:46 +00005549 ExprResult Final = buildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00005550 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005551 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005552 if (!Final.isUsable()) {
5553 HasErrors = true;
5554 break;
5555 }
5556
5557 // Build Div for the next iteration: Div <- Div * IS.NumIters
5558 if (Cnt != 0) {
5559 if (Div.isUnset())
5560 Div = IS.NumIterations;
5561 else
5562 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
5563 IS.NumIterations);
5564
5565 // Add parentheses (for debugging purposes only).
5566 if (Div.isUsable())
Alexey Bataev8b427062016-05-25 12:36:08 +00005567 Div = tryBuildCapture(SemaRef, Div.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005568 if (!Div.isUsable()) {
5569 HasErrors = true;
5570 break;
5571 }
5572 }
5573 if (!Update.isUsable() || !Final.isUsable()) {
5574 HasErrors = true;
5575 break;
5576 }
5577 // Save results
5578 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00005579 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005580 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005581 Built.Updates[Cnt] = Update.get();
5582 Built.Finals[Cnt] = Final.get();
5583 }
5584 }
5585
5586 if (HasErrors)
5587 return 0;
5588
5589 // Save results
5590 Built.IterationVarRef = IV.get();
5591 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00005592 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00005593 Built.CalcLastIteration =
5594 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005595 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00005596 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005597 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005598 Built.Init = Init.get();
5599 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005600 Built.LB = LB.get();
5601 Built.UB = UB.get();
5602 Built.IL = IL.get();
5603 Built.ST = ST.get();
5604 Built.EUB = EUB.get();
5605 Built.NLB = NextLB.get();
5606 Built.NUB = NextUB.get();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005607 Built.PrevLB = PrevLB.get();
5608 Built.PrevUB = PrevUB.get();
Carlo Bertolli8429d812017-02-17 21:29:13 +00005609 Built.DistInc = DistInc.get();
5610 Built.PrevEUB = PrevEUB.get();
Carlo Bertolliffafe102017-04-20 00:39:39 +00005611 Built.DistCombinedFields.LB = CombLB.get();
5612 Built.DistCombinedFields.UB = CombUB.get();
5613 Built.DistCombinedFields.EUB = CombEUB.get();
5614 Built.DistCombinedFields.Init = CombInit.get();
5615 Built.DistCombinedFields.Cond = CombCond.get();
5616 Built.DistCombinedFields.NLB = CombNextLB.get();
5617 Built.DistCombinedFields.NUB = CombNextUB.get();
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005618 Built.DistCombinedFields.DistCond = CombDistCond.get();
5619 Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005620
Alexey Bataevabfc0692014-06-25 06:52:00 +00005621 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005622}
5623
Alexey Bataev10e775f2015-07-30 11:36:16 +00005624static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005625 auto CollapseClauses =
5626 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
5627 if (CollapseClauses.begin() != CollapseClauses.end())
5628 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005629 return nullptr;
5630}
5631
Alexey Bataev10e775f2015-07-30 11:36:16 +00005632static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005633 auto OrderedClauses =
5634 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
5635 if (OrderedClauses.begin() != OrderedClauses.end())
5636 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005637 return nullptr;
5638}
5639
Kelvin Lic5609492016-07-15 04:39:07 +00005640static bool checkSimdlenSafelenSpecified(Sema &S,
5641 const ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005642 const OMPSafelenClause *Safelen = nullptr;
5643 const OMPSimdlenClause *Simdlen = nullptr;
Kelvin Lic5609492016-07-15 04:39:07 +00005644
Alexey Bataeve3727102018-04-18 15:57:46 +00005645 for (const OMPClause *Clause : Clauses) {
Kelvin Lic5609492016-07-15 04:39:07 +00005646 if (Clause->getClauseKind() == OMPC_safelen)
5647 Safelen = cast<OMPSafelenClause>(Clause);
5648 else if (Clause->getClauseKind() == OMPC_simdlen)
5649 Simdlen = cast<OMPSimdlenClause>(Clause);
5650 if (Safelen && Simdlen)
5651 break;
5652 }
5653
5654 if (Simdlen && Safelen) {
Fangrui Songf5d33352018-11-30 21:26:09 +00005655 llvm::APSInt SimdlenRes, SafelenRes;
Alexey Bataeve3727102018-04-18 15:57:46 +00005656 const Expr *SimdlenLength = Simdlen->getSimdlen();
5657 const Expr *SafelenLength = Safelen->getSafelen();
Kelvin Lic5609492016-07-15 04:39:07 +00005658 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
5659 SimdlenLength->isInstantiationDependent() ||
5660 SimdlenLength->containsUnexpandedParameterPack())
5661 return false;
5662 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
5663 SafelenLength->isInstantiationDependent() ||
5664 SafelenLength->containsUnexpandedParameterPack())
5665 return false;
Fangrui Songf5d33352018-11-30 21:26:09 +00005666 SimdlenLength->EvaluateAsInt(SimdlenRes, S.Context);
5667 SafelenLength->EvaluateAsInt(SafelenRes, S.Context);
Kelvin Lic5609492016-07-15 04:39:07 +00005668 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
5669 // If both simdlen and safelen clauses are specified, the value of the
5670 // simdlen parameter must be less than or equal to the value of the safelen
5671 // parameter.
5672 if (SimdlenRes > SafelenRes) {
5673 S.Diag(SimdlenLength->getExprLoc(),
5674 diag::err_omp_wrong_simdlen_safelen_values)
5675 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
5676 return true;
5677 }
Alexey Bataev66b15b52015-08-21 11:14:16 +00005678 }
5679 return false;
5680}
5681
Alexey Bataeve3727102018-04-18 15:57:46 +00005682StmtResult
5683Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
5684 SourceLocation StartLoc, SourceLocation EndLoc,
5685 VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005686 if (!AStmt)
5687 return StmtError();
5688
5689 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005690 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005691 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5692 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00005693 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataev10e775f2015-07-30 11:36:16 +00005694 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5695 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005696 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005697 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005698
Alexander Musmana5f070a2014-10-01 06:03:56 +00005699 assert((CurContext->isDependentContext() || B.builtAll()) &&
5700 "omp simd loop exprs were not built");
5701
Alexander Musman3276a272015-03-21 10:12:56 +00005702 if (!CurContext->isDependentContext()) {
5703 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005704 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005705 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexander Musman3276a272015-03-21 10:12:56 +00005706 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005707 B.NumIterations, *this, CurScope,
5708 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00005709 return StmtError();
5710 }
5711 }
5712
Kelvin Lic5609492016-07-15 04:39:07 +00005713 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005714 return StmtError();
5715
Reid Kleckner87a31802018-03-12 21:43:02 +00005716 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005717 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5718 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005719}
5720
Alexey Bataeve3727102018-04-18 15:57:46 +00005721StmtResult
5722Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
5723 SourceLocation StartLoc, SourceLocation EndLoc,
5724 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.
Alexey Bataeve3727102018-04-18 15:57:46 +00005732 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataev10e775f2015-07-30 11:36:16 +00005733 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5734 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005735 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005736 return StmtError();
5737
Alexander Musmana5f070a2014-10-01 06:03:56 +00005738 assert((CurContext->isDependentContext() || B.builtAll()) &&
5739 "omp for loop exprs were not built");
5740
Alexey Bataev54acd402015-08-04 11:18:19 +00005741 if (!CurContext->isDependentContext()) {
5742 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005743 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005744 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00005745 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005746 B.NumIterations, *this, CurScope,
5747 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005748 return StmtError();
5749 }
5750 }
5751
Reid Kleckner87a31802018-03-12 21:43:02 +00005752 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005753 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005754 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005755}
5756
Alexander Musmanf82886e2014-09-18 05:12:34 +00005757StmtResult Sema::ActOnOpenMPForSimdDirective(
5758 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00005759 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005760 if (!AStmt)
5761 return StmtError();
5762
5763 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005764 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005765 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5766 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00005767 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00005768 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00005769 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5770 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005771 if (NestedLoopCount == 0)
5772 return StmtError();
5773
Alexander Musmanc6388682014-12-15 07:07:06 +00005774 assert((CurContext->isDependentContext() || B.builtAll()) &&
5775 "omp for simd loop exprs were not built");
5776
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005777 if (!CurContext->isDependentContext()) {
5778 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005779 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005780 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005781 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005782 B.NumIterations, *this, CurScope,
5783 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005784 return StmtError();
5785 }
5786 }
5787
Kelvin Lic5609492016-07-15 04:39:07 +00005788 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005789 return StmtError();
5790
Reid Kleckner87a31802018-03-12 21:43:02 +00005791 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005792 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5793 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005794}
5795
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005796StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5797 Stmt *AStmt,
5798 SourceLocation StartLoc,
5799 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005800 if (!AStmt)
5801 return StmtError();
5802
5803 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005804 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00005805 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005806 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00005807 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005808 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005809 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005810 return StmtError();
5811 // All associated statements must be '#pragma omp section' except for
5812 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005813 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005814 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5815 if (SectionStmt)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005816 Diag(SectionStmt->getBeginLoc(),
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005817 diag::err_omp_sections_substmt_not_section);
5818 return StmtError();
5819 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005820 cast<OMPSectionDirective>(SectionStmt)
5821 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005822 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005823 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005824 Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005825 return StmtError();
5826 }
5827
Reid Kleckner87a31802018-03-12 21:43:02 +00005828 setFunctionHasBranchProtectedScope();
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005829
Alexey Bataev25e5b442015-09-15 12:52:43 +00005830 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5831 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005832}
5833
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005834StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5835 SourceLocation StartLoc,
5836 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005837 if (!AStmt)
5838 return StmtError();
5839
5840 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005841
Reid Kleckner87a31802018-03-12 21:43:02 +00005842 setFunctionHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00005843 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005844
Alexey Bataev25e5b442015-09-15 12:52:43 +00005845 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5846 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005847}
5848
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005849StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5850 Stmt *AStmt,
5851 SourceLocation StartLoc,
5852 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005853 if (!AStmt)
5854 return StmtError();
5855
5856 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00005857
Reid Kleckner87a31802018-03-12 21:43:02 +00005858 setFunctionHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00005859
Alexey Bataev3255bf32015-01-19 05:20:46 +00005860 // OpenMP [2.7.3, single Construct, Restrictions]
5861 // The copyprivate clause must not be used with the nowait clause.
Alexey Bataeve3727102018-04-18 15:57:46 +00005862 const OMPClause *Nowait = nullptr;
5863 const OMPClause *Copyprivate = nullptr;
5864 for (const OMPClause *Clause : Clauses) {
Alexey Bataev3255bf32015-01-19 05:20:46 +00005865 if (Clause->getClauseKind() == OMPC_nowait)
5866 Nowait = Clause;
5867 else if (Clause->getClauseKind() == OMPC_copyprivate)
5868 Copyprivate = Clause;
5869 if (Copyprivate && Nowait) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005870 Diag(Copyprivate->getBeginLoc(),
Alexey Bataev3255bf32015-01-19 05:20:46 +00005871 diag::err_omp_single_copyprivate_with_nowait);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005872 Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here);
Alexey Bataev3255bf32015-01-19 05:20:46 +00005873 return StmtError();
5874 }
5875 }
5876
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005877 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5878}
5879
Alexander Musman80c22892014-07-17 08:54:58 +00005880StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5881 SourceLocation StartLoc,
5882 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005883 if (!AStmt)
5884 return StmtError();
5885
5886 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00005887
Reid Kleckner87a31802018-03-12 21:43:02 +00005888 setFunctionHasBranchProtectedScope();
Alexander Musman80c22892014-07-17 08:54:58 +00005889
5890 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5891}
5892
Alexey Bataev28c75412015-12-15 08:19:24 +00005893StmtResult Sema::ActOnOpenMPCriticalDirective(
5894 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5895 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005896 if (!AStmt)
5897 return StmtError();
5898
5899 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005900
Alexey Bataev28c75412015-12-15 08:19:24 +00005901 bool ErrorFound = false;
5902 llvm::APSInt Hint;
5903 SourceLocation HintLoc;
5904 bool DependentHint = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00005905 for (const OMPClause *C : Clauses) {
Alexey Bataev28c75412015-12-15 08:19:24 +00005906 if (C->getClauseKind() == OMPC_hint) {
5907 if (!DirName.getName()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005908 Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name);
Alexey Bataev28c75412015-12-15 08:19:24 +00005909 ErrorFound = true;
5910 }
5911 Expr *E = cast<OMPHintClause>(C)->getHint();
5912 if (E->isTypeDependent() || E->isValueDependent() ||
Alexey Bataeve3727102018-04-18 15:57:46 +00005913 E->isInstantiationDependent()) {
Alexey Bataev28c75412015-12-15 08:19:24 +00005914 DependentHint = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00005915 } else {
Alexey Bataev28c75412015-12-15 08:19:24 +00005916 Hint = E->EvaluateKnownConstInt(Context);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005917 HintLoc = C->getBeginLoc();
Alexey Bataev28c75412015-12-15 08:19:24 +00005918 }
5919 }
5920 }
5921 if (ErrorFound)
5922 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00005923 const auto Pair = DSAStack->getCriticalWithHint(DirName);
Alexey Bataev28c75412015-12-15 08:19:24 +00005924 if (Pair.first && DirName.getName() && !DependentHint) {
5925 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5926 Diag(StartLoc, diag::err_omp_critical_with_hint);
Alexey Bataeve3727102018-04-18 15:57:46 +00005927 if (HintLoc.isValid())
Alexey Bataev28c75412015-12-15 08:19:24 +00005928 Diag(HintLoc, diag::note_omp_critical_hint_here)
5929 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +00005930 else
Alexey Bataev28c75412015-12-15 08:19:24 +00005931 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
Alexey Bataeve3727102018-04-18 15:57:46 +00005932 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005933 Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here)
Alexey Bataev28c75412015-12-15 08:19:24 +00005934 << 1
5935 << C->getHint()->EvaluateKnownConstInt(Context).toString(
5936 /*Radix=*/10, /*Signed=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +00005937 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005938 Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1;
Alexey Bataeve3727102018-04-18 15:57:46 +00005939 }
Alexey Bataev28c75412015-12-15 08:19:24 +00005940 }
5941 }
5942
Reid Kleckner87a31802018-03-12 21:43:02 +00005943 setFunctionHasBranchProtectedScope();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005944
Alexey Bataev28c75412015-12-15 08:19:24 +00005945 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5946 Clauses, AStmt);
5947 if (!Pair.first && DirName.getName() && !DependentHint)
5948 DSAStack->addCriticalWithHint(Dir, Hint);
5949 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005950}
5951
Alexey Bataev4acb8592014-07-07 13:01:15 +00005952StmtResult Sema::ActOnOpenMPParallelForDirective(
5953 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00005954 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005955 if (!AStmt)
5956 return StmtError();
5957
Alexey Bataeve3727102018-04-18 15:57:46 +00005958 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005959 // 1.2.2 OpenMP Language Terminology
5960 // Structured block - An executable statement with a single entry at the
5961 // top and a single exit at the bottom.
5962 // The point of exit cannot be a branch out of the structured block.
5963 // longjmp() and throw() must not violate the entry/exit criteria.
5964 CS->getCapturedDecl()->setNothrow();
5965
Alexander Musmanc6388682014-12-15 07:07:06 +00005966 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005967 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5968 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005969 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00005970 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00005971 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5972 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005973 if (NestedLoopCount == 0)
5974 return StmtError();
5975
Alexander Musmana5f070a2014-10-01 06:03:56 +00005976 assert((CurContext->isDependentContext() || B.builtAll()) &&
5977 "omp parallel for loop exprs were not built");
5978
Alexey Bataev54acd402015-08-04 11:18:19 +00005979 if (!CurContext->isDependentContext()) {
5980 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005981 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005982 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00005983 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005984 B.NumIterations, *this, CurScope,
5985 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005986 return StmtError();
5987 }
5988 }
5989
Reid Kleckner87a31802018-03-12 21:43:02 +00005990 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005991 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005992 NestedLoopCount, Clauses, AStmt, B,
5993 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00005994}
5995
Alexander Musmane4e893b2014-09-23 09:33:00 +00005996StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5997 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00005998 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005999 if (!AStmt)
6000 return StmtError();
6001
Alexey Bataeve3727102018-04-18 15:57:46 +00006002 auto *CS = cast<CapturedStmt>(AStmt);
Alexander Musmane4e893b2014-09-23 09:33:00 +00006003 // 1.2.2 OpenMP Language Terminology
6004 // Structured block - An executable statement with a single entry at the
6005 // top and a single exit at the bottom.
6006 // The point of exit cannot be a branch out of the structured block.
6007 // longjmp() and throw() must not violate the entry/exit criteria.
6008 CS->getCapturedDecl()->setNothrow();
6009
Alexander Musmanc6388682014-12-15 07:07:06 +00006010 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006011 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6012 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00006013 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00006014 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00006015 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6016 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00006017 if (NestedLoopCount == 0)
6018 return StmtError();
6019
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00006020 if (!CurContext->isDependentContext()) {
6021 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00006022 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00006023 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00006024 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006025 B.NumIterations, *this, CurScope,
6026 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00006027 return StmtError();
6028 }
6029 }
6030
Kelvin Lic5609492016-07-15 04:39:07 +00006031 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00006032 return StmtError();
6033
Reid Kleckner87a31802018-03-12 21:43:02 +00006034 setFunctionHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00006035 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00006036 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00006037}
6038
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006039StmtResult
6040Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
6041 Stmt *AStmt, SourceLocation StartLoc,
6042 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006043 if (!AStmt)
6044 return StmtError();
6045
6046 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006047 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00006048 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006049 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00006050 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006051 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00006052 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006053 return StmtError();
6054 // All associated statements must be '#pragma omp section' except for
6055 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00006056 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006057 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
6058 if (SectionStmt)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006059 Diag(SectionStmt->getBeginLoc(),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006060 diag::err_omp_parallel_sections_substmt_not_section);
6061 return StmtError();
6062 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006063 cast<OMPSectionDirective>(SectionStmt)
6064 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006065 }
6066 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006067 Diag(AStmt->getBeginLoc(),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006068 diag::err_omp_parallel_sections_not_compound_stmt);
6069 return StmtError();
6070 }
6071
Reid Kleckner87a31802018-03-12 21:43:02 +00006072 setFunctionHasBranchProtectedScope();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006073
Alexey Bataev25e5b442015-09-15 12:52:43 +00006074 return OMPParallelSectionsDirective::Create(
6075 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006076}
6077
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006078StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
6079 Stmt *AStmt, SourceLocation StartLoc,
6080 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006081 if (!AStmt)
6082 return StmtError();
6083
David Majnemer9d168222016-08-05 17:44:54 +00006084 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006085 // 1.2.2 OpenMP Language Terminology
6086 // Structured block - An executable statement with a single entry at the
6087 // top and a single exit at the bottom.
6088 // The point of exit cannot be a branch out of the structured block.
6089 // longjmp() and throw() must not violate the entry/exit criteria.
6090 CS->getCapturedDecl()->setNothrow();
6091
Reid Kleckner87a31802018-03-12 21:43:02 +00006092 setFunctionHasBranchProtectedScope();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006093
Alexey Bataev25e5b442015-09-15 12:52:43 +00006094 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
6095 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006096}
6097
Alexey Bataev68446b72014-07-18 07:47:19 +00006098StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
6099 SourceLocation EndLoc) {
6100 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
6101}
6102
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00006103StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
6104 SourceLocation EndLoc) {
6105 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
6106}
6107
Alexey Bataev2df347a2014-07-18 10:17:07 +00006108StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
6109 SourceLocation EndLoc) {
6110 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
6111}
6112
Alexey Bataev169d96a2017-07-18 20:17:46 +00006113StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses,
6114 Stmt *AStmt,
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006115 SourceLocation StartLoc,
6116 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006117 if (!AStmt)
6118 return StmtError();
6119
6120 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006121
Reid Kleckner87a31802018-03-12 21:43:02 +00006122 setFunctionHasBranchProtectedScope();
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006123
Alexey Bataev169d96a2017-07-18 20:17:46 +00006124 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses,
Alexey Bataev3b1b8952017-07-25 15:53:26 +00006125 AStmt,
6126 DSAStack->getTaskgroupReductionRef());
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006127}
6128
Alexey Bataev6125da92014-07-21 11:26:11 +00006129StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
6130 SourceLocation StartLoc,
6131 SourceLocation EndLoc) {
6132 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
6133 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
6134}
6135
Alexey Bataev346265e2015-09-25 10:37:12 +00006136StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
6137 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006138 SourceLocation StartLoc,
6139 SourceLocation EndLoc) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006140 const OMPClause *DependFound = nullptr;
6141 const OMPClause *DependSourceClause = nullptr;
6142 const OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00006143 bool ErrorFound = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00006144 const OMPThreadsClause *TC = nullptr;
6145 const OMPSIMDClause *SC = nullptr;
6146 for (const OMPClause *C : Clauses) {
Alexey Bataeveb482352015-12-18 05:05:56 +00006147 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
6148 DependFound = C;
6149 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
6150 if (DependSourceClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006151 Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
Alexey Bataeveb482352015-12-18 05:05:56 +00006152 << getOpenMPDirectiveName(OMPD_ordered)
6153 << getOpenMPClauseName(OMPC_depend) << 2;
6154 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006155 } else {
Alexey Bataeveb482352015-12-18 05:05:56 +00006156 DependSourceClause = C;
Alexey Bataeve3727102018-04-18 15:57:46 +00006157 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006158 if (DependSinkClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006159 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006160 << 0;
6161 ErrorFound = true;
6162 }
6163 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
6164 if (DependSourceClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006165 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006166 << 1;
6167 ErrorFound = true;
6168 }
6169 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00006170 }
Alexey Bataeve3727102018-04-18 15:57:46 +00006171 } else if (C->getClauseKind() == OMPC_threads) {
Alexey Bataev346265e2015-09-25 10:37:12 +00006172 TC = cast<OMPThreadsClause>(C);
Alexey Bataeve3727102018-04-18 15:57:46 +00006173 } else if (C->getClauseKind() == OMPC_simd) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006174 SC = cast<OMPSIMDClause>(C);
Alexey Bataeve3727102018-04-18 15:57:46 +00006175 }
Alexey Bataev346265e2015-09-25 10:37:12 +00006176 }
Alexey Bataeveb482352015-12-18 05:05:56 +00006177 if (!ErrorFound && !SC &&
6178 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006179 // OpenMP [2.8.1,simd Construct, Restrictions]
6180 // An ordered construct with the simd clause is the only OpenMP construct
6181 // that can appear in the simd region.
6182 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00006183 ErrorFound = true;
6184 } else if (DependFound && (TC || SC)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006185 Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd)
Alexey Bataeveb482352015-12-18 05:05:56 +00006186 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
6187 ErrorFound = true;
Alexey Bataevf138fda2018-08-13 19:04:24 +00006188 } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006189 Diag(DependFound->getBeginLoc(),
Alexey Bataeveb482352015-12-18 05:05:56 +00006190 diag::err_omp_ordered_directive_without_param);
6191 ErrorFound = true;
6192 } else if (TC || Clauses.empty()) {
Alexey Bataevf138fda2018-08-13 19:04:24 +00006193 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006194 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc;
Alexey Bataeveb482352015-12-18 05:05:56 +00006195 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
6196 << (TC != nullptr);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006197 Diag(Param->getBeginLoc(), diag::note_omp_ordered_param);
Alexey Bataeveb482352015-12-18 05:05:56 +00006198 ErrorFound = true;
6199 }
6200 }
6201 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006202 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00006203
6204 if (AStmt) {
6205 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6206
Reid Kleckner87a31802018-03-12 21:43:02 +00006207 setFunctionHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006208 }
Alexey Bataev346265e2015-09-25 10:37:12 +00006209
6210 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006211}
6212
Alexey Bataev1d160b12015-03-13 12:27:31 +00006213namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006214/// Helper class for checking expression in 'omp atomic [update]'
Alexey Bataev1d160b12015-03-13 12:27:31 +00006215/// construct.
6216class OpenMPAtomicUpdateChecker {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006217 /// Error results for atomic update expressions.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006218 enum ExprAnalysisErrorCode {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006219 /// A statement is not an expression statement.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006220 NotAnExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006221 /// Expression is not builtin binary or unary operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006222 NotABinaryOrUnaryExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006223 /// Unary operation is not post-/pre- increment/decrement operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006224 NotAnUnaryIncDecExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006225 /// An expression is not of scalar type.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006226 NotAScalarType,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006227 /// A binary operation is not an assignment operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006228 NotAnAssignmentOp,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006229 /// RHS part of the binary operation is not a binary expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006230 NotABinaryExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006231 /// RHS part is not additive/multiplicative/shift/biwise binary
Alexey Bataev1d160b12015-03-13 12:27:31 +00006232 /// expression.
6233 NotABinaryOperator,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006234 /// RHS binary operation does not have reference to the updated LHS
Alexey Bataev1d160b12015-03-13 12:27:31 +00006235 /// part.
6236 NotAnUpdateExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006237 /// No errors is found.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006238 NoError
6239 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006240 /// Reference to Sema.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006241 Sema &SemaRef;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006242 /// A location for note diagnostics (when error is found).
Alexey Bataev1d160b12015-03-13 12:27:31 +00006243 SourceLocation NoteLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006244 /// 'x' lvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006245 Expr *X;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006246 /// 'expr' rvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006247 Expr *E;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006248 /// Helper expression of the form
Alexey Bataevb4505a72015-03-30 05:20:59 +00006249 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6250 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6251 Expr *UpdateExpr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006252 /// Is 'x' a LHS in a RHS part of full update expression. It is
Alexey Bataevb4505a72015-03-30 05:20:59 +00006253 /// important for non-associative operations.
6254 bool IsXLHSInRHSPart;
6255 BinaryOperatorKind Op;
6256 SourceLocation OpLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006257 /// true if the source expression is a postfix unary operation, false
Alexey Bataevb78ca832015-04-01 03:33:17 +00006258 /// if it is a prefix unary operation.
6259 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006260
6261public:
6262 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00006263 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00006264 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006265 /// Check specified statement that it is suitable for 'atomic update'
Alexey Bataev1d160b12015-03-13 12:27:31 +00006266 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00006267 /// expression. If DiagId and NoteId == 0, then only check is performed
6268 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006269 /// \param DiagId Diagnostic which should be emitted if error is found.
6270 /// \param NoteId Diagnostic note for the main error message.
6271 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00006272 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006273 /// Return the 'x' lvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006274 Expr *getX() const { return X; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006275 /// Return the 'expr' rvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006276 Expr *getExpr() const { return E; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006277 /// Return the update expression used in calculation of the updated
Alexey Bataevb4505a72015-03-30 05:20:59 +00006278 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6279 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6280 Expr *getUpdateExpr() const { return UpdateExpr; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006281 /// Return true if 'x' is LHS in RHS part of full update expression,
Alexey Bataevb4505a72015-03-30 05:20:59 +00006282 /// false otherwise.
6283 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
6284
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006285 /// true if the source expression is a postfix unary operation, false
Alexey Bataevb78ca832015-04-01 03:33:17 +00006286 /// if it is a prefix unary operation.
6287 bool isPostfixUpdate() const { return IsPostfixUpdate; }
6288
Alexey Bataev1d160b12015-03-13 12:27:31 +00006289private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00006290 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
6291 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00006292};
6293} // namespace
6294
6295bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
6296 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
6297 ExprAnalysisErrorCode ErrorFound = NoError;
6298 SourceLocation ErrorLoc, NoteLoc;
6299 SourceRange ErrorRange, NoteRange;
6300 // Allowed constructs are:
6301 // x = x binop expr;
6302 // x = expr binop x;
6303 if (AtomicBinOp->getOpcode() == BO_Assign) {
6304 X = AtomicBinOp->getLHS();
Alexey Bataeve3727102018-04-18 15:57:46 +00006305 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006306 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
6307 if (AtomicInnerBinOp->isMultiplicativeOp() ||
6308 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
6309 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006310 Op = AtomicInnerBinOp->getOpcode();
6311 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataeve3727102018-04-18 15:57:46 +00006312 Expr *LHS = AtomicInnerBinOp->getLHS();
6313 Expr *RHS = AtomicInnerBinOp->getRHS();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006314 llvm::FoldingSetNodeID XId, LHSId, RHSId;
6315 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
6316 /*Canonical=*/true);
6317 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
6318 /*Canonical=*/true);
6319 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
6320 /*Canonical=*/true);
6321 if (XId == LHSId) {
6322 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006323 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006324 } else if (XId == RHSId) {
6325 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006326 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006327 } else {
6328 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6329 ErrorRange = AtomicInnerBinOp->getSourceRange();
6330 NoteLoc = X->getExprLoc();
6331 NoteRange = X->getSourceRange();
6332 ErrorFound = NotAnUpdateExpression;
6333 }
6334 } else {
6335 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6336 ErrorRange = AtomicInnerBinOp->getSourceRange();
6337 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
6338 NoteRange = SourceRange(NoteLoc, NoteLoc);
6339 ErrorFound = NotABinaryOperator;
6340 }
6341 } else {
6342 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
6343 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
6344 ErrorFound = NotABinaryExpression;
6345 }
6346 } else {
6347 ErrorLoc = AtomicBinOp->getExprLoc();
6348 ErrorRange = AtomicBinOp->getSourceRange();
6349 NoteLoc = AtomicBinOp->getOperatorLoc();
6350 NoteRange = SourceRange(NoteLoc, NoteLoc);
6351 ErrorFound = NotAnAssignmentOp;
6352 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006353 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006354 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6355 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6356 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006357 }
6358 if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006359 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006360 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006361}
6362
6363bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
6364 unsigned NoteId) {
6365 ExprAnalysisErrorCode ErrorFound = NoError;
6366 SourceLocation ErrorLoc, NoteLoc;
6367 SourceRange ErrorRange, NoteRange;
6368 // Allowed constructs are:
6369 // x++;
6370 // x--;
6371 // ++x;
6372 // --x;
6373 // x binop= expr;
6374 // x = x binop expr;
6375 // x = expr binop x;
6376 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
6377 AtomicBody = AtomicBody->IgnoreParenImpCasts();
6378 if (AtomicBody->getType()->isScalarType() ||
6379 AtomicBody->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006380 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006381 AtomicBody->IgnoreParenImpCasts())) {
6382 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00006383 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006384 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00006385 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006386 E = AtomicCompAssignOp->getRHS();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006387 X = AtomicCompAssignOp->getLHS()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006388 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006389 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
6390 AtomicBody->IgnoreParenImpCasts())) {
6391 // Check for Binary Operation
David Majnemer9d168222016-08-05 17:44:54 +00006392 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
Alexey Bataevb4505a72015-03-30 05:20:59 +00006393 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006394 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
David Majnemer9d168222016-08-05 17:44:54 +00006395 AtomicBody->IgnoreParenImpCasts())) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006396 // Check for Unary Operation
6397 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006398 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006399 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
6400 OpLoc = AtomicUnaryOp->getOperatorLoc();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006401 X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006402 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
6403 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006404 } else {
6405 ErrorFound = NotAnUnaryIncDecExpression;
6406 ErrorLoc = AtomicUnaryOp->getExprLoc();
6407 ErrorRange = AtomicUnaryOp->getSourceRange();
6408 NoteLoc = AtomicUnaryOp->getOperatorLoc();
6409 NoteRange = SourceRange(NoteLoc, NoteLoc);
6410 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006411 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006412 ErrorFound = NotABinaryOrUnaryExpression;
6413 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
6414 NoteRange = ErrorRange = AtomicBody->getSourceRange();
6415 }
6416 } else {
6417 ErrorFound = NotAScalarType;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006418 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006419 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6420 }
6421 } else {
6422 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006423 NoteLoc = ErrorLoc = S->getBeginLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006424 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6425 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006426 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006427 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6428 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6429 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006430 }
6431 if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006432 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006433 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006434 // Build an update expression of form 'OpaqueValueExpr(x) binop
6435 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
6436 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
6437 auto *OVEX = new (SemaRef.getASTContext())
6438 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
6439 auto *OVEExpr = new (SemaRef.getASTContext())
6440 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
Alexey Bataeve3727102018-04-18 15:57:46 +00006441 ExprResult Update =
Alexey Bataevb4505a72015-03-30 05:20:59 +00006442 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
6443 IsXLHSInRHSPart ? OVEExpr : OVEX);
6444 if (Update.isInvalid())
6445 return true;
6446 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
6447 Sema::AA_Casting);
6448 if (Update.isInvalid())
6449 return true;
6450 UpdateExpr = Update.get();
6451 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00006452 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006453}
6454
Alexey Bataev0162e452014-07-22 10:10:35 +00006455StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
6456 Stmt *AStmt,
6457 SourceLocation StartLoc,
6458 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006459 if (!AStmt)
6460 return StmtError();
6461
David Majnemer9d168222016-08-05 17:44:54 +00006462 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00006463 // 1.2.2 OpenMP Language Terminology
6464 // Structured block - An executable statement with a single entry at the
6465 // top and a single exit at the bottom.
6466 // The point of exit cannot be a branch out of the structured block.
6467 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00006468 OpenMPClauseKind AtomicKind = OMPC_unknown;
6469 SourceLocation AtomicKindLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00006470 for (const OMPClause *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00006471 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00006472 C->getClauseKind() == OMPC_update ||
6473 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00006474 if (AtomicKind != OMPC_unknown) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006475 Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00006476 << SourceRange(C->getBeginLoc(), C->getEndLoc());
Alexey Bataevdea47612014-07-23 07:46:59 +00006477 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
6478 << getOpenMPClauseName(AtomicKind);
6479 } else {
6480 AtomicKind = C->getClauseKind();
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006481 AtomicKindLoc = C->getBeginLoc();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006482 }
6483 }
6484 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006485
Alexey Bataeve3727102018-04-18 15:57:46 +00006486 Stmt *Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00006487 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
6488 Body = EWC->getSubExpr();
6489
Alexey Bataev62cec442014-11-18 10:14:22 +00006490 Expr *X = nullptr;
6491 Expr *V = nullptr;
6492 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006493 Expr *UE = nullptr;
6494 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006495 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00006496 // OpenMP [2.12.6, atomic Construct]
6497 // In the next expressions:
6498 // * x and v (as applicable) are both l-value expressions with scalar type.
6499 // * During the execution of an atomic region, multiple syntactic
6500 // occurrences of x must designate the same storage location.
6501 // * Neither of v and expr (as applicable) may access the storage location
6502 // designated by x.
6503 // * Neither of x and expr (as applicable) may access the storage location
6504 // designated by v.
6505 // * expr is an expression with scalar type.
6506 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
6507 // * binop, binop=, ++, and -- are not overloaded operators.
6508 // * The expression x binop expr must be numerically equivalent to x binop
6509 // (expr). This requirement is satisfied if the operators in expr have
6510 // precedence greater than binop, or by using parentheses around expr or
6511 // subexpressions of expr.
6512 // * The expression expr binop x must be numerically equivalent to (expr)
6513 // binop x. This requirement is satisfied if the operators in expr have
6514 // precedence equal to or greater than binop, or by using parentheses around
6515 // expr or subexpressions of expr.
6516 // * For forms that allow multiple occurrences of x, the number of times
6517 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00006518 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006519 enum {
6520 NotAnExpression,
6521 NotAnAssignmentOp,
6522 NotAScalarType,
6523 NotAnLValue,
6524 NoError
6525 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00006526 SourceLocation ErrorLoc, NoteLoc;
6527 SourceRange ErrorRange, NoteRange;
6528 // If clause is read:
6529 // v = x;
Alexey Bataeve3727102018-04-18 15:57:46 +00006530 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
6531 const auto *AtomicBinOp =
Alexey Bataev62cec442014-11-18 10:14:22 +00006532 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6533 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6534 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6535 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
6536 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6537 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
6538 if (!X->isLValue() || !V->isLValue()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006539 const Expr *NotLValueExpr = X->isLValue() ? V : X;
Alexey Bataev62cec442014-11-18 10:14:22 +00006540 ErrorFound = NotAnLValue;
6541 ErrorLoc = AtomicBinOp->getExprLoc();
6542 ErrorRange = AtomicBinOp->getSourceRange();
6543 NoteLoc = NotLValueExpr->getExprLoc();
6544 NoteRange = NotLValueExpr->getSourceRange();
6545 }
6546 } else if (!X->isInstantiationDependent() ||
6547 !V->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006548 const Expr *NotScalarExpr =
Alexey Bataev62cec442014-11-18 10:14:22 +00006549 (X->isInstantiationDependent() || X->getType()->isScalarType())
6550 ? V
6551 : X;
6552 ErrorFound = NotAScalarType;
6553 ErrorLoc = AtomicBinOp->getExprLoc();
6554 ErrorRange = AtomicBinOp->getSourceRange();
6555 NoteLoc = NotScalarExpr->getExprLoc();
6556 NoteRange = NotScalarExpr->getSourceRange();
6557 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006558 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00006559 ErrorFound = NotAnAssignmentOp;
6560 ErrorLoc = AtomicBody->getExprLoc();
6561 ErrorRange = AtomicBody->getSourceRange();
6562 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6563 : AtomicBody->getExprLoc();
6564 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6565 : AtomicBody->getSourceRange();
6566 }
6567 } else {
6568 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006569 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataev62cec442014-11-18 10:14:22 +00006570 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006571 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006572 if (ErrorFound != NoError) {
6573 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
6574 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006575 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6576 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00006577 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00006578 }
6579 if (CurContext->isDependentContext())
Alexey Bataev62cec442014-11-18 10:14:22 +00006580 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00006581 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006582 enum {
6583 NotAnExpression,
6584 NotAnAssignmentOp,
6585 NotAScalarType,
6586 NotAnLValue,
6587 NoError
6588 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006589 SourceLocation ErrorLoc, NoteLoc;
6590 SourceRange ErrorRange, NoteRange;
6591 // If clause is write:
6592 // x = expr;
Alexey Bataeve3727102018-04-18 15:57:46 +00006593 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
6594 const auto *AtomicBinOp =
Alexey Bataevf33eba62014-11-28 07:21:40 +00006595 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6596 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00006597 X = AtomicBinOp->getLHS();
6598 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006599 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6600 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
6601 if (!X->isLValue()) {
6602 ErrorFound = NotAnLValue;
6603 ErrorLoc = AtomicBinOp->getExprLoc();
6604 ErrorRange = AtomicBinOp->getSourceRange();
6605 NoteLoc = X->getExprLoc();
6606 NoteRange = X->getSourceRange();
6607 }
6608 } else if (!X->isInstantiationDependent() ||
6609 !E->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006610 const Expr *NotScalarExpr =
Alexey Bataevf33eba62014-11-28 07:21:40 +00006611 (X->isInstantiationDependent() || X->getType()->isScalarType())
6612 ? E
6613 : X;
6614 ErrorFound = NotAScalarType;
6615 ErrorLoc = AtomicBinOp->getExprLoc();
6616 ErrorRange = AtomicBinOp->getSourceRange();
6617 NoteLoc = NotScalarExpr->getExprLoc();
6618 NoteRange = NotScalarExpr->getSourceRange();
6619 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006620 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00006621 ErrorFound = NotAnAssignmentOp;
6622 ErrorLoc = AtomicBody->getExprLoc();
6623 ErrorRange = AtomicBody->getSourceRange();
6624 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6625 : AtomicBody->getExprLoc();
6626 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6627 : AtomicBody->getSourceRange();
6628 }
6629 } else {
6630 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006631 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006632 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006633 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00006634 if (ErrorFound != NoError) {
6635 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
6636 << ErrorRange;
6637 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6638 << NoteRange;
6639 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00006640 }
6641 if (CurContext->isDependentContext())
Alexey Bataevf33eba62014-11-28 07:21:40 +00006642 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006643 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006644 // If clause is update:
6645 // x++;
6646 // x--;
6647 // ++x;
6648 // --x;
6649 // x binop= expr;
6650 // x = x binop expr;
6651 // x = expr binop x;
6652 OpenMPAtomicUpdateChecker Checker(*this);
6653 if (Checker.checkStatement(
6654 Body, (AtomicKind == OMPC_update)
6655 ? diag::err_omp_atomic_update_not_expression_statement
6656 : diag::err_omp_atomic_not_expression_statement,
6657 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00006658 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006659 if (!CurContext->isDependentContext()) {
6660 E = Checker.getExpr();
6661 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006662 UE = Checker.getUpdateExpr();
6663 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00006664 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006665 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006666 enum {
6667 NotAnAssignmentOp,
6668 NotACompoundStatement,
6669 NotTwoSubstatements,
6670 NotASpecificExpression,
6671 NoError
6672 } ErrorFound = NoError;
6673 SourceLocation ErrorLoc, NoteLoc;
6674 SourceRange ErrorRange, NoteRange;
Alexey Bataeve3727102018-04-18 15:57:46 +00006675 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006676 // If clause is a capture:
6677 // v = x++;
6678 // v = x--;
6679 // v = ++x;
6680 // v = --x;
6681 // v = x binop= expr;
6682 // v = x = x binop expr;
6683 // v = x = expr binop x;
Alexey Bataeve3727102018-04-18 15:57:46 +00006684 const auto *AtomicBinOp =
Alexey Bataevb78ca832015-04-01 03:33:17 +00006685 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6686 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6687 V = AtomicBinOp->getLHS();
6688 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6689 OpenMPAtomicUpdateChecker Checker(*this);
6690 if (Checker.checkStatement(
6691 Body, diag::err_omp_atomic_capture_not_expression_statement,
6692 diag::note_omp_atomic_update))
6693 return StmtError();
6694 E = Checker.getExpr();
6695 X = Checker.getX();
6696 UE = Checker.getUpdateExpr();
6697 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6698 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00006699 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006700 ErrorLoc = AtomicBody->getExprLoc();
6701 ErrorRange = AtomicBody->getSourceRange();
6702 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6703 : AtomicBody->getExprLoc();
6704 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6705 : AtomicBody->getSourceRange();
6706 ErrorFound = NotAnAssignmentOp;
6707 }
6708 if (ErrorFound != NoError) {
6709 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6710 << ErrorRange;
6711 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6712 return StmtError();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006713 }
Alexey Bataeve3727102018-04-18 15:57:46 +00006714 if (CurContext->isDependentContext())
6715 UE = V = E = X = nullptr;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006716 } else {
6717 // If clause is a capture:
6718 // { v = x; x = expr; }
6719 // { v = x; x++; }
6720 // { v = x; x--; }
6721 // { v = x; ++x; }
6722 // { v = x; --x; }
6723 // { v = x; x binop= expr; }
6724 // { v = x; x = x binop expr; }
6725 // { v = x; x = expr binop x; }
6726 // { x++; v = x; }
6727 // { x--; v = x; }
6728 // { ++x; v = x; }
6729 // { --x; v = x; }
6730 // { x binop= expr; v = x; }
6731 // { x = x binop expr; v = x; }
6732 // { x = expr binop x; v = x; }
6733 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6734 // Check that this is { expr1; expr2; }
6735 if (CS->size() == 2) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006736 Stmt *First = CS->body_front();
6737 Stmt *Second = CS->body_back();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006738 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6739 First = EWC->getSubExpr()->IgnoreParenImpCasts();
6740 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6741 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6742 // Need to find what subexpression is 'v' and what is 'x'.
6743 OpenMPAtomicUpdateChecker Checker(*this);
6744 bool IsUpdateExprFound = !Checker.checkStatement(Second);
6745 BinaryOperator *BinOp = nullptr;
6746 if (IsUpdateExprFound) {
6747 BinOp = dyn_cast<BinaryOperator>(First);
6748 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6749 }
6750 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6751 // { v = x; x++; }
6752 // { v = x; x--; }
6753 // { v = x; ++x; }
6754 // { v = x; --x; }
6755 // { v = x; x binop= expr; }
6756 // { v = x; x = x binop expr; }
6757 // { v = x; x = expr binop x; }
6758 // Check that the first expression has form v = x.
Alexey Bataeve3727102018-04-18 15:57:46 +00006759 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006760 llvm::FoldingSetNodeID XId, PossibleXId;
6761 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6762 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6763 IsUpdateExprFound = XId == PossibleXId;
6764 if (IsUpdateExprFound) {
6765 V = BinOp->getLHS();
6766 X = Checker.getX();
6767 E = Checker.getExpr();
6768 UE = Checker.getUpdateExpr();
6769 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006770 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006771 }
6772 }
6773 if (!IsUpdateExprFound) {
6774 IsUpdateExprFound = !Checker.checkStatement(First);
6775 BinOp = nullptr;
6776 if (IsUpdateExprFound) {
6777 BinOp = dyn_cast<BinaryOperator>(Second);
6778 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6779 }
6780 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6781 // { x++; v = x; }
6782 // { x--; v = x; }
6783 // { ++x; v = x; }
6784 // { --x; v = x; }
6785 // { x binop= expr; v = x; }
6786 // { x = x binop expr; v = x; }
6787 // { x = expr binop x; v = x; }
6788 // Check that the second expression has form v = x.
Alexey Bataeve3727102018-04-18 15:57:46 +00006789 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006790 llvm::FoldingSetNodeID XId, PossibleXId;
6791 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6792 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6793 IsUpdateExprFound = XId == PossibleXId;
6794 if (IsUpdateExprFound) {
6795 V = BinOp->getLHS();
6796 X = Checker.getX();
6797 E = Checker.getExpr();
6798 UE = Checker.getUpdateExpr();
6799 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006800 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006801 }
6802 }
6803 }
6804 if (!IsUpdateExprFound) {
6805 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00006806 auto *FirstExpr = dyn_cast<Expr>(First);
6807 auto *SecondExpr = dyn_cast<Expr>(Second);
6808 if (!FirstExpr || !SecondExpr ||
6809 !(FirstExpr->isInstantiationDependent() ||
6810 SecondExpr->isInstantiationDependent())) {
6811 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6812 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006813 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00006814 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006815 : First->getBeginLoc();
Alexey Bataev5a195472015-09-04 12:55:50 +00006816 NoteRange = ErrorRange = FirstBinOp
6817 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00006818 : SourceRange(ErrorLoc, ErrorLoc);
6819 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006820 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6821 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6822 ErrorFound = NotAnAssignmentOp;
6823 NoteLoc = ErrorLoc = SecondBinOp
6824 ? SecondBinOp->getOperatorLoc()
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006825 : Second->getBeginLoc();
Alexey Bataev5a195472015-09-04 12:55:50 +00006826 NoteRange = ErrorRange =
6827 SecondBinOp ? SecondBinOp->getSourceRange()
6828 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00006829 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00006830 Expr *PossibleXRHSInFirst =
Alexey Bataev5a195472015-09-04 12:55:50 +00006831 FirstBinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00006832 Expr *PossibleXLHSInSecond =
Alexey Bataev5a195472015-09-04 12:55:50 +00006833 SecondBinOp->getLHS()->IgnoreParenImpCasts();
6834 llvm::FoldingSetNodeID X1Id, X2Id;
6835 PossibleXRHSInFirst->Profile(X1Id, Context,
6836 /*Canonical=*/true);
6837 PossibleXLHSInSecond->Profile(X2Id, Context,
6838 /*Canonical=*/true);
6839 IsUpdateExprFound = X1Id == X2Id;
6840 if (IsUpdateExprFound) {
6841 V = FirstBinOp->getLHS();
6842 X = SecondBinOp->getLHS();
6843 E = SecondBinOp->getRHS();
6844 UE = nullptr;
6845 IsXLHSInRHSPart = false;
6846 IsPostfixUpdate = true;
6847 } else {
6848 ErrorFound = NotASpecificExpression;
6849 ErrorLoc = FirstBinOp->getExprLoc();
6850 ErrorRange = FirstBinOp->getSourceRange();
6851 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6852 NoteRange = SecondBinOp->getRHS()->getSourceRange();
6853 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006854 }
6855 }
6856 }
6857 }
6858 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006859 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006860 NoteRange = ErrorRange =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006861 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
Alexey Bataevb78ca832015-04-01 03:33:17 +00006862 ErrorFound = NotTwoSubstatements;
6863 }
6864 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006865 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006866 NoteRange = ErrorRange =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006867 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
Alexey Bataevb78ca832015-04-01 03:33:17 +00006868 ErrorFound = NotACompoundStatement;
6869 }
6870 if (ErrorFound != NoError) {
6871 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6872 << ErrorRange;
6873 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6874 return StmtError();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006875 }
Alexey Bataeve3727102018-04-18 15:57:46 +00006876 if (CurContext->isDependentContext())
6877 UE = V = E = X = nullptr;
Alexey Bataev459dec02014-07-24 06:46:57 +00006878 }
Alexey Bataevdea47612014-07-23 07:46:59 +00006879 }
Alexey Bataev0162e452014-07-22 10:10:35 +00006880
Reid Kleckner87a31802018-03-12 21:43:02 +00006881 setFunctionHasBranchProtectedScope();
Alexey Bataev0162e452014-07-22 10:10:35 +00006882
Alexey Bataev62cec442014-11-18 10:14:22 +00006883 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00006884 X, V, E, UE, IsXLHSInRHSPart,
6885 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00006886}
6887
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006888StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6889 Stmt *AStmt,
6890 SourceLocation StartLoc,
6891 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006892 if (!AStmt)
6893 return StmtError();
6894
Alexey Bataeve3727102018-04-18 15:57:46 +00006895 auto *CS = cast<CapturedStmt>(AStmt);
Samuel Antao4af1b7b2015-12-02 17:44:43 +00006896 // 1.2.2 OpenMP Language Terminology
6897 // Structured block - An executable statement with a single entry at the
6898 // top and a single exit at the bottom.
6899 // The point of exit cannot be a branch out of the structured block.
6900 // longjmp() and throw() must not violate the entry/exit criteria.
6901 CS->getCapturedDecl()->setNothrow();
Alexey Bataev8451efa2018-01-15 19:06:12 +00006902 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target);
6903 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6904 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6905 // 1.2.2 OpenMP Language Terminology
6906 // Structured block - An executable statement with a single entry at the
6907 // top and a single exit at the bottom.
6908 // The point of exit cannot be a branch out of the structured block.
6909 // longjmp() and throw() must not violate the entry/exit criteria.
6910 CS->getCapturedDecl()->setNothrow();
6911 }
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006912
Alexey Bataev13314bf2014-10-09 04:18:56 +00006913 // OpenMP [2.16, Nesting of Regions]
6914 // If specified, a teams construct must be contained within a target
6915 // construct. That target construct must contain no statements or directives
6916 // outside of the teams construct.
6917 if (DSAStack->hasInnerTeamsRegion()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006918 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true);
Alexey Bataev13314bf2014-10-09 04:18:56 +00006919 bool OMPTeamsFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006920 if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00006921 auto I = CS->body_begin();
6922 while (I != CS->body_end()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006923 const auto *OED = dyn_cast<OMPExecutableDirective>(*I);
Alexey Bataev13314bf2014-10-09 04:18:56 +00006924 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6925 OMPTeamsFound = false;
6926 break;
6927 }
6928 ++I;
6929 }
6930 assert(I != CS->body_end() && "Not found statement");
6931 S = *I;
Kelvin Li3834dce2016-06-27 19:15:43 +00006932 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00006933 const auto *OED = dyn_cast<OMPExecutableDirective>(S);
Kelvin Li3834dce2016-06-27 19:15:43 +00006934 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
Alexey Bataev13314bf2014-10-09 04:18:56 +00006935 }
6936 if (!OMPTeamsFound) {
6937 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6938 Diag(DSAStack->getInnerTeamsRegionLoc(),
6939 diag::note_omp_nested_teams_construct_here);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006940 Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here)
Alexey Bataev13314bf2014-10-09 04:18:56 +00006941 << isa<OMPExecutableDirective>(S);
6942 return StmtError();
6943 }
6944 }
6945
Reid Kleckner87a31802018-03-12 21:43:02 +00006946 setFunctionHasBranchProtectedScope();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006947
6948 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6949}
6950
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006951StmtResult
6952Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6953 Stmt *AStmt, SourceLocation StartLoc,
6954 SourceLocation EndLoc) {
6955 if (!AStmt)
6956 return StmtError();
6957
Alexey Bataeve3727102018-04-18 15:57:46 +00006958 auto *CS = cast<CapturedStmt>(AStmt);
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006959 // 1.2.2 OpenMP Language Terminology
6960 // Structured block - An executable statement with a single entry at the
6961 // top and a single exit at the bottom.
6962 // The point of exit cannot be a branch out of the structured block.
6963 // longjmp() and throw() must not violate the entry/exit criteria.
6964 CS->getCapturedDecl()->setNothrow();
Alexey Bataev8451efa2018-01-15 19:06:12 +00006965 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel);
6966 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6967 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6968 // 1.2.2 OpenMP Language Terminology
6969 // Structured block - An executable statement with a single entry at the
6970 // top and a single exit at the bottom.
6971 // The point of exit cannot be a branch out of the structured block.
6972 // longjmp() and throw() must not violate the entry/exit criteria.
6973 CS->getCapturedDecl()->setNothrow();
6974 }
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006975
Reid Kleckner87a31802018-03-12 21:43:02 +00006976 setFunctionHasBranchProtectedScope();
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006977
6978 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6979 AStmt);
6980}
6981
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006982StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6983 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00006984 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006985 if (!AStmt)
6986 return StmtError();
6987
Alexey Bataeve3727102018-04-18 15:57:46 +00006988 auto *CS = cast<CapturedStmt>(AStmt);
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006989 // 1.2.2 OpenMP Language Terminology
6990 // Structured block - An executable statement with a single entry at the
6991 // top and a single exit at the bottom.
6992 // The point of exit cannot be a branch out of the structured block.
6993 // longjmp() and throw() must not violate the entry/exit criteria.
6994 CS->getCapturedDecl()->setNothrow();
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00006995 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
6996 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6997 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6998 // 1.2.2 OpenMP Language Terminology
6999 // Structured block - An executable statement with a single entry at the
7000 // top and a single exit at the bottom.
7001 // The point of exit cannot be a branch out of the structured block.
7002 // longjmp() and throw() must not violate the entry/exit criteria.
7003 CS->getCapturedDecl()->setNothrow();
7004 }
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007005
7006 OMPLoopDirective::HelperExprs B;
7007 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7008 // define the nested loops number.
7009 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007010 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00007011 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007012 VarsWithImplicitDSA, B);
7013 if (NestedLoopCount == 0)
7014 return StmtError();
7015
7016 assert((CurContext->isDependentContext() || B.builtAll()) &&
7017 "omp target parallel for loop exprs were not built");
7018
7019 if (!CurContext->isDependentContext()) {
7020 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007021 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007022 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007023 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007024 B.NumIterations, *this, CurScope,
7025 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007026 return StmtError();
7027 }
7028 }
7029
Reid Kleckner87a31802018-03-12 21:43:02 +00007030 setFunctionHasBranchProtectedScope();
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007031 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
7032 NestedLoopCount, Clauses, AStmt,
7033 B, DSAStack->isCancelRegion());
7034}
7035
Alexey Bataev95b64a92017-05-30 16:00:04 +00007036/// Check for existence of a map clause in the list of clauses.
7037static bool hasClauses(ArrayRef<OMPClause *> Clauses,
7038 const OpenMPClauseKind K) {
7039 return llvm::any_of(
7040 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; });
7041}
Samuel Antaodf67fc42016-01-19 19:15:56 +00007042
Alexey Bataev95b64a92017-05-30 16:00:04 +00007043template <typename... Params>
7044static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K,
7045 const Params... ClauseTypes) {
7046 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...);
Samuel Antaodf67fc42016-01-19 19:15:56 +00007047}
7048
Michael Wong65f367f2015-07-21 13:44:28 +00007049StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
7050 Stmt *AStmt,
7051 SourceLocation StartLoc,
7052 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007053 if (!AStmt)
7054 return StmtError();
7055
7056 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7057
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00007058 // OpenMP [2.10.1, Restrictions, p. 97]
7059 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00007060 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) {
7061 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
7062 << "'map' or 'use_device_ptr'"
David Majnemer9d168222016-08-05 17:44:54 +00007063 << getOpenMPDirectiveName(OMPD_target_data);
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00007064 return StmtError();
7065 }
7066
Reid Kleckner87a31802018-03-12 21:43:02 +00007067 setFunctionHasBranchProtectedScope();
Michael Wong65f367f2015-07-21 13:44:28 +00007068
7069 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
7070 AStmt);
7071}
7072
Samuel Antaodf67fc42016-01-19 19:15:56 +00007073StmtResult
7074Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
7075 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00007076 SourceLocation EndLoc, Stmt *AStmt) {
7077 if (!AStmt)
7078 return StmtError();
7079
Alexey Bataeve3727102018-04-18 15:57:46 +00007080 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00007081 // 1.2.2 OpenMP Language Terminology
7082 // Structured block - An executable statement with a single entry at the
7083 // top and a single exit at the bottom.
7084 // The point of exit cannot be a branch out of the structured block.
7085 // longjmp() and throw() must not violate the entry/exit criteria.
7086 CS->getCapturedDecl()->setNothrow();
7087 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data);
7088 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7089 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7090 // 1.2.2 OpenMP Language Terminology
7091 // Structured block - An executable statement with a single entry at the
7092 // top and a single exit at the bottom.
7093 // The point of exit cannot be a branch out of the structured block.
7094 // longjmp() and throw() must not violate the entry/exit criteria.
7095 CS->getCapturedDecl()->setNothrow();
7096 }
7097
Samuel Antaodf67fc42016-01-19 19:15:56 +00007098 // OpenMP [2.10.2, Restrictions, p. 99]
7099 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00007100 if (!hasClauses(Clauses, OMPC_map)) {
7101 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
7102 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data);
Samuel Antaodf67fc42016-01-19 19:15:56 +00007103 return StmtError();
7104 }
7105
Alexey Bataev7828b252017-11-21 17:08:48 +00007106 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
7107 AStmt);
Samuel Antaodf67fc42016-01-19 19:15:56 +00007108}
7109
Samuel Antao72590762016-01-19 20:04:50 +00007110StmtResult
7111Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
7112 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00007113 SourceLocation EndLoc, Stmt *AStmt) {
7114 if (!AStmt)
7115 return StmtError();
7116
Alexey Bataeve3727102018-04-18 15:57:46 +00007117 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00007118 // 1.2.2 OpenMP Language Terminology
7119 // Structured block - An executable statement with a single entry at the
7120 // top and a single exit at the bottom.
7121 // The point of exit cannot be a branch out of the structured block.
7122 // longjmp() and throw() must not violate the entry/exit criteria.
7123 CS->getCapturedDecl()->setNothrow();
7124 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data);
7125 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7126 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7127 // 1.2.2 OpenMP Language Terminology
7128 // Structured block - An executable statement with a single entry at the
7129 // top and a single exit at the bottom.
7130 // The point of exit cannot be a branch out of the structured block.
7131 // longjmp() and throw() must not violate the entry/exit criteria.
7132 CS->getCapturedDecl()->setNothrow();
7133 }
7134
Samuel Antao72590762016-01-19 20:04:50 +00007135 // OpenMP [2.10.3, Restrictions, p. 102]
7136 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00007137 if (!hasClauses(Clauses, OMPC_map)) {
7138 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
7139 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data);
Samuel Antao72590762016-01-19 20:04:50 +00007140 return StmtError();
7141 }
7142
Alexey Bataev7828b252017-11-21 17:08:48 +00007143 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
7144 AStmt);
Samuel Antao72590762016-01-19 20:04:50 +00007145}
7146
Samuel Antao686c70c2016-05-26 17:30:50 +00007147StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
7148 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00007149 SourceLocation EndLoc,
7150 Stmt *AStmt) {
7151 if (!AStmt)
7152 return StmtError();
7153
Alexey Bataeve3727102018-04-18 15:57:46 +00007154 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00007155 // 1.2.2 OpenMP Language Terminology
7156 // Structured block - An executable statement with a single entry at the
7157 // top and a single exit at the bottom.
7158 // The point of exit cannot be a branch out of the structured block.
7159 // longjmp() and throw() must not violate the entry/exit criteria.
7160 CS->getCapturedDecl()->setNothrow();
7161 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update);
7162 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7163 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7164 // 1.2.2 OpenMP Language Terminology
7165 // Structured block - An executable statement with a single entry at the
7166 // top and a single exit at the bottom.
7167 // The point of exit cannot be a branch out of the structured block.
7168 // longjmp() and throw() must not violate the entry/exit criteria.
7169 CS->getCapturedDecl()->setNothrow();
7170 }
7171
Alexey Bataev95b64a92017-05-30 16:00:04 +00007172 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) {
Samuel Antao686c70c2016-05-26 17:30:50 +00007173 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
7174 return StmtError();
7175 }
Alexey Bataev7828b252017-11-21 17:08:48 +00007176 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses,
7177 AStmt);
Samuel Antao686c70c2016-05-26 17:30:50 +00007178}
7179
Alexey Bataev13314bf2014-10-09 04:18:56 +00007180StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
7181 Stmt *AStmt, SourceLocation StartLoc,
7182 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007183 if (!AStmt)
7184 return StmtError();
7185
Alexey Bataeve3727102018-04-18 15:57:46 +00007186 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev13314bf2014-10-09 04:18:56 +00007187 // 1.2.2 OpenMP Language Terminology
7188 // Structured block - An executable statement with a single entry at the
7189 // top and a single exit at the bottom.
7190 // The point of exit cannot be a branch out of the structured block.
7191 // longjmp() and throw() must not violate the entry/exit criteria.
7192 CS->getCapturedDecl()->setNothrow();
7193
Reid Kleckner87a31802018-03-12 21:43:02 +00007194 setFunctionHasBranchProtectedScope();
Alexey Bataev13314bf2014-10-09 04:18:56 +00007195
Alexey Bataevceabd412017-11-30 18:01:54 +00007196 DSAStack->setParentTeamsRegionLoc(StartLoc);
7197
Alexey Bataev13314bf2014-10-09 04:18:56 +00007198 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
7199}
7200
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007201StmtResult
7202Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
7203 SourceLocation EndLoc,
7204 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007205 if (DSAStack->isParentNowaitRegion()) {
7206 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
7207 return StmtError();
7208 }
7209 if (DSAStack->isParentOrderedRegion()) {
7210 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
7211 return StmtError();
7212 }
7213 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
7214 CancelRegion);
7215}
7216
Alexey Bataev87933c72015-09-18 08:07:34 +00007217StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
7218 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00007219 SourceLocation EndLoc,
7220 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev80909872015-07-02 11:25:17 +00007221 if (DSAStack->isParentNowaitRegion()) {
7222 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
7223 return StmtError();
7224 }
7225 if (DSAStack->isParentOrderedRegion()) {
7226 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
7227 return StmtError();
7228 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00007229 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00007230 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
7231 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00007232}
7233
Alexey Bataev382967a2015-12-08 12:06:20 +00007234static bool checkGrainsizeNumTasksClauses(Sema &S,
7235 ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007236 const OMPClause *PrevClause = nullptr;
Alexey Bataev382967a2015-12-08 12:06:20 +00007237 bool ErrorFound = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00007238 for (const OMPClause *C : Clauses) {
Alexey Bataev382967a2015-12-08 12:06:20 +00007239 if (C->getClauseKind() == OMPC_grainsize ||
7240 C->getClauseKind() == OMPC_num_tasks) {
7241 if (!PrevClause)
7242 PrevClause = C;
7243 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007244 S.Diag(C->getBeginLoc(),
Alexey Bataev382967a2015-12-08 12:06:20 +00007245 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
7246 << getOpenMPClauseName(C->getClauseKind())
7247 << getOpenMPClauseName(PrevClause->getClauseKind());
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007248 S.Diag(PrevClause->getBeginLoc(),
Alexey Bataev382967a2015-12-08 12:06:20 +00007249 diag::note_omp_previous_grainsize_num_tasks)
7250 << getOpenMPClauseName(PrevClause->getClauseKind());
7251 ErrorFound = true;
7252 }
7253 }
7254 }
7255 return ErrorFound;
7256}
7257
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007258static bool checkReductionClauseWithNogroup(Sema &S,
7259 ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007260 const OMPClause *ReductionClause = nullptr;
7261 const OMPClause *NogroupClause = nullptr;
7262 for (const OMPClause *C : Clauses) {
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007263 if (C->getClauseKind() == OMPC_reduction) {
7264 ReductionClause = C;
7265 if (NogroupClause)
7266 break;
7267 continue;
7268 }
7269 if (C->getClauseKind() == OMPC_nogroup) {
7270 NogroupClause = C;
7271 if (ReductionClause)
7272 break;
7273 continue;
7274 }
7275 }
7276 if (ReductionClause && NogroupClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007277 S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup)
7278 << SourceRange(NogroupClause->getBeginLoc(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007279 NogroupClause->getEndLoc());
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007280 return true;
7281 }
7282 return false;
7283}
7284
Alexey Bataev49f6e782015-12-01 04:18:41 +00007285StmtResult Sema::ActOnOpenMPTaskLoopDirective(
7286 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007287 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00007288 if (!AStmt)
7289 return StmtError();
7290
7291 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7292 OMPLoopDirective::HelperExprs B;
7293 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7294 // define the nested loops number.
7295 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007296 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007297 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00007298 VarsWithImplicitDSA, B);
7299 if (NestedLoopCount == 0)
7300 return StmtError();
7301
7302 assert((CurContext->isDependentContext() || B.builtAll()) &&
7303 "omp for loop exprs were not built");
7304
Alexey Bataev382967a2015-12-08 12:06:20 +00007305 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7306 // The grainsize clause and num_tasks clause are mutually exclusive and may
7307 // not appear on the same taskloop directive.
7308 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7309 return StmtError();
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007310 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7311 // If a reduction clause is present on the taskloop directive, the nogroup
7312 // clause must not be specified.
7313 if (checkReductionClauseWithNogroup(*this, Clauses))
7314 return StmtError();
Alexey Bataev382967a2015-12-08 12:06:20 +00007315
Reid Kleckner87a31802018-03-12 21:43:02 +00007316 setFunctionHasBranchProtectedScope();
Alexey Bataev49f6e782015-12-01 04:18:41 +00007317 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
7318 NestedLoopCount, Clauses, AStmt, B);
7319}
7320
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007321StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
7322 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007323 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007324 if (!AStmt)
7325 return StmtError();
7326
7327 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7328 OMPLoopDirective::HelperExprs B;
7329 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7330 // define the nested loops number.
7331 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007332 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007333 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
7334 VarsWithImplicitDSA, B);
7335 if (NestedLoopCount == 0)
7336 return StmtError();
7337
7338 assert((CurContext->isDependentContext() || B.builtAll()) &&
7339 "omp for loop exprs were not built");
7340
Alexey Bataev5a3af132016-03-29 08:58:54 +00007341 if (!CurContext->isDependentContext()) {
7342 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007343 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007344 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007345 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007346 B.NumIterations, *this, CurScope,
7347 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007348 return StmtError();
7349 }
7350 }
7351
Alexey Bataev382967a2015-12-08 12:06:20 +00007352 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7353 // The grainsize clause and num_tasks clause are mutually exclusive and may
7354 // not appear on the same taskloop directive.
7355 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7356 return StmtError();
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007357 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7358 // If a reduction clause is present on the taskloop directive, the nogroup
7359 // clause must not be specified.
7360 if (checkReductionClauseWithNogroup(*this, Clauses))
7361 return StmtError();
Alexey Bataev438388c2017-11-22 18:34:02 +00007362 if (checkSimdlenSafelenSpecified(*this, Clauses))
7363 return StmtError();
Alexey Bataev382967a2015-12-08 12:06:20 +00007364
Reid Kleckner87a31802018-03-12 21:43:02 +00007365 setFunctionHasBranchProtectedScope();
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007366 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
7367 NestedLoopCount, Clauses, AStmt, B);
7368}
7369
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007370StmtResult Sema::ActOnOpenMPDistributeDirective(
7371 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007372 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007373 if (!AStmt)
7374 return StmtError();
7375
7376 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7377 OMPLoopDirective::HelperExprs B;
7378 // In presence of clause 'collapse' with number of loops, it will
7379 // define the nested loops number.
7380 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007381 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007382 nullptr /*ordered not a clause on distribute*/, AStmt,
7383 *this, *DSAStack, VarsWithImplicitDSA, B);
7384 if (NestedLoopCount == 0)
7385 return StmtError();
7386
7387 assert((CurContext->isDependentContext() || B.builtAll()) &&
7388 "omp for loop exprs were not built");
7389
Reid Kleckner87a31802018-03-12 21:43:02 +00007390 setFunctionHasBranchProtectedScope();
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007391 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
7392 NestedLoopCount, Clauses, AStmt, B);
7393}
7394
Carlo Bertolli9925f152016-06-27 14:55:37 +00007395StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
7396 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007397 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Carlo Bertolli9925f152016-06-27 14:55:37 +00007398 if (!AStmt)
7399 return StmtError();
7400
Alexey Bataeve3727102018-04-18 15:57:46 +00007401 auto *CS = cast<CapturedStmt>(AStmt);
Carlo Bertolli9925f152016-06-27 14:55:37 +00007402 // 1.2.2 OpenMP Language Terminology
7403 // Structured block - An executable statement with a single entry at the
7404 // top and a single exit at the bottom.
7405 // The point of exit cannot be a branch out of the structured block.
7406 // longjmp() and throw() must not violate the entry/exit criteria.
7407 CS->getCapturedDecl()->setNothrow();
Alexey Bataev7f96c372017-11-22 17:19:31 +00007408 for (int ThisCaptureLevel =
7409 getOpenMPCaptureLevels(OMPD_distribute_parallel_for);
7410 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7411 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7412 // 1.2.2 OpenMP Language Terminology
7413 // Structured block - An executable statement with a single entry at the
7414 // top and a single exit at the bottom.
7415 // The point of exit cannot be a branch out of the structured block.
7416 // longjmp() and throw() must not violate the entry/exit criteria.
7417 CS->getCapturedDecl()->setNothrow();
7418 }
Carlo Bertolli9925f152016-06-27 14:55:37 +00007419
7420 OMPLoopDirective::HelperExprs B;
7421 // In presence of clause 'collapse' with number of loops, it will
7422 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007423 unsigned NestedLoopCount = checkOpenMPLoop(
Carlo Bertolli9925f152016-06-27 14:55:37 +00007424 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataev7f96c372017-11-22 17:19:31 +00007425 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Carlo Bertolli9925f152016-06-27 14:55:37 +00007426 VarsWithImplicitDSA, B);
7427 if (NestedLoopCount == 0)
7428 return StmtError();
7429
7430 assert((CurContext->isDependentContext() || B.builtAll()) &&
7431 "omp for loop exprs were not built");
7432
Reid Kleckner87a31802018-03-12 21:43:02 +00007433 setFunctionHasBranchProtectedScope();
Carlo Bertolli9925f152016-06-27 14:55:37 +00007434 return OMPDistributeParallelForDirective::Create(
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00007435 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7436 DSAStack->isCancelRegion());
Carlo Bertolli9925f152016-06-27 14:55:37 +00007437}
7438
Kelvin Li4a39add2016-07-05 05:00:15 +00007439StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
7440 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007441 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li4a39add2016-07-05 05:00:15 +00007442 if (!AStmt)
7443 return StmtError();
7444
Alexey Bataeve3727102018-04-18 15:57:46 +00007445 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li4a39add2016-07-05 05:00:15 +00007446 // 1.2.2 OpenMP Language Terminology
7447 // Structured block - An executable statement with a single entry at the
7448 // top and a single exit at the bottom.
7449 // The point of exit cannot be a branch out of the structured block.
7450 // longjmp() and throw() must not violate the entry/exit criteria.
7451 CS->getCapturedDecl()->setNothrow();
Alexey Bataev974acd62017-11-27 19:38:52 +00007452 for (int ThisCaptureLevel =
7453 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd);
7454 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7455 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7456 // 1.2.2 OpenMP Language Terminology
7457 // Structured block - An executable statement with a single entry at the
7458 // top and a single exit at the bottom.
7459 // The point of exit cannot be a branch out of the structured block.
7460 // longjmp() and throw() must not violate the entry/exit criteria.
7461 CS->getCapturedDecl()->setNothrow();
7462 }
Kelvin Li4a39add2016-07-05 05:00:15 +00007463
7464 OMPLoopDirective::HelperExprs B;
7465 // In presence of clause 'collapse' with number of loops, it will
7466 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007467 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li4a39add2016-07-05 05:00:15 +00007468 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev974acd62017-11-27 19:38:52 +00007469 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li4a39add2016-07-05 05:00:15 +00007470 VarsWithImplicitDSA, B);
7471 if (NestedLoopCount == 0)
7472 return StmtError();
7473
7474 assert((CurContext->isDependentContext() || B.builtAll()) &&
7475 "omp for loop exprs were not built");
7476
Alexey Bataev438388c2017-11-22 18:34:02 +00007477 if (!CurContext->isDependentContext()) {
7478 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007479 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00007480 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7481 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7482 B.NumIterations, *this, CurScope,
7483 DSAStack))
7484 return StmtError();
7485 }
7486 }
7487
Kelvin Lic5609492016-07-15 04:39:07 +00007488 if (checkSimdlenSafelenSpecified(*this, Clauses))
7489 return StmtError();
7490
Reid Kleckner87a31802018-03-12 21:43:02 +00007491 setFunctionHasBranchProtectedScope();
Kelvin Li4a39add2016-07-05 05:00:15 +00007492 return OMPDistributeParallelForSimdDirective::Create(
7493 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7494}
7495
Kelvin Li787f3fc2016-07-06 04:45:38 +00007496StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
7497 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007498 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li787f3fc2016-07-06 04:45:38 +00007499 if (!AStmt)
7500 return StmtError();
7501
Alexey Bataeve3727102018-04-18 15:57:46 +00007502 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li787f3fc2016-07-06 04:45:38 +00007503 // 1.2.2 OpenMP Language Terminology
7504 // Structured block - An executable statement with a single entry at the
7505 // top and a single exit at the bottom.
7506 // The point of exit cannot be a branch out of the structured block.
7507 // longjmp() and throw() must not violate the entry/exit criteria.
7508 CS->getCapturedDecl()->setNothrow();
Alexey Bataev617db5f2017-12-04 15:38:33 +00007509 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd);
7510 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7511 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7512 // 1.2.2 OpenMP Language Terminology
7513 // Structured block - An executable statement with a single entry at the
7514 // top and a single exit at the bottom.
7515 // The point of exit cannot be a branch out of the structured block.
7516 // longjmp() and throw() must not violate the entry/exit criteria.
7517 CS->getCapturedDecl()->setNothrow();
7518 }
Kelvin Li787f3fc2016-07-06 04:45:38 +00007519
7520 OMPLoopDirective::HelperExprs B;
7521 // In presence of clause 'collapse' with number of loops, it will
7522 // define the nested loops number.
7523 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007524 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev617db5f2017-12-04 15:38:33 +00007525 nullptr /*ordered not a clause on distribute*/, CS, *this,
7526 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li787f3fc2016-07-06 04:45:38 +00007527 if (NestedLoopCount == 0)
7528 return StmtError();
7529
7530 assert((CurContext->isDependentContext() || B.builtAll()) &&
7531 "omp for loop exprs were not built");
7532
Alexey Bataev438388c2017-11-22 18:34:02 +00007533 if (!CurContext->isDependentContext()) {
7534 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007535 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00007536 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7537 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7538 B.NumIterations, *this, CurScope,
7539 DSAStack))
7540 return StmtError();
7541 }
7542 }
7543
Kelvin Lic5609492016-07-15 04:39:07 +00007544 if (checkSimdlenSafelenSpecified(*this, Clauses))
7545 return StmtError();
7546
Reid Kleckner87a31802018-03-12 21:43:02 +00007547 setFunctionHasBranchProtectedScope();
Kelvin Li787f3fc2016-07-06 04:45:38 +00007548 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
7549 NestedLoopCount, Clauses, AStmt, B);
7550}
7551
Kelvin Lia579b912016-07-14 02:54:56 +00007552StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
7553 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007554 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Lia579b912016-07-14 02:54:56 +00007555 if (!AStmt)
7556 return StmtError();
7557
Alexey Bataeve3727102018-04-18 15:57:46 +00007558 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Lia579b912016-07-14 02:54:56 +00007559 // 1.2.2 OpenMP Language Terminology
7560 // Structured block - An executable statement with a single entry at the
7561 // top and a single exit at the bottom.
7562 // The point of exit cannot be a branch out of the structured block.
7563 // longjmp() and throw() must not violate the entry/exit criteria.
7564 CS->getCapturedDecl()->setNothrow();
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007565 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
7566 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7567 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7568 // 1.2.2 OpenMP Language Terminology
7569 // Structured block - An executable statement with a single entry at the
7570 // top and a single exit at the bottom.
7571 // The point of exit cannot be a branch out of the structured block.
7572 // longjmp() and throw() must not violate the entry/exit criteria.
7573 CS->getCapturedDecl()->setNothrow();
7574 }
Kelvin Lia579b912016-07-14 02:54:56 +00007575
7576 OMPLoopDirective::HelperExprs B;
7577 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7578 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007579 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Lia579b912016-07-14 02:54:56 +00007580 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007581 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Kelvin Lia579b912016-07-14 02:54:56 +00007582 VarsWithImplicitDSA, B);
7583 if (NestedLoopCount == 0)
7584 return StmtError();
7585
7586 assert((CurContext->isDependentContext() || B.builtAll()) &&
7587 "omp target parallel for simd loop exprs were not built");
7588
7589 if (!CurContext->isDependentContext()) {
7590 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007591 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007592 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Lia579b912016-07-14 02:54:56 +00007593 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7594 B.NumIterations, *this, CurScope,
7595 DSAStack))
7596 return StmtError();
7597 }
7598 }
Kelvin Lic5609492016-07-15 04:39:07 +00007599 if (checkSimdlenSafelenSpecified(*this, Clauses))
Kelvin Lia579b912016-07-14 02:54:56 +00007600 return StmtError();
7601
Reid Kleckner87a31802018-03-12 21:43:02 +00007602 setFunctionHasBranchProtectedScope();
Kelvin Lia579b912016-07-14 02:54:56 +00007603 return OMPTargetParallelForSimdDirective::Create(
7604 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7605}
7606
Kelvin Li986330c2016-07-20 22:57:10 +00007607StmtResult Sema::ActOnOpenMPTargetSimdDirective(
7608 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007609 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li986330c2016-07-20 22:57:10 +00007610 if (!AStmt)
7611 return StmtError();
7612
Alexey Bataeve3727102018-04-18 15:57:46 +00007613 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li986330c2016-07-20 22:57:10 +00007614 // 1.2.2 OpenMP Language Terminology
7615 // Structured block - An executable statement with a single entry at the
7616 // top and a single exit at the bottom.
7617 // The point of exit cannot be a branch out of the structured block.
7618 // longjmp() and throw() must not violate the entry/exit criteria.
7619 CS->getCapturedDecl()->setNothrow();
Alexey Bataevf8365372017-11-17 17:57:25 +00007620 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd);
7621 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7622 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7623 // 1.2.2 OpenMP Language Terminology
7624 // Structured block - An executable statement with a single entry at the
7625 // top and a single exit at the bottom.
7626 // The point of exit cannot be a branch out of the structured block.
7627 // longjmp() and throw() must not violate the entry/exit criteria.
7628 CS->getCapturedDecl()->setNothrow();
7629 }
7630
Kelvin Li986330c2016-07-20 22:57:10 +00007631 OMPLoopDirective::HelperExprs B;
7632 // In presence of clause 'collapse' with number of loops, it will define the
7633 // nested loops number.
David Majnemer9d168222016-08-05 17:44:54 +00007634 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007635 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
Alexey Bataevf8365372017-11-17 17:57:25 +00007636 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Kelvin Li986330c2016-07-20 22:57:10 +00007637 VarsWithImplicitDSA, B);
7638 if (NestedLoopCount == 0)
7639 return StmtError();
7640
7641 assert((CurContext->isDependentContext() || B.builtAll()) &&
7642 "omp target simd loop exprs were not built");
7643
7644 if (!CurContext->isDependentContext()) {
7645 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007646 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007647 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Li986330c2016-07-20 22:57:10 +00007648 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7649 B.NumIterations, *this, CurScope,
7650 DSAStack))
7651 return StmtError();
7652 }
7653 }
7654
7655 if (checkSimdlenSafelenSpecified(*this, Clauses))
7656 return StmtError();
7657
Reid Kleckner87a31802018-03-12 21:43:02 +00007658 setFunctionHasBranchProtectedScope();
Kelvin Li986330c2016-07-20 22:57:10 +00007659 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
7660 NestedLoopCount, Clauses, AStmt, B);
7661}
7662
Kelvin Li02532872016-08-05 14:37:37 +00007663StmtResult Sema::ActOnOpenMPTeamsDistributeDirective(
7664 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007665 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li02532872016-08-05 14:37:37 +00007666 if (!AStmt)
7667 return StmtError();
7668
Alexey Bataeve3727102018-04-18 15:57:46 +00007669 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li02532872016-08-05 14:37:37 +00007670 // 1.2.2 OpenMP Language Terminology
7671 // Structured block - An executable statement with a single entry at the
7672 // top and a single exit at the bottom.
7673 // The point of exit cannot be a branch out of the structured block.
7674 // longjmp() and throw() must not violate the entry/exit criteria.
7675 CS->getCapturedDecl()->setNothrow();
Alexey Bataev95c6dd42017-11-29 15:14:16 +00007676 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute);
7677 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7678 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7679 // 1.2.2 OpenMP Language Terminology
7680 // Structured block - An executable statement with a single entry at the
7681 // top and a single exit at the bottom.
7682 // The point of exit cannot be a branch out of the structured block.
7683 // longjmp() and throw() must not violate the entry/exit criteria.
7684 CS->getCapturedDecl()->setNothrow();
7685 }
Kelvin Li02532872016-08-05 14:37:37 +00007686
7687 OMPLoopDirective::HelperExprs B;
7688 // In presence of clause 'collapse' with number of loops, it will
7689 // define the nested loops number.
7690 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007691 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses),
Alexey Bataev95c6dd42017-11-29 15:14:16 +00007692 nullptr /*ordered not a clause on distribute*/, CS, *this,
7693 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li02532872016-08-05 14:37:37 +00007694 if (NestedLoopCount == 0)
7695 return StmtError();
7696
7697 assert((CurContext->isDependentContext() || B.builtAll()) &&
7698 "omp teams distribute loop exprs were not built");
7699
Reid Kleckner87a31802018-03-12 21:43:02 +00007700 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007701
7702 DSAStack->setParentTeamsRegionLoc(StartLoc);
7703
David Majnemer9d168222016-08-05 17:44:54 +00007704 return OMPTeamsDistributeDirective::Create(
7705 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Kelvin Li02532872016-08-05 14:37:37 +00007706}
7707
Kelvin Li4e325f72016-10-25 12:50:55 +00007708StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective(
7709 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007710 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li4e325f72016-10-25 12:50:55 +00007711 if (!AStmt)
7712 return StmtError();
7713
Alexey Bataeve3727102018-04-18 15:57:46 +00007714 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li4e325f72016-10-25 12:50:55 +00007715 // 1.2.2 OpenMP Language Terminology
7716 // Structured block - An executable statement with a single entry at the
7717 // top and a single exit at the bottom.
7718 // The point of exit cannot be a branch out of the structured block.
7719 // longjmp() and throw() must not violate the entry/exit criteria.
7720 CS->getCapturedDecl()->setNothrow();
Alexey Bataev999277a2017-12-06 14:31:09 +00007721 for (int ThisCaptureLevel =
7722 getOpenMPCaptureLevels(OMPD_teams_distribute_simd);
7723 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7724 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7725 // 1.2.2 OpenMP Language Terminology
7726 // Structured block - An executable statement with a single entry at the
7727 // top and a single exit at the bottom.
7728 // The point of exit cannot be a branch out of the structured block.
7729 // longjmp() and throw() must not violate the entry/exit criteria.
7730 CS->getCapturedDecl()->setNothrow();
7731 }
7732
Kelvin Li4e325f72016-10-25 12:50:55 +00007733
7734 OMPLoopDirective::HelperExprs B;
7735 // In presence of clause 'collapse' with number of loops, it will
7736 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007737 unsigned NestedLoopCount = checkOpenMPLoop(
Samuel Antao4c8035b2016-12-12 18:00:20 +00007738 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev999277a2017-12-06 14:31:09 +00007739 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Samuel Antao4c8035b2016-12-12 18:00:20 +00007740 VarsWithImplicitDSA, B);
Kelvin Li4e325f72016-10-25 12:50:55 +00007741
7742 if (NestedLoopCount == 0)
7743 return StmtError();
7744
7745 assert((CurContext->isDependentContext() || B.builtAll()) &&
7746 "omp teams distribute simd loop exprs were not built");
7747
7748 if (!CurContext->isDependentContext()) {
7749 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007750 for (OMPClause *C : Clauses) {
Kelvin Li4e325f72016-10-25 12:50:55 +00007751 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7752 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7753 B.NumIterations, *this, CurScope,
7754 DSAStack))
7755 return StmtError();
7756 }
7757 }
7758
7759 if (checkSimdlenSafelenSpecified(*this, Clauses))
7760 return StmtError();
7761
Reid Kleckner87a31802018-03-12 21:43:02 +00007762 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007763
7764 DSAStack->setParentTeamsRegionLoc(StartLoc);
7765
Kelvin Li4e325f72016-10-25 12:50:55 +00007766 return OMPTeamsDistributeSimdDirective::Create(
7767 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7768}
7769
Kelvin Li579e41c2016-11-30 23:51:03 +00007770StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective(
7771 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007772 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li579e41c2016-11-30 23:51:03 +00007773 if (!AStmt)
7774 return StmtError();
7775
Alexey Bataeve3727102018-04-18 15:57:46 +00007776 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li579e41c2016-11-30 23:51:03 +00007777 // 1.2.2 OpenMP Language Terminology
7778 // Structured block - An executable statement with a single entry at the
7779 // top and a single exit at the bottom.
7780 // The point of exit cannot be a branch out of the structured block.
7781 // longjmp() and throw() must not violate the entry/exit criteria.
7782 CS->getCapturedDecl()->setNothrow();
7783
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00007784 for (int ThisCaptureLevel =
7785 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd);
7786 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7787 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7788 // 1.2.2 OpenMP Language Terminology
7789 // Structured block - An executable statement with a single entry at the
7790 // top and a single exit at the bottom.
7791 // The point of exit cannot be a branch out of the structured block.
7792 // longjmp() and throw() must not violate the entry/exit criteria.
7793 CS->getCapturedDecl()->setNothrow();
7794 }
7795
Kelvin Li579e41c2016-11-30 23:51:03 +00007796 OMPLoopDirective::HelperExprs B;
7797 // In presence of clause 'collapse' with number of loops, it will
7798 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007799 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li579e41c2016-11-30 23:51:03 +00007800 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00007801 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li579e41c2016-11-30 23:51:03 +00007802 VarsWithImplicitDSA, B);
7803
7804 if (NestedLoopCount == 0)
7805 return StmtError();
7806
7807 assert((CurContext->isDependentContext() || B.builtAll()) &&
7808 "omp for loop exprs were not built");
7809
7810 if (!CurContext->isDependentContext()) {
7811 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007812 for (OMPClause *C : Clauses) {
Kelvin Li579e41c2016-11-30 23:51:03 +00007813 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7814 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7815 B.NumIterations, *this, CurScope,
7816 DSAStack))
7817 return StmtError();
7818 }
7819 }
7820
7821 if (checkSimdlenSafelenSpecified(*this, Clauses))
7822 return StmtError();
7823
Reid Kleckner87a31802018-03-12 21:43:02 +00007824 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007825
7826 DSAStack->setParentTeamsRegionLoc(StartLoc);
7827
Kelvin Li579e41c2016-11-30 23:51:03 +00007828 return OMPTeamsDistributeParallelForSimdDirective::Create(
7829 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7830}
7831
Kelvin Li7ade93f2016-12-09 03:24:30 +00007832StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective(
7833 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007834 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li7ade93f2016-12-09 03:24:30 +00007835 if (!AStmt)
7836 return StmtError();
7837
Alexey Bataeve3727102018-04-18 15:57:46 +00007838 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li7ade93f2016-12-09 03:24:30 +00007839 // 1.2.2 OpenMP Language Terminology
7840 // Structured block - An executable statement with a single entry at the
7841 // top and a single exit at the bottom.
7842 // The point of exit cannot be a branch out of the structured block.
7843 // longjmp() and throw() must not violate the entry/exit criteria.
7844 CS->getCapturedDecl()->setNothrow();
7845
Carlo Bertolli62fae152017-11-20 20:46:39 +00007846 for (int ThisCaptureLevel =
7847 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for);
7848 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7849 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7850 // 1.2.2 OpenMP Language Terminology
7851 // Structured block - An executable statement with a single entry at the
7852 // top and a single exit at the bottom.
7853 // The point of exit cannot be a branch out of the structured block.
7854 // longjmp() and throw() must not violate the entry/exit criteria.
7855 CS->getCapturedDecl()->setNothrow();
7856 }
7857
Kelvin Li7ade93f2016-12-09 03:24:30 +00007858 OMPLoopDirective::HelperExprs B;
7859 // In presence of clause 'collapse' with number of loops, it will
7860 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007861 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li7ade93f2016-12-09 03:24:30 +00007862 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
Carlo Bertolli62fae152017-11-20 20:46:39 +00007863 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li7ade93f2016-12-09 03:24:30 +00007864 VarsWithImplicitDSA, B);
7865
7866 if (NestedLoopCount == 0)
7867 return StmtError();
7868
7869 assert((CurContext->isDependentContext() || B.builtAll()) &&
7870 "omp for loop exprs were not built");
7871
Reid Kleckner87a31802018-03-12 21:43:02 +00007872 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007873
7874 DSAStack->setParentTeamsRegionLoc(StartLoc);
7875
Kelvin Li7ade93f2016-12-09 03:24:30 +00007876 return OMPTeamsDistributeParallelForDirective::Create(
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00007877 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7878 DSAStack->isCancelRegion());
Kelvin Li7ade93f2016-12-09 03:24:30 +00007879}
7880
Kelvin Libf594a52016-12-17 05:48:59 +00007881StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses,
7882 Stmt *AStmt,
7883 SourceLocation StartLoc,
7884 SourceLocation EndLoc) {
7885 if (!AStmt)
7886 return StmtError();
7887
Alexey Bataeve3727102018-04-18 15:57:46 +00007888 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Libf594a52016-12-17 05:48:59 +00007889 // 1.2.2 OpenMP Language Terminology
7890 // Structured block - An executable statement with a single entry at the
7891 // top and a single exit at the bottom.
7892 // The point of exit cannot be a branch out of the structured block.
7893 // longjmp() and throw() must not violate the entry/exit criteria.
7894 CS->getCapturedDecl()->setNothrow();
7895
Alexey Bataevf9fc42e2017-11-22 14:25:55 +00007896 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams);
7897 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7898 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7899 // 1.2.2 OpenMP Language Terminology
7900 // Structured block - An executable statement with a single entry at the
7901 // top and a single exit at the bottom.
7902 // The point of exit cannot be a branch out of the structured block.
7903 // longjmp() and throw() must not violate the entry/exit criteria.
7904 CS->getCapturedDecl()->setNothrow();
7905 }
Reid Kleckner87a31802018-03-12 21:43:02 +00007906 setFunctionHasBranchProtectedScope();
Kelvin Libf594a52016-12-17 05:48:59 +00007907
7908 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses,
7909 AStmt);
7910}
7911
Kelvin Li83c451e2016-12-25 04:52:54 +00007912StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective(
7913 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007914 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li83c451e2016-12-25 04:52:54 +00007915 if (!AStmt)
7916 return StmtError();
7917
Alexey Bataeve3727102018-04-18 15:57:46 +00007918 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li83c451e2016-12-25 04:52:54 +00007919 // 1.2.2 OpenMP Language Terminology
7920 // Structured block - An executable statement with a single entry at the
7921 // top and a single exit at the bottom.
7922 // The point of exit cannot be a branch out of the structured block.
7923 // longjmp() and throw() must not violate the entry/exit criteria.
7924 CS->getCapturedDecl()->setNothrow();
Alexey Bataevdfa430f2017-12-08 15:03:50 +00007925 for (int ThisCaptureLevel =
7926 getOpenMPCaptureLevels(OMPD_target_teams_distribute);
7927 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7928 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7929 // 1.2.2 OpenMP Language Terminology
7930 // Structured block - An executable statement with a single entry at the
7931 // top and a single exit at the bottom.
7932 // The point of exit cannot be a branch out of the structured block.
7933 // longjmp() and throw() must not violate the entry/exit criteria.
7934 CS->getCapturedDecl()->setNothrow();
7935 }
Kelvin Li83c451e2016-12-25 04:52:54 +00007936
7937 OMPLoopDirective::HelperExprs B;
7938 // In presence of clause 'collapse' with number of loops, it will
7939 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007940 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataevdfa430f2017-12-08 15:03:50 +00007941 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses),
7942 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li83c451e2016-12-25 04:52:54 +00007943 VarsWithImplicitDSA, B);
7944 if (NestedLoopCount == 0)
7945 return StmtError();
7946
7947 assert((CurContext->isDependentContext() || B.builtAll()) &&
7948 "omp target teams distribute loop exprs were not built");
7949
Reid Kleckner87a31802018-03-12 21:43:02 +00007950 setFunctionHasBranchProtectedScope();
Kelvin Li83c451e2016-12-25 04:52:54 +00007951 return OMPTargetTeamsDistributeDirective::Create(
7952 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7953}
7954
Kelvin Li80e8f562016-12-29 22:16:30 +00007955StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective(
7956 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007957 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li80e8f562016-12-29 22:16:30 +00007958 if (!AStmt)
7959 return StmtError();
7960
Alexey Bataeve3727102018-04-18 15:57:46 +00007961 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li80e8f562016-12-29 22:16:30 +00007962 // 1.2.2 OpenMP Language Terminology
7963 // Structured block - An executable statement with a single entry at the
7964 // top and a single exit at the bottom.
7965 // The point of exit cannot be a branch out of the structured block.
7966 // longjmp() and throw() must not violate the entry/exit criteria.
7967 CS->getCapturedDecl()->setNothrow();
Carlo Bertolli52978c32018-01-03 21:12:44 +00007968 for (int ThisCaptureLevel =
7969 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for);
7970 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7971 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7972 // 1.2.2 OpenMP Language Terminology
7973 // Structured block - An executable statement with a single entry at the
7974 // top and a single exit at the bottom.
7975 // The point of exit cannot be a branch out of the structured block.
7976 // longjmp() and throw() must not violate the entry/exit criteria.
7977 CS->getCapturedDecl()->setNothrow();
7978 }
7979
Kelvin Li80e8f562016-12-29 22:16:30 +00007980 OMPLoopDirective::HelperExprs B;
7981 // In presence of clause 'collapse' with number of loops, it will
7982 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007983 unsigned NestedLoopCount = checkOpenMPLoop(
Carlo Bertolli52978c32018-01-03 21:12:44 +00007984 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
7985 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li80e8f562016-12-29 22:16:30 +00007986 VarsWithImplicitDSA, B);
7987 if (NestedLoopCount == 0)
7988 return StmtError();
7989
7990 assert((CurContext->isDependentContext() || B.builtAll()) &&
7991 "omp target teams distribute parallel for loop exprs were not built");
7992
Alexey Bataev647dd842018-01-15 20:59:40 +00007993 if (!CurContext->isDependentContext()) {
7994 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007995 for (OMPClause *C : Clauses) {
Alexey Bataev647dd842018-01-15 20:59:40 +00007996 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7997 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7998 B.NumIterations, *this, CurScope,
7999 DSAStack))
8000 return StmtError();
8001 }
8002 }
8003
Reid Kleckner87a31802018-03-12 21:43:02 +00008004 setFunctionHasBranchProtectedScope();
Kelvin Li80e8f562016-12-29 22:16:30 +00008005 return OMPTargetTeamsDistributeParallelForDirective::Create(
Alexey Bataev16e79882017-11-22 21:12:03 +00008006 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
8007 DSAStack->isCancelRegion());
Kelvin Li80e8f562016-12-29 22:16:30 +00008008}
8009
Kelvin Li1851df52017-01-03 05:23:48 +00008010StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
8011 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00008012 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li1851df52017-01-03 05:23:48 +00008013 if (!AStmt)
8014 return StmtError();
8015
Alexey Bataeve3727102018-04-18 15:57:46 +00008016 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li1851df52017-01-03 05:23:48 +00008017 // 1.2.2 OpenMP Language Terminology
8018 // Structured block - An executable statement with a single entry at the
8019 // top and a single exit at the bottom.
8020 // The point of exit cannot be a branch out of the structured block.
8021 // longjmp() and throw() must not violate the entry/exit criteria.
8022 CS->getCapturedDecl()->setNothrow();
Alexey Bataev647dd842018-01-15 20:59:40 +00008023 for (int ThisCaptureLevel = getOpenMPCaptureLevels(
8024 OMPD_target_teams_distribute_parallel_for_simd);
8025 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8026 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8027 // 1.2.2 OpenMP Language Terminology
8028 // Structured block - An executable statement with a single entry at the
8029 // top and a single exit at the bottom.
8030 // The point of exit cannot be a branch out of the structured block.
8031 // longjmp() and throw() must not violate the entry/exit criteria.
8032 CS->getCapturedDecl()->setNothrow();
8033 }
Kelvin Li1851df52017-01-03 05:23:48 +00008034
8035 OMPLoopDirective::HelperExprs B;
8036 // In presence of clause 'collapse' with number of loops, it will
8037 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00008038 unsigned NestedLoopCount =
8039 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd,
Alexey Bataev647dd842018-01-15 20:59:40 +00008040 getCollapseNumberExpr(Clauses),
8041 nullptr /*ordered not a clause on distribute*/, CS, *this,
8042 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li1851df52017-01-03 05:23:48 +00008043 if (NestedLoopCount == 0)
8044 return StmtError();
8045
8046 assert((CurContext->isDependentContext() || B.builtAll()) &&
8047 "omp target teams distribute parallel for simd loop exprs were not "
8048 "built");
8049
8050 if (!CurContext->isDependentContext()) {
8051 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00008052 for (OMPClause *C : Clauses) {
Kelvin Li1851df52017-01-03 05:23:48 +00008053 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8054 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8055 B.NumIterations, *this, CurScope,
8056 DSAStack))
8057 return StmtError();
8058 }
8059 }
8060
Alexey Bataev438388c2017-11-22 18:34:02 +00008061 if (checkSimdlenSafelenSpecified(*this, Clauses))
8062 return StmtError();
8063
Reid Kleckner87a31802018-03-12 21:43:02 +00008064 setFunctionHasBranchProtectedScope();
Kelvin Li1851df52017-01-03 05:23:48 +00008065 return OMPTargetTeamsDistributeParallelForSimdDirective::Create(
8066 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8067}
8068
Kelvin Lida681182017-01-10 18:08:18 +00008069StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective(
8070 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00008071 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Lida681182017-01-10 18:08:18 +00008072 if (!AStmt)
8073 return StmtError();
8074
8075 auto *CS = cast<CapturedStmt>(AStmt);
8076 // 1.2.2 OpenMP Language Terminology
8077 // Structured block - An executable statement with a single entry at the
8078 // top and a single exit at the bottom.
8079 // The point of exit cannot be a branch out of the structured block.
8080 // longjmp() and throw() must not violate the entry/exit criteria.
8081 CS->getCapturedDecl()->setNothrow();
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00008082 for (int ThisCaptureLevel =
8083 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd);
8084 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8085 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8086 // 1.2.2 OpenMP Language Terminology
8087 // Structured block - An executable statement with a single entry at the
8088 // top and a single exit at the bottom.
8089 // The point of exit cannot be a branch out of the structured block.
8090 // longjmp() and throw() must not violate the entry/exit criteria.
8091 CS->getCapturedDecl()->setNothrow();
8092 }
Kelvin Lida681182017-01-10 18:08:18 +00008093
8094 OMPLoopDirective::HelperExprs B;
8095 // In presence of clause 'collapse' with number of loops, it will
8096 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00008097 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Lida681182017-01-10 18:08:18 +00008098 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00008099 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Lida681182017-01-10 18:08:18 +00008100 VarsWithImplicitDSA, B);
8101 if (NestedLoopCount == 0)
8102 return StmtError();
8103
8104 assert((CurContext->isDependentContext() || B.builtAll()) &&
8105 "omp target teams distribute simd loop exprs were not built");
8106
Alexey Bataev438388c2017-11-22 18:34:02 +00008107 if (!CurContext->isDependentContext()) {
8108 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00008109 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00008110 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8111 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8112 B.NumIterations, *this, CurScope,
8113 DSAStack))
8114 return StmtError();
8115 }
8116 }
8117
8118 if (checkSimdlenSafelenSpecified(*this, Clauses))
8119 return StmtError();
8120
Reid Kleckner87a31802018-03-12 21:43:02 +00008121 setFunctionHasBranchProtectedScope();
Kelvin Lida681182017-01-10 18:08:18 +00008122 return OMPTargetTeamsDistributeSimdDirective::Create(
8123 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8124}
8125
Alexey Bataeved09d242014-05-28 05:53:51 +00008126OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008127 SourceLocation StartLoc,
8128 SourceLocation LParenLoc,
8129 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008130 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008131 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00008132 case OMPC_final:
8133 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
8134 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00008135 case OMPC_num_threads:
8136 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
8137 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00008138 case OMPC_safelen:
8139 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
8140 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00008141 case OMPC_simdlen:
8142 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
8143 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00008144 case OMPC_collapse:
8145 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
8146 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00008147 case OMPC_ordered:
8148 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
8149 break;
Michael Wonge710d542015-08-07 16:16:36 +00008150 case OMPC_device:
8151 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
8152 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00008153 case OMPC_num_teams:
8154 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
8155 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008156 case OMPC_thread_limit:
8157 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
8158 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00008159 case OMPC_priority:
8160 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
8161 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008162 case OMPC_grainsize:
8163 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
8164 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00008165 case OMPC_num_tasks:
8166 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
8167 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00008168 case OMPC_hint:
8169 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
8170 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008171 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008172 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008173 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008174 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008175 case OMPC_private:
8176 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00008177 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008178 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00008179 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008180 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008181 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00008182 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008183 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008184 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008185 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00008186 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008187 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008188 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008189 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00008190 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008191 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008192 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008193 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008194 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008195 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008196 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00008197 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008198 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008199 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00008200 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008201 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008202 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008203 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008204 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00008205 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00008206 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00008207 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00008208 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00008209 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00008210 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00008211 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00008212 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00008213 case OMPC_atomic_default_mem_order:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008214 llvm_unreachable("Clause is not allowed.");
8215 }
8216 return Res;
8217}
8218
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008219// An OpenMP directive such as 'target parallel' has two captured regions:
8220// for the 'target' and 'parallel' respectively. This function returns
8221// the region in which to capture expressions associated with a clause.
8222// A return value of OMPD_unknown signifies that the expression should not
8223// be captured.
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008224static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
8225 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
8226 OpenMPDirectiveKind NameModifier = OMPD_unknown) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008227 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008228 switch (CKind) {
8229 case OMPC_if:
8230 switch (DKind) {
8231 case OMPD_target_parallel:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008232 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00008233 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008234 // If this clause applies to the nested 'parallel' region, capture within
8235 // the 'target' region, otherwise do not capture.
8236 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
8237 CaptureRegion = OMPD_target;
8238 break;
Carlo Bertolli52978c32018-01-03 21:12:44 +00008239 case OMPD_target_teams_distribute_parallel_for:
8240 case OMPD_target_teams_distribute_parallel_for_simd:
8241 // If this clause applies to the nested 'parallel' region, capture within
8242 // the 'teams' region, otherwise do not capture.
8243 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
8244 CaptureRegion = OMPD_teams;
8245 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +00008246 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008247 case OMPD_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008248 CaptureRegion = OMPD_teams;
8249 break;
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008250 case OMPD_target_update:
Alexey Bataevfab20e42017-12-27 18:49:38 +00008251 case OMPD_target_enter_data:
8252 case OMPD_target_exit_data:
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008253 CaptureRegion = OMPD_task;
8254 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008255 case OMPD_cancel:
8256 case OMPD_parallel:
8257 case OMPD_parallel_sections:
8258 case OMPD_parallel_for:
8259 case OMPD_parallel_for_simd:
8260 case OMPD_target:
8261 case OMPD_target_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008262 case OMPD_target_teams:
8263 case OMPD_target_teams_distribute:
8264 case OMPD_target_teams_distribute_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008265 case OMPD_distribute_parallel_for:
8266 case OMPD_distribute_parallel_for_simd:
8267 case OMPD_task:
8268 case OMPD_taskloop:
8269 case OMPD_taskloop_simd:
8270 case OMPD_target_data:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008271 // Do not capture if-clause expressions.
8272 break;
8273 case OMPD_threadprivate:
8274 case OMPD_taskyield:
8275 case OMPD_barrier:
8276 case OMPD_taskwait:
8277 case OMPD_cancellation_point:
8278 case OMPD_flush:
8279 case OMPD_declare_reduction:
8280 case OMPD_declare_simd:
8281 case OMPD_declare_target:
8282 case OMPD_end_declare_target:
8283 case OMPD_teams:
8284 case OMPD_simd:
8285 case OMPD_for:
8286 case OMPD_for_simd:
8287 case OMPD_sections:
8288 case OMPD_section:
8289 case OMPD_single:
8290 case OMPD_master:
8291 case OMPD_critical:
8292 case OMPD_taskgroup:
8293 case OMPD_distribute:
8294 case OMPD_ordered:
8295 case OMPD_atomic:
8296 case OMPD_distribute_simd:
8297 case OMPD_teams_distribute:
8298 case OMPD_teams_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008299 case OMPD_requires:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008300 llvm_unreachable("Unexpected OpenMP directive with if-clause");
8301 case OMPD_unknown:
8302 llvm_unreachable("Unknown OpenMP directive");
8303 }
8304 break;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008305 case OMPC_num_threads:
8306 switch (DKind) {
8307 case OMPD_target_parallel:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008308 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00008309 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008310 CaptureRegion = OMPD_target;
8311 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +00008312 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008313 case OMPD_teams_distribute_parallel_for_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008314 case OMPD_target_teams_distribute_parallel_for:
8315 case OMPD_target_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008316 CaptureRegion = OMPD_teams;
8317 break;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008318 case OMPD_parallel:
8319 case OMPD_parallel_sections:
8320 case OMPD_parallel_for:
8321 case OMPD_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008322 case OMPD_distribute_parallel_for:
8323 case OMPD_distribute_parallel_for_simd:
8324 // Do not capture num_threads-clause expressions.
8325 break;
8326 case OMPD_target_data:
8327 case OMPD_target_enter_data:
8328 case OMPD_target_exit_data:
8329 case OMPD_target_update:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008330 case OMPD_target:
8331 case OMPD_target_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008332 case OMPD_target_teams:
8333 case OMPD_target_teams_distribute:
8334 case OMPD_target_teams_distribute_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008335 case OMPD_cancel:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008336 case OMPD_task:
8337 case OMPD_taskloop:
8338 case OMPD_taskloop_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008339 case OMPD_threadprivate:
8340 case OMPD_taskyield:
8341 case OMPD_barrier:
8342 case OMPD_taskwait:
8343 case OMPD_cancellation_point:
8344 case OMPD_flush:
8345 case OMPD_declare_reduction:
8346 case OMPD_declare_simd:
8347 case OMPD_declare_target:
8348 case OMPD_end_declare_target:
8349 case OMPD_teams:
8350 case OMPD_simd:
8351 case OMPD_for:
8352 case OMPD_for_simd:
8353 case OMPD_sections:
8354 case OMPD_section:
8355 case OMPD_single:
8356 case OMPD_master:
8357 case OMPD_critical:
8358 case OMPD_taskgroup:
8359 case OMPD_distribute:
8360 case OMPD_ordered:
8361 case OMPD_atomic:
8362 case OMPD_distribute_simd:
8363 case OMPD_teams_distribute:
8364 case OMPD_teams_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008365 case OMPD_requires:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008366 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause");
8367 case OMPD_unknown:
8368 llvm_unreachable("Unknown OpenMP directive");
8369 }
8370 break;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008371 case OMPC_num_teams:
8372 switch (DKind) {
8373 case OMPD_target_teams:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008374 case OMPD_target_teams_distribute:
8375 case OMPD_target_teams_distribute_simd:
8376 case OMPD_target_teams_distribute_parallel_for:
8377 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008378 CaptureRegion = OMPD_target;
8379 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008380 case OMPD_teams_distribute_parallel_for:
8381 case OMPD_teams_distribute_parallel_for_simd:
8382 case OMPD_teams:
8383 case OMPD_teams_distribute:
8384 case OMPD_teams_distribute_simd:
8385 // Do not capture num_teams-clause expressions.
8386 break;
8387 case OMPD_distribute_parallel_for:
8388 case OMPD_distribute_parallel_for_simd:
8389 case OMPD_task:
8390 case OMPD_taskloop:
8391 case OMPD_taskloop_simd:
8392 case OMPD_target_data:
8393 case OMPD_target_enter_data:
8394 case OMPD_target_exit_data:
8395 case OMPD_target_update:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008396 case OMPD_cancel:
8397 case OMPD_parallel:
8398 case OMPD_parallel_sections:
8399 case OMPD_parallel_for:
8400 case OMPD_parallel_for_simd:
8401 case OMPD_target:
8402 case OMPD_target_simd:
8403 case OMPD_target_parallel:
8404 case OMPD_target_parallel_for:
8405 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008406 case OMPD_threadprivate:
8407 case OMPD_taskyield:
8408 case OMPD_barrier:
8409 case OMPD_taskwait:
8410 case OMPD_cancellation_point:
8411 case OMPD_flush:
8412 case OMPD_declare_reduction:
8413 case OMPD_declare_simd:
8414 case OMPD_declare_target:
8415 case OMPD_end_declare_target:
8416 case OMPD_simd:
8417 case OMPD_for:
8418 case OMPD_for_simd:
8419 case OMPD_sections:
8420 case OMPD_section:
8421 case OMPD_single:
8422 case OMPD_master:
8423 case OMPD_critical:
8424 case OMPD_taskgroup:
8425 case OMPD_distribute:
8426 case OMPD_ordered:
8427 case OMPD_atomic:
8428 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008429 case OMPD_requires:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008430 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
8431 case OMPD_unknown:
8432 llvm_unreachable("Unknown OpenMP directive");
8433 }
8434 break;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008435 case OMPC_thread_limit:
8436 switch (DKind) {
8437 case OMPD_target_teams:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008438 case OMPD_target_teams_distribute:
8439 case OMPD_target_teams_distribute_simd:
8440 case OMPD_target_teams_distribute_parallel_for:
8441 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008442 CaptureRegion = OMPD_target;
8443 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008444 case OMPD_teams_distribute_parallel_for:
8445 case OMPD_teams_distribute_parallel_for_simd:
8446 case OMPD_teams:
8447 case OMPD_teams_distribute:
8448 case OMPD_teams_distribute_simd:
8449 // Do not capture thread_limit-clause expressions.
8450 break;
8451 case OMPD_distribute_parallel_for:
8452 case OMPD_distribute_parallel_for_simd:
8453 case OMPD_task:
8454 case OMPD_taskloop:
8455 case OMPD_taskloop_simd:
8456 case OMPD_target_data:
8457 case OMPD_target_enter_data:
8458 case OMPD_target_exit_data:
8459 case OMPD_target_update:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008460 case OMPD_cancel:
8461 case OMPD_parallel:
8462 case OMPD_parallel_sections:
8463 case OMPD_parallel_for:
8464 case OMPD_parallel_for_simd:
8465 case OMPD_target:
8466 case OMPD_target_simd:
8467 case OMPD_target_parallel:
8468 case OMPD_target_parallel_for:
8469 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008470 case OMPD_threadprivate:
8471 case OMPD_taskyield:
8472 case OMPD_barrier:
8473 case OMPD_taskwait:
8474 case OMPD_cancellation_point:
8475 case OMPD_flush:
8476 case OMPD_declare_reduction:
8477 case OMPD_declare_simd:
8478 case OMPD_declare_target:
8479 case OMPD_end_declare_target:
8480 case OMPD_simd:
8481 case OMPD_for:
8482 case OMPD_for_simd:
8483 case OMPD_sections:
8484 case OMPD_section:
8485 case OMPD_single:
8486 case OMPD_master:
8487 case OMPD_critical:
8488 case OMPD_taskgroup:
8489 case OMPD_distribute:
8490 case OMPD_ordered:
8491 case OMPD_atomic:
8492 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008493 case OMPD_requires:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008494 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause");
8495 case OMPD_unknown:
8496 llvm_unreachable("Unknown OpenMP directive");
8497 }
8498 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008499 case OMPC_schedule:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008500 switch (DKind) {
Alexey Bataev2ba67042017-11-28 21:11:44 +00008501 case OMPD_parallel_for:
8502 case OMPD_parallel_for_simd:
Alexey Bataev7f96c372017-11-22 17:19:31 +00008503 case OMPD_distribute_parallel_for:
Alexey Bataev974acd62017-11-27 19:38:52 +00008504 case OMPD_distribute_parallel_for_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008505 case OMPD_teams_distribute_parallel_for:
8506 case OMPD_teams_distribute_parallel_for_simd:
8507 case OMPD_target_parallel_for:
8508 case OMPD_target_parallel_for_simd:
8509 case OMPD_target_teams_distribute_parallel_for:
8510 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataev7f96c372017-11-22 17:19:31 +00008511 CaptureRegion = OMPD_parallel;
8512 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008513 case OMPD_for:
8514 case OMPD_for_simd:
8515 // Do not capture schedule-clause expressions.
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008516 break;
8517 case OMPD_task:
8518 case OMPD_taskloop:
8519 case OMPD_taskloop_simd:
8520 case OMPD_target_data:
8521 case OMPD_target_enter_data:
8522 case OMPD_target_exit_data:
8523 case OMPD_target_update:
8524 case OMPD_teams:
8525 case OMPD_teams_distribute:
8526 case OMPD_teams_distribute_simd:
8527 case OMPD_target_teams_distribute:
8528 case OMPD_target_teams_distribute_simd:
8529 case OMPD_target:
8530 case OMPD_target_simd:
8531 case OMPD_target_parallel:
8532 case OMPD_cancel:
8533 case OMPD_parallel:
8534 case OMPD_parallel_sections:
8535 case OMPD_threadprivate:
8536 case OMPD_taskyield:
8537 case OMPD_barrier:
8538 case OMPD_taskwait:
8539 case OMPD_cancellation_point:
8540 case OMPD_flush:
8541 case OMPD_declare_reduction:
8542 case OMPD_declare_simd:
8543 case OMPD_declare_target:
8544 case OMPD_end_declare_target:
8545 case OMPD_simd:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008546 case OMPD_sections:
8547 case OMPD_section:
8548 case OMPD_single:
8549 case OMPD_master:
8550 case OMPD_critical:
8551 case OMPD_taskgroup:
8552 case OMPD_distribute:
8553 case OMPD_ordered:
8554 case OMPD_atomic:
8555 case OMPD_distribute_simd:
8556 case OMPD_target_teams:
Kelvin Li1408f912018-09-26 04:28:39 +00008557 case OMPD_requires:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008558 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
8559 case OMPD_unknown:
8560 llvm_unreachable("Unknown OpenMP directive");
8561 }
8562 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008563 case OMPC_dist_schedule:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008564 switch (DKind) {
8565 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008566 case OMPD_teams_distribute_parallel_for_simd:
8567 case OMPD_teams_distribute:
8568 case OMPD_teams_distribute_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008569 case OMPD_target_teams_distribute_parallel_for:
8570 case OMPD_target_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008571 case OMPD_target_teams_distribute:
8572 case OMPD_target_teams_distribute_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008573 CaptureRegion = OMPD_teams;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008574 break;
8575 case OMPD_distribute_parallel_for:
8576 case OMPD_distribute_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008577 case OMPD_distribute:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008578 case OMPD_distribute_simd:
8579 // Do not capture thread_limit-clause expressions.
8580 break;
8581 case OMPD_parallel_for:
8582 case OMPD_parallel_for_simd:
8583 case OMPD_target_parallel_for_simd:
8584 case OMPD_target_parallel_for:
8585 case OMPD_task:
8586 case OMPD_taskloop:
8587 case OMPD_taskloop_simd:
8588 case OMPD_target_data:
8589 case OMPD_target_enter_data:
8590 case OMPD_target_exit_data:
8591 case OMPD_target_update:
8592 case OMPD_teams:
8593 case OMPD_target:
8594 case OMPD_target_simd:
8595 case OMPD_target_parallel:
8596 case OMPD_cancel:
8597 case OMPD_parallel:
8598 case OMPD_parallel_sections:
8599 case OMPD_threadprivate:
8600 case OMPD_taskyield:
8601 case OMPD_barrier:
8602 case OMPD_taskwait:
8603 case OMPD_cancellation_point:
8604 case OMPD_flush:
8605 case OMPD_declare_reduction:
8606 case OMPD_declare_simd:
8607 case OMPD_declare_target:
8608 case OMPD_end_declare_target:
8609 case OMPD_simd:
8610 case OMPD_for:
8611 case OMPD_for_simd:
8612 case OMPD_sections:
8613 case OMPD_section:
8614 case OMPD_single:
8615 case OMPD_master:
8616 case OMPD_critical:
8617 case OMPD_taskgroup:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008618 case OMPD_ordered:
8619 case OMPD_atomic:
8620 case OMPD_target_teams:
Kelvin Li1408f912018-09-26 04:28:39 +00008621 case OMPD_requires:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008622 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
8623 case OMPD_unknown:
8624 llvm_unreachable("Unknown OpenMP directive");
8625 }
8626 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008627 case OMPC_device:
8628 switch (DKind) {
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008629 case OMPD_target_update:
Alexey Bataevfab20e42017-12-27 18:49:38 +00008630 case OMPD_target_enter_data:
8631 case OMPD_target_exit_data:
Alexey Bataev8451efa2018-01-15 19:06:12 +00008632 case OMPD_target:
Alexey Bataevf41c88f2018-01-16 15:05:16 +00008633 case OMPD_target_simd:
Alexey Bataev0c869ef2018-01-16 15:57:07 +00008634 case OMPD_target_teams:
Alexey Bataev54d5c7d2018-01-16 16:27:49 +00008635 case OMPD_target_parallel:
Alexey Bataev79df7562018-01-16 16:46:46 +00008636 case OMPD_target_teams_distribute:
Alexey Bataev8d16a432018-01-16 17:22:50 +00008637 case OMPD_target_teams_distribute_simd:
Alexey Bataev8ed895512018-01-16 17:41:04 +00008638 case OMPD_target_parallel_for:
Alexey Bataevd60d1ba2018-01-16 17:55:15 +00008639 case OMPD_target_parallel_for_simd:
Alexey Bataev9f9fb0b2018-01-16 19:02:33 +00008640 case OMPD_target_teams_distribute_parallel_for:
Alexey Bataev9350fc32018-01-16 19:18:24 +00008641 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008642 CaptureRegion = OMPD_task;
8643 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008644 case OMPD_target_data:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008645 // Do not capture device-clause expressions.
8646 break;
8647 case OMPD_teams_distribute_parallel_for:
8648 case OMPD_teams_distribute_parallel_for_simd:
8649 case OMPD_teams:
8650 case OMPD_teams_distribute:
8651 case OMPD_teams_distribute_simd:
8652 case OMPD_distribute_parallel_for:
8653 case OMPD_distribute_parallel_for_simd:
8654 case OMPD_task:
8655 case OMPD_taskloop:
8656 case OMPD_taskloop_simd:
8657 case OMPD_cancel:
8658 case OMPD_parallel:
8659 case OMPD_parallel_sections:
8660 case OMPD_parallel_for:
8661 case OMPD_parallel_for_simd:
8662 case OMPD_threadprivate:
8663 case OMPD_taskyield:
8664 case OMPD_barrier:
8665 case OMPD_taskwait:
8666 case OMPD_cancellation_point:
8667 case OMPD_flush:
8668 case OMPD_declare_reduction:
8669 case OMPD_declare_simd:
8670 case OMPD_declare_target:
8671 case OMPD_end_declare_target:
8672 case OMPD_simd:
8673 case OMPD_for:
8674 case OMPD_for_simd:
8675 case OMPD_sections:
8676 case OMPD_section:
8677 case OMPD_single:
8678 case OMPD_master:
8679 case OMPD_critical:
8680 case OMPD_taskgroup:
8681 case OMPD_distribute:
8682 case OMPD_ordered:
8683 case OMPD_atomic:
8684 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008685 case OMPD_requires:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008686 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
8687 case OMPD_unknown:
8688 llvm_unreachable("Unknown OpenMP directive");
8689 }
8690 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008691 case OMPC_firstprivate:
8692 case OMPC_lastprivate:
8693 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008694 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008695 case OMPC_in_reduction:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008696 case OMPC_linear:
8697 case OMPC_default:
8698 case OMPC_proc_bind:
8699 case OMPC_final:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008700 case OMPC_safelen:
8701 case OMPC_simdlen:
8702 case OMPC_collapse:
8703 case OMPC_private:
8704 case OMPC_shared:
8705 case OMPC_aligned:
8706 case OMPC_copyin:
8707 case OMPC_copyprivate:
8708 case OMPC_ordered:
8709 case OMPC_nowait:
8710 case OMPC_untied:
8711 case OMPC_mergeable:
8712 case OMPC_threadprivate:
8713 case OMPC_flush:
8714 case OMPC_read:
8715 case OMPC_write:
8716 case OMPC_update:
8717 case OMPC_capture:
8718 case OMPC_seq_cst:
8719 case OMPC_depend:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008720 case OMPC_threads:
8721 case OMPC_simd:
8722 case OMPC_map:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008723 case OMPC_priority:
8724 case OMPC_grainsize:
8725 case OMPC_nogroup:
8726 case OMPC_num_tasks:
8727 case OMPC_hint:
8728 case OMPC_defaultmap:
8729 case OMPC_unknown:
8730 case OMPC_uniform:
8731 case OMPC_to:
8732 case OMPC_from:
8733 case OMPC_use_device_ptr:
8734 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00008735 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00008736 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00008737 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00008738 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00008739 case OMPC_atomic_default_mem_order:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008740 llvm_unreachable("Unexpected OpenMP clause.");
8741 }
8742 return CaptureRegion;
8743}
8744
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008745OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
8746 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008747 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008748 SourceLocation NameModifierLoc,
8749 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008750 SourceLocation EndLoc) {
8751 Expr *ValExpr = Condition;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008752 Stmt *HelperValStmt = nullptr;
8753 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008754 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
8755 !Condition->isInstantiationDependent() &&
8756 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00008757 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008758 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008759 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008760
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008761 ValExpr = Val.get();
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008762
8763 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
8764 CaptureRegion =
8765 getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier);
Alexey Bataev2ba67042017-11-28 21:11:44 +00008766 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008767 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00008768 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008769 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8770 HelperValStmt = buildPreInits(Context, Captures);
8771 }
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008772 }
8773
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008774 return new (Context)
8775 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
8776 LParenLoc, NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008777}
8778
Alexey Bataev3778b602014-07-17 07:32:53 +00008779OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
8780 SourceLocation StartLoc,
8781 SourceLocation LParenLoc,
8782 SourceLocation EndLoc) {
8783 Expr *ValExpr = Condition;
8784 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
8785 !Condition->isInstantiationDependent() &&
8786 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00008787 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataev3778b602014-07-17 07:32:53 +00008788 if (Val.isInvalid())
8789 return nullptr;
8790
Richard Smith03a4aa32016-06-23 19:02:52 +00008791 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataev3778b602014-07-17 07:32:53 +00008792 }
8793
8794 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8795}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00008796ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
8797 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00008798 if (!Op)
8799 return ExprError();
8800
8801 class IntConvertDiagnoser : public ICEConvertDiagnoser {
8802 public:
8803 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00008804 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00008805 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
8806 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008807 return S.Diag(Loc, diag::err_omp_not_integral) << T;
8808 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008809 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
8810 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008811 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
8812 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008813 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
8814 QualType T,
8815 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008816 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
8817 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008818 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
8819 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008820 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00008821 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00008822 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008823 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
8824 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008825 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
8826 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008827 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
8828 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008829 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00008830 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00008831 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008832 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
8833 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008834 llvm_unreachable("conversion functions are permitted");
8835 }
8836 } ConvertDiagnoser;
8837 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
8838}
8839
Alexey Bataeve3727102018-04-18 15:57:46 +00008840static bool isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00008841 OpenMPClauseKind CKind,
8842 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008843 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
8844 !ValExpr->isInstantiationDependent()) {
8845 SourceLocation Loc = ValExpr->getExprLoc();
8846 ExprResult Value =
8847 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
8848 if (Value.isInvalid())
8849 return false;
8850
8851 ValExpr = Value.get();
8852 // The expression must evaluate to a non-negative integer value.
8853 llvm::APSInt Result;
8854 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00008855 Result.isSigned() &&
8856 !((!StrictlyPositive && Result.isNonNegative()) ||
8857 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008858 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00008859 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
8860 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008861 return false;
8862 }
8863 }
8864 return true;
8865}
8866
Alexey Bataev568a8332014-03-06 06:15:19 +00008867OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
8868 SourceLocation StartLoc,
8869 SourceLocation LParenLoc,
8870 SourceLocation EndLoc) {
8871 Expr *ValExpr = NumThreads;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008872 Stmt *HelperValStmt = nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00008873
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008874 // OpenMP [2.5, Restrictions]
8875 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +00008876 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
Alexey Bataeva0569352015-12-01 10:17:31 +00008877 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008878 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00008879
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008880 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +00008881 OpenMPDirectiveKind CaptureRegion =
8882 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads);
8883 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008884 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00008885 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008886 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8887 HelperValStmt = buildPreInits(Context, Captures);
8888 }
8889
8890 return new (Context) OMPNumThreadsClause(
8891 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00008892}
8893
Alexey Bataev62c87d22014-03-21 04:51:18 +00008894ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008895 OpenMPClauseKind CKind,
8896 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00008897 if (!E)
8898 return ExprError();
8899 if (E->isValueDependent() || E->isTypeDependent() ||
8900 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00008901 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00008902 llvm::APSInt Result;
8903 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
8904 if (ICE.isInvalid())
8905 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008906 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
8907 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00008908 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008909 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
8910 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00008911 return ExprError();
8912 }
Alexander Musman09184fe2014-09-30 05:29:28 +00008913 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
8914 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
8915 << E->getSourceRange();
8916 return ExprError();
8917 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008918 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
8919 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00008920 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008921 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00008922 return ICE;
8923}
8924
8925OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
8926 SourceLocation LParenLoc,
8927 SourceLocation EndLoc) {
8928 // OpenMP [2.8.1, simd construct, Description]
8929 // The parameter of the safelen clause must be a constant
8930 // positive integer expression.
8931 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
8932 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008933 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00008934 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00008935 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00008936}
8937
Alexey Bataev66b15b52015-08-21 11:14:16 +00008938OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
8939 SourceLocation LParenLoc,
8940 SourceLocation EndLoc) {
8941 // OpenMP [2.8.1, simd construct, Description]
8942 // The parameter of the simdlen clause must be a constant
8943 // positive integer expression.
8944 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
8945 if (Simdlen.isInvalid())
8946 return nullptr;
8947 return new (Context)
8948 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
8949}
8950
Alexander Musman64d33f12014-06-04 07:53:32 +00008951OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
8952 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00008953 SourceLocation LParenLoc,
8954 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00008955 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00008956 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00008957 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00008958 // The parameter of the collapse clause must be a constant
8959 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00008960 ExprResult NumForLoopsResult =
8961 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
8962 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00008963 return nullptr;
8964 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00008965 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00008966}
8967
Alexey Bataev10e775f2015-07-30 11:36:16 +00008968OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
8969 SourceLocation EndLoc,
8970 SourceLocation LParenLoc,
8971 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00008972 // OpenMP [2.7.1, loop construct, Description]
8973 // OpenMP [2.8.1, simd construct, Description]
8974 // OpenMP [2.9.6, distribute construct, Description]
8975 // The parameter of the ordered clause must be a constant
8976 // positive integer expression if any.
8977 if (NumForLoops && LParenLoc.isValid()) {
8978 ExprResult NumForLoopsResult =
8979 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
8980 if (NumForLoopsResult.isInvalid())
8981 return nullptr;
8982 NumForLoops = NumForLoopsResult.get();
Alexey Bataeve3727102018-04-18 15:57:46 +00008983 } else {
Alexey Bataev346265e2015-09-25 10:37:12 +00008984 NumForLoops = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +00008985 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00008986 auto *Clause = OMPOrderedClause::Create(
8987 Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0,
8988 StartLoc, LParenLoc, EndLoc);
8989 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause);
8990 return Clause;
Alexey Bataev10e775f2015-07-30 11:36:16 +00008991}
8992
Alexey Bataeved09d242014-05-28 05:53:51 +00008993OMPClause *Sema::ActOnOpenMPSimpleClause(
8994 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
8995 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008996 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008997 switch (Kind) {
8998 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00008999 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00009000 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
9001 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009002 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009003 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00009004 Res = ActOnOpenMPProcBindClause(
9005 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
9006 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009007 break;
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009008 case OMPC_atomic_default_mem_order:
9009 Res = ActOnOpenMPAtomicDefaultMemOrderClause(
9010 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument),
9011 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
9012 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009013 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00009014 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00009015 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00009016 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009017 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00009018 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009019 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009020 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009021 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00009022 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00009023 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00009024 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00009025 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00009026 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00009027 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009028 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009029 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009030 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009031 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00009032 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009033 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009034 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009035 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00009036 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009037 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00009038 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00009039 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00009040 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009041 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009042 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00009043 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00009044 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009045 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00009046 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009047 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009048 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009049 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009050 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00009051 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00009052 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009053 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00009054 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009055 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009056 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009057 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00009058 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00009059 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00009060 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00009061 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00009062 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00009063 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009064 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009065 case OMPC_dynamic_allocators:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009066 llvm_unreachable("Clause is not allowed.");
9067 }
9068 return Res;
9069}
9070
Alexey Bataev6402bca2015-12-28 07:25:51 +00009071static std::string
9072getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
9073 ArrayRef<unsigned> Exclude = llvm::None) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009074 SmallString<256> Buffer;
9075 llvm::raw_svector_ostream Out(Buffer);
Alexey Bataev6402bca2015-12-28 07:25:51 +00009076 unsigned Bound = Last >= 2 ? Last - 2 : 0;
9077 unsigned Skipped = Exclude.size();
9078 auto S = Exclude.begin(), E = Exclude.end();
Alexey Bataeve3727102018-04-18 15:57:46 +00009079 for (unsigned I = First; I < Last; ++I) {
9080 if (std::find(S, E, I) != E) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00009081 --Skipped;
9082 continue;
9083 }
Alexey Bataeve3727102018-04-18 15:57:46 +00009084 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'";
9085 if (I == Bound - Skipped)
9086 Out << " or ";
9087 else if (I != Bound + 1 - Skipped)
9088 Out << ", ";
Alexey Bataev6402bca2015-12-28 07:25:51 +00009089 }
Alexey Bataeve3727102018-04-18 15:57:46 +00009090 return Out.str();
Alexey Bataev6402bca2015-12-28 07:25:51 +00009091}
9092
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009093OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
9094 SourceLocation KindKwLoc,
9095 SourceLocation StartLoc,
9096 SourceLocation LParenLoc,
9097 SourceLocation EndLoc) {
9098 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00009099 static_assert(OMPC_DEFAULT_unknown > 0,
9100 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009101 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009102 << getListOfPossibleValues(OMPC_default, /*First=*/0,
9103 /*Last=*/OMPC_DEFAULT_unknown)
9104 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009105 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009106 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00009107 switch (Kind) {
9108 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009109 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009110 break;
9111 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009112 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009113 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009114 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009115 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00009116 break;
9117 }
Alexey Bataeved09d242014-05-28 05:53:51 +00009118 return new (Context)
9119 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009120}
9121
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009122OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
9123 SourceLocation KindKwLoc,
9124 SourceLocation StartLoc,
9125 SourceLocation LParenLoc,
9126 SourceLocation EndLoc) {
9127 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009128 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009129 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
9130 /*Last=*/OMPC_PROC_BIND_unknown)
9131 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009132 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009133 }
Alexey Bataeved09d242014-05-28 05:53:51 +00009134 return new (Context)
9135 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009136}
9137
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009138OMPClause *Sema::ActOnOpenMPAtomicDefaultMemOrderClause(
9139 OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc,
9140 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
9141 if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) {
9142 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
9143 << getListOfPossibleValues(
9144 OMPC_atomic_default_mem_order, /*First=*/0,
9145 /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown)
9146 << getOpenMPClauseName(OMPC_atomic_default_mem_order);
9147 return nullptr;
9148 }
9149 return new (Context) OMPAtomicDefaultMemOrderClause(Kind, KindKwLoc, StartLoc,
9150 LParenLoc, EndLoc);
9151}
9152
Alexey Bataev56dafe82014-06-20 07:16:17 +00009153OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00009154 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00009155 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00009156 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00009157 SourceLocation EndLoc) {
9158 OMPClause *Res = nullptr;
9159 switch (Kind) {
9160 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00009161 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
9162 assert(Argument.size() == NumberOfElements &&
9163 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009164 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00009165 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
9166 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
9167 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
9168 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
9169 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009170 break;
9171 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00009172 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
9173 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
9174 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
9175 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009176 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00009177 case OMPC_dist_schedule:
9178 Res = ActOnOpenMPDistScheduleClause(
9179 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
9180 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
9181 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009182 case OMPC_defaultmap:
9183 enum { Modifier, DefaultmapKind };
9184 Res = ActOnOpenMPDefaultmapClause(
9185 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
9186 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
David Majnemer9d168222016-08-05 17:44:54 +00009187 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind],
9188 EndLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009189 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00009190 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009191 case OMPC_num_threads:
9192 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009193 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009194 case OMPC_collapse:
9195 case OMPC_default:
9196 case OMPC_proc_bind:
9197 case OMPC_private:
9198 case OMPC_firstprivate:
9199 case OMPC_lastprivate:
9200 case OMPC_shared:
9201 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00009202 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00009203 case OMPC_in_reduction:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009204 case OMPC_linear:
9205 case OMPC_aligned:
9206 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009207 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009208 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00009209 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009210 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009211 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009212 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00009213 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009214 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00009215 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00009216 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00009217 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009218 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009219 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00009220 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00009221 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009222 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00009223 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009224 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009225 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009226 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009227 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00009228 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00009229 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009230 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009231 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009232 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00009233 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00009234 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00009235 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00009236 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00009237 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00009238 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009239 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009240 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009241 case OMPC_atomic_default_mem_order:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009242 llvm_unreachable("Clause is not allowed.");
9243 }
9244 return Res;
9245}
9246
Alexey Bataev6402bca2015-12-28 07:25:51 +00009247static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
9248 OpenMPScheduleClauseModifier M2,
9249 SourceLocation M1Loc, SourceLocation M2Loc) {
9250 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
9251 SmallVector<unsigned, 2> Excluded;
9252 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
9253 Excluded.push_back(M2);
9254 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
9255 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
9256 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
9257 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
9258 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
9259 << getListOfPossibleValues(OMPC_schedule,
9260 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
9261 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
9262 Excluded)
9263 << getOpenMPClauseName(OMPC_schedule);
9264 return true;
9265 }
9266 return false;
9267}
9268
Alexey Bataev56dafe82014-06-20 07:16:17 +00009269OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00009270 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00009271 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00009272 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
9273 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
9274 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
9275 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
9276 return nullptr;
9277 // OpenMP, 2.7.1, Loop Construct, Restrictions
9278 // Either the monotonic modifier or the nonmonotonic modifier can be specified
9279 // but not both.
9280 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
9281 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
9282 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
9283 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
9284 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
9285 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
9286 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
9287 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
9288 return nullptr;
9289 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00009290 if (Kind == OMPC_SCHEDULE_unknown) {
9291 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00009292 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
9293 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
9294 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
9295 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
9296 Exclude);
9297 } else {
9298 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
9299 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009300 }
9301 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
9302 << Values << getOpenMPClauseName(OMPC_schedule);
9303 return nullptr;
9304 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00009305 // OpenMP, 2.7.1, Loop Construct, Restrictions
9306 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
9307 // schedule(guided).
9308 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
9309 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
9310 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
9311 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
9312 diag::err_omp_schedule_nonmonotonic_static);
9313 return nullptr;
9314 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00009315 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00009316 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00009317 if (ChunkSize) {
9318 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
9319 !ChunkSize->isInstantiationDependent() &&
9320 !ChunkSize->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00009321 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
Alexey Bataev56dafe82014-06-20 07:16:17 +00009322 ExprResult Val =
9323 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
9324 if (Val.isInvalid())
9325 return nullptr;
9326
9327 ValExpr = Val.get();
9328
9329 // OpenMP [2.7.1, Restrictions]
9330 // chunk_size must be a loop invariant integer expression with a positive
9331 // value.
9332 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00009333 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
9334 if (Result.isSigned() && !Result.isStrictlyPositive()) {
9335 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00009336 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00009337 return nullptr;
9338 }
Alexey Bataev2ba67042017-11-28 21:11:44 +00009339 } else if (getOpenMPCaptureRegionForClause(
9340 DSAStack->getCurrentDirective(), OMPC_schedule) !=
9341 OMPD_unknown &&
Alexey Bataevb46cdea2016-06-15 11:20:48 +00009342 !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00009343 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00009344 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev5a3af132016-03-29 08:58:54 +00009345 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
9346 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009347 }
9348 }
9349 }
9350
Alexey Bataev6402bca2015-12-28 07:25:51 +00009351 return new (Context)
9352 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00009353 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009354}
9355
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009356OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
9357 SourceLocation StartLoc,
9358 SourceLocation EndLoc) {
9359 OMPClause *Res = nullptr;
9360 switch (Kind) {
9361 case OMPC_ordered:
9362 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
9363 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00009364 case OMPC_nowait:
9365 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
9366 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009367 case OMPC_untied:
9368 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
9369 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009370 case OMPC_mergeable:
9371 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
9372 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009373 case OMPC_read:
9374 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
9375 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00009376 case OMPC_write:
9377 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
9378 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00009379 case OMPC_update:
9380 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
9381 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00009382 case OMPC_capture:
9383 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
9384 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009385 case OMPC_seq_cst:
9386 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
9387 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00009388 case OMPC_threads:
9389 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
9390 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009391 case OMPC_simd:
9392 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
9393 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00009394 case OMPC_nogroup:
9395 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
9396 break;
Kelvin Li1408f912018-09-26 04:28:39 +00009397 case OMPC_unified_address:
9398 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc);
9399 break;
Patrick Lyster4a370b92018-10-01 13:47:43 +00009400 case OMPC_unified_shared_memory:
9401 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
9402 break;
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009403 case OMPC_reverse_offload:
9404 Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc);
9405 break;
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009406 case OMPC_dynamic_allocators:
9407 Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc);
9408 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009409 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00009410 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009411 case OMPC_num_threads:
9412 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009413 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009414 case OMPC_collapse:
9415 case OMPC_schedule:
9416 case OMPC_private:
9417 case OMPC_firstprivate:
9418 case OMPC_lastprivate:
9419 case OMPC_shared:
9420 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00009421 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00009422 case OMPC_in_reduction:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009423 case OMPC_linear:
9424 case OMPC_aligned:
9425 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009426 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009427 case OMPC_default:
9428 case OMPC_proc_bind:
9429 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00009430 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009431 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00009432 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00009433 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009434 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009435 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009436 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009437 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00009438 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009439 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00009440 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009441 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009442 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009443 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00009444 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00009445 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00009446 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00009447 case OMPC_is_device_ptr:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009448 case OMPC_atomic_default_mem_order:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009449 llvm_unreachable("Clause is not allowed.");
9450 }
9451 return Res;
9452}
9453
Alexey Bataev236070f2014-06-20 11:19:47 +00009454OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
9455 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00009456 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00009457 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
9458}
9459
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009460OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
9461 SourceLocation EndLoc) {
9462 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
9463}
9464
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009465OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
9466 SourceLocation EndLoc) {
9467 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
9468}
9469
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009470OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
9471 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009472 return new (Context) OMPReadClause(StartLoc, EndLoc);
9473}
9474
Alexey Bataevdea47612014-07-23 07:46:59 +00009475OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
9476 SourceLocation EndLoc) {
9477 return new (Context) OMPWriteClause(StartLoc, EndLoc);
9478}
9479
Alexey Bataev67a4f222014-07-23 10:25:33 +00009480OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
9481 SourceLocation EndLoc) {
9482 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
9483}
9484
Alexey Bataev459dec02014-07-24 06:46:57 +00009485OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
9486 SourceLocation EndLoc) {
9487 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
9488}
9489
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009490OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
9491 SourceLocation EndLoc) {
9492 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
9493}
9494
Alexey Bataev346265e2015-09-25 10:37:12 +00009495OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
9496 SourceLocation EndLoc) {
9497 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
9498}
9499
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009500OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
9501 SourceLocation EndLoc) {
9502 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
9503}
9504
Alexey Bataevb825de12015-12-07 10:51:44 +00009505OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
9506 SourceLocation EndLoc) {
9507 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
9508}
9509
Kelvin Li1408f912018-09-26 04:28:39 +00009510OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc,
9511 SourceLocation EndLoc) {
9512 return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc);
9513}
9514
Patrick Lyster4a370b92018-10-01 13:47:43 +00009515OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc,
9516 SourceLocation EndLoc) {
9517 return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
9518}
9519
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009520OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc,
9521 SourceLocation EndLoc) {
9522 return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc);
9523}
9524
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009525OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc,
9526 SourceLocation EndLoc) {
9527 return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc);
9528}
9529
Alexey Bataevc5e02582014-06-16 07:08:35 +00009530OMPClause *Sema::ActOnOpenMPVarListClause(
9531 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
9532 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
9533 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009534 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00009535 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
9536 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
9537 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009538 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009539 switch (Kind) {
9540 case OMPC_private:
9541 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9542 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009543 case OMPC_firstprivate:
9544 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9545 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00009546 case OMPC_lastprivate:
9547 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9548 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00009549 case OMPC_shared:
9550 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
9551 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009552 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00009553 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9554 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00009555 break;
Alexey Bataev169d96a2017-07-18 20:17:46 +00009556 case OMPC_task_reduction:
9557 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9558 EndLoc, ReductionIdScopeSpec,
9559 ReductionId);
9560 break;
Alexey Bataevfa312f32017-07-21 18:48:21 +00009561 case OMPC_in_reduction:
9562 Res =
9563 ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9564 EndLoc, ReductionIdScopeSpec, ReductionId);
9565 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00009566 case OMPC_linear:
9567 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00009568 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00009569 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009570 case OMPC_aligned:
9571 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
9572 ColonLoc, EndLoc);
9573 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009574 case OMPC_copyin:
9575 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
9576 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009577 case OMPC_copyprivate:
9578 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9579 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00009580 case OMPC_flush:
9581 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
9582 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009583 case OMPC_depend:
David Majnemer9d168222016-08-05 17:44:54 +00009584 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
Kelvin Li0bff7af2015-11-23 05:32:03 +00009585 StartLoc, LParenLoc, EndLoc);
9586 break;
9587 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00009588 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
9589 DepLinMapLoc, ColonLoc, VarList, StartLoc,
9590 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009591 break;
Samuel Antao661c0902016-05-26 17:39:58 +00009592 case OMPC_to:
9593 Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc);
9594 break;
Samuel Antaoec172c62016-05-26 17:49:04 +00009595 case OMPC_from:
9596 Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc);
9597 break;
Carlo Bertolli2404b172016-07-13 15:37:16 +00009598 case OMPC_use_device_ptr:
9599 Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
9600 break;
Carlo Bertolli70594e92016-07-13 17:16:49 +00009601 case OMPC_is_device_ptr:
9602 Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
9603 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009604 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00009605 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00009606 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00009607 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009608 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00009609 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009610 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009611 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009612 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009613 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00009614 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009615 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009616 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009617 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009618 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00009619 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00009620 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00009621 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009622 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00009623 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00009624 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009625 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009626 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009627 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009628 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009629 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00009630 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00009631 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009632 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00009633 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009634 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009635 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009636 case OMPC_uniform:
Kelvin Li1408f912018-09-26 04:28:39 +00009637 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00009638 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009639 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009640 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009641 case OMPC_atomic_default_mem_order:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009642 llvm_unreachable("Clause is not allowed.");
9643 }
9644 return Res;
9645}
9646
Alexey Bataev90c228f2016-02-08 09:29:13 +00009647ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00009648 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00009649 ExprResult Res = BuildDeclRefExpr(
9650 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
9651 if (!Res.isUsable())
9652 return ExprError();
9653 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
9654 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
9655 if (!Res.isUsable())
9656 return ExprError();
9657 }
9658 if (VK != VK_LValue && Res.get()->isGLValue()) {
9659 Res = DefaultLvalueConversion(Res.get());
9660 if (!Res.isUsable())
9661 return ExprError();
9662 }
9663 return Res;
9664}
9665
Alexey Bataev60da77e2016-02-29 05:54:20 +00009666static std::pair<ValueDecl *, bool>
9667getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
9668 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00009669 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
9670 RefExpr->containsUnexpandedParameterPack())
9671 return std::make_pair(nullptr, true);
9672
Alexey Bataevd985eda2016-02-10 11:29:16 +00009673 // OpenMP [3.1, C/C++]
9674 // A list item is a variable name.
9675 // OpenMP [2.9.3.3, Restrictions, p.1]
9676 // A variable that is part of another variable (as an array or
9677 // structure element) cannot appear in a private clause.
9678 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009679 enum {
9680 NoArrayExpr = -1,
9681 ArraySubscript = 0,
9682 OMPArraySection = 1
9683 } IsArrayExpr = NoArrayExpr;
9684 if (AllowArraySection) {
9685 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009686 Expr *Base = ASE->getBase()->IgnoreParenImpCasts();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009687 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
9688 Base = TempASE->getBase()->IgnoreParenImpCasts();
9689 RefExpr = Base;
9690 IsArrayExpr = ArraySubscript;
9691 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009692 Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009693 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
9694 Base = TempOASE->getBase()->IgnoreParenImpCasts();
9695 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
9696 Base = TempASE->getBase()->IgnoreParenImpCasts();
9697 RefExpr = Base;
9698 IsArrayExpr = OMPArraySection;
9699 }
9700 }
9701 ELoc = RefExpr->getExprLoc();
9702 ERange = RefExpr->getSourceRange();
9703 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00009704 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
9705 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
9706 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
9707 (S.getCurrentThisType().isNull() || !ME ||
9708 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
9709 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009710 if (IsArrayExpr != NoArrayExpr) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009711 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
9712 << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +00009713 } else {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009714 S.Diag(ELoc,
9715 AllowArraySection
9716 ? diag::err_omp_expected_var_name_member_expr_or_array_item
9717 : diag::err_omp_expected_var_name_member_expr)
9718 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
9719 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009720 return std::make_pair(nullptr, false);
9721 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +00009722 return std::make_pair(
9723 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009724}
9725
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009726OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
9727 SourceLocation StartLoc,
9728 SourceLocation LParenLoc,
9729 SourceLocation EndLoc) {
9730 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00009731 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeve3727102018-04-18 15:57:46 +00009732 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009733 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009734 SourceLocation ELoc;
9735 SourceRange ERange;
9736 Expr *SimpleRefExpr = RefExpr;
9737 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009738 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009739 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009740 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009741 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009742 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009743 ValueDecl *D = Res.first;
9744 if (!D)
9745 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009746
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009747 QualType Type = D->getType();
9748 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009749
9750 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9751 // A variable that appears in a private clause must not have an incomplete
9752 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009753 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009754 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009755 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009756
Alexey Bataev758e55e2013-09-06 18:03:48 +00009757 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9758 // in a Construct]
9759 // Variables with the predetermined data-sharing attributes may not be
9760 // listed in data-sharing attributes clauses, except for the cases
9761 // listed below. For these exceptions only, listing a predetermined
9762 // variable in a data-sharing attribute clause is allowed and overrides
9763 // the variable's predetermined data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +00009764 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009765 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009766 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9767 << getOpenMPClauseName(OMPC_private);
Alexey Bataeve3727102018-04-18 15:57:46 +00009768 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009769 continue;
9770 }
9771
Alexey Bataeve3727102018-04-18 15:57:46 +00009772 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009773 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009774 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Kelvin Libf594a52016-12-17 05:48:59 +00009775 isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009776 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
9777 << getOpenMPClauseName(OMPC_private) << Type
Kelvin Libf594a52016-12-17 05:48:59 +00009778 << getOpenMPDirectiveName(CurrDir);
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009779 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009780 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009781 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009782 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009783 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009784 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009785 continue;
9786 }
9787
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009788 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
9789 // A list item cannot appear in both a map clause and a data-sharing
9790 // attribute clause on the same construct
Alexey Bataeve3727102018-04-18 15:57:46 +00009791 if (isOpenMPTargetExecutionDirective(CurrDir)) {
Samuel Antao6890b092016-07-28 14:25:09 +00009792 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +00009793 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +00009794 VD, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +00009795 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
9796 OpenMPClauseKind WhereFoundClauseKind) -> bool {
9797 ConflictKind = WhereFoundClauseKind;
9798 return true;
9799 })) {
9800 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009801 << getOpenMPClauseName(OMPC_private)
Samuel Antao6890b092016-07-28 14:25:09 +00009802 << getOpenMPClauseName(ConflictKind)
Kelvin Libf594a52016-12-17 05:48:59 +00009803 << getOpenMPDirectiveName(CurrDir);
Alexey Bataeve3727102018-04-18 15:57:46 +00009804 reportOriginalDsa(*this, DSAStack, D, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009805 continue;
9806 }
9807 }
9808
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009809 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
9810 // A variable of class type (or array thereof) that appears in a private
9811 // clause requires an accessible, unambiguous default constructor for the
9812 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00009813 // Generate helper private variable and initialize it with the default
9814 // value. The address of the original variable is replaced by the address of
9815 // the new private variable in CodeGen. This new variable is not added to
9816 // IdResolver, so the code in the OpenMP region uses original variable for
9817 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009818 Type = Type.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +00009819 VarDecl *VDPrivate =
Alexey Bataev63cc8e92018-03-20 14:45:59 +00009820 buildVarDecl(*this, ELoc, Type, D->getName(),
9821 D->hasAttrs() ? &D->getAttrs() : nullptr,
9822 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Richard Smith3beb7c62017-01-12 02:27:38 +00009823 ActOnUninitializedDecl(VDPrivate);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009824 if (VDPrivate->isInvalidDecl())
9825 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00009826 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009827 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009828
Alexey Bataev90c228f2016-02-08 09:29:13 +00009829 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009830 if (!VD && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00009831 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00009832 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009833 Vars.push_back((VD || CurContext->isDependentContext())
9834 ? RefExpr->IgnoreParens()
9835 : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009836 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009837 }
9838
Alexey Bataeved09d242014-05-28 05:53:51 +00009839 if (Vars.empty())
9840 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009841
Alexey Bataev03b340a2014-10-21 03:16:40 +00009842 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9843 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009844}
9845
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009846namespace {
9847class DiagsUninitializedSeveretyRAII {
9848private:
9849 DiagnosticsEngine &Diags;
9850 SourceLocation SavedLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00009851 bool IsIgnored = false;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009852
9853public:
9854 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
9855 bool IsIgnored)
9856 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
9857 if (!IsIgnored) {
9858 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
9859 /*Map*/ diag::Severity::Ignored, Loc);
9860 }
9861 }
9862 ~DiagsUninitializedSeveretyRAII() {
9863 if (!IsIgnored)
9864 Diags.popMappings(SavedLoc);
9865 }
9866};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00009867}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009868
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009869OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
9870 SourceLocation StartLoc,
9871 SourceLocation LParenLoc,
9872 SourceLocation EndLoc) {
9873 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009874 SmallVector<Expr *, 8> PrivateCopies;
9875 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00009876 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009877 bool IsImplicitClause =
9878 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
Alexey Bataeve3727102018-04-18 15:57:46 +00009879 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009880
Alexey Bataeve3727102018-04-18 15:57:46 +00009881 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009882 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009883 SourceLocation ELoc;
9884 SourceRange ERange;
9885 Expr *SimpleRefExpr = RefExpr;
9886 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009887 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009888 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009889 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009890 PrivateCopies.push_back(nullptr);
9891 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009892 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009893 ValueDecl *D = Res.first;
9894 if (!D)
9895 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009896
Alexey Bataev60da77e2016-02-29 05:54:20 +00009897 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00009898 QualType Type = D->getType();
9899 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009900
9901 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9902 // A variable that appears in a private clause must not have an incomplete
9903 // type or a reference type.
9904 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00009905 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009906 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009907 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009908
9909 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
9910 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00009911 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009912 // class type.
Alexey Bataeve3727102018-04-18 15:57:46 +00009913 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009914
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009915 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00009916 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009917 if (!IsImplicitClause) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009918 DSAStackTy::DSAVarData DVar =
9919 DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00009920 TopDVar = DVar;
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009921 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009922 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009923 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
9924 // A list item that specifies a given variable may not appear in more
9925 // than one clause on the same directive, except that a variable may be
9926 // specified in both firstprivate and lastprivate clauses.
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009927 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
9928 // A list item may appear in a firstprivate or lastprivate clause but not
9929 // both.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009930 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevb358f992017-12-01 17:40:15 +00009931 (isOpenMPDistributeDirective(CurrDir) ||
9932 DVar.CKind != OMPC_lastprivate) &&
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009933 DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009934 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00009935 << getOpenMPClauseName(DVar.CKind)
9936 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +00009937 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009938 continue;
9939 }
9940
9941 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9942 // in a Construct]
9943 // Variables with the predetermined data-sharing attributes may not be
9944 // listed in data-sharing attributes clauses, except for the cases
9945 // listed below. For these exceptions only, listing a predetermined
9946 // variable in a data-sharing attribute clause is allowed and overrides
9947 // the variable's predetermined data-sharing attributes.
9948 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9949 // in a Construct, C/C++, p.2]
9950 // Variables with const-qualified type having no mutable member may be
9951 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00009952 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009953 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
9954 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00009955 << getOpenMPClauseName(DVar.CKind)
9956 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +00009957 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009958 continue;
9959 }
9960
9961 // OpenMP [2.9.3.4, Restrictions, p.2]
9962 // A list item that is private within a parallel region must not appear
9963 // in a firstprivate clause on a worksharing construct if any of the
9964 // worksharing regions arising from the worksharing construct ever bind
9965 // to any of the parallel regions arising from the parallel construct.
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009966 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
9967 // A list item that is private within a teams region must not appear in a
9968 // firstprivate clause on a distribute construct if any of the distribute
9969 // regions arising from the distribute construct ever bind to any of the
9970 // teams regions arising from the teams construct.
9971 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
9972 // A list item that appears in a reduction clause of a teams construct
9973 // must not appear in a firstprivate clause on a distribute construct if
9974 // any of the distribute regions arising from the distribute construct
9975 // ever bind to any of the teams regions arising from the teams construct.
9976 if ((isOpenMPWorksharingDirective(CurrDir) ||
9977 isOpenMPDistributeDirective(CurrDir)) &&
Kelvin Li579e41c2016-11-30 23:51:03 +00009978 !isOpenMPParallelDirective(CurrDir) &&
9979 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00009980 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009981 if (DVar.CKind != OMPC_shared &&
9982 (isOpenMPParallelDirective(DVar.DKind) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009983 isOpenMPTeamsDirective(DVar.DKind) ||
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009984 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00009985 Diag(ELoc, diag::err_omp_required_access)
9986 << getOpenMPClauseName(OMPC_firstprivate)
9987 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +00009988 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00009989 continue;
9990 }
9991 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009992 // OpenMP [2.9.3.4, Restrictions, p.3]
9993 // A list item that appears in a reduction clause of a parallel construct
9994 // must not appear in a firstprivate clause on a worksharing or task
9995 // construct if any of the worksharing or task regions arising from the
9996 // worksharing or task construct ever bind to any of the parallel regions
9997 // arising from the parallel construct.
9998 // OpenMP [2.9.3.4, Restrictions, p.4]
9999 // A list item that appears in a reduction clause in worksharing
10000 // construct must not appear in a firstprivate clause in a task construct
10001 // encountered during execution of any of the worksharing regions arising
10002 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +000010003 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010004 DVar = DSAStack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +000010005 D, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
10006 [](OpenMPDirectiveKind K) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010007 return isOpenMPParallelDirective(K) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010008 isOpenMPWorksharingDirective(K) ||
10009 isOpenMPTeamsDirective(K);
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010010 },
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010011 /*FromParent=*/true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010012 if (DVar.CKind == OMPC_reduction &&
10013 (isOpenMPParallelDirective(DVar.DKind) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010014 isOpenMPWorksharingDirective(DVar.DKind) ||
10015 isOpenMPTeamsDirective(DVar.DKind))) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010016 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
10017 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataeve3727102018-04-18 15:57:46 +000010018 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010019 continue;
10020 }
10021 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +000010022
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010023 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
10024 // A list item cannot appear in both a map clause and a data-sharing
10025 // attribute clause on the same construct
Alexey Bataevb358f992017-12-01 17:40:15 +000010026 if (isOpenMPTargetExecutionDirective(CurrDir)) {
Samuel Antao6890b092016-07-28 14:25:09 +000010027 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +000010028 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +000010029 VD, /*CurrentRegionOnly=*/true,
Alexey Bataeve3727102018-04-18 15:57:46 +000010030 [&ConflictKind](
10031 OMPClauseMappableExprCommon::MappableExprComponentListRef,
10032 OpenMPClauseKind WhereFoundClauseKind) {
Samuel Antao6890b092016-07-28 14:25:09 +000010033 ConflictKind = WhereFoundClauseKind;
10034 return true;
10035 })) {
10036 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010037 << getOpenMPClauseName(OMPC_firstprivate)
Samuel Antao6890b092016-07-28 14:25:09 +000010038 << getOpenMPClauseName(ConflictKind)
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010039 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000010040 reportOriginalDsa(*this, DSAStack, D, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010041 continue;
10042 }
10043 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010044 }
10045
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010046 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000010047 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +000010048 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010049 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
10050 << getOpenMPClauseName(OMPC_firstprivate) << Type
10051 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
10052 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +000010053 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010054 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +000010055 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010056 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +000010057 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010058 continue;
10059 }
10060
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010061 Type = Type.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010062 VarDecl *VDPrivate =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000010063 buildVarDecl(*this, ELoc, Type, D->getName(),
10064 D->hasAttrs() ? &D->getAttrs() : nullptr,
10065 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010066 // Generate helper private variable and initialize it with the value of the
10067 // original variable. The address of the original variable is replaced by
10068 // the address of the new private variable in the CodeGen. This new variable
10069 // is not added to IdResolver, so the code in the OpenMP region uses
10070 // original variable for proper diagnostics and variable capturing.
10071 Expr *VDInitRefExpr = nullptr;
10072 // For arrays generate initializer for single element and replace it by the
10073 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010074 if (Type->isArrayType()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010075 VarDecl *VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +000010076 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010077 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataeve3727102018-04-18 15:57:46 +000010078 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010079 ElemType = ElemType.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010080 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
10081 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +000010082 InitializedEntity Entity =
10083 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010084 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
10085
10086 InitializationSequence InitSeq(*this, Entity, Kind, Init);
10087 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
10088 if (Result.isInvalid())
10089 VDPrivate->setInvalidDecl();
10090 else
10091 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010092 // Remove temp variable declaration.
10093 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010094 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +000010095 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
10096 ".firstprivate.temp");
Alexey Bataevd985eda2016-02-10 11:29:16 +000010097 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
10098 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +000010099 AddInitializerToDecl(VDPrivate,
10100 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000010101 /*DirectInit=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010102 }
10103 if (VDPrivate->isInvalidDecl()) {
10104 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +000010105 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010106 diag::note_omp_task_predetermined_firstprivate_here);
10107 }
10108 continue;
10109 }
10110 CurContext->addDecl(VDPrivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000010111 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +000010112 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
10113 RefExpr->getExprLoc());
10114 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010115 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010116 if (TopDVar.CKind == OMPC_lastprivate) {
Alexey Bataev005248a2016-02-25 05:25:57 +000010117 Ref = TopDVar.PrivateCopy;
Alexey Bataeve3727102018-04-18 15:57:46 +000010118 } else {
Alexey Bataev61205072016-03-02 04:57:40 +000010119 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataeve3727102018-04-18 15:57:46 +000010120 if (!isOpenMPCapturedDecl(D))
Alexey Bataev005248a2016-02-25 05:25:57 +000010121 ExprCaptures.push_back(Ref->getDecl());
10122 }
Alexey Bataev417089f2016-02-17 13:19:37 +000010123 }
Alexey Bataevd985eda2016-02-10 11:29:16 +000010124 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010125 Vars.push_back((VD || CurContext->isDependentContext())
10126 ? RefExpr->IgnoreParens()
10127 : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010128 PrivateCopies.push_back(VDPrivateRefExpr);
10129 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010130 }
10131
Alexey Bataeved09d242014-05-28 05:53:51 +000010132 if (Vars.empty())
10133 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010134
10135 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +000010136 Vars, PrivateCopies, Inits,
10137 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010138}
10139
Alexander Musman1bb328c2014-06-04 13:06:39 +000010140OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
10141 SourceLocation StartLoc,
10142 SourceLocation LParenLoc,
10143 SourceLocation EndLoc) {
10144 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +000010145 SmallVector<Expr *, 8> SrcExprs;
10146 SmallVector<Expr *, 8> DstExprs;
10147 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +000010148 SmallVector<Decl *, 4> ExprCaptures;
10149 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeve3727102018-04-18 15:57:46 +000010150 for (Expr *RefExpr : VarList) {
Alexander Musman1bb328c2014-06-04 13:06:39 +000010151 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +000010152 SourceLocation ELoc;
10153 SourceRange ERange;
10154 Expr *SimpleRefExpr = RefExpr;
10155 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +000010156 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +000010157 // It will be analyzed later.
10158 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +000010159 SrcExprs.push_back(nullptr);
10160 DstExprs.push_back(nullptr);
10161 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010162 }
Alexey Bataev74caaf22016-02-20 04:09:36 +000010163 ValueDecl *D = Res.first;
10164 if (!D)
10165 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +000010166
Alexey Bataev74caaf22016-02-20 04:09:36 +000010167 QualType Type = D->getType();
10168 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010169
10170 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
10171 // A variable that appears in a lastprivate clause must not have an
10172 // incomplete type or a reference type.
10173 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +000010174 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +000010175 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010176 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +000010177
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010178 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexander Musman1bb328c2014-06-04 13:06:39 +000010179 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
10180 // in a Construct]
10181 // Variables with the predetermined data-sharing attributes may not be
10182 // listed in data-sharing attributes clauses, except for the cases
10183 // listed below.
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010184 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
10185 // A list item may appear in a firstprivate or lastprivate clause but not
10186 // both.
Alexey Bataeve3727102018-04-18 15:57:46 +000010187 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010188 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
Alexey Bataevb358f992017-12-01 17:40:15 +000010189 (isOpenMPDistributeDirective(CurrDir) ||
10190 DVar.CKind != OMPC_firstprivate) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +000010191 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
10192 Diag(ELoc, diag::err_omp_wrong_dsa)
10193 << getOpenMPClauseName(DVar.CKind)
10194 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000010195 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010196 continue;
10197 }
10198
Alexey Bataevf29276e2014-06-18 04:14:57 +000010199 // OpenMP [2.14.3.5, Restrictions, p.2]
10200 // A list item that is private within a parallel region, or that appears in
10201 // the reduction clause of a parallel construct, must not appear in a
10202 // lastprivate clause on a worksharing construct if any of the corresponding
10203 // worksharing regions ever binds to any of the corresponding parallel
10204 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +000010205 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +000010206 if (isOpenMPWorksharingDirective(CurrDir) &&
Kelvin Li579e41c2016-11-30 23:51:03 +000010207 !isOpenMPParallelDirective(CurrDir) &&
10208 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +000010209 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +000010210 if (DVar.CKind != OMPC_shared) {
10211 Diag(ELoc, diag::err_omp_required_access)
10212 << getOpenMPClauseName(OMPC_lastprivate)
10213 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +000010214 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +000010215 continue;
10216 }
10217 }
Alexey Bataev74caaf22016-02-20 04:09:36 +000010218
Alexander Musman1bb328c2014-06-04 13:06:39 +000010219 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +000010220 // A variable of class type (or array thereof) that appears in a
10221 // lastprivate clause requires an accessible, unambiguous default
10222 // constructor for the class type, unless the list item is also specified
10223 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +000010224 // A variable of class type (or array thereof) that appears in a
10225 // lastprivate clause requires an accessible, unambiguous copy assignment
10226 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +000010227 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010228 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(),
10229 Type.getUnqualifiedType(), ".lastprivate.src",
10230 D->hasAttrs() ? &D->getAttrs() : nullptr);
10231 DeclRefExpr *PseudoSrcExpr =
Alexey Bataev74caaf22016-02-20 04:09:36 +000010232 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataeve3727102018-04-18 15:57:46 +000010233 VarDecl *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +000010234 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +000010235 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000010236 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +000010237 // For arrays generate assignment operation for single element and replace
10238 // it by the original array element in CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +000010239 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
10240 PseudoDstExpr, PseudoSrcExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +000010241 if (AssignmentOp.isInvalid())
10242 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +000010243 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +000010244 /*DiscardedValue=*/true);
10245 if (AssignmentOp.isInvalid())
10246 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +000010247
Alexey Bataev74caaf22016-02-20 04:09:36 +000010248 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010249 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010250 if (TopDVar.CKind == OMPC_firstprivate) {
Alexey Bataev005248a2016-02-25 05:25:57 +000010251 Ref = TopDVar.PrivateCopy;
Alexey Bataeve3727102018-04-18 15:57:46 +000010252 } else {
Alexey Bataev61205072016-03-02 04:57:40 +000010253 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000010254 if (!isOpenMPCapturedDecl(D))
Alexey Bataev005248a2016-02-25 05:25:57 +000010255 ExprCaptures.push_back(Ref->getDecl());
10256 }
10257 if (TopDVar.CKind == OMPC_firstprivate ||
Alexey Bataeve3727102018-04-18 15:57:46 +000010258 (!isOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010259 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +000010260 ExprResult RefRes = DefaultLvalueConversion(Ref);
10261 if (!RefRes.isUsable())
10262 continue;
10263 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +000010264 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
10265 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +000010266 if (!PostUpdateRes.isUsable())
10267 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +000010268 ExprPostUpdates.push_back(
10269 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +000010270 }
10271 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010272 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010273 Vars.push_back((VD || CurContext->isDependentContext())
10274 ? RefExpr->IgnoreParens()
10275 : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +000010276 SrcExprs.push_back(PseudoSrcExpr);
10277 DstExprs.push_back(PseudoDstExpr);
10278 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +000010279 }
10280
10281 if (Vars.empty())
10282 return nullptr;
10283
10284 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +000010285 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +000010286 buildPreInits(Context, ExprCaptures),
10287 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +000010288}
10289
Alexey Bataev758e55e2013-09-06 18:03:48 +000010290OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
10291 SourceLocation StartLoc,
10292 SourceLocation LParenLoc,
10293 SourceLocation EndLoc) {
10294 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +000010295 for (Expr *RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010296 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +000010297 SourceLocation ELoc;
10298 SourceRange ERange;
10299 Expr *SimpleRefExpr = RefExpr;
10300 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010301 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +000010302 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000010303 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010304 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010305 ValueDecl *D = Res.first;
10306 if (!D)
10307 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +000010308
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010309 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010310 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
10311 // in a Construct]
10312 // Variables with the predetermined data-sharing attributes may not be
10313 // listed in data-sharing attributes clauses, except for the cases
10314 // listed below. For these exceptions only, listing a predetermined
10315 // variable in a data-sharing attribute clause is allowed and overrides
10316 // the variable's predetermined data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +000010317 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataeved09d242014-05-28 05:53:51 +000010318 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
10319 DVar.RefExpr) {
10320 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
10321 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +000010322 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010323 continue;
10324 }
10325
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010326 DeclRefExpr *Ref = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000010327 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +000010328 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010329 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010330 Vars.push_back((VD || !Ref || CurContext->isDependentContext())
10331 ? RefExpr->IgnoreParens()
10332 : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010333 }
10334
Alexey Bataeved09d242014-05-28 05:53:51 +000010335 if (Vars.empty())
10336 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +000010337
10338 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
10339}
10340
Alexey Bataevc5e02582014-06-16 07:08:35 +000010341namespace {
10342class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
10343 DSAStackTy *Stack;
10344
10345public:
10346 bool VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010347 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
10348 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataevc5e02582014-06-16 07:08:35 +000010349 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
10350 return false;
10351 if (DVar.CKind != OMPC_unknown)
10352 return true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010353 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +000010354 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; },
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010355 /*FromParent=*/true);
Alexey Bataeve3727102018-04-18 15:57:46 +000010356 return DVarPrivate.CKind != OMPC_unknown;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010357 }
10358 return false;
10359 }
10360 bool VisitStmt(Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010361 for (Stmt *Child : S->children()) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010362 if (Child && Visit(Child))
10363 return true;
10364 }
10365 return false;
10366 }
Alexey Bataev23b69422014-06-18 07:08:49 +000010367 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +000010368};
Alexey Bataev23b69422014-06-18 07:08:49 +000010369} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +000010370
Alexey Bataev60da77e2016-02-29 05:54:20 +000010371namespace {
10372// Transform MemberExpression for specified FieldDecl of current class to
10373// DeclRefExpr to specified OMPCapturedExprDecl.
10374class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
10375 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
Alexey Bataeve3727102018-04-18 15:57:46 +000010376 ValueDecl *Field = nullptr;
10377 DeclRefExpr *CapturedExpr = nullptr;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010378
10379public:
10380 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
10381 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
10382
10383 ExprResult TransformMemberExpr(MemberExpr *E) {
10384 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
10385 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +000010386 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +000010387 return CapturedExpr;
10388 }
10389 return BaseTransform::TransformMemberExpr(E);
10390 }
10391 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
10392};
10393} // namespace
10394
Alexey Bataev97d18bf2018-04-11 19:21:00 +000010395template <typename T, typename U>
10396static T filterLookupForUDR(SmallVectorImpl<U> &Lookups,
10397 const llvm::function_ref<T(ValueDecl *)> Gen) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010398 for (U &Set : Lookups) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010399 for (auto *D : Set) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010400 if (T Res = Gen(cast<ValueDecl>(D)))
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010401 return Res;
10402 }
10403 }
10404 return T();
10405}
10406
Alexey Bataev43b90b72018-09-12 16:31:59 +000010407static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) {
10408 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case");
10409
10410 for (auto RD : D->redecls()) {
10411 // Don't bother with extra checks if we already know this one isn't visible.
10412 if (RD == D)
10413 continue;
10414
10415 auto ND = cast<NamedDecl>(RD);
10416 if (LookupResult::isVisible(SemaRef, ND))
10417 return ND;
10418 }
10419
10420 return nullptr;
10421}
10422
10423static void
10424argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &ReductionId,
10425 SourceLocation Loc, QualType Ty,
10426 SmallVectorImpl<UnresolvedSet<8>> &Lookups) {
10427 // Find all of the associated namespaces and classes based on the
10428 // arguments we have.
10429 Sema::AssociatedNamespaceSet AssociatedNamespaces;
10430 Sema::AssociatedClassSet AssociatedClasses;
10431 OpaqueValueExpr OVE(Loc, Ty, VK_LValue);
10432 SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces,
10433 AssociatedClasses);
10434
10435 // C++ [basic.lookup.argdep]p3:
10436 // Let X be the lookup set produced by unqualified lookup (3.4.1)
10437 // and let Y be the lookup set produced by argument dependent
10438 // lookup (defined as follows). If X contains [...] then Y is
10439 // empty. Otherwise Y is the set of declarations found in the
10440 // namespaces associated with the argument types as described
10441 // below. The set of declarations found by the lookup of the name
10442 // is the union of X and Y.
10443 //
10444 // Here, we compute Y and add its members to the overloaded
10445 // candidate set.
10446 for (auto *NS : AssociatedNamespaces) {
10447 // When considering an associated namespace, the lookup is the
10448 // same as the lookup performed when the associated namespace is
10449 // used as a qualifier (3.4.3.2) except that:
10450 //
10451 // -- Any using-directives in the associated namespace are
10452 // ignored.
10453 //
10454 // -- Any namespace-scope friend functions declared in
10455 // associated classes are visible within their respective
10456 // namespaces even if they are not visible during an ordinary
10457 // lookup (11.4).
10458 DeclContext::lookup_result R = NS->lookup(ReductionId.getName());
10459 for (auto *D : R) {
10460 auto *Underlying = D;
10461 if (auto *USD = dyn_cast<UsingShadowDecl>(D))
10462 Underlying = USD->getTargetDecl();
10463
10464 if (!isa<OMPDeclareReductionDecl>(Underlying))
10465 continue;
10466
10467 if (!SemaRef.isVisible(D)) {
10468 D = findAcceptableDecl(SemaRef, D);
10469 if (!D)
10470 continue;
10471 if (auto *USD = dyn_cast<UsingShadowDecl>(D))
10472 Underlying = USD->getTargetDecl();
10473 }
10474 Lookups.emplace_back();
10475 Lookups.back().addDecl(Underlying);
10476 }
10477 }
10478}
10479
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010480static ExprResult
10481buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
10482 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
10483 const DeclarationNameInfo &ReductionId, QualType Ty,
10484 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
10485 if (ReductionIdScopeSpec.isInvalid())
10486 return ExprError();
10487 SmallVector<UnresolvedSet<8>, 4> Lookups;
10488 if (S) {
10489 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
10490 Lookup.suppressDiagnostics();
10491 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010492 NamedDecl *D = Lookup.getRepresentativeDecl();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010493 do {
10494 S = S->getParent();
10495 } while (S && !S->isDeclScope(D));
10496 if (S)
10497 S = S->getParent();
Alexey Bataev43b90b72018-09-12 16:31:59 +000010498 Lookups.emplace_back();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010499 Lookups.back().append(Lookup.begin(), Lookup.end());
10500 Lookup.clear();
10501 }
10502 } else if (auto *ULE =
10503 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
10504 Lookups.push_back(UnresolvedSet<8>());
10505 Decl *PrevD = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000010506 for (NamedDecl *D : ULE->decls()) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010507 if (D == PrevD)
10508 Lookups.push_back(UnresolvedSet<8>());
10509 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
10510 Lookups.back().addDecl(DRD);
10511 PrevD = D;
10512 }
10513 }
Alexey Bataevfdc20352017-08-25 15:43:55 +000010514 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() ||
10515 Ty->isInstantiationDependentType() ||
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010516 Ty->containsUnexpandedParameterPack() ||
Alexey Bataeve3727102018-04-18 15:57:46 +000010517 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010518 return !D->isInvalidDecl() &&
10519 (D->getType()->isDependentType() ||
10520 D->getType()->isInstantiationDependentType() ||
10521 D->getType()->containsUnexpandedParameterPack());
10522 })) {
10523 UnresolvedSet<8> ResSet;
Alexey Bataeve3727102018-04-18 15:57:46 +000010524 for (const UnresolvedSet<8> &Set : Lookups) {
Alexey Bataev43b90b72018-09-12 16:31:59 +000010525 if (Set.empty())
10526 continue;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010527 ResSet.append(Set.begin(), Set.end());
10528 // The last item marks the end of all declarations at the specified scope.
10529 ResSet.addDecl(Set[Set.size() - 1]);
10530 }
10531 return UnresolvedLookupExpr::Create(
10532 SemaRef.Context, /*NamingClass=*/nullptr,
10533 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
10534 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
10535 }
Alexey Bataev43b90b72018-09-12 16:31:59 +000010536 // Lookup inside the classes.
10537 // C++ [over.match.oper]p3:
10538 // For a unary operator @ with an operand of a type whose
10539 // cv-unqualified version is T1, and for a binary operator @ with
10540 // a left operand of a type whose cv-unqualified version is T1 and
10541 // a right operand of a type whose cv-unqualified version is T2,
10542 // three sets of candidate functions, designated member
10543 // candidates, non-member candidates and built-in candidates, are
10544 // constructed as follows:
10545 // -- If T1 is a complete class type or a class currently being
10546 // defined, the set of member candidates is the result of the
10547 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise,
10548 // the set of member candidates is empty.
10549 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
10550 Lookup.suppressDiagnostics();
10551 if (const auto *TyRec = Ty->getAs<RecordType>()) {
10552 // Complete the type if it can be completed.
10553 // If the type is neither complete nor being defined, bail out now.
10554 if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() ||
10555 TyRec->getDecl()->getDefinition()) {
10556 Lookup.clear();
10557 SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl());
10558 if (Lookup.empty()) {
10559 Lookups.emplace_back();
10560 Lookups.back().append(Lookup.begin(), Lookup.end());
10561 }
10562 }
10563 }
10564 // Perform ADL.
10565 argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010566 if (auto *VD = filterLookupForUDR<ValueDecl *>(
10567 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
10568 if (!D->isInvalidDecl() &&
10569 SemaRef.Context.hasSameType(D->getType(), Ty))
10570 return D;
10571 return nullptr;
10572 }))
10573 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
10574 if (auto *VD = filterLookupForUDR<ValueDecl *>(
10575 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
10576 if (!D->isInvalidDecl() &&
10577 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
10578 !Ty.isMoreQualifiedThan(D->getType()))
10579 return D;
10580 return nullptr;
10581 })) {
10582 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
10583 /*DetectVirtual=*/false);
10584 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
10585 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
10586 VD->getType().getUnqualifiedType()))) {
10587 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
10588 /*DiagID=*/0) !=
10589 Sema::AR_inaccessible) {
10590 SemaRef.BuildBasePathArray(Paths, BasePath);
10591 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
10592 }
10593 }
10594 }
10595 }
10596 if (ReductionIdScopeSpec.isSet()) {
10597 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
10598 return ExprError();
10599 }
10600 return ExprEmpty();
10601}
10602
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010603namespace {
10604/// Data for the reduction-based clauses.
10605struct ReductionData {
10606 /// List of original reduction items.
10607 SmallVector<Expr *, 8> Vars;
10608 /// List of private copies of the reduction items.
10609 SmallVector<Expr *, 8> Privates;
10610 /// LHS expressions for the reduction_op expressions.
10611 SmallVector<Expr *, 8> LHSs;
10612 /// RHS expressions for the reduction_op expressions.
10613 SmallVector<Expr *, 8> RHSs;
10614 /// Reduction operation expression.
10615 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev88202be2017-07-27 13:20:36 +000010616 /// Taskgroup descriptors for the corresponding reduction items in
10617 /// in_reduction clauses.
10618 SmallVector<Expr *, 8> TaskgroupDescriptors;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010619 /// List of captures for clause.
10620 SmallVector<Decl *, 4> ExprCaptures;
10621 /// List of postupdate expressions.
10622 SmallVector<Expr *, 4> ExprPostUpdates;
10623 ReductionData() = delete;
10624 /// Reserves required memory for the reduction data.
10625 ReductionData(unsigned Size) {
10626 Vars.reserve(Size);
10627 Privates.reserve(Size);
10628 LHSs.reserve(Size);
10629 RHSs.reserve(Size);
10630 ReductionOps.reserve(Size);
Alexey Bataev88202be2017-07-27 13:20:36 +000010631 TaskgroupDescriptors.reserve(Size);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010632 ExprCaptures.reserve(Size);
10633 ExprPostUpdates.reserve(Size);
10634 }
10635 /// Stores reduction item and reduction operation only (required for dependent
10636 /// reduction item).
10637 void push(Expr *Item, Expr *ReductionOp) {
10638 Vars.emplace_back(Item);
10639 Privates.emplace_back(nullptr);
10640 LHSs.emplace_back(nullptr);
10641 RHSs.emplace_back(nullptr);
10642 ReductionOps.emplace_back(ReductionOp);
Alexey Bataev88202be2017-07-27 13:20:36 +000010643 TaskgroupDescriptors.emplace_back(nullptr);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010644 }
10645 /// Stores reduction data.
Alexey Bataev88202be2017-07-27 13:20:36 +000010646 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp,
10647 Expr *TaskgroupDescriptor) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010648 Vars.emplace_back(Item);
10649 Privates.emplace_back(Private);
10650 LHSs.emplace_back(LHS);
10651 RHSs.emplace_back(RHS);
10652 ReductionOps.emplace_back(ReductionOp);
Alexey Bataev88202be2017-07-27 13:20:36 +000010653 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010654 }
10655};
10656} // namespace
10657
Alexey Bataeve3727102018-04-18 15:57:46 +000010658static bool checkOMPArraySectionConstantForReduction(
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010659 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement,
10660 SmallVectorImpl<llvm::APSInt> &ArraySizes) {
10661 const Expr *Length = OASE->getLength();
10662 if (Length == nullptr) {
10663 // For array sections of the form [1:] or [:], we would need to analyze
10664 // the lower bound...
10665 if (OASE->getColonLoc().isValid())
10666 return false;
10667
10668 // This is an array subscript which has implicit length 1!
10669 SingleElement = true;
10670 ArraySizes.push_back(llvm::APSInt::get(1));
10671 } else {
Fangrui Songf5d33352018-11-30 21:26:09 +000010672 llvm::APSInt ConstantLengthValue;
10673 if (!Length->EvaluateAsInt(ConstantLengthValue, Context))
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010674 return false;
10675
10676 SingleElement = (ConstantLengthValue.getSExtValue() == 1);
10677 ArraySizes.push_back(ConstantLengthValue);
10678 }
10679
10680 // Get the base of this array section and walk up from there.
10681 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
10682
10683 // We require length = 1 for all array sections except the right-most to
10684 // guarantee that the memory region is contiguous and has no holes in it.
10685 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) {
10686 Length = TempOASE->getLength();
10687 if (Length == nullptr) {
10688 // For array sections of the form [1:] or [:], we would need to analyze
10689 // the lower bound...
10690 if (OASE->getColonLoc().isValid())
10691 return false;
10692
10693 // This is an array subscript which has implicit length 1!
10694 ArraySizes.push_back(llvm::APSInt::get(1));
10695 } else {
Fangrui Songf5d33352018-11-30 21:26:09 +000010696 llvm::APSInt ConstantLengthValue;
10697 if (!Length->EvaluateAsInt(ConstantLengthValue, Context) ||
10698 ConstantLengthValue.getSExtValue() != 1)
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010699 return false;
10700
10701 ArraySizes.push_back(ConstantLengthValue);
10702 }
10703 Base = TempOASE->getBase()->IgnoreParenImpCasts();
10704 }
10705
10706 // If we have a single element, we don't need to add the implicit lengths.
10707 if (!SingleElement) {
10708 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) {
10709 // Has implicit length 1!
10710 ArraySizes.push_back(llvm::APSInt::get(1));
10711 Base = TempASE->getBase()->IgnoreParenImpCasts();
10712 }
10713 }
10714
10715 // This array section can be privatized as a single value or as a constant
10716 // sized array.
10717 return true;
10718}
10719
Alexey Bataeve3727102018-04-18 15:57:46 +000010720static bool actOnOMPReductionKindClause(
Alexey Bataev169d96a2017-07-18 20:17:46 +000010721 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind,
10722 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
10723 SourceLocation ColonLoc, SourceLocation EndLoc,
10724 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010725 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010726 DeclarationName DN = ReductionId.getName();
10727 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator();
Alexey Bataevc5e02582014-06-16 07:08:35 +000010728 BinaryOperatorKind BOK = BO_Comma;
10729
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010730 ASTContext &Context = S.Context;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010731 // OpenMP [2.14.3.6, reduction clause]
10732 // C
10733 // reduction-identifier is either an identifier or one of the following
10734 // operators: +, -, *, &, |, ^, && and ||
10735 // C++
10736 // reduction-identifier is either an id-expression or one of the following
10737 // operators: +, -, *, &, |, ^, && and ||
Alexey Bataevc5e02582014-06-16 07:08:35 +000010738 switch (OOK) {
10739 case OO_Plus:
10740 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010741 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010742 break;
10743 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010744 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010745 break;
10746 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010747 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010748 break;
10749 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010750 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010751 break;
10752 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010753 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010754 break;
10755 case OO_AmpAmp:
10756 BOK = BO_LAnd;
10757 break;
10758 case OO_PipePipe:
10759 BOK = BO_LOr;
10760 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010761 case OO_New:
10762 case OO_Delete:
10763 case OO_Array_New:
10764 case OO_Array_Delete:
10765 case OO_Slash:
10766 case OO_Percent:
10767 case OO_Tilde:
10768 case OO_Exclaim:
10769 case OO_Equal:
10770 case OO_Less:
10771 case OO_Greater:
10772 case OO_LessEqual:
10773 case OO_GreaterEqual:
10774 case OO_PlusEqual:
10775 case OO_MinusEqual:
10776 case OO_StarEqual:
10777 case OO_SlashEqual:
10778 case OO_PercentEqual:
10779 case OO_CaretEqual:
10780 case OO_AmpEqual:
10781 case OO_PipeEqual:
10782 case OO_LessLess:
10783 case OO_GreaterGreater:
10784 case OO_LessLessEqual:
10785 case OO_GreaterGreaterEqual:
10786 case OO_EqualEqual:
10787 case OO_ExclaimEqual:
Richard Smithd30b23d2017-12-01 02:13:10 +000010788 case OO_Spaceship:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010789 case OO_PlusPlus:
10790 case OO_MinusMinus:
10791 case OO_Comma:
10792 case OO_ArrowStar:
10793 case OO_Arrow:
10794 case OO_Call:
10795 case OO_Subscript:
10796 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +000010797 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010798 case NUM_OVERLOADED_OPERATORS:
10799 llvm_unreachable("Unexpected reduction identifier");
10800 case OO_None:
Alexey Bataeve3727102018-04-18 15:57:46 +000010801 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010802 if (II->isStr("max"))
10803 BOK = BO_GT;
10804 else if (II->isStr("min"))
10805 BOK = BO_LT;
10806 }
10807 break;
10808 }
10809 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010810 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +000010811 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataev4d4624c2017-07-20 16:47:47 +000010812 else
10813 ReductionIdRange.setBegin(ReductionId.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +000010814 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +000010815
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010816 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
10817 bool FirstIter = true;
Alexey Bataeve3727102018-04-18 15:57:46 +000010818 for (Expr *RefExpr : VarList) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010819 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +000010820 // OpenMP [2.1, C/C++]
10821 // A list item is a variable or array section, subject to the restrictions
10822 // specified in Section 2.4 on page 42 and in each of the sections
10823 // describing clauses and directives for which a list appears.
10824 // OpenMP [2.14.3.3, Restrictions, p.1]
10825 // A variable that is part of another variable (as an array or
10826 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010827 if (!FirstIter && IR != ER)
10828 ++IR;
10829 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010830 SourceLocation ELoc;
10831 SourceRange ERange;
10832 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010833 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
Alexey Bataev60da77e2016-02-29 05:54:20 +000010834 /*AllowArraySection=*/true);
10835 if (Res.second) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010836 // Try to find 'declare reduction' corresponding construct before using
10837 // builtin/overloaded operators.
10838 QualType Type = Context.DependentTy;
10839 CXXCastPath BasePath;
10840 ExprResult DeclareReductionRef = buildDeclareReductionRef(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010841 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010842 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010843 Expr *ReductionOp = nullptr;
10844 if (S.CurContext->isDependentContext() &&
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010845 (DeclareReductionRef.isUnset() ||
10846 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010847 ReductionOp = DeclareReductionRef.get();
10848 // It will be analyzed later.
10849 RD.push(RefExpr, ReductionOp);
Alexey Bataevc5e02582014-06-16 07:08:35 +000010850 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000010851 ValueDecl *D = Res.first;
10852 if (!D)
10853 continue;
10854
Alexey Bataev88202be2017-07-27 13:20:36 +000010855 Expr *TaskgroupDescriptor = nullptr;
Alexey Bataeva1764212015-09-30 09:22:36 +000010856 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010857 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
10858 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
Alexey Bataeve3727102018-04-18 15:57:46 +000010859 if (ASE) {
Alexey Bataev31300ed2016-02-04 11:27:03 +000010860 Type = ASE->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010861 } else if (OASE) {
10862 QualType BaseType =
10863 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
10864 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
Alexey Bataeva1764212015-09-30 09:22:36 +000010865 Type = ATy->getElementType();
10866 else
10867 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +000010868 Type = Type.getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010869 } else {
Alexey Bataev60da77e2016-02-29 05:54:20 +000010870 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
Alexey Bataeve3727102018-04-18 15:57:46 +000010871 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000010872 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +000010873
Alexey Bataevc5e02582014-06-16 07:08:35 +000010874 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
10875 // A variable that appears in a private clause must not have an incomplete
10876 // type or a reference type.
Joel E. Denny3cabf732018-06-28 19:54:49 +000010877 if (S.RequireCompleteType(ELoc, D->getType(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010878 diag::err_omp_reduction_incomplete_type))
Alexey Bataevc5e02582014-06-16 07:08:35 +000010879 continue;
10880 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +000010881 // A list item that appears in a reduction clause must not be
10882 // const-qualified.
10883 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataev169d96a2017-07-18 20:17:46 +000010884 S.Diag(ELoc, diag::err_omp_const_reduction_list_item) << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010885 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010886 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10887 VarDecl::DeclarationOnly;
10888 S.Diag(D->getLocation(),
10889 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +000010890 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +000010891 }
Alexey Bataevc5e02582014-06-16 07:08:35 +000010892 continue;
10893 }
Alexey Bataevbc529672018-09-28 19:33:14 +000010894
10895 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective();
Alexey Bataevc5e02582014-06-16 07:08:35 +000010896 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
10897 // If a list-item is a reference type then it must bind to the same object
10898 // for all threads of the team.
Alexey Bataevbc529672018-09-28 19:33:14 +000010899 if (!ASE && !OASE) {
10900 if (VD) {
10901 VarDecl *VDDef = VD->getDefinition();
10902 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
10903 DSARefChecker Check(Stack);
10904 if (Check.Visit(VDDef->getInit())) {
10905 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg)
10906 << getOpenMPClauseName(ClauseKind) << ERange;
10907 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
10908 continue;
10909 }
Alexey Bataeva1764212015-09-30 09:22:36 +000010910 }
Alexey Bataevc5e02582014-06-16 07:08:35 +000010911 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010912
Alexey Bataevbc529672018-09-28 19:33:14 +000010913 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
10914 // in a Construct]
10915 // Variables with the predetermined data-sharing attributes may not be
10916 // listed in data-sharing attributes clauses, except for the cases
10917 // listed below. For these exceptions only, listing a predetermined
10918 // variable in a data-sharing attribute clause is allowed and overrides
10919 // the variable's predetermined data-sharing attributes.
10920 // OpenMP [2.14.3.6, Restrictions, p.3]
10921 // Any number of reduction clauses can be specified on the directive,
10922 // but a list item can appear only once in the reduction clauses for that
10923 // directive.
10924 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false);
10925 if (DVar.CKind == OMPC_reduction) {
10926 S.Diag(ELoc, diag::err_omp_once_referenced)
10927 << getOpenMPClauseName(ClauseKind);
10928 if (DVar.RefExpr)
10929 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
10930 continue;
10931 }
10932 if (DVar.CKind != OMPC_unknown) {
10933 S.Diag(ELoc, diag::err_omp_wrong_dsa)
10934 << getOpenMPClauseName(DVar.CKind)
10935 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataeve3727102018-04-18 15:57:46 +000010936 reportOriginalDsa(S, Stack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010937 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +000010938 }
Alexey Bataevbc529672018-09-28 19:33:14 +000010939
10940 // OpenMP [2.14.3.6, Restrictions, p.1]
10941 // A list item that appears in a reduction clause of a worksharing
10942 // construct must be shared in the parallel regions to which any of the
10943 // worksharing regions arising from the worksharing construct bind.
10944 if (isOpenMPWorksharingDirective(CurrDir) &&
10945 !isOpenMPParallelDirective(CurrDir) &&
10946 !isOpenMPTeamsDirective(CurrDir)) {
10947 DVar = Stack->getImplicitDSA(D, true);
10948 if (DVar.CKind != OMPC_shared) {
10949 S.Diag(ELoc, diag::err_omp_required_access)
10950 << getOpenMPClauseName(OMPC_reduction)
10951 << getOpenMPClauseName(OMPC_shared);
10952 reportOriginalDsa(S, Stack, D, DVar);
10953 continue;
10954 }
10955 }
Alexey Bataevf29276e2014-06-18 04:14:57 +000010956 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010957
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010958 // Try to find 'declare reduction' corresponding construct before using
10959 // builtin/overloaded operators.
10960 CXXCastPath BasePath;
10961 ExprResult DeclareReductionRef = buildDeclareReductionRef(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010962 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010963 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
10964 if (DeclareReductionRef.isInvalid())
10965 continue;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010966 if (S.CurContext->isDependentContext() &&
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010967 (DeclareReductionRef.isUnset() ||
10968 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010969 RD.push(RefExpr, DeclareReductionRef.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010970 continue;
10971 }
10972 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
10973 // Not allowed reduction identifier is found.
Stephen Kellyf2ceec42018-08-09 21:08:08 +000010974 S.Diag(ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010975 diag::err_omp_unknown_reduction_identifier)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010976 << Type << ReductionIdRange;
10977 continue;
10978 }
10979
10980 // OpenMP [2.14.3.6, reduction clause, Restrictions]
10981 // The type of a list item that appears in a reduction clause must be valid
10982 // for the reduction-identifier. For a max or min reduction in C, the type
10983 // of the list item must be an allowed arithmetic data type: char, int,
10984 // float, double, or _Bool, possibly modified with long, short, signed, or
10985 // unsigned. For a max or min reduction in C++, the type of the list item
10986 // must be an allowed arithmetic data type: char, wchar_t, int, float,
10987 // double, or bool, possibly modified with long, short, signed, or unsigned.
10988 if (DeclareReductionRef.isUnset()) {
10989 if ((BOK == BO_GT || BOK == BO_LT) &&
10990 !(Type->isScalarType() ||
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010991 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
10992 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
Alexey Bataev169d96a2017-07-18 20:17:46 +000010993 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010994 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010995 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10996 VarDecl::DeclarationOnly;
10997 S.Diag(D->getLocation(),
10998 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010999 << D;
11000 }
11001 continue;
11002 }
11003 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011004 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) {
Alexey Bataev169d96a2017-07-18 20:17:46 +000011005 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg)
11006 << getOpenMPClauseName(ClauseKind);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011007 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011008 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
11009 VarDecl::DeclarationOnly;
11010 S.Diag(D->getLocation(),
11011 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011012 << D;
11013 }
11014 continue;
11015 }
11016 }
11017
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011018 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000011019 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs",
11020 D->hasAttrs() ? &D->getAttrs() : nullptr);
11021 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(),
11022 D->hasAttrs() ? &D->getAttrs() : nullptr);
11023 QualType PrivateTy = Type;
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011024
11025 // Try if we can determine constant lengths for all array sections and avoid
11026 // the VLA.
11027 bool ConstantLengthOASE = false;
11028 if (OASE) {
11029 bool SingleElement;
11030 llvm::SmallVector<llvm::APSInt, 4> ArraySizes;
Alexey Bataeve3727102018-04-18 15:57:46 +000011031 ConstantLengthOASE = checkOMPArraySectionConstantForReduction(
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011032 Context, OASE, SingleElement, ArraySizes);
11033
11034 // If we don't have a single element, we must emit a constant array type.
11035 if (ConstantLengthOASE && !SingleElement) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011036 for (llvm::APSInt &Size : ArraySizes)
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011037 PrivateTy = Context.getConstantArrayType(
11038 PrivateTy, Size, ArrayType::Normal, /*IndexTypeQuals=*/0);
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011039 }
11040 }
11041
11042 if ((OASE && !ConstantLengthOASE) ||
Jonas Hahnfeld96087f32017-11-02 13:30:42 +000011043 (!OASE && !ASE &&
Alexey Bataev60da77e2016-02-29 05:54:20 +000011044 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Jonas Hahnfeld87d44262017-11-18 21:00:46 +000011045 if (!Context.getTargetInfo().isVLASupported() &&
11046 S.shouldDiagnoseTargetSupportFromOpenMP()) {
11047 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
11048 S.Diag(ELoc, diag::note_vla_unsupported);
11049 continue;
11050 }
David Majnemer9d168222016-08-05 17:44:54 +000011051 // For arrays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011052 // Create pseudo array type for private copy. The size for this array will
11053 // be generated during codegen.
11054 // For array subscripts or single variables Private Ty is the same as Type
11055 // (type of the variable or single array element).
11056 PrivateTy = Context.getVariableArrayType(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011057 Type,
Alexey Bataevd070a582017-10-25 15:54:04 +000011058 new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue),
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011059 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +000011060 } else if (!ASE && !OASE &&
Alexey Bataeve3727102018-04-18 15:57:46 +000011061 Context.getAsArrayType(D->getType().getNonReferenceType())) {
Alexey Bataev60da77e2016-02-29 05:54:20 +000011062 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000011063 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011064 // Private copy.
Alexey Bataeve3727102018-04-18 15:57:46 +000011065 VarDecl *PrivateVD =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000011066 buildVarDecl(S, ELoc, PrivateTy, D->getName(),
11067 D->hasAttrs() ? &D->getAttrs() : nullptr,
11068 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011069 // Add initializer for private variable.
11070 Expr *Init = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000011071 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc);
11072 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011073 if (DeclareReductionRef.isUsable()) {
11074 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
11075 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
11076 if (DRD->getInitializer()) {
11077 Init = DRDRef;
11078 RHSVD->setInit(DRDRef);
11079 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011080 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011081 } else {
11082 switch (BOK) {
11083 case BO_Add:
11084 case BO_Xor:
11085 case BO_Or:
11086 case BO_LOr:
11087 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
11088 if (Type->isScalarType() || Type->isAnyComplexType())
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011089 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011090 break;
11091 case BO_Mul:
11092 case BO_LAnd:
11093 if (Type->isScalarType() || Type->isAnyComplexType()) {
11094 // '*' and '&&' reduction ops - initializer is '1'.
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011095 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +000011096 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011097 break;
11098 case BO_And: {
11099 // '&' reduction op - initializer is '~0'.
11100 QualType OrigType = Type;
11101 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
11102 Type = ComplexTy->getElementType();
11103 if (Type->isRealFloatingType()) {
11104 llvm::APFloat InitValue =
11105 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
11106 /*isIEEE=*/true);
11107 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
11108 Type, ELoc);
11109 } else if (Type->isScalarType()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011110 uint64_t Size = Context.getTypeSize(Type);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011111 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
11112 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
11113 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
11114 }
11115 if (Init && OrigType->isAnyComplexType()) {
11116 // Init = 0xFFFF + 0xFFFFi;
11117 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011118 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011119 }
11120 Type = OrigType;
11121 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011122 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011123 case BO_LT:
11124 case BO_GT: {
11125 // 'min' reduction op - initializer is 'Largest representable number in
11126 // the reduction list item type'.
11127 // 'max' reduction op - initializer is 'Least representable number in
11128 // the reduction list item type'.
11129 if (Type->isIntegerType() || Type->isPointerType()) {
11130 bool IsSigned = Type->hasSignedIntegerRepresentation();
Alexey Bataeve3727102018-04-18 15:57:46 +000011131 uint64_t Size = Context.getTypeSize(Type);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011132 QualType IntTy =
11133 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
11134 llvm::APInt InitValue =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011135 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
11136 : llvm::APInt::getMinValue(Size)
11137 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
11138 : llvm::APInt::getMaxValue(Size);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011139 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
11140 if (Type->isPointerType()) {
11141 // Cast to pointer type.
Alexey Bataeve3727102018-04-18 15:57:46 +000011142 ExprResult CastExpr = S.BuildCStyleCastExpr(
Alexey Bataevd070a582017-10-25 15:54:04 +000011143 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011144 if (CastExpr.isInvalid())
11145 continue;
11146 Init = CastExpr.get();
11147 }
11148 } else if (Type->isRealFloatingType()) {
11149 llvm::APFloat InitValue = llvm::APFloat::getLargest(
11150 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
11151 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
11152 Type, ELoc);
11153 }
11154 break;
11155 }
11156 case BO_PtrMemD:
11157 case BO_PtrMemI:
11158 case BO_MulAssign:
11159 case BO_Div:
11160 case BO_Rem:
11161 case BO_Sub:
11162 case BO_Shl:
11163 case BO_Shr:
11164 case BO_LE:
11165 case BO_GE:
11166 case BO_EQ:
11167 case BO_NE:
Richard Smithc70f1d62017-12-14 15:16:18 +000011168 case BO_Cmp:
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011169 case BO_AndAssign:
11170 case BO_XorAssign:
11171 case BO_OrAssign:
11172 case BO_Assign:
11173 case BO_AddAssign:
11174 case BO_SubAssign:
11175 case BO_DivAssign:
11176 case BO_RemAssign:
11177 case BO_ShlAssign:
11178 case BO_ShrAssign:
11179 case BO_Comma:
11180 llvm_unreachable("Unexpected reduction operation");
11181 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011182 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011183 if (Init && DeclareReductionRef.isUnset())
11184 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false);
11185 else if (!Init)
11186 S.ActOnUninitializedDecl(RHSVD);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011187 if (RHSVD->isInvalidDecl())
11188 continue;
11189 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011190 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible)
11191 << Type << ReductionIdRange;
11192 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
11193 VarDecl::DeclarationOnly;
11194 S.Diag(D->getLocation(),
11195 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +000011196 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011197 continue;
11198 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011199 // Store initializer for single element in private copy. Will be used during
11200 // codegen.
11201 PrivateVD->setInit(RHSVD->getInit());
11202 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataeve3727102018-04-18 15:57:46 +000011203 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011204 ExprResult ReductionOp;
11205 if (DeclareReductionRef.isUsable()) {
11206 QualType RedTy = DeclareReductionRef.get()->getType();
11207 QualType PtrRedTy = Context.getPointerType(RedTy);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011208 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
11209 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011210 if (!BasePath.empty()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011211 LHS = S.DefaultLvalueConversion(LHS.get());
11212 RHS = S.DefaultLvalueConversion(RHS.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011213 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
11214 CK_UncheckedDerivedToBase, LHS.get(),
11215 &BasePath, LHS.get()->getValueKind());
11216 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
11217 CK_UncheckedDerivedToBase, RHS.get(),
11218 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011219 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011220 FunctionProtoType::ExtProtoInfo EPI;
11221 QualType Params[] = {PtrRedTy, PtrRedTy};
11222 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
11223 auto *OVE = new (Context) OpaqueValueExpr(
11224 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011225 S.DefaultLvalueConversion(DeclareReductionRef.get()).get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011226 Expr *Args[] = {LHS.get(), RHS.get()};
11227 ReductionOp = new (Context)
11228 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
11229 } else {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011230 ReductionOp = S.BuildBinOp(
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011231 Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, LHSDRE, RHSDRE);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011232 if (ReductionOp.isUsable()) {
11233 if (BOK != BO_LT && BOK != BO_GT) {
11234 ReductionOp =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011235 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011236 BO_Assign, LHSDRE, ReductionOp.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011237 } else {
Alexey Bataevd070a582017-10-25 15:54:04 +000011238 auto *ConditionalOp = new (Context)
11239 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE,
11240 Type, VK_LValue, OK_Ordinary);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011241 ReductionOp =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011242 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011243 BO_Assign, LHSDRE, ConditionalOp);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011244 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000011245 if (ReductionOp.isUsable())
11246 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011247 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000011248 if (!ReductionOp.isUsable())
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011249 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +000011250 }
11251
Alexey Bataevfa312f32017-07-21 18:48:21 +000011252 // OpenMP [2.15.4.6, Restrictions, p.2]
11253 // A list item that appears in an in_reduction clause of a task construct
11254 // must appear in a task_reduction clause of a construct associated with a
11255 // taskgroup region that includes the participating task in its taskgroup
11256 // set. The construct associated with the innermost region that meets this
11257 // condition must specify the same reduction-identifier as the in_reduction
11258 // clause.
11259 if (ClauseKind == OMPC_in_reduction) {
Alexey Bataevfa312f32017-07-21 18:48:21 +000011260 SourceRange ParentSR;
11261 BinaryOperatorKind ParentBOK;
11262 const Expr *ParentReductionOp;
Alexey Bataev88202be2017-07-27 13:20:36 +000011263 Expr *ParentBOKTD, *ParentReductionOpTD;
Alexey Bataevf189cb72017-07-24 14:52:13 +000011264 DSAStackTy::DSAVarData ParentBOKDSA =
Alexey Bataev88202be2017-07-27 13:20:36 +000011265 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK,
11266 ParentBOKTD);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011267 DSAStackTy::DSAVarData ParentReductionOpDSA =
Alexey Bataev88202be2017-07-27 13:20:36 +000011268 Stack->getTopMostTaskgroupReductionData(
11269 D, ParentSR, ParentReductionOp, ParentReductionOpTD);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011270 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown;
11271 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown;
11272 if (!IsParentBOK && !IsParentReductionOp) {
11273 S.Diag(ELoc, diag::err_omp_in_reduction_not_task_reduction);
11274 continue;
11275 }
Alexey Bataevfa312f32017-07-21 18:48:21 +000011276 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) ||
11277 (DeclareReductionRef.isUsable() && IsParentBOK) || BOK != ParentBOK ||
11278 IsParentReductionOp) {
11279 bool EmitError = true;
11280 if (IsParentReductionOp && DeclareReductionRef.isUsable()) {
11281 llvm::FoldingSetNodeID RedId, ParentRedId;
11282 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true);
11283 DeclareReductionRef.get()->Profile(RedId, Context,
11284 /*Canonical=*/true);
11285 EmitError = RedId != ParentRedId;
11286 }
11287 if (EmitError) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011288 S.Diag(ReductionId.getBeginLoc(),
Alexey Bataevfa312f32017-07-21 18:48:21 +000011289 diag::err_omp_reduction_identifier_mismatch)
11290 << ReductionIdRange << RefExpr->getSourceRange();
11291 S.Diag(ParentSR.getBegin(),
11292 diag::note_omp_previous_reduction_identifier)
Alexey Bataevf189cb72017-07-24 14:52:13 +000011293 << ParentSR
11294 << (IsParentBOK ? ParentBOKDSA.RefExpr
11295 : ParentReductionOpDSA.RefExpr)
11296 ->getSourceRange();
Alexey Bataevfa312f32017-07-21 18:48:21 +000011297 continue;
11298 }
11299 }
Alexey Bataev88202be2017-07-27 13:20:36 +000011300 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD;
11301 assert(TaskgroupDescriptor && "Taskgroup descriptor must be defined.");
Alexey Bataevfa312f32017-07-21 18:48:21 +000011302 }
11303
Alexey Bataev60da77e2016-02-29 05:54:20 +000011304 DeclRefExpr *Ref = nullptr;
11305 Expr *VarsExpr = RefExpr->IgnoreParens();
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011306 if (!VD && !S.CurContext->isDependentContext()) {
Alexey Bataev60da77e2016-02-29 05:54:20 +000011307 if (ASE || OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011308 TransformExprToCaptures RebuildToCapture(S, D);
Alexey Bataev60da77e2016-02-29 05:54:20 +000011309 VarsExpr =
11310 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
11311 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +000011312 } else {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011313 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +000011314 }
Alexey Bataeve3727102018-04-18 15:57:46 +000011315 if (!S.isOpenMPCapturedDecl(D)) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011316 RD.ExprCaptures.emplace_back(Ref->getDecl());
Alexey Bataev5a3af132016-03-29 08:58:54 +000011317 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011318 ExprResult RefRes = S.DefaultLvalueConversion(Ref);
Alexey Bataev5a3af132016-03-29 08:58:54 +000011319 if (!RefRes.isUsable())
11320 continue;
11321 ExprResult PostUpdateRes =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011322 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
11323 RefRes.get());
Alexey Bataev5a3af132016-03-29 08:58:54 +000011324 if (!PostUpdateRes.isUsable())
11325 continue;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011326 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
11327 Stack->getCurrentDirective() == OMPD_taskgroup) {
11328 S.Diag(RefExpr->getExprLoc(),
11329 diag::err_omp_reduction_non_addressable_expression)
Alexey Bataevbcd0ae02017-07-11 19:16:44 +000011330 << RefExpr->getSourceRange();
11331 continue;
11332 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011333 RD.ExprPostUpdates.emplace_back(
11334 S.IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +000011335 }
11336 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000011337 }
Alexey Bataev169d96a2017-07-18 20:17:46 +000011338 // All reduction items are still marked as reduction (to do not increase
11339 // code base size).
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011340 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011341 if (CurrDir == OMPD_taskgroup) {
11342 if (DeclareReductionRef.isUsable())
Alexey Bataev3b1b8952017-07-25 15:53:26 +000011343 Stack->addTaskgroupReductionData(D, ReductionIdRange,
11344 DeclareReductionRef.get());
Alexey Bataevf189cb72017-07-24 14:52:13 +000011345 else
Alexey Bataev3b1b8952017-07-25 15:53:26 +000011346 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011347 }
Alexey Bataev88202be2017-07-27 13:20:36 +000011348 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(),
11349 TaskgroupDescriptor);
Alexey Bataevc5e02582014-06-16 07:08:35 +000011350 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011351 return RD.Vars.empty();
11352}
Alexey Bataevc5e02582014-06-16 07:08:35 +000011353
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011354OMPClause *Sema::ActOnOpenMPReductionClause(
11355 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11356 SourceLocation ColonLoc, SourceLocation EndLoc,
11357 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11358 ArrayRef<Expr *> UnresolvedReductions) {
11359 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000011360 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList,
Alexey Bataev169d96a2017-07-18 20:17:46 +000011361 StartLoc, LParenLoc, ColonLoc, EndLoc,
11362 ReductionIdScopeSpec, ReductionId,
11363 UnresolvedReductions, RD))
Alexey Bataevc5e02582014-06-16 07:08:35 +000011364 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +000011365
Alexey Bataevc5e02582014-06-16 07:08:35 +000011366 return OMPReductionClause::Create(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011367 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11368 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
11369 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
11370 buildPreInits(Context, RD.ExprCaptures),
11371 buildPostUpdate(*this, RD.ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +000011372}
11373
Alexey Bataev169d96a2017-07-18 20:17:46 +000011374OMPClause *Sema::ActOnOpenMPTaskReductionClause(
11375 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11376 SourceLocation ColonLoc, SourceLocation EndLoc,
11377 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11378 ArrayRef<Expr *> UnresolvedReductions) {
11379 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000011380 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList,
11381 StartLoc, LParenLoc, ColonLoc, EndLoc,
11382 ReductionIdScopeSpec, ReductionId,
Alexey Bataev169d96a2017-07-18 20:17:46 +000011383 UnresolvedReductions, RD))
11384 return nullptr;
11385
11386 return OMPTaskReductionClause::Create(
11387 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11388 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
11389 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
11390 buildPreInits(Context, RD.ExprCaptures),
11391 buildPostUpdate(*this, RD.ExprPostUpdates));
11392}
11393
Alexey Bataevfa312f32017-07-21 18:48:21 +000011394OMPClause *Sema::ActOnOpenMPInReductionClause(
11395 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11396 SourceLocation ColonLoc, SourceLocation EndLoc,
11397 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11398 ArrayRef<Expr *> UnresolvedReductions) {
11399 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000011400 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList,
Alexey Bataevfa312f32017-07-21 18:48:21 +000011401 StartLoc, LParenLoc, ColonLoc, EndLoc,
11402 ReductionIdScopeSpec, ReductionId,
11403 UnresolvedReductions, RD))
11404 return nullptr;
11405
11406 return OMPInReductionClause::Create(
11407 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11408 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
Alexey Bataev88202be2017-07-27 13:20:36 +000011409 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors,
Alexey Bataevfa312f32017-07-21 18:48:21 +000011410 buildPreInits(Context, RD.ExprCaptures),
11411 buildPostUpdate(*this, RD.ExprPostUpdates));
11412}
11413
Alexey Bataevecba70f2016-04-12 11:02:11 +000011414bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
11415 SourceLocation LinLoc) {
11416 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
11417 LinKind == OMPC_LINEAR_unknown) {
11418 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
11419 return true;
11420 }
11421 return false;
11422}
11423
Alexey Bataeve3727102018-04-18 15:57:46 +000011424bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc,
Alexey Bataevecba70f2016-04-12 11:02:11 +000011425 OpenMPLinearClauseKind LinKind,
11426 QualType Type) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011427 const auto *VD = dyn_cast_or_null<VarDecl>(D);
Alexey Bataevecba70f2016-04-12 11:02:11 +000011428 // A variable must not have an incomplete type or a reference type.
11429 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
11430 return true;
11431 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
11432 !Type->isReferenceType()) {
11433 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
11434 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
11435 return true;
11436 }
11437 Type = Type.getNonReferenceType();
11438
11439 // A list item must not be const-qualified.
11440 if (Type.isConstant(Context)) {
11441 Diag(ELoc, diag::err_omp_const_variable)
11442 << getOpenMPClauseName(OMPC_linear);
11443 if (D) {
11444 bool IsDecl =
11445 !VD ||
11446 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
11447 Diag(D->getLocation(),
11448 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
11449 << D;
11450 }
11451 return true;
11452 }
11453
11454 // A list item must be of integral or pointer type.
11455 Type = Type.getUnqualifiedType().getCanonicalType();
11456 const auto *Ty = Type.getTypePtrOrNull();
11457 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
11458 !Ty->isPointerType())) {
11459 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
11460 if (D) {
11461 bool IsDecl =
11462 !VD ||
11463 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
11464 Diag(D->getLocation(),
11465 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
11466 << D;
11467 }
11468 return true;
11469 }
11470 return false;
11471}
11472
Alexey Bataev182227b2015-08-20 10:54:39 +000011473OMPClause *Sema::ActOnOpenMPLinearClause(
11474 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
11475 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
11476 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +000011477 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011478 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +000011479 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +000011480 SmallVector<Decl *, 4> ExprCaptures;
11481 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +000011482 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +000011483 LinKind = OMPC_LINEAR_val;
Alexey Bataeve3727102018-04-18 15:57:46 +000011484 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000011485 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011486 SourceLocation ELoc;
11487 SourceRange ERange;
11488 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011489 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011490 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +000011491 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000011492 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011493 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +000011494 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +000011495 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011496 ValueDecl *D = Res.first;
11497 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +000011498 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +000011499
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011500 QualType Type = D->getType();
11501 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +000011502
11503 // OpenMP [2.14.3.7, linear clause]
11504 // A list-item cannot appear in more than one linear clause.
11505 // A list-item that appears in a linear clause cannot appear in any
11506 // other data-sharing attribute clause.
Alexey Bataeve3727102018-04-18 15:57:46 +000011507 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexander Musman8dba6642014-04-22 13:09:42 +000011508 if (DVar.RefExpr) {
11509 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
11510 << getOpenMPClauseName(OMPC_linear);
Alexey Bataeve3727102018-04-18 15:57:46 +000011511 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +000011512 continue;
11513 }
11514
Alexey Bataevecba70f2016-04-12 11:02:11 +000011515 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +000011516 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +000011517 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +000011518
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011519 // Build private copy of original var.
Alexey Bataeve3727102018-04-18 15:57:46 +000011520 VarDecl *Private =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000011521 buildVarDecl(*this, ELoc, Type, D->getName(),
11522 D->hasAttrs() ? &D->getAttrs() : nullptr,
11523 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011524 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +000011525 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011526 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011527 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011528 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000011529 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev78849fb2016-03-09 09:49:00 +000011530 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011531 if (!isOpenMPCapturedDecl(D)) {
Alexey Bataev78849fb2016-03-09 09:49:00 +000011532 ExprCaptures.push_back(Ref->getDecl());
11533 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
11534 ExprResult RefRes = DefaultLvalueConversion(Ref);
11535 if (!RefRes.isUsable())
11536 continue;
11537 ExprResult PostUpdateRes =
11538 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
11539 SimpleRefExpr, RefRes.get());
11540 if (!PostUpdateRes.isUsable())
11541 continue;
11542 ExprPostUpdates.push_back(
11543 IgnoredValueConversions(PostUpdateRes.get()).get());
11544 }
11545 }
11546 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011547 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011548 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011549 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011550 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011551 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000011552 /*DirectInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011553 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011554
11555 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000011556 Vars.push_back((VD || CurContext->isDependentContext())
11557 ? RefExpr->IgnoreParens()
11558 : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011559 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +000011560 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +000011561 }
11562
11563 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000011564 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000011565
11566 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +000011567 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000011568 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
11569 !Step->isInstantiationDependent() &&
11570 !Step->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011571 SourceLocation StepLoc = Step->getBeginLoc();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +000011572 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +000011573 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000011574 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +000011575 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +000011576
Alexander Musman3276a272015-03-21 10:12:56 +000011577 // Build var to save the step value.
11578 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +000011579 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +000011580 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +000011581 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +000011582 ExprResult CalcStep =
11583 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +000011584 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +000011585
Alexander Musman8dba6642014-04-22 13:09:42 +000011586 // Warn about zero linear step (it would be probably better specified as
11587 // making corresponding variables 'const').
11588 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +000011589 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
11590 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +000011591 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
11592 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +000011593 if (!IsConstant && CalcStep.isUsable()) {
11594 // Calculate the step beforehand instead of doing this on each iteration.
11595 // (This is not used if the number of iterations may be kfold-ed).
11596 CalcStepExpr = CalcStep.get();
11597 }
Alexander Musman8dba6642014-04-22 13:09:42 +000011598 }
11599
Alexey Bataev182227b2015-08-20 10:54:39 +000011600 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
11601 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +000011602 StepExpr, CalcStepExpr,
11603 buildPreInits(Context, ExprCaptures),
11604 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +000011605}
11606
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011607static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
11608 Expr *NumIterations, Sema &SemaRef,
11609 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +000011610 // Walk the vars and build update/final expressions for the CodeGen.
11611 SmallVector<Expr *, 8> Updates;
11612 SmallVector<Expr *, 8> Finals;
11613 Expr *Step = Clause.getStep();
11614 Expr *CalcStep = Clause.getCalcStep();
11615 // OpenMP [2.14.3.7, linear clause]
11616 // If linear-step is not specified it is assumed to be 1.
Alexey Bataeve3727102018-04-18 15:57:46 +000011617 if (!Step)
Alexander Musman3276a272015-03-21 10:12:56 +000011618 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000011619 else if (CalcStep)
Alexander Musman3276a272015-03-21 10:12:56 +000011620 Step = cast<BinaryOperator>(CalcStep)->getLHS();
11621 bool HasErrors = false;
11622 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011623 auto CurPrivate = Clause.privates().begin();
Alexey Bataeve3727102018-04-18 15:57:46 +000011624 OpenMPLinearClauseKind LinKind = Clause.getModifier();
11625 for (Expr *RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011626 SourceLocation ELoc;
11627 SourceRange ERange;
11628 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011629 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange);
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011630 ValueDecl *D = Res.first;
11631 if (Res.second || !D) {
11632 Updates.push_back(nullptr);
11633 Finals.push_back(nullptr);
11634 HasErrors = true;
11635 continue;
11636 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011637 auto &&Info = Stack->isLoopControlVariable(D);
Alexey Bataev2b86f212017-11-29 21:31:48 +000011638 // OpenMP [2.15.11, distribute simd Construct]
11639 // A list item may not appear in a linear clause, unless it is the loop
11640 // iteration variable.
11641 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) &&
11642 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) {
11643 SemaRef.Diag(ELoc,
11644 diag::err_omp_linear_distribute_var_non_loop_iteration);
11645 Updates.push_back(nullptr);
11646 Finals.push_back(nullptr);
11647 HasErrors = true;
11648 continue;
11649 }
Alexander Musman3276a272015-03-21 10:12:56 +000011650 Expr *InitExpr = *CurInit;
11651
11652 // Build privatized reference to the current linear var.
David Majnemer9d168222016-08-05 17:44:54 +000011653 auto *DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011654 Expr *CapturedRef;
11655 if (LinKind == OMPC_LINEAR_uval)
11656 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
11657 else
11658 CapturedRef =
11659 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
11660 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
11661 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +000011662
11663 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011664 ExprResult Update;
Alexey Bataeve3727102018-04-18 15:57:46 +000011665 if (!Info.first)
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011666 Update =
Alexey Bataeve3727102018-04-18 15:57:46 +000011667 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011668 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011669 else
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011670 Update = *CurPrivate;
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011671 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +000011672 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +000011673
11674 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011675 ExprResult Final;
Alexey Bataeve3727102018-04-18 15:57:46 +000011676 if (!Info.first)
11677 Final =
11678 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
11679 InitExpr, NumIterations, Step, /*Subtract=*/false);
11680 else
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011681 Final = *CurPrivate;
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011682 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +000011683 /*DiscardedValue=*/true);
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011684
Alexander Musman3276a272015-03-21 10:12:56 +000011685 if (!Update.isUsable() || !Final.isUsable()) {
11686 Updates.push_back(nullptr);
11687 Finals.push_back(nullptr);
11688 HasErrors = true;
11689 } else {
11690 Updates.push_back(Update.get());
11691 Finals.push_back(Final.get());
11692 }
Richard Trieucc3949d2016-02-18 22:34:54 +000011693 ++CurInit;
11694 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +000011695 }
11696 Clause.setUpdates(Updates);
11697 Clause.setFinals(Finals);
11698 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +000011699}
11700
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011701OMPClause *Sema::ActOnOpenMPAlignedClause(
11702 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
11703 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011704 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +000011705 for (Expr *RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +000011706 assert(RefExpr && "NULL expr in OpenMP linear clause.");
11707 SourceLocation ELoc;
11708 SourceRange ERange;
11709 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011710 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev1efd1662016-03-29 10:59:56 +000011711 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011712 // It will be analyzed later.
11713 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011714 }
Alexey Bataev1efd1662016-03-29 10:59:56 +000011715 ValueDecl *D = Res.first;
11716 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011717 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011718
Alexey Bataev1efd1662016-03-29 10:59:56 +000011719 QualType QType = D->getType();
11720 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011721
11722 // OpenMP [2.8.1, simd construct, Restrictions]
11723 // The type of list items appearing in the aligned clause must be
11724 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011725 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011726 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +000011727 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011728 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +000011729 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011730 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +000011731 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011732 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +000011733 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011734 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +000011735 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011736 continue;
11737 }
11738
11739 // OpenMP [2.8.1, simd construct, Restrictions]
11740 // A list-item cannot appear in more than one aligned clause.
Alexey Bataeve3727102018-04-18 15:57:46 +000011741 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +000011742 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011743 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
11744 << getOpenMPClauseName(OMPC_aligned);
11745 continue;
11746 }
11747
Alexey Bataev1efd1662016-03-29 10:59:56 +000011748 DeclRefExpr *Ref = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000011749 if (!VD && isOpenMPCapturedDecl(D))
Alexey Bataev1efd1662016-03-29 10:59:56 +000011750 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
11751 Vars.push_back(DefaultFunctionArrayConversion(
11752 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
11753 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011754 }
11755
11756 // OpenMP [2.8.1, simd construct, Description]
11757 // The parameter of the aligned clause, alignment, must be a constant
11758 // positive integer expression.
11759 // If no optional parameter is specified, implementation-defined default
11760 // alignments for SIMD instructions on the target platforms are assumed.
11761 if (Alignment != nullptr) {
11762 ExprResult AlignResult =
11763 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
11764 if (AlignResult.isInvalid())
11765 return nullptr;
11766 Alignment = AlignResult.get();
11767 }
11768 if (Vars.empty())
11769 return nullptr;
11770
11771 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
11772 EndLoc, Vars, Alignment);
11773}
11774
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011775OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
11776 SourceLocation StartLoc,
11777 SourceLocation LParenLoc,
11778 SourceLocation EndLoc) {
11779 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011780 SmallVector<Expr *, 8> SrcExprs;
11781 SmallVector<Expr *, 8> DstExprs;
11782 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeve3727102018-04-18 15:57:46 +000011783 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000011784 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
11785 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011786 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000011787 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011788 SrcExprs.push_back(nullptr);
11789 DstExprs.push_back(nullptr);
11790 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011791 continue;
11792 }
11793
Alexey Bataeved09d242014-05-28 05:53:51 +000011794 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011795 // OpenMP [2.1, C/C++]
11796 // A list item is a variable name.
11797 // OpenMP [2.14.4.1, Restrictions, p.1]
11798 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeve3727102018-04-18 15:57:46 +000011799 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011800 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000011801 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
11802 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011803 continue;
11804 }
11805
11806 Decl *D = DE->getDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000011807 auto *VD = cast<VarDecl>(D);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011808
11809 QualType Type = VD->getType();
11810 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
11811 // It will be analyzed later.
11812 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011813 SrcExprs.push_back(nullptr);
11814 DstExprs.push_back(nullptr);
11815 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011816 continue;
11817 }
11818
11819 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
11820 // A list item that appears in a copyin clause must be threadprivate.
11821 if (!DSAStack->isThreadPrivate(VD)) {
11822 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +000011823 << getOpenMPClauseName(OMPC_copyin)
11824 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011825 continue;
11826 }
11827
11828 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
11829 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +000011830 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011831 // operator for the class type.
Alexey Bataeve3727102018-04-18 15:57:46 +000011832 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
11833 VarDecl *SrcVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011834 buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000011835 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011836 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011837 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +000011838 VarDecl *DstVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011839 buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst",
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000011840 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011841 DeclRefExpr *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011842 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011843 // For arrays generate assignment operation for single element and replace
11844 // it by the original array element in CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +000011845 ExprResult AssignmentOp =
11846 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr,
11847 PseudoSrcExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011848 if (AssignmentOp.isInvalid())
11849 continue;
11850 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
11851 /*DiscardedValue=*/true);
11852 if (AssignmentOp.isInvalid())
11853 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011854
11855 DSAStack->addDSA(VD, DE, OMPC_copyin);
11856 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011857 SrcExprs.push_back(PseudoSrcExpr);
11858 DstExprs.push_back(PseudoDstExpr);
11859 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011860 }
11861
Alexey Bataeved09d242014-05-28 05:53:51 +000011862 if (Vars.empty())
11863 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011864
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011865 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
11866 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011867}
11868
Alexey Bataevbae9a792014-06-27 10:37:06 +000011869OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
11870 SourceLocation StartLoc,
11871 SourceLocation LParenLoc,
11872 SourceLocation EndLoc) {
11873 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +000011874 SmallVector<Expr *, 8> SrcExprs;
11875 SmallVector<Expr *, 8> DstExprs;
11876 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeve3727102018-04-18 15:57:46 +000011877 for (Expr *RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +000011878 assert(RefExpr && "NULL expr in OpenMP linear clause.");
11879 SourceLocation ELoc;
11880 SourceRange ERange;
11881 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011882 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataeve122da12016-03-17 10:50:17 +000011883 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000011884 // It will be analyzed later.
11885 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000011886 SrcExprs.push_back(nullptr);
11887 DstExprs.push_back(nullptr);
11888 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011889 }
Alexey Bataeve122da12016-03-17 10:50:17 +000011890 ValueDecl *D = Res.first;
11891 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +000011892 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000011893
Alexey Bataeve122da12016-03-17 10:50:17 +000011894 QualType Type = D->getType();
11895 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011896
11897 // OpenMP [2.14.4.2, Restrictions, p.2]
11898 // A list item that appears in a copyprivate clause may not appear in a
11899 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +000011900 if (!VD || !DSAStack->isThreadPrivate(VD)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011901 DSAStackTy::DSAVarData DVar =
11902 DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataeva63048e2015-03-23 06:18:07 +000011903 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
11904 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000011905 Diag(ELoc, diag::err_omp_wrong_dsa)
11906 << getOpenMPClauseName(DVar.CKind)
11907 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000011908 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011909 continue;
11910 }
11911
11912 // OpenMP [2.11.4.2, Restrictions, p.1]
11913 // All list items that appear in a copyprivate clause must be either
11914 // threadprivate or private in the enclosing context.
11915 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +000011916 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011917 if (DVar.CKind == OMPC_shared) {
11918 Diag(ELoc, diag::err_omp_required_access)
11919 << getOpenMPClauseName(OMPC_copyprivate)
11920 << "threadprivate or private in the enclosing context";
Alexey Bataeve3727102018-04-18 15:57:46 +000011921 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011922 continue;
11923 }
11924 }
11925 }
11926
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011927 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000011928 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011929 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +000011930 << getOpenMPClauseName(OMPC_copyprivate) << Type
11931 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011932 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +000011933 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011934 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +000011935 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011936 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +000011937 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011938 continue;
11939 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +000011940
Alexey Bataevbae9a792014-06-27 10:37:06 +000011941 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
11942 // A variable of class type (or array thereof) that appears in a
11943 // copyin clause requires an accessible, unambiguous copy assignment
11944 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011945 Type = Context.getBaseElementType(Type.getNonReferenceType())
11946 .getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000011947 VarDecl *SrcVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011948 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src",
Alexey Bataeve122da12016-03-17 10:50:17 +000011949 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011950 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
11951 VarDecl *DstVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011952 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst",
Alexey Bataeve122da12016-03-17 10:50:17 +000011953 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011954 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
11955 ExprResult AssignmentOp = BuildBinOp(
11956 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000011957 if (AssignmentOp.isInvalid())
11958 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +000011959 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +000011960 /*DiscardedValue=*/true);
11961 if (AssignmentOp.isInvalid())
11962 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000011963
11964 // No need to mark vars as copyprivate, they are already threadprivate or
11965 // implicitly private.
Alexey Bataeve3727102018-04-18 15:57:46 +000011966 assert(VD || isOpenMPCapturedDecl(D));
Alexey Bataeve122da12016-03-17 10:50:17 +000011967 Vars.push_back(
11968 VD ? RefExpr->IgnoreParens()
11969 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +000011970 SrcExprs.push_back(PseudoSrcExpr);
11971 DstExprs.push_back(PseudoDstExpr);
11972 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +000011973 }
11974
11975 if (Vars.empty())
11976 return nullptr;
11977
Alexey Bataeva63048e2015-03-23 06:18:07 +000011978 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11979 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011980}
11981
Alexey Bataev6125da92014-07-21 11:26:11 +000011982OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
11983 SourceLocation StartLoc,
11984 SourceLocation LParenLoc,
11985 SourceLocation EndLoc) {
11986 if (VarList.empty())
11987 return nullptr;
11988
11989 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
11990}
Alexey Bataevdea47612014-07-23 07:46:59 +000011991
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011992OMPClause *
11993Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
11994 SourceLocation DepLoc, SourceLocation ColonLoc,
11995 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
11996 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +000011997 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011998 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +000011999 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000012000 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +000012001 return nullptr;
12002 }
12003 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012004 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
12005 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +000012006 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012007 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000012008 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
12009 /*Last=*/OMPC_DEPEND_unknown, Except)
12010 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012011 return nullptr;
12012 }
12013 SmallVector<Expr *, 8> Vars;
Alexey Bataev8b427062016-05-25 12:36:08 +000012014 DSAStackTy::OperatorOffsetTy OpsOffs;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012015 llvm::APSInt DepCounter(/*BitWidth=*/32);
12016 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
Alexey Bataevf138fda2018-08-13 19:04:24 +000012017 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) {
12018 if (const Expr *OrderedCountExpr =
12019 DSAStack->getParentOrderedRegionParam().first) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012020 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
12021 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012022 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012023 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012024 for (Expr *RefExpr : VarList) {
Alexey Bataev17daedf2018-02-15 22:42:57 +000012025 assert(RefExpr && "NULL expr in OpenMP shared clause.");
12026 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
12027 // It will be analyzed later.
12028 Vars.push_back(RefExpr);
12029 continue;
12030 }
12031
12032 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataeve3727102018-04-18 15:57:46 +000012033 Expr *SimpleExpr = RefExpr->IgnoreParenCasts();
Alexey Bataev17daedf2018-02-15 22:42:57 +000012034 if (DepKind == OMPC_DEPEND_sink) {
Alexey Bataevf138fda2018-08-13 19:04:24 +000012035 if (DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000012036 DepCounter >= TotalDepCount) {
12037 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
12038 continue;
12039 }
12040 ++DepCounter;
12041 // OpenMP [2.13.9, Summary]
12042 // depend(dependence-type : vec), where dependence-type is:
12043 // 'sink' and where vec is the iteration vector, which has the form:
12044 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
12045 // where n is the value specified by the ordered clause in the loop
12046 // directive, xi denotes the loop iteration variable of the i-th nested
12047 // loop associated with the loop directive, and di is a constant
12048 // non-negative integer.
12049 if (CurContext->isDependentContext()) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012050 // It will be analyzed later.
12051 Vars.push_back(RefExpr);
12052 continue;
12053 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012054 SimpleExpr = SimpleExpr->IgnoreImplicit();
12055 OverloadedOperatorKind OOK = OO_None;
12056 SourceLocation OOLoc;
12057 Expr *LHS = SimpleExpr;
12058 Expr *RHS = nullptr;
12059 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
12060 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
12061 OOLoc = BO->getOperatorLoc();
12062 LHS = BO->getLHS()->IgnoreParenImpCasts();
12063 RHS = BO->getRHS()->IgnoreParenImpCasts();
12064 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
12065 OOK = OCE->getOperator();
12066 OOLoc = OCE->getOperatorLoc();
12067 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
12068 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
12069 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
12070 OOK = MCE->getMethodDecl()
12071 ->getNameInfo()
12072 .getName()
12073 .getCXXOverloadedOperator();
12074 OOLoc = MCE->getCallee()->getExprLoc();
12075 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
12076 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012077 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012078 SourceLocation ELoc;
12079 SourceRange ERange;
Alexey Bataevbc529672018-09-28 19:33:14 +000012080 auto Res = getPrivateItem(*this, LHS, ELoc, ERange);
Alexey Bataev17daedf2018-02-15 22:42:57 +000012081 if (Res.second) {
12082 // It will be analyzed later.
12083 Vars.push_back(RefExpr);
12084 }
12085 ValueDecl *D = Res.first;
12086 if (!D)
12087 continue;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012088
Alexey Bataev17daedf2018-02-15 22:42:57 +000012089 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
12090 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
12091 continue;
12092 }
12093 if (RHS) {
12094 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
12095 RHS, OMPC_depend, /*StrictlyPositive=*/false);
12096 if (RHSRes.isInvalid())
12097 continue;
12098 }
12099 if (!CurContext->isDependentContext() &&
Alexey Bataevf138fda2018-08-13 19:04:24 +000012100 DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000012101 DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012102 const ValueDecl *VD =
Alexey Bataev17daedf2018-02-15 22:42:57 +000012103 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue());
Alexey Bataeve3727102018-04-18 15:57:46 +000012104 if (VD)
Alexey Bataev17daedf2018-02-15 22:42:57 +000012105 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
12106 << 1 << VD;
Alexey Bataeve3727102018-04-18 15:57:46 +000012107 else
Alexey Bataev17daedf2018-02-15 22:42:57 +000012108 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0;
Alexey Bataev17daedf2018-02-15 22:42:57 +000012109 continue;
12110 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012111 OpsOffs.emplace_back(RHS, OOK);
Alexey Bataev17daedf2018-02-15 22:42:57 +000012112 } else {
12113 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
12114 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
12115 (ASE &&
12116 !ASE->getBase()->getType().getNonReferenceType()->isPointerType() &&
12117 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
12118 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
12119 << RefExpr->getSourceRange();
12120 continue;
12121 }
12122 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
12123 getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
12124 ExprResult Res =
12125 CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RefExpr->IgnoreParenImpCasts());
12126 getDiagnostics().setSuppressAllDiagnostics(Suppress);
12127 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr)) {
12128 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
12129 << RefExpr->getSourceRange();
12130 continue;
12131 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012132 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012133 Vars.push_back(RefExpr->IgnoreParenImpCasts());
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012134 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012135
12136 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
12137 TotalDepCount > VarList.size() &&
Alexey Bataevf138fda2018-08-13 19:04:24 +000012138 DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000012139 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) {
12140 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
12141 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
12142 }
12143 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
12144 Vars.empty())
12145 return nullptr;
12146
Alexey Bataev8b427062016-05-25 12:36:08 +000012147 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataevf138fda2018-08-13 19:04:24 +000012148 DepKind, DepLoc, ColonLoc, Vars,
12149 TotalDepCount.getZExtValue());
Alexey Bataev17daedf2018-02-15 22:42:57 +000012150 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) &&
12151 DSAStack->isParentOrderedRegion())
Alexey Bataev8b427062016-05-25 12:36:08 +000012152 DSAStack->addDoacrossDependClause(C, OpsOffs);
12153 return C;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012154}
Michael Wonge710d542015-08-07 16:16:36 +000012155
12156OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
12157 SourceLocation LParenLoc,
12158 SourceLocation EndLoc) {
12159 Expr *ValExpr = Device;
Alexey Bataev931e19b2017-10-02 16:32:39 +000012160 Stmt *HelperValStmt = nullptr;
Michael Wonge710d542015-08-07 16:16:36 +000012161
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012162 // OpenMP [2.9.1, Restrictions]
12163 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000012164 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
Alexey Bataeva0569352015-12-01 10:17:31 +000012165 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012166 return nullptr;
12167
Alexey Bataev931e19b2017-10-02 16:32:39 +000012168 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000012169 OpenMPDirectiveKind CaptureRegion =
12170 getOpenMPCaptureRegionForClause(DKind, OMPC_device);
12171 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000012172 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000012173 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev931e19b2017-10-02 16:32:39 +000012174 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
12175 HelperValStmt = buildPreInits(Context, Captures);
12176 }
12177
Alexey Bataev8451efa2018-01-15 19:06:12 +000012178 return new (Context) OMPDeviceClause(ValExpr, HelperValStmt, CaptureRegion,
12179 StartLoc, LParenLoc, EndLoc);
Michael Wonge710d542015-08-07 16:16:36 +000012180}
Kelvin Li0bff7af2015-11-23 05:32:03 +000012181
Alexey Bataeve3727102018-04-18 15:57:46 +000012182static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
Alexey Bataev95c23e72018-02-27 21:31:11 +000012183 DSAStackTy *Stack, QualType QTy,
12184 bool FullCheck = true) {
Kelvin Li0bff7af2015-11-23 05:32:03 +000012185 NamedDecl *ND;
12186 if (QTy->isIncompleteType(&ND)) {
12187 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
12188 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +000012189 }
Alexey Bataev95c23e72018-02-27 21:31:11 +000012190 if (FullCheck && !SemaRef.CurContext->isDependentContext() &&
12191 !QTy.isTrivialType(SemaRef.Context))
12192 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR;
Kelvin Li0bff7af2015-11-23 05:32:03 +000012193 return true;
12194}
12195
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000012196/// Return true if it can be proven that the provided array expression
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012197/// (array section or array subscript) does NOT specify the whole size of the
12198/// array whose base type is \a BaseQTy.
Alexey Bataeve3727102018-04-18 15:57:46 +000012199static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012200 const Expr *E,
12201 QualType BaseQTy) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012202 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012203
12204 // If this is an array subscript, it refers to the whole size if the size of
12205 // the dimension is constant and equals 1. Also, an array section assumes the
12206 // format of an array subscript if no colon is used.
12207 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012208 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012209 return ATy->getSize().getSExtValue() != 1;
12210 // Size can't be evaluated statically.
12211 return false;
12212 }
12213
12214 assert(OASE && "Expecting array section if not an array subscript.");
Alexey Bataeve3727102018-04-18 15:57:46 +000012215 const Expr *LowerBound = OASE->getLowerBound();
12216 const Expr *Length = OASE->getLength();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012217
12218 // If there is a lower bound that does not evaluates to zero, we are not
David Majnemer9d168222016-08-05 17:44:54 +000012219 // covering the whole dimension.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012220 if (LowerBound) {
Fangrui Songf5d33352018-11-30 21:26:09 +000012221 llvm::APSInt ConstLowerBound;
12222 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012223 return false; // Can't get the integer value as a constant.
12224 if (ConstLowerBound.getSExtValue())
12225 return true;
12226 }
12227
12228 // If we don't have a length we covering the whole dimension.
12229 if (!Length)
12230 return false;
12231
12232 // If the base is a pointer, we don't have a way to get the size of the
12233 // pointee.
12234 if (BaseQTy->isPointerType())
12235 return false;
12236
12237 // We can only check if the length is the same as the size of the dimension
12238 // if we have a constant array.
Alexey Bataeve3727102018-04-18 15:57:46 +000012239 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012240 if (!CATy)
12241 return false;
12242
Fangrui Songf5d33352018-11-30 21:26:09 +000012243 llvm::APSInt ConstLength;
12244 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012245 return false; // Can't get the integer value as a constant.
12246
12247 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
12248}
12249
12250// Return true if it can be proven that the provided array expression (array
12251// section or array subscript) does NOT specify a single element of the array
12252// whose base type is \a BaseQTy.
Alexey Bataeve3727102018-04-18 15:57:46 +000012253static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
David Majnemer9d168222016-08-05 17:44:54 +000012254 const Expr *E,
12255 QualType BaseQTy) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012256 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012257
12258 // An array subscript always refer to a single element. Also, an array section
12259 // assumes the format of an array subscript if no colon is used.
12260 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
12261 return false;
12262
12263 assert(OASE && "Expecting array section if not an array subscript.");
Alexey Bataeve3727102018-04-18 15:57:46 +000012264 const Expr *Length = OASE->getLength();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012265
12266 // If we don't have a length we have to check if the array has unitary size
12267 // for this dimension. Also, we should always expect a length if the base type
12268 // is pointer.
12269 if (!Length) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012270 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012271 return ATy->getSize().getSExtValue() != 1;
12272 // We cannot assume anything.
12273 return false;
12274 }
12275
12276 // Check if the length evaluates to 1.
Fangrui Songf5d33352018-11-30 21:26:09 +000012277 llvm::APSInt ConstLength;
12278 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012279 return false; // Can't get the integer value as a constant.
12280
12281 return ConstLength.getSExtValue() != 1;
12282}
12283
Samuel Antao661c0902016-05-26 17:39:58 +000012284// Return the expression of the base of the mappable expression or null if it
12285// cannot be determined and do all the necessary checks to see if the expression
12286// is valid as a standalone mappable expression. In the process, record all the
Samuel Antao90927002016-04-26 14:54:23 +000012287// components of the expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000012288static const Expr *checkMapClauseExpressionBase(
Samuel Antao90927002016-04-26 14:54:23 +000012289 Sema &SemaRef, Expr *E,
Samuel Antao661c0902016-05-26 17:39:58 +000012290 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012291 OpenMPClauseKind CKind, bool NoDiagnose) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012292 SourceLocation ELoc = E->getExprLoc();
12293 SourceRange ERange = E->getSourceRange();
12294
12295 // The base of elements of list in a map clause have to be either:
12296 // - a reference to variable or field.
12297 // - a member expression.
12298 // - an array expression.
12299 //
12300 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
12301 // reference to 'r'.
12302 //
12303 // If we have:
12304 //
12305 // struct SS {
12306 // Bla S;
12307 // foo() {
12308 // #pragma omp target map (S.Arr[:12]);
12309 // }
12310 // }
12311 //
12312 // We want to retrieve the member expression 'this->S';
12313
Alexey Bataeve3727102018-04-18 15:57:46 +000012314 const Expr *RelevantExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012315
Samuel Antao5de996e2016-01-22 20:21:36 +000012316 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
12317 // If a list item is an array section, it must specify contiguous storage.
12318 //
12319 // For this restriction it is sufficient that we make sure only references
12320 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012321 // exist except in the rightmost expression (unless they cover the whole
12322 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +000012323 //
12324 // r.ArrS[3:5].Arr[6:7]
12325 //
12326 // r.ArrS[3:5].x
12327 //
12328 // but these would be valid:
12329 // r.ArrS[3].Arr[6:7]
12330 //
12331 // r.ArrS[3].x
12332
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012333 bool AllowUnitySizeArraySection = true;
12334 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +000012335
Dmitry Polukhin644a9252016-03-11 07:58:34 +000012336 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012337 E = E->IgnoreParenImpCasts();
12338
12339 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
12340 if (!isa<VarDecl>(CurE->getDecl()))
Alexey Bataev27041fa2017-12-05 15:22:49 +000012341 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012342
12343 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012344
12345 // If we got a reference to a declaration, we should not expect any array
12346 // section before that.
12347 AllowUnitySizeArraySection = false;
12348 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000012349
12350 // Record the component.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012351 CurComponents.emplace_back(CurE, CurE->getDecl());
12352 } else if (auto *CurE = dyn_cast<MemberExpr>(E)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012353 Expr *BaseE = CurE->getBase()->IgnoreParenImpCasts();
Samuel Antao5de996e2016-01-22 20:21:36 +000012354
12355 if (isa<CXXThisExpr>(BaseE))
12356 // We found a base expression: this->Val.
12357 RelevantExpr = CurE;
12358 else
12359 E = BaseE;
12360
12361 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012362 if (!NoDiagnose) {
12363 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
12364 << CurE->getSourceRange();
12365 return nullptr;
12366 }
12367 if (RelevantExpr)
12368 return nullptr;
12369 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000012370 }
12371
12372 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
12373
12374 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
12375 // A bit-field cannot appear in a map clause.
12376 //
12377 if (FD->isBitField()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012378 if (!NoDiagnose) {
12379 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
12380 << CurE->getSourceRange() << getOpenMPClauseName(CKind);
12381 return nullptr;
12382 }
12383 if (RelevantExpr)
12384 return nullptr;
12385 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000012386 }
12387
12388 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12389 // If the type of a list item is a reference to a type T then the type
12390 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012391 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000012392
12393 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
12394 // A list item cannot be a variable that is a member of a structure with
12395 // a union type.
12396 //
Alexey Bataeve3727102018-04-18 15:57:46 +000012397 if (CurType->isUnionType()) {
12398 if (!NoDiagnose) {
12399 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
12400 << CurE->getSourceRange();
12401 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012402 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012403 continue;
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012404 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012405
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012406 // If we got a member expression, we should not expect any array section
12407 // before that:
12408 //
12409 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
12410 // If a list item is an element of a structure, only the rightmost symbol
12411 // of the variable reference can be an array section.
12412 //
12413 AllowUnitySizeArraySection = false;
12414 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000012415
12416 // Record the component.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012417 CurComponents.emplace_back(CurE, FD);
12418 } else if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012419 E = CurE->getBase()->IgnoreParenImpCasts();
12420
12421 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012422 if (!NoDiagnose) {
12423 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
12424 << 0 << CurE->getSourceRange();
12425 return nullptr;
12426 }
12427 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000012428 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012429
12430 // If we got an array subscript that express the whole dimension we
12431 // can have any array expressions before. If it only expressing part of
12432 // the dimension, we can only have unitary-size array expressions.
Alexey Bataeve3727102018-04-18 15:57:46 +000012433 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012434 E->getType()))
12435 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000012436
12437 // Record the component - we don't have any declaration associated.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012438 CurComponents.emplace_back(CurE, nullptr);
12439 } else if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012440 assert(!NoDiagnose && "Array sections cannot be implicitly mapped.");
Samuel Antao5de996e2016-01-22 20:21:36 +000012441 E = CurE->getBase()->IgnoreParenImpCasts();
12442
Alexey Bataev27041fa2017-12-05 15:22:49 +000012443 QualType CurType =
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012444 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
12445
Samuel Antao5de996e2016-01-22 20:21:36 +000012446 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12447 // If the type of a list item is a reference to a type T then the type
12448 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +000012449 if (CurType->isReferenceType())
12450 CurType = CurType->getPointeeType();
12451
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012452 bool IsPointer = CurType->isAnyPointerType();
12453
12454 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012455 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
12456 << 0 << CurE->getSourceRange();
Alexey Bataev27041fa2017-12-05 15:22:49 +000012457 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012458 }
12459
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012460 bool NotWhole =
Alexey Bataeve3727102018-04-18 15:57:46 +000012461 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012462 bool NotUnity =
Alexey Bataeve3727102018-04-18 15:57:46 +000012463 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012464
Samuel Antaodab51bb2016-07-18 23:22:11 +000012465 if (AllowWholeSizeArraySection) {
12466 // Any array section is currently allowed. Allowing a whole size array
12467 // section implies allowing a unity array section as well.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012468 //
12469 // If this array section refers to the whole dimension we can still
12470 // accept other array sections before this one, except if the base is a
12471 // pointer. Otherwise, only unitary sections are accepted.
12472 if (NotWhole || IsPointer)
12473 AllowWholeSizeArraySection = false;
Samuel Antaodab51bb2016-07-18 23:22:11 +000012474 } else if (AllowUnitySizeArraySection && NotUnity) {
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012475 // A unity or whole array section is not allowed and that is not
12476 // compatible with the properties of the current array section.
12477 SemaRef.Diag(
12478 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
12479 << CurE->getSourceRange();
Alexey Bataev27041fa2017-12-05 15:22:49 +000012480 return nullptr;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012481 }
Samuel Antao90927002016-04-26 14:54:23 +000012482
12483 // Record the component - we don't have any declaration associated.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012484 CurComponents.emplace_back(CurE, nullptr);
12485 } else {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012486 if (!NoDiagnose) {
12487 // If nothing else worked, this is not a valid map clause expression.
12488 SemaRef.Diag(
12489 ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
12490 << ERange;
12491 }
Alexey Bataev27041fa2017-12-05 15:22:49 +000012492 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012493 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012494 }
12495
12496 return RelevantExpr;
12497}
12498
12499// Return true if expression E associated with value VD has conflicts with other
12500// map information.
Alexey Bataeve3727102018-04-18 15:57:46 +000012501static bool checkMapConflicts(
12502 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E,
Samuel Antao90927002016-04-26 14:54:23 +000012503 bool CurrentRegionOnly,
Samuel Antao661c0902016-05-26 17:39:58 +000012504 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
12505 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012506 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +000012507 SourceLocation ELoc = E->getExprLoc();
12508 SourceRange ERange = E->getSourceRange();
12509
12510 // In order to easily check the conflicts we need to match each component of
12511 // the expression under test with the components of the expressions that are
12512 // already in the stack.
12513
Samuel Antao5de996e2016-01-22 20:21:36 +000012514 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000012515 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000012516 "Map clause expression with unexpected base!");
12517
12518 // Variables to help detecting enclosing problems in data environment nests.
12519 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +000012520 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012521
Samuel Antao90927002016-04-26 14:54:23 +000012522 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
12523 VD, CurrentRegionOnly,
Alexey Bataeve3727102018-04-18 15:57:46 +000012524 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc,
12525 ERange, CKind, &EnclosingExpr,
12526 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef
12527 StackComponents,
12528 OpenMPClauseKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012529 assert(!StackComponents.empty() &&
12530 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000012531 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000012532 "Map clause expression with unexpected base!");
Fangrui Song16fe49a2018-04-18 19:32:01 +000012533 (void)VD;
Samuel Antao5de996e2016-01-22 20:21:36 +000012534
Samuel Antao90927002016-04-26 14:54:23 +000012535 // The whole expression in the stack.
Alexey Bataeve3727102018-04-18 15:57:46 +000012536 const Expr *RE = StackComponents.front().getAssociatedExpression();
Samuel Antao90927002016-04-26 14:54:23 +000012537
Samuel Antao5de996e2016-01-22 20:21:36 +000012538 // Expressions must start from the same base. Here we detect at which
12539 // point both expressions diverge from each other and see if we can
12540 // detect if the memory referred to both expressions is contiguous and
12541 // do not overlap.
12542 auto CI = CurComponents.rbegin();
12543 auto CE = CurComponents.rend();
12544 auto SI = StackComponents.rbegin();
12545 auto SE = StackComponents.rend();
12546 for (; CI != CE && SI != SE; ++CI, ++SI) {
12547
12548 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
12549 // At most one list item can be an array item derived from a given
12550 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +000012551 if (CurrentRegionOnly &&
12552 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
12553 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
12554 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
12555 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
12556 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +000012557 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +000012558 << CI->getAssociatedExpression()->getSourceRange();
12559 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
12560 diag::note_used_here)
12561 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +000012562 return true;
12563 }
12564
12565 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +000012566 if (CI->getAssociatedExpression()->getStmtClass() !=
12567 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +000012568 break;
12569
12570 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +000012571 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +000012572 break;
12573 }
Kelvin Li9f645ae2016-07-18 22:49:16 +000012574 // Check if the extra components of the expressions in the enclosing
12575 // data environment are redundant for the current base declaration.
12576 // If they are, the maps completely overlap, which is legal.
12577 for (; SI != SE; ++SI) {
12578 QualType Type;
Alexey Bataeve3727102018-04-18 15:57:46 +000012579 if (const auto *ASE =
David Majnemer9d168222016-08-05 17:44:54 +000012580 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
Kelvin Li9f645ae2016-07-18 22:49:16 +000012581 Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
Alexey Bataeve3727102018-04-18 15:57:46 +000012582 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(
David Majnemer9d168222016-08-05 17:44:54 +000012583 SI->getAssociatedExpression())) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012584 const Expr *E = OASE->getBase()->IgnoreParenImpCasts();
Kelvin Li9f645ae2016-07-18 22:49:16 +000012585 Type =
12586 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
12587 }
12588 if (Type.isNull() || Type->isAnyPointerType() ||
Alexey Bataeve3727102018-04-18 15:57:46 +000012589 checkArrayExpressionDoesNotReferToWholeSize(
Kelvin Li9f645ae2016-07-18 22:49:16 +000012590 SemaRef, SI->getAssociatedExpression(), Type))
12591 break;
12592 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012593
12594 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
12595 // List items of map clauses in the same construct must not share
12596 // original storage.
12597 //
12598 // If the expressions are exactly the same or one is a subset of the
12599 // other, it means they are sharing storage.
12600 if (CI == CE && SI == SE) {
12601 if (CurrentRegionOnly) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012602 if (CKind == OMPC_map) {
Samuel Antao661c0902016-05-26 17:39:58 +000012603 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +000012604 } else {
Samuel Antaoec172c62016-05-26 17:49:04 +000012605 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000012606 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
12607 << ERange;
12608 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012609 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12610 << RE->getSourceRange();
12611 return true;
Samuel Antao5de996e2016-01-22 20:21:36 +000012612 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012613 // If we find the same expression in the enclosing data environment,
12614 // that is legal.
12615 IsEnclosedByDataEnvironmentExpr = true;
12616 return false;
Samuel Antao5de996e2016-01-22 20:21:36 +000012617 }
12618
Samuel Antao90927002016-04-26 14:54:23 +000012619 QualType DerivedType =
12620 std::prev(CI)->getAssociatedDeclaration()->getType();
12621 SourceLocation DerivedLoc =
12622 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000012623
12624 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12625 // If the type of a list item is a reference to a type T then the type
12626 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000012627 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000012628
12629 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
12630 // A variable for which the type is pointer and an array section
12631 // derived from that variable must not appear as list items of map
12632 // clauses of the same construct.
12633 //
12634 // Also, cover one of the cases in:
12635 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
12636 // If any part of the original storage of a list item has corresponding
12637 // storage in the device data environment, all of the original storage
12638 // must have corresponding storage in the device data environment.
12639 //
12640 if (DerivedType->isAnyPointerType()) {
12641 if (CI == CE || SI == SE) {
12642 SemaRef.Diag(
12643 DerivedLoc,
12644 diag::err_omp_pointer_mapped_along_with_derived_section)
12645 << DerivedLoc;
Alexey Bataev2819260b2018-02-27 17:42:00 +000012646 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12647 << RE->getSourceRange();
12648 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +000012649 }
12650 if (CI->getAssociatedExpression()->getStmtClass() !=
Alexey Bataev2819260b2018-02-27 17:42:00 +000012651 SI->getAssociatedExpression()->getStmtClass() ||
12652 CI->getAssociatedDeclaration()->getCanonicalDecl() ==
12653 SI->getAssociatedDeclaration()->getCanonicalDecl()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012654 assert(CI != CE && SI != SE);
Alexey Bataev2819260b2018-02-27 17:42:00 +000012655 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced)
Samuel Antao5de996e2016-01-22 20:21:36 +000012656 << DerivedLoc;
Alexey Bataev2819260b2018-02-27 17:42:00 +000012657 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12658 << RE->getSourceRange();
12659 return true;
Samuel Antao5de996e2016-01-22 20:21:36 +000012660 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012661 }
12662
12663 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
12664 // List items of map clauses in the same construct must not share
12665 // original storage.
12666 //
12667 // An expression is a subset of the other.
12668 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012669 if (CKind == OMPC_map) {
Alexey Bataeve82445f2018-09-20 13:54:02 +000012670 if (CI != CE || SI != SE) {
12671 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is
12672 // a pointer.
12673 auto Begin =
12674 CI != CE ? CurComponents.begin() : StackComponents.begin();
12675 auto End = CI != CE ? CurComponents.end() : StackComponents.end();
12676 auto It = Begin;
12677 while (It != End && !It->getAssociatedDeclaration())
12678 std::advance(It, 1);
12679 assert(It != End &&
12680 "Expected at least one component with the declaration.");
12681 if (It != Begin && It->getAssociatedDeclaration()
12682 ->getType()
12683 .getCanonicalType()
12684 ->isAnyPointerType()) {
12685 IsEnclosedByDataEnvironmentExpr = false;
12686 EnclosingExpr = nullptr;
12687 return false;
12688 }
12689 }
Samuel Antao661c0902016-05-26 17:39:58 +000012690 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +000012691 } else {
Samuel Antaoec172c62016-05-26 17:49:04 +000012692 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000012693 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
12694 << ERange;
12695 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012696 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12697 << RE->getSourceRange();
12698 return true;
12699 }
12700
12701 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000012702 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000012703 if (!CurrentRegionOnly && SI != SE)
12704 EnclosingExpr = RE;
12705
12706 // The current expression is a subset of the expression in the data
12707 // environment.
12708 IsEnclosedByDataEnvironmentExpr |=
12709 (!CurrentRegionOnly && CI != CE && SI == SE);
12710
12711 return false;
12712 });
12713
12714 if (CurrentRegionOnly)
12715 return FoundError;
12716
12717 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
12718 // If any part of the original storage of a list item has corresponding
12719 // storage in the device data environment, all of the original storage must
12720 // have corresponding storage in the device data environment.
12721 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
12722 // If a list item is an element of a structure, and a different element of
12723 // the structure has a corresponding list item in the device data environment
12724 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000012725 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000012726 // data environment prior to the task encountering the construct.
12727 //
12728 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
12729 SemaRef.Diag(ELoc,
12730 diag::err_omp_original_storage_is_shared_and_does_not_contain)
12731 << ERange;
12732 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
12733 << EnclosingExpr->getSourceRange();
12734 return true;
12735 }
12736
12737 return FoundError;
12738}
12739
Samuel Antao661c0902016-05-26 17:39:58 +000012740namespace {
12741// Utility struct that gathers all the related lists associated with a mappable
12742// expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000012743struct MappableVarListInfo {
Samuel Antao661c0902016-05-26 17:39:58 +000012744 // The list of expressions.
12745 ArrayRef<Expr *> VarList;
12746 // The list of processed expressions.
12747 SmallVector<Expr *, 16> ProcessedVarList;
12748 // The mappble components for each expression.
12749 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
12750 // The base declaration of the variable.
12751 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
12752
12753 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
12754 // We have a list of components and base declarations for each entry in the
12755 // variable list.
12756 VarComponents.reserve(VarList.size());
12757 VarBaseDeclarations.reserve(VarList.size());
12758 }
12759};
12760}
12761
12762// Check the validity of the provided variable list for the provided clause kind
12763// \a CKind. In the check process the valid expressions, and mappable expression
12764// components and variables are extracted and used to fill \a Vars,
12765// \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and
12766// \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'.
12767static void
12768checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS,
12769 OpenMPClauseKind CKind, MappableVarListInfo &MVLI,
12770 SourceLocation StartLoc,
12771 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
12772 bool IsMapTypeImplicit = false) {
Samuel Antaoec172c62016-05-26 17:49:04 +000012773 // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
12774 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
Samuel Antao661c0902016-05-26 17:39:58 +000012775 "Unexpected clause kind with mappable expressions!");
Kelvin Li0bff7af2015-11-23 05:32:03 +000012776
Samuel Antao90927002016-04-26 14:54:23 +000012777 // Keep track of the mappable components and base declarations in this clause.
12778 // Each entry in the list is going to have a list of components associated. We
12779 // record each set of the components so that we can build the clause later on.
12780 // In the end we should have the same amount of declarations and component
12781 // lists.
Samuel Antao90927002016-04-26 14:54:23 +000012782
Alexey Bataeve3727102018-04-18 15:57:46 +000012783 for (Expr *RE : MVLI.VarList) {
Samuel Antaoec172c62016-05-26 17:49:04 +000012784 assert(RE && "Null expr in omp to/from/map clause");
Kelvin Li0bff7af2015-11-23 05:32:03 +000012785 SourceLocation ELoc = RE->getExprLoc();
12786
Alexey Bataeve3727102018-04-18 15:57:46 +000012787 const Expr *VE = RE->IgnoreParenLValueCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012788
12789 if (VE->isValueDependent() || VE->isTypeDependent() ||
12790 VE->isInstantiationDependent() ||
12791 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012792 // We can only analyze this information once the missing information is
12793 // resolved.
Samuel Antao661c0902016-05-26 17:39:58 +000012794 MVLI.ProcessedVarList.push_back(RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012795 continue;
12796 }
12797
Alexey Bataeve3727102018-04-18 15:57:46 +000012798 Expr *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012799
Samuel Antao5de996e2016-01-22 20:21:36 +000012800 if (!RE->IgnoreParenImpCasts()->isLValue()) {
Samuel Antao661c0902016-05-26 17:39:58 +000012801 SemaRef.Diag(ELoc,
12802 diag::err_omp_expected_named_var_member_or_array_expression)
Samuel Antao5de996e2016-01-22 20:21:36 +000012803 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012804 continue;
12805 }
12806
Samuel Antao90927002016-04-26 14:54:23 +000012807 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
12808 ValueDecl *CurDeclaration = nullptr;
12809
12810 // Obtain the array or member expression bases if required. Also, fill the
12811 // components array with all the components identified in the process.
Alexey Bataeve3727102018-04-18 15:57:46 +000012812 const Expr *BE = checkMapClauseExpressionBase(
12813 SemaRef, SimpleExpr, CurComponents, CKind, /*NoDiagnose=*/false);
Samuel Antao5de996e2016-01-22 20:21:36 +000012814 if (!BE)
12815 continue;
12816
Samuel Antao90927002016-04-26 14:54:23 +000012817 assert(!CurComponents.empty() &&
12818 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000012819
Samuel Antao90927002016-04-26 14:54:23 +000012820 // For the following checks, we rely on the base declaration which is
12821 // expected to be associated with the last component. The declaration is
12822 // expected to be a variable or a field (if 'this' is being mapped).
12823 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
12824 assert(CurDeclaration && "Null decl on map clause.");
12825 assert(
12826 CurDeclaration->isCanonicalDecl() &&
12827 "Expecting components to have associated only canonical declarations.");
12828
12829 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
Alexey Bataeve3727102018-04-18 15:57:46 +000012830 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000012831
12832 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000012833 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000012834
12835 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
Samuel Antao661c0902016-05-26 17:39:58 +000012836 // threadprivate variables cannot appear in a map clause.
12837 // OpenMP 4.5 [2.10.5, target update Construct]
12838 // threadprivate variables cannot appear in a from clause.
12839 if (VD && DSAS->isThreadPrivate(VD)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012840 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
Samuel Antao661c0902016-05-26 17:39:58 +000012841 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
12842 << getOpenMPClauseName(CKind);
Alexey Bataeve3727102018-04-18 15:57:46 +000012843 reportOriginalDsa(SemaRef, DSAS, VD, DVar);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012844 continue;
12845 }
12846
Samuel Antao5de996e2016-01-22 20:21:36 +000012847 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
12848 // A list item cannot appear in both a map clause and a data-sharing
12849 // attribute clause on the same construct.
Kelvin Li0bff7af2015-11-23 05:32:03 +000012850
Samuel Antao5de996e2016-01-22 20:21:36 +000012851 // Check conflicts with other map clause expressions. We check the conflicts
12852 // with the current construct separately from the enclosing data
Samuel Antao661c0902016-05-26 17:39:58 +000012853 // environment, because the restrictions are different. We only have to
12854 // check conflicts across regions for the map clauses.
Alexey Bataeve3727102018-04-18 15:57:46 +000012855 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
Samuel Antao661c0902016-05-26 17:39:58 +000012856 /*CurrentRegionOnly=*/true, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000012857 break;
Samuel Antao661c0902016-05-26 17:39:58 +000012858 if (CKind == OMPC_map &&
Alexey Bataeve3727102018-04-18 15:57:46 +000012859 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
Samuel Antao661c0902016-05-26 17:39:58 +000012860 /*CurrentRegionOnly=*/false, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000012861 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000012862
Samuel Antao661c0902016-05-26 17:39:58 +000012863 // OpenMP 4.5 [2.10.5, target update Construct]
Samuel Antao5de996e2016-01-22 20:21:36 +000012864 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12865 // If the type of a list item is a reference to a type T then the type will
12866 // be considered to be T for all purposes of this clause.
Alexey Bataev354df2e2018-05-02 18:44:10 +000012867 auto I = llvm::find_if(
12868 CurComponents,
12869 [](const OMPClauseMappableExprCommon::MappableComponent &MC) {
12870 return MC.getAssociatedDeclaration();
12871 });
12872 assert(I != CurComponents.end() && "Null decl on map clause.");
12873 QualType Type =
12874 I->getAssociatedDeclaration()->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000012875
Samuel Antao661c0902016-05-26 17:39:58 +000012876 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
12877 // A list item in a to or from clause must have a mappable type.
Samuel Antao5de996e2016-01-22 20:21:36 +000012878 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000012879 // A list item must have a mappable type.
Alexey Bataeve3727102018-04-18 15:57:46 +000012880 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
Samuel Antao661c0902016-05-26 17:39:58 +000012881 DSAS, Type))
Kelvin Li0bff7af2015-11-23 05:32:03 +000012882 continue;
12883
Samuel Antao661c0902016-05-26 17:39:58 +000012884 if (CKind == OMPC_map) {
12885 // target enter data
12886 // OpenMP [2.10.2, Restrictions, p. 99]
12887 // A map-type must be specified in all map clauses and must be either
12888 // to or alloc.
12889 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
12890 if (DKind == OMPD_target_enter_data &&
12891 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
12892 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
12893 << (IsMapTypeImplicit ? 1 : 0)
12894 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
12895 << getOpenMPDirectiveName(DKind);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000012896 continue;
12897 }
Samuel Antao661c0902016-05-26 17:39:58 +000012898
12899 // target exit_data
12900 // OpenMP [2.10.3, Restrictions, p. 102]
12901 // A map-type must be specified in all map clauses and must be either
12902 // from, release, or delete.
12903 if (DKind == OMPD_target_exit_data &&
12904 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
12905 MapType == OMPC_MAP_delete)) {
12906 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
12907 << (IsMapTypeImplicit ? 1 : 0)
12908 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
12909 << getOpenMPDirectiveName(DKind);
12910 continue;
12911 }
12912
12913 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
12914 // A list item cannot appear in both a map clause and a data-sharing
12915 // attribute clause on the same construct
Alexey Bataeve3727102018-04-18 15:57:46 +000012916 if (VD && isOpenMPTargetExecutionDirective(DKind)) {
12917 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
Samuel Antao661c0902016-05-26 17:39:58 +000012918 if (isOpenMPPrivate(DVar.CKind)) {
Samuel Antao6890b092016-07-28 14:25:09 +000012919 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Samuel Antao661c0902016-05-26 17:39:58 +000012920 << getOpenMPClauseName(DVar.CKind)
Samuel Antao6890b092016-07-28 14:25:09 +000012921 << getOpenMPClauseName(OMPC_map)
Samuel Antao661c0902016-05-26 17:39:58 +000012922 << getOpenMPDirectiveName(DSAS->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000012923 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar);
Samuel Antao661c0902016-05-26 17:39:58 +000012924 continue;
12925 }
12926 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +000012927 }
12928
Samuel Antao90927002016-04-26 14:54:23 +000012929 // Save the current expression.
Samuel Antao661c0902016-05-26 17:39:58 +000012930 MVLI.ProcessedVarList.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000012931
12932 // Store the components in the stack so that they can be used to check
12933 // against other clauses later on.
Samuel Antao6890b092016-07-28 14:25:09 +000012934 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
12935 /*WhereFoundClauseKind=*/OMPC_map);
Samuel Antao90927002016-04-26 14:54:23 +000012936
12937 // Save the components and declaration to create the clause. For purposes of
12938 // the clause creation, any component list that has has base 'this' uses
Samuel Antao686c70c2016-05-26 17:30:50 +000012939 // null as base declaration.
Samuel Antao661c0902016-05-26 17:39:58 +000012940 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
12941 MVLI.VarComponents.back().append(CurComponents.begin(),
12942 CurComponents.end());
12943 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
12944 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012945 }
Samuel Antao661c0902016-05-26 17:39:58 +000012946}
12947
12948OMPClause *
12949Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
12950 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
12951 SourceLocation MapLoc, SourceLocation ColonLoc,
12952 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
12953 SourceLocation LParenLoc, SourceLocation EndLoc) {
12954 MappableVarListInfo MVLI(VarList);
12955 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc,
12956 MapType, IsMapTypeImplicit);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012957
Samuel Antao5de996e2016-01-22 20:21:36 +000012958 // We need to produce a map clause even if we don't have variables so that
12959 // other diagnostics related with non-existing map clauses are accurate.
Samuel Antao661c0902016-05-26 17:39:58 +000012960 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc,
12961 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
12962 MVLI.VarComponents, MapTypeModifier, MapType,
12963 IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012964}
Kelvin Li099bb8c2015-11-24 20:50:12 +000012965
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012966QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
12967 TypeResult ParsedType) {
12968 assert(ParsedType.isUsable());
12969
12970 QualType ReductionType = GetTypeFromParser(ParsedType.get());
12971 if (ReductionType.isNull())
12972 return QualType();
12973
12974 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
12975 // A type name in a declare reduction directive cannot be a function type, an
12976 // array type, a reference type, or a type qualified with const, volatile or
12977 // restrict.
12978 if (ReductionType.hasQualifiers()) {
12979 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
12980 return QualType();
12981 }
12982
12983 if (ReductionType->isFunctionType()) {
12984 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
12985 return QualType();
12986 }
12987 if (ReductionType->isReferenceType()) {
12988 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
12989 return QualType();
12990 }
12991 if (ReductionType->isArrayType()) {
12992 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
12993 return QualType();
12994 }
12995 return ReductionType;
12996}
12997
12998Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
12999 Scope *S, DeclContext *DC, DeclarationName Name,
13000 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
13001 AccessSpecifier AS, Decl *PrevDeclInScope) {
13002 SmallVector<Decl *, 8> Decls;
13003 Decls.reserve(ReductionTypes.size());
13004
13005 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
Richard Smithbecb92d2017-10-10 22:33:17 +000013006 forRedeclarationInCurContext());
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013007 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
13008 // A reduction-identifier may not be re-declared in the current scope for the
13009 // same type or for a type that is compatible according to the base language
13010 // rules.
13011 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
13012 OMPDeclareReductionDecl *PrevDRD = nullptr;
13013 bool InCompoundScope = true;
13014 if (S != nullptr) {
13015 // Find previous declaration with the same name not referenced in other
13016 // declarations.
13017 FunctionScopeInfo *ParentFn = getEnclosingFunction();
13018 InCompoundScope =
13019 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
13020 LookupName(Lookup, S);
13021 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
13022 /*AllowInlineNamespace=*/false);
13023 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
Alexey Bataeve3727102018-04-18 15:57:46 +000013024 LookupResult::Filter Filter = Lookup.makeFilter();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013025 while (Filter.hasNext()) {
13026 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
13027 if (InCompoundScope) {
13028 auto I = UsedAsPrevious.find(PrevDecl);
13029 if (I == UsedAsPrevious.end())
13030 UsedAsPrevious[PrevDecl] = false;
Alexey Bataeve3727102018-04-18 15:57:46 +000013031 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope())
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013032 UsedAsPrevious[D] = true;
13033 }
13034 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
13035 PrevDecl->getLocation();
13036 }
13037 Filter.done();
13038 if (InCompoundScope) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013039 for (const auto &PrevData : UsedAsPrevious) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013040 if (!PrevData.second) {
13041 PrevDRD = PrevData.first;
13042 break;
13043 }
13044 }
13045 }
13046 } else if (PrevDeclInScope != nullptr) {
13047 auto *PrevDRDInScope = PrevDRD =
13048 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
13049 do {
13050 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
13051 PrevDRDInScope->getLocation();
13052 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
13053 } while (PrevDRDInScope != nullptr);
13054 }
Alexey Bataeve3727102018-04-18 15:57:46 +000013055 for (const auto &TyData : ReductionTypes) {
13056 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013057 bool Invalid = false;
13058 if (I != PreviousRedeclTypes.end()) {
13059 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
13060 << TyData.first;
13061 Diag(I->second, diag::note_previous_definition);
13062 Invalid = true;
13063 }
13064 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
13065 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
13066 Name, TyData.first, PrevDRD);
13067 DC->addDecl(DRD);
13068 DRD->setAccess(AS);
13069 Decls.push_back(DRD);
13070 if (Invalid)
13071 DRD->setInvalidDecl();
13072 else
13073 PrevDRD = DRD;
13074 }
13075
13076 return DeclGroupPtrTy::make(
13077 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
13078}
13079
13080void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
13081 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13082
13083 // Enter new function scope.
13084 PushFunctionScope();
Reid Kleckner87a31802018-03-12 21:43:02 +000013085 setFunctionHasBranchProtectedScope();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013086 getCurFunction()->setHasOMPDeclareReductionCombiner();
13087
13088 if (S != nullptr)
13089 PushDeclContext(S, DRD);
13090 else
13091 CurContext = DRD;
13092
Faisal Valid143a0c2017-04-01 21:30:49 +000013093 PushExpressionEvaluationContext(
13094 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013095
13096 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013097 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
13098 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
13099 // uses semantics of argument handles by value, but it should be passed by
13100 // reference. C lang does not support references, so pass all parameters as
13101 // pointers.
13102 // Create 'T omp_in;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013103 VarDecl *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013104 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013105 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
13106 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
13107 // uses semantics of argument handles by value, but it should be passed by
13108 // reference. C lang does not support references, so pass all parameters as
13109 // pointers.
13110 // Create 'T omp_out;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013111 VarDecl *OmpOutParm =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013112 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
13113 if (S != nullptr) {
13114 PushOnScopeChains(OmpInParm, S);
13115 PushOnScopeChains(OmpOutParm, S);
13116 } else {
13117 DRD->addDecl(OmpInParm);
13118 DRD->addDecl(OmpOutParm);
13119 }
Alexey Bataeve6aa4692018-09-13 16:54:05 +000013120 Expr *InE =
13121 ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation());
13122 Expr *OutE =
13123 ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation());
13124 DRD->setCombinerData(InE, OutE);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013125}
13126
13127void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
13128 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13129 DiscardCleanupsInEvaluationContext();
13130 PopExpressionEvaluationContext();
13131
13132 PopDeclContext();
13133 PopFunctionScopeInfo();
13134
13135 if (Combiner != nullptr)
13136 DRD->setCombiner(Combiner);
13137 else
13138 DRD->setInvalidDecl();
13139}
13140
Alexey Bataev070f43a2017-09-06 14:49:58 +000013141VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013142 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13143
13144 // Enter new function scope.
13145 PushFunctionScope();
Reid Kleckner87a31802018-03-12 21:43:02 +000013146 setFunctionHasBranchProtectedScope();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013147
13148 if (S != nullptr)
13149 PushDeclContext(S, DRD);
13150 else
13151 CurContext = DRD;
13152
Faisal Valid143a0c2017-04-01 21:30:49 +000013153 PushExpressionEvaluationContext(
13154 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013155
13156 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013157 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
13158 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
13159 // uses semantics of argument handles by value, but it should be passed by
13160 // reference. C lang does not support references, so pass all parameters as
13161 // pointers.
13162 // Create 'T omp_priv;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013163 VarDecl *OmpPrivParm =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013164 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013165 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
13166 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
13167 // uses semantics of argument handles by value, but it should be passed by
13168 // reference. C lang does not support references, so pass all parameters as
13169 // pointers.
13170 // Create 'T omp_orig;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013171 VarDecl *OmpOrigParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013172 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013173 if (S != nullptr) {
13174 PushOnScopeChains(OmpPrivParm, S);
13175 PushOnScopeChains(OmpOrigParm, S);
13176 } else {
13177 DRD->addDecl(OmpPrivParm);
13178 DRD->addDecl(OmpOrigParm);
13179 }
Alexey Bataeve6aa4692018-09-13 16:54:05 +000013180 Expr *OrigE =
13181 ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation());
13182 Expr *PrivE =
13183 ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation());
13184 DRD->setInitializerData(OrigE, PrivE);
Alexey Bataev070f43a2017-09-06 14:49:58 +000013185 return OmpPrivParm;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013186}
13187
Alexey Bataev070f43a2017-09-06 14:49:58 +000013188void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer,
13189 VarDecl *OmpPrivParm) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013190 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13191 DiscardCleanupsInEvaluationContext();
13192 PopExpressionEvaluationContext();
13193
13194 PopDeclContext();
13195 PopFunctionScopeInfo();
13196
Alexey Bataev070f43a2017-09-06 14:49:58 +000013197 if (Initializer != nullptr) {
13198 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit);
13199 } else if (OmpPrivParm->hasInit()) {
13200 DRD->setInitializer(OmpPrivParm->getInit(),
13201 OmpPrivParm->isDirectInit()
13202 ? OMPDeclareReductionDecl::DirectInit
13203 : OMPDeclareReductionDecl::CopyInit);
13204 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013205 DRD->setInvalidDecl();
Alexey Bataev070f43a2017-09-06 14:49:58 +000013206 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013207}
13208
13209Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
13210 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013211 for (Decl *D : DeclReductions.get()) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013212 if (IsValid) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013213 if (S)
13214 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S,
13215 /*AddToContext=*/false);
13216 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013217 D->setInvalidDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000013218 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013219 }
13220 return DeclReductions;
13221}
13222
David Majnemer9d168222016-08-05 17:44:54 +000013223OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
Kelvin Li099bb8c2015-11-24 20:50:12 +000013224 SourceLocation StartLoc,
13225 SourceLocation LParenLoc,
13226 SourceLocation EndLoc) {
13227 Expr *ValExpr = NumTeams;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000013228 Stmt *HelperValStmt = nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000013229
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013230 // OpenMP [teams Constrcut, Restrictions]
13231 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000013232 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
Alexey Bataeva0569352015-12-01 10:17:31 +000013233 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013234 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000013235
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000013236 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000013237 OpenMPDirectiveKind CaptureRegion =
13238 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams);
13239 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000013240 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000013241 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000013242 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13243 HelperValStmt = buildPreInits(Context, Captures);
13244 }
13245
13246 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion,
13247 StartLoc, LParenLoc, EndLoc);
Kelvin Li099bb8c2015-11-24 20:50:12 +000013248}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013249
13250OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
13251 SourceLocation StartLoc,
13252 SourceLocation LParenLoc,
13253 SourceLocation EndLoc) {
13254 Expr *ValExpr = ThreadLimit;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000013255 Stmt *HelperValStmt = nullptr;
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013256
13257 // OpenMP [teams Constrcut, Restrictions]
13258 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000013259 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
Alexey Bataeva0569352015-12-01 10:17:31 +000013260 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013261 return nullptr;
13262
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000013263 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000013264 OpenMPDirectiveKind CaptureRegion =
13265 getOpenMPCaptureRegionForClause(DKind, OMPC_thread_limit);
13266 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000013267 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000013268 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000013269 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13270 HelperValStmt = buildPreInits(Context, Captures);
13271 }
13272
13273 return new (Context) OMPThreadLimitClause(
13274 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013275}
Alexey Bataeva0569352015-12-01 10:17:31 +000013276
13277OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
13278 SourceLocation StartLoc,
13279 SourceLocation LParenLoc,
13280 SourceLocation EndLoc) {
13281 Expr *ValExpr = Priority;
13282
13283 // OpenMP [2.9.1, task Constrcut]
13284 // The priority-value is a non-negative numerical scalar expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000013285 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
Alexey Bataeva0569352015-12-01 10:17:31 +000013286 /*StrictlyPositive=*/false))
13287 return nullptr;
13288
13289 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
13290}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000013291
13292OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
13293 SourceLocation StartLoc,
13294 SourceLocation LParenLoc,
13295 SourceLocation EndLoc) {
13296 Expr *ValExpr = Grainsize;
13297
13298 // OpenMP [2.9.2, taskloop Constrcut]
13299 // The parameter of the grainsize clause must be a positive integer
13300 // expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000013301 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000013302 /*StrictlyPositive=*/true))
13303 return nullptr;
13304
13305 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
13306}
Alexey Bataev382967a2015-12-08 12:06:20 +000013307
13308OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
13309 SourceLocation StartLoc,
13310 SourceLocation LParenLoc,
13311 SourceLocation EndLoc) {
13312 Expr *ValExpr = NumTasks;
13313
13314 // OpenMP [2.9.2, taskloop Constrcut]
13315 // The parameter of the num_tasks clause must be a positive integer
13316 // expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000013317 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
Alexey Bataev382967a2015-12-08 12:06:20 +000013318 /*StrictlyPositive=*/true))
13319 return nullptr;
13320
13321 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
13322}
13323
Alexey Bataev28c75412015-12-15 08:19:24 +000013324OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
13325 SourceLocation LParenLoc,
13326 SourceLocation EndLoc) {
13327 // OpenMP [2.13.2, critical construct, Description]
13328 // ... where hint-expression is an integer constant expression that evaluates
13329 // to a valid lock hint.
13330 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
13331 if (HintExpr.isInvalid())
13332 return nullptr;
13333 return new (Context)
13334 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
13335}
13336
Carlo Bertollib4adf552016-01-15 18:50:31 +000013337OMPClause *Sema::ActOnOpenMPDistScheduleClause(
13338 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
13339 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
13340 SourceLocation EndLoc) {
13341 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
13342 std::string Values;
13343 Values += "'";
13344 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
13345 Values += "'";
13346 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
13347 << Values << getOpenMPClauseName(OMPC_dist_schedule);
13348 return nullptr;
13349 }
13350 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000013351 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000013352 if (ChunkSize) {
13353 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
13354 !ChunkSize->isInstantiationDependent() &&
13355 !ChunkSize->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000013356 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
Carlo Bertollib4adf552016-01-15 18:50:31 +000013357 ExprResult Val =
13358 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
13359 if (Val.isInvalid())
13360 return nullptr;
13361
13362 ValExpr = Val.get();
13363
13364 // OpenMP [2.7.1, Restrictions]
13365 // chunk_size must be a loop invariant integer expression with a positive
13366 // value.
13367 llvm::APSInt Result;
13368 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
13369 if (Result.isSigned() && !Result.isStrictlyPositive()) {
13370 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
13371 << "dist_schedule" << ChunkSize->getSourceRange();
13372 return nullptr;
13373 }
Alexey Bataev2ba67042017-11-28 21:11:44 +000013374 } else if (getOpenMPCaptureRegionForClause(
13375 DSAStack->getCurrentDirective(), OMPC_dist_schedule) !=
13376 OMPD_unknown &&
Alexey Bataevb46cdea2016-06-15 11:20:48 +000013377 !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000013378 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000013379 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev5a3af132016-03-29 08:58:54 +000013380 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13381 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000013382 }
13383 }
13384 }
13385
13386 return new (Context)
13387 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000013388 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000013389}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013390
13391OMPClause *Sema::ActOnOpenMPDefaultmapClause(
13392 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
13393 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
13394 SourceLocation KindLoc, SourceLocation EndLoc) {
13395 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
David Majnemer9d168222016-08-05 17:44:54 +000013396 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) {
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013397 std::string Value;
13398 SourceLocation Loc;
13399 Value += "'";
13400 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
13401 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000013402 OMPC_DEFAULTMAP_MODIFIER_tofrom);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013403 Loc = MLoc;
13404 } else {
13405 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000013406 OMPC_DEFAULTMAP_scalar);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013407 Loc = KindLoc;
13408 }
13409 Value += "'";
13410 Diag(Loc, diag::err_omp_unexpected_clause_value)
13411 << Value << getOpenMPClauseName(OMPC_defaultmap);
13412 return nullptr;
13413 }
Alexey Bataev2fd0cb22017-10-05 17:51:39 +000013414 DSAStack->setDefaultDMAToFromScalar(StartLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013415
13416 return new (Context)
13417 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
13418}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013419
13420bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
13421 DeclContext *CurLexicalContext = getCurLexicalContext();
13422 if (!CurLexicalContext->isFileContext() &&
13423 !CurLexicalContext->isExternCContext() &&
Alexey Bataev502ec492017-10-03 20:00:00 +000013424 !CurLexicalContext->isExternCXXContext() &&
13425 !isa<CXXRecordDecl>(CurLexicalContext) &&
13426 !isa<ClassTemplateDecl>(CurLexicalContext) &&
13427 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) &&
13428 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013429 Diag(Loc, diag::err_omp_region_not_file_context);
13430 return false;
13431 }
Kelvin Libc38e632018-09-10 02:07:09 +000013432 ++DeclareTargetNestingLevel;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013433 return true;
13434}
13435
13436void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
Kelvin Libc38e632018-09-10 02:07:09 +000013437 assert(DeclareTargetNestingLevel > 0 &&
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013438 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
Kelvin Libc38e632018-09-10 02:07:09 +000013439 --DeclareTargetNestingLevel;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013440}
13441
David Majnemer9d168222016-08-05 17:44:54 +000013442void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope,
13443 CXXScopeSpec &ScopeSpec,
13444 const DeclarationNameInfo &Id,
13445 OMPDeclareTargetDeclAttr::MapTypeTy MT,
13446 NamedDeclSetType &SameDirectiveDecls) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013447 LookupResult Lookup(*this, Id, LookupOrdinaryName);
13448 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
13449
13450 if (Lookup.isAmbiguous())
13451 return;
13452 Lookup.suppressDiagnostics();
13453
13454 if (!Lookup.isSingleResult()) {
13455 if (TypoCorrection Corrected =
13456 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
13457 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
13458 CTK_ErrorRecovery)) {
13459 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
13460 << Id.getName());
13461 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
13462 return;
13463 }
13464
13465 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
13466 return;
13467 }
13468
13469 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
Alexey Bataev30a78212018-09-11 13:59:10 +000013470 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND) ||
13471 isa<FunctionTemplateDecl>(ND)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013472 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
13473 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
Alexey Bataev30a78212018-09-11 13:59:10 +000013474 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
13475 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
13476 cast<ValueDecl>(ND));
13477 if (!Res) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013478 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013479 ND->addAttr(A);
13480 if (ASTMutationListener *ML = Context.getASTMutationListener())
13481 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
Kelvin Li1ce87c72017-12-12 20:08:12 +000013482 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Id.getLoc());
Alexey Bataev30a78212018-09-11 13:59:10 +000013483 } else if (*Res != MT) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013484 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
13485 << Id.getName();
13486 }
Alexey Bataeve3727102018-04-18 15:57:46 +000013487 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013488 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
Alexey Bataeve3727102018-04-18 15:57:46 +000013489 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013490}
13491
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013492static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
13493 Sema &SemaRef, Decl *D) {
Alexey Bataev30a78212018-09-11 13:59:10 +000013494 if (!D || !isa<VarDecl>(D))
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013495 return;
Alexey Bataev30a78212018-09-11 13:59:10 +000013496 auto *VD = cast<VarDecl>(D);
13497 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
13498 return;
13499 SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context);
13500 SemaRef.Diag(SL, diag::note_used_here) << SR;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013501}
13502
13503static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
13504 Sema &SemaRef, DSAStackTy *Stack,
13505 ValueDecl *VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013506 return VD->hasAttr<OMPDeclareTargetDeclAttr>() ||
13507 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(),
13508 /*FullCheck=*/false);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013509}
13510
Kelvin Li1ce87c72017-12-12 20:08:12 +000013511void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D,
13512 SourceLocation IdLoc) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013513 if (!D || D->isInvalidDecl())
13514 return;
13515 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
Stephen Kellyf2ceec42018-08-09 21:08:08 +000013516 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation();
Alexey Bataeve3727102018-04-18 15:57:46 +000013517 if (auto *VD = dyn_cast<VarDecl>(D)) {
Alexey Bataevc1943e72018-07-09 19:58:08 +000013518 // Only global variables can be marked as declare target.
Alexey Bataev30a78212018-09-11 13:59:10 +000013519 if (!VD->isFileVarDecl() && !VD->isStaticLocal() &&
13520 !VD->isStaticDataMember())
Alexey Bataevc1943e72018-07-09 19:58:08 +000013521 return;
13522 // 2.10.6: threadprivate variable cannot appear in a declare target
13523 // directive.
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013524 if (DSAStack->isThreadPrivate(VD)) {
13525 Diag(SL, diag::err_omp_threadprivate_in_target);
Alexey Bataeve3727102018-04-18 15:57:46 +000013526 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013527 return;
13528 }
13529 }
Alexey Bataev97b72212018-08-14 18:31:20 +000013530 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
13531 D = FTD->getTemplatedDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000013532 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Alexey Bataev30a78212018-09-11 13:59:10 +000013533 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
13534 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD);
13535 if (Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) {
Kelvin Li1ce87c72017-12-12 20:08:12 +000013536 assert(IdLoc.isValid() && "Source location is expected");
13537 Diag(IdLoc, diag::err_omp_function_in_link_clause);
13538 Diag(FD->getLocation(), diag::note_defined_here) << FD;
13539 return;
13540 }
13541 }
Alexey Bataev30a78212018-09-11 13:59:10 +000013542 if (auto *VD = dyn_cast<ValueDecl>(D)) {
13543 // Problem if any with var declared with incomplete type will be reported
13544 // as normal, so no need to check it here.
13545 if ((E || !VD->getType()->isIncompleteType()) &&
13546 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD))
13547 return;
13548 if (!E && !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) {
13549 // Checking declaration inside declare target region.
13550 if (isa<VarDecl>(D) || isa<FunctionDecl>(D) ||
13551 isa<FunctionTemplateDecl>(D)) {
13552 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
13553 Context, OMPDeclareTargetDeclAttr::MT_To);
13554 D->addAttr(A);
13555 if (ASTMutationListener *ML = Context.getASTMutationListener())
13556 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
13557 }
13558 return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013559 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013560 }
Alexey Bataev30a78212018-09-11 13:59:10 +000013561 if (!E)
13562 return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013563 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
13564}
Samuel Antao661c0902016-05-26 17:39:58 +000013565
13566OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
13567 SourceLocation StartLoc,
13568 SourceLocation LParenLoc,
13569 SourceLocation EndLoc) {
13570 MappableVarListInfo MVLI(VarList);
13571 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc);
13572 if (MVLI.ProcessedVarList.empty())
13573 return nullptr;
13574
13575 return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13576 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
13577 MVLI.VarComponents);
13578}
Samuel Antaoec172c62016-05-26 17:49:04 +000013579
13580OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
13581 SourceLocation StartLoc,
13582 SourceLocation LParenLoc,
13583 SourceLocation EndLoc) {
13584 MappableVarListInfo MVLI(VarList);
13585 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc);
13586 if (MVLI.ProcessedVarList.empty())
13587 return nullptr;
13588
13589 return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13590 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
13591 MVLI.VarComponents);
13592}
Carlo Bertolli2404b172016-07-13 15:37:16 +000013593
13594OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
13595 SourceLocation StartLoc,
13596 SourceLocation LParenLoc,
13597 SourceLocation EndLoc) {
Samuel Antaocc10b852016-07-28 14:23:26 +000013598 MappableVarListInfo MVLI(VarList);
13599 SmallVector<Expr *, 8> PrivateCopies;
13600 SmallVector<Expr *, 8> Inits;
13601
Alexey Bataeve3727102018-04-18 15:57:46 +000013602 for (Expr *RefExpr : VarList) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000013603 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
13604 SourceLocation ELoc;
13605 SourceRange ERange;
13606 Expr *SimpleRefExpr = RefExpr;
13607 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13608 if (Res.second) {
13609 // It will be analyzed later.
Samuel Antaocc10b852016-07-28 14:23:26 +000013610 MVLI.ProcessedVarList.push_back(RefExpr);
13611 PrivateCopies.push_back(nullptr);
13612 Inits.push_back(nullptr);
Carlo Bertolli2404b172016-07-13 15:37:16 +000013613 }
13614 ValueDecl *D = Res.first;
13615 if (!D)
13616 continue;
13617
13618 QualType Type = D->getType();
Samuel Antaocc10b852016-07-28 14:23:26 +000013619 Type = Type.getNonReferenceType().getUnqualifiedType();
13620
13621 auto *VD = dyn_cast<VarDecl>(D);
13622
13623 // Item should be a pointer or reference to pointer.
13624 if (!Type->isPointerType()) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000013625 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
13626 << 0 << RefExpr->getSourceRange();
13627 continue;
13628 }
Samuel Antaocc10b852016-07-28 14:23:26 +000013629
13630 // Build the private variable and the expression that refers to it.
Alexey Bataev63cc8e92018-03-20 14:45:59 +000013631 auto VDPrivate =
13632 buildVarDecl(*this, ELoc, Type, D->getName(),
13633 D->hasAttrs() ? &D->getAttrs() : nullptr,
13634 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Samuel Antaocc10b852016-07-28 14:23:26 +000013635 if (VDPrivate->isInvalidDecl())
13636 continue;
13637
13638 CurContext->addDecl(VDPrivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000013639 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Samuel Antaocc10b852016-07-28 14:23:26 +000013640 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
13641
13642 // Add temporary variable to initialize the private copy of the pointer.
Alexey Bataeve3727102018-04-18 15:57:46 +000013643 VarDecl *VDInit =
Samuel Antaocc10b852016-07-28 14:23:26 +000013644 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
Alexey Bataeve3727102018-04-18 15:57:46 +000013645 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr(
13646 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc());
Samuel Antaocc10b852016-07-28 14:23:26 +000013647 AddInitializerToDecl(VDPrivate,
13648 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000013649 /*DirectInit=*/false);
Samuel Antaocc10b852016-07-28 14:23:26 +000013650
13651 // If required, build a capture to implement the privatization initialized
13652 // with the current list item value.
13653 DeclRefExpr *Ref = nullptr;
13654 if (!VD)
13655 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
13656 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
13657 PrivateCopies.push_back(VDPrivateRefExpr);
13658 Inits.push_back(VDInitRefExpr);
13659
13660 // We need to add a data sharing attribute for this variable to make sure it
13661 // is correctly captured. A variable that shows up in a use_device_ptr has
13662 // similar properties of a first private variable.
13663 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
13664
13665 // Create a mappable component for the list item. List items in this clause
13666 // only need a component.
13667 MVLI.VarBaseDeclarations.push_back(D);
13668 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13669 MVLI.VarComponents.back().push_back(
13670 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D));
Carlo Bertolli2404b172016-07-13 15:37:16 +000013671 }
13672
Samuel Antaocc10b852016-07-28 14:23:26 +000013673 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli2404b172016-07-13 15:37:16 +000013674 return nullptr;
13675
Samuel Antaocc10b852016-07-28 14:23:26 +000013676 return OMPUseDevicePtrClause::Create(
13677 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
13678 PrivateCopies, Inits, MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli2404b172016-07-13 15:37:16 +000013679}
Carlo Bertolli70594e92016-07-13 17:16:49 +000013680
13681OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
13682 SourceLocation StartLoc,
13683 SourceLocation LParenLoc,
13684 SourceLocation EndLoc) {
Samuel Antao6890b092016-07-28 14:25:09 +000013685 MappableVarListInfo MVLI(VarList);
Alexey Bataeve3727102018-04-18 15:57:46 +000013686 for (Expr *RefExpr : VarList) {
Kelvin Li84376252016-12-14 15:39:58 +000013687 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause.");
Carlo Bertolli70594e92016-07-13 17:16:49 +000013688 SourceLocation ELoc;
13689 SourceRange ERange;
13690 Expr *SimpleRefExpr = RefExpr;
13691 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13692 if (Res.second) {
13693 // It will be analyzed later.
Samuel Antao6890b092016-07-28 14:25:09 +000013694 MVLI.ProcessedVarList.push_back(RefExpr);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013695 }
13696 ValueDecl *D = Res.first;
13697 if (!D)
13698 continue;
13699
13700 QualType Type = D->getType();
13701 // item should be a pointer or array or reference to pointer or array
13702 if (!Type.getNonReferenceType()->isPointerType() &&
13703 !Type.getNonReferenceType()->isArrayType()) {
13704 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
13705 << 0 << RefExpr->getSourceRange();
13706 continue;
13707 }
Samuel Antao6890b092016-07-28 14:25:09 +000013708
13709 // Check if the declaration in the clause does not show up in any data
13710 // sharing attribute.
Alexey Bataeve3727102018-04-18 15:57:46 +000013711 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Samuel Antao6890b092016-07-28 14:25:09 +000013712 if (isOpenMPPrivate(DVar.CKind)) {
13713 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
13714 << getOpenMPClauseName(DVar.CKind)
13715 << getOpenMPClauseName(OMPC_is_device_ptr)
13716 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000013717 reportOriginalDsa(*this, DSAStack, D, DVar);
Samuel Antao6890b092016-07-28 14:25:09 +000013718 continue;
13719 }
13720
Alexey Bataeve3727102018-04-18 15:57:46 +000013721 const Expr *ConflictExpr;
Samuel Antao6890b092016-07-28 14:25:09 +000013722 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +000013723 D, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +000013724 [&ConflictExpr](
13725 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
13726 OpenMPClauseKind) -> bool {
13727 ConflictExpr = R.front().getAssociatedExpression();
13728 return true;
13729 })) {
13730 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
13731 Diag(ConflictExpr->getExprLoc(), diag::note_used_here)
13732 << ConflictExpr->getSourceRange();
13733 continue;
13734 }
13735
13736 // Store the components in the stack so that they can be used to check
13737 // against other clauses later on.
13738 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D);
13739 DSAStack->addMappableExpressionComponents(
13740 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
13741
13742 // Record the expression we've just processed.
13743 MVLI.ProcessedVarList.push_back(SimpleRefExpr);
13744
13745 // Create a mappable component for the list item. List items in this clause
13746 // only need a component. We use a null declaration to signal fields in
13747 // 'this'.
13748 assert((isa<DeclRefExpr>(SimpleRefExpr) ||
13749 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
13750 "Unexpected device pointer expression!");
13751 MVLI.VarBaseDeclarations.push_back(
13752 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr);
13753 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13754 MVLI.VarComponents.back().push_back(MC);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013755 }
13756
Samuel Antao6890b092016-07-28 14:25:09 +000013757 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli70594e92016-07-13 17:16:49 +000013758 return nullptr;
13759
Samuel Antao6890b092016-07-28 14:25:09 +000013760 return OMPIsDevicePtrClause::Create(
13761 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
13762 MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013763}